The behaviour of "=" has two facets, instantiation and
matching, and we have seen both.
We have seen that instantiation is the way in which variables
acquire a value. We have also seen that once a Prolog variable has
been instantiated, it cannot be reinstantiated. This is an important
point, because Prolog (and logic programming languages in general)
differ from procedural languages at this point.
We have seen that matching compares two items. Matching
either succeeds or fails.
Prolog combines both the facets in one built-in predicate, which
has the name "unify". Prolog (and logic programming languages in
general) use the unification algorithm to implement matching
and instantiation. Briefly put, as it is an algorithm it is a predetermined
method that will always give us predictable results. We can unify
the following:
atom = atom.
atom = Variable.
Variable = atom.
Variable1 = Variable2.
Self-test 5 further develops your understanding of unification.
Unify requires a term on either side of it. If both terms
are atoms, it is simply a matter of comparing the two atoms to see
if they are identical.
What if one or both of the terms are variables? We have seen several
cases, eg:
Unifying a variable with an atom:
| ?- Lorry = scammell, write(Lorry).
scammell
Lorry = scammell ?
So "=" has the effect of giving a variable a value. This
process is known as instantiation. An uninstantiated variable
(eg "Lorry") is instantiated when it is unified with
an atom.
If both terms are instantiated variables, then the values of the
variables are matched just as if they were atoms, hence the following:
| ?- Car1 = nova, Car2 = nova, Car1 = Car2.
Car1 = nova,
Car2 = nova ?
| ?- Car1 = nova, Car2 = fiesta, Car1 = Car2.
no
|