P ROLOG.
Substitutions and Unification
Antoni Ligeza
Katedra Automatyki, AGH w Krakowie
2011
Antoni Ligeza
Prolog
1/1
References
[1] Ulf Nilsson, Jan Mauszynski: Logic, Programming and Prolog, John Wiley &
Sons Ltd., pdf, http://www.ida.liu.se/ ulfni/lpp
[2] Dennis Merritt: Adventure in Prolog, Amzi, 2004
http://www.amzi.com/AdventureInProlog
[3] Quick Prolog:
http://www.dai.ed.ac.uk/groups/ssp/bookpages/quickprolog/quickprolog.html
[4] W. F. Clocksin, C. S. Mellish: Prolog. Programowanie. Helion, 2003
[5] SWI-Prologs home: http://www.swi-prolog.org
[6] Learn Prolog Now!: http://www.learnprolognow.org
[7] http://home.agh.edu.pl/ ligeza/wiki/prolog
[8] http://www.im.pwr.wroc.pl/ przemko/prolog
Antoni Ligeza
Prolog
2/1
Substitutions
The role of substitutions
z Substitution is an operation allowing to replace some variables occurring in
a formula with terms.
z The goal of applying a substitution is to make a certain formula more specific so
that it matches another formula. Substitutions allow for unification of formulae
(or terms).
Definition
A substitution is any finite mapping of variables into terms of the form
: V TER.
Notation of substitutions
z Any (finite) substitution can be presented as
= {X1 /t1 , X2 /t2 , . . . , Xn /tn },
where ti is a term to be substituted for variable Xi , i = 1, 2, . . . , n.
z is the formula (or term) resulting from simultaneous replacement of the
variables of with the appropriate terms of .
Antoni Ligeza
Prolog
3/1
Extending substitutions over Terms and Formulae
Any substitution ( : V TER) is extended to operate on terms and formulae so
that a finite mapping of the form
: TER FOR TER FOR
satisfying the following conditions is induced:
z (c) = c for any c C;
z (X) TER, and (X) 6= X for a certain finite number of variables only;
z if f (t1 , t2 , . . . , tn ) TER, then
(f (t1 , t2 , . . . , tn )) = f ((t1 ), (t2 ), . . . , (tn ));
z if p(t1 , t2 , . . . , tn ) ATOM, then
(p(t1 , t2 , . . . , tn )) = p((t1 ), (t2 ), . . . , (tn ));
z ( ) = () () for any two formulae , FOR and for
{, , , };
Antoni Ligeza
Prolog
4/1
Instances, Ground Terms, Ground Formulae
An Instance
Any formula () resulting from application of substitution to the variables of
will be denoted as
and it will be called a substitution instance or simply an instance of .
A Ground Instance, Term Formula
If no variables occur in , it will be called a ground instance (a ground formula or a
ground term, respectively).
Example
Let = {X/a, Y/f (b)}, and let = p(X, Y, g(X)). Then
= p(a, f (b), g(a))
and it is a ground formula.
Antoni Ligeza
Prolog
5/1
Composition of Substitutions
Since substitutions are mappings, a composition of substitutions is well defined.
Having two substitutions, say and , the composed substitution can be obtained
from by:
z simultaneous application of to all the terms of ,
z deletion of any pairs of the form X/t where t = X (identity substitutions), and
z enclosing all the pairs X/t of , such that does not substitute for (operate on)
X.
More formally:
Let = {X1 /t1 , X2 /t2 , . . . , Xn /tn } and let = {Y1 /s1 , Y2 /s2 , . . . , Ym /sm }. The
composition of the above substitutions is obtained from the set
{X1 /t1 , X2 /t2 , . . . , Xn /tn , Y1 /s1 , Y2 /s2 , . . . , Ym /sm }
by:
z removing all the pairs Xi /ti where Xi = ti , and
z removing all the pairs Yj /sj where Yj {X1 , X2 , . . . , Xn }.
Antoni Ligeza
Prolog
6/1
An Example
Example
Consider the following substitutions:
= {X/g(U), Y/f (Z), V/W, Z/c}
and
= {Z/f (U), W/V, U/b}.
The composition of them is defined as: ???
Antoni Ligeza
Prolog
7/1
An Example
Example
Consider the following substitutions:
= {X/g(U), Y/f (Z), V/W, Z/c}
and
= {Z/f (U), W/V, U/b}.
The composition of them is defined as:
= {X/g(b), Y/f (f (U)), Z/c, W/V, U/b}.
Antoni Ligeza
Prolog
8/1
Renaming and Inverse Substitutions
A Renaming Substitution
Substitution is a renaming substitution iff it is off the form
= {X1 /Y1 , X2 /Y2 , . . . , Xn /Yn }
(1)
Moreover, it is a one-to-one mapping if Yi 6= Yj for i 6= j, i, j {1, 2, . . . , n}.
An Inverse Substitution
Assume is a renaming, one-to-one substitution . The inverse substitution for it is
given by
1 = {Y1 /X1 , Y2 /X2 , . . . , Yn /Xn , }.
Composition of inverse substitutions
The composition of a renaming substitution and the inverse one leads to an empty
substitution, traditionally denoted with ; we have
1 = .
Antoni Ligeza
Prolog
9/1
Some properties
An Instance
Let E denote an expression (formula or term), denote an empty substitution, and let
be a one-to-one renaming substitution; and denote any substitutions.
The following properties are satisfied for any substitutions:
z E() = (E),
z () = () (associativity),
z E = E,
z = = .
Note that, in general, the composition of substitutions is not commutative.
Antoni Ligeza
Prolog
10/1
Unification
Substitutions are applied to unify terms and formulae. Unification is a process of
determining and applying a certain substitution to a set of expressions (terms or
formulae) in order to make them identical. We have the following definition of
unification.
Definition
Let E1 , E2 , . . . , En TER FOR are certain expressions. We shall say that
expressions E1 , E2 , . . . , En are unifiable if and only if there exists a substitution ,
such that
{E1 , E2 , . . . , En } = {E1 , E2 , . . . , En }
is a single-element set.
Substitution satisfying the above condition is called a unifier (or a unifying
substitution) for expressions E1 , E2 , . . . , En .
Note that if there exists a unifying substitution for some two or more expressions
(terms or formulae), then there usually exists more than one such substitution (or
even infinitely many unifiers).
Antoni Ligeza
Prolog
11/1
The Most General Unifier (mgu)
It is useful to define the so-called most general unifier (mgu, for short), which,
roughly speaking, substitutes terms for variables only if it is necessary, leaving as
much place for possible further substitutions, as possible.
Definition
A substitution is a most general unifier for a certain set of expressions if and only
if, for any other unifier of this set of expressions, there exists a substitution , such
that = .
The meaning of the above definition is obvious. Substitution is not a most general
unifier, since it is a composition of some simpler substitution with an auxiliary
substitution .
In general, for arbitrary expressions there may exist an infinite number of unifying
substitutions. However, it can be proved that any two most general unifiers can differ
only with respect to variable names. This is stated with the following theorem.
A Theorem
Let 1 and 2 be two most general unifiers for a certain set of expressions. Then, there
exists a one-to-one renaming substitution such that 1 = 2 and 2 = 1 1 .
Antoni Ligeza
Prolog
12/1
An example
Example
As an example consider atomic formulae p(X, f (Y)) and p(Z, f (Z)). The following
substitutions are all most general unifiers:
z = {X/U, Y/U, Z/U},
z 1 = {Z/X, Y/X},
z 2 = {X/Y, Z/Y},
z 3 = {X/Z, Y/Z}.
All of the above unifiers are equivalent each of them can be obtained from another
one by applying a renaming substitution. For example, = 1 for = {X/U}; on
the other hand obviously 1 = 1 .
Antoni Ligeza
Prolog
13/1
Unification Algorithm An Idea
z It can be proved that if the analyzed expressions are terms or formulae, then
there exists an algorithm for efficient generating the most general unifier,
provided that there exists one; in the other case the algorithm terminates after
finite number of steps . Hence, the unification problem is decidable.
z The basic idea of the unification algorithm can be explained as a subsequent
search through the structure of the expressions to be unified for inconsistent
relative components and replacing one of them, hopefully being a variable, with
the other.
z In order to find inconsistent components it is useful to define the so-called
disagreement set.
z Let W TER FOR be a set of expressions to be unified. A disagreement set
D(W) for a nonempty set W is the set of terms obtained through parallel search
of all the expressions of W (from left to right), which are different with respect
to the first symbol. Hence, the set D(W) specifies all the inconsistent relative
elements met first during the search.
Antoni Ligeza
Prolog
14/1
Unification Algorithm
Algorithm for Unification
1
Set i = 0, Wi = W, i = .
If Wi is a singleton, then stop; i is the most general unifier for W.
Find D(Wi ).
If there are a variable X D(Wi ) and a term t D(Wi ), such that X does not
occur in t, then proceed; otherwise stop W is not unifiable.
Set i+1 = {X/t}, Wi+1 = Wi {X/t}.
Set i = i + 1 and go to 2.
Antoni Ligeza
Prolog
15/1
Application Example
Consider two atomic formulae p(X, f (X, Y), g(f (Y, X))) and p(c, Z, g(Z)). The
following steps illustrate the application of the unification algorithm to these atomic
formulae.
1
i = 0, W0 = {p(X, f (X, Y), g(f (Y, X))), p(c, Z, g(Z))}, 0 = {}.
D(W0 ) = {X, c}.
1 = {X/c}, W1 = {p(c, f (c, Y), g(f (Y, c))), p(c, Z, g(Z))}.
D(W1 ) = {f (c, Y), Z}.
2 = {X/c}{Z/f (c, Y)} = {X/c, Z/f (c, Y)},
W2 = {p(c, f (c, Y), g(f (Y, c))), p(c, f (c, Y), g(f (c, Y)))}.
D(W2 ) = {Y, c}.
3 = {X/c, Z/f (c, Y)}{Y/c} = {X/c, Z/f (c, c), Y/c},
W3 = {p(c, f (c, c), g(f (c, c))), p(c, f (c, c), g(f (c, c)))}.
Stop; the most general unifier is 3 = {X/c, Z/f (c, c), Y/c}.
Antoni Ligeza
Prolog
16/1
Properties of the Unification Algorithm
Theorem
1
If W is a finite set of unifiable expressions, then
1
2
the Unification Algorithm always terminates at step 2 and
it produces the most general unifier for W.
Moreover, if the expressions of W are not unifiable, then the algorithm
terminates at step 4.
Antoni Ligeza
Prolog
17/1
Unification in Prolog
From: R. Bartak:
http://kti.mff.cuni.cz/~bartak/prolog/data_struct.html
Unification Algorithm defined in Prolog
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
unify(A,B):atomic(A),atomic(B),A=B.
unify(A,B):var(A),A=B.
% without occurs check
unify(A,B):nonvar(A),var(B),A=B. % without occurs check
unify(A,B):compound(A),compound(B),
A=..[F|ArgsA],B=..[F|ArgsB],
unify_args(ArgsA,ArgsB).
unify_args([A|TA],[B|TB]):unify(A,B),
unify_args(TA,TB).
unify_args([],[]).
Antoni Ligeza
Prolog
18/1
A final problem
Question
Are X and f (X) unifiable?
Example
What is/should be the result of:
?- X=f(X).
?- X=a, X=f(X).
?- X=f(X), write(X).
Antoni Ligeza
Prolog
19/1