next up previous contents
Next: Type inference with shallow Up: Unification Previous: Unification   Contents

A unification algorithm

The algorithm to find a most general unifier proceeds as follows.

We claim that this algorithm terminates with a most general unifier. Termination follows from the fact that the first four transformation rules can be used only a finite number of times without using rule 5. Let $n$ be the number of distinct variables in the original set of $m$ equations. Rule 5 can be used at most one time for each variable. Thus, overall, the five transformations can be applied only a finite number of times.

When no rules apply, every equation is of the from $X = t$ and each variable $X$ that appears on the left hand side of an equation does not appear anywhere else in the set of equations. Thus, the resulting set of equations defines a substitution


\begin{displaymath}
\{X_1 \leftarrow t_1, X_2 \leftarrow t_2, \ldots, X_n \leftarrow t_n\}
\end{displaymath}

We have to argue that this is a most general unifier. First, we argue that it is a unifying substitution. This follows from the fact that each of the transformations in the algorithm preserves the set of unifiers. Arguing that it is an mgu is more complicated and we omit the proof.

Here is an example of how the algorithm works. We start with the equations


\begin{displaymath}
g(Y) = X, f(X,h(X),Y) = f(g(Z),W,Z)
\end{displaymath}

Applying rule 1 to the first equation and rule 3 to the second equation, we get


\begin{displaymath}
X = g(Y), X = g(Z), h(X) = W, Y = Z
\end{displaymath}

Using rule 5 on the second equation, we replace $X$ by $g(Z)$ everywhere to get


\begin{displaymath}
g(Z) = g(Y), X = g(Z), h(g(Z)) = W, Y = Z
\end{displaymath}

Applying rule 3 to the first equation we get


\begin{displaymath}
Z = Y, X = g(Z), h(g(Z)) = W, Y = Z
\end{displaymath}

Using rule 5 on the last equation to replace $Y$ by $Z$ and eliminating the resulting equation $Z = Z$ by rule 2, we get


\begin{displaymath}
X = g(Z), h(g(Z)) = W, Y = Z
\end{displaymath}

Finally, we reverse the second equation by rule 1 to get


\begin{displaymath}
X = g(Z), W = h(g(Z)), Y = Z
\end{displaymath}

No further rules apply, so the most general unifier is $\{X \leftarrow
g(Z), W \leftarrow h(g(Z)), Y \leftarrow Z\}$.

The algorithm we have described is different from Robinson's original algorithm, whose correctness is more difficult to establish. The most efficient algorithms for unification work in linear time.


next up previous contents
Next: Type inference with shallow Up: Unification Previous: Unification   Contents
Madhavan Mukund 2004-04-29