-
Notifications
You must be signed in to change notification settings - Fork 0
/
20.scm
367 lines (301 loc) · 8.14 KB
/
20.scm
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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
;;; -*- buffer-read-only:t -*-
;; (load "19.scm")
;;; HELPERS
(define (m MSG expr)
(define DEBUG 0)
(or (zero? DEBUG)
(begin (display " ")
(display "**") (display MSG) (display "**")
(display "[") (display expr) (display "]")
(display ";"))))
(define (add1 x)
(+ x 1))
(define (sub1 x)
(- x 1))
(define (times x y)
(* x y))
(define (atom? x)
(and
(not (pair? x))
(not (null? x))))
;;; EVALUATOR
(define lookup
(lambda (table name)
(table name)))
(define extend
(lambda (name1 value table)
(lambda (name2)
(cond ((eq? name1 name2) value)
(else (table name2))))))
(define define?
(lambda (e)
(cond ((atom? e) false)
((atom? (car e)) (eq? (car e) 'define))
(else false))))
(define *define
(lambda (e)
(newline)
(begin
(display "DEFINE")
(display "...")
(display (name-of e))
(display "...")
(display (right-side-of e)))
(newline)
(set! global-table
(extend (name-of e)
(box (the-meaning (right-side-of e)))
global-table))))
(define box
(lambda (it)
(lambda (sel)
(sel it (lambda (new)
(set! it new))))))
(define setbox
(lambda (box new)
(box (lambda (it set)
(set new)))))
(define unbox
(lambda (box)
(box (lambda (it set) it))))
(define the-meaning
(lambda (e)
(m "THE-MEANING" e)
(meaning e lookup-in-global-table)))
(define lookup-in-global-table
(lambda (name)
(lookup global-table name)))
(define meaning
(lambda (e table)
(m "MEANING" e)
((expression-to-action e)
e table)))
(define *quote
(lambda (e table)
(m "QUOTE" e)
(text-of e)))
(define *identifier
(lambda (e table)
(unbox (lookup table e))))
(define *set
(lambda (e table)
(setbox (lookup table (name-of e))
(meaning (right-of e) table))))
(define *lambda
(lambda (e table)
(lambda (args)
(beglis (body-of e)
(multi-extend (formals-of e)
(box-all args)
table)))))
(define beglis
(lambda (es table)
(cond ((null? (cdr es)) (meaning (car es) table))
(else ((lambda (val) (beglis (cdr es) table))
(meaning (car es) table))))))
(define box-all
(lambda (vals)
(cond ((null? vals) '())
(else (cons (box (car vals))
(box-all (cdr vals)))))))
(define multi-extend
(lambda (names values table)
(cond ((null? names) table)
(else (extend (car names) (car values)
(multi-extend (cdr names) (cdr values)
table))))))
(define *application
(lambda (e table)
(m "APPLICATION" e)
((meaning (function-of e) table)
(evlist (arguments-of e) table))))
(define evlist
(lambda (args table)
(cond ((null? args) '())
(else ((lambda (val) (cons val
(evlist (cdr args) table)))
(meaning (car args) table))))))
(define :car
(lambda (args-in-a-list)
(car (car args-in-a-list))))
(define primitive1
(lambda (p)
(lambda (args-in-a-list)
(m "PRIMITIVE1" args-in-a-list)
(p (car args-in-a-list)))))
(define primitive2
(lambda (p)
(lambda (args-in-a-list)
(p (car args-in-a-list)
(car (cdr args-in-a-list))))))
(define *const
(let ((:cons (primitive2 cons))
(:car (primitive1 car))
(:cdr (primitive1 cdr))
(:null? (primitive1 null?))
(:eq? (primitive2 eq?))
(:atom? (primitive1 atom?))
(:number? (primitive1 number?))
(:zero? (primitive1 zero?))
(:add1 (primitive1 add1))
(:sub1 (primitive1 sub1))
(:times (primitive2 times)))
(lambda (e table)
(m "CONST" e)
(cond ((number? e) e)
((eq? e true) true)
((eq? e false) false)
((eq? e 'true) true)
((eq? e 'false) false)
((eq? e 'cons) :cons)
((eq? e 'car) :car)
((eq? e 'cdr) :cdr)
((eq? e 'null?) :null?)
((eq? e 'eq?) :eq?)
((eq? e 'atom?) :atom?)
((eq? e 'zero?) :zero?)
((eq? e 'add1) :add1)
((eq? e 'sub1) :sub1)
((eq? e 'times) :times)
((eq? e 'number?) :number?)))))
(define *cond
(lambda (e table)
(evcon (cond-lines-of e)
table)))
(define evcon
(lambda (lines table)
(cond ((else? (question-of (car lines)))
(meaning (answer-of (car lines)) table))
((meaning (question-of (car lines)) table)
(meaning (answer-of (car lines)) table))
(else (evcon (cdr lines) table)))))
(define *letcc
(lambda (e table)
(call-with-current-continuation
(lambda (skip)
(beglis (ccbody-of e)
(extend (name-of e)
(box (primitive1 skip))
table))))))
(define abort)
(define value
(lambda (e)
(call-with-current-continuation
(lambda (the-end)
(set! abort the-end)
(newline)
(display "COMPUTE VALUE OF ==> ")
(display e)
(cond ((define? e) (*define e))
(else (the-meaning e)))))))
(define the-empty-table
(lambda (name)
(abort (cons 'no-answer
(cons name '())))))
(define global-table the-empty-table)
(define expression-to-action
(lambda (e)
(m "EXPRESSION->ACTION" e)
(cond ((atom? e) (atom-to-action e))
(else (list-to-action e)))))
(define atom-to-action
(lambda (e)
(m "ATOM->ACTION" e)
(cond ((number? e) *const)
((eq? e true) *const)
((eq? e false) *const)
((eq? e 'true) *const)
((eq? e 'false) *const)
((eq? e 'cons) *const)
((eq? e 'car) *const)
((eq? e 'cdr) *const)
((eq? e 'null?) *const)
((eq? e 'eq?) *const)
((eq? e 'atom?) *const)
((eq? e 'zero?) *const)
((eq? e 'times) *const)
((eq? e 'add1) *const)
((eq? e 'sub1) *const)
((eq? e 'number?) *const)
(else *identifier))))
(define list-to-action
(lambda (e)
(m "LIST->ACTION" e)
(cond ((atom? (car e))
(cond ((eq? (car e) 'quote) *quote)
((eq? (car e) 'lambda) *lambda)
((eq? (car e) 'letcc) *letcc)
((eq? (car e) 'cond) *cond)
(else *application)))
(else *application))))
(define right-side-of
(lambda (x)
(cond ((null? (cdr (cdr x))) 0)
(else (car (cdr (cdr x)))))))
(define text-of
(lambda (x)
(car (cdr x))))
(define formals-of
(lambda (x)
(car (cdr x))))
(define body-of
(lambda (x)
(cdr (cdr x))))
(define ccbody-of
(lambda (x)
(cdr (cdr x))))
(define name-of
(lambda (x)
(car (cdr x))))
(define right-side-of
(lambda (x)
(cond ((null? (cdr (cdr x))) 0)
(else (car (cdr (cdr x)))))))
(define cond-lines-of
(lambda (x)
(cdr x)))
(define else?
(lambda (x)
(cond ((atom? x) (eq? x 'else))
(else false))))
(define question-of
(lambda (x)
(car x)))
(define answer-of
(lambda (x)
(car (cdr x))))
(define function-of
(lambda (x)
(m "FUNCTION OF" x)
(car x)))
(define arguments-of
(lambda (x)
(m "ARGUMENTS OF" x)
(cdr x)))
"** tests ** $~$~$~$~$~$~$~$~$~$~$~$~$~$~$~$~$~$~$~$~$~$~$~$"
(value 1)
(value '(define x 11))
(value 'x)
(value '(define odd?
(lambda (n)
(cond ((zero? n) false)
(else (even? (sub1 n)))))))
(value '(define even?
(lambda (n)
(cond ((zero? n) true)
(else (odd? (sub1 n)))))))
(map value '((odd? 0) (odd? 1) (even? 0) (even? 1)))
(value '(define 0? zero?))
(map value '((zero? 20) (zero? 0) (0? 1) (0? 0)))
(value 'variable-not-found)
(value ''ok)
(value '(define 1+ (lambda (n) (add1 n))))
(value '(add1 11))
(value '(1+ 11))
(value '(define factorial
(lambda (n)
(cond ((zero? n) 1)
(else (times n (factorial (sub1 n))))))))
(value '(factorial 3))
(value '(factorial 20))
'ok