domain_error(not_less_then_zero, Arg)
if Arg
< 0.
?- foo(hello, X) =.. List. List = [foo, hello, X] ?- Term =.. [baz, foo(1)] Term = baz(foo(1)) |
$VAR(N)
,
where N is the number of the variable. Counting starts at
Start. End is unified with the number that should
be given to the next variable. Example:
?- numbervars(foo(A, B, A), 0, End). A = '$VAR'(0) B = '$VAR'(1) End = 2 |
See also the numbervars
option to write_term/3
and numbervars/4.
$VAR
.
skip
, which causes numbervars/3
to ignore the attributed variable, bind
which causes it to
thread it as a normal variable and assign the next $VAR
(N)
term to it or (default)
error
which raises the a type_error
exception. (41)
?- term_variables(a(X, b(Y, X), Z), L). L = [G367, G366, G371] X = G367 Y = G366 Z = G371 |
Prolog is not capable to modify instantiated parts of a term. Lacking that capability makes that language much safer, but unfortunately there are problems that suffer severely in terms of time and/or memory usage. Always try hard to avoid the use of these primitives, but they can be a good alternative to using dynamic predicates. See also section 6.3, discussing the use of global variables.
This predicate may be used for destructive assignment to terms, using them as and extra-logical storage bin. Always try hard to avoid the use of setarg/3 as it is not supported by many Prolog systems and one has to be very careful about unexpected copying as well as unexpected not copying of terms.
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 as discusses with flag/3
is much more complicated.
:- module_transparent succeeds_n_times/2. 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) ). |