A Unification Algorithm for Associative-Commutative
Functions
M A R K E. STICKEL
Carnegie-Mellon Umversity, Pittsburgh, Pennsylvania
ABSTRACT An important component of automated theorem-proving systems are unification algorithms
which fred most general substitutions which, when apphed to two expressions, make them equivalent
Functions which are associative and commutative (such as the arithmetic addition and multiphcatton
functions) are often the subject of automated theorem proving An algorithm which unifies terms whose
function is associative and commutauve is presented here Termmaaon, soundness, and completeness of
the algorithm have been proved for a subclass of the general case. The algorithm is an efficient alternative
to other methods of handling associative-commutative functions
KEY WORDS AND PHRASES;
automated
theorem proving, unification
CR CATEGORIES: 3 64, 3 66, 5.21
1. Introduction
C e n t r a l to m o s t t h e o r e m - p r o v i n g systems is a u n i f i c a t i o n a l g o r i t h m that, for a p a i r o f
i n p u t expressions, r e t u r n s a set o f u n i f y i n g substitutions, a s s i g n m e n t s to t h e v a r i a b l e s
o f the expressions, w h i c h m a k e the two expressions i d e n t i c a l o r e q u i v a l e n t . T y p i c a l
is the u n i f i c a t i o n a l g o r i t h m o f R o b i n s o n [13] for u n i f y i n g a t o m i c f o r m u l a s o f the
f i r s t - o r d e r p r e d i c a t e calculus in r e s o l u t i o n t h e o r e m p r o v i n g [1, 6, 13].
T h i s p a p e r considers the case o f u n i f y i n g t e r m s o f the f i r s t - o r d e r p r e d i c a t e calculus
w h e r e the f u n c t i o n is associative a n d c o m m u t a t i v e . S u c h functions a r e m a t h e m a t i cally i m p o r t a n t a n d o f interest to d e v e l o p e r s o f t h e o r e m - p r o v i n g p r o g r a m s . E x a m p l e s
are the a r i t h m e t i c a d d i t i o n a n d m u l t i p l i c a t i o n functions.
T h e case in w h i c h the f u n c t i o n is s i m p l y c o m m u t a t i v e is easily h a n d l e d b y a n
e x t e n s i o n to R o b i n s o n ' s u n i f i c a t i o n a l g o r i t h m , w h i c h unifies t h e a r g u m e n t s o f o n e
t e r m a g a i n s t p e r m u t a t i o n s o f t h e a r g u m e n t s o f the o t h e r term.
T h e case in w h i c h the function is s i m p l y associative is unsolved. M a k a n i n [7]
presents a n a l g o r i t h m for d e t e r m i n i n g the existence o f solutions to t h e r e l a t e d w o r d
e q u a t i o n p r o b l e m for free s e m i g r o u p s b u t does n o t give a n a l g o r i t h m for c o m p u t i n g
all solutions. T h e r e m a y be a n infinite n u m b e r o f unifiers in t h e associative u n i f i c a t i o n
case. F o r e x a m p l e , the t e r m s f ( x a ) a n d f ( a x ) , w h e r e f i s a n associative function, a is
a constant, a n d x is a v a r i a b l e , has unifiers w i t h x -- a, x - - f ( a a ) , x ~ f ( a a a ) . . . . .
Permission to copy without fee all or part of this material is granted provided that the copies are not made
or distributed for direct commercial advantage, the ACM copynght notice and the title of the pubhcauon
and as date appear, and notice is given that copying is by permission of the Association for Computing
Machinery To copy otherwise, or to republish, reqmres a fee and/or specific permission
This research was supported by the National Science Foundation under Grant GJ-28457X2 and by the
Defense Advanced Research ProJects Agency under Contract F44620-73-C-0074
An earher version of this paper was presented at the Fourth International Joint Conference on Artificial
Intelhgence, Tbdisi, Georgia, U S.S.R., September 1975
Author's present address RCA, David Sarnoff Research Center, Pnnceton, NJ 08540
© 1981 ACM 0004-5411/81/0700--0423 $00 75
Journal of the Assooatinn for Computing Machinery, Vol 28, No 3, July t981, pp 423-..434
424
MARK E. STICKEL
Argument lists of associative functions are represented here with no extra parentheses,
for example, f (abe) rather than f(af(be)) or f(f(ab)c).
Two principal approaches to handling associativity or commutativity are available.
The first, the standard approach, is to represent the terms conventionally, for
example, f(af(bc)) or f(f(ab)c) rather than f(abO, and axiomatize the associativity
or commutativity property. The associativity axiom f o r f i s f ( x f ( y z ) =f(f(xy)2), and
the commutativity axiom f o r f i s f ( x y ) = f ( y x ) . These axioms can be used with an
axiomatization of equality in resolution theorem proving or can be applied more
directly using equality inference rules such as paramodulation [12].
In the second approach, special-purpose unification algorithms are provided for
terms whose functions are associative and/or commutative. Examples of this approach are the work of Nevins [8] and Slagle [15]. The algorithms for associativity
and for associativity and commutativity are incomplete; that is, they do not return all
the unifiers in some cases. An example of this approach in the area of programming
languages for artificial intelligence research is the use of the associative tuple and the
associative and commutative bag data types in QA4 and QLISP [14, 17]. In this case,
also, the algorithms for pattern matching (unifying) these expressions are incomplete.
In both cases the incomplete algorithms can be augmented by a process which alters
the input expressions to cause the unification algorithm apphed to the altered
expressions to return additional unifiers. The addition of this process (Slagle's
widening operation for the first-order predicate calculus [15] and Stickel's similar
variable-splitting operation for QA4 and QLISP [16]) results in completeness. Widening and variable splitting are operations that must be performed on one or both input
expressions an arbitrary number of times, replacing single variables of the expressions
uniformly by two variables; it is essentially repeated paramodulation by the functionally reflexive axiom.
An example of the latter approach is the unification off(abz) andf(xy), where
f is associative and commutative, a and b are constants, and x, y, and z are variables. The associative-commutative unification algorithm would return the unifiers
{x ~-- a, y ~--f(bz)}, {x ~ b, y ~--f(az)}, {x ~-- z, y ~--f(ab)}, {x ~-f(bz), y ~- a},
{x ~-f(az), y ~ b}, and {x ~---f(ab), y ~ z}. This is an incomplete set of unifiers
since z is always entirely contained in x or y; the possibility that part of the value of
z is in x and the rest in y is not represented. After a widening operation is performed
onf(abz), which results inf(abzlz2) by instantiating z byf(zlz2), additional unifiers
such as {x ~--f(azO, y ~--f(bz2), z ,~--f(zlz2)} and {x ~--f(abZl), y ~ z2, z ,,-f(zlz2)}
are returned by the unification algorithm.
Related to this approach is Plotkin's work on the theory of building in equational
theories [10], of which associativity and commutativity are examples. In the case of
associativity, Plotkin retains terms in a normal, that is, right-associative, form,
although it could equivalently have been our unparenthesized form. His equivalent
of the widening rule, the replacement of a variable by two new variables, is applied
continually inside the unification algorithm rather than being used outside it. Thus
his unification algorithm may generate an infinite number of unifiers, as opposed to
a unification algorithm guaranteed to produce a finite number of unifiers, and a
potentially infinite process (widening) for altering inputs to the unification algorithm
to obtain additional unifiers.
This paper presents the AC unification algorithm for terms whose functions are
associative and commutative. The algorithm eliminates the need for using both the
associativity and commutativity axioms and the widening or variable-splitting process. It is proved to be a complete unification algorithm for a class of expressions
Unification A lgorithm for Associative- Commutative Functions
425
incorporating associative-commutative functions. It has been successfully used in
research with complete sets of reductions [3, 4, 9].
A similar algorithm was developed independently by Livesey and Siekmann I51,
who also extend it to the associative--commutative-idempotent case. The state of the
art in special unification algorithms is surveyed in [11].
2. Terminology
Definition.
A term is defined to be
(1) a constant,
(2) a variable, or
(3) a function symbol succeeded by a list of terms (the arguments of the function).
The symbols a, b, and c are used to represent constants; x, y, and z are used to
represent variables; a n d f a n d g are used to represent functions.
Definition. A substitution component is an ordered pair of a variable v and a term
t written as v ~- t. A substitution component denotes the assignment of the term to
the variable or the replacement of the variable by the term.
Definition. A substitution is a set of substitution components with distinct first
elements, that is, distinct variables being substituted for. Applying a substitution to
an expression results in the replacement of those variables of the expression included
among the first elements of the substitution components by the corresponding terms.
The substitution components are applied to the expression in parallel, and no variable
occurrence in the second element of a substitution component is replaced even if the
variable occurs as the first element in another substitution component. Substitutions
are represented by the symbols o, 0, and ~. The application of substitution 0 to
expression A is denoted by A0. The composition of substitutions 0o denotes the
substitution whose effect is the same as first applying substitution 0, then applying
substitution o; that is, A (0o) = (A 0)0 for every expression A.
Definition. A unifying substitution or unifier of two expressions is a substitution
which when applied to the two expressions results in equivalent expressions. In
ordinary unification, two expressions are equivalent if and only if they are identical.
In unification of argument lists of commutative functions, two expressions are
equivalent if they have the same function symbol and equivalent arguments in the
same or different order.
Definition. Term s is an instance of term t, and t is a generalization of s, if there
exists a substitution 0 such that tO = s.
Substitution 0 is an instance (generalization) of substitution o if, for every term t,
tO is an instance (generalization) of to.
3. The AC Unification Algorithm
An algorithm for unifying two terms whose function is associative and commutative,
called the AC unification algorithm, is presented below. Terms are represented as if
the function had an arbitrary number of arguments.
It is assumed that the argument lists of the two terms being unified have no
common arguments. This presents no difficulty, since no unifiers are lost and
efficiency is gained if common arguments are eliminated immediately. This is done
by removing common arguments a pair at a time, one from each of the argument
426
MARK E. STICKEL
TABLE I
Solution
number
x~
x2
x3
y~
y2
2xl + x2 + xs
2y~ + y2
1
2
3
4
5
6
7
0
0
0
0
0
1
1
0
1
0
1
2
0
0
1
0
2
1
0
0
0
0
0
1
1
1
0
1
1
1
0
0
0
2
0
1
1
2
2
2
2
2
1
1
2
2
2
2
2
zl
z2
z3
z~
z~
z6
z7
lists. For example, before unifyingf(xxyabc) andf(bbbcz), the b's and c's common
to the two terms are removed, yieldingf(xxya) andf(bbz). An example of the utility
of immediately removing common arguments is the unification of f ( g ( x ) y ) and
f ( g ( x ) g ( a ) ) . If the g(x)'s common to the two terms are immediately removed, the
unification algorithm will return the most general unifier { y ~ g(a)). If the common
g(x)'s are retained, unification will probably also result in the generation of the
additional, less general unifier {x ~-- a, y ~-- g(a)}.
3.1 VARIABLE-ONLYCASE. The algorithm is expressed partially in terms of an
algorithm for the complete unification of terms with an associative and commutative
function with only variables as arguments. Conceptually, the result of unifying such
terms is an assignment to each variable of the terms some sequence of terms.
Each variable is assigned a term t, (whose function symbol is not f ) or a term
f(t~ 1 . . . t,~~) (with n, occurrences of term t, as arguments o f f ) . For such an assignment to be a unifier, the only requirement is that for each term t~ used in any
assignment, there be the same number of occurrences of that term as arguments of
f in each of the unified terms instantiated by the assignment. For example, {Xl
f(bb), x~ ~-- f(ab), x3 ~ a, yl ~-- b, y2 ~-- f(aabbb)} is a unifier of f(xlx~x2x~) and
f ( y l y l y 2 ) since there are two a's and five b's in the instantiations o f f ( x l x l x 2 x 3 ) and
f(yly~y2), with the unified term beingf(aabbbbb).
With each pair of terms involving an associative and commutative function with
only variable arguments is associated a single equation representing the number and
multiplicity of variables in each term. For example, the equation 2xl + x2 + xa =
2yl + y2 is associated with the pair of terms given in the preceding paragraph.
Nonnegative integral solutions to such equations can be used to represent unifiers.
The solutions must be nonnegative integral since each variable must be assigned a
nonnegative integral number of occurrences of each term.
In order to generate all the solutions to the problem of unifying the two terms, it
is necessary to be able to represent all the solutions to the equation derived from the
terms. Every nonnegative integral solution to the equation can be represented as a
sum of elements of a particular finite set of nonnegative integral solutions to the
equation, a basis set for the nonnegative integral solution space. The finite set of
nonnegative integral solutions by whose addition the entire nonnegative integral
solution space is spanned is generable by generating solutions to the equation in
ascending order of value, eliminating those solutions composable from those previously generated. This process can be made finite by placing a bound on the maximum
solution value which will be used; such a maximum is proved in a lemma to eliminate
no needed solutions.
Consider the equation 2Xl + x 2 + x 3 = 2 y l + f12. Solutions to the equation are
given in Table I.
Associated with each solution above is a new variable not contained in the original
Unification Algorithm for Associative- Commutative Functions
427
terms. T h e assignment o f as m a n y occurrences o f that variable as specified in the
solution to each o f the variables o f the original term results in a partial solution to
the unification o f the original terms. F o r example, the assignment o f two occurrences
o f variable z3 to xa a n d one occurrence toy1 results in an equal n u m b e r o f occurrences
o f variable za in each off(xlxlx2xa) andf(yly2).
Every nonnegative integral solution to the e q u a t i o n is a nonnegative integer
weighted sum o f the seven solutions presented above; that is, every solution is
representable as Xl = z6 + zv, x2 = z2 + z4 + 2z5, x3 = zx + 2z3 + z4, yl ffi z3 + z4 +
z5 + z7, y2 = za + z2 + 2z6 for some nonnegative integral values o f zl, . . . , ZT. However,
not every solution to the equation is a solution to the unification p r o b l e m f r o m which
the equation was derived. There is an additional constraint that each variable o f the
original terms must have at least one term in its value; it c a n n o t have zero terms in
its value.
Hence, that subset o f the 2 7 - - 128 sums for which each element o f the 5-tuple is
nonzero must be formed. There are 69 such sums including, for example (representing
the sum by the set o f its indices), {2, 3, 6}, (1, 2, 3, 6}, a n d {4, 6}, with associated
unifiers
{x, ~-- z6, x2 ~ z2, x3 ~'--f(z3z3), yl ~'- z3, y2 ~'-f(z2z6z6)},
{Xl <-'- Z6, X2 ~
Z2, X3
~--f(ZlZ3Z3), 71
(--" Z3, ]2
~-'f(ZlZ2Z6Z6)},
{x, ~-- z6, x2 ~-- z4, x3 ~ z4, y, ~-- z4, )'2 ~--f(z6z6)).
To be more precise in the definition o f the algorithm, it consists o f the following
steps:
1 Elunmate common terms
2 Form an equaUon from the two terms where the coefficient of each variable in the equation as equal to
the mulUphclty of the corresponding variable m the term.
3. Generate all nonnegaUve integral soluUons to the equaUon, ¢lunmatmg all those soluuons composable
from other soluuons.
4 Associate with each soluUon a new vanable.
5 For each sum of the soluUons (no soluuon occurnng m the sum more than once) w i t h no zero
components, assemble a unifier composed of assignments to the original variables with as many of each
new variable as specified by the soluUon element m the sum associated with the new variable and the
ongmal variable.
3.2 GENERAL CASE. T h e algorithm for unifying general terms with associative
and c o m m u t a t i v e functions using the algorithm for the variable-only ease is presented
below. T h e general case concerns terms whose function is associative a n d c o m m u tative with arbitrary arguments, that is, arguments that m a y contain ordinary
(nonassociative, n o n c o m m u t a t i v e ) functions, f, or other functions which are assoeiaUve and commutative. T h e presence o f ordinary unification is assumed to deal with
the non-associative-commutative aspects o f unification.
First, w h e n unifying two terms, two new terms, called the variable abstraction o f
the original two terms, with only variable arguments, are f o r m e d b y uniformly
replacing distinct arguments by new variables. These new terms have only variable
arguments and are generalizations o f the original two terms. F o r example, in unifying
f ( x x y a ) and f(bbz), the generalizations f(xlxlx2xa) and f(ylyly2) with substitution
{Xl ~ x, x2 ~---y, x3 ~ a, yl ,-- b, y2 "~- z} are formed.
Next, using the previous algorithm for the variable-only case, the generalizations
are unified. This has already been done for the example above, resulting in 69
unifiers as stated previously.
N o w there exist generalizations o f the two original terms, a substitution to
428
MARK E. STICKEL
instantiate them to the original terms, and a complete set of their unifiers. Every
unifier of the original terms is a simultaneous instance of the substitution to instantiate
the generalizations to the original terms and a unifier of the generalizations. All that
is necessary to get all the unifiers of the original terms is to unify (for each variable
being substituted for) the value in the substitution and the value in the unifiers.
In the example, x0 must have value a and yl must have value b. Thus any unifier
off(xlx~x2xo) andf(y~y~y2) which assigns to x3 ory2 a nonvariable, that is, a term of
the form f(...), .), may be immediately excluded from consideration, since its unification
with the assignment including x3 *- a and yl *- b will fail. This constraint can, and
for efficiency probably should, be applied during the generation of sums of solutions
to the equation rather than afterward. This constraint eliminates 63 of the 69 unifiers,
leaving sums (1) {4, 6}, (2) {2, 4, 6}, (3) {1, 5, 6}, (4) {1, 2, 5, 6}, (5) {1, 2, 7}, and
(6) { 1, 2, 6, 7}, with associated unifiers
( l ) {X1 '6"Z6, X2
(2) {xl ~ z6, x2
(3) {xl * - z6, x2
(4) {xl ~ z6, x2
(5) (X1 ~
~--f(z6z6)},
*"-f(z2z4), x3 ~ z4, y l ~ z4, y2 *-'-f(z2z6z6)),
* - f ( z s z s ) , x3 ~ zl, yl ~ zs, y2 <-"f(ZxZ6Z6)},
* - f ( z 2 z s z O , x3 ~ Zl, y l ~ Z5, y2 <--f(ZlZ2Z6Z6)},
'6-'Z4, X3 <-" Z4, yl ~-- Z4, y2
ZT, X2 ~
Z2, X3 ~
Zl, y l ~
ZT, y2
<"f(ZlZ2)},
(6) {X1 ~"f(z6ZT), X2 ~ Z2, X3 ~ Za, yl ~ Z7, yZ ~"f(zlz2z6Z6)}.
Unifying each of these with (xl ~ x, x2 ~ y, x3 ~ a, yl ~ b , y2 ~ z}, the
following are obtained:
(1)
(2)
(3)
(4)
(5)
(6)
no
no
{x
{x
{x
(x
unifier, since z4 ~ a and z4 ~ b are not unifiable;
unifier, since z4 ~ a and z4 ~ b are not unifiable;
~ z6,y , - f ( b b ) , z *.-f(az6z6)) (ffi{y ~--f(bb), z ~---f(axx)});
,--z6, y *--f(bbz2), z ~--f(az2zoz6)} ( - - { y *--f(bbz2), z ~--f(az2xx)});
~ b, y ~ z2, z *-.-f(az2)} (ffi{x ~ b, z *.--f(ay)});
~.--f(bz6), y ~ z2, z *-.-f(azzz6z6)} (-- {x *-f(bz6), z ~---f(ayz6z6))).
This is a complete set of unifiers o f f ( x x y a ) a n d f ( b b z )
Since xa and y~ of the variable-only case correspond to a and b, respectively, and
a and b are not unifiable, any sum including solution 4 to the equation 2x~ + xz +
Xa -- 2y~ + y2 can be excluded from consideration since it would require (as in (1)
and (2) above) the unification of a and b. As with the constraint on variables
corresponding to nonvariable terms not being assigned more than one variable (terms
of the form f ( . . . ) ) in the variable-only case, this latter constraint on solutions can,
and probably should, be applied during the generation of unifiers in the variableonly case rather than afterward. Elimination of solution 4 before generation of the 2n
sums and elimination of sums which do not meet the first constraint would result in
the formation only of unifiers (3)-(6) of the variable-only case, each of which has a
corresponding unifier in the general case.
More precisely, the algorithm consists of the following steps:
1. Form generahzatlons (the variable abstraction) of the two terms by replacing each distinct argument by
a new variable.
2 Use the algorithm for the vanable-only case to generate umfiers for the generahzaUons of the two
terms The variable-only-case algorithm may be constrained to eliminate the generation of umfiers
assigning more than one term to vanables whose value must be a single term, and the generatmn of
urn fiefs which will reqmre the later umfication of terms which are obviously not unifiable.
3 Unify for each variable in the subsUtution from step 1 and the umfiers from step 2 the variable values
and return the resulting assignments for variables of the original terms. This is a complete set of umfiers
of the original terms
Unification A lgorithm f o r A s s o c i a t i v e - C o m m u t a t i v e F u n c t i o n s
429
4. Termination, Soundness, a n d C o m p l e t e n e s s
The validity of eliminating arguments c o m m o n to the two terms will first be
established. This will be done by proving that any unifier of the terms is a unifier of
the terms with a pair o f c o m m o n arguments removed, and vice versa.
THEOREM 1. L e t st . . . . . sin, tt . . . . . tn be t e r m s with & = t j f o r s o m e i, j . L e t 0 be
a unifier o f f ( s x . . . sin) a n d f ( h . . . t,~) a n d o be a unifier o f f ( s 1 . . . s,-ts,+t . . . Sm)
a n d f ( t l . . . tj-~tj+a . . . t,). Then (1) 0 is a unifier o f f ( s a . . . s,_as,+~ . . . Sin) a n d
f ( h " " tj-~t~+~ . . . tn), a n d (2) o is a unifier o f f ( s a . . . Sin) a n d f ( t l . . . tn).
PROOF
(1)
f(&Of(Sl ... S,-lS,+l...
s i n ) O ) = f ( s l " ' " S m ) O - - - - f ( t l " ' " tn)O
=f(bOf(tl ... b-d,+,."
tn)O),
and s,O = bO. Therefore,
f ( s l . . . s,-ls,+l . . . sm)O = f ( t l
. . . t,-lt,+l . . . tn)O,
and 0 is a unifier off(s1 . . . s,-~s,+l . . . Sm) a n d f ( t l . - . b-~b+t "'" tn).
(2)
f ( s l . . . s,-ls,+l . . . Sm)O = f ( t l " ' "
t , - t t j + l " ' " t,)o,
and s,o = bo. Therefore,
f ( s , o f ( s l . . . s,-,s,+l . . . s i n ) o ) = f ( s ~ . . .
Sm)O = f ( t t " "
tn)o
- - f ( t j o f ( t t . . . t j - l t ~ + t " " tn)o),
and o is a unifier of t(st . . . sin) a n d f ( t l . . . tn).
[]
The l e m m a below establishes that every nonnegative integral solution to an
equation of the form alXl + . . . + amXm = bly~ + . . . + bnyn is composable as a
nonnegative integral weighted sum o f a fixed finite set o f nonnegative integral
solutions. It is also establishes a solution value within which all the nonnegative
integral solutions in the set m a y be found.
LEMMA 1. E v e r y nonnegative integral solution (Xl. . . . . Xm, y t . . . . . yn) tO the
equation a~xt + . . . + amXm = b~yl + "'" + bnyn with positive integral coefficients
at . . . . . am, bt . . . . . bn is an additive linear combination o f nonnegative integral solutions
with value a~x~ + . . . + amXm ( = b l y~ + • . . + bnyn) less than or equal to the m a x i m u m
o f m a n d n times the m a x i m u m o f the least c o m m o n multiples o f p a i r s o f numbers, one
f r o m at . . . . . am and one f r o m bl . . . . . bn.
PROOF. Assume with no less o f generality that the least c o m m o n multiple (lcm)
of a~ and bm is the m a x i m u m of the least c o m m o n multiples and that m _> n. The
p r o o f is by induction on the value of a solution k.
k--0.
The solution w i t h k = 0 w i t h x ~
=0 ..... XmfO, yt =0 .... ,ynf0is
generable as the additive linear combination o f nonnegative integral solutions with
value less than or equal to m x lcm(al, bl) with zero coefficients.
Assume the l e m m a is true for every nonnegative integral solution with value less
than or equal to k. We prove it is true for k.
Case 1. k <_ m x lcm(a~, bl). In this case the solution is included a m o n g the
nonnegative integral solutions with value less than or equal to m x lcm(a~, bl), and
the l e m m a is true.
430
MARK E. STICKEL
Case 2. k > m x lcm(ax, bl). Since alxl + . . . + amXm ---- k > m X lcm(al, bl)
and each a,x, _> 0, at least one a/x, must be greater than lem(a~, bl), and x, must
be greater than lcm(a~, b~)/ai. Similarly, since bly~ + . . . + bnyn -- k > m x
lcm(al, b0, each bjyj _> 0, and n _< m, at least one bayj must be greater than
lcm(al, bl), and ya must be greater than lcm(a~, b~)/bj. Consider the solution with
x, -- lcm(a,, bj)/a,, yj -- lcm(a~, b~)/b~, and all other variables zero. This is just the
solution in lowest terms involving only x, and yj and has value lcm(a~, bj) _<
lcm(al, bx). Since lcm(al, bl)/a, _> lcm(a,, bj)/a, and lcm(al, bl)/bj _> lcm(a,, bj)/ba by
the maximality of lcm(a~, bl), the solution involving only x, and y~ can be subtracted
from the solution with value k, leaving a nonnegative integral solution as the result.
But this difference solution has value k - lcm(a~, bj) < k and is thus composable
from solutions with value less than or equal to m × lcm(al, b~). Therefore the solution
with value k > m x lcm(a~, bl) is the sum of some solution involving only x~ and ya
with value less than or equal to m × lcm(a~, bl) and some other set of solutions with
value less than or equal to m x lcm(al, b~), and the lemma is true for this case. []
The lemma proves an upper bound on solution values that must be examined in
the determination of a complete set of nonnegative integral solutions which span the
normegative integral solution space by addition. Substantially more effective algorithms for computing the spanning set have been developed by Huet [2].
THEOREM 2. The A C unification algorithm f o r terms with associative and commutative function with only variables as arguments always terminates, is sound (returns no
substitutions which are not unifiers), and is complete (every unifier is an instance o f a
returned unifier).
PROOF. The algorithm is guaranteed to terminate, since it performs a finite
number of operations on the finite number of nonnegative integral solutions generated from the equation corresponding to the two terms. The generation of these
solutions is finite owing to the trial solution values being bounded.
The algorithm is sound, since each solution of the derived equation causes the
introduction into each of the instantiated terms of an equal number of new variable
occurrences. Thus the two instantiated terms have the same number of occurrences
of each new variable and are therefore unified.
Any unifier must assign to each variable a term of the form t, (whose function
symbol is not f ) or a termf(t7 x . . . t~m) (with n, occurrences of term t, as arguments
o f f ) . Let k be the cardinality of the set of such terms t, in any solution to the
unification of a pair of terms with only variables as arguments. The two instantiated
terms must have an equal number of occurrences of each of these k terms as
arguments off. That is, alC~l + . . . + amC,m ---- bld, l + . . . + bnd,n (l __<i __<k), where
m is the number of distinct variables in the first term being unified, n is the number
of distinct variables in the second term, aj is the multiplicity of the j t h variable in the
first term, b~ is the multiplicity of t h e j t h variable in the second term, cv is the number
of occurrences of term i in variable j in the first term, and d v is the number of
occurrences of term i in variable j in the second term.
Each tuple (c,x. . . . . C,m, d,t . . . . . d,n) is a solution to the equation alXl + . . . +
amXm = b l y l + . ' . + bnyn corresponding to the terms being unified. It can thus
(according to the lemma) be formed as the sum of certain nonnegative integral
solutions to the equation weighted by positive integers.
Consider the unifier corresponding to the sum of all those solutions to the equation
which are required in the formation of any of the tuples (c,1. . . . . C,m, d,~. . . . . d,n).
The hypothesized unifier is an instance of this unifier returned by the algorithm.
Unification A lgorithm f o r Associative-Commutative Functions
431
Include in the value of the new variable associated with each o f these solutions a
n u m b e r of occurrences o f term i equal to the coefficient o f the solution in the
weighted sum. This will result in the proper assignment o f c,j occurrences o f term i
to each variable j of the first term and d,~ occurrences of term i to each variable j o f
the second term.
D o this for each of the k terms in the solution. Let no other or additional terms be
included in the values of the new variables.
This assignment of terms in the solution to new variables associated with equation
solutions generated in the unification process results in the correct n u m b e r c,j or d,j
of each term being assigned to each variable of the original two terms.
Thus any solution to the unification of two terms with only variables as arguments
is an instance o f a returned unifier, and the algorithm is complete. []
Termination, soundness, and completeness is more difficult to prove in the general
case, since intermediate unification steps can have more apparent complexity than
the original owing to the introduction of variables in the AC unification algorithm.
The following theorem proves termination, soundness, and completeness for a
subclass o f general terms, which includes the variables-and-constants-only case.
Experience suggests much wider applicability of the algorithm, and no counterexample has yet been discovered to its termination. (Each step o f the algorithm is
soundness and completeness preserving; the only remaining issue is whether it will
fail to terminate owing to increasing term complexity.)
THEOREM 3. The A C unification algorithm terminates, is sound, and is complete
f o r terms composed with associative-commutative and ordinary functions such that no
variable that is an argument to an associative-commutative function symbol occurrence
is an argument to any other function symbol occurrence.
PROOF. The p r o o f relies upon a complexity measure on unification problems. Let
c(s, t) = (m, n), where m and n are, respectively, the n u m b e r o f distinct variables and
the n u m b e r of symbols in s and t. Define C(S1, t l ) > C(S2, t2) as ml > mz or (rex = ms
and nl > nz), where c(sl, tl) = (ml, nl) and c(sz, tz) = (ms, n2).
Cases 1 and 2 are basis cases involving one term being a variable or constant for
which unification is immediate regardless of the complexity o f the other term. Case
3 covers another instance of immediate unification when function symbols do not
match.
Case 1. s is a variable. I f t does not contain s, then (s ~ t} is a complete set o f
unifiers. I f t does contain s, then there is no unifier. The argument is similar if t is a
variable.
Case 2. s is a constant (and t is not a variable). I f s = t, the null substitution is a
complete set o f unifiers. I f s ~ t, then there is no unifier. The argument is similar if
t is a constant.
Case 3.
s and t are headed by different function symbols. There is no unifier.
Case 4. s and t are headed by the same ordinary function symbol. Let sl . . . . , sn
and tl . . . . . tn be the arguments o f s and t. Unification o f sl and tl, s~41 • .- 4,-1 and
t,41 . . . 4~-1 . . . . . and sn41 . . . 4n-1 and tn41 . . - 4n-1 terminates, is sound, and is
complete by induction, where 4, denotes each unifier o f St41 ° ' " 4,--1 and t,41 . . .
4,-1. c(s, t) > c(s,41 . . . 4~-1, t,41 . . . 4~-1), since if s~4~ . . . 4,-1 # s~ or t,4a - - .
4,-1 # t,, then s,41 • • • 4,,-1 and t,41 • • • 4~-1 combined must have fewer variables than
s and t; and if s,41 . . . 4~-1 = s~ and t~41 . - " 4~-1 = t,, then s,41 . . . 4,-1 and t~41 . - -
432
MARK E. STICKEL
¢i-1 have fewer symbols than s and t. The set of all ffl . . . ,~n is a complete set of
unifiers of s and t.
Case 5. s and t are headed by the same associative-commutative function symbol
f. Let s* and t* be the variable abstraction of s and t. s* and t* are generalizations
of s and t; that is, s * 8 = s and t*O -- t for some 8 of the form {x~ ~ c~. . . . . xn
c,,}, where each x, is a new variable and each c, is the term in s and t it replaces in s*
and t*.
Let o denote each unifier of s* and t* returned by the unification algorithm for
terms with associative-commutative functions with only variables as arguments, o is
of the form {xl ~- di, . . . , xn ~- dn}, where each x, is a variable of s* or t* and d, is
the term (either a variable o r f a p p l i e d to variables) assigned to it by the unification
algorithm. According to the previous theorem, unification terminates, is sound, and
is complete for the variable-only case.
Unification of cl and dl . . . . . c~ffi . . - ~,-1 and d, ckl . . . ~k,-1. . . . . en¢l . . . ~kn-i and
dn~bl . . . en-~ terminates, is sound, and is complete by induction, where ~b, denotes
each unifier of C,~l
¢,--1 and d,¢l . . . ¢,-1. If c,~1 . . . ¢1-1 o r d,~pl . . . q~,-1 is a
variable, then unification is immediate (case 1). If d,¢~ . . . ¢,-~ is of the form f ( . . . ) ,
then unification is impossible (case 3) unless c,~1 . . . ~,-~ is a variable, since c, cannot
be of the form f(...) .) if s and t are in normal form without arguments headed by f ,
and for c, to be a variable instantiated by previous unification to a term of the form
f ( . . . ) would violate the restriction that no variable appearing as an argument
to an associative-commutative function occurs elsewhere. Otherwise, c,4,~ . . . ~,-~
and d,¢~ . . . ~,-~ must both be instances of subterms of s and t with c(s, t) >
c(c,~kl . . . ¢,,-~, d,¢l - . . ~,-1) since the latter have either fewer variables, as no new
ones can be introduced, or fewer symbols. The set of all o¢~ . . . ¢n is a complete set
of unifiers of s and t. []
" ' °
5. E x t e n s i o n s
The AC unification algorithm can be easily adapted to handle other cases of special
unification: associative-commutative unification with identity (ACId unification)
and assoeiative-commutative-idempotent unification (ACI unification).
5.1 ASSOCIATIVE-COMMUTATIVE UNIFICATION WITH IDENTITY. Associativecommutative unification with identity is the simpler extension. Two modifications to
the AC unification are required to derive the ACId algorithm: (1) in the variableonly case, zero solutions are now allowed, and (2) in the general ease, the ordinary
first-order unification algorithm can no longer be used to unify original terms with
substitutions for variables of the variable abstraction.
In the variable-only case of AC unification, every sum with all nonzero components
of the 2'* (where n is the number of basis solutions to the Diophantine equation) is
generated. In the ACId algorithm, zero values are valid and represent the identity.
The single sum of all the basis solutions and the corresponding substitution are
found. This single substitution comprises a complete set of unifiers in the variable
only case.
For example, ACId unification o f f ( x ~ x l x ~ 3 ) and f ( y l y ~ y 2 ) results in the single
unifier {xx ~ - f ( z n z 7 ) , x2 ~-- f ( z 2 z 4 z s z s ) , Xn ~ - f ( z l z 3 z n z 4 ) , y l ~---f(z3z4znz7), y2
f ( z ~ z 2 z e z e ) } . Each of the previously presented AC unifiers is obtainable from this
unifier by substituting the identity for some of the variables.
In the general ease, unification of the original terms and the substitutions generated
in solving the variable-only case must permit variables to be assigned the identity.
Unification A lgorithm f o r Associative- Commutative Functions
433
I n the A C I d u n i f i c a t i o n o f f ( x x y a ) a n d f(bbz), the s u b s t i t u t i o n s {xl ~---f(z6z7),
X2 ~'-f(z2Z4ZsZs), X3 ~'-f(ZlZaZ3Z4), fll ~-'f(z3z4Z5ZT), ~2 ~-'f(zlz2Z6Z6)}, a n d {Xl ~-- x,
X2 ~ y, X3 ~
a, y~ ~ b, y2 ~ z} m u s t be reconciled.
U n i f y i n g these t e r m s sequentially, t h e f o l l o w i n g results are o b t a i n e d :
x
f(zoz ),
y ~---f(z2z4zszs),
zl ~ a,
z3 ~ e,
z4 ~ e
or
zl ~ e,
z3 ~---e,
z4 ~ a,
w h e r e e is the identity.
U n i f y i n g f(z3z4z~zT) a n d b w h e r e z4 ~ a fails. Unifying f(z~Z4ZsZT) a n d b w h e r e
z~ ~ a, za ~-- e, z4 ~ e yields z~ ~ b, z7 ~ e o r z5 ~ e, z7 ~ b. U n i f y i n g t h e final t e r m
results in
z ~---f(az2z6z6).
T h e resulting substitutions are, thus,
{x ~-- ze, y ~--f(z2bb), z ,~..-f(az2z6z6)}
(ffi{y ~--f(z2bb), z ~ - f ( a z ~ x x ) }
{x ~---f(znb), y ~- z2, z ~--f(az2z6z6)}
( = { z ~-f(z6b), z ~-.-f(ayz6z6)}).
and
A g a i n , these unifiers c a n be specialized to the A C unifiers b y s u b s t i t u t i o n o f the
i d e n t i t y for s o m e o f the variables.
T h e A C I d u n i f i c a t i o n a l g o r i t h m was d i s c o v e r e d b y L i v e s e y a n d S i e k m a n n [5], w h o
first p r e s e n t the A C I d a l g o r i t h m ( w h i c h differs f r o m this o n e since it solves i n h o m o g e n e o u s e q u a t i o n s to solve the g e n e r a l case r a t h e r t h a n using v a r i a b l e a b s t r a c t i o n )
a n d d e r i v e A C u n i f i c a t i o n f r o m it b y a s s i g n m e n t o f the i d e n t i t y to v a r i a b l e s in the
A C I d unifiers. It was also d i s c o v e r e d b y Stickel in the context o f b a g u n i f i c a t i o n with
f r a g m e n t v a r i a b l e s in Q A 4 a n d Q L I S P [14, 17].
5.2 ASSOCIATIW-COMMVTATIVE--IDEMPOT~NT UNIHCAT~ON. L i v e s e y a n d Siekm a n n p r e s e n t a m o d i f i c a t i o n o f A C u n i f i c a t i o n to o b t a i n A C I u n i f i c a t i o n for w h e n
the f u n c t i o n is associative, c o m m u t a t i v e , a n d i d e m p o t e n t [5]. I n A C I unification,
B o o l e a n a l g e b r a is u s e d to solve the e q u a t i o n s r a t h e r t h a n o r d i n a r y i n t e g e r arithmetic.
REFERENCES
1. CHANG,C.L, AND LEE, R C.T. Symbohe Logic and Mechanical Theorem Proving Academic Press,
New York, 1973
2 HUET, G. An algorithm to generate the bas~s of soluUons to homogeneous linear Dlophantine
equations Inf. Process. Left. 7, 3 (Apnl 1978), 144-147.
3 KNUTH, D E, AND BENDIX, P B. Stmple word problems in universal algebras. In Computatwnal
Problems in Abstract Algebras, J. Leech, Ed., Pergamon Press, 1970, pp. 263-297.
4. LANg~ORD, D.S., AND BAtLANTYN~, A.M. Decision procedures for stmple equauonal theories with
commutatlve-assocmuve axioms: complete sets of commutauve-associaUve reductions. Tech. Rep.,
Mathemaucs Dep, Umv of Texas, Austin, Texas, August 1977.
5. LivEs~Y, M., ANO SImC~.~ANN,J. Unificauon of A + C-terms (bags) and A + C + I-terms (sets).
Intern Ber. Nr. 5/76, InsUtut fur Informauk I, Umversitat Karlsruhe, Karlsruhe, W. Germany, 1976
6. LOVELAND,D.W Automawd Theorem Proving: A Logtcal Basis. North-Holland, Amsterdam, 1978.
7. MAKANIN,G.S. The problem of solvabd~ty of equations in a free semigroup. Sowet Akad. Nauk
SSSR 233, 2 (1977)
8. NEVINS, AJ A human oriented logic for automaUc theorem proving. J. ACM 21, 4 (Oct 1974),
606-621.
9. PETERSON,G.E., AND STIClC~L,M.E. Complete sets of reductmns for some equational theories. J.
ACM 28, 2 (Aprd 1981), 233-264
434
MARK E. STICKEL
10. PLOTKIN, G.D Budding-in equational theories. In Machine Intelligence 7, B. Meltzer and D Mlchle,
Eds, Edinburgh University Press, Edinburgh, 1972, pp. 73-90.
11. RAULEFS,P, SIEKMANN,J., AND SZABO0P. A short survey on the state of the art in matching and
umficatlon problems. SIGSAM Bull. 13, 2 (May 1979), 14-20.
12. ROBINSON, G., AND WOS, L. Paramodulation and theorem-proving in first-order theories with
equality. Machine Intelligence 4, B. Meltzer and D. Michie, Eds., Edinburgh Umverstty Press,
Edinburgh, 1969, pp. 135-150.
13. ROBINSON,J.A. A machine-oriented logic based on the resolution principle J ACM 12, 1 (Jan.
1965), 23--41.
14. RUL1FSON, J.F, DERKSEN, J A , AND WALDINGER, R.J. QA4. A procedural calculus for mtumve
reasoning. Tech. Note 73, Artificial Intelhgence Center, Stanford Research Institute, Menlo Park,
Cahf, November 1972
15 SLAGLE,J R Automated theorem-proving for theories wtth simphfiers, commutativtty, and associaUv~ty. J ACM 21, 4 (Oct. 1974), 622-642.
16 STICKEL,M.E Mechamcal theorem proving and artificial mtelhgence languages Ph D. Dissertation,
Dep of Computer Science, Carneg~e-MeUon Umv., Pittsburgh, Pa., December 1977
17. WILBER, B.M. A QLISP reference manual. Tech. Note 118, Aruficlal Intelligence Center, Stanford
Research Insutute, Menlo Park, Cahf., March 1976.
RECEIVED JUNE 1977; REVISEDJULY 1980; ACCEPTEDAUGUST 1980
Journalof the Assoclauonfor ComputingMachinery,Vol 28, No 3, July 1981
© Copyright 2026 Paperzz