Top Banner
Prolog: Unification & Recursion http://www.learnprolognow.org © Patrick Blackburn, Johan Bos & Kristina Striegnitz
61

Prolog: Unification & Recursion © Patrick Blackburn, Johan Bos & Kristina Striegnitz.

Dec 18, 2015

Download

Documents

Verity Johns
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Prolog: Unification & Recursion  © Patrick Blackburn, Johan Bos & Kristina Striegnitz.

Prolog: Unification & Recursion

http://www.learnprolognow.org © Patrick Blackburn, Johan Bos & Kristina Striegnitz

Page 2: Prolog: Unification & Recursion  © Patrick Blackburn, Johan Bos & Kristina Striegnitz.

Unification

• Recall previous example, where we said that Prolog unifies

woman(X)

with

woman(mia)

thereby instantiating the variable X with the atom mia.

Page 3: Prolog: Unification & Recursion  © Patrick Blackburn, Johan Bos & Kristina Striegnitz.

Unification

• Working definition:• Two terms unify if they are the same term or if

they contain variables that can be uniformly instantiated with terms in such a way that the resulting terms are equal

Page 4: Prolog: Unification & Recursion  © Patrick Blackburn, Johan Bos & Kristina Striegnitz.

Unification

• This means that:• mia and mia unify• 42 and 42 unify• woman(mia) and woman(mia) unify

• This also means that:• vincent and mia do not unify• woman(mia) and woman(jody) do not unify

Page 5: Prolog: Unification & Recursion  © Patrick Blackburn, Johan Bos & Kristina Striegnitz.

Unification

• What about the terms:• mia and X

Page 6: Prolog: Unification & Recursion  © Patrick Blackburn, Johan Bos & Kristina Striegnitz.

Unification

• What about the terms:• mia and X• woman(Z) and woman(mia)

Page 7: Prolog: Unification & Recursion  © Patrick Blackburn, Johan Bos & Kristina Striegnitz.

Unification

• What about the terms:• mia and X• woman(Z) and woman(mia)• loves(mia,X) and loves(X,vincent)

Page 8: Prolog: Unification & Recursion  © Patrick Blackburn, Johan Bos & Kristina Striegnitz.

Instantiations

• When Prolog unifies two terms it performs all the necessary instantiations, so that the terms are equal afterwards

• This makes unification a powerful programming mechanism

Page 9: Prolog: Unification & Recursion  © Patrick Blackburn, Johan Bos & Kristina Striegnitz.

Revised Definition 1/3

1. If T1 and T2 are constants, then T1 and T2 unify if they are the same atom, or the same number.

Page 10: Prolog: Unification & Recursion  © Patrick Blackburn, Johan Bos & Kristina Striegnitz.

Revised Definition 2/3

1. If T1 and T2 are constants, then T1 and T2 unify if they are the same atom, or the same number.

2. If T1 is a variable and T2 is any type of term, then T1

and T2 unify, and T1 is instantiated to T2. (and vice versa)

Page 11: Prolog: Unification & Recursion  © Patrick Blackburn, Johan Bos & Kristina Striegnitz.

Revised Definition 3/3

1. If T1 and T2 are constants, then T1 and T2 unify if they are the same atom, or the same number.

2. If T1 is a variable and T2 is any type of term, then T1

and T2 unify, and T1 is instantiated to T2. (and vice versa)

3. If T1 and T2 are complex terms then they unify if:a) They have the same functor and arity, andb) all their corresponding arguments unify, andc) the variable instantiations are compatible.

Page 12: Prolog: Unification & Recursion  © Patrick Blackburn, Johan Bos & Kristina Striegnitz.

Prolog unification: =/2

?- mia = mia.yes?-

Page 13: Prolog: Unification & Recursion  © Patrick Blackburn, Johan Bos & Kristina Striegnitz.

Prolog unification: =/2

?- mia = mia.yes?- mia = vincent.no?-

Page 14: Prolog: Unification & Recursion  © Patrick Blackburn, Johan Bos & Kristina Striegnitz.

