b_assertz(Term) :- assertz(Term, Ref), undo(erase(Ref)).
b_assertz(Term) :- assertz(Term, Ref), ( true ; erase(Ref), fail ).
The undo/1 based solution avoids leaving a choice point open and, more importantly, keeps undoing the assert also if the choice point from the second alternative is pruned.
Currently the following remarks apply
- Goal is copied when it is registered.
- “First opportunity'' means after backtracking or at the first
call port reached.
- Multiple undo goals may be scheduled that are executed as a batch.
If multiple goals raise an exception, the most urgent is preserved after
all goals have been executed.
- It is not allowed for Goal to call undo/1.
An attempt to do so results in a
- Note that an exception that is caught higher in the call stack backtracks and therefore ensures Goal is called.