Unification in logic is the procedure that decides whether two symbolic expressions can be made syntactically identical by replacing their variables with terms, and if so, computes the most general substitution that does it. In Symbolic AI & reasoning, unification is the matching engine behind resolution theorem proving, logic programming in Prolog, type inference and many rule-based systems.
Unlike simple pattern matching, unification is two-sided: variables may appear in both expressions, and each binding must stay consistent across the whole problem. The result, called the most general unifier (MGU), commits to nothing more than necessary, which keeps later reasoning steps as flexible as possible.
How the unification algorithm works
The unification algorithm takes a set of equations between terms and rewrites it until it either reaches a solved form or detects a contradiction. A term is a variable, a constant or a function symbol applied to argument terms, such as f(X, g(a)). The classic formulation by John Alan Robinson in 1965 made unification the central inference step of first-order resolution.
The modern presentation, due to Martelli and Montanari, applies a small set of transformation rules to the equation set. Each rule either simplifies an equation, eliminates a variable or reports failure. When no rule applies, the remaining equations of the form X = t define the MGU.
| Rule | Situation | Action |
|---|---|---|
| Delete | t = t (identical terms) | Remove the equation |
| Decompose | f(s1..sn) = f(t1..tn) | Replace with s1 = t1 … sn = tn |
| Conflict | f(…) = g(…) with f different from g | Fail: symbols clash |
| Swap | t = X where t is not a variable | Rewrite as X = t |
| Eliminate | X = t, X not in t | Substitute t for X everywhere else |
| Occurs check | X = t, X occurs inside t | Fail: no finite solution |
For example, unifying p(X, f(Y)) with p(a, f(b)) decomposes into X = a and Y = b, giving the MGU {X/a, Y/b}. Unifying p(X, X) with p(a, b) fails, because X cannot be bound to two different constants.
Most general unifiers and the occurs check
A substitution σ is a unifier of two terms when applying σ makes them equal, and it is most general when every other unifier can be obtained by further instantiating σ. For first-order syntactic unification, an MGU is unique up to variable renaming, which is why resolution provers can rely on a single answer instead of exploring alternatives.
The occurs check prevents a variable from being bound to a term that contains it, as in X = f(X). Without it, the algorithm would create an infinite, cyclic term and could derive unsound conclusions. Most Prolog implementations omit the occurs check by default for speed, and ISO Prolog offers unify_with_occurs_check/2 when soundness matters.
Complexity depends on representation. The naive algorithm can take exponential time and space when terms are copied, while the algorithms of Paterson and Wegman and of Martelli and Montanari, using shared term graphs, run in linear or near-linear time.
| Algorithm | Representation | Worst-case cost | Typical use |
|---|---|---|---|
| Robinson (1965) | Strings or trees | Exponential | Teaching, early provers |
| Martelli and Montanari | Equation sets with sharing | Near-linear | General theorem provers |
| Paterson and Wegman | Directed acyclic graphs | Linear | Theoretical baseline |
| Union-find based | Shared graphs with union-find | Almost linear | Type checkers, Prolog engines |
Unification compared with related operations
Unification is often confused with pattern matching, equality testing and subsumption, yet each answers a different question. Matching is one-way: variables appear only in the pattern, so f(X, b) matches f(a, b) but the data side is never instantiated. Forward chaining systems and the Rete algorithm mostly rely on matching against ground facts.
| Operation | Variables allowed in | Question answered | Typical setting |
|---|---|---|---|
| Syntactic unification | Both terms | Can both become identical? | Resolution, Prolog, backward chaining |
| Pattern matching | Pattern only | Is the data an instance of the pattern? | Production rules, Rete |
| Subsumption | General clause | Is one clause more general? | Redundancy elimination in provers |
| Anti-unification | Result only | What is the most specific generalization? | Inductive logic programming |
| E-unification | Both, modulo a theory | Equal under axioms such as commutativity? | Equational reasoning, SMT |
Anti-unification, introduced by Plotkin and Reynolds, is the dual problem: it computes the least general generalization of two terms and underpins learning in inductive logic programming. Higher-order unification, needed for lambda terms in proof assistants, is undecidable in general, which is why systems restrict it to decidable fragments such as higher-order patterns.
Where unification is used in practice
Unification is the workhorse of logic-based systems. In Prolog and other implementations of logic programming, every call to a predicate unifies the goal with clause heads, so unification performance directly determines query throughput. In resolution-based automated theorem proving, the MGU of two complementary literals produces each resolvent.
- Backward chaining: goals unify with rule conclusions to generate subgoals, as in Horn clause reasoning.
- Type inference: the Hindley-Milner system in ML and Haskell unifies type expressions to infer principal types.
- Natural language grammars: feature structure unification combines grammatical constraints in unification-based grammars.
- Term rewriting and SMT: critical pair computation and quantifier instantiation rely on unification or matching.
Choosing a variant follows from the underlying theory and performance needs, as the following decision table summarizes.
| Situation | Recommended choice | Reason |
|---|---|---|
| Fast Prolog execution on trusted code | Unification without occurs check | Avoids repeated term traversal |
| Sound theorem proving | Full unification with occurs check | Prevents cyclic, unsound bindings |
| Commutative or associative operators | E-unification for that theory | Captures equalities the syntax hides |
| Proof assistants with lambda terms | Higher-order pattern unification | Decidable fragment with unique MGUs |
Limitations and common mistakes
The most frequent practical error is trusting unification without the occurs check in a setting that needs soundness. A Prolog program can then succeed on a query that has no logical solution, a defect that is hard to trace. Another mistake is forgetting to rename clause variables apart before unification, which creates accidental bindings between unrelated clauses.
Theory-aware unification also carries costs. AC-unification, for associative and commutative operators, can produce many incomparable unifiers instead of one MGU, and some equational theories make unification undecidable. Designers therefore keep equational reasoning in specialized solvers and use plain syntactic unification wherever possible.
Put simply
Unification is a way of filling in blanks so that two statements say exactly the same thing. Imagine two forms. One says “parent of X is Bob”. The other says “parent of Alice is Y”. Unification fills both blanks at once: X becomes Alice and Y becomes Bob.
Sometimes the blanks cannot be filled. If one form demands that the same blank holds both Alice and Carol, the attempt fails. The key idea is consistency: every blank gets one value, and that value must fit everywhere. Logic software repeats this step millions of times to answer questions.
In practice
A team building a rule-based eligibility checker for an insurance workflow writes its rules in Prolog. One rule states that a claim is covered when the policy holder has an active policy whose category matches the claim category. When an analyst asks whether claim 4417 is covered, the engine unifies the query with the rule head, binding the claim identifier, and then unifies each subgoal with stored facts about policies and categories.
During testing, a developer notices that a recursive rule about linked accounts sometimes succeeds when it should fail. The cause is a binding of the form Account = link(Account), which standard Prolog unification accepts because the occurs check is off. After switching the critical predicate to `unify_with_occurs_check/2`, the false positives disappear, while the remaining 12,000 daily queries keep the speed of ordinary unification. The outcome is a checker that is both fast and logically sound.