Prolog unification: =/2

?- mia = X.X=miayes?-

Page 15: Prolog: Unification & Recursion  © Patrick Blackburn, Johan Bos & Kristina Striegnitz.

How will Prolog respond?

?- X=mia, X=vincent.

Page 16: Prolog: Unification & Recursion  © Patrick Blackburn, Johan Bos & Kristina Striegnitz.

How will Prolog respond?

?- X=mia, X=vincent.no?-

Why? After working through the first goal, Prolog has instantiated X with mia, so that it cannot unify it with vincent anymore. Hence the second goal fails.

Page 17: Prolog: Unification & Recursion  © Patrick Blackburn, Johan Bos & Kristina Striegnitz.

Example with complex terms

?- k(s(g),Y) = k(X,t(k)).

Page 18: Prolog: Unification & Recursion  © Patrick Blackburn, Johan Bos & Kristina Striegnitz.

Example with complex terms

?- k(s(g),Y) = k(X,t(k)).X=s(g)Y=t(k)yes?-

Page 19: Prolog: Unification & Recursion  © Patrick Blackburn, Johan Bos & Kristina Striegnitz.

Example with complex terms

?- k(s(g),t(k)) = k(X,t(Y)).

Page 20: Prolog: Unification & Recursion  © Patrick Blackburn, Johan Bos & Kristina Striegnitz.

Example with complex terms

?- k(s(g),t(k)) = k(X,t(Y)).X=s(g)Y=kyes?-

Page 21: Prolog: Unification & Recursion  © Patrick Blackburn, Johan Bos & Kristina Striegnitz.

One last example

?- loves(X,X) = loves(marsellus,mia).

Page 22: Prolog: Unification & Recursion  © Patrick Blackburn, Johan Bos & Kristina Striegnitz.

Programming with Unification

vertical( line(point(X,Y), point(X,Z))).

horizontal( line(point(X,Y), point(Z,Y))).

Page 23: Prolog: Unification & Recursion  © Patrick Blackburn, Johan Bos & Kristina Striegnitz.

Programming with Unification

vertical( line(point(X,Y), point(X,Z))).

horizontal( line(point(X,Y), point(Z,Y))).

?-

Page 24: Prolog: Unification & Recursion  © Patrick Blackburn, Johan Bos & Kristina Striegnitz.

Programming with Unification

vertical( line(point(X,Y), point(X,Z))).

horizontal( line(point(X,Y), point(Z,Y))).

?- vertical(line(point(1,1),point(1,3))).yes?-

Page 25: Prolog: Unification & Recursion  © Patrick Blackburn, Johan Bos & Kristina Striegnitz.

Programming with Unification

vertical( line(point(X,Y), point(X,Z))).

horizontal( line(point(X,Y), point(Z,Y))).

?- vertical(line(point(1,1),point(1,3))).yes?- vertical(line(point(1,1),point(3,2))).no?-

Page 26: Prolog: Unification & Recursion  © Patrick Blackburn, Johan Bos & Kristina Striegnitz.

Programming with Unification

vertical( line(point(X,Y), point(X,Z))).

horizontal( line(point(X,Y), point(Z,Y))).

?- horizontal(line(point(1,1),point(1,Y))).Y = 1;no?-

Page 27: Prolog: Unification & Recursion  © Patrick Blackburn, Johan Bos & Kristina Striegnitz.

Programming with Unification

vertical( line(point(X,Y), point(X,Z))).

horizontal( line(point(X,Y), point(Z,Y))).

?- horizontal(line(point(2,3),Point)).Point = point(_554,3);no?-

Page 28: Prolog: Unification & Recursion  © Patrick Blackburn, Johan Bos & Kristina Striegnitz.

Proof Search

• Now that we know about unification, we are in a position to learn how Prolog searches a knowledge base to see if a query is satisfied.

• In other words: we are ready to learn about proof search

