Warning: template has been deprecated. evaluates to

( BETA ( LAMBDA <vars> <body> ) E ) in environment E

When a closure is to be applied to some arguments:

(( BETA ( LAMBDA <vars> <body> ) E1 ) <args> ) in environment E2

this is performed by reducing the application expression to

<body> in environment Pairlis[<vars> <args in E2> E1]

That is, any free variables in the closed lambda expression refer to the environment as of the time of closure ( E1 ), not as of the time of application ( E2 ), whereas the arguments are evaluated in the application environment as expected.

This notion of closure has gone by many other names in other contexts. In LISP, for example, such a closure has been traditionally known as a funarg. ALGOL has several related ideas. Every ALGOL procedure is, at the time of its invocation, essentially a "downward funarg". In addition, expressions which are passed by name instead of by value are closed through the use of mechanisms called thunks [Ingerman]. It turns out that an actor (other than a cell or a serializer) is also a closure. Hewitt [Smith and Hewitt] describes an actor as consisting of a script, which is code to be executed, and a set of acquaintances, which are other actors which it knows about. We contend that the script is in fact identical to the lambda expression in a closure, and that the set of acquaintances is in effect an environment. As an example, consider the following code for cons; taken from [Smith and Hewitt] (cf. [Fischer]):

[CONS ≡ (≡> [=A =B] (CASES (≡> FIRST? A) (≡> REST? B) (≡> LIST? YES)))]

(cons x y)

cases

first?

rest?

x

y

a

b

cases

x

y

first?

rest?

yes

​

( DEFINE CONS ( LAMBDA ( A B ) ( LAMBDA ( M ) ( IF ( EQ M 'FIRST? ) A ( IF ( EQ M 'REST? ) B ( IF ( EQ M 'LIST? ) 'YES ( ERROR '|UNRECOGNIZED MESSAGE - CONS| M 'WRNG-TYPE-ARG )))))))

When the formis evaluated, the result is an (evaluated)statement, which is a receiver ready to accept a message such as "" or "". This resulting receiver evidentlythe actorsandas being bound to the variablesand; it is evidently aof thescript plus a set of acquaintances which includesand(as well as "" and "" and "", for example; PLASMA considers such "constant acquaintances" to be part of the set, whereas in SCHEME we might prefer to consider them part of the script). Once we realize that it is a closure and nothing more, we can see easily how to express the same semantics in SCHEME:

Note that we have used explicit eq tests on the incoming message rather than the implicit pattern-matching of the cases statement, but the underlying semantics of the approach are the same.

There are several important consequences of closing every lambda expression in the environment from which it is passed (i.e., in its "lexical" or "static" environment). First, the axioms of lambda calculus are automatically preserved. Thus, referential transparency is enforced. This in turn implies that there are no "fluid" variable bindings (as there are in standard stack implementations of LISP such as MacLISP). Second, the upward funarg problem [Moses][6] requires that the environment structure be potentially tree-like. Finally, the environment at any point in a computation can never be deeper than the lexical depth of the expression being evaluated at that time; i.e., the environment contains bindings only for variables bound in lambdas lexically surrounding the expression being evaluated. This is true even if recursive functions are involved. It follows that if list structures are used to implement environments, the time to look up a variable is proportional only to the lexical distance from the reference to the binding and not to the depth of recursion or any other dynamic parameter. Therefore it is not necessarily as expensive as many people have been led to believe. Furthermore, it is not even necessary to scan the environment for the variable, since its value must be in a known position relative to the top of the environment structure; this position can be computed by a compiler at compile time on the basis of lexical scope. The tree-like structure of an environment prevents one from merely indexing into the it, however; it is necessary to cdr down it. (Some ALGOL compilers use a similar technique involving base registers pointing to sets of variables for each level of block nesting; it is necessary to determine the base pointer for the block desired for a variable reference, but then the variable is at a known offset from the base address.) It also follows that an iterative programming style will lead to no net accumulation of environment structures in the interpreter. The recursive style, with or without continuation-passing, will lead to accumulation of environment structures as a function of the recursion depth, not because any environment becomes arbitrarily deep, but rather because at each level of recursion it is necessary to save the environment at that point. It is saved by the interpreter in the case of traditional recursion, so that computation can continue in the correct environment on return from the recursive call; it is saved as part of the continuation closure in continuation-passing.

Another problem is concerned with control. This is a consequence of the ​functional interpretation of the lambda calculus, i.e. the view that a "expression" (combination) represents a value to be "returned" (to replace the combination) to its "caller" (the process evaluating the combination containing the original one). The interpreter must provide for correctly resuming the caller when the callee has returned its value. The state of the computation at the time of the call must therefore be preserved. We see, then, that part of the state of the computation must be (a pointer to) the preserved state of its caller; we will call this component of the state the clink [McDermott and Sussman][7] [Bobrow and Wegbreit][8]. Just before the evaluation of a subexpression, the state of the current computation, including the clink, must be gathered together into a single data structure, which we will call a frame; the clink is then altered to point to this new frame. The evaluation of the subexpression then returns by restoring the state of the process from the current clink. Note that the value of the subexpression had better not be part of the state, for otherwise it would be lost by the state restoration. Thus, we only build a new frame if further computation would result in losing information which might be necessary. This only occurs if we must somehow return to that state. This in turn can only occur if we must evaluate an expression whose value must be obtained in order to continue computation in the current state.

