-
Notifications
You must be signed in to change notification settings - Fork 16
/
unstaged-interp.scm
477 lines (424 loc) · 12.3 KB
/
unstaged-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
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
#lang racket/base
(provide
evalo-unstaged
u-eval-expo)
(require "main.rkt")
(defrel (absent-tago v)
(absento 'struct v))
(defrel (not-tago v)
(fresh () ;; the fresh is for tests-peano-fib to mysteriously not overfit
(=/= 'struct v)))
(defrel (booleano t)
(conde
((== #f t))
((== #t t))))
(defrel (pos-tago v)
(== v 'struct))
(defrel-partial (u-eval-apply rep [x* body env] [a* val])
(fresh (env^)
(conde
((symbolo x*)
(== `((,x* . (val . ,a*)) . ,env) env^))
((u-ext-env*o x* a* env env^)))
(u-eval-expo body env^ val)))
(defrel-partial (u-eval-apply-rec rep [f x* e env] [a* res])
(fresh (env^ env-self)
(== env-self `((,f . (val . (struct rec-closure ,rep))) . ,env))
(conde
((symbolo x*)
(== env^ `((,x* . (val . ,a*)) . ,env-self)))
((u-ext-env*o x* a* env-self env^)))
(u-eval-expo e env^ res)))
(defrel (u-evalo expr val)
(u-eval-expo expr u-initial-env val))
(defrel (u-handle-appo rator rands env val)
(fresh (a* cfun rep proc prim-id)
(u-eval-expo rator env cfun)
(conde
((== `(struct prim . ,prim-id) cfun)
(u-eval-primo prim-id a* val)
(u-eval-listo rands env a*))
((== `(struct closure ,rep) cfun)
(u-eval-listo rands env a*)
(u-callo cfun val a*))
((== `(struct rec-closure ,rep) cfun)
(u-eval-listo rands env a*)
(u-callo cfun val a*)))))
(defrel (u-callo proc val a*)
(conde
((fresh (rep)
(== proc `(struct closure ,rep))
(finish-apply rep u-eval-apply a* val)))
((fresh (rep)
(== proc `(struct rec-closure ,rep))
(finish-apply rep u-eval-apply-rec a* val)))
((fresh (prim-id)
(== proc `(struct prim . ,prim-id))
(u-eval-primo prim-id a* val)))))
(defrel (u-eval-expo expr env val)
(conde
((== `(quote ,val) expr)
(absent-tago val)
(u-not-in-envo 'quote env))
((numbero expr) (== expr val))
((symbolo expr) (u-lookupo expr env val))
((fresh (rator rands)
(== `(,rator . ,rands) expr)
(u-handle-appo rator rands env val)))
((fresh (rep x body)
(== `(lambda ,x ,body) expr)
(== `(struct closure ,rep) val)
(conde
;; Variadic
((symbolo x))
;; Multi-argument
((u-list-of-symbolso x)))
(u-not-in-envo 'lambda env)
(partial-apply rep u-eval-apply x body env)
))
((u-handle-matcho expr env val))
((fresh (letrec-body f x e rep)
(== `(letrec ((,f (lambda ,x ,e)))
,letrec-body)
expr)
(u-not-in-envo 'letrec env)
(partial-apply rep u-eval-apply-rec f x e env)
(u-eval-expo letrec-body
`((,f . (val . (struct rec-closure ,rep))) . ,env)
val)))
((u-prim-expo expr env val))
))
(define u-empty-env '())
(defrel (u-lookupo x env t)
(fresh (y b rest)
(== `((,y . ,b) . ,rest) env)
(conde
((== x y)
(== `(val . ,t) b))
((=/= x y)
(u-lookupo x rest t)))))
(defrel (u-not-in-envo x env)
(conde
((== u-empty-env env))
((fresh (y b rest)
(== `((,y . ,b) . ,rest) env)
(=/= y x)
(u-not-in-envo x rest)))))
(defrel (u-eval-listo expr env val)
(conde
((== '() expr)
(== '() val))
((fresh (a d v-a v-d)
(== `(,a . ,d) expr)
(== `(,v-a . ,v-d) val)
(u-eval-expo a env v-a)
(u-eval-listo d env v-d)))))
;; need to make sure lambdas are well formed.
;; grammar constraints would be useful here!!!
(defrel (u-list-of-symbolso los)
(conde
((== '() los))
((fresh (a d)
(== `(,a . ,d) los)
(symbolo a)
(u-list-of-symbolso d)))))
(defrel (u-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)
(u-ext-env*o dx* da* env2 out)))))
(defrel (u-eval-primo prim-id a* val)
(conde
[(== prim-id 'list)
(== a* val)]
[(== prim-id 'cons)
(fresh (a d)
(== `(,a ,d) a*)
(== `(,a . ,d) val))]
[(== prim-id 'car)
(fresh (d)
(== `((,val . ,d)) a*)
(not-tago val))]
[(== prim-id 'cdr)
(fresh (a)
(== `((,a . ,val)) a*)
(not-tago 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))))]
[(== prim-id 'symbol?)
(fresh (v)
(== `(,v) a*)
(conde
((symbolo v) (== #t val))
((numbero v) (== #f val))
((fresh (a d)
(== `(,a . ,d) v)
(== #f val)))
((booleano v) (== #f val))))]
[(== prim-id 'number?)
(fresh (v)
(== `(,v) a*)
(conde
((numbero v) (== #t val))
((symbolo v) (== #f val))
((fresh (a d)
(== `(,a . ,d) v)
(== #f val)))
((booleano v) (== #f val))))]
[(== prim-id 'pair?)
(fresh (v)
(== `(,v) a*)
(conde
((symbolo v) (== #f val))
((numbero v) (== #f val))
((booleano v) (== #f val))
((fresh (a d)
(== `(,a . ,d) v)
(== #t val)
(not-tago a)))
((fresh (a d)
(== `(,a . ,d) v)
(== #f val)
(pos-tago a)))))]
[(== prim-id 'null?)
(fresh (v)
(== `(,v) a*)
(conde
((== '() v) (== #t val))
((=/= '() v) (== #f val))))]))
(defrel (u-prim-expo expr env val)
(conde
((u-boolean-primo expr env val))
((u-and-primo expr env val))
((u-or-primo expr env val))
((u-if-primo expr env val))))
(defrel (u-boolean-primo expr env val)
(conde
((== #t expr) (== #t val))
((== #f expr) (== #f val))))
(defrel (u-and-primo expr env val)
(fresh (e*)
(== `(and . ,e*) expr)
(u-not-in-envo 'and env)
(u-ando e* env val)))
(defrel (u-ando e* env val)
(conde
((== '() e*) (== #t val))
((fresh (e)
(== `(,e) e*)
(u-eval-expo e env val)))
((fresh (e1 e2 e-rest v)
(== `(,e1 ,e2 . ,e-rest) e*)
(conde
((== #f v)
(== #f val)
(u-eval-expo e1 env v))
((=/= #f v)
(u-eval-expo e1 env v)
(u-ando `(,e2 . ,e-rest) env val)))))))
(defrel (u-or-primo expr env val)
(fresh (e*)
(== `(or . ,e*) expr)
(u-not-in-envo 'or env)
(u-oro e* env val)))
(defrel (u-oro e* env val)
(conde
((== '() e*) (== #f val))
((fresh (e)
(== `(,e) e*)
(u-eval-expo e env val)))
((fresh (e1 e2 e-rest v)
(== `(,e1 ,e2 . ,e-rest) e*)
(conde
((=/= #f v)
(== v val)
(u-eval-expo e1 env v))
((== #f v)
(u-eval-expo e1 env v)
(u-oro `(,e2 . ,e-rest) env val)))))))
(defrel (u-if-primo expr env val)
(fresh (e1 e2 e3 t)
(== `(if ,e1 ,e2 ,e3) expr)
(u-not-in-envo 'if env)
(u-eval-expo e1 env t)
(conde
((=/= #f t) (u-eval-expo e2 env val))
((== #f t) (u-eval-expo e3 env val)))))
(define u-initial-env `((list . (val . (struct prim . list)))
(not . (val . (struct prim . not)))
(equal? . (val . (struct prim . equal?)))
(symbol? . (val . (struct prim . symbol?)))
(number? . (val . (struct prim . number?)))
(pair? . (val . (struct prim . pair?)))
(cons . (val . (struct prim . cons)))
(null? . (val . (struct prim . null?)))
(car . (val . (struct prim . car)))
(cdr . (val . (struct prim . cdr)))
. ,u-empty-env))
(defrel (u-handle-matcho expr env val)
(fresh (against-expr mval clause clauses)
(== `(match ,against-expr ,clause . ,clauses) expr)
(u-not-in-envo 'match env)
(u-eval-expo against-expr env mval)
(u-match-clauses mval `(,clause . ,clauses) env val)))
(defrel (u-not-symbolo t)
(conde
((== #f t))
((== #t t))
((== '() t))
((numbero t))
((fresh (a d)
(== `(,a . ,d) t)))))
(defrel (u-not-numbero t)
(conde
((== #f t))
((== #t t))
((== '() t))
((symbolo t))
((fresh (a d)
(== `(,a . ,d) t)))))
(defrel (u-self-eval-literalo t)
(conde
((numbero t))
((u-booleano t))))
(defrel (u-literalo t)
(conde
((numbero t))
((symbolo t) (fresh () (not-tago t)))
((u-booleano t))
((== '() t))))
(defrel (u-booleano t)
(conde
((== #f t))
((== #t t))))
(defrel (u-regular-env-appendo env1 env2 env-out)
(conde
((== u-empty-env env1) (== env2 env-out))
((fresh (y v rest res)
(== `((,y . (val . ,v)) . ,rest) env1)
(== `((,y . (val . ,v)) . ,res) env-out)
(u-regular-env-appendo rest env2 res)))))
(defrel (u-match-clauses mval clauses env val)
(fresh (p result-expr d penv)
(== `((,p ,result-expr) . ,d) clauses)
(conde
((fresh (env^)
(u-p-match p mval '() penv)
(u-regular-env-appendo penv env env^)
(u-eval-expo result-expr env^ val)))
((u-p-no-match p mval '() penv)
(u-match-clauses mval d env val)))))
(defrel (u-var-p-match var mval penv penv-out)
(fresh ()
(symbolo var)
(not-tago mval)
(u-var-p-match-extend var mval penv penv-out)))
(defrel (u-var-p-match-extend var val penv penv-out)
(conde
((== penv penv-out)
(u-lookupo var penv val))
((== `((,var . (val . ,val)) . ,penv) penv-out)
(u-not-in-envo var penv))))
(defrel (u-var-p-no-match var mval penv penv-out)
(fresh (val)
(symbolo var)
(=/= mval val)
(== penv penv-out)
(u-lookupo var penv val)))
(defrel (u-p-match p mval penv penv-out)
(conde
((u-self-eval-literalo p)
(== p mval)
(== penv penv-out))
((u-var-p-match p mval penv penv-out))
((fresh (var pred)
(== `(? ,pred ,var) p)
(u-pred-match pred mval)
(u-var-p-match var mval penv penv-out)))
((fresh (quasi-p)
(== (list 'quasiquote quasi-p) p)
(u-quasi-p-match quasi-p mval penv penv-out)))))
(defrel (u-pred-match pred mval)
(conde
((== 'symbol? pred)
(symbolo mval))
((== 'number? pred)
(numbero mval))))
(defrel (u-p-no-match p mval penv penv-out)
(conde
((u-self-eval-literalo p)
(=/= p mval)
(== penv penv-out))
((u-var-p-no-match p mval penv penv-out))
((fresh (var pred)
(== `(? ,pred ,var) p)
(== penv penv-out)
(symbolo var)
(u-pred-no-match pred var mval penv penv-out)))
((fresh (quasi-p)
(== (list 'quasiquote quasi-p) p)
(u-quasi-p-no-match quasi-p mval penv penv-out)))))
(defrel (u-pred-no-match pred var mval penv penv-out)
(conde
((== 'symbol? pred)
(conde
((u-not-symbolo mval))
((symbolo mval)
(u-var-p-no-match var mval penv penv-out))))
((== 'number? pred)
(conde
((u-not-numbero mval))
((numbero mval)
(u-var-p-no-match var mval penv penv-out))))))
(defrel (u-quasi-p-match quasi-p mval penv penv-out)
(conde
((== quasi-p mval)
(== penv penv-out)
(u-literalo quasi-p))
((fresh (p)
(== (list 'unquote p) quasi-p)
(u-p-match p mval penv penv-out)))
((fresh (a d v1 v2 penv^)
(== `(,a . ,d) quasi-p)
(== `(,v1 . ,v2) mval)
(=/= 'unquote a)
(u-quasi-p-match a v1 penv penv^)
(u-quasi-p-match d v2 penv^ penv-out)))))
(defrel (u-quasi-p-no-match quasi-p mval penv penv-out)
(conde
((=/= quasi-p mval)
(== penv penv-out)
(u-literalo quasi-p))
((fresh (p)
(== (list 'unquote p) quasi-p)
(not-tago mval)
(u-p-no-match p mval penv penv-out)))
((fresh (a d)
(== `(,a . ,d) quasi-p)
(=/= 'unquote a)
(== penv penv-out)
(u-literalo mval)))
((fresh (a d v1 v2 penv^)
(== `(,a . ,d) quasi-p)
(=/= 'unquote a)
(== `(,v1 . ,v2) mval)
(conde
((u-quasi-p-no-match a v1 penv penv-out))
((u-quasi-p-match a v1 penv penv^)
(u-quasi-p-no-match d v2 penv^ penv-out)))))))
(defrel (evalo-unstaged expr val)
(u-eval-expo expr u-initial-env val))