Consider the following code:
a(X) :- b(X),!,c(X),fail.
a(X) :- d(X).
b(1).
b(4).
c(1).
c(3).
d(4).
The query a(X).
produces
1 ?- a(X).
false.
2 ?-
but with this code
a(X) :- b(X),!,c(X).
a(X) :- d(X).
b(1).
b(4).
c(1).
c(3).
d(4).
The query a(X).
results in :
1 ?- a(X).
X = 1.
So my question is, why does the fail/1
produces false? it is supposed to force backtracking, right ? then b(1)
and c(1).
would be checked, I think, so why does it fail?
it fails because fail
must fail.
The cut removes alternatives, then forbids values that otherwise would be 'returned' by means of X
binding. Try
a(X) :- b(X),c(X),fail.
...
you'll get
?- a(X).
X = 4.