Unification Algorithm
Finds a substitution that makes two logical expressions identical.
The idea
Unification answers "can these two patterns be made the same, and how?". For example Knows(John, x) and Knows(y, Mary) unify with {y/John, x/Mary}. It is the matching step inside first-order resolution and Prolog. A variable may not be bound to a term containing itself (the occurs check).
Watch it run
This is a real computation, not a drawing. Pause, step, change the settings, or try new random data.
Unification finds values for the variables (capital letters) that make two terms identical. Lower-case names are constants.
Try New random data to cycle through four examples, including one that fails.
Step by step
- 1Compare the two expressions piece by piece.
- 2If both pieces are the same, continue.
- 3If one is a variable, bind it to the other (after the occurs check) and apply the binding to the rest.
- 4If constants or function names differ, fail.
- 5Return the combined substitution.
Where it is used
Prolog, type inference in languages like Haskell and Rust, theorem provers.
Watch out for
Skipping the occurs check can create infinite terms.