9.7 Global References
Terms stored in non-logical variables and arrays are copies of the
and the terms obtained by getval/2 are thus not identical
to the original terms, in particular their variables are different.
Sometimes it is necessary to be able
to access the original term with its variables, i.e. to have
global variables in the meaning of conventional programming
A typical example is global state that a set of predicates wants to
share without having to pass an argument pair through all the
ECLiPSe offers the possibility to store references to general terms
and to access them even inside predicates that have no common variables
with the predicate that has stored them.
They are stored in so-called references.
:- local reference(p).
creates a named reference p (with an initial value of 0)
which can be used to store references to terms.
This reference is accessed and modified in the same way as non-logical variables,
but the following points are different for references:
:- local reference(p, 0).
The use of references should be considered carefully.
Their overuse can lead to programs which are
difficult to understand and difficult to optimize.
Typical applications use at most a single reference per module,
for example to hold state that would otherwise have to be passed
via additional arguments through many predicate invocations.
the accessed term is identical to the stored term (with its current
[eclipse 1]: local reference(a), variable(b).
[eclipse 2]: Term = p(X), setval(a, Term), getval(a, Y), Y == Term.
X = X
Y = p(X)
Term = p(X)
[eclipse 3]: Term = p(X), setval(b, Term), getval(b, Y), Y == Term.
no (more) solution.
- the modifications are backtrackable, when the execution fails
over the setval/2 call, the previous value of the global reference is restored
[eclipse 4]: setval(a, 1), (setval(a, 2), getval(a, X); getval(a, Y)).
X = 2
Y = Y More? (;)
X = X
Y = 1
- there are no arrays of references, but the same effect can be
achieved by storing a structure in a reference and using the structure's
arguments. The arguments can then be accessed and modified using