-
Notifications
You must be signed in to change notification settings - Fork 3
/
micro-in-mini.rkt
276 lines (263 loc) · 8.08 KB
/
micro-in-mini.rkt
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
#lang racket
(provide (all-defined-out))
(require C311/mk)
(require C311/numbers)
;; Jason Hemann and Dan Friedman
;; An implementation of a microKanren-like language in miniKanren.
;; Right now, this requires the C311 implementation of mK + relational
;; arithmetic suite. However, any implementation with ==, =/=, fresh,
;; conde, symbolo, and not-pairo will work.
;; Building this actually required writing an interpreter in order to
;; properly scope variables. Too, this implementation allows only a
;; single user-defined goal constructor.
;; Variables are tagged lists.
;; Goals are tagged lists.
;; Thunks are tagged lists.
;; Closures are tagged lists.
;; Basically, if you can think of it, it's a tagged list.
(define assoco
(lambda (ud s o)
(conde
((== s '()) (== #f o))
((fresh (a d)
(== `(,a . ,d) s)
(fresh (aa ad)
(== `(,aa . ,ad) a)
(conde
((== aa ud) (== a o))
((=/= aa ud) (assoco ud d o)))))))))
(define walko
(lambda (u s o)
(conde
((not-pairo u) (== u o))
((fresh (ua ud)
(== `(,ua . ,ud) u)
(conde
((== ua 'var)
(fresh (pr)
(conde
((== pr #f) (== u o) (assoco ud s pr))
((fresh (prd)
(== `(,ud . ,prd) pr)
(assoco ud s pr)
(walko prd s o))))))
((=/= ua 'var) (== u o))))))))
(define unifyo
(lambda (u^ v^ s o)
(fresh (u v)
(walko u^ s u)
(walko v^ s v)
(conde
((fresh (ua ud)
(== u `(,ua . ,ud))
(conde
((== ua 'var)
(conde
((fresh (va vd)
(== `(,va . ,vd) v)
(conde
((== va 'var)
(conde
((== ud vd) (== s o))
((=/= ud vd) (== `((,ud . ,v) . ,s) o))))
((=/= va 'var) (== `((,ud . ,v) . ,s) o)))))
((not-pairo v) (== `((,ud . ,v) . ,s) o))))
((=/= ua 'var)
(conde
((fresh (va vd)
(== `(,va . ,vd) v)
(conde
((== va 'var) (== `((,vd . ,u) . ,s) o))
((=/= va 'var)
(conde
((fresh (s^)
(== s^ #f)
(== #f o)
(unifyo ua va s s^)))
((fresh (s^)
(=/= s^ #f)
(unifyo ua va s s^)
(unifyo ud vd s^ o))))))))
((not-pairo v) (== #f o)))))))
((not-pairo u)
(conde
((fresh (va vd)
(== `(,va . ,vd) v)
(conde
((== va 'var) (== o `((,vd . ,u) . ,s)))
((=/= va 'var) (== #f o)))))
((not-pairo v)
(conde
((== u v) (== s o))
((=/= u v) (== #f o))))))))))
(define mpluso
(lambda ($1 $2 o)
(conde
((== '() $1) (== $2 o))
((fresh (a d)
(== `(,a . ,d) $1)
(conde
((== a 'th) (== `(th mplus ,$1 ,$2) o))
((=/= a 'th)
(fresh ($res)
(== `(,a . ,$res) o)
(mpluso d $2 $res)))))))))
(define run-goalo
(lambda (g s c o)
(conde
[(fresh (u v env)
(== g `(==-g ,u ,v ,env))
(conde
((fresh (s^)
(== s^ #f)
(== o '())
(fresh (u^ v^)
(microo u env u^)
(microo v env v^)
(unifyo u^ v^ s s^))))
((fresh (s^)
(== o `((,s^ . ,c)))
(=/= s^ #f)
(fresh (u^ v^)
(microo u env u^)
(microo v env v^)
(unifyo u^ v^ s s^))))))]
[(fresh (f env)
(== g `(call/fresh-g ,f ,env))
(fresh (x body env^)
(microo f env `(closure ,x ,body ,env^))
(fresh (g^ c+)
(microo body `(,x (reg (var . ,c)) ,env^) g^)
(pluso c (build-num 1) c+)
(run-goalo g^ s c+ o))))]
[(fresh (g1 g2 env)
(== g `(conj-g ,g1 ,g2 ,env))
(fresh (g1v g2v)
(microo g1 env g1v)
(microo g2 env g2v)
(fresh ($)
(run-goalo g1v s c $)
(bindo $ g2v o))))]
[(fresh (g1 g2 env)
(== g `(disj-g ,g1 ,g2 ,env))
(fresh (g1v g2v)
(microo g1 env g1v)
(microo g2 env g2v)
(fresh ($1 $2)
(run-goalo g1v s c $1)
(run-goalo g2v s c $2)
(mpluso $1 $2 o))))]
[(fresh (rand env)
(== g `(top-g ,rand ,env))
(== `(th top ,env ,rand ,s ,c) o))])))
(define run-tho
(lambda (th o)
(fresh (d)
(== th `(th . ,d))
(conde
[(fresh ($ g)
(== d `(bind ,$ ,g))
(fresh ($v)
(run-tho $ $v)
(bindo $v g o)))]
[(fresh ($1 $2)
(== d `(mplus ,$1 ,$2))
(fresh ($1v)
(run-tho $1 $1v)
(mpluso $2 $1v o)))]
[(fresh (env rand s c)
(== d `(top ,env ,rand ,s ,c))
(fresh (x body env^ randv)
(microo 'top env `(closure ,x ,body ,env^))
(microo rand env randv)
(fresh (g)
(microo body `(,x (reg ,randv) ,env) g)
(run-goalo g s c o))))]))))
(define microo
(lambda (exp env o)
(conde
((symbolo exp) (apply-envo env exp o))
((== #t exp) (== #t o))
((== #f exp) (== #f o))
((fresh (f)
(== exp `(call/fresh ,f))
(== `(call/fresh-g ,f ,env) o)))
((fresh (pr)
(== `(car ,pr) exp)
(fresh (prva prvd)
(microo pr env `(,prva . ,prvd))
(== prva o))))
((fresh (pr)
(== exp `(cdr ,pr))
(fresh (prva prvd)
(microo pr env `(,prva . ,prvd))
(== prvd o))))
((fresh (exp2)
(== `(quote ,exp2) exp)
(== exp2 o)))
((fresh (rand)
(== `(top ,rand) exp)
(== `(top-g ,rand ,env) o)))
((fresh (rator rand)
(== `(,rator ,rand) exp)
(=/= 'call/fresh rator)
(=/= 'top rator)
(=/= 'car rator)
(=/= 'cdr rator)
(=/= 'quote rator)
(fresh (x body env^ randv)
(microo rator env `(closure ,x ,body ,env^))
(microo rand env randv)
(microo body `(,x (reg ,randv) ,env^) o))))
((fresh (u v)
(== `(== ,u ,v) exp)
(== `(==-g ,u ,v ,env) o)))
((fresh (g1 g2)
(== exp `(conj ,g1 ,g2))
(== o `(conj-g ,g1 ,g2 ,env))))
((fresh (g1 g2)
(== exp `(disj ,g1 ,g2))
(== o `(disj-g ,g1 ,g2 ,env))))
((fresh (a d)
(== exp `(cons ,a ,d))
(fresh (av dv)
(== `(,av . ,dv) o)
(microo a env av)
(microo d env dv))))
((fresh (x body)
(== exp `(lambda (,x) ,body))
(== o `(closure ,x ,body ,env))
(symbolo x)))
((fresh (f x exp2 exp3)
(== exp `(letrec ((,f (lambda (,x) ,exp2))) ,exp3))
(symbolo f)
(symbolo x)
(microo exp3 `(,f (rec ,x ,exp2) ,env) o))))))
(define apply-envo
(lambda (env y o)
(fresh (f b env^)
(== `(,f ,b ,env^) env)
(conde
((== y f)
(conde
((fresh (x exp2)
(== `(rec ,x ,exp2) b)
(microo `(lambda (,x) ,exp2) env o)))
((fresh (a)
(== `(reg ,a) b)
(== a o)))))
((=/= y f) (apply-envo env^ y o))))))
(define bindo
(lambda ($ g o)
(conde
((== '() $) (== '() o))
((fresh (a d)
(== `(,a . ,d) $)
(conde
((== 'th a) (== `(th bind ,$ ,g) o))
((fresh (aa da)
(== `(,aa . ,da) a)
(fresh ($1 $2)
(run-goalo g aa da $1)
(bindo d g $2)
(mpluso $1 $2 o))))))))))