Presentation is loading. Please wait.

Presentation is loading. Please wait.

Unification, Recursion and Lists

Similar presentations


Presentation on theme: "Unification, Recursion and Lists"— Presentation transcript:

1

2 Unification, Recursion and Lists
Chapter 4 Unification, Recursion and Lists Taif University Fall 2010 Dr. Yasser Ahmed nada

3 Unification

4 Unification is the name given to the way Prolog does its matching.
A naive view of the matching process might be represented by the question “can the target object be made to fit one of the source objects”. Unification implies mutual coercion. There is an attempt to alter both the target and the current source object to make them look the same. Unification is a two way matching process

5 Consider how we might match the term book(waverley,X) against some clause for which
book(Y, scott) book(waverley,X) book(Y, scott) Unification provides the substitutions X/scott and Y/waverley these substitutions both terms look like book(waverley,scott)

6 The substitution X/scott and Y/waverley is known as a unifier , the most general unifier
If we unify X with Y then one unifier might be the substitution X/1 and Y/1 but this is not the most general unifier.

7 Success with X=fred, Y=fred
possible unifications X=fred fails because you can’t match two distinct atoms jane=fred Y=fred, X=Y Success with X=fred, Y=fred

8 Success X=happy(jim) Success X=Y

9 likes(jim,X) = likes(X,fred) Consider a query likes(jim,X).
Suppose there is one clause: likes(X,fred) and mentions a variable with the textual name of X So now consider whether the head of the clause likes(X,fred) unifies with the current goal likes(jim,X). Match the first arguments, we get X=jim. Then we try to match the second arguments. Now can X=fred? If X=jim then the answer is no. failed likes(jim,X) = likes(X,fred)

10 Another possible unifications
Success X=M,Y=M Likes(X,Y)=likes(M,M) Success X=M,X=a Likes(X,X)=likes(M,a) Failed 5+1=6 Failed 6=5+1

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

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

13 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

14 Unification This means that: This also 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

15 Unification What about the terms: mia and X

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

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

18 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

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

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

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

22 Prolog unification ?- mia = mia. yes ?-

23 Prolog unification ?- mia = mia. yes ?- mia = vincent. no ?-

24 Prolog unification ?- mia = X. X=mia yes ?-

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

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

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

28 Prolog and unification
Prolog does not use a standard unification algorithm Consider the following query: ?- father(X) = X. Do these terms unify or not?

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))). ?- © 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 ?- © 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))). ?- vertical(line(point(1,1),point(1,3))). yes ?- vertical(line(point(1,1),point(3,2))). 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(1,1),point(1,Y))). Y = 1; no ?- © Patrick Blackburn, Johan Bos & Kristina Striegnitz

34 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

35 Exercise: unification

36 Recursion

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

38 One of my ancestors is one of my parents or one of their ancestors.
A string of characters is a single character or a single character followed by a string of characters. A paragraph is a sentence or a sentence appended to a paragraph. To decouple a train, uncouple the first carriage and then decouple the rest of the train.

39 An example recursive program:
talks about(A,B):- knows(A,B). talks about(P,R):- knows(P,Q), talks about(Q,R). Rule 1 Rule 2 recursion in Rule2

40 If you look at the AND/OR tree of the search space you can see that
There is a subtree which is the same shape as the whole tree reflecting the single recursive call to talks about/2. The solution of a given problem depends on being able to stop recursing at some point. Because the leftmost path down the tree is not infinite in length it is reasonable to hope for a solution.

41 Rule 2 Rule 1 Rule 1 Rule 2 talks about(X,Y) talks about(Z,Y)
knows(X,Z) knows(X,Y) Rule 1 knows(Z,Y) talks about(Z1,Y) knows(Z,Z1) Rule 1 Rule 2

42 Goal is talks about(X,Y)
Data base 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). Goal is talks about(X,Y)

43 we get, in the order shown:
X= bill Y= jane X= jane Y= pat X= jane Y= fred X= fred Y= bill X= bill Y= pat and so on The search strategy implies that Prolog keep on trying to satisfy the subgoal knows(X,Y) until there are no more solutions to this.

44 Example 1: Eating isDigesting(X,Y):- justAte(X,Y).
isDigesting(X,Y):- justAte(X,Z), isDigesting(Z,Y). justAte(mosquito,blood(john)). justAte(frog,mosquito). justAte(stork,frog). ?-

45 Example 1: Eating isDigesting(X,Y):- justAte(X,Y).
isDigesting(X,Y):- justAte(X,Z), isDigesting(Z,Y). justAte(mosquito,blood(john)). justAte(frog,mosquito). justAte(stork,frog). ?- isDigesting(stork,mosquito).

46 Another recursive definition
p:- p. ?-

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

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

49 Example 2: Decendant child(bridget,caroline). child(caroline,donna).
descend(X,Y):- child(X,Y). descend(X,Y):- child(X,Z), child(Z,Y).

50 Example 2: Decendant child(anna,bridget). child(bridget,caroline).
child(caroline,donna). child(donna,emily). descend(X,Y):- child(X,Y). descend(X,Y):- child(X,Z), child(Z,Y).

51 Example 2: Decendant child(anna,bridget). child(bridget,caroline).
child(caroline,donna). child(donna,emily). descend(X,Y):- child(X,Y). descend(X,Y):- child(X,Z), child(Z,Y). ?- descend(anna,donna). no ?-

