Presentation is loading. Please wait.

Presentation is loading. Please wait.

© Patrick Blackburn, Johan Bos & Kristina Striegnitz

Similar presentations


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

1 © Patrick Blackburn, Johan Bos & Kristina Striegnitz
Lecture 2 Theory Unification Unification in Prolog Proof search Exercises Exercises of LPN chapter 2 Practical work © Patrick Blackburn, Johan Bos & Kristina Striegnitz

2 © Patrick Blackburn, Johan Bos & Kristina Striegnitz
Aim of this lecture Discuss unification in Prolog Show how Prolog unification differs from standard unification Explain the search strategy that Prolog uses when it tries to deduce new information from old, using modus ponens © Patrick Blackburn, Johan Bos & Kristina Striegnitz

3 © 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. © Patrick Blackburn, Johan Bos & Kristina Striegnitz

4 © Patrick Blackburn, Johan Bos & Kristina Striegnitz
Recall Prolog Terms Terms Simple Terms Complex Terms © Patrick Blackburn, Johan Bos & Kristina Striegnitz Constants Variables Atoms Numbers

5 © 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 © Patrick Blackburn, Johan Bos & Kristina Striegnitz

6 © 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 © Patrick Blackburn, Johan Bos & Kristina Striegnitz

7 © Patrick Blackburn, Johan Bos & Kristina Striegnitz
Unification What about the terms: mia and X © Patrick Blackburn, Johan Bos & Kristina Striegnitz

8 © Patrick Blackburn, Johan Bos & Kristina Striegnitz
Unification What about the terms: mia and X woman(Z) and woman(mia) © Patrick Blackburn, Johan Bos & Kristina Striegnitz

9 © 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) © Patrick Blackburn, Johan Bos & Kristina Striegnitz

10 © 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 © Patrick Blackburn, Johan Bos & Kristina Striegnitz

11 © Patrick Blackburn, Johan Bos & Kristina Striegnitz
Revised Definition 1/3 If T1 and T2 are constants, then T1 and T2 unify if they are the same atom, or the same number. © Patrick Blackburn, Johan Bos & Kristina Striegnitz

12 © Patrick Blackburn, Johan Bos & Kristina Striegnitz
Revised Definition 2/3 If T1 and T2 are constants, then T1 and T2 unify if they are the same atom, or the same number. 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) © Patrick Blackburn, Johan Bos & Kristina Striegnitz

13 © Patrick Blackburn, Johan Bos & Kristina Striegnitz
Revised Definition 3/3 If T1 and T2 are constants, then T1 and T2 unify if they are the same atom, or the same number. 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) If T1 and T2 are complex terms then they unify if: They have the same functor and arity, and all their corresponding arguments unify, and the variable instantiations are compatible. © Patrick Blackburn, Johan Bos & Kristina Striegnitz

14 Prolog unification: =/2
?- mia = mia. yes ?- © Patrick Blackburn, Johan Bos & Kristina Striegnitz

15 Prolog unification: =/2
?- mia = mia. yes ?- mia = vincent. no ?- © Patrick Blackburn, Johan Bos & Kristina Striegnitz

16 Prolog unification: =/2
?- mia = X. X=mia yes ?- © Patrick Blackburn, Johan Bos & Kristina Striegnitz

17 How will Prolog respond?
?- X=mia, X=vincent. © Patrick Blackburn, Johan Bos & Kristina Striegnitz

18 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. © Patrick Blackburn, Johan Bos & Kristina Striegnitz

19 Example with complex terms
?- k(s(g),Y) = k(X,t(k)). © Patrick Blackburn, Johan Bos & Kristina Striegnitz

20 Example with complex terms
?- k(s(g),Y) = k(X,t(k)). X=s(g) Y=t(k) yes ?- © Patrick Blackburn, Johan Bos & Kristina Striegnitz

21 Example with complex terms
?- k(s(g),t(k)) = k(X,t(Y)). © Patrick Blackburn, Johan Bos & Kristina Striegnitz

