Unification Algorithm

GATE Relevance: High

Prerequisites

🎯 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

Definition — 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.

Substitution Example

θ = {x/A, y/f(B)}

P(x, g(y))θ = P(A, g(f(B)))

2. Most General Unifier (MGU)

Unifier

A substitution θ is a unifier of expressions E1 and E2 if E1θ = E2θ.

Most General Unifier

θ 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 θ).

Uniqueness of MGU

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)

UNIFY(E1, E2, θ)
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

The 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.

Occur Check Failure

Unify P(x) with P(f(x)):

  • Try {x/f(x)}: but x occurs in f(x) ⇒ Fail
GATE Note

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

Example 1: P(x, f(y)) and P(a, f(b))
  1. Match predicate P: same ✓
  2. Unify arg 1: x with a ⇒ θ = {x/a}
  3. Unify arg 2: f(y) with f(b)
  4.   Match function f: same ✓
  5.   Unify: y with b ⇒ θ = {x/a, y/b}

MGU = {x/a, y/b}

Example 2: Q(x, x) and Q(a, b)
  1. Match Q: same ✓
  2. Unify arg 1: x with a ⇒ θ = {x/a}
  3. Unify arg 2: x with b ⇒ apply θ: a with b ⇒ a ≠ b ⇒ Fail

Not unifiable (x cannot be both a and b).

Example 3: R(x, f(y), g(x)) and R(z, f(h(z)), g(a))
  1. Match R: same ✓
  2. Unify arg 1: x with z ⇒ θ = {x/z}
  3. Unify arg 2: f(y) with f(h(z)) ⇒ unify y with h(z) ⇒ θ = {x/z, y/h(z)}
  4. 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)) ✓

Example 4: Knows(John, x) and Knows(y, Mother(y))
  1. Match Knows: same ✓
  2. Unify arg 1: John with y ⇒ θ = {y/John}
  3. 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)) ✓

Example 5: Occur Check Failure

Unify: Loves(x, f(x)) and Loves(f(y), y)

  1. Match Loves: same ✓
  2. Unify arg 1: x with f(y) ⇒ θ = {x/f(y)}
  3. 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

Time Complexity
  • 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.”