# Prolog Learnings: Unification

The concept of Unification is an underpinning of Prolog. Most languages assign values to variables. For example, the statement int x = 0; causes the value zero to be assigned to x, resulting in x having a value of zero. But a statement like int 0 = x; is a syntax error.

Prolog performs "Unification" which superficially resembles assignment but is more powerful. Prolog attempts to evaluate the clause below to "true" by "Unifying" (make the same) the term on the left of the = with the term on the right, thus assigning 0 to X.

Prolog
1
2
?- X = 0.
X = 0.


Unification allows a variable to be on either side of the =.

Prolog
1
2
3
4
5
6
7
8
?- 0 = X.
X = 0.

?- [1,2,3] = A
A = [1,2,3].

?- [1,2,3] = [1,2,3].
true.


Prolog can also use pattern matching in combination with Unification to derive answers.

Prolog
1
2
3
?- [1,2,Third] = [1,Second,3].
Third = 3,
Second = 2.


If X is already assigned a value then any later attempts to unify to a different value will fail. This behaviour is different to iterative languages where a value assigned to a variable can change.

Prolog
1
2
?- X = 1, X = 0.
false