52 Example 2: Decendant child(anna,bridget). child(bridget,caroline).
child(caroline,donna). child(donna,emily). descend(X,Y):- child(X,Y). descend(X,Y):- child(X,Z), child(Z,Y). descend(X,Y):- child(X,Z), child(Z,U), child(U,Y). ?-

53 Example 2: Decendant 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). ?-

54 Example 2: Decendant 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(anna,donna).

55 Search tree Draw search tree for ?- descend(anna,donna).

56 Example 3: Successor Suppose we use the following way to write numerals: 0 is a numeral. If X is a numeral, then so is succ(X).

57 Example 3: Successor numeral(0). numeral(succ(X)):- numeral(X).

58 Example 3: Successor numeral(0). numeral(succ(X)):- numeral(X).
?- numeral(succ(succ(succ(0)))). yes ?-

59 Example 3: Successor numeral(0). numeral(succ(X)):- numeral(X).

60 Example 3: Successor numeral(0). numeral(succ(X)):- numeral(X).
X=succ(0); X=succ(succ(0)); X=succ(succ(succ(0))); X=succ(succ(succ(succ(0))))

61 Example 4: Addition add(0,X,X) %%% base clause

62 Example 4: Addition add(0,X,X). %%% base clause
add(succ(X),Y,succ(Z)): %%% recursive clause add(X,Y,Z). ?- add(succ(succ(0)),succ(succ(succ(0))), Result). Result=succ(succ(succ(succ(succ(0))))) yes

63 Exercises

64 Lists

65 Lists, for now, can be regarded as special Prolog structures that can be used to represent an ordered sequence of Prolog terms. For example [ice cream, coffee, chocolate] a list with three elements (all atoms) [a, b, c, c, d, e] a list with six elements (all atoms) [ ] a list with no elements in it (it is an atom) [dog(fido), cat(rufus), goldfish(jimmy)] a list with three elements (all Prolog terms) [happy(fred),[ice cream,chocolate],[1,[2],3]] a list with three elements!

66 Note that the “,” used in the construction of a list is just an argument separator as in the term foo(a,b). Also note that, because order is preserved, the list [a,b,c] is not the same as [a,c,b].

67 How to construct/deconstruct a list

68 [X|Y] = [f,r,e,d] result is X=f Y=[r,e,d] Head Tail
The basic approach provides a simple way of splitting a list into two bits: the first element (if there is one!) and the rest of the list. The corresponding way of joining two bits to form a list requires taking an element and a list and inserting the element at the front of the list. List Destruction: first, we show how to remove the first element from a list. [X|Y] = [f,r,e,d] result is X=f Y=[r,e,d] Head Tail

69 List Construction: the construction of a list is the reverse: take a variable bound to any old list say, X=[r, e, d] and add the element, say, b at the front with: Result Wanted = [b|X] Bigger Chunks: it is possible to add (or take away) bigger chunks onto (from) the front of a list than one element at a time. Suppose you want to stick the elements a, b and c onto the front of the list X to make a new list Y. then this can be done with Y=[a,b,c|X].

70 suppose you want to take three elements off the front of a list
X in such a way that the remaining list, Y, is available for use. This can be done with X=[A,B,C|Y] A limitation of this approach is that there is no direct way of evading specifying how many elements to attach/rip off.

71 The Empty List Simply written [ ]
This list ([ ]) has no elements in it: it cannot therefore be destructed. The empty list ([ ]) is an atom.

72 Some Possible Matches [b,a,d]=[d,a,b] [X]=[b,a,d] [X|Y]=[he,is,a,cat]
fails —as the order matters [b,a,d]=[d,a,b] fails —the two lists are of different lengths [X]=[b,a,d] succeeds with X=he, Y=[is,a,cat] [X|Y]=[he,is,a,cat] succeeds with X=a, Y=b, Z=[c,d] [X,Y|Z]=[a,b,c,d]

73 succeeds with X=[a], Y=[]
fails —the empty list can’t be deconstructed [X|Y]=[] succeeds with X=[a,[b,c]], Y=[d] [X|Y]=[[a,[b,c]],d] succeeds with X=[a], Y=[] [X|Y]=[a]

74 A Recursive Program Using Lists
We make use of a built-in predicate called write/1 to write out all the elements of a list in order write/1 is a side-effecting predicate. It captures the logical relation of always being true but it also produces output which has no part to play in the logical interpretation. It is therefore hard to produce a declarative reading for this predicate despite its utility from the procedural point of view. There are a fair number of other predicates which suffer from this problem including consult/1 and reconsult/1.

75 To write out a list of terms, write out the first element and then write out
the remainder (the tail). print a list([]). print a list([H|T]):- write(H), print a list(T). Note that this can be improved by printing a space between elements of the list. This requires you to add the subgoal write(’ ’) into the body of the second clause and before the recursive call to print a list/1. This will write the elements out on a single line. If you wanted to write each element on a different line then you would need the built-in predicate nl/0.

76 What You Should Be Able To Do
You should be able to determine whether or not two Prolog terms unify. You should be able to identify programs that are recursive. You should be able to build and take apart list structures. You should be able to write simple list processing programs using recursion.

77 End Of The Chapter


Download ppt "Unification, Recursion and Lists"

Similar presentations


Ads by Google