Logic Programming

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

description

Logic Programming. PROLOG. Dr. Yasser Nada Fall 2010/2011 Lecture 3. Logic Programming. 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. - PowerPoint PPT Presentation

Transcript of Logic Programming

Page 1: Logic Programming

Logic Programming

Dr. Yasser NadaFall 2010/2011

Lecture 3

1Logic Programming

Page 2: Logic Programming

• 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.

2

Prolog Search Strategy

Logic Programming

Page 3: Logic Programming

• 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.

3

Resolution and Substitution

Logic Programming

Page 4: Logic Programming

• 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• ?

4

And/Or Graph

Logic Programming

happy(P)

wealthy(P) healthy(P) wise(P)

p/ali p/ahmed p/salem

success success success

Page 5: Logic Programming

• 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• ?

5

And/Or Graph

Logic Programming

k(X)

f(X) g(X) f(X)

f(a) f(a) h(b)g(a) f(b)f(b) g(b)

h(X)

Page 6: Logic Programming

• 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.

6

Proof Tree or Search Tree

Logic Programming

Page 7: Logic Programming

• 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• ?

7

Search Tree or Proof Tree

Logic Programming

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.

Page 8: Logic Programming

• 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• ?

8

Search Tree or Proof Tree

Logic Programming

f(X)

X/a X/b

Match

• If the query is non-grounded fact (i.e. contains variables):

• Find values for the variables in the query that make the query true.

Page 9: Logic Programming

• 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• ?

9

Search Tree or Proof Tree

Logic Programming

k(X)

f(X),g(X) f(X), h(X)

g(a) h(a)

X/aX/a

h(b)g(b)

X/b X/b

Match

replace

Page 10: Logic Programming

10

Example

Logic Programming

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

brown(Z).

Page 11: Logic Programming

11

Example

Logic Programming

• 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)

black(X),big(X) brown(X),big(X)

big(cat) big(bear)

X/Z X/Z

X/cat X/bear

Page 12: Logic Programming

12

Example

Logic Programming

• big(bear).• big(elephant).• small(cat).

• brown(bear).• gray(elephant).• black(cat).

• dark(Z) :- black(Z).• dark(Z) :-brown(Z).

big(X),dark(X)

dark(bear) dark(elephant)

black(bear)

black(elephant)

X/bear X/elephant

Z/bear

brown(bear)

brown(elephant)

Z/bear

Z/elephant

Page 13: Logic Programming

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).

13

Example

Logic Programming

Page 14: Logic Programming

14Logic Programming

brother(P,A)

father(Z,P), father(Z,A), male(P), P\=A.

P/X, A/Y

father(salem,A), male(ali), ali\=A.

salem/Z, ali/P

male(ali),ali\=ali.

ali\=ali.

A/ali

male(ali),ali\=ahmed.

ali\=ahmed.

father(salem,A), male(ahmed), ahmed\=A.

salem/Z, ahmed/P

male(ahmed),ahmed\=ali.

ahmed\=ali.

A/ali

A/ahmed

male(ahmed),ahmed\=ahmed.

A/ahmed

ali\=ali.

Proof Tree

Page 15: Logic Programming

15

Recursion

Logic Programming

• 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).

Page 16: Logic Programming

16

And/Or Graph

Logic Programming

talk_about(X,Y)

knows(X,Y) knows(X,Q) talk_about(Q,Y)

knows(Q,Y) talk_about(Q1,Y)knows(Q,Q1)

talk_about(A,B) :- knows(A,B).talk_about(P,R) :- knows(P,Q),talk_about(Q,R).

X/A,Y/BX/P,Y/R

Q/A,Y/BQ/P,Y/R

Same as root tree

Page 17: Logic Programming

17

Recursion

Logic Programming

• 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).

? 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 ? ;…?

Page 18: Logic Programming

18

Proof Tree

Logic Programming

talk_about(X,Y)

knows(X,Y)

X/bill,Y/jane X/jane,Y/pat X/jane,Y/fred X/fred,Y/bill

knows(X,Q), talk_about(Q,Y)

Rule1

Rule2

X/A, Y/BX/P, Y/R

Page 19: Logic Programming

19

Proof Tree

Logic Programming

knows(X,Q), talk_about(Q,Y)

talk_about(jane,Y)

X/bill,Q/jane

Not executed

yet

talk_about(pat,Y)

X/jane,Q/pat

talk_about(fred,Y)

X/jane,Q/fred

talk_about(bill,Y)

X/fred,Q/bill

1

23

4

Page 20: Logic Programming

20

Proof Tree

Logic Programming

Jane/A,Y/B

knows(jane,Y)

Y/pat

knows(jane,Q),talk_about(Q,Y)

P/jane,Y/Q

talk_about(jane,Y)1

talk_about(pat,Y)Y/fred

talk_about(fred,Y)

Q/pat Q/fred

knows(pat,Y) knows(pat,Q), talk_about(Q,Y)

pat/A,Y/B P/pat,Y/R5

Page 21: Logic Programming

21

Proof Tree

Logic Programming

5 talk_about(fred,Y)

fred/A,Y/B

knows(fred,Y)

Y/bill

knows(fred,Q), talk_about(Q,Y)

fred/P,Y/R

talk_about(bill,Y)

Q/bill

knows(bill,Y)

bill/A,Y/B

Y/jane

knows(bill,Q),talk_about(Q,Y)

bill/P,Y/R

talk_about(jane,Y)Q/jane

Infinte loop, same as sub-goal 1

Page 22: Logic Programming

22

Homework

Logic Programming

• Do Ex. 3.1 and 3.2 in your book.

Page 23: Logic Programming

23

Questions

Logic Programming