A very flexible way of introducing extensions in relational languages
is by specifying
them in LL.
In the following, a simple REL extension is described:
global variables.
Global variables can be implemented by allowing the
following two LL functions, setvar and getvar,
to be accessed from REL:
(defun setvar (var value) (eval (list 'setq var (list 'quote value)))) (defun getvar (var) (eval var))
(setvar var value) sets the global LL variable var to value by evaluating (setq var 'value). (getvar var) returns the value of the global LL variable var by simply evaluating var.
From the point of view of REL, such a variable is a
`constant' changeably associated with a term.
Global variables are useful in REL in two situations:
The second situation occurs when all solutions of a query have to be
collected: bagof in PROLOG
or tupof in RELFUN
.
In pure PROLOG and RELFUN, it is impossible to collect all solutions of a query:
intermediate results are reset when backtracking occurs,
which is used to
generate the next solution; thus all computed values are lost.
In order to overcome this difficulty, bagof in PROLOG and
tupof in RELFUN were introduced.
These extra-logicals can easily be implemented via global variables: a global variable holds a stack of lists containing the intermediate results (a stack is necessary because it is possible that in the execution of a tupof another tupof call occurs).