Presentation is loading. Please wait.

Presentation is loading. Please wait.

Introduction to Prolog Proof Procedures. Exercise parent(mark, alex). parent(di, alex). brother(brian, mark). sister(cathy, di). wife(susan, brian). husband(brad,

Similar presentations


Presentation on theme: "Introduction to Prolog Proof Procedures. Exercise parent(mark, alex). parent(di, alex). brother(brian, mark). sister(cathy, di). wife(susan, brian). husband(brad,"— Presentation transcript:

1 Introduction to Prolog Proof Procedures

2 Exercise parent(mark, alex). parent(di, alex). brother(brian, mark). sister(cathy, di). wife(susan, brian). husband(brad, cathy). n Who are Alex’s aunts? n Write a definition of aunt/2 –aunt(Aunt, NorN).

3 Recursion n Predicates may be recursive –Needs a base case – fact or non-recursive rule ancestor(Anc, Desc) :- parent(Anc, Desc). ancestor(Anc, Desc) :- parent(Parent, Desc), ancestor(Anc, Parent). n Usually write base case first – more later Base case: your parent is your ancestor Recursive case: an ancestor of your parent is your ancestor

4 Rule Diagrams n Each diagram a clause n Note recursion –Needs base case D A parent ancestor D A P parent ancestor

5 Alex’s Ancestors parent(mark, alex). parent(bob, mark). parent(franklin, bob). parent(garvie, franklin). ancestor(A, D) :- parent(A, D). ancestor(A, D) :- parent(P, D), ancestor(A, P). ?- ancestor(Who, alex). Who = mark ; Who = bob ; Who = franklin ; Who = garvie ; no

6 How Prolog Answers Questions n Prolog answers questions in order –first question first, then second, then third n Prolog uses facts/rules in order –first fact/rule first, then second, then third n Prolog uses recursion for rules –body = set of rules –answered in order to get answer for head

7 Asking About Grandparents ?- grandparent(tom, GC). n This is the main question –AKA “top-level goal” n The question Prolog prints answers to n Prolog needs to look for answers n Starts looking for clauses for grandparent/2

8 Goal #1 looking for grandparent(tom, GC) grandparent(X, Z) :-match: X=tom, Z=GC parent(X, Y), parent(Y, Z). creates sub-goals: parent(tom, Y), parent(Y, GC). Database facts & rules Commentary

9 Sub-Goaling n Found a rule for grandfather/2 –head of rule matches the question n Replace grandfather question with rule body –variables filled in from main question n Sub-questions answered in order –parent(tom, Y) first –parent(Y, GC) second

10 Sub-Goal #1 looking for parent(tom, Y) parent(pam, bob).pam  tom parent(tom, bob).match: bob = Y sub-goal #1 succeeds sub-goal #2 next parent(Y, GC) becomes parent(bob, GC)

11 Sub-Goal #2 looking for parent(bob, GC) parent(pam, bob).pam  bob parent(tom, bob).tom  bob parent(tom, liz).tom  bob parent(bob, ann).match: ann = GC sub-goal #2 succeeds ends sub-goaling, return to main question

12 Goal #1 (review) looking for grandparent(tom, GC) grandparent(X, Z) :-match: X=tom, Z=GC parent(X, Y),matched: Y=bob parent(Y, Z).matched: Z=GC=ann main goal succeeds

13 Top-Level ?- grandparent(tom, GC). GC = ann n Ann is one of tom’s grandchildren n Prolog waiting for user action n Let’s type ; –see if there are more answers

14 More Answers n If you ask for more answers, Prolog “backs up” and tries again –called “backtracking” n Go back to last question asked & see if it has a different answer –last question was parent(bob, GC) –answer was GC = ann

15 Sub-Goal #2: Backtracking parent(bob, pat).2 nd match: pat = GC sub-goal #2 succeeds again looking for parent(bob, GC) parent(pam, bob).pam  bob parent(tom, bob).tom  bob parent(tom, liz).tom  bob parent(bob, ann).1 st match: ann = GC

16 Goal #1: 2 nd Solution looking for grandparent(tom, GC) grandparent(X, Z) :-match: X=tom, Z=GC parent(X, Y),matched: Y=bob parent(Y, Z).matched: Z=GC=pat main goal succeeds again

17 Top-Level ?- grandparent(tom, GC). GC = ann ; GC = pat n Let’s see if there are any more answers n Once again back to the last question

18 Sub-Goal #2: Backtracking parent(pat, jim).pat  bob no more answers looking for parent(bob, GC) parent(pam, bob).pam  bob parent(tom, bob).tom  bob parent(tom, liz).tom  bob parent(bob, ann).1 st match: ann = GC parent(bob, pat).2 nd match: pat = GC

19 Backtracking n 2 nd sub-goal has no more answers n But Prolog’s not done –maybe the 1 st sub-goal has more answers

20 Sub-Goal #1: Backtracking parent(tom, liz).2 nd match: liz = Y sub-goal #1 succeeds again redo sub-goal #2 using new binding parent(liz, GC) looking for parent(tom, Y) parent(pam, bob).pam  tom parent(tom, bob).1 st match: bob = Y

21 Sub-Goal #2 looking for parent(liz, GC) parent(pam, bob).pam  liz parent(tom, bob).tom  liz parent(tom, liz).tom  liz parent(bob, ann).bob  liz parent(bob, pat).bob  liz parent(pat, jim).pat  liz no answers

22 Backtracking n Liz has no children at all –2 nd answer for 1 st sub-goal got us nothing n But don’t give up –maybe Tom has more children

23 Sub-Goal #1: Backtracking parent(bob, ann).bob  tom parent(bob, pat).bob  tom parent(pat, jim).pat  tom no more answers looking for parent(tom, Y) parent(pam, bob).pam  tom parent(tom, bob).1 st match: bob = Y parent(tom, liz).2 nd match: liz = Y

24 Backtracking n 1 st sub-goal had no more answers n Prolog returns to main goal looking for grandparent(tom, GC) grandparent(X, Z) :-1 st answer parent(X, Y), parent(Y, Z). no more facts or rules for grandparent/2

25 Top-Level n No more answers –and nowhere else to look ?- grandparent(tom, GC). GC = ann ; GC = pat ; No?-

26 Prolog Normal Flow of Control n Things done in sequence n If reach an impasse… –no answer found or no more answers found n …backtrack = back up –just follow code in reverse n “Depth first search” n There are ways to modify this – more later

27 Program Tracing n You can have Prolog trace its work ?- trace, grandparent(tom, GC). n Shows you what it’s doing as it does it Call: (7) grandparent(tom, _G301) ? creep Call: (8) parent(tom, _G371) ? creep Exit: (8) parent(tom, bob) ? creep Call: (8) parent(bob, _G301) ? just press Enter

28 Tracing “Ports” n Call port = when question first asked n Exit port = answer found n Redo port = looking for more answers n Fail port = no (more) answers found n (Number) = how deep –first 6 levels belong to the system

29 Trace of grandparent/2 ?- grandparent(tom, GC). call grandparent(tom, _G_1) call parent(tom, _G_2) exit parent(tom, bob) call parent(bob, _G_3) exit parent(bob, ann) exit grandparent(tom, ann) GC = ann ; find tom’s kids find bob’s kids

30 Trace of grandparent/2 redo grandparent(tom, ann) redo parent(bob, ann) exit parent(bob, pat) exit grandparent(tom, pat) GC = pat ; more of bob’s kids

31 Trace of grandparent/2 redo grandparent(tom, pat) redo parent(bob, pat) fail parent(bob, _G_3) redo parent(tom, bob) exit parent(tom, liz) call parent(liz, _G_4) fail parent(liz, _G_4) redo parent(tom, _G_2) fail parent(tom, _G_2) redo grandparent(tom, _G_1) fail grandparent(tom, _G_1) more of bob’s kids more of tom’s kids find liz’s kids more of tom’s kids more rules for grandparents?

32 Trace Prolog’s Work… mother(di, alex). mother(di, zachary). father(ralph, di). mother(esther, di). parent(P, C) :- father(P, C). parent(P, C) :- mother(P, C). ?- parent(ralph, Who), parent(Who, GC). (show all answers) use call, exit, redo, fail

33 Proof and Recursion n All variables are local n Different clauses = different variables n Recursive calls: different variables ancestor(A, D) :- parent(A, P), ancestor(P, D). ?- ancestor(tom, jim). [A 1 = tom, D 1 = jim, P 1 = bob] ?- ancestor(bob, jim) [A 2 = bob, D 2 = jim, P 2 = ann] ?- ancestor(ann, jim) [A 3 = ann, D 3 = jim] (fails)

34 Tracing Actions n Press enter/space to “creep” thru code n Press s to skip sub-goals for this question –when you’re not interested in how it works n Press n to stop tracing –or use notrace/0 at the prompt n Press ? for more options –find out about spy points

35 Infinite Loops n Because of Prolog’s simple-minded control, it’s easy to write infinite loops ancestor(X, Z) :- ancestor(X, Y), parent(Y, Z). ancestor(X, Z) :- parent(X, Z). ?- ancestor(tom, D). If Z is a variable when this matches Then 1 st sub-goal same as main goal Makes no progress Infinite loop May depend on exact question asked

36 Avoiding Infinite Loops n Write base case first –Prolog looks for non-recursive answers ancestor(A, D) :- parent(A, D). n Leave recursion to end of recursive body –Gather as much information as you can before you recur ancestor(A, D) :- parent(P, D), ancestor(A, P).

37 Exercise n Rewrite the predicate below to make it less likely to go into an infinite loop something(A, B) :- something(A, C), elfin(C, B, no). something(A, B) :- basic(B, C, A), multiple(C, B).

38 Brothers and Sisters n Sister = same parent(s) and female sister(Sister, Person) :- parent(Parent, Sister), parent(Parent, Person), female(Sister). n Note: better variable names than in the text n Note how we say they have the same parent –just use same variable for “both” parents

39 Tracing Sister/2 ?- sister(Who, pat). parent(Parent,Who), parent(Parent,pat), female(Who) parent(pam,bob), parent(pam,pat) (fails) parent(tom,bob), parent(tom,pat) (fails) parent(tom,liz), parent(tom,pat) (fails) parent(bob,ann), parent(bob,pat), female(ann). Who = ann ;

40 Tracing Sister/2 ?- sister(Who, pat). parent(Parent,Who), parent(Parent,pat), female(Who) parent(pam,bob), parent(pam,pat) (fails) parent(tom,bob), parent(tom,pat) (fails) parent(tom,liz), parent(liz,pat) (fails) parent(bob,ann), parent(bob,pat), female(ann). Who = ann ; parent(bob,pat), parent(bob,pat), female(pat). Who = pat

41 Pat Is Not Her Own Sister n Sister to Person must be a different person –how can we say that? –text uses different/2 –not actually defined different(X, Y) :- X \= Y. –the “not equals” predicate/operator –means “could not be the same”

42 Sisters (Revised) n Sister = same parent(s), female, different sister(Sister, Person) :- parent(Parent, Sister), parent(Parent, Person), female(Sister), different(Sister, Person). n Last line could just use \= Sister \= Person.

43 Tracing Revised Sister/2 ?- sister(W, pat). parent(P,W), parent(P,pat), female(W),W \= pat parent(pam,bob), parent(pam,pat) (fails) parent(tom,bob), parent(tom,pat) (fails) parent(tom,liz), parent(liz,pat) (fails) parent(bob,ann),parent(bob,pat),female(ann),ann\=pat. Who = ann ; parent(bob,pat),parent(bob,pat),female(pat),pat\=pat (fails). parent(pat,jim), parent(pat,pat) (fails)

44 Inequality & Variables n \= means “could not be the same” n But a variable can be the same as anything n So, \= fails if either argument is unbound ?- Who \= pat. No ?- Who \= Whom. No ?- Who = bob, Who \= pat. Who = bob “unbound” means: doesn’t have a value yet =/2 means “they are the same” not usually needed

45 Inequality and Proof n Recall that sub-goals are checked in order n different/2 called last in sister/2 –wouldn’t work properly if it were called first! ?- sister(W, pat). W \= pat, parent(P,W), parent(P,pat), female(W) W \= pat (fails) No n But pat does have a sister: ann

46 Exercise n Which of the following would work for the question sister(Who, pat)? sister(S, P) :- parent(PP, S), parent(PP, P), S \= P, female(S). sister(S, P) :- parent(PP, S), S \= P, parent(PP, P), female(S). sister(S, P) :- female(S), parent(PP, P), parent(PP, S), S \= P. What about ?- sister(pat, Who).

47 Next Time n Terms & Unification n Bratko, Chapter 2


Download ppt "Introduction to Prolog Proof Procedures. Exercise parent(mark, alex). parent(di, alex). brother(brian, mark). sister(cathy, di). wife(susan, brian). husband(brad,"

Similar presentations


Ads by Google