Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Artificial Intelligence CS370D Prolog programming Declarative meaning of Prolog programs and Lists representation.

Similar presentations


Presentation on theme: "1 Artificial Intelligence CS370D Prolog programming Declarative meaning of Prolog programs and Lists representation."— Presentation transcript:

1 1 Artificial Intelligence CS370D Prolog programming Declarative meaning of Prolog programs and Lists representation.

2 Outline: Declarative meaning of Prolog programs. Examples of conjunction. Examples of disjunction. List. Exemples of lists. List representation. List representation examples. 2

3 3 Declarative meaning of Prolog programs: AND operator: More than one relation can be included as the “goal” of a query. A comma (“,”) is used as an AND operator to indicate a conjunction of goals—all must be satisfied by a solution to the query. OR operator: Prolog also accepts the disjunction (OR) of goals (“ ; ”). Any one of the goals in a disjunction has to be true.

4 4 Example of conjunction: following facts which define “fatherOf” and “motherOf” relations. fatherOf(ahmed,salih). fatherOf(salih,ali). fatherOf(sara,ali). motherOf(ahmed,judy). motherOf(salih,maryam). motherOf(sara,maryam). The symbols fatherOf and motherOf are predicates. The symbols ahmed, salih, ali, judy, maryam and sara are atoms.

5 5 Example of conjunction:(cont) Questions: 1- fatherOf(sara,X),motherOf(sara,Y). X = ali, Y = maryam ; no 2- fatherOf(ahmed,X),fatherOf(X,ali). X = salih ; no

6 6 Example of disjunction: 1) happy1(X) :- rich(X) ; famous(X). 2) happy2(X) :- attractive(X), ( rich(X) ; famous(X) ). 3) A:- (B, C); (D, E, F).

7 7 List: A list is written as a sequence of values, known as list elements, separated by commas and enclosed in square brackets. e.g. [dog,cat,fish]. A list element does not have to be an atom. It can be any Prolog term, including variable or another list. e.g:[X,Y,[dog,cat,fish],elephant]. A list element that is itself a list is known as a sublist like [dog,cat,fish].

8 8 (cont.) List: Lists can have any number of elements, including zero. The list with no elements is known as the empty list and is written as []. For non-empty lists, the first element is known as the head. The list remaining after the first element is removed is called the tail. The notation [H|T] represents a list with H matching the head of the list and T matching the rest of the list. The head can be any Prolog object and The tail has to be list. For example: the head of the list [dog,cat,fish] is the atom dog and the tail is the list [cat,fish].

9 Exemples of lists: 9

10 List representation: The head and the tail can be combined into a structure by a special functor ‘. ’.(Head,Tail) The tail is a list, it is either empty or it has its own head and tail. The previous list can be represented as the term:.(dog,.(cat,.(fish,.(elephant, [])))) 10

11 List representation:(cont) The element of a list can be object of any kind; it can be also list e.g L= [a,b,c] can be written as: Tail=[b,c] and then L=.(a, Tail) To express it in the square bracket notation we can use vertical bar that separate the head and tail: L=[a|Tail] Alternative ways of writing the list L are: [a,b,c]=[a|[b,c]]=[a,b|[c]]=[a,b,c|[]] 11

12 List representation examples: ?- L1=[1,2,3],L2=.(1,[2,3]). L1= [1,2,3] L2=[1,2,3] ?- Cities1=.(london,.(tokyo,[])), Cities2=[paris,madrid], L=[ann,cities1,tom,cities2]. Cities1=[london,tokyo] Cities2=[paris,madrid] L=[ann,[london,tokyo],tom,[paris,madrid]] 12

13 List representation examples: Given the fact p([1,2,3,4]). ?- p([X|Y]). Answer: X = 1, Y = [2,3,4] ?- p([_,_,X|Y]). Answer: X = 3, Y = [4] ?- Tail=[c,d,e], L=[a|Tail]. Answer: Tail = [c, d, e], L = [a, c, d, e] 13


Download ppt "1 Artificial Intelligence CS370D Prolog programming Declarative meaning of Prolog programs and Lists representation."

Similar presentations


Ads by Google