# Unification

Document Sample

```					     Unification:

Matching Expressions
Motivations

• You know what a well-formed FOL sentence
looks like.
• You know how to interpret a sentence to get
its meaning from a model.
• In this lecture, you will learn about one
particular method that Prolog uses to help
him calculate new true sentences from old
ones.
• The method is called unification.
Objectives

•   Matching names
•   Unify two expressions
•   Unification procedure
•   Trace an example
All men are mortal: Revisit

X (man(X) => mortal(X)).
man(socrates)
man(socrates) => mortal(socrates) % substitute socrates for X
mortal(socrates)                  % apply modus ponens

Substitute socrates for X before applying
modus ponens.
Matching names

• To derive new true sentences, Prolog needs
to use inference rules such as modus
ponens.
• To use modus ponens, Prolog needs to
perform substitution operations, changing a
variable to another variable or to a constant.
• Unification is an algorithm for determining the
substitutions needed to make two
expressions match.
Substitute a constant for a variable

• Unify p(a,X) and p(a,b)
– b/X, i.e., substitute b for X
– 1st expression, p(a,X), becomes p(a,b)
– 2nd expression, p(a,b), reminds unchanged
– both results in p(a,b)
• Unify p(a,X) and p(Y,b)
– a/Y, b/X
– p(a,b)
• The set of substitutions is called a unifier.
Substitute a function for a variable

• Unify p(a,X) and p(Y,f(Y))
– a/Y
– Now unify p(a,X) and p(a,f(a))
 Substitute a function for a variable

 f(a)/X

 p(a,f(a))
Unification failures

• Unify p(a,X) and p(X,b)
– a/X
– Now unify p(a,a) and p(a,b)
 failure

• Unify p(a,b) and p(X,X)
– a/X
– Now unify p(a,b) and p(a,a)
 failure
General and specific substitutions

• General substitution
– Unify p(X) and p(Y)
– unifiers: Z/X, Z/Y
– p(Z)
• Specific substitution
– Unify p(X) and p(Y)
– fred/X, fred/Y
– p(fred)
General and specific substitutions 2

• General substitution
– Unify p(X, f(Y), b) and p(X, f(b), b)
– b/Y
– p(X, f(b),b)
• Specific substitution
– Unify p(X, f(Y), b) and p(X, f(b), b)
– b/X, b/Y
– p(b, f(b), b)
Composition of unifiers
• E is a list of expressions.
• s is a unifier of E.
• g is the most general unifier of E
• Then there exists another unifier s’
such that s = gs’ and Es = Egs'
• Example: Unify p(X) and p(Y)
– E = [p(X), p(Y)]
– s = {fred/X, fred/Y}
– g = {Z/X, Z/Y}.
– Let s’ = {fred/Z}
– gs’ = {Z/X, Z/Y} {fred/Z} = {fred/X, fred/Y} = s
– Es = [p(fred), p(fred)]
– Egs' = [p(Z), p(Z)]{fred/Z} = [p(fred), p(fred)]
List representation

1. p(a,X)
(p a X)
2. p(Y, f(Y))
(p Y (f Y))
3. parents( X, father(X), mother(bill) )
(parents X (father X) (mother bill) )
4. parents(bill, father(bill), Y)
(parents bill (father bill) Y)
Unification algorithm
1. function unify(E1, E2)              4 trivial or non-
2. Begin                               recursive cases
3.     case
4. 1. Both E1 and E2 are constants or the empty list
5.          if E1 = E2 then return {}
6.          else return Fail;
7. 2. E1 is a variable:
8.          if E1 occurs in E2 then return Fail
9.          else return {E2/E1};
10. 3. E2 is a variable:
11.         if E2 occurs in E1 then return Fail
12.         else return {E1/E2}
13. 4. Either E1 or E2 are empty then return Fail
Unification algorithm cont’
14. 5. Otherwise:         1 recursive case when E1 and E2
15.       Begin           are lists.
16.          HE1 := first element of E1;
17.          HE2 := first element of E2;
18.          SUBS1 := unify(HE1, HE2); Unify the head elements
19.          if SUBS1 := FAIL then return FAIL;
20.          TE1 := apply(SUBS1, rest of E1); Apply substitutions
21.          TE2 := apply(SUBS1, rest of E2); to the tails
22.          SUBS2 := unify(TE1, TE2); Unify the tails
23.          if SUBS2 = FAIL then return FAIL;
24.          else return composition(SUBS1,SUBS2)
25.       End begin                      Combine the two
26.     End case                         results and return.
27.    End function
Trace
Predicates:    Parents(X, father(X), mother(bill))           Parents(bill, father(bill), Y)
Lists:     (parents X (father X) (mother bill))        (parents bill (father bill) Y)

(parents X (father X)                     (mother bill) )
(parents bill (father bill)                  Y                  )

1. Unify ( (parents X (father X) (mother bill)) , (parents bill (father bill) Y) )

24
Return{(mother bill)/Y,
5                                           bill/Z}}
Return {}
Line 18
2. Unify (parents, parents)
22
4              3. Unify ((X (father X) (mother bill)) , ( bill (father bill) Y))
Example cont’d

22
3. Unify((X (father X) (mother bill)) , ( bill (father bill) Y))

24
20, 21                    Return{(mother bill)/Y}
9            Apply
Return{bill/X}       substitution
18                                    22
5. Unify( ( (father bill) (mother bill) ), ( (father bill) Y ) )
4. Unify(X,bill)
7                                          5
Return{}
18
6. Unify ( (father bill), (father bill) )

5
5                                  Return{}
22
18                   Return {}
8. Unify ( (bill), (bill) )
7.. Unify (father, father)
Example cont’d
5. Unify( ( (father bill) (mother bill) ), ( (father bill) Y ) )
5
Return{}                                                             24
Return{(mother bill)/Y}
22                                                    18
8. Unify((bill),(bill))                               11. Unify ( ((mother,bill)), (Y) )

5
Return{}                                                    12
5                                                                5
18                               Return{}                Return{(mother bill)/Y}
Return{}
9. Unify (bill, bill)                           18
22                      12. Unify ( (mother,bill), Y )          22
10. Unify((),())                                              13. Unify((),())
Complete trace

Branch left to call itself with head elements.
Branch right to call itself with tail elements.

Go left before go right.
Go down before go up.

Leaf nodes are the trivial cases.

(parents X (father X) (mother bill) )
(parents bill (father bill) Y       )
Summary

• In FOL, a variable name can match a
constant, predicate, or function.
• Expressions are represented as lists for
efficient processing.
• Algorithm unify performs matching of two
expressions by matching their heads first and
their tails recursively.
• Prolog uses unification as part of the process
for deriving new true sentences.

```
DOCUMENT INFO
Shared By:
Categories:
Tags:
Stats:
 views: 12 posted: 2/28/2012 language: pages: 19