Comparison and unification of arbitrary terms. Terms are ordered in the so-called “standard order” . This order is defined as follows:
-inf. If the comparison
is equal, the float is considered the smaller value. If the Prolog flag
iso is defined, all
floating point numbers precede all rationals.
Although variables are ordered, there are some unexpected properties
one should keep in mind when relying on variable ordering. This applies
to the predicates below as to predicate such as sort/2
as well as libraries that reply on ordering such as library library(assoc)
and library
library(ordsets). Obviously, an established relation A B
no longer holds if A is unified with e.g., a number. Also
unifying A with B invalidates the relation because
they become equivalent (==/2) after unification. The predicate
partial_compare/3
can be used to detect that a comparison depends on a variable and is
therefore not stable.
@<
As stated above, variables are sorted by address, which implies that they are sorted by ‘age’, where ‘older’ variables are ordered before ‘newer’ variables. If two variables are unified their ‘shared’ age is the age of oldest variable. This implies we can examine a list of sorted variables with ‘newer’ (fresh) variables without invalidating the order. Attaching an attribute, see section 8.1, turns an ‘old’ variable into a ‘new’ one as illustrated below. Note that the first always succeeds as the first argument of a term is always the oldest. This only applies for the first attribute, i.e., further manipulation of the attribute list does not change the ‘age’.
?- T = f(A,B), A @< B. T = f(A, B). ?- T = f(A,B), put_attr(A, name, value), A @< B. false.
The above implies you can use e.g., an assoc (from library
library(assoc), implemented as an AVL tree) to maintain
information about a set of variables. You must be careful about what you
do with the attributes though. In many cases it is more robust to use
attributes to register information about variables.
Note that the standard order is not well defined on rational trees, also known as cyclic terms. This issue was identified by Mats Carlsson, quoted below. See also issue#1162 on GitHub.
Consider the terms A and B defined by the equations[1] A = s(B,0). [2] B = s(A,1).
- Clearly, A and B are not identical, so either
A @< BorA @> Bmust hold.
- Assume
A @< B. But then,s(A,1) @> s(B,0)i.e.,B @< A. Contradiction.
- Assume
A @> B. But then,s(A,1) @< s(B,0)i.e.,B @< A. Contradiction.
By default, comparing such terms returns an arbitrary order, which
breaks predicates that rely on a consistent order such as sort/2,
library library(assoc) and library library(ordsets).
Setting the Prolog flag incomparable
to error raises an exception instead. The predicate partial_compare/3
reports such pairs as
incomparable(Sub1, Sub2).
\+Term1 == Term2.<, >
or =, with the obvious meaning.undecided(Sub1, Sub2), where Sub1
and
Sub2 are the first pair of subterms of Term1 and Term2
that could not be compared. Because the standard order compares terms
lexicographically, this pair decides the entire comparison: after any
further instantiation, compare(Order, Term1, Term2) gives
the same result as compare(Order, Sub1, Sub2), unless Sub1
and
Sub2 become equal (==/2), in which case the comparison must
continue with the remaining subterms.
If the two terms are cyclic and have no order (see
section 4.6.1), Order
is unified with
incomparable(Sub1, Sub2), where Sub1 and Sub2
are a pair of subterms at which the lexicographic comparison runs into a
cycle. Unlike undecided(Sub1, Sub2), further instantiation
cannot make the terms comparable.
?- partial_compare(Order, f(a,1), f(a,2)). Order = (<). ?- partial_compare(Order, f(a,X), f(a,b)). Order = undecided(X, b). ?- partial_compare(Order, f(X,1), f(X,2)). Order = (<). ?- partial_compare(Order, f(_,1), g(_,2)). Order = (<). ?- A = s(B,0), B = s(A,1), partial_compare(Order, A, B). A = s(s(A, 1), 0), B = s(A, 1), Order = incomparable(s(s(A, 1), 0), s(A, 1)).
The last two queries illustrate that a variable only makes the comparison undecided if it is actually reached: the first of these compares the same variable to itself and continues with the second argument, while the second is decided by the functor name before the arguments are examined at all.