Page 29: Prolog: Unification & Recursion  © Patrick Blackburn, Johan Bos & Kristina Striegnitz.

Example

f(a).f(b).g(a).g(b).h(b).k(X):- f(X), g(X), h(X).

?- k(Y).

Page 30: Prolog: Unification & Recursion  © Patrick Blackburn, Johan Bos & Kristina Striegnitz.

Example: search tree

f(a).f(b).g(a).g(b).h(b).k(X):- f(X), g(X), h(X).

?- k(Y).

?- k(Y).

Page 31: Prolog: Unification & Recursion  © Patrick Blackburn, Johan Bos & Kristina Striegnitz.

Example: search tree

f(a).f(b).g(a).g(b).h(b).k(X):- f(X), g(X), h(X).

?- k(Y).

?- k(Y).

?- f(X), g(X), h(X).

Y=X

Page 32: Prolog: Unification & Recursion  © Patrick Blackburn, Johan Bos & Kristina Striegnitz.

Example: search tree

f(a).f(b).g(a).g(b).h(b).k(X):- f(X), g(X), h(X).

?- k(Y).

?- k(Y).

?- f(X), g(X), h(X).

?- g(a), h(a).

X=a

Y=X

Page 33: Prolog: Unification & Recursion  © Patrick Blackburn, Johan Bos & Kristina Striegnitz.

Example: search tree

f(a).f(b).g(a).g(b).h(b).k(X):- f(X), g(X), h(X).

?- k(Y).

?- k(Y).

?- f(X), g(X), h(X).

?- g(a), h(a).

?- h(a).

X=a

Y=X

Page 34: Prolog: Unification & Recursion  © Patrick Blackburn, Johan Bos & Kristina Striegnitz.

Example: search tree

f(a).f(b).g(a).g(b).h(b).k(X):- f(X), g(X), h(X).

?- k(Y).

?- k(Y).

?- f(X), g(X), h(X).

?- g(a), h(a).

?- h(a).

X=a

Y=X

Page 35: Prolog: Unification & Recursion  © Patrick Blackburn, Johan Bos & Kristina Striegnitz.

Example: search tree

f(a).f(b).g(a).g(b).h(b).k(X):- f(X), g(X), h(X).

?- k(Y).

?- k(Y).

?- f(X), g(X), h(X).

?- g(a), h(a).

?- h(a).

X=a

?- g(b), h(b).

X=b

Y=X

Page 36: Prolog: Unification & Recursion  © Patrick Blackburn, Johan Bos & Kristina Striegnitz.

Example: search tree

f(a).f(b).g(a).g(b).h(b).k(X):- f(X), g(X), h(X).

?- k(Y).

?- k(Y).

?- f(X), g(X), h(X).

?- g(a), h(a).

?- h(a).

X=a

?- g(b), h(b).

X=b

?- h(b).

Y=X

Page 37: Prolog: Unification & Recursion  © Patrick Blackburn, Johan Bos & Kristina Striegnitz.

Example: search tree

f(a).f(b).g(a).g(b).h(b).k(X):- f(X), g(X), h(X).

?- k(Y).Y=b

?- k(Y).

?- f(X), g(X), h(X).

?- g(a), h(a).

?- h(a).

X=a

?- g(b), h(b).

X=b

?- h(b).

Y=X

Page 38: Prolog: Unification & Recursion  © Patrick Blackburn, Johan Bos & Kristina Striegnitz.

Example: search tree

f(a).f(b).g(a).g(b).h(b).k(X):- f(X), g(X), h(X).

?- k(Y).Y=b;no?-

?- k(Y).

?- f(X), g(X), h(X).

?- g(a), h(a).

?- h(a).

X=a

?- g(b), h(b).

X=b

?- h(b).

Y=X

Page 39: Prolog: Unification & Recursion  © Patrick Blackburn, Johan Bos & Kristina Striegnitz.

Another example

loves(vincent,mia).loves(marsellus,mia).

jealous(A,B):- loves(A,C), loves(B,C).

?- jealous(X,Y).