22 Example with complex terms
?- k(s(g),t(k)) = k(X,t(Y)). X=s(g) Y=k yes ?- © Patrick Blackburn, Johan Bos & Kristina Striegnitz

23 © Patrick Blackburn, Johan Bos & Kristina Striegnitz
One last example ?- loves(X,X) = loves(marsellus,mia). © Patrick Blackburn, Johan Bos & Kristina Striegnitz

24 Prolog and unification
Prolog does not use a standard unification algorithm Consider the following query: ?- father(X) = X. Do these terms unify or not? © Patrick Blackburn, Johan Bos & Kristina Striegnitz

25 © Patrick Blackburn, Johan Bos & Kristina Striegnitz
Infinite terms ?- father(X) = X. X=father(father(father(father(father(father(father(father(father(father(father(father(father(father(father(father(father(father(father(father(father(father(father(father(father(father(father(father(father(father(father(father(father(father(father(father(father(father(father(father(father(father(father(father(father(father(father(father( © Patrick Blackburn, Johan Bos & Kristina Striegnitz

26 © Patrick Blackburn, Johan Bos & Kristina Striegnitz
Infinite terms ?- father(X) = X. X=father(father(father(…)))) yes ?- © Patrick Blackburn, Johan Bos & Kristina Striegnitz

27 © Patrick Blackburn, Johan Bos & Kristina Striegnitz
Occurs Check A standard unification algorithm carries out an occurs check If it is asked to unify a variable with another term it checks whether the variable occurs in the term In Prolog: ?- unify_with_occurs_check(father(X), X). no © Patrick Blackburn, Johan Bos & Kristina Striegnitz

28 Programming with Unification
vertical( line(point(X,Y), point(X,Z))). horizontal( line(point(X,Y), point(Z,Y))). © Patrick Blackburn, Johan Bos & Kristina Striegnitz

29 Programming with Unification
vertical( line(point(X,Y), point(X,Z))). horizontal( line(point(X,Y), point(Z,Y))). ?- © Patrick Blackburn, Johan Bos & Kristina Striegnitz

30 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 ?- © Patrick Blackburn, Johan Bos & Kristina Striegnitz

31 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 ?- © Patrick Blackburn, Johan Bos & Kristina Striegnitz

32 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 ?- © Patrick Blackburn, Johan Bos & Kristina Striegnitz

33 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 ?- © Patrick Blackburn, Johan Bos & Kristina Striegnitz

34 Exercise: unification
© Patrick Blackburn, Johan Bos & Kristina Striegnitz

35 © 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 © Patrick Blackburn, Johan Bos & Kristina Striegnitz

36 © 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). © Patrick Blackburn, Johan Bos & Kristina Striegnitz

37 © 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). © Patrick Blackburn, Johan Bos & Kristina Striegnitz

38 © 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=X ?- f(X), g(X), h(X). ?- k(Y). © Patrick Blackburn, Johan Bos & Kristina Striegnitz

39 © 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=X ?- f(X), g(X), h(X). X=a ?- g(a), h(a). ?- k(Y). © Patrick Blackburn, Johan Bos & Kristina Striegnitz

40 © 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=X ?- f(X), g(X), h(X). X=a ?- g(a), h(a). ?- k(Y). © Patrick Blackburn, Johan Bos & Kristina Striegnitz ?- h(a).

41 © 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=X ?- f(X), g(X), h(X). X=a ?- g(a), h(a). ?- k(Y). © Patrick Blackburn, Johan Bos & Kristina Striegnitz ?- h(a).

42 © 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=X ?- f(X), g(X), h(X). X=a X=b ?- g(a), h(a). ?- g(b), h(b). ?- k(Y). © Patrick Blackburn, Johan Bos & Kristina Striegnitz ?- h(a).

43 © 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=X ?- f(X), g(X), h(X). X=a X=b ?- g(a), h(a). ?- g(b), h(b). ?- k(Y). © Patrick Blackburn, Johan Bos & Kristina Striegnitz ?- h(a). ?- h(b).

44 © 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=X ?- f(X), g(X), h(X). X=a X=b ?- g(a), h(a). ?- g(b), h(b). ?- k(Y). Y=b © Patrick Blackburn, Johan Bos & Kristina Striegnitz ?- h(a). ?- h(b).

45 © 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=X ?- f(X), g(X), h(X). X=a X=b ?- g(a), h(a). ?- g(b), h(b). ?- k(Y). Y=b; no ?- © Patrick Blackburn, Johan Bos & Kristina Striegnitz ?- h(a). ?- h(b).

46 © 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). © Patrick Blackburn, Johan Bos & Kristina Striegnitz

47 © 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). © Patrick Blackburn, Johan Bos & Kristina Striegnitz

48 © 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=A Y=B ?- loves(A,C), loves(B,C). ?- jealous(X,Y). © Patrick Blackburn, Johan Bos & Kristina Striegnitz

49 © 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=A Y=B ?- loves(A,C), loves(B,C). A=vincent C=mia ?- loves(B,mia). ?- jealous(X,Y). © Patrick Blackburn, Johan Bos & Kristina Striegnitz

50 © 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=A Y=B ?- loves(A,C), loves(B,C). A=vincent C=mia ?- loves(B,mia). ?- jealous(X,Y). X=vincent Y=vincent © Patrick Blackburn, Johan Bos & Kristina Striegnitz B=vincent

51 © 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=A Y=B ?- loves(A,C), loves(B,C). A=vincent C=mia ?- loves(B,mia). ?- jealous(X,Y). X=vincent Y=vincent; Y=marsellus © Patrick Blackburn, Johan Bos & Kristina Striegnitz B=vincent B=marsellus

52 © 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=A Y=B ?- loves(A,C), loves(B,C). A=vincent C=mia A=marsellus C=mia ?- loves(B,mia). ?- loves(B,mia). ?- jealous(X,Y). X=vincent Y=vincent; Y=marsellus; © Patrick Blackburn, Johan Bos & Kristina Striegnitz B=vincent B=marsellus

53 © 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=A Y=B ?- loves(A,C), loves(B,C). A=vincent C=mia A=marsellus C=mia ?- loves(B,mia). ?- loves(B,mia). …. X=vincent Y=marsellus; X=marsellus Y=vincent © Patrick Blackburn, Johan Bos & Kristina Striegnitz B=vincent B=vincent B=marsellus

54 © 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=A Y=B ?- loves(A,C), loves(B,C). A=vincent C=mia A=marsellus C=mia ?- loves(B,mia). ?- loves(B,mia). …. X=marsellus Y=vincent; Y=marsellus © Patrick Blackburn, Johan Bos & Kristina Striegnitz B=vincent B=vincent B=marsellus B=marsellus

55 © 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=A Y=B ?- loves(A,C), loves(B,C). A=vincent C=mia A=marsellus C=mia ?- loves(B,mia). ?- loves(B,mia). …. X=marsellus Y=vincent; Y=marsellus; no © Patrick Blackburn, Johan Bos & Kristina Striegnitz B=vincent B=vincent B=marsellus B=marsellus

56 © Patrick Blackburn, Johan Bos & Kristina Striegnitz
Exercises © Patrick Blackburn, Johan Bos & Kristina Striegnitz

57 Summary of this lecture
In this lecture we have defined unification looked at the difference between standard unification and Prolog unification introduced search trees © Patrick Blackburn, Johan Bos & Kristina Striegnitz

58 © Patrick Blackburn, Johan Bos & Kristina Striegnitz
Next lecture Discuss recursion in Prolog Introduce recursive definitions in Prolog Show that there can be mismatches between the declarative meaning of a Prolog program, and its procedural meaning. © Patrick Blackburn, Johan Bos & Kristina Striegnitz


Download ppt "© Patrick Blackburn, Johan Bos & Kristina Striegnitz"

Similar presentations


Ads by Google