Thank you Peter.
But I would expect the program to continue as if dif(X,Y)
had failed, and thus call the “else” goal.
Another example:
docut(X,Y) :- dif(X,Y),!,postdif(X,Y).
docut(X,Y) :- writeln(alternative),postdif(X,Y).
do(X,Y) :- dif(X,Y),postdif(X,Y).
do(X,Y) :- writeln(alternative),postdif(X,Y).
postdif(X,Y) :-
writeln(X), X = 1, writeln(Y), Y = 1,
writeln('end of clause').
After dif/2
“late failure” Prolog rolls back to where?
For do/2
it seems to behave as if dif(X,Y)
had failed, and takes the alternative clause.
?- do(X,Y).
_13340
_13372
alternative
_12986
_12988
end of clause
X = Y, Y = 1.
For do_cut/2
it seems to behave as if dif(X,Y)
had failed, but the cut traversed. The alternative clause is not taken:
?- docut(X,Y).
_12628
_12660
false.
This seems strange. Why has the cut been traversed already?
I will definitely check library(reif).