1 / 23

Logic Programming

Learn how Prolog finds answers using search strategy, resolution, and substitution. Explore logic programming concepts with examples of And/Or graphs, proof trees, and recursion in Prolog.

cindys
Download Presentation

Logic Programming

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Logic Programming PROLOG Dr. Yasser Nada Fall 2010/2011 Lecture 3 Logic Programming

  2. Prolog Search Strategy • How Prolog finds answers to goals? • Prolog search for clauses from: • Top to bottom. • And left to right. • This is the same as we read the prolog program. PROLOG Logic Programming

  3. Resolution and Substitution • Resolution is the process of matching the goal with the head of a clause in the program database and finding whatever substitutions that can be implied. • Substitution is replacing the variable with other variables or constants. PROLOG Logic Programming

  4. And/Or Graph happy(P) • wealthy(ali). • healthy(ahmed). • wise(salem). • happy(P) :- wealthy(P). • happy(P) :- healthy(P). • happy(P) :- wise(P). • ? happy(P). • p=ali ? ; • p=ahmed? ; • p=salem ? ; • no • ? PROLOG wealthy(P) healthy(P) wise(P) p/ali p/salem p/ahmed success success success Logic Programming

  5. And/Or Graph k(X) • f(a). • f(b). • g(a). • g(b). • h(b). • k(X) :- f(X), g(X). • k(X) :- f(X), h(X). • ? k(X). • X = a ? ; • X = b ? ; • X = b ? ; • no • ? PROLOG f(X) g(X) f(X) h(X) f(a) f(b) g(a) g(b) f(a) f(b) h(b) Logic Programming

  6. Proof Tree or Search Tree • It is a tree that consists of: • Nodes which represents the goals. • Edges which represents the goal derivation. • The root of the tree is the query. PROLOG Logic Programming

  7. Search Tree or Proof Tree • f(a). • f(b). • g(a). • g(b). • h(b). • k(X) :- f(X), g(X). • k(X) :- f(X), h(X). • ? f(a). • yes • ? f(a) Match PROLOG • If the query is grounded fact (i.e. no variables): • match the fact with the set of facts in the program. • The answer to the query is either yes or no. Logic Programming

  8. Search Tree or Proof Tree • f(a). • f(b). • g(a). • g(b). • h(b). • k(X) :- f(X), g(X). • k(X) :- f(X), h(X). • ? f(X). • X = a ? ; • X = b ? ; • no • ? f(X) Match X/a X/b PROLOG • If the query is non-grounded fact (i.e. contains variables): • Find values for the variables in the query that make the query true. Logic Programming

  9. Search Tree or Proof Tree • f(a). • f(b). • g(a). • g(b). • h(b). • k(X) :- f(X), g(X). • k(X) :- f(X), h(X). • ? k(X). • X = a ? ; • X = b ? ; • X = b ? ; • no • ? k(X) Match replace PROLOG f(X),g(X) f(X), h(X) X/a X/a X/b X/b g(a) g(b) h(a) h(b) Logic Programming

  10. Example • big(bear). • big(elephant). • small(cat). • brown(bear). • gray(elephant). • black(cat). • dark(Z) :- black(Z). • dark(Z) :- brown(Z). PROLOG Logic Programming

  11. Example • big(bear). • big(elephant). • small(cat). • brown(bear). • gray(elephant). • black(cat). • dark(Z) :- black(Z),big(Z). • dark(Z) :- brown(Z), big(Z). dark(X), big(X) X/Z X/Z PROLOG black(X),big(X) brown(X),big(X) X/cat X/bear big(cat) big(bear) Logic Programming

  12. Example • big(bear). • big(elephant). • small(cat). • brown(bear). • gray(elephant). • black(cat). • dark(Z) :- black(Z). • dark(Z) :-brown(Z). big(X),dark(X) X/bear X/elephant PROLOG dark(bear) dark(elephant) Z/bear Z/bear black(bear) brown(bear) Z/elephant black(elephant) brown(elephant) Logic Programming

  13. Example male(ali). male(ahmed). male(salem). father(salem, ali). father(salem, ahmed). brother(X,Y) :- father(Z,X), father(Z,Y), male(X), X \= Y. ? brother(P,A). PROLOG Logic Programming

  14. Proof Tree brother(P,A) P/X, A/Y father(Z,P), father(Z,A), male(P), P\=A. PROLOG salem/Z, ahmed/P salem/Z, ali/P father(salem,A), male(ali), ali\=A. father(salem,A), male(ahmed), ahmed\=A. A/ali A/ahmed A/ali A/ahmed male(ali), ali\=ahmed. male(ali),ali\=ali. male(ahmed), ahmed\=ali. male(ahmed), ahmed\=ahmed. ali\=ahmed. ali\=ali. ahmed\=ali. ali\=ali. Logic Programming

  15. Recursion • Recursion is a rule that define a relationship in terms of themselves. • You talk about someone either if you know him or you know someone who talks about him. • talk_about(A,B) :- knows(A,B). • talk_about(P,R) :- knows(P,Q), talk_about(Q,R). PROLOG Logic Programming

  16. And/Or Graph Same as root tree talk_about(X,Y) X/A,Y/B X/P,Y/R PROLOG knows(X,Y) knows(X,Q) talk_about(Q,Y) Q/A,Y/B Q/P,Y/R knows(Q,Q1) talk_about(Q1,Y) knows(Q,Y) talk_about(A,B) :- knows(A,B). talk_about(P,R) :- knows(P,Q),talk_about(Q,R). Logic Programming

  17. Recursion • talks about(A,B):- knows(A,B). • talks about(P,R):- knows(P,Q), talks about(Q,R). • knows(bill,jane). • knows(jane,pat). • knows(jane,fred). • knows(fred,bill). PROLOG ? talk_about(X,Y). X=bill Y=jane ? ; X=jane Y=pat ? ; X=jane Y=fred ? ; X=fred Y=bill ? ; X=bill Y=pat ? ; X=bill Y=fred ? ; X=bill Y=bill ? ; X=bill Y=jane ? ; … ? Logic Programming

  18. Proof Tree Rule2 talk_about(X,Y) X/P, Y/R Rule1 X/A, Y/B knows(X,Y) knows(X,Q), talk_about(Q,Y) PROLOG X/bill,Y/jane X/jane,Y/pat X/jane,Y/fred X/fred,Y/bill Logic Programming

  19. Proof Tree Not executed yet knows(X,Q), talk_about(Q,Y) X/fred,Q/bill X/bill,Q/jane PROLOG talk_about(bill,Y) talk_about(jane,Y) 4 1 X/jane,Q/pat X/jane,Q/fred talk_about(pat,Y) talk_about(fred,Y) 2 3 Logic Programming

  20. Proof Tree 1 talk_about(jane,Y) Jane/A,Y/B P/jane,Y/Q PROLOG knows(jane,Y) knows(jane,Q),talk_about(Q,Y) Q/pat Q/fred Y/pat Y/fred talk_about(fred,Y) talk_about(pat,Y) 5 P/pat,Y/R pat/A,Y/B knows(pat,Y) knows(pat,Q), talk_about(Q,Y) Logic Programming

  21. Proof Tree 5 talk_about(fred,Y) fred/P,Y/R fred/A,Y/B PROLOG knows(fred,Y) knows(fred,Q), talk_about(Q,Y) Q/bill Y/bill talk_about(bill,Y) bill/A,Y/B bill/P,Y/R knows(bill,Y) knows(bill,Q),talk_about(Q,Y) Q/jane talk_about(jane,Y) Y/jane Infinte loop, same as sub-goal 1 Logic Programming

  22. Homework • Do Ex. 3.1 and 3.2 in your book. PROLOG Logic Programming

  23. Questions PROLOG Logic Programming 23

More Related