Type Unification: Solving Type Equations
Type unification finds replacements for type variables that make given type expressions identical. You encounter it when a programming language tool has already produced equations between types and needs to solve them. Here, the equations are supplied: your task is to simplify them, identify consistent variable bindings, and recognise when no solution exists. We work with finite type expressions built from variables, base types such as Int and Bool, and structured types such as lists, pairs, and functions.
Equations and substitutions
A substitution S acts consistently on every occurrence of a variable. If S={a↦Int}, then applying S to Pair(a,a) gives Pair(Int,Int). You cannot replace just one occurrence and leave the other unchanged.
The governing requirement for a set of equations is
Here, τi and υi stand for the type expressions on the two sides. A substitution satisfying every equation is a unifier of the set.
A type constructor supplies structure. For example, List takes one type argument and Pair takes two. Function types use the binary constructor written A→B, with domain A and result type B.
Constructors are rigid, and argument positions matter. Thus, equal pair types have equal first components and equal second components. Equal function types have equal domains and equal result types. We assume ordinary structural equality, without subtyping or special equations that identify different constructors.
Reducing the equations
Keep the unsolved equations in a worklist. Select one equation and use its structure to decide what to do.
Selected equation | Action | Reason |
|---|---|---|
T=T | Delete it | It imposes no new restriction. |
List(A)=List(B) | Replace it with A=B | Matching constructors require matching arguments. |
A→B=C→D | Replace it with A=C and B=D | Domains and results match separately. |
a=T, with a absent from T | Bind a↦T | A variable can stand for that finite expression. |
Int=Bool | Report failure | Distinct rigid base types cannot become identical. |
a=List(a) | Report failure | The proposed binding would require an infinite expression. |
An equation with a variable on the right can first be reversed. Before binding a variable, perform the occurs check. Delete a=a before this check: an identical-variable equation is harmless.
After a binding, apply it to every remaining equation and update previously recorded bindings. If you record a↦b and later b↦Char, the completed substitution sends both variables to Char.
Preserving generality
A most general unifier solves the equations without imposing unnecessary choices. Every other unifier can be obtained by applying a further substitution to its result, considering the variables in the original problem.
For a=b, the substitution {a↦b} is most general. Choosing {a↦Int,b↦Int} also solves the equation, but unnecessarily fixes the shared type. Most general unifiers need not have identical spelling: reversing a variable binding can describe the same freedom.
Substitution composition records sequential replacement. Writing R∘S means apply S first and then R:
Keep unconstrained variables free. An empty worklist means success, even when some variables remain unspecified. Failure instead requires a constructor conflict or an occurs-check violation under these finite-type assumptions.
What to remember
- Decompose matching constructors by corresponding argument positions.
- Propagate each binding through equations and earlier bindings.
- Reject constructor conflicts and direct or indirect cycles.
- Preserve freedom, then verify the original equations.
Example: Resolving a Compiler's Linked Type Constraints
A compiler component passes the following equations to its type unification solver. The variables a,b,c,d,e,z represent unknown types. The constructors Pair, List, and the function arrow are rigid, and all type expressions are finite.
a) Decompose the first equation and record its variable bindings.
Matching pair constructors require equality of their corresponding components:
The first component fixes a to Int. Reverse the second component equation to put its variable on the left; the replacement for c is List(b). Neither binding contains its own variable on the right, so both pass the occurs check.
The bindings from the first equation are
b) Process the remaining equations and write the completed substitution, leaving any unconstrained variable free.
Apply the recorded replacement for c to the second original equation:
Matching list constructors reduce this to ________. Choose the binding b↦d. Propagate it into the stored replacement for c, giving c↦List(d), and into the function equation:
Matching domains set d to ________, while matching results set e to ________. Apply these bindings to the earlier replacements. In particular, b also becomes Bool, and the list element type in the replacement for c becomes Bool.
The completed substitution is
The variable z remains unchanged. No equation requires a concrete choice for z.
c) Verify the substitution against all three original equations and explain whether it is most general.
Apply the completed substitution to both sides of the first equation. Both sides become
For the second equation, the substituted left side is List(Bool). Its right side is also List(Bool) because d becomes Bool.
For the third equation, the substituted left side is Bool→z, and the substituted right side is the same expression. Every original equation is therefore satisfied.
The equations force a, b, c, and d to the types shown and require e and z to share a type. Any further choice for that shared type can be made by replacing z afterward. Thus, S is a most general unifier: it satisfies every constraint while retaining all remaining freedom.
A compiler solves equations between finite types using type variables, the base type Int, and the constructor List. Match each unification rule to its action. Stored bindings remain part of the solution even after they leave the pending equations. Here, a denotes a type variable and t denotes a type expression.
Remove t=t from the pending equations without adding a binding.
Replace List(a)=List(Int) with a=Int.
Store a=t and replace a by t in every remaining equation, provided a does not occur in t.
Change List(Int)=a into a=List(Int).
Decomposition
Elimination
Orientation
Deletion
A type-equation solver records a=List(b) and then discovers b=Pair(Char,Bool). What is the fully substituted expression for a? Enter your expression using constructor notation such as List(Pair(Int,Int)).