Page 40: Prolog: Unification & Recursion  © Patrick Blackburn, Johan Bos & Kristina Striegnitz.

Another example

loves(vincent,mia).loves(marsellus,mia).

jealous(A,B):- loves(A,C), loves(B,C).

?- jealous(X,Y).

?- jealous(X,Y).

Page 41: Prolog: Unification & Recursion  © Patrick Blackburn, Johan Bos & Kristina Striegnitz.

Another example

loves(vincent,mia).loves(marsellus,mia).

jealous(A,B):- loves(A,C), loves(B,C).

?- jealous(X,Y).

?- jealous(X,Y).

?- loves(A,C), loves(B,C).

X=A Y=B

Page 42: Prolog: Unification & Recursion  © Patrick Blackburn, Johan Bos & Kristina Striegnitz.

Another example

loves(vincent,mia).loves(marsellus,mia).

jealous(A,B):- loves(A,C), loves(B,C).

?- jealous(X,Y).

?- jealous(X,Y).

?- loves(A,C), loves(B,C).

?- loves(B,mia).

A=vincentC=mia

X=A Y=B

Page 43: Prolog: Unification & Recursion  © Patrick Blackburn, Johan Bos & Kristina Striegnitz.

Another example

loves(vincent,mia).loves(marsellus,mia).

jealous(A,B):- loves(A,C), loves(B,C).

?- jealous(X,Y).X=vincent Y=vincent

?- jealous(X,Y).

?- loves(A,C), loves(B,C).

?- loves(B,mia).

A=vincentC=mia

B=vincent

X=A Y=B

Page 44: Prolog: Unification & Recursion  © Patrick Blackburn, Johan Bos & Kristina Striegnitz.

Another example

loves(vincent,mia).loves(marsellus,mia).

jealous(A,B):- loves(A,C), loves(B,C).

?- jealous(X,Y).X=vincent Y=vincent;X=vincentY=marsellus

?- jealous(X,Y).

?- loves(A,C), loves(B,C).

?- loves(B,mia).

A=vincentC=mia

B=vincent

B=marsellus

X=A Y=B

Page 45: Prolog: Unification & Recursion  © Patrick Blackburn, Johan Bos & Kristina Striegnitz.

Another example

loves(vincent,mia).loves(marsellus,mia).

jealous(A,B):- loves(A,C), loves(B,C).

?- jealous(X,Y).X=vincent Y=vincent;X=vincentY=marsellus;

?- jealous(X,Y).

?- loves(A,C), loves(B,C).

?- loves(B,mia).

A=vincentC=mia

?- loves(B,mia).

A=marsellusC=mia

B=vincent

B=marsellus

X=A Y=B

Page 46: Prolog: Unification & Recursion  © Patrick Blackburn, Johan Bos & Kristina Striegnitz.

Another example

loves(vincent,mia).loves(marsellus,mia).

jealous(A,B):- loves(A,C), loves(B,C).

….X=vincentY=marsellus;X=marsellusY=vincent

?- jealous(X,Y).

?- loves(A,C), loves(B,C).

?- loves(B,mia).

A=vincentC=mia

?- loves(B,mia).

A=marsellusC=mia

B=vincent B=vincent

B=marsellus

X=A Y=B

Page 47: Prolog: Unification & Recursion  © Patrick Blackburn, Johan Bos & Kristina Striegnitz.

Another example

loves(vincent,mia).loves(marsellus,mia).

jealous(A,B):- loves(A,C), loves(B,C).

….X=marsellusY=vincent;X=marsellusY=marsellus

?- jealous(X,Y).

?- loves(A,C), loves(B,C).

?- loves(B,mia).

A=vincentC=mia

?- loves(B,mia).

A=marsellusC=mia

B=vincent B=vincent

B=marsellus B=marsellus

X=A Y=B

Page 48: Prolog: Unification & Recursion  © Patrick Blackburn, Johan Bos & Kristina Striegnitz.

Another example

loves(vincent,mia).loves(marsellus,mia).

