Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 TP #3:List and Database manipulation n Last TP exercises solved; n Lists and their predefined predicates; n Prolog as a Database: predefined predicates.

Similar presentations


Presentation on theme: "1 TP #3:List and Database manipulation n Last TP exercises solved; n Lists and their predefined predicates; n Prolog as a Database: predefined predicates."— Presentation transcript:

1 1 TP #3:List and Database manipulation n Last TP exercises solved; n Lists and their predefined predicates; n Prolog as a Database: predefined predicates.

2 © L.Lúcio, 2003 2 Last TP exercises solved n Ex.1. Trace the « predecessor(joe,clara) » goal in the predecessor program. parent(john,clara). parent(clara,mike). parent(mike,maria). parent(joe,kim). predecessor(X,Y) :- parent(X,Y). predecessor(X,Y) :- parent(X,Z),predecessor(Z,Y). predecessor(joe,clara) parent(joe,clara) False parent(joe,Z), predecessor(Z,clara) parent(joe,kim)predecessor(kim,clara) parent(kim,clara) False parent(kim,Z1), predecessor(Z1,clara) parent(kim,Z1) False Z = kim

3 © L.Lúcio, 2003 3 Last TP exercises solved (cont) n Ex.2. Trace the « factorial(3) » goal in the factorial program. factorial(0,1). factorial(Y,X) :- Y>0, Y1 is Y-1, factorial(Y1,X1), X is Y*X1. factorial(3,X) 3 > 02 is 3-1factorial(2,X1)6 is 3*2 2 > 01 is 2-1factorial(1,X2)2 is 2*1 1 > 00 is 1-1factorial(0,X3)1 is 1*1 factorial(0,1) X3 = 1 Tru e

4 © L.Lúcio, 2003 4 Lists n We are now leaving the « pure » logic aspects of Prolog and going into Prolog programming itself; n Some of the predefined Prolog predicates will be taught, others not! Use the Prolog help, the recommended book or the online manual available for reference… n A List is a sequence of any number of items, e.g.: [bottle, coffee, car, John, fishing] [ ] - The empty list

5 © L.Lúcio, 2003 5 Lists (cont) n A list can be seen either as empty ([ ]) or as a sequence of elements consisting of a head and tail; n The head can be any Prolog object (atom, variable, term, another list); n The tail is a list of Prolog objects; n Syntax example: [bottle, coffee, car, John, fishing] HeadTail ?- L=[ann|[mike,john]]. L = [ann, mike, john] Yes Use the « | » operator to concatenate head and tail

6 © L.Lúcio, 2003 6 Predefined predicates for Lists n The member operation returns true if the given element is member of a list. ?- member(a,[a,b,c]). Yes ?- member(d,[a,b,c]). No n The member operation is defined as follows: member(X,[X|Tail]). member(X,[Head|Tail]) :- member(X,Tail).

7 © L.Lúcio, 2003 7 Predefined predicates for Lists (cont) n append: defines the concatenation of two lists; ?- append([a,b],[1,2],List). List = [a, b, 1, 2] Yes ?- append([X,Y],[1,likes(e,f)],[a,b,1,likes(e,f)]). X = a Y = b Yes n The append operation is defined as follows: append([],L,L). append([X|L1],L2,[X|L3]) :- append(L1,L2,L3).

8 © L.Lúcio, 2003 8 Exercises n There are many more list predicates defined in the SWI Prolog libraries. While doing the TP practical exercises, use them as much as you can instead of redefining every list operation. 1. Write a relation that will retrieve the last element of a list: n Example: my_last(X,[a,b,c,d]) … X=d 2. Write a relation that will give the length of a list: n Example: my_length(X,[e,f,g(h,f)]) … X=3 3. Write a relation that will reverse a list: n Example: my_reverse(X,[a,b,c,d]) … X=[d,c,b,a]

9 © L.Lúcio, 2003 9 Solutions 1. my_last: my_last(X,[X|[]]). my_last(X,[Head|Tail]):- my_last(X,Tail). 2. my_length: my_length(0,[]). my_length(X,[Head|Tail]):- my_length(X1,Tail), X is X1+1.

10 © L.Lúcio, 2003 10 Solutions (cont) 3. my_reverse: my_reverse(List,Reversed) :- my_reverse(List,[],Reversed). my_reverse([],Reversed,Reversed). my_reverse([Head|Tail], SoFar, Reversed) :- my_reverse(Tail, [Head|SoFar], Reversed).

11 © L.Lúcio, 2003 11 Prolog as a Database n In a way Prolog can be seen as a relational database, since: everything can be seen as a relation; we have the operators to manipulate those relations; n In SWI Prolog there are two sorts of relations: u Static: the ones that are read from a file by « consult » and that are not defined as dynamic; u Dynamic: the ones that are either input by « assert » or that are defined in a file as dynamic; n It is possible to add in run-time new facts to a relation that is defined as dynamic; n It is NOT possible to add in run-time new facts to a relation that is defined as static.

12 © L.Lúcio, 2003 12 Prolog as a Database (cont) /* immigrants.pl */ :- dynamic wasborn/2 :- dynamic livesin/2 wasborn(rui,portugal). wasborn(jo,germany). ?- consult(‘immigrants.pl’). Yes ?- wasborn(mark,england). No ?- assert(wasborn(mark,england)). Yes ?- wasborn(mark,england). Yes n SWI Prolog provides several predefined predicates for fact database manipulation: u assert: adds a fact to the database; u asserta: adds a fact to the beginning of the database; u assertz: adds a fact to the end of the database (same as assert); u retract: removes a fact from the database.

13 © L.Lúcio, 2003 13 Prolog as a Database – an example maketable :- L=[0,1,2,3,4,5,6,7,8,9], member(X,L), member(Y,L), Z is X*Y, assert(product(X,Y,Z)), fail. ?- maketable. No ?- listing(product). :- dynamic product/3. product(0, 0, 0). product(0, 1, 0). product(0, 2, 0). product(0, 3, 0). product(0, 4, 0). product(0, 5, 0). product(0, 6, 0). product(0, 7, 0). product(0, 8, 0). product(0, 9, 0). product(1, 0, 0). product(1, 1, 1). product(1, 2, 2). product(1, 3, 3). product(1, 4, 4). product(1, 5, 5). product(1, 6, 6). … n Questions: u Why does the program reply « no »? u What is the « fail » doing there?


Download ppt "1 TP #3:List and Database manipulation n Last TP exercises solved; n Lists and their predefined predicates; n Prolog as a Database: predefined predicates."

Similar presentations


Ads by Google