Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Prolog: Unification & Recursion © Patrick Blackburn, Johan Bos & Kristina Striegnitz."— Presentation transcript:

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

2 Unification Recall previous example, where we said that Prolog unifies woman(X) with woman(mia) thereby instantiating the variable X with the atom mia.

3 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

4 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

5 Unification What about the terms: mia and X

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

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

8 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

9 Revised Definition 1/3 1.If T 1 and T 2 are constants, then T 1 and T 2 unify if they are the same atom, or the same number.

10 Revised Definition 2/3 1.If T 1 and T 2 are constants, then T 1 and T 2 unify if they are the same atom, or the same number. 2.If T 1 is a variable and T 2 is any type of term, then T 1 and T 2 unify, and T 1 is instantiated to T 2. (and vice versa)

11 Revised Definition 3/3 1.If T 1 and T 2 are constants, then T 1 and T 2 unify if they are the same atom, or the same number. 2.If T 1 is a variable and T 2 is any type of term, then T 1 and T 2 unify, and T 1 is instantiated to T 2. (and vice versa) 3.If T 1 and T 2 are complex terms then they unify if: a)They have the same functor and arity, and b)all their corresponding arguments unify, and c)the variable instantiations are compatible.

12 Prolog unification: =/2 ?- mia = mia. yes ?-

13 Prolog unification: =/2 ?- mia = mia. yes ?- mia = vincent. no ?-

14 Prolog unification: =/2 ?- mia = X. X=mia yes ?-

15 How will Prolog respond? ?- X=mia, X=vincent.

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

17 Example with complex terms ?- k(s(g),Y) = k(X,t(k)).

18 Example with complex terms ?- k(s(g),Y) = k(X,t(k)). X=s(g) Y=t(k) yes ?-

19 Example with complex terms ?- k(s(g),t(k)) = k(X,t(Y)).

20 Example with complex terms ?- k(s(g),t(k)) = k(X,t(Y)). X=s(g) Y=k yes ?-

21 One last example ?- loves(X,X) = loves(marsellus,mia).

22 Programming with Unification vertical( line(point(X,Y), point(X,Z))). horizontal( line(point(X,Y), point(Z,Y))).

23 Programming with Unification vertical( line(point(X,Y), point(X,Z))). horizontal( line(point(X,Y), point(Z,Y))). ?-

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

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

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

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

28 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

29 Example f(a). f(b). g(a). g(b). h(b). k(X):- f(X), g(X), h(X). ?- k(Y).

30 Example: search tree f(a). f(b). g(a). g(b). h(b). k(X):- f(X), g(X), h(X). ?- k(Y).

31 Example: search tree f(a). f(b). g(a). g(b). h(b). k(X):- f(X), g(X), h(X). ?- k(Y). ?- f(X), g(X), h(X). Y=X

32 Example: search tree f(a). f(b). g(a). g(b). h(b). k(X):- f(X), g(X), h(X). ?- k(Y). ?- f(X), g(X), h(X). ?- g(a), h(a). X=a Y=X

33 Example: search tree f(a). f(b). g(a). g(b). h(b). k(X):- f(X), g(X), h(X). ?- k(Y). ?- f(X), g(X), h(X). ?- g(a), h(a). ?- h(a). X=a Y=X

34 Example: search tree f(a). f(b). g(a). g(b). h(b). k(X):- f(X), g(X), h(X). ?- k(Y). ?- f(X), g(X), h(X). ?- g(a), h(a). ?- h(a). X=a † Y=X

35 Example: search tree f(a). f(b). g(a). g(b). h(b). k(X):- f(X), g(X), h(X). ?- k(Y). ?- f(X), g(X), h(X). ?- g(a), h(a). ?- h(a). X=a ?- g(b), h(b). X=b † Y=X

36 Example: search tree f(a). f(b). g(a). g(b). h(b). k(X):- f(X), g(X), h(X). ?- 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

37 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

38 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

39 Another example loves(vincent,mia). loves(marsellus,mia). jealous(A,B):- loves(A,C), loves(B,C). ?- jealous(X,Y).

40 Another example loves(vincent,mia). loves(marsellus,mia). jealous(A,B):- loves(A,C), loves(B,C). ?- jealous(X,Y).

41 Another example loves(vincent,mia). loves(marsellus,mia). jealous(A,B):- loves(A,C), loves(B,C). ?- jealous(X,Y). ?- loves(A,C), loves(B,C). X=AY=B

42 Another example loves(vincent,mia). loves(marsellus,mia). jealous(A,B):- loves(A,C), loves(B,C). ?- jealous(X,Y). ?- loves(A,C), loves(B,C). ?- loves(B,mia). A=vincent C=mia X=AY=B

43 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=vincent C=mia B=vincent X=AY=B

44 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=vincent Y=marsellus ?- jealous(X,Y). ?- loves(A,C), loves(B,C). ?- loves(B,mia). A=vincent C=mia B=vincent B=marsellus X=AY=B

45 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=vincent Y=marsellus; ?- jealous(X,Y). ?- loves(A,C), loves(B,C). ?- loves(B,mia). A=vincent C=mia ?- loves(B,mia). A=marsellus C=mia B=vincent B=marsellus X=AY=B

46 Another example loves(vincent,mia). loves(marsellus,mia). jealous(A,B):- loves(A,C), loves(B,C). …. X=vincent Y=marsellus; X=marsellus Y=vincent ?- jealous(X,Y). ?- loves(A,C), loves(B,C). ?- loves(B,mia). A=vincent C=mia ?- loves(B,mia). A=marsellus C=mia B=vincent B=marsellus X=AY=B

47 Another example loves(vincent,mia). loves(marsellus,mia). jealous(A,B):- loves(A,C), loves(B,C). …. X=marsellus Y=vincent; X=marsellus Y=marsellus ?- jealous(X,Y). ?- loves(A,C), loves(B,C). ?- loves(B,mia). A=vincent C=mia ?- loves(B,mia). A=marsellus C=mia B=vincent B=marsellus X=AY=B

48 Another example loves(vincent,mia). loves(marsellus,mia). jealous(A,B):- loves(A,C), loves(B,C). …. X=marsellus Y=vincent; X=marsellus Y=marsellus; no ?- jealous(X,Y). ?- loves(A,C), loves(B,C). ?- loves(B,mia). A=vincent C=mia ?- loves(B,mia). A=marsellus C=mia B=vincent B=marsellus X=AY=B

49 Recursive Definitions Prolog predicates can be defined recursively A predicate is recursively defined if one or more rules in its definition refers to itself

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

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

52 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

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

54 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

55 Another recursive definition p:- p. ?-

56 Another recursive definition p:- p. ?- p.

57 Another recursive definition p:- p. ?- p. ERROR: out of memory

58 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=anna B=bridget

59 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=anna B=emily

60 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

61 Summary of this lecture In this lecture we have – unification – introduced search trees – Recursion


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

Similar presentations


Ads by Google