Standard term order (ISO/IEC 13211-1 7.2 Term order) is defined over all terms — including variables. While there are good uses for this — think of the implementation of setof/3
, this makes many otherwise clean and logical uses of the built-ins in 8.4 Term comparison a declarative nightmare with imps (short form for imperative constructs) all around. 8.4 Term comparison features:
8.4 Term comparison
8.4.1 (@=<)/2, (==)/2, (\==)/2, (@<)/2, (@>)/2, (@>=)/2.
8.4.2 compare/3.
8.4.3 sort/2.
8.4.4 keysort/2.
To give an example, consider:
?- X @< a.
true.
This succeeds, because
7.2 Term order
An ordering term_precedes (3.181) defines whether or
not a termX
term-precedes a termY
.If
X
andY
are identical terms thenX
term_precedesY
andY
term_precedesX
are both false.If
X
andY
have different types:X
term_precedesY
iff the
type ofX
precedes the type ofY
in the following order:
variable
precedesfloating point
precedesinteger
precedesatom
precedescompound
.NOTE — Built-in predicates which test the ordering of terms
are defined in 8.4.
...
And thus all variables are smaller than a
. But once X
is instantiated:
?- X @< a, X = a.
X = a.
the result becomes invalid.
So that is the problem. To overcome this, one might either use constraints, or stick to core behavior only and therefore produce an instantiation_error
.
7.12.2 Error classification
Errors are classified according to the form of
Error_term
:a) There shall be an Instantiation Error when an
argument or one of its components is a variable, and an
instantiated argument or component is required. It has
the forminstantiation_error
.
In this manner we know for sure that a result is well defined as long as no instantiation error occurs.
For (\==)/2
, there is already either dif/2
which uses constraints or iso_dif/2
which produces a clean instantiation error.
iso_dif(X, Y) :-
X \== Y,
( X \= Y -> true
; throw(error(instantiation_error,iso_dif/2))
).
So what my question is about: How to define (and name) the corresponding safe term comparison predicates in ISO Prolog? Ideally, without any explicit term traversal. Maybe to clarify: Above iso_dif/2
does not use any explicit term traversal. Both (\==)/2
and (\=)/2
traverse the term internally, but the overheads for this are extremely low compared to explicit traversal with (=..)/2
or functor/3, arg/3
.
In this answer we present the predicate
safe_term_less_than/2
, a monotonic analogue to the iso-prolog built-in predicate(@<)/2
(§8.4.1, "term less than"). Its main properties are:Based on prolog-coroutining facilities, in particular
when/2
.The comparison may progress gradually:
The current frontline of the comparison is represented as an explicit (LIFO) stack.
The following code has been developed and tested on sicstus-prolog version 4.3.2:
Above definition of
safe_term_less_than/2
is based on the following auxiliary predicates:Sample queries:
In comparison to previous answers, we observe:
?- safe_term_less_than(X+2, Y+1), X = Y.
fails—just like it should!Here is a sketch of what I believe might be a working approach. Consider the goal
lt(X, Y)
andterm_variables(X, XVars), term_variables(Y, YVars)
.The purpose of the definition is to determine whether or not a further instantiation might change the term order (7.2). So we might want to find out the responsible variables directly. Since
term_variables/2
traverses a term in the very same way that is of relevance to term order, the following holds:If there is an instantiation that changes the term order, then the variables that have to be instantiated to witness that change are in the list prefixes
XCs
,YCs
ofXVars
andYVars
respectively, and eitherXCs
,YCs
,XVars
, andYVars
are identical, orXCs
andYCs
are identical up to the last element, orXCs
andYCs
are identical up to the end where one list has a further element, and the other list is identical to its corresponding variable listXVars
orYVars
.As an interesting special case, if the first elements in
XVars
andYVars
differ, then those are the only variables to be tested for relevance. So this includes the case where there is no common variable, but it is even more general than that.