Backtrackable global variables

Author(s): Jose F. Morales, Rémy Haemmerlé.

This module provides a simple way to assign and read fully backtrackable global variables.

Global variables differ from storing information using dynamic predicates:

  • Global variables are semantically equivalent to a dictionary passed around as an implicit pair of variables:
    • only one value is associated to a variable at a time
    • the value associated to a variable can be replaced
    • any assignment is undone on backtracking
    • access cost is proportional to a unification
    • variable sharing is preserved during assignment
  • Keys of global variables are local to each module.
  • Contrary to dynamic predicates, there is no copy of terms. This is particularly interesting for large terms.

The implementation is based on a globally reachable structure in the heap, where each entry is allocated dynamically for each key.

Note that the current implementation has some limitations (see bug entries in this module).

Usage and interface

Documentation on exports

PREDICATEsetval/2

Usage:setval(Name,Value)

Associate the term Value with the atom Name. If Name does not refer to an existing global variable, an unbounded global variable Value is created and unified to Value. On backtracking the assignment is reversed. If Name is not a atom the predicate silently fails.

    PREDICATEgetval/2

    Usage:getval(Name,Value)

    Unifies Value with the current value of the global variable refered to by the atom Name. If Name does not refer to an exisiting global variable, a free unbound variable is created and unified with Value. If Name is not an atom the predicate silently fails.

      PREDICATEcurrent/2

      Usage:current(Name,Value)

      Enumerate all defined variables with their value. The order of enumeration is undefined.

        Documentation on imports

        This module has the following direct dependencies:

        Known bugs and planned improvements

        • The use of global variables may produce incorrect results for sharing analysis.
        • We still do not support statically declared global variables
        • Allocation of entries is not undone on backtracking, which may exhaust available space quickly for some programs
        • No more than 255 modules can use global variables in a program.
        • No more than 255 global variables can be used in a module.