Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 TP #2: How does Prolog answer questions? n Miscellaneous info; n Last TP exercises solved; n How does Prolog answer questions? n Recursive Prolog programs;

Similar presentations


Presentation on theme: "1 TP #2: How does Prolog answer questions? n Miscellaneous info; n Last TP exercises solved; n How does Prolog answer questions? n Recursive Prolog programs;"— Presentation transcript:

1 1 TP #2: How does Prolog answer questions? n Miscellaneous info; n Last TP exercises solved; n How does Prolog answer questions? n Recursive Prolog programs; n Exercise

2 © L.Lúcio, 2003 2 Miscellaneous Info n IMPORTANT: The exercices for the TPs can be downloaded from http://cui.unige.ch/smv/teaching/li/TP_exercises.html http://cui.unige.ch/smv/teaching/li/TP_exercises.html n Comments can be added in prolog using the « /* …… */ » notation: /* piece of code about animals */ habitat(bear,forest). habitat(elephant,savanah). habitat(cat,house). n If you want to add a (dynamic) fact or rule without using the source text file, use the assert predicate: n Example: assert(habitat(whale,sea)). n If you want to remove a (dynamic) fact or rule from the program (already in the interpreter), use the retract predicate: n Example: retract(habitat(whale,sea)).

3 © L.Lúcio, 2003 3 Last TP exercises solved n 1. The « livesin » relation: livesin(italy,mario). livesin(france,jean). livesin(portugal,maria). … n 1.1. “Who are the people that live in Switzerland?” ?- livesin(switzerland,X). n Download the solution from http://cui.unige.ch/smv/teaching/li/material/transparencies/tp/4_11_03/ and load it into the interpreter. http://cui.unige.ch/smv/teaching/li/material/transparencies/tp/4_11_03/

4 © L.Lúcio, 2003 4 Last TP exercises solved (cont) n 2.1. The nationality rules: italian(X) :- wasborn(italy,X). swiss(X) :- wasborn(switzerland,X). portuguese(X) :- wasborn(portugal,X). … 3. The immigrant rule : ?- immigrant(X) :- livesin(Y,X),wasborn(Z,X),Y\==Z. n 2. The « wasborn » relation: wasborn(italy,mario). wasborn(switzerland,jean). wasborn(portugal,maria). …

5 © L.Lúcio, 2003 5 Last TP exercises solved (cont) n 4. “Give me the names of all the french immigrants in England”: ?- wasborn(france,X),livesin(england,X). or ?- french(X),livesin(england,X).

6 © L.Lúcio, 2003 6 How does Prolog answer questions? n Taking the immigrant example from the TP: immigrant(fabien) … livesin(england,carry). livesin(switzerland,fabien). livesin(england,veronique). … wasborn(england,carry). wasborn(france,fabien). wasborn(france,veronique). … immigrant(X) :- livesin(Y,X),wasborn(Z,X),Y\==Z. … TRUE livesin(Y,fabien),wasborn(Z,fabien),Y\==Z X = fabien (unification) wasborn(Z,fabien),switzerland\==Z Y = switzerland switzerland\==france Z = france

7 © L.Lúcio, 2003 7 How does Prolog answer questions? (cont) n Prolog tries to prove the theorem (question) that the user entered. In fact Prolog it is a theorem prover, with axioms (facts) and rules. n It is possible to follow the mechanism of Prolog by enabling the « trace » mode. It displays in text mode the search tree: ?- trace. Yes ?- immigrant(fabien). Call: (7) immigrant(fabien) ? creep Call: (8) livesin(_G478, fabien) ? creep Exit: (8) livesin(switzerland, fabien) ? creep Call: (8) wasborn(_G478, fabien) ? creep Exit: (8) wasborn(france, fabien) ? creep Call: (8) switzerland\==france ? creep Exit: (8) switzerland\==france ? creep Exit: (7) immigrant(fabien) ? creep Yes Temporary variables created by the interpreter!

8 © L.Lúcio, 2003 8 How does Prolog answer questions? (cont) n Now taking the example from question 4, let’s assume we want to write a rule that will print (on the screen, to a file) all the immigrants of nationality X that live in country Y: First approach: all_immigrants(X,Y) :- wasborn(X,Z),livesin(Y,Z),print(Z) n « print » is a predefined predicate in Prolog, like « \== » n Why doesn’t this first approach work and it only gives one answer? Pierre is also a french immigrant in England!… ?- all_immigrants(france,england). veronique Yes.

9 © L.Lúcio, 2003 9 How does Prolog answer questions? (cont) n In fact the answer is correct. What we asked for was a proof that the fact all_immigrants(france,england) is true, and Prolog was able to prove it; n The real question we asked was « Is there at least one french person living in England? » n If we want to get Prolog to tell us all the results for which the proof is possible, we have to use one more variable: all_immigrants(X,Y,Z) :- wasborn(X,Z),livesin(Y,Z),print(Z)

10 © L.Lúcio, 2003 10 How does Prolog answer questions? (cont) n Prolog tells us it was able to find two instances of X for which the fact all_immigrants(France,england,X) exists: « veronique » and « pierre »; n Try to use « trace » to check how Prolog finds the answers for the question. ?- all_immigrants(france,england,X). veronique X = veronique ; pierre X = pierre ; No

11 © L.Lúcio, 2003 11 Recursive Prolog programs n What is a recursive program? n Example: given a family tree using the relation « parentof », how can we define a rule that says whether someone is a predecessor of somebody else? n A predecessor of somebody is either a direct parent… or n There is a chain of parents between the predecessor and that person. n How do we express this in Prolog? parent(john,clara). parent(clara,mike). parent(mike,maria). JohnClaraMikeMaria predecessor

12 © L.Lúcio, 2003 12 Recursive Prolog programs (cont) n The program is recursive because it uses itself in order to be able to calculate the result; n There has to be a STOP condition (otherwise the program will call itself forever). n Recursion is the way LOOPS are made in Prolog! predecessor(X,Y) :- parent(X,Y). predecessor(X,Y) :- parent(X,Z),predecessor(Z,Y). n EXERCISE: How would we change the program to check if somebody is a descendant of somebody else? (use the predecessor program at: http://cui.unige.ch/smv/teaching/li/material/transparencies/tp/4_11_03/ ) http://cui.unige.ch/smv/teaching/li/material/transparencies/tp/4_11_03/


Download ppt "1 TP #2: How does Prolog answer questions? n Miscellaneous info; n Last TP exercises solved; n How does Prolog answer questions? n Recursive Prolog programs;"

Similar presentations


Ads by Google