| ?- me = me. yes | ?- me=you. no | ?- me=X. X = me | ?- f(a,X) = f(Y,b). X = b, Y = a ; no | ?- f(X) = g(X). no | ?- f(X) = f(a,b). no | ?- f(a,g(X)) = f(Y,b). no | ?- f(a,g(X)) = f(Y,g(b)). X = b, Y = a | ?- X=Y. X = _434, Y = _434
The definition treats both terms symmetrically, in contrast to
ordinary pattern matching, in which one term (the pattern) is allowed
to have variables subject to instantiation, but the other (the
subject) is not. For example, in pattern matching, if f(a,
X) is the pattern and f(Y, b) is the subject, we can
instantiate X to b but not
Note how repeated occurrences of the same variable are handled.
For example,
In most applications of unification, variables are renamed, if necessary, so that the two input terms have disjoint sets of variables. This is done for example in unifying a Prolog goal or subgoal term with the head term of a rule in the Prolog database. However, even if this renaming is done initially, it can still happen that recursive subcases of unification encounter pairs of terms that have variables in common. For example, . . .
Prolog's unification algorithm famously (or notoriously) differs from the standard algorithm in one respect: in unifying an uninstantiated variable v with a term t, it omits the check for v occurring in t (for the sake of efficiency). Omission of this check, called the "occurs check," can be considered a bug. (Why? What problems can it cause?)
(1) ancestor(X, Y) :- parent(X, Z), ancestor(Z,Y). (2) ancestor(X, X). (3) parent(amy, bob).Given the goal ancestor(X, bob), Prolog's search strategy is left to right and depth first on the following tree of subgoals. Edges are labeled by the number of the clause used by Prolog for resolution, and instantiations of variables are written in curly brackets.
(1) ancestor(X, Y) :- ancestor(Z,Y), parent(X, Z).
Consider the appnd predicate, which is true if its third argument is a list that is the concatenation of its first two arguments.
appnd([],L,L).
appnd([H | T],L,[H | TL]) :-
appnd(T,L,TL).
Here are some examples of the use of this predicate:
blackbox.cs.rpi.edu% prolog
Aquarius Prolog version 1.0 top-level (SPARC, SunOS)
| ?- consult('appnd.pro').
yes
| ?- appnd([1], [2, 3], [1, 2, 3]).
yes
| ?- appnd([1], [2, 3], [1, 2, 4]).
no
| ?- appnd([1], X, [1, 2, 4]).
X = [2,4] ;
no
Here the semicolon was typed by the user to cause prolog to search
for further solutions. There were no others. But if we use two
variables we can find all possible combinations of lists whose
concatenation is [1, 2, 4], by typing semicolons until all combinations have
been displayed:
| ?- appnd(X, Y, [1, 2, 4]). X = [], Y = [1,2,4] ; X = [1], Y = [2,4] ; X = [1,2], Y = [4] ; X = [1,2,4], Y = [] ; no | ?-
A good exercise is to draw the backtracking tree that Prolog builds in producing these solutions.