-
Notifications
You must be signed in to change notification settings - Fork 8
/
full-interp.scm
447 lines (406 loc) · 11.8 KB
/
full-interp.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
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
;; The definition of 'letrec' is based based on Dan Friedman's code,
;; using the "half-closure" approach from Reynold's definitional
;; interpreters.
(define (evalo expr val)
(eval-expo expr initial-env val))
(define (eval-expo expr env val)
(conde
((== `(quote ,val) expr)
(absento 'closure val)
(absento 'prim val)
(not-in-envo 'quote env))
((numbero expr) (== expr val))
((symbolo expr) (lookupo expr env val))
((fresh (x body)
(== `(lambda ,x ,body) expr)
(== `(closure (lambda ,x ,body) ,env) val)
(conde
;; Variadic
((symbolo x))
;; Multi-argument
((list-of-symbolso x)))
(not-in-envo 'lambda env)))
((fresh (rator x rands body env^ a* res)
(== `(,rator . ,rands) expr)
;; variadic
(symbolo x)
(== `((,x . (val . ,a*)) . ,env^) res)
(eval-expo rator env `(closure (lambda ,x ,body) ,env^))
(eval-expo body res val)
(eval-listo rands env a*)))
((fresh (rator x* rands body env^ a* res)
(== `(,rator . ,rands) expr)
;; Multi-argument
(eval-expo rator env `(closure (lambda ,x* ,body) ,env^))
(eval-listo rands env a*)
(ext-env*o x* a* env^ res)
(eval-expo body res val)))
((fresh (rator x* rands a* prim-id)
(== `(,rator . ,rands) expr)
(eval-expo rator env `(prim . ,prim-id))
(eval-primo prim-id a* val)
(eval-listo rands env a*)))
((handle-matcho expr env val))
((fresh (p-name x body letrec-body)
;; single-function variadic letrec version
(== `(letrec ((,p-name (lambda ,x ,body)))
,letrec-body)
expr)
(conde
; Variadic
((symbolo x))
; Multiple argument
((list-of-symbolso x)))
(not-in-envo 'letrec env)
(eval-expo letrec-body
`((,p-name . (rec . (lambda ,x ,body))) . ,env)
val)))
((prim-expo expr env val))
))
(define empty-env '())
(define (lookupo x env t)
(fresh (y b rest)
(== `((,y . ,b) . ,rest) env)
(conde
((== x y)
(conde
((== `(val . ,t) b))
((fresh (lam-expr)
(== `(rec . ,lam-expr) b)
(== `(closure ,lam-expr ,env) t)))))
((=/= x y)
(lookupo x rest t)))))
(define (not-in-envo x env)
(conde
((== empty-env env))
((fresh (y b rest)
(== `((,y . ,b) . ,rest) env)
(=/= y x)
(not-in-envo x rest)))))
(define (eval-listo expr env val)
(conde
((== '() expr)
(== '() val))
((fresh (a d v-a v-d)
(== `(,a . ,d) expr)
(== `(,v-a . ,v-d) val)
(eval-expo a env v-a)
(eval-listo d env v-d)))))
;; need to make sure lambdas are well formed.
;; grammar constraints would be useful here!!!
(define (list-of-symbolso los)
(conde
((== '() los))
((fresh (a d)
(== `(,a . ,d) los)
(symbolo a)
(list-of-symbolso d)))))
(define (ext-env*o x* a* env out)
(conde
((== '() x*) (== '() a*) (== env out))
((fresh (x a dx* da* env2)
(== `(,x . ,dx*) x*)
(== `(,a . ,da*) a*)
(== `((,x . (val . ,a)) . ,env) env2)
(symbolo x)
(ext-env*o dx* da* env2 out)))))
(define (eval-primo prim-id a* val)
(conde
[(== prim-id 'cons)
(fresh (a d)
(== `(,a ,d) a*)
(== `(,a . ,d) val))]
[(== prim-id 'car)
(fresh (d)
(== `((,val . ,d)) a*)
(=/= 'closure val))]
[(== prim-id 'cdr)
(fresh (a)
(== `((,a . ,val)) a*)
(=/= 'closure a))]
[(== prim-id 'not)
(fresh (b)
(== `(,b) a*)
(conde
((=/= #f b) (== #f val))
((== #f b) (== #t val))))]
[(== prim-id 'equal?)
(fresh (v1 v2)
(== `(,v1 ,v2) a*)
(conde
((== v1 v2) (== #t val))
((=/= v1 v2) (== #f val))))]
;; FIXME (webyrd) -- symbol?, and perhaps other type predicates, doesn't handle booleans (fails)
[(== prim-id 'symbol?)
(fresh (v)
(== `(,v) a*)
(conde
((symbolo v) (== #t val))
((numbero v) (== #f val))
((fresh (a d)
(== `(,a . ,d) v)
(== #f val)))))]
[(== prim-id 'null?)
(fresh (v)
(== `(,v) a*)
(conde
((== '() v) (== #t val))
((=/= '() v) (== #f val))))]
[(conde
[(== prim-id '+)]
[(== prim-id '-)]
[(== prim-id '*)]
[(== prim-id '/)])
(fresh (a1 a2)
(== `(,a1 ,a2) a*)
;; we could use list-of-numbero instead
;; but it causes more divergence with run*
;; (list-of-numbero a*)
(numbero a1)
(numbero a2)
(numbero val)
(conde
((== prim-id '/)
(z/assert `(not (= ,a2 0))))
((=/= prim-id '/)))
(z/assert `(= ,val (,prim-id ,a1 ,a2))))]
[(conde
[(== prim-id '=)]
[(== prim-id '>)]
[(== prim-id '>=)]
[(== prim-id '<)]
[(== prim-id '<=)])
(fresh (a1 a2)
(== `(,a1 ,a2) a*)
;; we could use list-of-numbero instead
;; but it causes more divergence with run*
;; (list-of-numbero a*)
(numbero a1)
(numbero a2)
(conde
[(z/assert `(,prim-id ,a1 ,a2)) (== #t val)]
[(z/assert `(not (,prim-id ,a1 ,a2))) (== #f val)]))]
))
(define (prim-expo expr env val)
(conde
((boolean-primo expr env val))
((and-primo expr env val))
((or-primo expr env val))
((if-primo expr env val))))
(define (boolean-primo expr env val)
(conde
((== #t expr) (== #t val))
((== #f expr) (== #f val))))
(define (and-primo expr env val)
(fresh (e*)
(== `(and . ,e*) expr)
(not-in-envo 'and env)
(ando e* env val)))
(define (ando e* env val)
(conde
((== '() e*) (== #t val))
((fresh (e)
(== `(,e) e*)
(eval-expo e env val)))
((fresh (e1 e2 e-rest v)
(== `(,e1 ,e2 . ,e-rest) e*)
(conde
((== #f v)
(== #f val)
(eval-expo e1 env v))
((=/= #f v)
(eval-expo e1 env v)
(ando `(,e2 . ,e-rest) env val)))))))
(define (or-primo expr env val)
(fresh (e*)
(== `(or . ,e*) expr)
(not-in-envo 'or env)
(oro e* env val)))
(define (oro e* env val)
(conde
((== '() e*) (== #f val))
((fresh (e)
(== `(,e) e*)
(eval-expo e env val)))
((fresh (e1 e2 e-rest v)
(== `(,e1 ,e2 . ,e-rest) e*)
(conde
((=/= #f v)
(== v val)
(eval-expo e1 env v))
((== #f v)
(eval-expo e1 env v)
(oro `(,e2 . ,e-rest) env val)))))))
(define (if-primo expr env val)
(fresh (e1 e2 e3 t)
(== `(if ,e1 ,e2 ,e3) expr)
(not-in-envo 'if env)
(eval-expo e1 env t)
(conde
((=/= #f t) (eval-expo e2 env val))
((== #f t) (eval-expo e3 env val)))))
(define initial-env `((list . (val . (closure (lambda x x) ,empty-env)))
(not . (val . (prim . not)))
(equal? . (val . (prim . equal?)))
(symbol? . (val . (prim . symbol?)))
(cons . (val . (prim . cons)))
(null? . (val . (prim . null?)))
(car . (val . (prim . car)))
(cdr . (val . (prim . cdr)))
(+ . (val . (prim . +)))
(- . (val . (prim . -)))
(* . (val . (prim . *)))
(/ . (val . (prim . /)))
(= . (val . (prim . =)))
(> . (val . (prim . >)))
(>= . (val . (prim . >=)))
(< . (val . (prim . <)))
(<= . (val . (prim . <=)))
. ,empty-env))
(define handle-matcho
(lambda (expr env val)
(fresh (against-expr mval clause clauses)
(== `(match ,against-expr ,clause . ,clauses) expr)
(not-in-envo 'match env)
(eval-expo against-expr env mval)
(match-clauses mval `(,clause . ,clauses) env val))))
(define (not-symbolo t)
(conde
((== #f t))
((== #t t))
((== '() t))
((numbero t))
((fresh (a d)
(== `(,a . ,d) t)))))
(define (not-numbero t)
(conde
((== #f t))
((== #t t))
((== '() t))
((symbolo t))
((fresh (a d)
(== `(,a . ,d) t)))))
(define (self-eval-literalo t)
(conde
((numbero t))
((booleano t))))
(define (literalo t)
(conde
((numbero t))
((symbolo t) (=/= 'closure t))
((booleano t))
((== '() t))))
(define (booleano t)
(conde
((== #f t))
((== #t t))))
(define (regular-env-appendo env1 env2 env-out)
(conde
((== empty-env env1) (== env2 env-out))
((fresh (y v rest res)
(== `((,y . (val . ,v)) . ,rest) env1)
(== `((,y . (val . ,v)) . ,res) env-out)
(regular-env-appendo rest env2 res)))))
(define (match-clauses mval clauses env val)
(fresh (p result-expr d penv)
(== `((,p ,result-expr) . ,d) clauses)
(conde
((fresh (env^)
(p-match p mval '() penv)
(regular-env-appendo penv env env^)
(eval-expo result-expr env^ val)))
((p-no-match p mval '() penv)
(match-clauses mval d env val)))))
(define (var-p-match var mval penv penv-out)
(fresh (val)
(symbolo var)
(=/= 'closure mval)
(conde
((== mval val)
(== penv penv-out)
(lookupo var penv val))
((== `((,var . (val . ,mval)) . ,penv) penv-out)
(not-in-envo var penv)))))
(define (var-p-no-match var mval penv penv-out)
(fresh (val)
(symbolo var)
(=/= mval val)
(== penv penv-out)
(lookupo var penv val)))
(define (p-match p mval penv penv-out)
(conde
((self-eval-literalo p)
(== p mval)
(== penv penv-out))
((var-p-match p mval penv penv-out))
((fresh (var pred val)
(== `(? ,pred ,var) p)
(conde
((== 'symbol? pred)
(symbolo mval))
((== 'number? pred)
(numbero mval)))
(var-p-match var mval penv penv-out)))
((fresh (quasi-p)
(== (list 'quasiquote quasi-p) p)
(quasi-p-match quasi-p mval penv penv-out)))))
(define (p-no-match p mval penv penv-out)
(conde
((self-eval-literalo p)
(=/= p mval)
(== penv penv-out))
((var-p-no-match p mval penv penv-out))
((fresh (var pred val)
(== `(? ,pred ,var) p)
(== penv penv-out)
(symbolo var)
(conde
((== 'symbol? pred)
(conde
((not-symbolo mval))
((symbolo mval)
(var-p-no-match var mval penv penv-out))))
((== 'number? pred)
(conde
((not-numbero mval))
((numbero mval)
(var-p-no-match var mval penv penv-out)))))))
((fresh (quasi-p)
(== (list 'quasiquote quasi-p) p)
(quasi-p-no-match quasi-p mval penv penv-out)))))
(define (quasi-p-match quasi-p mval penv penv-out)
(conde
((== quasi-p mval)
(== penv penv-out)
(literalo quasi-p))
((fresh (p)
(== (list 'unquote p) quasi-p)
(p-match p mval penv penv-out)))
((fresh (a d v1 v2 penv^)
(== `(,a . ,d) quasi-p)
(== `(,v1 . ,v2) mval)
(=/= 'unquote a)
(quasi-p-match a v1 penv penv^)
(quasi-p-match d v2 penv^ penv-out)))))
(define (quasi-p-no-match quasi-p mval penv penv-out)
(conde
((=/= quasi-p mval)
(== penv penv-out)
(literalo quasi-p))
((fresh (p)
(== (list 'unquote p) quasi-p)
(=/= 'closure mval)
(p-no-match p mval penv penv-out)))
((fresh (a d)
(== `(,a . ,d) quasi-p)
(=/= 'unquote a)
(== penv penv-out)
(literalo mval)))
((fresh (a d v1 v2 penv^)
(== `(,a . ,d) quasi-p)
(=/= 'unquote a)
(== `(,v1 . ,v2) mval)
(conde
((quasi-p-no-match a v1 penv penv^))
((quasi-p-match a v1 penv penv^)
(quasi-p-no-match d v2 penv^ penv-out)))))))