Assigns the Arg-th argument of the compound term Term
with the given Value as setarg/3,
but on backtracking the assignment is not reversed. If Value
is not atomic, it is duplicated using duplicate_term/2.
This predicate uses the same technique as
nb_setval/2.
We therefore refer to the description of nb_setval/2
for details on non-backtrackable assignment of terms. This predicate is
compatible with GNU-Prolog setarg(A,T,V,false), removing
the type restriction on Value. See also nb_linkarg/3.
Below is an example for counting the number of solutions of a goal. Note
that this implementation is thread-safe, reentrant and capable of
handling exceptions. Realising these features with a traditional
implementation based on assert/retract or flag/3
is much more complicated.
:- meta_predicate
succeeds_n_times(0, -).
succeeds_n_times(Goal, Times) :-
Counter = counter(0),
( Goal,
arg(1, Counter, N0),
N is N0 + 1,
nb_setarg(1, Counter, N),
fail
; arg(1, Counter, Times)
).