Unification Algorithm
Prerequisites
- FOL Syntax — terms, predicates, functions
- FOL Inference Rules — UI, EI, substitution
🎯 Learning Objectives
- Define substitution and Most General Unifier (MGU)
- Execute the unification algorithm step by step
- Understand and apply the occur check
- Solve 4+ unification problems (GATE-style)
1. Substitution
A substitution θ is a finite set of mappings from variables to terms: θ = {x1/t1, x2/t2, …, xn/tn}.
Applying θ to expression E (written Eθ) simultaneously replaces each xi by ti.
θ = {x/A, y/f(B)}
P(x, g(y))θ = P(A, g(f(B)))
2. Most General Unifier (MGU)
A substitution θ is a unifier of expressions E1 and E2 if E1θ = E2θ.
θ is a Most General Unifier (MGU) if for every other unifier σ there exists a substitution λ such that σ = θλ (i.e., θ is more general — any other unifier can be obtained by further specializing θ).
If an MGU exists, it is unique up to variable renaming. The unification algorithm finds the MGU if it exists, or reports failure if the expressions cannot be unified.
3. Unification Algorithm (Pseudocode)
function UNIFY(x, y, θ):
if θ = failure then return failure
if x = y then return θ
if x is a variable then return UNIFY-VAR(x, y, θ)
if y is a variable then return UNIFY-VAR(y, x, θ)
if x is compound and y is compound then
if OP(x) ≠ OP(y) or |ARGS(x)| ≠ |ARGS(y)| then
return failure
return UNIFY(ARGS(x), ARGS(y), θ)
else return failure
function UNIFY-VAR(var, val, θ):
if {var/t} ∈ θ for some t then
return UNIFY(t, val, θ)
if {val/t} ∈ θ for some t then
return UNIFY(var, t, θ)
if OCCUR-CHECK(var, val) then
return failure
return θ ∪ {var/val}
4. Occur Check
Before adding {x/t} to θ, check whether x occurs in t. If it does, unification fails.
Why? If x occurs in t, the substitution would create an infinite term. For example, unifying x with f(x) would require x = f(f(f(…))) — an infinite structure.
Unify P(x) with P(f(x)):
- Try {x/f(x)}: but x occurs in f(x) ⇒ Fail
Some Prolog implementations omit the occur check for efficiency. But in GATE exams, always apply the occur check unless stated otherwise.
5. Step-by-Step Unification Examples
- Match predicate P: same ✓
- Unify arg 1: x with a ⇒ θ = {x/a}
- Unify arg 2: f(y) with f(b)
- Match function f: same ✓
- Unify: y with b ⇒ θ = {x/a, y/b}
MGU = {x/a, y/b}
- Match Q: same ✓
- Unify arg 1: x with a ⇒ θ = {x/a}
- Unify arg 2: x with b ⇒ apply θ: a with b ⇒ a ≠ b ⇒ Fail
Not unifiable (x cannot be both a and b).
- Match R: same ✓
- Unify arg 1: x with z ⇒ θ = {x/z}
- Unify arg 2: f(y) with f(h(z)) ⇒ unify y with h(z) ⇒ θ = {x/z, y/h(z)}
- Unify arg 3: g(x) with g(a) ⇒ apply θ: g(z) with g(a) ⇒ unify z with a ⇒ θ = {x/a, y/h(a), z/a}
MGU = {x/a, y/h(a), z/a}
Verification: R(a, f(h(a)), g(a)) = R(a, f(h(a)), g(a)) ✓
- Match Knows: same ✓
- Unify arg 1: John with y ⇒ θ = {y/John}
- Unify arg 2: x with Mother(y) ⇒ apply θ: x with Mother(John) ⇒ θ = {y/John, x/Mother(John)}
MGU = {y/John, x/Mother(John)}
Result: Knows(John, Mother(John)) ✓
Unify: Loves(x, f(x)) and Loves(f(y), y)
- Match Loves: same ✓
- Unify arg 1: x with f(y) ⇒ θ = {x/f(y)}
- Unify arg 2: f(x) with y ⇒ apply θ: f(f(y)) with y ⇒ occur check: y in f(f(y))? Yes ⇒ Fail
Not unifiable (would require y = f(f(y)) = f(f(f(f(…))))).
6. Complexity Analysis
- Without occur check: O(n) where n is the size of the expressions (using union-find).
- With occur check: O(n²) in the worst case (naive implementation). With Paterson-Wegman algorithm: O(n).
- Space: O(n) for storing the substitution.
Without occur check, pathological cases can produce exponentially large unified terms (e.g., the term size doubles at each step), but the representation stays linear using DAGs.
Summary
- Unification finds the MGU: the most general substitution making two expressions identical.
- The algorithm works recursively, decomposing compound expressions.
- The occur check prevents infinite terms and is essential for soundness.
- Unification is the core mechanism for resolution in FOL and Prolog execution.
- GATE frequently asks to find the MGU of two given expressions.
References
- Russell, S. & Norvig, P. AIMA, 4th Ed. — Section 9.2
- Robinson, J.A. (1965). “A Machine-Oriented Logic Based on the Resolution Principle.”