1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
|
;; Simple evaluator for Scheme without DEFINE, using substitution model.
;; Version 1: No DEFINE, only primitive names are global.
;; The "read-eval-print loop" (REPL):
(define (scheme-1)
(display "Scheme-1: ")
(flush)
(print (eval-1 (read)))
(scheme-1))
;; Two important procedures:
;; EVAL-1 takes an expression and returns its value.
;; APPLY-1 takes a procedure and a list of actual argument values, and
;; calls the procedure.
;; They have these names to avoid conflict with STk's EVAL and APPLY,
;; which have similar meanings.
;; Comments on EVAL-1:
;; There are four basic expression types in Scheme:
;; 1. self-evaluating (a/k/a constant) expressions: numbers, #t, etc.
;; 2. symbols (variables)
;; 3. special forms (in this evaluator, just QUOTE, IF, and LAMBDA)
;; 4. procedure calls (can call a primitive or a LAMBDA-generated procedure)
;; 1. The value of a constant is itself. Unlike real Scheme, an STk
;; procedure is here considered a constant expression. You can't type in
;; procedure values, but the value of a global variable can be a procedure,
;; and that value might get substituted for a parameter in the body of a
;; higher-order function such as MAP, so the evaluator has to be ready to
;; see a built-in procedure as an "expression." Therefore, the procedure
;; CONSTANT? includes a check for (PROCEDURE? EXP).
;; 2. In the substitution model, we should never actually evaluate a *local*
;; variable name, because we should have substituted the actual value for
;; the parameter name before evaluating the procedure body.
;; In this simple evaluator, there is no DEFINE, and so the only *global*
;; symbols are the ones representing primitive procedures. We cheat a little
;; by using STk's EVAL to get the values of these variables.
;; 3. The value of the expression (QUOTE FOO) is FOO -- the second element of
;; the expression.
;; To evaluate the expression (IF A B C) we first evaluate A; then, if A is
;; true, we evaluate B; if A is false, we evaluate C.
;; The value of a LAMBDA expression is the expression itself. There is no
;; work to do until we actually call the procedure. (This won't be true
;; when we write a more realistic interpreter that handles more Scheme
;; features, but it works in the substitution model.)
;; 4. To evaluate a procedure call, we recursively evaluate all the
;; subexpressions. We call APPLY-1 to handle the actual procedure invocation.
(define (eval-1 exp)
(cond ((constant? exp) exp)
((symbol? exp) (eval exp)) ; use underlying Scheme's EVAL
((quote-exp? exp) (cadr exp))
((if-exp? exp)
(if (eval-1 (cadr exp))
(eval-1 (caddr exp))
(eval-1 (cadddr exp))))
((lambda-exp? exp) exp)
((define-exp? exp)
(eval (list 'define (cadr exp) (maybe-quote (eval-1 (caddr exp))))))
((pair? exp) (apply-1 (eval-1 (car exp)) ; eval the operator
(map eval-1 (cdr exp))))
(else (error "bad expr: " exp))))
;; Comments on APPLY-1:
;; There are two kinds of procedures: primitive and LAMBDA-created.
;; We recognize a primitive procedure using the PROCEDURE? predicate in
;; the underlying STk interpreter.
;; If the procedure isn't primitive, then it must be LAMBDA-created.
;; In this interpreter (but not in later, more realistic ones), the value
;; of a LAMBDA expression is the expression itself. So (CADR PROC) is
;; the formal parameter list, and (CADDR PROC) is the expression in the
;; procedure body.
;; To call the procedure, we must substitute the actual arguments for
;; the formal parameters in the body; the result of this substitution is
;; an expression which we can then evaluate with EVAL-1.
(define (apply-1 proc args)
(cond ((procedure? proc) ; use underlying Scheme's APPLY
(apply proc args))
((lambda-exp? proc)
(eval-1 (substitute (caddr proc) ; the body
(cadr proc) ; the formal parameters
args ; the actual arguments
'()))) ; bound-vars, see below
(else (error "bad proc: " proc))))
;; Some trivial helper procedures:
(define (constant? exp)
(or (number? exp) (boolean? exp) (string? exp) (procedure? exp)))
(define (exp-checker type)
(lambda (exp) (and (pair? exp) (eq? (car exp) type))))
(define quote-exp? (exp-checker 'quote))
(define if-exp? (exp-checker 'if))
(define lambda-exp? (exp-checker 'lambda))
(define define-exp? (exp-checker 'define))
;; SUBSTITUTE substitutes actual arguments for *free* references to the
;; corresponding formal parameters. For example, given the expression
;;
;; ((lambda (x y)
;; ((lambda (x) (+ x y))
;; (* x y)))
;; 5 8)
;;
;; the body of the procedure we're calling is
;;
;; ((lambda (x) (+ x y))
;; (* x y))
;;
;; and we want to substitute 5 for X and 8 for Y, but the result should be
;;
;; ((lambda (x) (+ x 8))
;; (* 5 8))
;;
;; and *NOT*
;;
;; ((lambda (5) (+ 5 8))
;; (* 5 8))
;;
;; The X in (* X Y) is a "free reference," but the X in (LAMBDA (X) (+ X Y))
;; is a "bound reference."
;;
;; To make this work, in its recursive calls, SUBSTITUTE keeps a list of
;; bound variables in the current subexpression -- ones that shouldn't be
;; substituted for -- in its argument BOUND. This argument is the empty
;; list in the top-level call to SUBSTITUTE from APPLY-1.
;; Another complication is that when an argument value isn't a self-evaluating
;; expression, we actually want to substitute the value *quoted*. For example,
;; consider the expression
;;
;; ((lambda (x) (first x)) 'foo)
;;
;; The actual argument value is FOO, but we want the result of the
;; substitution to be
;;
;; (first 'foo)
;;
;; and not
;;
;; (first foo)
;;
;; because what we're going to do with this expression is try to evaluate
;; it, and FOO would be an unbound variable.
;; There is a strangeness in MAYBE-QUOTE, which must handle the
;; case of a primitive procedure as the actual argument value; these
;; procedures shouldn't be quoted.
(define (substitute exp params args bound)
(cond ((constant? exp) exp)
((symbol? exp)
(if (memq exp bound)
exp
(lookup exp params args)))
((quote-exp? exp) exp)
((lambda-exp? exp)
(list 'lambda
(cadr exp)
(substitute (caddr exp) params args (append bound (cadr exp)))))
(else (map (lambda (subexp) (substitute subexp params args bound))
exp))))
(define (lookup name params args)
(cond ((null? params) name)
((eq? name (car params)) (maybe-quote (car args)))
(else (lookup name (cdr params) (cdr args)))))
(define (maybe-quote value)
(cond ((lambda-exp? value) value)
((constant? value) value)
((procedure? value) value) ; real Scheme primitive procedure
(else (list 'quote value))))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Sample evaluation, computing factorial of 5:
; Scheme-1: ((lambda (n)
; ((lambda (f) (f f n))
; (lambda (f n)
; (if (= n 0)
; 1
; (* n (f f (- n 1))) )) ))
; 5)
; 120
;; Sample evaluation, using a primitive as argument to MAP:
; Scheme-1: ((lambda (f n)
; ((lambda (map) (map map f n))
; (lambda (map f n)
; (if (null? n)
; '()
; (cons (f (car n)) (map map f (cdr n))) )) ))
; first
; '(the rain in spain))
; (t r i s)
|