-
Notifications
You must be signed in to change notification settings - Fork 3
/
appendixA.scm
221 lines (201 loc) · 6.21 KB
/
appendixA.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
(load "appendixC.scm")
(load "appendixB.scm")
(load "interp-helpers.scm")
(define eval-expo
(lambda (exp env val)
(conde
((fresh (v)
(== `(quote ,v) exp)
(not-in-envo 'quote env)
(absento 'closure v)
(absento 'int-val v)
(== v val)))
((fresh (a*)
(== `(list . ,a*) exp)
(not-in-envo 'list env)
(absento 'closure a*)
(absento 'int-val a*)
(proper-listo a* env val)))
((prim-expo exp env val))
((symbolo exp) (lookupo exp env val))
((fresh (rator x* rands body env^ a* res)
(== `(,rator . ,rands) exp)
(eval-expo rator env `(closure ,x* ,body ,env^))
(proper-listo rands env a*)
(ext-env*o x* a* env^ res)
(eval-expo body res val)))
((fresh (x* body)
(== `(lambda ,x* ,body) exp)
(not-in-envo 'lambda env)
(== `(closure ,x* ,body ,env) val))))))
(define ext-env*o
(lambda (x* a* env out)
(conde
((== '() x*) (== '() a*) (== env out))
((fresh (x a dx* da* env2)
(== `(,x . ,dx*) x*)
(== `(,a . ,da*) a*)
(== `((,x . ,a) . ,env) env2)
(ext-env*o dx* da* env2 out))))))
(define prim-expo
(lambda (exp env val)
(conde
((boolean-primo exp env val))
((number-primo exp env val))
((sub1-primo exp env val))
((zero?-primo exp env val))
((*-primo exp env val))
((cons-primo exp env val))
((car-primo exp env val))
((cdr-primo exp env val))
((not-primo exp env val))
((if-primo exp env val)))))
(define boolean-primo
(lambda (exp env val)
(conde
((== #t exp) (== #t val))
((== #f exp) (== #f val)))))
(define cons-primo
(lambda (exp env val)
(fresh (a d v-a v-d)
(== `(cons ,a ,d) exp)
(== `(,v-a . ,v-d) val)
(absento 'closure val)
(absento 'int-val val)
(not-in-envo 'cons env)
(eval-expo a env v-a)
(eval-expo d env v-d))))
(define car-primo
(lambda (exp env val)
(fresh (p d)
(== `(car ,p) exp)
(=/= 'int-val val)
(=/= 'closure val)
(not-in-envo 'car env)
(eval-expo p env `(,val . ,d)))))
(define cdr-primo
(lambda (exp env val)
(fresh (p a)
(== `(cdr ,p) exp)
(=/= 'int-val a)
(=/= 'closure a)
(not-in-envo 'cdr env)
(eval-expo p env `(,a . ,val)))))
(define not-primo
(lambda (exp env val)
(fresh (e b)
(== `(not ,e) exp)
(conde
((== #t b) (== #f val))
((== #f b) (== #t val)))
(not-in-envo 'not env)
(eval-expo e env b))))
(define number-primo
(lambda (exp env val)
(fresh (n)
(== `(int-exp ,n) exp)
(== `(int-val ,n) val)
(not-in-envo 'int-exp env))))
(define sub1-primo
(lambda (exp env val)
(fresh (e n n-1)
(== `(sub1 ,e) exp)
(== `(int-val ,n-1) val)
(not-in-envo 'sub1 env)
(eval-expo e env `(int-val ,n))
(minuso n '(1) n-1))))
(define zero?-primo
(lambda (exp env val)
(fresh (e n)
(== `(zero? ,e) exp)
(conde
((zeroo n) (== #t val))
((poso n) (== #f val)))
(not-in-envo 'zero? env)
(eval-expo e env `(int-val ,n)))))
(define *-primo
(lambda (exp env val)
(fresh (e1 e2 n1 n2 n3)
(== `(* ,e1 ,e2) exp)
(== `(int-val ,n3) val)
(not-in-envo '* env)
(eval-expo e1 env `(int-val ,n1))
(eval-expo e2 env `(int-val ,n2))
(*o n1 n2 n3))))
(define if-primo
(lambda (exp env val)
(fresh (e1 e2 e3 t)
(== `(if ,e1 ,e2 ,e3) exp)
(not-in-envo 'if env)
(eval-expo e1 env t)
(conde
((== #t t) (eval-expo e2 env val))
((== #f t) (eval-expo e3 env val))))))
(test-check "appA-1"
(run 12 (q) (eval-expo q '() `(int-val ,(build-num 6))))
'((int-exp (0 1 1)) ((lambda () (int-exp (0 1 1)))) (sub1 (int-exp (1 1 1)))
(((lambda (_.0) (int-exp (0 1 1))) '_.1)
(=/= ((_.0 int-exp)))
(absent closure _.1)
(absent int-val _.1))
(* (int-exp (1)) (int-exp (0 1 1))) (* (int-exp (0 1 1)) (int-exp (1)))
(* (int-exp (0 1)) (int-exp (1 1)))
(((lambda (_.0) (int-exp (0 1 1))) (list)) (=/= ((_.0 int-exp))))
(car (list (int-exp (0 1 1))))
((lambda () ((lambda () (int-exp (0 1 1))))))
(sub1 ((lambda () (int-exp (1 1 1)))))
((lambda () (sub1 (int-exp (1 1 1)))))))
(test-check "appA-2"
(car (reverse (run 7 (q) (eval-expo q '() `(int-val ,(build-num 6))))))
'(* (int-exp (0 1)) (int-exp (1 1))))
(test-check "appA-3"
(length (run 500 (q) (eval-expo q '() `(int-val ,(build-num 6)))))
500)
(test-check "appA-4"
(car (reverse (run 270 (q) (eval-expo q '() `(int-val ,(build-num 6))))))
'(sub1 (sub1 (sub1 (int-exp (1 0 0 1))))))
(define rel-fact5
`((lambda (f)
((f f) (int-exp ,(build-num 5))))
(lambda (f)
(lambda (n)
(if (zero? n)
(int-exp ,(build-num 1))
(* n ((f f) (sub1 n))))))))
(test-check "appA-5"
(run* (q) (eval-expo rel-fact5 '() q))
'((int-val (0 0 0 1 1 1 1))))
(test-check "appA-6"
(let ((ans (run 5 (q) (eval-expo q '() q))))
(and
(equal? ans
'(#t
#f
(((lambda (_.0) (list _.0 (list 'quote _.0)))
'(lambda (_.0) (list _.0 (list 'quote _.0))))
(=/= ((_.0 closure))
((_.0 int-val))
((_.0 list))
((_.0 quote)))
(sym _.0))
(((lambda (_.0) (list _.0 (list (car '(quote . _.1)) _.0)))
'(lambda (_.0) (list _.0 (list (car '(quote . _.1)) _.0))))
(=/= ((_.0 car))
((_.0 closure))
((_.0 int-val))
((_.0 list))
((_.0 quote)))
(absent closure _.1)
(absent int-val _.1)
(sym _.0))
(((lambda (_.0) (list (list 'lambda '(_.0) _.0) (list 'quote _.0)))
'(list (list 'lambda '(_.0) _.0) (list 'quote _.0)))
(=/= ((_.0 closure))
((_.0 int-val))
((_.0 list))
((_.0 quote)))
(sym _.0))))
(andmap
(lambda (quine) (equal? (eval quine) quine))
(map car (cddr ans)))))
#t)