This implies that no frame need be created in order to apply a lambda expression to its arguments. This in turn implies that the iterative and continuation-passing styles lead to no net creation of frames, because they are implemented only in terms of explicit lambda applications, whereas the recursive style leads to the creation of one net frame per level of recursive depth, because the recursive invocation involves the evaluation of a expression containing the recursive lambda application as a subexpression.

A clink in a lambda calculus-based interpreter is in fact equivalent to a low-level default continuation as created by the PLASMA interpreter. Such a continuation is a (closed) lambda expression of one argument whose script will carry on the computation after receiving the value of the subexpression. The clink mechanism is therefore not necessary, if we are willing to transform all our programs into pure continuation-passing style. We could do this explicitly, by requiring the user to write his programs in this form; or implicitly, as PLASMA does, by creating these one-argument continuations as necessary, passing them as hidden extra arguments to lambda expressions which behave like functions. On the other hand, we may think of a clink as a highly optimized continuation, whose "script" is that carefully coded portion of the lambda calculus interpreter which restores the frame and then carries on. We find this notion useful in defining a primitive, CATCH (named for the CATCH construct in MacLISP [Moon][4]), for "hairy control structure", similar to Reynolds' ESCAPE operator [Reynolds][9], which makes these low-level continuations available to the user. Note that PLASMA has a similar facility for getting hold of the low-level continuations, namely the "≡≡>" receiver construct.

Another problem for the implementor of an interpreter of a lambda calculus based language is the order in which to perform reductions. There are two standard orders of evaluation (and several other semi-standard ones, which we will not consider here). The first is Normal Order, which ​corresponds roughly to ALGOL's "call by name", and the second is Applicative Order, which corresponds roughly to ALGOL's "call by value" or to LISP functional application.

Under a call-by-name implementation, the <args>* mentioned above are in fact the actual argument expressions, each paired with the environment E . The evaluator has two additional rules:

when a variable x is to be evaluated in environment E1 , then its associated expression-environment pair [ A , E2 ] (which is equivalent to an ALGOL thunk) is looked up in E1 , and then A is evaluated in E2 . when a "primitive operator" is to be applied, its arguments must be evaluated at that time, and then the operator applied in a call-by-value manner.

Under a call-by-value implementation, the <args>* are the values of the argument expressions; i.e., the argument expressions are evaluated in environment E , and only then is the lambda expression applied. Note that this leads to trouble in defining conditionals. Under call-by-name one may define predicates to return (LAMBDA (X Y) X) for TRUE and (LAMBDA (X Y) Y) for FALSE , and then one may simply write

(( = A B ) <do this if TRUE> <do this if FALSE> )

This trick depends implicitly on the order of evaluation. It will not work under call-by-value, nor in general under any other reductive order except Normal Order. It is therefore necessary to introduce a special primitive operator (such as " if ") which is applied in a call-by-name manner. This leads us to the interesting conclusion that a practical lambda calculus interpreter cannot be purely call-by-name or call-by-value; it is necessary to have at least a little of each.

There is a fundamental problem, however, with using Normal Order evaluation in a lambda calculus interpreter, which is brought out by the iterative programming style. We already know that no net frames are created by iterative programs, and that no net environment structures are created either. The problem is that under a call-by-name implementation there may be a net thunk structure created proportional in size to the number of iteration steps. This problem is inherent in Normal Order, because Normal Order substitution semantics exhibit the same phenomenon of increasing expression size. Therefore iteration cannot be effectively modeled in a call-by-name interpreter. An alternative view is that a call-by-name interpreter remembers more than is logically necessary to perform the computations indicated by the original expressions. This is indicated by the fact that the Applicative Order substitution semantics lead to expressions of fixed maximum size independent of the number of iteration steps.

It turns out that this conflict between call-by-name and iteration is resolved by the use of continuation-passing. If we use a pure continuation-passing programming style, then Normal Order and Applicative Order are the same order! In pure continuation-passing no combination is ever a subcombination of another combination. (This is the justification for the fact mentioned above that no clinks are needed if pure continuation-passing style is used.) Thus, if we wish to model iteration in pure lambda calculus without even an if primitive, we can use Normal Order substitutions and ​express the iteration in the continuation-passing style.

Under any reductive order, whether Normal Order, Applicative Order, or any other order, it is in practice convenient to introduce a means of terminating the evaluation process on a given form; in order to do this we introduce three different and equally useful notions. The first is the primitive operator such as + ; the evaluator can apply such an operator directly, without substituting a lambda expression for the operator and reducing the resulting form. The second is the self-evaluating constant; this is used for primitive objects such as numbers, which effectively behave as if always "bound to themselves" in any environment. The third is the quoting function, which protects its argument from reductions so that it is returned as is; this is used for treating forms as data in the usual LISP manner.

