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\mathrm{Int} and Bool\mathrm{Bool}, and structured types such as lists, pairs, and functions.

Equations and substitutions

A substitution SS acts consistently on every occurrence of a variable. If S={a↦Int}S=\{a\mapsto\mathrm{Int}\}, then applying SS to Pair(a,a)\mathrm{Pair}(a,a) gives Pair(Int,Int)\mathrm{Pair}(\mathrm{Int},\mathrm{Int}). You cannot replace just one occurrence and leave the other unchanged.


The governing requirement for a set of equations is


S(τi)=S(υi)for every equation τi=υi.S(\tau_i)=S(\upsilon_i)\qquad\text{for every equation }\tau_i=\upsilon_i.


Here, τi\tau_i and υi\upsilon_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\mathrm{List} takes one type argument and Pair\mathrm{Pair} takes two. Function types use the binary constructor written A→BA\to B, with domain AA and result type BB.


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=TT=T

Delete it

It imposes no new restriction.

List(A)=List(B)\mathrm{List}(A)=\mathrm{List}(B)

Replace it with A=BA=B

Matching constructors require matching arguments.

A→B=C→DA\to B=C\to D

Replace it with A=CA=C and B=DB=D

Domains and results match separately.

a=Ta=T, with aa absent from TT

Bind a↦Ta\mapsto T

A variable can stand for that finite expression.

Int=Bool\mathrm{Int}=\mathrm{Bool}

Report failure

Distinct rigid base types cannot become identical.

a=List(a)a=\mathrm{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=aa=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↦ba\mapsto b and later b↦Charb\mapsto\mathrm{Char}, the completed substitution sends both variables to Char\mathrm{Char}.


Four connected boxes show the repeated stages of solving a set of type equations.



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=ba=b, the substitution {a↦b}\{a\mapsto b\} is most general. Choosing {a↦Int,b↦Int}\{a\mapsto\mathrm{Int},b\mapsto\mathrm{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∘SR\circ S means apply SS first and then RR:


(R∘S)(T)=R(S(T)).(R\circ S)(T)=R(S(T)).



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,za,b,c,d,e,z represent unknown types. The constructors Pair\mathrm{Pair}, List\mathrm{List}, and the function arrow are rigid, and all type expressions are finite.


Pair(a,List(b))=Pair(Int,c),c=List(d),b→e=Bool→z.\begin{aligned} \mathrm{Pair}(a,\mathrm{List}(b))&=\mathrm{Pair}(\mathrm{Int},c),\\ c&=\mathrm{List}(d),\\ b\to e&=\mathrm{Bool}\to z. \end{aligned}


a) Decompose the first equation and record its variable bindings.


Matching pair constructors require equality of their corresponding components:


a=Int,List(b)=c.a=\mathrm{Int},\qquad \mathrm{List}(b)=c.


The first component fixes aa to Int\mathrm{Int}. Reverse the second component equation to put its variable on the left; the replacement for cc is List(b)\mathrm{List}(b). Neither binding contains its own variable on the right, so both pass the occurs check.


The bindings from the first equation are


a↦Int,c↦List(b).\boxed{a\mapsto\mathrm{Int},\qquad c\mapsto\mathrm{List}(b).}


b) Process the remaining equations and write the completed substitution, leaving any unconstrained variable free.


Apply the recorded replacement for cc to the second original equation:


List(b)=List(d).\mathrm{List}(b)=\mathrm{List}(d).


Matching list constructors reduce this to ________. Choose the binding b↦db\mapsto d. Propagate it into the stored replacement for cc, giving c↦List(d)c\mapsto\mathrm{List}(d), and into the function equation:


d→e=Bool→z.d\to e=\mathrm{Bool}\to z.


Matching domains set dd to ________, while matching results set ee to ________. Apply these bindings to the earlier replacements. In particular, bb also becomes Bool\mathrm{Bool}, and the list element type in the replacement for cc becomes Bool\mathrm{Bool}.


The completed substitution is


S={a↦Int, b↦Bool, c↦List(Bool), d↦Bool, e↦z}.\boxed{S=\{a\mapsto\mathrm{Int},\ b\mapsto\mathrm{Bool},\ c\mapsto\mathrm{List}(\mathrm{Bool}),\ d\mapsto\mathrm{Bool},\ e\mapsto z\}.}


The variable zz remains unchanged. No equation requires a concrete choice for zz.


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


Pair(Int,List(Bool)).\mathrm{Pair}(\mathrm{Int},\mathrm{List}(\mathrm{Bool})).


For the second equation, the substituted left side is List(Bool)\mathrm{List}(\mathrm{Bool}). Its right side is also List(Bool)\mathrm{List}(\mathrm{Bool}) because dd becomes Bool\mathrm{Bool}.


For the third equation, the substituted left side is Bool→z\mathrm{Bool}\to z, and the substituted right side is the same expression. Every original equation is therefore satisfied.


The equations force aa, bb, cc, and dd to the types shown and require ee and zz to share a type. Any further choice for that shared type can be made by replacing zz afterward. Thus, SS is a most general unifier: it satisfies every constraint while retaining all remaining freedom.

A compiler component must solve the following equation between finite types, where aa and bb are variables and constructors are rigid:


Pair(a,Char)→b=Pair(Bool,Char)→List(Int).\mathrm{Pair}(a,\mathrm{Char})\to b = \mathrm{Pair}(\mathrm{Bool},\mathrm{Char})\to\mathrm{List}(\mathrm{Int}).


Which pair of equations remains after decomposing matching constructors and deleting the identical-type equation?

Choose an answer

Create a free account to get started

Unlock full course notes by signing up

  • Video lessons taught by top educators
  • Practice questions with guided solutions
  • Written review notes for reference
  • Expert answers anytime from course tutors

A compiler solves equations between finite types using type variables, the base type Int\mathrm{Int}, and the constructor List\mathrm{List}. Match each unification rule to its action. Stored bindings remain part of the solution even after they leave the pending equations. Here, aa denotes a type variable and tt denotes a type expression.

Match each term
A.

Remove t=tt=t from the pending equations without adding a binding.

B.

Replace List(a)=List(Int)\mathrm{List}(a)=\mathrm{List}(\mathrm{Int}) with a=Inta=\mathrm{Int}.

C.

Store a=ta=t and replace aa by tt in every remaining equation, provided aa does not occur in tt.

D.

Change List(Int)=a\mathrm{List}(\mathrm{Int})=a into a=List(Int)a=\mathrm{List}(\mathrm{Int}).

Decomposition

Elimination

Orientation

Deletion

Create a free account to get started

Unlock full course notes by signing up

  • Video lessons taught by top educators
  • Practice questions with guided solutions
  • Written review notes for reference
  • Expert answers anytime from course tutors

A type-equation solver records a=List(b)a=\mathrm{List}(b) and then discovers b=Pair(Char,Bool)b=\mathrm{Pair}(\mathrm{Char},\mathrm{Bool}). What is the fully substituted expression for aa? Enter your expression using constructor notation such as List(Pair(Int,Int)).

Create a free account to get started

Unlock full course notes by signing up

  • Video lessons taught by top educators
  • Practice questions with guided solutions
  • Written review notes for reference
  • Expert answers anytime from course tutors

Extra Practice