jealous(A,B):- loves(A,C), loves(B,C).

….X=marsellusY=vincent;X=marsellusY=marsellus;no

?- jealous(X,Y).

?- loves(A,C), loves(B,C).

?- loves(B,mia).

A=vincentC=mia

?- loves(B,mia).

A=marsellusC=mia

B=vincent B=vincent

B=marsellus B=marsellus

X=A Y=B

Page 49: Prolog: Unification & Recursion  © Patrick Blackburn, Johan Bos & Kristina Striegnitz.

Recursive Definitions

• Prolog predicates can be defined recursively• A predicate is recursively defined if one or

more rules in its definition refers to itself

Page 50: Prolog: Unification & Recursion  © Patrick Blackburn, Johan Bos & Kristina Striegnitz.

Example 1: Ancestor

parent(john,paul). parent(paul,tom). parent(tom,mary).

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

?-

Page 51: Prolog: Unification & Recursion  © Patrick Blackburn, Johan Bos & Kristina Striegnitz.

Example 1: Ancestor

parent(john,paul). parent(paul,tom). parent(tom,mary).

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

?- ancestor(john,mary).

Page 52: Prolog: Unification & Recursion  © Patrick Blackburn, Johan Bos & Kristina Striegnitz.

Example 1: Ancestor

parent(john,paul). parent(paul,tom). parent(tom,mary).

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

?- ancestor(john,mary).yes

Page 53: Prolog: Unification & Recursion  © Patrick Blackburn, Johan Bos & Kristina Striegnitz.

Example 1: Ancestor

parent(john,paul). parent(paul,tom). parent(tom,mary).

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

?- ancestor(john,mary).yes?- ancestor(john,X).

Page 54: Prolog: Unification & Recursion  © Patrick Blackburn, Johan Bos & Kristina Striegnitz.

Example 1: Ancestor

parent(john,paul). parent(paul,tom). parent(tom,mary).

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

?- ancestor(john,mary).yes?- ancestor(john,X).X = paul ;X = tom ;X = mary ;no

Page 55: Prolog: Unification & Recursion  © Patrick Blackburn, Johan Bos & Kristina Striegnitz.

Another recursive definition

p:- p.

?-

Page 56: Prolog: Unification & Recursion  © Patrick Blackburn, Johan Bos & Kristina Striegnitz.

Another recursive definition

p:- p.

?- p.

Page 57: Prolog: Unification & Recursion  © Patrick Blackburn, Johan Bos & Kristina Striegnitz.

Another recursive definition

p:- p.

?- p.ERROR: out of memory

Page 58: Prolog: Unification & Recursion  © Patrick Blackburn, Johan Bos & Kristina Striegnitz.

descend1.pl

child(anna,bridget).child(bridget,caroline).child(caroline,donna).child(donna,emily).

descend(X,Y):- child(X,Y).descend(X,Y):- child(X,Z), descend(Z,Y).

?- descend(A,B).A=annaB=bridget

Page 59: Prolog: Unification & Recursion  © Patrick Blackburn, Johan Bos & Kristina Striegnitz.

descend2.pl

child(anna,bridget).child(bridget,caroline).child(caroline,donna).child(donna,emily).

descend(X,Y):- child(X,Z), descend(Z,Y).descend(X,Y):- child(X,Y).

?- descend(A,B).A=annaB=emily

Page 60: Prolog: Unification & Recursion  © Patrick Blackburn, Johan Bos & Kristina Striegnitz.

descend3.pl

child(anna,bridget).child(bridget,caroline).child(caroline,donna).child(donna,emily).

descend(X,Y):- descend(Z,Y), child(X,Z).descend(X,Y):- child(X,Y).

?- descend(A,B).ERROR: OUT OF LOCAL STACK

Page 61: Prolog: Unification & Recursion  © Patrick Blackburn, Johan Bos & Kristina Striegnitz.

Summary of this lecture

• In this lecture we have– unification – introduced search trees– Recursion