These three ideas are not logically necessary, since the evaluation process will (eventually) terminate when no reductions can be made, but they are a great convenience for introducing various functions and data into the lambda calculus. Note too that some are easily defined in terms of the others; for example, instead of letting 3 be a self-evaluating constant, we could let 3 be a primitive operator of no arguments which returned 3 , or we could merely quote it; similarly, instead of quoting forms we could let forms be a self-evaluating data type, as in MDL [Galley and Pfister] (better known as MUDDLE), written with different parentheses. Because, as we have said, these constructs are all strictly for convenience, we will not strive for any kind of minimality, but will continue to use all three notions in our interpreter, as we already have in our examples. We provide an interface so that all MacLISP subr s may be used as primitive operators; we define numbers to be self-evaluating; and we will use QUOTE to quote forms as in LISP (and thus we may use the " ' " character as an abbreviation).

One final issue which the implementor of a lambda calculus based interpreter should consider is that of extensions to the language, such as primitives for side effects, multiprocessing, and synchronization of processes. Note that these are ideas which are very hard, if not impossible, to model using the substitution semantics of the lambda calculus, but which are easily incorporated in other semantic models, including the environment interpreter and, perhaps more notably, the ACTORS model [Greif and Hewitt]. The fundamental problem with modelling such concepts using substitution semantics is that substitution produces copies of expressions, and so cannot model the notion of sharing very well. In an interpreter which uses environments, all instances of a variable scoped in a given environment refer to the same virtual substitution contained in that environment, and so may be thought of as sharing a value cell in that environment. We can take advantage of this sharing by introducing a primitive operator which modifies the contents of a value cell; since all occurrences refer to the same value cell, changing the contents of that value cell will change the result of future references to that value cell (i.e., occurrences of the variable which invoke the virtual substitution mechanism). Such a primitive operator would then be similar to the SET function of LISP, or the := of ALGOL. We include such an operator, ASET , in our interpreter.

Introducing multiprocessing into the interpreter is fairly straightforward; all that is necessary is to introduce a mechanism for ​time-slicing the interpreter among several processes. One can even model this in substitution semantics by supposing that there can be more than one expression, and at each step an expression is randomly chosen to perform a reduction within. (On the other hand, synchronizing of the processes is very hard to model using substitution semantics!)

:=

EVALUATE!UNINTERRUPTIBLY

​

