![]() |
YAP 7.1.0
|
Global variables are associations between names (atoms) and terms. More...
Global variables are associations between names (atoms) and terms.
They differ in various ways from storing information using assert/1 or recorda/3 4
Currently global variables are scoped globally We may consider module scoping in future versions Both b_setval/2 and nb_setval/2 implicitly create a variable if the referenced name does not already refer to a variable
Global variables may be initialized 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 thread_initialization/1
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 initialized using the exception hook called exception/3 The latter technique is used by CHR
SWI-Prolog global variables are associations between names (atoms) and terms They differ in various ways from storing information using assert/1 or recorda/3
They support both global assignment using nb_setval/2 and backtrackable assignment using b_setval/2
Both b_setval/2 and nb_setval/2 implicitly create a variable if the referenced name does not already refer to a variable
Global variables may be initialized 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 thread_inititialization/1
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
class nb_setval/2 |
nb_setval(+ Name,+ Value)
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 assignment
class nb_set_shared_val/2 |
nb_set_shared_val(+ Name, + Value)
Associates the term Value with the atom Name, but sharing non-backtrackable terms This may be useful if you want to rewrite a global variable so that the new copy will survive backtracking, but you want to share structure with the previous term
The next example shows the differences between the three built-ins:
class nb_delete/1 |
nb_delete(+ Name)
Delete the named global variable
Global variables have been introduced by various Prolog implementations recently We follow the implementation of them in SWI-Prolog, itself based on hProlog by Bart Demoen
GNU-Prolog provides a rich set of global variables, including arrays Arrays can be implemented easily in YAP and SWI-Prolog using functor/3 and setarg/3
due to the unrestricted arity of compound terms
Macros | |
#define | GLOBALS_C 1 |
#define | QUEUE_FUNCTOR_ARITY 4 |
#define | QUEUE_ARENA 0 |
#define | QUEUE_HEAD 1 |
#define | QUEUE_TAIL 2 |
#define | QUEUE_SIZE 3 |
#define | HEAP_FUNCTOR_MIN_ARITY |
#define | HEAP_SIZE 0 |
#define | HEAP_MAX 1 |
#define | HEAP_ARENA 2 |
#define | HEAP_START 3 |
Functions | |
Term | Yap_SetGlobalVal (Atom at, Term t0) |
Term | Yap_CopyTermToArena (Term inp, Term *arenap) |
Term | Yap_SaveTerm (Term t0) |
Term | Yap_GetGlobal (Atom at) |
Int | Yap_DeleteGlobal (Atom at) |
CELL * | new_heap_entry (CELL *qd) |
void | Yap_InitGlobals (void) |