REDUCE

8.3 Variables and Bindings

Variables in PSL are ids, and associated values are usually stored in and retrieved from the value cell of this id. If variables appear as parameters in lambda expressions or in prog’s, the contents of the value cell are saved on a binding stack. A new value or nil is stored in the value cell and the computation proceeds. On exit from the lambda or prog the old value is restored. This is called the ”shallow binding” model of LISP. It is chosen to permit compiled code to do binding efficiently. For even more efficiency, compiled code may eliminate the variable names and simply keep values in registers or a stack. The scope of a variable is the range over which the variable has a defined value. There are three different binding mechanisms in PSL.

Local Binding

Only compiled functions bind variables locally. Local variables occur as formal parameters in lambda expressions and as prog form variables. The binding occurs as a lambda expression is evaluated or as a prog form is executed. The scope of a local variable is the body of the function in which it is defined.

Fluid Binding

Fluid variables are global in scope but may occur as formal parameters or prog form variables. In interpreted functions, all formal parameters and local variables are considered to have fluid binding until changed to local binding by compilation. A variable can be treated as a fluid only by declaration. If fluid variables are used as parameters or locals they are rebound in such a way that the previous binding may be restored. All references to fluid variables are to the currently active binding. Access to the values is by name, going to the value cell.

Global Binding

In theory global variables may never be used as parameters in lambda expressions or as prog variables. This restriction is not enforced in PSL. You are encouraged not to declare an identifier global if it is used as a parameter in a lambda expression or prog. For more information see the Section Fluid and Global Declarations, Chapter 19.

8.3.1 Binding Type Declaration

(fluid IDLIST:id-list): nil expr
The ids in IDLIST are declared as fluid type variables. An id which has not been previously declared is initialized to nil. Variables in IDLIST already declared fluid are ignored. It is an error to attempt to change the type of a variable from global to fluid.

⋆⋆⋆⋆⋆ ID cannot be changed to FLUID

(global IDLIST:id-list): nil expr
The ids of IDLIST are declared global type variables. If an id has not been previously declared, it is initialized to nil. Variables which have already been declared global are ignored. Attempting to change the type of an id from fluid to global will result in an error.

⋆⋆⋆⋆⋆ ID cannot be changed to GLOBAL

(unfluid IDLIST:id-list): nil expr
The variables in IDLIST which have been declared as fluid are no longer considered fluid. Other variables are ignored.

8.3.2 Binding Type Predicates

(fluidp U:id): boolean expr
If U has been declared fluid then t is returned, otherwise nil is returned.

(globalp U:id): boolean expr
If U has been declared global then t is returned, otherwise nil is returned.

(unboundp U:id): boolean expr
If U does not have a value then t is returned, otherwise nil is returned.