Warning: template has been deprecated. Section 5: The Implementation of the Interpreter [ edit ] Here we present a real live SCHEME interpreter. This particular version was written primarily for expository purposes; it works, but not as efficiently as possible. The "production version" of SCHEME is coded somewhat more intricately, and runs about twice as fast as the interpreter presented below. The basic idea behind the implementation is think machine language. In particular, we must not use recursion in the implementation language to implement recursion in the language being interpreted. This is a crucial mistake which has screwed many language implementations (e.g. Micro-PLANNER [Sussman]). The reason for this is that if the implementation language does not support certain kinds of control structures, then we will not be able to effectively interpret them. Thus, for example, if the control frame structure in the implementation language is constrained to be stack-like, then modelling more general control structures in the interpreted language will be very difficult unless we divorce ourselves from the constrained structures at the outset. It will be convenient to think of an implementation machine which has certain operations, which are "micro-coded" in LISP; these are used to operate on various "registers", which are represented as free LISP variables. These registers are: **EXP** The expression currently being evaluated. **ENV** A pointer to the environment in which to evaluate EXP . **CLINK** A pointer to the frame for the computation of which the current one is a subcomputation. **PC** The "program counter". As each "instruction" is executed, it updates **PC** to point to the next instruction to be executed. **VAL** The returned value of a subcomputation. This register is not saved and restored in **CLINK** frames; in fact, its sole purpose is to pass values back safely across the restoration of a frame. **UNEVLIST** , **EVLIS** These are utility registers which are part of the state of the interpreter (they are saved in **CLINK** frames). They are used primarily for evaluation of components of combinations, but may be used for other purposes also. ​ **TEM** A super-temporary register, used for random purposes, and not saved in **CLINK** frames or across interrupts. It therefore may not be used to pass information between "instructions" of the "machine", and so is best thought of as an internal hardware register. **QUEUE** A list of all processes other than the one currently being interpreted. **TICK** A magic register which a "hardware clock" sets to T every so often, used to drive the scheduler. **PROCESS** This register always contains the name of the process currently swapped in and running. ​ Warning: template has been deprecated. The following declarations and macros are present only to make the compiler happy, and to make the version number of the SCHEME implementation available in the global variable VERSION . ( DECLARE ( SPECIAL **EXP** **UNEVLIS** **ENV** **EVLIS** **PC** **CLINK** **VAL** **TEM** **TOP** **QUEUE** **TICK** **PROCESS** **QUANTUM** VERSION LISPVERSION )) ( DEFUN VERSION MACRO ( X ) ( COND ( COMPILER-STATE ( LIST 'QUOTE ( STATUS UREAD ))) ( T ( RPLACA X 'QUOTE ) ( RPLACD X ( LIST VERSION )) ( LIST 'QUOTE VERSION )))) ( DECLARE ( READ )) ( SETQ VERSION (( LAMBDA ( COMPILER-STATE ) ( VERSION )) T )) The function SCHEME initializes the system driver. The two SETQ 's merely set up version numbers. The top level loop itself is written in SCHEME, and is a LABELS which binds the function **TOP** to be a read-eval-print loop. The LISP global variable **TOP** is initialized to the closure of the **TOP** function for convenience and accessibility to user-defined functions. ( DEFUN SCHEME () ( SETQ VERSION ( VERSION ) LISPVERSION ( STATUS LISPVERSION )) ( TERPRI ) ( PRINC '|This is SCHEME | ) ( PRINC VERSION ) ( PRINC '| running in LISP | ) ( PRINC LISPVERSION ) ( SETQ **ENV** NIL **QUEUE** NIL **PROCESS** ( CREATE!PROCESS ' ( **TOP** '|SCHEME -- Toplevel| ))) ( SWAPINPROCESS ) ( ALARMCLOCK 'RUNTIME **QUANTUM** ) ( MLOOP )) ( SETQ **TOP** ' ( BETA ( LAMBDA ( **MESSAGE** ) ( LABELS (( **TOP1** ( LAMBDA ( **IGNORE1** **IGNORE2** **IGNORE3** ) ( **TOP1** ( TERPRI ) ( PRINC '|==> | ) ( PRINT ( SET '* ( EVALUATE ( READ )))))))) ( **TOP1** ( TERPRI ) ( PRINC **MESSAGE** ) NIL ))) NIL )) When the LISP alarmclock tick occurs, the global register **TICK** is set to T . **QUANTUM** , the amount of runtime between ticks, is measured in ​micro-seconds. ( DEFUN SETTICK ( X ) ( SETQ **TICK** T )) ( SETQ **QUANTUM** 1000000. ALARMCLOCK 'SETTICK ) MLOOP is the main loop of the interpreter. It may be thought of as the instruction dispatch in the micro-code of the implementation machine. If an alarmclock tick has occurred, and interrupts are allowed, then the scheduler is called to switch processes. Otherwise the "instruction" specified by **PC** is executed via FASTCALL . ( DEFUN MLOOP () ( DO (( **TICK** NIL )) ( NIL ) ;DO forever ( AND **TICK** ( ALLOW ) ( SCHEDULE )) ( FASTCALL **PC** ))) FASTCALL is essentially a FUNCALL optimized for compiled "microcode". Note the way it pulls the SUBR property to the front of the property list if possible for speed. ( DEFUN FASTCALL ( ATSYM ) ( COND (( EQ ( CAR ( CDR ATSYM )) 'SUBR ) ( SUBRCALL NIL ( CADR ( CDR ATSYM )))) ( T (( LAMBDA ( SUBR ) ( COND ( SUBR ( REMPROP ATSYM 'SUBR ) ( PUTPROP ATSYM SUBR 'SUBR ) ( SUBRCALL NIL SUBR )) ( T ( FUNCALL ATSYM )))) ( GET ATSYM 'SUBR ))))) Interrupts are allowed unless the variable *ALLOW* is bound to NIL in the current environment. This is used to implement the EVALUATE!UNINTERRUPTIBLY primitive. ( DEFUN ALLOW () (( LAMBDA ( VCELL ) ( COND ( VCELL ( CADR VCELL )) ( T T ))) ( ASSQ '*ALLOW* **ENV** ))) Next comes the scheduler. It is apparently interrupt-driven, but in fact is not. The key here is to think microcode! There is one place in the microcoded instruction interpretation loop which checks to see if there is an interrupt pending; in our "machine", this occurs in MLOOP , where **TICK** is checked on every cycle. This is another case where we must beware of using too much of the power of the host language; just as we must avoid using host recursion directly to implement recursion, so we must avoid using host interrupts directly to implement interrupts. We may not modify any register during a host language interrupt, except one (such as **TICK** ) which is ​specifically intended to signal interrupts. Thus, if we were to add an interrupt character facility to SCHEME similar to that in MacLISP [Moon], the MacLISP interrupt character function would merely set a register like **TICK** and dismiss; MLOOP would eventually notice that this register had changed and dispatch to the interrupt handler. All this implies that the "microcode" for the interrupt handlers does not itself contain critical code that must be protected from host language interrupts. When the scheduler is invoked, if there is another process waiting on the process queue, then the current process is swapped out and put on the end of the queue, and a new process swapped in from the front of the queue. The process stored on the queue consists of an atom which has the current frame and **VAL** register on its property list. Note that the **TEM** register is not saved, and so cannot be used to pass information between instructions. ( DEFUN SCHEDULE () ( COND ( **QUEUE** ( SWAPOUTPROCESS ) ( NCONC **QUEUE** ( LIST **PROCESS** )) ( SETQ **PROCESS** ( CAR **QUEUE** ) **QUEUE** ( CDR **QUEUE** )) ( SWAPINPROCESS ))) ( SETQ **TICK** NIL ) ( ALARMCLOCK 'RUNTIME **QUANTUM** )) ( DEFUN SWAPOUTPROCESS () (( LAMBDA ( **CLINK** ) ( PUTPROP **PROCESS** ( SAVEUP **PC** ) 'CLINK ) ( PUTPROP **PROCESS** **VAL** 'VAL )) **CLINK** )) ( DEFUN SWAPINPROCESS () ( SETQ **CLINK** ( GET **PROCESS** 'CLINK ) **VAL** ( GET **PROCESS** 'VAL )) ( RESTORE )) Primitive operators are LISP functions, i.e. SUBR s, EXPR s, and LSUBR s. ( DEFUN PRIMOP ( x ) ( GETL x ' ( SUBR EXPR LSUBR ))) SAVEUP conses a new frame onto the **CLINK** structure. It saves the values of all important registers. It takes one argument, RETAG , which is the instruction to return to when the computation is restored. ( DEFUN SAVEUP ( RETAG ) ( SETQ **CLINK** ( LIST **EXP** **UNEVLIS** **ENV** **EVLIS** RETAG **CLINK** ))) RESTORE restores a computation from the CLINK . The use of TEMP is a kludge to optimize the compilation of the "microcode". ​ Warning: template has been deprecated. ( DEFUN RESTORE () ( PROG ( TEMP ) ( SETQ TEMP ( OR **CLINK** ( ERROR '|PROCESS RAN OUT - RESTORE| **EXP** 'FAIL-ACT )) **EXP** ( CAR TEMP ) TEMP ( CDR TEMP ) **UNEVLIS** ( CAR TEMP ) TEMP ( CDR TEMP ) **ENV** ( CAR TEMP ) TEMP ( CDR TEMP ) **EVLIS** ( CAR TEMP ) TEMP ( CDR TEMP ) **PC** ( CAR TEMP ) TEMP ( CDR TEMP ) **CLINK** ( CAR TEMP )))) This is the central function of the SCHEME interpreter. This "instruction" expects **EXP** to contain an expression to evaluate, and **ENV** to contain the environment for the evaluation. The fact that we have arrived here indicates that **PC** contains 'AEVAL , and so we need not change **PC** if the next instruction is also to be AEVAL . Besides the obvious objects likes numbers, identifiers, LAMBDA expressions, and BETA expressions (closures), there are also several other objects of interest. There are primitive operators (LISP functions); AINT s (which are to SCHEME as FSUBR s like COND are to LISP); and AMACRO s, which are used to implement DO , AND , OR , COND , BLOCK , etc. ​ ( DEFUN AEVAL () ( COND (( ATOM **EXP** ) ( COND (( NUMBERP **EXP** ) ( SETQ **VAL** **EXP** ) ( RESTORE )) (( PRIMOP **EXP** ) ( SETQ **VAL** **EXP** ) ( RESTORE )) (( SETQ **TEM** ( ASSQ **EXP** **ENV** )) ( SETQ **VAL** ( CADR **TEM** )) ( RESTORE )) ( T ( SETQ **VAL** ( SYMEVAL **EXP** )) ( RESTORE )))) (( ATOM ( CAR **EXP** )) ( COND (( SETQ **TEM** ( GET ( CAR **EXP** ) 'AINT )) ( SETQ **PC** **TEM** )) (( EQ ( CAR **EXP** ) 'LAMBDA ) ( SETQ **VAL** ( LIST 'BETA **EXP** **ENV** )) ( RESTORE )) (( SETQ **TEM** ( GET ( CAR **EXP** ) 'AMACRO )) ( SETQ **EXP** ( FUNCALL **TEM** **EXP** ))) ( T ( SETQ **EVLIS** NIL **UNEVLIS** **EXP** **PC** 'EVLIS )))) (( EQ ( CAAR **EXP** ) 'LAMBDA ) ( SETQ **EVLIS** ( LIST ( CAR **EXP** )) **UNEVLIS** ( CDR **EXP** ) **PC** 'EVLIS )) ( T ( SETQ **EVLIS** NIL **UNEVLIS** **EXP** **PC** 'EVLIS )))) This is the central function of the SCHEME interpreter. This "instruction" expectsto contain an expression to evaluate, andto contain the environment for the evaluation. The fact that we have arrived here indicates thatcontains, and so we need not changeif the next instruction is also to be. Besides the obvious objects likes numbers, identifiers,expressions, andexpressions (closures), there are also several other objects of interest. There are primitive operators (LISP functions);s (which are to SCHEME ass likeare to LISP); ands, which are used to implement, etc. We come to EVLIS when a combination is encountered. The intention is to evaluate each component of the combination and then apply the resulting function to the resulting arguments. We use the register **UNEVLIS** to hold the list of components yet to be evaluated, and the register **EVLIS** to hold the list of evaluated components. We assume that these have been set up by AEVAL . Note that in the case of an explicit LAMBDA expression in the CAR of a combination **UNEVLIS** is initialized to be the list of unevaluated arguments and **EVLIS** is initialized to be the list containing the lambda expression. EVLIS checks to see if there remain any more components yet to be evaluated. If not, it applies the function. Note that the primitive operators are applied using the LISP function APPLY . Note also how a BETA expression controls the environment in which its body is to be evaluated. DELTA expressions are CATCH tags (see CATCH ). It is interesting that the evaluated components are collected in the reverse order from that which we need them in, and so we must reverse the list before applying the function. Do you see why we must not use side effects (e.g. the NREVERSE function) to reverse the list? Think about CATCH ! If there remain components yet to be evaluated, EVLIS saves up a frame, ​so that execution can be resumed at EVLIS1 when the evaluation of the component returns with a value. It then sets up **EXP** to point to the component to be evaluated and dispatches to AEVAL . ( DEFUN EVLIS () ( COND (( NULL **UNEVLIS** ) ( SETQ **EVLIS** ( REVERSE **EVLIS** )) ( COND (( ATOM ( CAR **EVLIS** )) ( SETQ **VAL** ( APPLY ( CAR **EVLIS** ) ( CDR **EVLIS** ))) ( RESTORE )) (( EQ ( CAAR **EVLIS** ) 'LAMBDA ) ( SETQ **ENV** ( PAIRLIS ( CADAR **EVLIS** ) ( CDR **EVLIS** ) **ENV** ) **EXP** ( CADDAR **EVLIS** ) **PC** 'AEVAL )) (( EQ ( CAAR **EVLIS** ) 'BETA ) ( SETQ **ENV** ( PAIRLIS ( CADR ( CADAR **EVLIS** )) ( CDR **EVLIS** ) ( CADDAR **EVLIS** )) **EXP** ( CADDR ( CADAR **EVLIS** )) **PC** 'AEVAL )) (( EQ ( CAAR **EVLIS** ) 'DELTA ) ( SETQ **CLINK** ( CADAR **EVLIS** )) ( RESTORE )) ( T ( ERROR '|BAD FUNCTION - EVARGLIST| **EXP** 'FAIL-ACT )))) ( T ( SAVEUP 'EVLIS1 ) ( SETQ **EXP** ( CAR **UNEVLIS** ) **PC** 'AEVAL )))) The purpose of EVLIS1 is to gobble up the value, passed in the **VAL** register, of the subexpression just evaluated. It saves this value on the list in the **EVLIS** register, pops off the unevaluated subexpression from the **UNEVLIS** register, and dispatches back to EVLIS . ( DEFUN EVLIS1 () ( SETQ **EVLIS** ( CONS **VAL** **EVLIS** ) **UNEVLIS** ( CDR **UNEVLIS** ) **PC** 'EVLIS )) Here is the code for the various AINT s. On arrival at the instruction for an AINT , **EXP** contains the expression whose functional position contains the name of the AINT . None of the arguments have been evaluated, and no new control frame has been created. Note that each AINT is defined by the presence of an AINT property on the property list of the LISP atom which is its name. The value of this property is the LISP function which is the first "instruction" of the AINT . EVALUATE is similar to the LISP function EVAL ; it evaluates its argument, which should result in a s-expression, which is then fed back into the SCHEME expression evaluator ( AEVAL ). ​ Warning: template has been deprecated. ( DEFPROP EVALUATE EVALUATE AINT ) ( DEFUN EVALUATE () ( SAVEUP 'EVALUATE1 ) ( SETQ **EXP** ( CADR **EXP** ) **PC** 'AEVAL )) ( DEFUN EVALUATE1 () ( SETQ **EXP** **VAL** **PC** 'AEVAL )) IF evaluates its first argument, with a return address of IF1 . IF1 examines the resulting **VAL** , and gives either the second or third argument to AEVAL depending on whether the **VAL** was non- NIL or NIL . ( DEFPROP IF IF AINT ) ( DEFUN IF () ( SAVEUP 'IF1 ) ( SETQ **EXP** ( CADR **EXP** ) **PC** 'AEVAL )) ( DEFUN IF1 () ( COND ( **VAL** ( SETQ **EXP** ( CADDR **EXP** ))) ( T ( SETQ **EXP** ( CADDDR **EXP** )))) ( SETQ **PC** 'AEVAL )) As it was in the beginning, is now, and ever shall be: QUOTE without end. (Amen, amen.) ( DEFPROP QUOTE AQUOTE AINT ) ( DEFUN AQUOTE () ( SETQ **VAL** ( CADR **EXP** )) ( RESTORE )) LABELS merely feeds its second argument to AEVAL after constructing a fiendishly clever environment structure. This is done in two stages: first the skeleton of the structure is created, with null environments in the closures of the bound functions; next the created environment is clobbered into each of the closures. ​ Warning: template has been deprecated. ( DEFPROP LABELS LABELS AINT ) ( DEFUN LABELS () ( SETQ **TEM** ( MAPCAR ' ( LAMBDA ( DEF ) ( LIST ( CAR DEF ) ( LIST 'BETA ( CADR DEF ) NIL ))) ( CADR **EXP** ))) ( MAPC ' ( LAMBDA ( VC ) ( RPLACA ( CDDADR VC ) **TEM** )) **TEM** ) ( SETQ **ENV** ( NCONC **TEM** **ENV** ) **EXP** ( CADDR **EXP** ) **PC** 'AEVAL )) We now come to the multiprocess primitives. CREATE!PROCESS temporarily creates a new set of machine registers (by the lambda-binding mechanism of the host language), establishes the new process in those registers, swaps it out, and returns the new process id; returning causes the old machine registers to be restored. ( DEFUN CREATE!PROCESS ( EXP ) (( LAMBDA ( **PROCESS** **EXP** **ENV** **UNEVLIS** **EVLIS** **PC** **CLINK** **VAL** ) ( SWAPOUTPROCESS ) **PROCESS** ) ( GENSYM ) EXP **ENV** NIL NIL 'AEVAL ( LIST NIL NIL NIL NIL 'TERMINATE NIL ) NIL )) ( DEFUN START!PROCESS ( P ) ( COND (( OR ( NOT ( ATOM P )) ( NOT ( GET P 'CLINK ))) ( ERROR '|BAD PROCESS -- START!PROCESS| **EXP** 'FAIL-ACT ))) ( OR ( EQ P **PROCESS** ) ( MEMQ P **QUEUE** ) ( SETQ **QUEUE** ( NCONC **QUEUE** ( LIST P )))) P ) ( DEFUN STOP!PROCESS ( P ) ( COND (( MEMQ P **QUEUE** ) ( SETQ **QUEUE** ( DELQ P **QUEUE** ))) (( EQ P **PROCESS** ) ( TERMINATE ))) P ) TERMINATE is an internal microcode routine which terminates the current process. If the current process is the only one, then all processes have been stopped, and so a new SCHEME top level is created; otherwise TERMINATE pulls the next process off the scheduler queue and swaps it in. Note that we cannot use SWAPINPROCESS because a RESTORE will happen in EVLIS as soon as TERMINATE completes (this is a very deep global property of the interpreter, and a fine ​source of bugs; much care is required). ( DEFUN TERMINATE () ( COND (( NULL **QUEUE** ) ( SETQ **PROCESS** ( CREATE!PROCESS ' ( **TOP** '|SCHEME -- QUEUEOUT| )))) ( T ( SETQ **PROCESS** ( CAR **QUEUE** ) **QUEUE** ( CDR **QUEUE** )))) ( SETQ **CLINK** ( GET **PROCESS** 'CLINK )) ( SETQ **VAL** ( GET **PROCESS** 'VAL )) 'TERMINATE-VALUE ) EVALUATE!UNINTERRUPTIBLY merely binds the variable *ALLOW* to NIL , and then evaluates its argument. This is why this primitive follows the scoping rules for variables! ( DEFPROP EVALUATE!UNINTERRUPTIBLY EVALUATE!UNINTERRUPTIBLY AINT ) ( DEFUN EVALUATE!UNINTERRUPTIBLY () ( SETQ **ENV** ( CONS ( LIST '*ALLOW* NIL ) **ENV** ) **EXP** ( CADR **EXP** ) **PC** 'AEVAL )) DEFINE closes the function to be defined in the null environment, and installs the closure in the LISP value cell. ( DEFPROP DEFINE DEFINE AINT ) ( DEFUN DEFINE () ( SET ( CADR **EXP** ) ( LIST 'BETA ( CADDR **EXP** ) NIL )) ( SETQ **VAL** ( CADR **EXP** )) ( RESTORE )) ASET looks up the specified variable in the current environment, and clobbers the value cell in the environment with the new value. If the variable is not bound in the current environment, the LISP value cell is set. Note that ASET does not need to be an AINT , since it does not fool with order of evaluation; all it needs is access to the "machine register" **ENV** . ( DEFUN ASET ( VAR VALU ) ( SETQ **TEM** ( ASSQ VAR **ENV** )) ( COND ( **TEM** ( RPLACA ( CDR **TEM** ) VALU )) ( T ( SET VAR VALU ))) VALU ) CATCH binds the tag variable to a DELTA expression which contains the current CLINK . When AEVAL applies such an expression as a function (of one argument), it makes the **CLINK** in the DELTA expression be the **CLINK** , places the value of the argument in **VAL** , and does a RESTORE . The effect is to return from the CATCH expression with the argument to the DELTA ​expression as its value (can you see why?). ( DEFPROP CATCH ACATCH AINT ) ( DEFUN ACATCH () ( SETQ **ENV** ( CONS ( LIST ( CADR **EXP** ) ( LIST 'DELTA **CLINK** )) **ENV** ) **EXP** ( CADDR **EXP** ) **PC** 'AEVAL )) PAIRLIS is as in the LISP 1.5 Programmer's Manual [McCarthy][10]. ( DEFUN PAIRLIS ( X Y Z ) ( DO (( I X ( CDR I )) ( J Y ( CDR J )) ( L Z ( CONS ( LIST ( CAR I ) ( CAR J )) L ))) (( AND ( NULL I ) ( NULL J )) L ) ( AND ( OR ( NULL I ) ( NULL J )) ( ERROR '|WRONG NUMBER OF ARGUMENTS - PAIRLIS| **EXP** 'WRNG-NO-ARGS )))) AMACRO s are fairly complicated beasties, and have very little to do with the basic issues of the implementation of SCHEME per se, so the code for them will not be given here. AMACRO s behave almost exactly like MacLISP macros [Moon][4]. This is the end of the SCHEME interpreter! ​ Warning: template has been deprecated. Acknowledgements [ edit ] This paper would not have happened if Sussman had not been forced to think about lambda calculus by having to teach 6.031, nor would it have happened had not Steele been forced to understand PLASMA by morbid curiosity. This work developed out of an initial attempt to understand the actorness of actors. Steele thought he understood it, but couldn't explain it; Sussman suggested the experimental approach of actually building an "ACTORS interpreter". This interpreter attempted to intermix the use of actors and LISP lambda expressions in a clean manner. When it was completed, we discovered that the "actors" and the lambda expressions were identical in implementation. Once we had discovered this, all the rest fell into place, and it was only natural to begin thinking about actors in terms of lambda calculus. The original interpreter was call-by-name for various reasons having to do with 6.031; we subsequently experimentally discovered how call-by-name screws iteration, and rewrote it to use call-by-value. Note well that we did not bring forth a clean implementation in one brilliant flash of understanding; we used an experimental and highly empirical approach to bootstrap our knowledge. We wish to thank the staff of 6.031, Mike Dertouzos, and Steve Ward, for precipitating this intellectual adventure. ​ Warning: template has been deprecated. Bibliography [ edit ] [Bobrow and Wegbreit]

Bobrow, Daniel G. and Wegbreit, Ben. "A Model and Stack Implementation of Multiple Environments." CACM 16, 10 (October 1973) pp. 591-603. [Church]

Church, Alonzo. The Calculi of Lambda Conversion. Annals of Mathematics Studies Number 6. Princeton University Press (Princeton, 1941). Reprinted by Klaus Reprint Corp. (New York, 1965). [Dijkstra]

Dijkstra, Edsger W. "Solution of a Problem in Concurrent Programming Control." CACM 8,9 (September 1965) p. 569. [Fischer]

Fischer, Michael J. "Lambda Calculus Schemata." Proceedings of ACM Conference on Proving Assertions about Programs. SIGPLAN Notices (January 1972). [Galley and Pfister]

Galley, S.W. and Pfister, Greg. The MDL Language. Programming Technology Division Document SYS.11.01. Project MAC, MIT (Cambridge, November 1975). [Greif]

Greif, Irene. Semantics of Communicating Parallel Processes. Ph.D. thesis. MAC-TR-154, Project MAC, MIT (Cambridge, September 1975). [Greif and Hewitt]

Greif, Irene and Hewitt, Carl. Actor Semantics of Planner-73. Working Paper 81, MIT AI Lab (Cambridge, 1975). [Ingerman]

Ingerman, P. Z. "Thunks -- A Way of Compiling Procedure Statements with Some Comments on Procedure Declarations." CACM 4,1 (January 1961) pp. 55-58. [Knuth]

Knuth, Donald E. "Additional Comments on a Problem in Concurrent Programming Control." CACM 9,5 (May 1966) pp. 321-322. [Lamport]

Lamport, Leslie. "A New Solution of Dijkstra's Concurrent Programming Problem." CACM 17,8 (August 1974) pp. 453-455. [MAC]

Project MAC Progress Report XI (July 1973 - July 1974). Project MAC, MIT (Cambridge, 1974). ​ Warning: template has been deprecated. [McCarthy]

McCarthy, John, et al. LISP 1.5 Programmer's Manual. The MIT Press (Cambridge, 1965). [McDermott and Sussman]

McDermott, Drew V. and Sussman, Gerald Jay. The CONNIVER Reference Manual. AI Memo 295a. MIT AI Lab (Cambridge, January 1974). [Moon]

Moon, David A. MACLISP Reference Manual, Revision 0. Project MAC, MIT (Cambridge, April 1974). [Moses]

Moses, Joel. The Function of FUNCTION in LISP. AI Memo 199, MIT AI Lab (Cambridge, June 1970). [Reynolds]

Reynolds, John C. "Definitional Interpreters for Higher Order Programming Languages." ACM Conference Proceedings 1972. [Smith and Hewitt]

Smith, Brian C. and Hewitt, Carl. A PLASMA Primer (draft). MIT AI Lab (Cambridge, October 1975). [Sussman]

Sussman, Gerald Jay, Winograd, Terry, and Charniak, Eugene. Micro-PLANNER Reference Manual. AI Memo 203A. MIT AI Lab (Cambridge, December 1971). . Project MAC, MIT (Cambridge, 1974). We wish to thank the staff of 6.031, Mike Dertouzos, and Steve Ward, for precipitating this intellectual adventure. Carl Hewitt spent many hours explaining the innards and outards of PLASMA to Steele over the course of several months; Marilyn McClennan was also helpful in this respect. Brian Smith and Richard Zippel helped a lot. We wish to thank Seymour Papert , Ben Kuipers, Marvin Minsky , and Vaughn Pratt for their excellent suggestions. This is the end of the SCHEME interpreter! merely feeds its second argument toafter constructing a fiendishly clever environment structure. This is done in two stages: first the skeleton of the structure is created, with null environments in the closures of the bound functions; next the created environment is clobbered into each of the closures. is similar to the LISP function; it evaluates its argument, which should result in a s-expression, which is then fed back into the SCHEME expression evaluator (). restores a computation from the. The use ofis a kludge to optimize the compilation of the "microcode".

Since our value cells effectively solve the readers and writers problem (i.e. reads and writes of variables are indivisible) no more than our side effect primitive is necessary to synchronize our processes [Dijkstra] [Knuth] [Lamport]. However, the techniques for achieving synchronization using onlyare quite cumbersome and opaque. It behooves the implementor to make things easier for the user by introducing a more tractable synchronization primitive (e.g. P+V or monitors or path expressions or ...). Machine language programmers have long known that the easiest way to synchronize processes is to turn off the scheduling clock during the execution of critical code. We have introduced such a primitive,, (which is a sort of "over-anxious serializer", because it locks out the whole world) into our interpreter.