Global Variables

Global variables are associations between names (atoms) and terms.

They differ in various ways from storing information using assert_49 "assert/1" or @ref recorda_51 @"recorda/3". 4 + The value lives on the Prolog (global) stack. This implies that lookup time is independent from the size of the term. This is particularly interesting for large data structures such as parsed XML documents or the CHR global constraint store.

  • They support both global assignment using @ref nb_setval_50 @"nb_setval/2" and backtrackable assignment using @ref b_setval_50 @"b_setval/2".

  • Only one value (which can be an arbitrary complex Prolog term) can be associated to a variable at a time.

  • Their value cannot be shared among threads. Each thread has its own namespace and values for global variables.

Currently global variables are scoped globally. We may consider module scoping in future versions. Both @ref b_setval_50 @"b_setval/2" and @ref nb_setval_50 @"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 @ref initialization_49 @"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 \@ref thread_initialization_49 \@"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 @ref exception_51 @"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 @ref assert_49 @"assert/1" or @ref recorda_51 @"recorda/3".

  • The value lives on the Prolog (global) stack. This implies that lookup time is independent from the size of the term. This is particulary interesting for large data structures qqqsuch as parsed XML documents or the CHR global constraint store.

They support both global assignment using @ref nb_setval_50 @"nb_setval/2" and backtrackable assignment using @ref b_setval_50 @"b_setval/2".

  • Only one value (which can be an arbitrary complex Prolog term) can be associated to a variable at a time.

  • Their value cannot be shared among threads. Each thread has its own namespace and values for global variables.

  • Currently global variables are scoped globally. We may consider module scoping in future versions.

Both @ref b_setval_50 @"b_setval/2" and @ref nb_setval_50 @"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 @ref initialization_49 @"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 \@ref thread_inititialization_49 \@"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.

Define:

1. GLOBALS_C:

1. QUEUE_FUNCTOR_ARITY:

1. QUEUE_ARENA:

1. QUEUE_HEAD:

1. QUEUE_TAIL:

1. QUEUE_SIZE:

1. HEAP_FUNCTOR_MIN_ARITY:

1. HEAP_SIZE:

1. HEAP_MAX:

1. HEAP_ARENA:

1. HEAP_START:

Functions:

1. static GlobalEntry * FindGlobalEntry(Atom at USES_REGS):

1. static Int nb_create_accumulator(USES_REGS1):

1. static Int nb_add_to_accumulator(USES_REGS1):

1. static Int nb_accumulator_value(USES_REGS1):

1. Term Yap_SetGlobalVal(Atom at, Term t0):

1. Term Yap_CopyTermToArena(Term inp, Term *arenap):

1. Term Yap_SaveTerm(Term t0):

1. static Int nb_setval(USES_REGS1):

1. static Int nb_set_shared_val(USES_REGS1):

1. bool Yap_SetBacktrackableGlobalVal(Atom atkey, Term val USES_REGS):

1. static Int p_b_setval(USES_REGS1): : b_setval(+Key, ?Val)

Associate the term Val with the atom Key. This association is undone by backtracking, or by doing a new b_setval_50 "b_setval/2". You can use b_getval to access the term.

1. static int undefined_global(USES_REGS1):

1. static Int nb_getval(USES_REGS1):

1. Term Yap_GetGlobal(Atom at):

1. static Int nbdelete(Atom at USES_REGS):

1. Int Yap_DeleteGlobal(Atom at):

1. static Int nb_delete(USES_REGS1):

1. static Int nb_create(USES_REGS1):

1. static Int nb_creat/2(USES_REGS1):

1. static Int nb_queue_sized(size_t arena_sz USES_REGS):

1. static Int nb_queue(USES_REGS1):

1. static Int nb_queu/2(USES_REGS1):

1. static CELL * GetQueue(Term t, char *caller):

1. static Term GetQueueArena(CELL qd, char caller):

1. static void RecoverArena(Term arena USES_REGS):

1. static void RecoverQueue(Term *qp USES_REGS):

1. static Int nb_queue_close(USES_REGS1):

1. static Int nb_queue_enqueue(USES_REGS1):

1. static Int nb_queue_dequeue(USES_REGS1):

1. static Int nb_queue_replace(USES_REGS1):

1. static Int nb_queue_peek(USES_REGS1):

1. static Int nb_queue_empty(USES_REGS1):

1. static Int nb_queue_size(USES_REGS1):

1. static Int nb_queue_show(USES_REGS1):

1. static CELL * GetHeap(Term t, char *caller):

1. static Term MkZeroApplTerm(Atom f, UInt sz):

1. static Int nb_heap(USES_REGS1):

1. static Int nb_heap_close(USES_REGS1):

1. static Int nb_heap_clear(USES_REGS1):

1. static void DelHeapRoot(CELL *pt, UInt sz):

1. CELL * new_heap_entry(CELL *qd):

1. static Int nb_heap_add_to_heap(USES_REGS1):

1. static Int nb_heap_del(USES_REGS1):

1. static Int nb_heap_peek(USES_REGS1):

1. static Int nb_heap_empty(USES_REGS1):

1. static Int nb_heap_size(USES_REGS1):

1. static Int nb_beam(USES_REGS1):

1. static Int nb_beam_close(USES_REGS1):

1. static void PushBeam(CELL pt, CELL npt, UInt hsize, Term key, Term to):

1. static void DelBeamMax(CELL pt, CELL pt2, UInt sz):

1. static Term DelBeamMin(CELL pt, CELL pt2, UInt sz):

1. static size_t new_beam_entry(CELL qdp)**:

1. static Int nb_beam_add_to_beam(USES_REGS1):

1. static Int nb_beam_del(USES_REGS1):

1. static Int nb_beam_keys(USES_REGS1):

1. static Int nb_beam_peek(USES_REGS1):

1. static Int nb_beam_empty(USES_REGS1):

1. static Int nb_beam_size(USES_REGS1):

1. static Int cont_current_nb(USES_REGS1):

1. static Int init_current_nb(USES_REGS1):

1. void Yap_InitGlobals(void):