Global variables are associations between names (atoms) and terms.
They differ in various ways from storing information using assert/1
- The value lives on the Prolog (global) stack. This implies that
lookup time is independent from the size of the term. This is
particularly interesting for large data structures such as parsed XML
documents or the CHR global constraint store.
- They support both global assignment using nb_setval/2
and backtrackable assignment using b_setval/2.
- Only one value (which can be an arbitrary complex Prolog term) can
be associated to a variable at a time.
- Their value cannot be shared among threads. Each thread has its own
namespace and values for global variables.
- Currently global variables are scoped globally. We may consider
module scoping in future versions.
implicitly create a variable if the referenced name does not already
refer to a variable.
Global variables may be initialised from directives to make them
available during the program lifetime, but some considerations are
necessary for saved-states and threads. Saved-states to not store global
variables, which implies they have to be declared with initialization/1
to recreate them after loading the saved state. Each thread has its own
set of global variables, starting with an empty set. Using
to define a global variable it will be defined, restored after reloading
a saved state and created in all threads that are created after
the registration. Finally, global variables can be initialised using the
exception hook called
The latter technique is by CHR (see chapter
Associate the term Value with the atom Name or
replaces the currently associated value with Value. If Name
does not refer to an existing global variable a variable with initial
 is created (the empty list). On backtracking the
assignment is reversed.
Get the value associated with the global variable Name and
unify it with Value. Note that this unification may further
instantiate the value of the global variable. If this is undesirable the
normal precautions (double negation or copy_term/2)
must be taken. The
predicate generates errors if Name is not an atom or the
requested variable does not exist.
Associates a copy of Value created with duplicate_term/2
with the atom Name. Note that this can be used to set an
initial value other than
 prior to backtrackable
predicate is a synonym for b_getval/2,
introduced for compatibility and symmetry. As most scenarios will use a
particular global variable either using non-backtracable or
backtrackable assignment, using nb_getval/2
can be used to document that the variable is used non-backtracable.
Associates the term Value with the atom Name
without copying it. This is a fast special-purpose variation of nb_setval/2
intended for expert users only because the semantics on backtracking to
a point before creating the link are poorly defined for compound terms.
The principal term is always left untouched, but backtracking behaviour
on arguments is undone if the original assignment was trailed
and left alone otherwise, which implies that the history that created
the term affects the behaviour on backtracking. Please consider the
T = nice(N),
( N = world,
; nb_getval(myvar, V),
Enumerate all defined variables with their value. The order of
enumeration is undefined.
Delete the named global variable.
Global variables have been introduced by various Prolog
implementations recently. The implementation of them in SWI-Prolog is
based on hProlog by Bart Demoen. In discussion with Bart it was decided
that the semantics if hProlog nb_setval/2,
which is equivalent to nb_linkval/2
is not acceptable for normal Prolog users as the behaviour is influenced
by how built-in predicates constructing terms (read/1,
=../2, etc.) are implemented.
GNU-Prolog provides a rich set of global variables, including arrays.
Arrays can be implemented easily in SWI-Prolog using functor/3
due to the unrestricted arity of compound terms.