next up previous contents
Next: A unification algorithm Up: Introducing types into the Previous: Type inference as equation   Contents

Unification

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 $a$, $b$, $f$, $p$, ...to denote function symbols and capital letters like $X$, $Y$, ...to denote variables. An instance of the unification problem is a system of equations, like the following:


\begin{displaymath}
\begin{array}{lcl}
f(X) & = & f(f(a)) \\
g(Y) & = & g(Z)\\
\end{array}\end{displaymath}

To solve this system, we have to assign values to the variables $X$, $Y$ and $Z$. 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 $X$ the term $f(a)$, $Y$ the term $g(a)$ and $Z$ the term $g(a)$. We write such a substitution as $\{X \leftarrow f(a), Y \leftarrow g(a), Z \leftarrow
g(a)\}$. We can give this substitution a name, for instance $\theta$.

Given a term $t$ and a substitution $\theta$, the effect of applying $\theta$ to $t$ is usually written $t \theta$ (note that this convention reverses the usual notation for function application). The term $t \theta$ is the result obtained by uniformly mapping the variables in $t$ according to the assignments dictated by $\theta$. For instance, in the example above, if $\theta = \{X \leftarrow f(a), Y
\leftarrow g(a), Z \leftarrow g(a)\}$ and $t = f(X)$, then $t \theta
= f(f(a))$. We can easily verify that $f(X) \theta = f(f(a)) \theta$ and $g(Y) \theta = g(Z)\theta$, so $\theta$ 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 $g(p(X),q(f(Y)))$ and we apply the substitution $\gamma = \{X \leftarrow Y, Y \leftarrow f(a)\}$ to this expression, we get a new expression $g(p(Y),q(f(f(a))))$. The point to note is that subexpression $p(Y)$ obtained by the assignment $X \leftarrow Y$ is not transformed transitively into $p(f(a))$ by the assignment $Y \leftarrow f(a)$ in $\gamma$.

Coming back to the example above, the substitution $\theta = \{X \leftarrow f(a), Y
\leftarrow g(a), Z \leftarrow g(a)\}$ is not the only solution to the set of equations


\begin{displaymath}
\begin{array}{lcl}
f(X) & = & f(f(a)) \\
g(Y) & = & g(Z)\\
\end{array}\end{displaymath}

Another, simpler, solution is $\theta' = \{X \leftarrow f(a), Y
\leftarrow a, Z \leftarrow a\}$. Yet another solution is $\theta'' =
\{X \leftarrow f(a), Y \leftarrow Z\}$. 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 $\theta''$. For instance, $\theta$ is the same as $\theta''$ followed by the substitution $\{Y \leftarrow g(a)\}$. 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 $ p(Z) = q(f(Y))$ with different outermost function symbols, no substitution for $Z$ and $Y$ can make these terms equal.

We also have a problem with equations of the form $ X = f(X)$. Since $X$ occurs in $f(X)$, any substitution for $X$ will also apply to the nested $X$ within $f(X)$ 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.



Subsections
next up previous contents
Next: A unification algorithm Up: Introducing types into the Previous: Type inference as equation   Contents
Madhavan Mukund 2004-04-29