How can I simulate a soft cut I *-> T; E in ISO Prolog? I has side effects, so I can not call it multiple times.
Except for the last requirement, I think the following definition works:
if_(I, T, E) :-
not(not(I)) ->
call((I, T));
call((not(I), E)).
(I'm actually using XSB prolog; a solution for XSB would be useful for me too.)
Yes, we can implement this in ISO Prolog and even in XSB, but not very efficiently. To make this efficient, you would need some "selective cut". Further, XSB does not implement ISO conforming integers so the overflow must be handled separately.
The major source of inefficiency is that for a determinate condition
If_0
, there is still a choice point left. It isthinkablenext to unthinkable that an implementation could conclude thatretract(no_if_answer(Id))
will always fail, onceretractall(no_if_answer(Id))
has been executed, but I doubt that implementers will invest in such optimizations. EDIT: The reason why this seems highly improbable is that an implementation would have to guarantee that the numbers asserted always go up.Note that soft cut produces incompleteness in the same way the cut does. Consider:
This clearly misses an answer! To see why, take
X = b
:Conjunction should be commutative (modulo non-termination, errors, side-effects).
If you are interested in declaratively sound conditionals that are also very efficient and often faster than their impure counterparts, consider
if_/3
. Seelibrary(reif)
for SICStus which gives all correct answers: