-
Notifications
You must be signed in to change notification settings - Fork 228
/
meta-interp.ss
269 lines (221 loc) · 7.87 KB
/
meta-interp.ss
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
;; a meta-circular interpreter (reflection tower) is an
;; interpreter which can interpret itself to interpret
;; itself to interpret itself ...
;; This version saves indentation by defining 'cond'.
;; author: Yin Wang ([email protected])
(define Y
'(lambda (f)
((lambda (u) (u u))
(lambda (x) (f (lambda (t) ((x x) t)))))))
(define interp-text
`(,Y
(lambda (interp)
(lambda (exp)
(lambda (env)
(lambda (k)
(cond
[(number? exp) (k exp)]
[(boolean? exp) (k exp)]
[(string? exp) (k exp)]
[(symbol? exp) (k (env exp))]
[(eq? 'cond (car exp))
((((,Y (lambda (loop)
(lambda (cls)
(lambda (env)
(lambda (k)
(((interp (car (car cls))) env)
(lambda (t)
(if t
(((interp (car (cdr (car cls)))) env) k)
(((loop (cdr cls)) env) k)))))))))
(cdr exp)) env) k)]
[(eq? 'eq? (car exp))
(((interp (car (cdr exp))) env)
(lambda (v1)
(((interp (car (cdr (cdr exp)))) env)
(lambda (v2) (k (eq? v1 v2))))))]
[(eq? '= (car exp))
(((interp (car (cdr exp))) env)
(lambda (v1)
(((interp (car (cdr (cdr exp)))) env)
(lambda (v2) (k (= v1 v2))))))]
[(eq? '* (car exp))
(((interp (car (cdr exp))) env)
(lambda (v1)
(((interp (car (cdr (cdr exp)))) env)
(lambda (v2) (k (* v1 v2))))))]
[(eq? 'cons (car exp))
(((interp (car (cdr exp))) env)
(lambda (v1)
(((interp (car (cdr (cdr exp)))) env)
(lambda (v2) (k (cons v1 v2))))))]
[(eq? 'quote (car exp)) (k (car (cdr exp)))]
[(eq? 'sub1 (car exp))
(((interp (car (cdr exp))) env) (lambda (v) (k (sub1 v))))]
[(eq? 'number? (car exp))
(((interp (car (cdr exp))) env) (lambda (v) (k (number? v))))]
[(eq? 'boolean? (car exp))
(((interp (car (cdr exp))) env) (lambda (v) (k (boolean? v))))]
[(eq? 'string? (car exp))
(((interp (car (cdr exp))) env) (lambda (v) (k (string? v))))]
[(eq? 'symbol? (car exp))
(((interp (car (cdr exp))) env) (lambda (v) (k (symbol? v))))]
[(eq? 'zero? (car exp))
(((interp (car (cdr exp))) env) (lambda (v) (k (zero? v))))]
[(eq? 'null? (car exp))
(((interp (car (cdr exp))) env) (lambda (v) (k (null? v))))]
[(eq? 'car (car exp))
(((interp (car (cdr exp))) env) (lambda (v) (k (car v))))]
[(eq? 'cdr (car exp))
(((interp (car (cdr exp))) env) (lambda (v) (k (cdr v))))]
[(eq? 'if (car exp))
(((interp (car (cdr exp))) env)
(lambda (t)
(if t
(((interp (car (cdr (cdr exp)))) env) k)
(((interp (car (cdr (cdr (cdr exp))))) env) k))))]
[(eq? 'lambda (car exp))
(k (lambda (a)
(lambda (k)
(((interp (car (cdr (cdr exp))))
(lambda (x^)
(if (eq? x^ (car (car (cdr exp)))) a (env x^))))
k))))]
[(eq? 'rho (car exp))
(k (lambda (a)
(lambda (k)
(((interp (car (cdr (cdr exp))))
(lambda (x^)
(cond
[(eq? x^ (car (cdr (cdr (car (cdr exp))))))
(lambda (a) (lambda (k^) (k a)))]
[(eq? x^ (car (cdr (car (cdr exp))))) env]
[(eq? x^ (car (car (cdr exp)))) a]
[#t (env x^)])))
k))))]
[#t
(((interp (car exp)) env)
(lambda (v1)
(((interp (car (cdr exp))) env)
(lambda (v2)
((v1 v2) k)))))])))))))
;;;;;;;;; nested evaluators ;;;;;;;;;;
; level 0 is eval, our base evaluator
(define interp0 eval)
; level 1 uses eval to interpret an interpreter text together with the
; input program
(define interp1
(lambda (e)
(eval `(((,interp-text (quote ,e)) (lambda (x) x)) (lambda (v) v)))))
; level 2 uses interp1 to interpret an interpreter text together with the
; input program
(define interp2
(lambda (e)
(interp1 `(((,interp-text (quote ,e)) (lambda (x) x)) (lambda (v) v)))))
; and so on ...
(define interp3
(lambda (e)
(interp2 `(((,interp-text (quote ,e)) (lambda (x) x)) (lambda (v) v)))))
; We can extract the above pattern into a general nesting facility, which
; takes a text of interpreter and a number n, and generates an interpreter
; nested to level n.
(define nest-interp
(lambda (interp n)
(cond
[(zero? n) eval]
[else
(lambda (e)
((nest-interp interp (sub1 n))
`(((,interp (quote ,e)) (lambda (x) x)) (lambda (v) v))))])))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; tests ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(define-syntax test
(syntax-rules ()
((_ title tested-expression expected-result)
(let* ((expected expected-result)
(produced tested-expression))
(if (equal? expected produced)
(printf "~s works!\n" title)
(error
'test
"Failed ~s: ~a\nExpected: ~a\nComputed: ~a"
title 'tested-expression expected produced))))))
;;;;;;;;;; fact 5 ;;;;;;;;;;;
(define fact5
`((,Y
(lambda (fac)
(lambda (n)
(if (zero? n) 1 (* n (fac (sub1 n)))))))
5))
(test "fact5 - Level 0"
((nest-interp interp-text 0) fact5)
120)
(test "fact5 - Level 1"
((nest-interp interp-text 1) fact5)
120)
(test "fact5 - Level 2"
((nest-interp interp-text 2) fact5)
120)
(test "fact5 - Level 3"
((nest-interp interp-text 3) fact5)
120)
(time ((nest-interp interp-text 1) fact5))
;; cpu time: 15 real time: 9 gc time: 0
(time ((nest-interp interp-text 2) fact5))
;; cpu time: 15 real time: 12 gc time: 0
(time ((nest-interp interp-text 3) fact5))
;; cpu time: 156 real time: 157 gc time: 16
(time ((nest-interp interp-text 4) fact5))
;; cpu time: 11107 real time: 11706 gc time: 1401
;;;;;;;;; member-test ;;;;;;;;;;
(define member-test
`(((,Y
(lambda (member?)
(lambda (a)
(lambda (lat)
(if
(null? lat) #f
(if (eq? a (car lat)) #t
((member? a) (cdr lat))))))))
'a) '(b a c)))
(test "member-test - Level 0"
((nest-interp interp-text 0) member-test)
#t)
(test "member-test - Level 1"
((nest-interp interp-text 1) member-test)
#t)
(test "member-test - Level 2"
((nest-interp interp-text 2) member-test)
#t)
(test "member-test - Level 3"
((nest-interp interp-text 3) member-test)
#t)
;;;;;;;;;;;; rho-test ;;;;;;;;;;;;;;
(define rho-test '(* 2 ((rho (x e k) (* 3 (k 4))) 5)))
(test "rho-test - Level 1"
((nest-interp interp-text 1) rho-test)
8)
(test "rho-test - Level 2"
((nest-interp interp-text 2) rho-test)
8)
(test "rho-test - Level 3"
((nest-interp interp-text 3) rho-test)
8)
;;;;;;;;;;;; prod-test-rho ;;;;;;;;;;;;;
(define prod-test-rho
`((,Y
(rho (prod _ __)
(rho (ls _ k)
(cond
[(null? ls) 1]
[(zero? (car ls)) (k 0)]
[else (* (car ls) (prod (cdr ls)))]))))
'(1 2 3 0 5 6)))
(test "prod-test-rho - Level 1"
((nest-interp interp-text 1) prod-test-rho)
0)
(test "prod-test-rho - Level 2"
((nest-interp interp-text 2) prod-test-rho)
0)
(test "prod-test-rho - Level 3"
((nest-interp interp-text 3) prod-test-rho)
0)