4.6.1 Standard Order of Terms
All Application Manual Name SummaryHelp

  • Documentation
    • Reference manual
      • Built-in Predicates
        • Comparison and Unification of Terms
          • Standard Order of Terms
            • ==/2
            • \==/2
            • @</2
            • @=</2
            • @>/2
            • @>=/2
            • compare/3
            • partial_compare/3
    • Packages
Availability:built-in
partial_compare(?Order, @Term1, @Term2)
As compare/3, but only decides the Order if this decision is stable, i.e., if it cannot be invalidated by binding a variable. This predicate is the building block for comparisons that must be sound on non-ground terms. If the order depends on a variable, Order is unified with 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.