0% found this document useful (0 votes)
5 views14 pages

Prolog programming: a do-it-yourself course for beginners

The document outlines Day 2 of a Prolog programming course for beginners, focusing on recursive predicate definitions and how Prolog answers queries. It includes examples of defining ancestor relationships and demonstrates matching and proof search through various queries. The session emphasizes practical exercises on matching, proof search, and recursion.

Uploaded by

rodrigorgs
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views14 pages

Prolog programming: a do-it-yourself course for beginners

The document outlines Day 2 of a Prolog programming course for beginners, focusing on recursive predicate definitions and how Prolog answers queries. It includes examples of defining ancestor relationships and demonstrates matching and proof search through various queries. The session emphasizes practical exercises on matching, proof search, and recursion.

Uploaded by

rodrigorgs
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

Prolog programming: a do-it-yourself

course for beginners


Day 2

Kristina Striegnitz
Department of Computational Linguistics
Saarland University, Saarbrücken, Germany
[email protected]
http://www.coli.uni-sb.de/˜kris

Day 2: Matching and Proof Search – p.1


Day 2: Matching and Proof Search

Today: • recursive predicate definitions


• how Prolog answers queries

Reader: Lectures 2 and 3 of Learn Prolog Now!

Day 2: Matching and Proof Search – p.2


Ancestors
parent of(paul,petunia).
Paul Helen Albert Ruth parent of(helen,petunia).
parent of(paul,lili).
Vernon Petunia Lili James parent of(helen,lili).
parent of(albert,james).

Dudley Harry
parent of(ruth,james).
parent of(petunia,dudley).
parent of(vernon,dudley).
parent of(lili,harry).
parent of(james,harry).

Task: Define a predicate ancestor of(X,Y) which is true if X


is an ancestor of Y.

Day 2: Matching and Proof Search – p.3


Ancestors (cont.)
grandparent of(X,Y) :- parent of(X,Z), parent of(Z,Y).
greatgrandparent of(X,Y) :- parent of(X,Z), parent of(Z,A), parent of(A,Y).
greatgreatgrandparent of(X,Y) :- parent of(X,Z), parent of(Z,A),
parent of(A,B), parent of(B,Y).
➜ Doesn’t work for ancestor of; don’t know “how many parents we
have to go back”.

ancestor of(X,Y) :- parent of(X,Y).


People are ancestors of their children,

ancestor of(X,Y) :- parent of(X,Z), ancestor of(Z,Y).

and they are ancestors of anybody that their children may be an-
cestors of (i.e., of all the descendants of their children).

Day 2: Matching and Proof Search – p.4


Ancestors (cont.)
grandparent of(X,Y) :- parent of(X,Z), parent of(Z,Y).
greatgrandparent of(X,Y) :- parent of(X,Z), parent of(Z,A), parent of(A,Y).
greatgreatgrandparent of(X,Y) :- parent of(X,Z), parent of(Z,A),
recursion
parent of(A,B), parent of(B,Y).
➜ Doesn’t work for ancestor of; don’t know “how many parents we
have to go back”.

ancestor of(X,Y) :- parent of(X,Y).


People are ancestors of their children,

ancestor of(X,Y) :- parent of(X,Z), ancestor of(Z,Y).

and they are ancestors of anybody that their children may be an-
cestors of (i.e., of all the descendants of their children).

Day 2: Matching and Proof Search – p.5


Example 1

KB: wizard(harry).
wizard(ron).
wizard(hermione).
muggle(uncle vernon).
muggle(aunt petunia).
chases(crookshanks,scabbars).
Query: ?- wizard(hermione).

yes

Easy: wizard(hermione) is a fact in the knowledge base.

Day 2: Matching and Proof Search – p.6


Example 2

KB: wizard(harry). Query: ?- wizard(X).


wizard(ron). X = harry ;
wizard(hermione). X = ron ;
muggle(uncle vernon). X = hermione ;
muggle(aunt petunia). no
chases(crookshanks,scabbars).

• The query wizard(X) matches the fact wizard(harry). This


instantiates the variable X with harry.
• It also matches the facts wizard(ron) and wizard(hermione).

Day 2: Matching and Proof Search – p.7


