| ?- assert(p(1)), assert(p(2)), assert(p(3)).
yes
| ?- p(N), write(N), nl, retract(p(2)),
retract(p(3)), fail.
1
2
3
no
| ?- p(N), write(N), fail.
1
no
| ?-
At the first call to p/1, the procedure has three clauses.
These remain visible throughout execution of the call to p/1.
Thus, when backtracking is forced by fail/0, N is bound
to 2 and written. The retraction is again attempted, causing
backtracking into p/1. N is bound to 3 and written out.
The call to retract/1 fails. There are no more clauses in
p/1, so the query finally fails. A subsequent call to
p/1, made after the retractions, sees only one clause.