Scheme: An Interpreter for Extended Lambda Calculus/Section 1
Section 1: The SCHEME Reference ManualEdit
SCHEME is essentially a full-funarg LISP.
LAMBDA expressions need not be
*FUNCTIONed when passed as arguments or returned as values; they will evaluate to closures of themselves.
All LISP functions (i.e.,
LSUBRs, but not
MACROs) are primitive operators in SCHEME, and have the same meaning as they have in LISP. Like
LAMBDA expressions, primitive operators and numbers are self-evaluating (they evaluate to trivial closures of themselves).
There are a number of special primitives known as
AINTs which are to SCHEME as
FSUBRs are to LISP. We will enumerate them here.
This is the primitive conditional operator. It takes three arguments. If the first evaluates to non-
NIL, it evaluates the second expression, and otherwise the third.
As in LISP, this quotes the argument form so that it will be passed verbatim as data. The abbreviation "
'FOO" may be used instead of "
This is analogous to the MacLISP
DEFUNprimitive (but note that the
LAMBDAmust appear explicitly!). It is used for defining a function in the "global environment" permanently, as opposed to
LABELS(see below), which is used for temporary definitions in a local environment.
DEFINEtakes a name and a lambda expression; it closes the lambda expression in the global environment and stores the closure in the LISP value cell of the name (which is a LISP atom).
We have decided not to use the traditional
LABELprimitive in this interpreter because it is difficult to define several mutually recursive functions using only
LABEL. The solution, which Hewitt [Smith and Hewitt] also uses, is to adopt an ALGOLesque block syntax:
(LABELS <function definition list> <expression>)
This has the effect of evaluating the expression in an environment where all the functions are defined as specified by the definitions list. Furthermore, the functions are themselves closed in that environment, and not in the outer environment; this allows the functions to call themselves and each other recursively. For example, consider a function which counts all the atoms in a list structure recursively to all levels, but which doesn't count the
NILs which terminate lists (but
NILs in the
CARof some list count). In order to perform this we use two mutually recursive functions, one to count the
carand one to count the
cdr, as follows:
(DEFINE COUNT (LAMBDA (L) (LABELS ((COUNTCAR (LAMBDA (L) (IF (ATOM L) 1 (+ (COUNTCAR (CAR L)) (COUNTCDR (CDR L)))))) (COUNTCDR (LAMBDA (L) (IF (ATOM L) (IF (NULL L) 0 1) (+ (COUNTCAR (CAR L)) (COUNTCDR (CDR L))))))) (COUNTCDR L)))) ;Note: COUNTCDR is defined here.
This is the side effect primitive. It is analogous to the LISP function
SET. For example, to define a cell [Smith and Hewitt], we may use
(DEFINE CONS-CELL (LAMBDA (CONTENTS) (LABELS ((THE-CELL (LAMBDA (MSG) (IF (EQ MSG 'CONTENTS?) CONTENTS (IF (EQ MSG 'CELL?) 'YES (IF (EQ (CAR MSG) '<-) (BLOCK (ASET 'CONTENTS (CADR MSG)) THE-CELL) (ERROR '|UNRECOGNIZED MESSAGE - CELL| MSG 'WRNG-TYPE-ARG))))))) THE-CELL)))
Those of you who may complain about the lack of
ASETQare invited to write
(ASET' foo bar)instead of
(ASET 'foo bar).
This is similar to the LISP function
EVAL. It evaluates its argument, and then evaluates the resulting s-expression as SCHEME code.
This is the "escape operator" which gives the user a handle on the control structure of the interpreter. The expression:
(CATCH <identifier> <expression>)
<expression>in an environment where
<identifier>is bound to a continuation which is "just about to return from the
CATCH"; that is, if the continuation is called as a function of one argument, then control proceeds as if the
CATCHexpression had returned with the supplied (evaluated) argument as its value. For example, consider the following obscure definition of
SQRT(Sussman's favorite style/Steele's least favorite):
(DEFINE SQRT (LAMBDA (X EPSILON) ((LAMBDA (ANS LOOPTAG) (CATCH RETURNTAG (PROGN (ASET 'LOOPTAG (CATCH M M)) ;CREATE PROG TAG (IF (< (ABS (-$ (*$ ANS ANS) X)) EPSILON) (RETURNTAG ANS) ;RETURN NIL) ;JFCL (ASET 'ANS (//$ (+$ (//$ X ANS) ANS) 2.0)) (LOOPTAG LOOPTAG)))) ;GOTO 1.0 NIL)))
Anyone who doesn't understand how this manages to work probably should not attempt to use
As another example, we can define a
THROWfunction, which may then be used with
CATCHmuch as they are in LISP:
(DEFINE THROW (LAMBDA (TAG RESULT) (TAG RESULT)))
This is the process generator for multiprocessing. It takes one argument, an expression to be evaluated in the current environment as a separate parallel process. If the expression ever returns a value, the process automatically terminates. The value of
CREATE!PROCESSis a process id for the newly generated process. Note that the newly created process will not actually run until it is explicitly started.
- This takes one argument, a process id, and starts up that process. It then runs.
This also takes a process id, but stops the process. The stopped process may be continued from where it was stopped by using
START!PROCESSagain on it. The magic global variable
**PROCESS**always contains the process id of the currently running process; thus a process can stop itself by doing
(STOP!PROCESS **PROCESS**). A stopped process is garbage collected if no live process has a pointer to its process id.
- This is the synchronization primitive. It evaluates an expression uninterruptibly; i.e. no other process may run until the expression has returned a value. Note that if a
funargis returned from the scope of an
EVALUATE!UNINTERRUPTIBLY, then that
funargwill be uninterruptible when it is applied; that is, the uninterruptibility property follows the rules of variable scoping. For example, consider the following function:
(DEFINE SEMGEN (LAMBDA (SEMVAL) (LIST (LAMBDA () (EVALUATE!UNINTERRUPTIBLY (ASET' SEMVAL (+ SEMVAL 1)))) (LABELS (P (LAMBDA () (EVALUATE!UNINTERRUPTIBLY (IF (PLUSP SEMVAL) (ASET' SEMVAL (- SEMVAL 1)) (P))))) P))))
This returns a pair of functions which are V and P operations on a newly created semaphore. The argument to
SEMGENis the initial value for the semaphore. Note that P busy-waits by iterating if necessary; because
EVALUATE!UNINTERRUPTIBLYuses variable-scoping rules, other processes have a chance to get in at the beginning of each iteration. This busy-wait can be made much more efficient by replacing the expression
(P)in the definition of
((LAMBDA (ME) (BLOCK (START!PROCESS (CREATE!PROCESS '(START!PROCESS ME))) (STOP!PROCESS ME) (P))) **PROCESS**)
Let's see you figure this one out! Note that a
EVALUATE!UNINTERRUPTIBLYforces the process to be swapped out even if it is the current one, and so other processes get to run; but as soon as it gets swapped in again, others are locked out as before.
AINTs, SCHEME has a class of primitives known as
AMACROs. These are similar to MacLISP
MACROs, in that they are expanded into equivalent code before being executed. Some
AMACROs supplied with the SCHEME interpreter:
This is like the MacLISP
CONDstatement, except that singleton clauses (where the result of the predicate is the returned value) are not allowed.
- These are also as in MacLISP.
This is like the MacLISP
PROGN, but arranges to evaluate its last argument without an extra net control frame (explained later), so that the last argument may involved in an iteration. Note that in SCHEME, unlike MacLISP, the body of a
LAMBDAexpression is not an implicit
This is like the MacLISP "new-style"
DOis not supported.
These are like
MAPLIST, but they expect a SCHEME lambda closure for the first argument.
To use SCHEME, simply incant at DDT (on MIT-AI):
which will load up the current version of SCHEME, which will announce itself and give a prompt. If you want to escape to LISP, merely hit
^G. To restart SCHEME, type
(SCHEME). Sometimes one does need to use a LISP
FSUBR such as
UREAD; this may be accomplished by typing, for example,
(EVAL' (UREAD FOO BAR DSK LOSER))
After doing this, typing
^Q will, of course, cause SCHEME to read from the file.
This concludes the SCHEME Reference Manual.