Matching
• Two atoms match if they are the same atom.
Ex.: harry = harry, but harry \= ’Harry’.
• A variable matches any other Prolog term. The variable gets
instantiated with the other term.
Ex.: X = wizard(harry)
Ex.: X = Y
• Two complex terms match if they have the same functor and the
same number of arguments and if all pairs of parallel arguments
match.
Ex.: like(harry,hargrid) = like(harry,X)
Ex.: like(harry,hargrid) = like(harry,X,Y)
Ex.: like(harry,hargrid) \= like(X,X)

Day 2: Matching and Proof Search – p.8


Back to Example 2
KB: wizard(harry). Query: ?- wizard(X).
wizard(ron). X = harry ;
wizard(hermione). X = ron ;
muggle(uncle vernon). X = hermione ;
muggle(aunt petunia). no
chases(crookshanks,scabbars).

• Prolog checks for facts that match the query. (There are three.)
• Prolog starts from the top of the knowledge base and, therefore,
finds wizard(harry) first.
• Typing ; forces Prolog to check whether there are other possibilities.

Day 2: Matching and Proof Search – p.9


Example 3

KB: eating(dudley).

happy(aunt petunia) :- happy(dudley).

happy(uncle vernon) :- happy(dudley),unhappy(harry).

happy(dudley) :- kicking(dudley,harry).

happy(dudley) :- eating(dudley).
Query: ?- happy(aunt petunia).
yes

• Check for a fact or a rule’s head that match the query.


• If you find a fact, you’re done.
• If you find a rule, prove all goals specified in the body of the rule.

Day 2: Matching and Proof Search – p.10


Example 4

KB: eating(dudley).
happy(aunt petunia):-happy(dudley).
happy(uncle vernon):-happy(dudley),unhappy(harry).
happy(dudley):-kicking(dudley,harry).
happy(dudley):-eating(dudley).
Query: ?- happy(X).

happy(X)
X=ap X=d
X=uv X=d

happy(d) happy(d),unhappy(h) kicking(d,h) eating(d)

kicking(d,h) eating(h) kicking(d,h),unhappy(h)


eating(d),unhappy(h) X=d

X=ap unhappy(d)

Day 2: Matching and Proof Search – p.11


wizard(X)
Example 5 X=lili
X=I1
X=ruth
father(I2,I1),
X=albert
wizard(I2),
father(albert,james). mother(I3,I1),
wizard(I3)
father(james,harry).
I1=james I1=harry
mother(ruth,james). I2=albert I2=james
mother(lili,harry).
wizard(albert), wizard(james),
wizard(lili). mother(I3,james), mother(I3,harry),
wizard(I3) wizard(I3)
wizard(ruth).
wizard(albert).
mother(I3,james), father(I4,james),
wizard(X) :- wizard(I3) wizard(I4),
father(Y,X), mother(I5,james),
I3=ruth wizard(I5)
wizard(Y), mother(I3,harry),
wizard(ruth) wizard(I3)
mother(Z,X),
wizard(Z). I4=albert
I5=ruth
mother(I3,harry),
wizard(I3)

I3=lili

wizard(lili)

Day 2: Matching and Proof Search – p.12


Ancestors (cont.)
ancestor_of(albert,harry)

parent of(paul,petunia).
parent of(helen,petunia).
parent_of(albert,harry)
parent of(paul,lili).
parent of(helen,lili). parent_of(albert,I1)
parent of(albert,james). ancestor_of(I1,harry)
parent of(ruth,james).
I1=james
parent of(petunia,dudley).
parent of(vernon,dudley).
parent of(lili,harry). ancestor_of(james,harry)
parent of(james,harry).

ancestor of(X,Y) :- parent_of(james,harry)


parent of(X,Y).
ancestor of(X,Y) :-
parent of(X,Z),
ancestor of(Z,Y).

Day 2: Matching and Proof Search – p.13


Practical Session

• matching
• proof search
• recursion

http://www.coli.uni-sb.de/˜kris/esslli04prolog
(Maybe it’s a good idea to bookmark it, if you haven’t done so already.)

Day 2: Matching and Proof Search – p.14

You might also like