Logic Programming OLO G Dr Yasser Nada Fall

  • Slides: 23
Download presentation
Logic Programming OLO G Dr. Yasser Nada Fall 2010/2011 Lecture 3 Logic Programming 1

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

Prolog Search Strategy • How Prolog finds answers to goals? – Prolog search for

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

Resolution and Substitution • Resolution is the process of matching the goal with the

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. OLO G • Substitution is replacing the variable with other variables or constants. Logic Programming 3

And/Or Graph • • • wealthy(ali). healthy(ahmed). wise(salem). happy(P) : - wealthy(P) happy(P) :

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

And/Or Graph • • f(a). f(b). g(a). g(b). h(b). k(X) : - f(X), g(X).

And/Or Graph • • 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? ; no ? OLO G k(X) f(a) f(b) g(X) g(a) g(b) Logic Programming f(X) f(a) h(X) f(b) h(b) 5

Proof Tree or Search Tree • It is a tree that consists of: –

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. OLO G Logic Programming 6

Search Tree or Proof Tree • • f(a). f(b). g(a). g(b). h(b). k(X) :

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). OLO G • ? f(a). • yes • ? f(a) Match • 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 7

Search Tree or Proof Tree • • f(a). f(b). g(a). g(b). h(b). k(X) :

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). OLO G • • • ? f(X). X=a? ; X=b? ; no ? f(X) X/a Match X/b • 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 8

Search Tree or Proof Tree • • f(a). f(b). g(a). g(b). h(b). k(X) :

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? ; no ? OLO G k(X) Match replace f(X), g(X) f(X), h(X) X/a X/b X/a g(a) g(b) h(a) Logic Programming X/b h(b) 9

Example big(bear). big(elephant). small(cat). brown(bear). gray(elephant). black(cat). dark(Z) : black(Z). • dark(Z) : brown(Z).

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

Example big(bear). big(elephant). small(cat). brown(bear). gray(elephant). black(cat). dark(Z) : black(Z), big(Z). • dark(Z) :

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). • • OLO G dark(X), big(X) X/Z black(X), big(X) brown(X), big(X) X/cat X/bear big(cat) Logic Programming big(bear) 11

Example • big(bear). • big(elephant). • small(cat). OLO G big(X), dark(X) X/bear dark(bear) •

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

Example male(ali). male(ahmed). male(salem). father(salem, ali). father(salem, ahmed). brother(X, Y) : father(Z, X), father(Z,

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. OLO G ? brother(P, A). Logic Programming 13

Proof Tree brother(P, A) P/X, A/Y father(Z, P), father(Z, A), male(P), P=A. OLO G

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

Recursion • Recursion is a rule that define a relationship in terms of themselves.

Recursion • Recursion is a rule that define a relationship in terms of themselves. OLO G • 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). Logic Programming 15

And/Or Graph Same as root tree talk_about(X, Y) X/A, Y/B knows(X, Y) OLO G

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

Recursion • talks about(A, B): - knows(A, B). • talks about(P, R): - knows(P,

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). OLO G ? 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 17

Proof Tree talk_about(X, Y ) X/A, Y/B Rule 1 knows(X, Y) OLO G X/bill,

Proof Tree talk_about(X, Y ) X/A, Y/B Rule 1 knows(X, Y) OLO G X/bill, Y/jane X/P, Y/R Rule 2 knows(X, Q), talk_about(Q, Y) X/jane, Y/pat X/jane, Y/fred Logic Programming X/fred, Y/bill 18

Proof Tree Not executed yet knows(X, Q), talk_about(Q, Y) X/fred, Q/bill X/bill, Q/jane OLO

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

Proof Tree 1 talk_about(jane, Y) Jane/A, Y/B OLO G P/jane, Y/Q knows(jane, Y) Y/pat

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

Proof Tree 5 talk_about(fred, Y) fred/P, Y/R fred/A, Y/B knows(fred, Y) OLO G knows(fred,

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

Homework • Do Ex. 3. 1 and 3. 2 in your book. OLO G

Homework • Do Ex. 3. 1 and 3. 2 in your book. OLO G Logic Programming 22

Questions OLO Logic Programming 23

Questions OLO Logic Programming 23