Page Numbers: Yes X: 306 Y: 1.0" First Page: 50
Margins: Top: 1.0" Bottom: 1.3"
Heading:
STANDARD PROCEDURES3-LISP REFERENCE MANUAL March 17, 1984
————————————————————————————————————————————
4.c.8. PROCEDURE DEFINITION, VARIABLES, and ENVIRONMENTS
————————————————————————————————————————————
(DEFINE LABEL FUN)
Establishes a binding of the atom LABEL (not the designation of that atom — i.e., LABEL is in an intensional context) to the function designator (closure) that results from normalising FUN. Unlike SET, DEFINE normalises FUN in an environment in which LABEL will ultimately be bound to the result of the normalisation, to facilitate recursion. (DEFINELABELFUN) is usually used both to establish LABEL as the public name for the function designated by FUN, and to enables FUN to use LABEL as its own internal name for itself. Returns a handle to LABEL [[[puts the string "LABEL" into the comment field of the closure to which FUN normalises]]].
Properties:Smash-env; abnormal.
Macro:(DEFINE LABEL FUN)
W> (BEGIN (SET LABEL FUN)
(SET-COMMENT ↑LABEL (ATOM-NOTATION ↑LABEL))
LABEL)
Examples:1> (DEFINE SQUARE (LAMBDA SIMPLE [N] (* N N)))
1=
’SQUARE
1> (DEFINE FACTORIAL
(LAMBDA SIMPLE [N]
(IF (= N 0) 1 (* N (FACTORIAL (1- N))))))
1=
’FACTORIAL
1> (FACTORIAL 6)
1=
120
(LAMBDA PATTERN BODY1 ... BODYk)
Informally, (LAMBDAPATTERNBODY1 ... BODYk) designates the function that is signified by the lambda abstraction of the formal parameters in pattern PATTERN over the BODY structures. LAMBDA is intensional in all its argument positions: neither PATTERN nor any of the BODY is normalised. More formally, (LAMBDAPATTERNBODY) normalises to the closure consisting of a designator of the current environment, and the non-normalised structures PATTERN and BODY. If k>2, the BODYi are wrapped in a (BEGIN ... ) block.
Properties:Kernel; cons; abnormal.
Examples:(LAMBDA [A B] (* A B))g{closure: }
((LAMBDA [N] (+ N N)) 4)
g8
1> ((LAMBDA [N] (PRINT "Hello") N) 4) Hello
1= 4
(RLAMBDA PATTERN BODY1 ... BODYk)
RLAMBDA is used to define reflective procedures; its arguments are exactly as for LAMBDA, but the closure returned is reflectified.
Examples:1> (DEFINE BOUND
(RLAMBDA [CALL ENV ESC CONT]
(CONT (IF (= "Unbound variable"
(BINDING (1ST (PARGS CALL)) ENV))
’$TRUE
’$FALSE))))
1= ’BOUND
1> (BOUND (ACONS))
1= $FALSE
1> (LET [[X 3]] (BOUND ’X))
1= $TRUE
(MLAMBDA PATTERN BODY1 ... BODYk)
MLAMBDA is used to define macro procedures; its arguments are exactly as for LAMBDA, but the closure returned is macroified. When a procedure call (FOO.ARGS) is normalised and FOO designates a macro procedure, the sequence of events will be as follows: the arguments to the procedure will not be normalised; the defining environment will be extended by matching the pattern against a designator of the unnormalised arguments; the body will be normalised in this new environment; finally, the result of this normalisation will be re-normalised in the original environment.
F-Type:[ RAILS X STRUCTURES X STRUCTURES ] FUNCTIONS Properties: Cons.
Examples:1> (DEFINE INCREMENT
(MLAMBDA [N]
̀ (+ ,N 1)))
1=
’INCREMENT
1> (INCREMENT 5)
1=
6
(SET VAR BINDING)
SET alters the current environment’s binding of the atom VAR to be the result of normalising BINDING (in the current environment). Note that the first argument, VAR, is not normalised. Returns the result of normalising BINDING, and therefore designates the designation of BINDING.
Properties: Smash-env; abnormal.
Examples:1> (SET X (+ 2 2))
1=
4
1> X
1=
4
1> (SET X (+ X X))
1=
8
(SETREF VAR BINDING)
SETREF is a variant of SET in which both VAR and BINDING are normalised. Returns the result of normalising BINDING.
Properties:Smash-env.
F-Type: [ ATOMS X OBJECTS ] OBJECTSProperties: Smash-env.
Examples:1> (SET X ’Y)
1=
’Y
1> (SETREF X (* 2 2))
1=
4
1> Y
1=
4
(ENVIRONMENT E)
(ENVIRONMENT-DESIGNATOR E)
True just in case E designates an environment or environment designator, respectively.
F-Type: [ OBJECTS ] TRUTH-VALUESProperties: Primitive; kernel.
Examples:(ENVIRONMENT GLOBAL)g$TRUE
(ENVIRONMENT (CLOSURE-ENVIRONMENT ↑+))
g$TRUE
(ENVIRONMENT (ECONS))
g$FALSE
(ENVIRONMENT-DESIGNATOR (ECONS))
g$TRUE
GLOBAL
This atom, bound in the global environment, designates the global environment. The environment designator to which GLOBAL is bound is shared across all reflective levels, and is a tail of the environment designator captured in most closures.
F-Type: ENVIRONMENTSProperties: Variable.
Examples:1> (SET XXXX (SQUARE-ROOT 16))
1=
4
1> (BINDING ’XXXX GLOBAL)
1=
’4
1> (
(BINDING ’↑ GLOBAL) 3 4)
1= 7
(ECONS)
Designates an otherwise inaccessible environment designator that in turn designates an empty empty environment. Can be used with BINDING to build environments from scratch.
F-Type: [ ] ENVIRONMENT-DESIGNATORSProperties: Primitive.
Examples:(ECONS)g’{Environment}
(BINDING ’FOO
(ECONS))g{Error: Unbound Variable}
; Empty environments don’t contain bindings for any atoms.
(REBIND VAR BINDING ENV)
Modifies the environment designated by ENV to contain a binding of the structure designated by VAR to the structure designated by BIND. If the structure designated by VAR is already bound, that binding will be modified in place; if not, a new binding of the structure designated by VAR to the structure designated by BIND will be added at the far end of the environment designated by ENV. Environments generated by the 3-LISP processor consist only of atoms bound to normal-form structures, so that VAR should designate an atom and BIND a normal-form internal structure if ENV is intended to continue to designate a well-formed 3-LISP environment. Returns the structure to which BINDING normalises.
F-Type: [ STRUCTURES X STRUCTURES X ENVIRONMENTS ] STRUCTURES
Properties: Cons; smash.
Examples:(LETSEQ [[X 1]
[Y 2]
[ENV (CURRENT-ENVIRONMENT)]]
(BEGIN (REBIND ’X ↑(+ 2 3) ENV)
(REBIND ’Y ↑X ENV)
(+ X Y)))
g10]
(BINDING VAR ENV)
Designates the binding of the internal structure designated by VAR in the environment designated by ENV. The 3-LISP processor will on its own only establish normal-form bindings for atoms, so VAR should designate an atom unless the user provides his or her own environment structure (in which case BINDING can be used as a 3-LISP analog of LISP 1.5’s ASSOC). Designates the string ‘Unbound variable’ if the designation of VAR is unbound in the environment designated by ENV.
F-Type: [ STRUCTURES X ENVIRONMENTS ] STRUCTURESProperties: Kernel.
Examples:(BINDING ’Y (BIND ’Y ’3 (ECONS)))g’3
(BINDING ’NORMALISE GLOBAL)
g’{NORMALISE closure}
(LET [[X (+ 1 2)]]
((RLAMBDA [CALL ENV ESC CONT]
(CONT (BINDING ’X ENV)))))
g3
(BIND PATTERN ARGS ENV)
Designates an environment obtained by augmenting the environment designated by ENV with the variable bindings that result from matching of the pattern structure designated by PATTERN against the argument structure designated by ARGS. A pattern consisting of a single atom will match any argument structure directly; this results in the atom becoming bound to the entire argument structure. This basic matching process is extended to rail patterns in the usual way: pattern and argument rails must match on an element by element basis.
F-Type: [ STRUCTURES X STRUCTURES X ENVIROMENTS ] ENVIROMENTS Properties: Kernel; cons.
[[[Examples:(BIND ’X ’2 [[’Y ’1]]) g[[’X ’2] [’Y ’1]]
(BIND ’[X] ’[2] [[’Y ’1]])
g[[’X ’2] [’Y ’1]]
(BIND ’[X] ’’[2] [[’Y ’1]])
g[[’X ’’2] [’Y ’1]]
(BIND ’[X Z] ’[2 3] [[’Y ’1]])
g[[’X ’2] [’Z ’3] [’Y ’1]]
(BIND ’[X Y] ’[[2] 3] [[’Y ’1]])
g[[’X ’[2]] [’Y ’3] [’Y ’1]]
(BIND ’[X [Z]] ’[2 [3]] [[’Y ’1]])
g[[’X ’2] [’Z ’3] [’Y ’1]]
(BIND ’[X] ’2 [[’Y ’1]])
g{ERROR}
(BIND ’[X] ’’’[2] [[’Y ’1]])
g{ERROR}
(BIND ’(A . B) ’(1 . 2) [[’Y ’1]])
g{ERROR}]]]
(LET [[P1 E1] ... [Pk Ek]] BODY1 ... BODYk)
Designates the designation that BODY has in an environment which is like the current environment except extended by matching the patterns Pi to the results of normalising the expressions Ei in environment the current environment. In other words all of the Ei are normalised in the same environment. It can be determined (because of the way in which rails are normalised) that the Ei will be normalised sequentially, but it is considered bad programming practice to depend on this fact (BEGIN should be used for explicit sequential processing).
Properties:Kernel; env; abnormal.
Macro:(LET [[P1 E1] ... [Pk Ek]] BODY)
W> ((LAMBDA SIMPLE [P1 ... Pk] BODY) E1 ... Ek)
Examples:(LET [[X 3] [Y 4]] (+ X Y))g7
(LET [[[A B] (REST [1 2 3])]] (+ A B))
g5
(LET [[X 3]]
(LET [[X 4] [Y X]] (+ X Y)))
g7
(LETSEQ [[P1 E1] ... [Pk Ek]] BODY1 ... BODYk)
LETSEQ is like LET except that each expression Ei+1 is normalised in the environment that results from extending the previous environment with the results of matching pattern Pi against the normalisation of Ei.
Properties:Env; abnormal.
Macro:(LETSEQ [[P1 E1][P2 E2] ... [Pk Ek]] BODY)
W> (LET [[P1 E1]]
(LETSEQ [[
P2 E2] ... [Pk Ek]] BODY))
Examples:(LET [[X 3]]
(LETSEQ [[X 4] [Y X]] (+ X Y)))
g8
(LETREC [[V1 E1] ... [Vk Ek]] BODY1 ... BODYk)
Like LET and LETSEQ except that each expression Ei is normalised in the environment that results from extending the original environment with the results of binding all of the variables Vi against the normalisations of their Ei.
Properties:Env; abnormal.
Macro:(LETREC [[V1 E1][V2 E2] ... [Vk Ek]] BODY)
W> (LET [[V1 ’HUCAIRZ][P2 ’HUCAIRZ] ... [Vk HUCAIRZ]]
(BLOCK (SET
V1 E1)
(SET
V2 E2)
...
(SET
Vk Ek)
BODY))
Examples:(LETREC [[EVEN (LAMBDA SIMPLE [N]
(IF (= N 0) $T (ODD (1- N))))]
[ODD (LAMBDA SIMPLE [N]
(IF (= N 0) $F (NOT (EVEN N))))]]
(SCONS (EVEN 2) (ODD 2)))
g[$T $F]