-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmetaKanren.rkt
495 lines (452 loc) · 12.6 KB
/
metaKanren.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
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
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
#|
author : Bharathi Ramana Joshi
email : [email protected]
latest version at : http://github.com/iambrj/metaKanren
|#
#|
Syntax
<mk-program> ::= (run* <logic variable> <goal expr>) |
(run <peano> <logic variable> <goal expr>)
<goal expr> ::= (disj <goal expr> <goal expr>) |
(conj <goal expr> <goal expr>) |
(fresh (<logic-var>) <goal expr>) |
(== <term expr> <term expr>) |
(letrec-rel ((<lexical var> (<lexical var>*) <goal expr>)) <goal expr>) |
(call-rel <lexical var> <term expr>*) |
(delay <goal expr>)
<term expr> ::= (quote <datum>) |
<lexical variable> |
<logic variable> |
(cons <term expr> <term expr>)
<datum> ::= <number> |
<boolean> |
<symbol> (not the var tag) |
()
<peano> ::= () |
(<peano>)
<lexical variable> ::= <symbol>
<logic variable> ::= (var . <peano>)
<number> ::= [0-9]+
<boolean> ::= #f |
#t
|#
#lang racket
; Get faster-miniKanren here https://github.com/michaelballantyne/faster-miniKanren
(require "../faster-miniKanren/mk.rkt")
(provide (all-defined-out))
(define empty-s '())
(define peano-zero '())
(define init-env '())
(define (peano n)
(if (zero? n) '() `(,(peano (- n 1)))))
(define (peanoo p)
(conde
[(== '() p)]
[(fresh (p1)
(== `(,p1) p)
(peanoo p1))]))
(define (var?o x)
(fresh (val)
(== `(var . ,val) x)
(peanoo val)))
(define (var=?o x y)
(fresh (val)
(== `(var . ,val) x)
(== `(var . ,val) y)
(peanoo val)))
(define (var=/=o x y)
(fresh (val1 val2)
(== `(var . ,val1) x)
(== `(var . ,val2) y)
(=/= val1 val2)
(peanoo val1)
(peanoo val2)))
(define (booleano b)
(conde
[(== #t b)]
[(== #f b)]))
(define (walko u s v)
(conde
[(== u v)
(conde
[(symbolo u) (== u v)]
[(numbero u) (== u v)]
[(booleano u) (== u v)]
[(== '() u) (== u v)])]
[(fresh (a d)
(== `(,a . ,d) u)
(=/= a 'var)
(== u v))]
[(var?o u)
(conde
[(== u v) (not-assp-subo u s)]
[(fresh (pr-d)
(assp-subo u s `(,u . ,pr-d))
(walko pr-d s v))])]))
(define (assp-subo v s out)
(fresh (h t h-a h-d)
(== `(,h . ,t) s)
(== `(,h-a . ,h-d) h)
(var?o v)
(var?o h-a)
(conde
[(== h-a v) (== h out)]
[(=/= h-a v) (assp-subo v t out)])))
(define (not-assp-subo v s)
(fresh ()
(var?o v)
(conde
[(== '() s)]
[(fresh (t h-a h-d)
(== `((,h-a . ,h-d) . ,t) s)
(var?o h-a)
(=/= h-a v)
(not-assp-subo v t))])))
(define (ext-so u v s s1)
(== `((,u . ,v) . ,s) s1))
; u, v <- {logic var, number, symbol, boolean, empty list, non-empty list}
; Total 36 + 5 (types match, but terms do not) = 41 cases
(define (unifyo u-unwalked v-unwalked s s1)
(fresh (u v)
(walko u-unwalked s u)
(walko v-unwalked s v)
(conde
[(var?o u) (var?o v) (var=?o u v) (== s s1)]
[(var?o u) (var?o v) (var=/=o u v) (ext-so u v s s1)]
[(var?o u) (numbero v) (ext-so u v s s1)]
[(var?o u) (symbolo v) (ext-so u v s s1)]
[(var?o u) (booleano v) (ext-so u v s s1)]
[(var?o u) (== '() v) (ext-so u v s s1)]
[(var?o u)
(fresh (a d)
(== `(,a . ,d) v)
(=/= 'var a))
(ext-so u v s s1)]
[(numbero u) (var?o v) (ext-so v u s s1)]
[(numbero u) (numbero v) (== u v) (== s s1)]
[(numbero u) (numbero v) (=/= u v) (== #f s1)]
[(numbero u) (symbolo v) (== #f s1)]
[(numbero u) (booleano v) (== #f s1)]
[(numbero u) (== '() v) (== #f s1)]
[(numbero u)
(fresh (a d)
(== `(,a . ,d) v)
(=/= 'var a))
(== #f s1)]
[(symbolo u) (var?o v) (ext-so v u s s1)]
[(symbolo u) (numbero v) (== #f s1)]
[(symbolo u) (symbolo v) (== u v) (== s s1)]
[(symbolo u) (symbolo v) (=/= u v) (== #f s1)]
[(symbolo u) (booleano v) (== #f s1)]
[(symbolo u) (== '() v) (== #f s1)]
[(symbolo u)
(fresh (a d)
(== `(,a . ,d) v)
(=/= 'var a))
(== #f s1)]
[(booleano u) (var?o v) (ext-so v u s s1)]
[(booleano u) (numbero v) (== #f s1)]
[(booleano u) (symbolo v) (== #f s1)]
[(booleano u) (booleano v) (== u v) (== s s1)]
[(booleano u) (booleano v) (=/= u v) (== #f s1)]
[(booleano u) (== '() v) (== #f s1)]
[(booleano u)
(fresh (a d)
(== `(,a . ,d) v)
(=/= 'var a))
(== #f s1)]
[(== '() u) (var?o v) (ext-so v u s s1)]
[(== '() u) (numbero v) (== #f s1)]
[(== '() u) (symbolo v) (== #f s1)]
[(== '() u) (booleano v) (== #f s1)]
[(== '() u) (== '() v) (== s s1)]
[(== '() u)
(fresh (a d)
(== `(,a . ,d) v)
(=/= 'var a))
(== #f s1)]
[(var?o v)
(fresh (a d)
(== `(,a . ,d) u)
(=/= 'var a))
(ext-so v u s s1)]
[(numbero v)
(fresh (a d)
(== `(,a . ,d) u)
(=/= 'var a))
(== #f s1)]
[(symbolo v)
(fresh (a d)
(== `(,a . ,d) u)
(=/= 'var a))
(== #f s1)]
[(booleano v)
(fresh (a d)
(== `(,a . ,d) u)
(=/= 'var a))
(== #f s1)]
[(== '() v)
(fresh (a d)
(== `(,a . ,d) u)
(=/= 'var a))
(== #f s1)]
[(fresh (u-a u-d v-a v-d s-a)
(== `(,u-a . ,u-d) u)
(== `(,v-a . ,v-d) v)
(=/= 'var u-a)
(=/= 'var v-a)
(conde
[(== s-a #f) (== #f s1) (unifyo u-a v-a s s-a)]
[(=/= s-a #f)
(unifyo u-a v-a s s-a)
(unifyo u-d v-d s-a s1)]))])))
(define mzero '())
(define (mpluso $1 $2 $)
(conde
[(== '() $1) (== $2 $)]
[(fresh (a d r1)
(== `(,a . ,d) $1)
(=/= 'delayed a)
(== `(,a . ,r1) $)
(mpluso d $2 r1))]
[(fresh (d)
(== `(delayed . ,d) $1)
(== `(delayed mplus ,$1 ,$2) $))]))
(define (bindo $ ge env $1)
(conde
[(== '() $) (== mzero $1)]
[(fresh ($1-a $1-d v-a v-d)
(== `(,$1-a . ,$1-d) $)
(=/= 'delayed $1-a)
(eval-gexpro ge $1-a env v-a)
(bindo $1-d ge env v-d)
(mpluso v-a v-d $1))]
[(fresh (d)
(== `(delayed . ,d) $)
(== `(delayed bind ,$ ,ge ,env) $1))]))
(define (exto params args env env1)
(conde
[(== params '())
(== args '())
(== env env1)]
[(fresh (x-a x-d v-a v-d)
(== `(,x-a . ,x-d) params)
(== `(,v-a . ,v-d) args)
(exto x-d v-d `((,x-a . ,v-a) . ,env) env1))]))
(define (lookupo x env v)
(conde
[(fresh (y u env1)
(== `((,y . ,u) . ,env1) env)
(=/= y 'rec)
(conde
[(== x y) (== v u)]
[(=/= x y) (lookupo x env1 v)]))]
[(fresh (id params geb env1)
(== `((rec (closr ,id ,params ,geb)) . ,env1) env)
(conde
[(== id x)
(== `(closr ,params ,geb ,env) v)]
[(=/= id x)
(lookupo x env1 v)]))]))
(define (not-in-envo x env)
(conde
[(== '() env)]
[(fresh (y v env1)
(== `((,y . ,v) . ,env1) env)
(=/= x y)
(not-in-envo x env1))]))
(define (eval-args args env vals)
(conde
[(== args '())
(== vals '())]
[(fresh (a d va vd)
(== `(,a . ,d) args)
(== `(,va . ,vd) vals)
(eval-texpro a env va)
(eval-args d env vd))]))
(define (eval-gexpro expr s/c env $)
(conde
[(fresh (ge)
(== `(delay ,ge) expr)
(== `(delayed eval ,ge ,s/c ,env) $))]
[(fresh (ge1 ge2 ge1-$ ge2-$)
(== `(disj ,ge1 ,ge2) expr)
(eval-gexpro ge1 s/c env ge1-$)
(eval-gexpro ge2 s/c env ge2-$)
(mpluso ge1-$ ge2-$ $))]
[(fresh (ge1 ge2 ge1-$)
(== `(conj ,ge1 ,ge2) expr)
(eval-gexpro ge1 s/c env ge1-$)
(bindo ge1-$ ge2 env $))]
[(fresh (x ge s c env1)
(== `(fresh (,x) ,ge) expr)
(== `(,s . ,c) s/c)
(exto `(,x) `((var . ,c)) env env1)
(eval-gexpro ge `(,s . (,c)) env1 $))]
[(fresh (te1 te2 v1 v2 s c s1)
(== `(== ,te1 ,te2) expr)
(== `(,s . ,c) s/c)
(eval-texpro te1 env v1)
(eval-texpro te2 env v2)
(conde
[(== #f s1) (== '() $)]
[(=/= #f s1) (== `((,s1 . ,c)) $)])
(unifyo v1 v2 s s1))]
[(fresh (id params geb ge e1)
(== `(letrec-rel ((,id ,params ,geb)) ,ge) expr)
(== `((rec (closr ,id ,params ,geb)) . ,env) e1)
(eval-gexpro ge s/c e1 $))]
[(fresh (id args params geb env1 ext-env vargs)
(== `(call-rel ,id . ,args) expr)
(lookupo id env `(closr ,params ,geb ,env1))
(eval-args args env vargs)
(exto params vargs env1 ext-env)
(eval-gexpro geb s/c ext-env $))]))
(define (eval-texpro expr env val)
(conde
[(== expr val)
(conde
[(numbero expr)]
[(booleano expr)]
[(== '() expr)])]
[(== `(quote ,val) expr)
(conde
[(numbero val)]
[(symbolo val)]
[(booleano val)]
[(== '() val)]
[(fresh (a d)
(== `(,a . ,d) val))])
(not-in-envo 'quote env)
(absento 'var val)
(absento 'closr val)]
[(symbolo expr)
(lookupo expr env val)]
[(fresh (e1 e2 v-e1 v-e2)
(== `(cons ,e1 ,e2) expr)
(== `(,v-e1 . ,v-e2) val)
(not-in-envo 'cons env)
(eval-texpro e1 env v-e1)
(eval-texpro e2 env v-e2))]))
(define (walk*o unwalked-v s u)
(fresh (v)
(walko unwalked-v s v)
(conde
[(== v u)
(conde
[(var?o v)]
[(numbero v)]
[(symbolo v)]
[(booleano v)]
[(== '() v)])]
[(fresh (a d walk*-a walk*-d)
(== `(,a . ,d) v)
(=/= a 'var)
(conde
[(== '_. a)
(== u v)]
[(=/= '_. a)
(== `(,walk*-a . ,walk*-d) u)
(walk*o a s walk*-a)
(walk*o d s walk*-d)]))])))
(define (pullo $ $1)
(conde
[(== '() $) (== '() $1)]
[(fresh (a d)
(== `(,a . ,d) $)
(== $ $1)
(=/= 'delayed a))]
[(fresh (ge s/c env $2)
(== `(delayed eval ,ge ,s/c ,env) $)
(eval-gexpro ge s/c env $2)
(pullo $2 $1))]
[(fresh ($a $b $a1 $2)
(== `(delayed mplus ,$a ,$b) $)
(pullo $a $a1)
(mpluso $b $a1 $2)
(pullo $2 $1))]
[(fresh (saved-ge saved-env saved-$ saved-$1 $2)
(== `(delayed bind ,saved-$ ,saved-ge ,saved-env) $)
(pullo saved-$ saved-$1)
(bindo saved-$1 saved-ge saved-env $2)
(pullo $2 $1))]))
(define (take-allo $ s/c*)
(fresh ($1)
(pullo $ $1)
(conde
[(== '() $1) (== '() s/c*)]
[(fresh (a d-s/c* $d)
(== `(,a . ,$d) $1)
(== `(,a . ,d-s/c*) s/c*)
(take-allo $d d-s/c*))])))
(define (take-no n $ s/c*)
(conde
[(== '() n) (== '() s/c*)]
[(=/= '() n)
(fresh ($1)
(pullo $ $1)
(conde
[(== '() $1) (== '() s/c*)]
[(fresh (n-1 d-s/c* a d)
(== `(,a . ,d) $1)
(== `(,n-1) n)
(== `(,a . ,d-s/c*) s/c*)
(take-no n-1 d d-s/c*))]))]))
(define (lengtho l len)
(conde
[(== '() l) (== '() len)]
[(fresh (a d len-d)
(== `(,a . ,d) l)
(== `(,len-d) len)
(lengtho d len-d))]))
(define (reify-so v-unwalked s s1)
(fresh (v)
(walko v-unwalked s v)
(conde
[(var?o v)
(fresh (len)
(lengtho s len)
(== `((,v . (_. . ,len)) . ,s) s1))]
[(== s s1)
(conde
[(numbero v)]
[(symbolo v)]
[(booleano v)]
[(== '() v)])]
[(fresh (a d sa)
(=/= 'var a)
(== `(,a . ,d) v)
(conde
[(== '_. a)
(== s s1)]
[(=/= '_. a)
(reify-so a s sa)
(reify-so d sa s1)]))])))
(define (reify-state/1st-varo s/c out)
(fresh (s c v u)
(== `(,s . ,c) s/c)
(walk*o `(var . ()) s v)
(reify-so v '() u)
(walk*o v u out)))
(define (reifyo s/c* out)
(conde
[(== '() s/c*) (== '() out)]
[(fresh (a d va vd)
(== `(,a . ,d) s/c*)
(== `(,va . ,vd) out)
(reify-state/1st-varo a va)
(reifyo d vd))]))
(define (eval-programo expr out)
(conde
[(fresh (lvar gexpr $ s/c*)
(symbolo lvar)
(== `(run* (,lvar) ,gexpr) expr)
(eval-gexpro `(fresh (,lvar) ,gexpr) `(,empty-s . ,peano-zero) init-env $)
(take-allo $ s/c*)
(reifyo s/c* out))]
[(fresh (n lvar gexpr $ s/c*)
(symbolo lvar)
(== `(run ,n (,lvar) ,gexpr) expr)
(eval-gexpro `(fresh (,lvar) ,gexpr) `(,empty-s . ,peano-zero) init-env $)
(take-no n $ s/c*)
(reifyo s/c* out))]))