In general, for type inference we need a systematic method to perform the intuitive computation described above. The solution we seek to our system of equations is a special type of function, called a substitution, that maps each variable to an expression in such a way that all the equations are satisfied. In order to find the ``most general'' solution to a system of equations involving variables, we have to identify a substitution with ``least constraints''.
This problem is called unification and the ``least constrained'' substitution map that we seek is called the most general unifier or mgu. In 1965, J.A. Robinson described an algorithm that solves the unification problem. We present a version of the algorithm below.
In general, the unification problem is one of solving equations involving terms over a fixed signature. A signature consists of a collection of function symbols, each of a fixed arity. Constants can be regarded as functions with arity 0. In addition we have variables that can stand for arbitrary terms. Informally, a term is a well-formed expression consisting of function symbols and variables. Formally, terms can be defined inductively as follows:
In the discussion that follows, we use lower case letters like ,
,
,
, ...to denote function symbols and capital letters
like
,
, ...to denote variables. An instance of the
unification problem is a system of equations, like the following:
To solve this system, we have to assign values to the variables ,
and
. The values we assign are terms and, as we mentioned
earlier, an assignment of terms to variables is called a
substitution. For instance, we could assign
the term
,
the term
and
the term
. We write such a
substitution as
. We can give this substitution a name, for instance
.
Given a term and a substitution
, the effect of applying
to
is usually written
(note that this
convention reverses the usual notation for function application). The
term
is the result obtained by uniformly mapping the
variables in
according to the assignments dictated by
.
For instance, in the example above, if
and
, then
. We can easily verify that
and
, so
is a solution to the
unification problem given above.
Substitutions are applied in parallel, simultaneously. For instance,
if we have an expression of the form
and we apply
the substitution
to
this expression, we get a new expression
. The
point to note is that subexpression
obtained by the assignment
is not transformed transitively into
by the
assignment
in
.
Coming back to the example above, the substitution
is not the
only solution to the set of equations
Another, simpler, solution is
. Yet another solution is
. The last solution is the
``least constrained'' substitution. Any other substitution that solves
the problem can be broken upto into two steps, the first of which is
the substitution
. For instance,
is the same as
followed by the substitution
.
A solution that has this property is called a most general
unifier for the unification problem.
Not all unification problems admit a solution. For instance, if we
have an equation of the form
with different
outermost function symbols, no substitution for
and
can make
these terms equal.
We also have a problem with equations of the form . Since
occurs in
, any substitution for
will also apply to the
nested
within
and the two terms cannot be made equal.
It turns out that any set of equations in which these two problems do not occur admits a most general unifier.