YAP 7.1.0

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

Detailed Description

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 Documentation

◆ nb_setval/2

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

◆ nb_set_shared_val/2

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:

?-
nb_setval(a,a(_)),nb_getval(a,A),nb_setval(b,t(C,A)),nb_getval(b,B).
A = a(_A), B = t(_B,a(_C)) ?
?-
nb_setval(a,a(_)),nb_getval(a,A),nb_set_shared_val(b,t(C,A)),nb_getval(b,B).
?-
nb_setval(a,a(_)),nb_getval(a,A),nb_linkval(b,t(C,A)),nb_getval(b,B).
A = a(_A),
B = t(C,a(_A)) ?

◆ nb_delete/1

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

Macro Definition Documentation

◆ GLOBALS_C

#define GLOBALS_C   1

Definition at line 94 of file globals.c.

◆ HEAP_ARENA

#define HEAP_ARENA   2

Definition at line 135 of file globals.c.

◆ HEAP_FUNCTOR_MIN_ARITY

#define HEAP_FUNCTOR_MIN_ARITY

Definition at line 131 of file globals.c.

◆ HEAP_MAX

#define HEAP_MAX   1

Definition at line 134 of file globals.c.

◆ HEAP_SIZE

#define HEAP_SIZE   0

Definition at line 133 of file globals.c.

◆ HEAP_START

#define HEAP_START   3

Definition at line 136 of file globals.c.

◆ QUEUE_ARENA

#define QUEUE_ARENA   0

Definition at line 126 of file globals.c.

◆ QUEUE_FUNCTOR_ARITY

#define QUEUE_FUNCTOR_ARITY   4

Definition at line 124 of file globals.c.

◆ QUEUE_HEAD

#define QUEUE_HEAD   1

Definition at line 127 of file globals.c.

◆ QUEUE_SIZE

#define QUEUE_SIZE   3

Definition at line 129 of file globals.c.

◆ QUEUE_TAIL

#define QUEUE_TAIL   2

Definition at line 128 of file globals.c.

Function Documentation

◆ new_heap_entry()

CELL * new_heap_entry ( CELL *  qd)

Definition at line 1064 of file globals.c.

◆ Yap_CopyTermToArena()

Term Yap_CopyTermToArena ( Term  inp,
Term *  arenap 
)

Definition at line 306 of file globals.c.

◆ Yap_DeleteGlobal()

Int Yap_DeleteGlobal ( Atom  at)

Definition at line 534 of file globals.c.

◆ Yap_GetGlobal()

Term Yap_GetGlobal ( Atom  at)

Definition at line 459 of file globals.c.

◆ Yap_InitGlobals()

void Yap_InitGlobals ( void  )

Definition at line 1655 of file globals.c.

◆ Yap_SaveTerm()

Term Yap_SaveTerm ( Term  t0)

Definition at line 311 of file globals.c.

◆ Yap_SetGlobalVal()

Term Yap_SetGlobalVal ( Atom  at,
Term  t0 
)

Definition at line 291 of file globals.c.

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)