next up previous index
Next: Input and Output Up: Arrays and Global Variables Previous: Non-logical Arrays   Index


Global References

Terms stored in non-logical variables and arrays are copies of the setval/2 arguments, and the terms obtained by getval/2 are thus not identical to the original terms, in particular their variables are different. Sometimes it is more convenient or even necessary to be able to access the original term with its variables, i.e. to have global variables in the meaning of conventional programming languages. A typical example is the use of graphical interface: if we want to modify the value of a Prolog variable through a graphical user interface, this mechanism has to be used because the user interface has no means to access Prolog terms directly. Another use is global state that a set of predicates wants to share without having to pass an argument pair through all the predicate invocations.

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. For example,

:- local reference(p).
creates a named reference p which can be used to store references to terms. This reference is accessed and modified in the same way as non-logical variables, with setval/2 and getval/2, but the following points are different for references: .

There is only a limited number of references available and their use should be considered very carefully. Their misuse can lead to very bad programs which are difficult to understand and difficult to optimize.


next up previous index
Next: Input and Output Up: Arrays and Global Variables Previous: Non-logical Arrays   Index

1999-08-06