I know that var/1
, nonvar/1
and !/0
are impure primitives, but does their use make every program that uses them impure?
I wrote the following predicate plus/3
that behaves as if it were pure or at least that is what I claim. The predicate is demonstrative, not designed to be efficient.
% nat(X) is true if X is a natural number
nat(0).
nat(X):- nonvar(X), !, X > 0.
nat(X):- nat(X1), X is X1 + 1.
% plus(A, B, C) is true if A,B and C are natural numbers and A+B=C
plus(A, B, C):-
nat(A),
(nonvar(C), C < A, !, false ; true),
plus_(B, A, C).
plus_(A, B, C):-
nat(A),
(nonvar(C), C < A, !, false ; true),
C1 is A + B,
(C = C1 ; nonvar(C), C < C1, !, false).
I have two questions:
- Is the above predicate
plus/3
really pure? - In general, how can you prove that a particular relation has logical purity?
This question follows the discussion on this answer.
Regarding the question "is the above
plus/3
really pure?" I can only say: I have no idea which properties are preserved here, because the code, due to all these extra-logical predicates, is so complicated and hard to follow that it is hard to tell what it is actually doing. And I really must say doing in this case, because this is far from declarative code that is describing something we can comparatively easily talk about. All kinds of properties we expect from declarative code may be destroyed in this case.The best way to generally prove that a relation is pure is to restrict yourself to the pure and monotonic subset of Prolog. It is for this reason that this property is so important, because this subset is closed under composition. Once you leave this subset, it quickly gets really hard to prove any properties whatsoever about your programs, as your example nicely demonstrates.
For example, to prove your
plus/3
monotonic, you need to show, among many other things, that if?- plus(X, _, _), X = T
fails for any termT
, then?- X = T, plus(X, _, _)
does not succeed. Since it is in general not decidable whether a query fails, loops or succeeds and we therefore in general cannot even determine a single side of this implication, let alone both, I can only say: Good luck with that!It has some odd behavior: Sometimes it accepts arithmetic expressions, and sometimes not ; and this although all arguments are evaluated:
I would rather be concerned about the inefficiency of
nat/1
for cases like:So, it looks to me that your definition is pure. However, this very style of programming makes it quite difficult to guarantee this property. A minimal change will have disastrous effects.
The easiest way is by construction. That is, by using only pure monotonic building blocks. I.e., Prolog without any built-ins and using only conjunction and disjunction of regular goals. Any program built this manner will be pure and monotonic, too. Then, execute this program with Prolog flag occurs check set to
true
orerror
.Add to this pure built-ins like
(=)/2
anddif/2
.Add to this built-ins that try to emulate pure relations and that produce instantiation errors when they are unable to do so. Think of
(is)/2
and the arithmetic comparison predicates. Some of these are quite on the borderline likecall/N
which might call some impure predicates.Add
library(clpfd)
with flagclpfd_monotonic
set totrue
.Many constructs are fine and pure for certain uses, but impure for others. Think of if-then-else which is perfect for arithmetic comparison:
but which does not work together with a pure goal like
What remains are impure built-ins that can be used to construct predicates that behave in a pure manner ; but whose definition as such is no longer pure.
As an example, consider truly green cuts. These are extremely rare, and even rarer here on SO. See this and that.
Another common pattern to provide a pure relation are conditionals like:
And to guard against cases that are not cleanly covered, errors can be thrown.