-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathminiKanren.rkt
58 lines (58 loc) · 1.58 KB
/
miniKanren.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
#lang racket
(require "microKanren.rkt")
(require "impure-microKanren-extensions.rkt")
(provide (all-defined-out))
(define-syntax inverse-eta-delay
(syntax-rules ()
((_ g) (lambda (s/c) (lambda () (g s/c))))))
(define-syntax conj+
(syntax-rules ()
((_ g) g)
((_ g0 g ...) (conj g0 (conj+ g ...)))))
(define-syntax disj+
(syntax-rules ()
((_ g) g)
((_ g0 g ...) (disj g0 (disj+ g ...)))))
(define-syntax fresh
(syntax-rules ()
((_ () g0 g ...)
(inverse-eta-delay (conj+ g0 g ...)))
((_ (x0 x ...) g0 g ...)
(call/fresh (lambda (x0) (fresh (x ...) g0 g ...))))))
(define-syntax conde
(syntax-rules ()
((_ (g0 g ...) (g0* g* ...) ...)
(inverse-eta-delay
(disj+ (conj+ g0 g ...) (conj+ g0* g* ...) ...)))))
(define (reify-var0 s/c)
(let ((v (walk* (var 0) (car s/c))))
(walk* v (reify-s v '()))))
(define (reify-s v s)
(let ((v (walk v s)))
(cond
((var? v)
(let ((name (reify-name (length s))))
(cons (cons v name) s)))
((pair? v) (reify-s (cdr v) (reify-s (car v) s)))
(else s))))
(define (reify-name n)
(string->symbol
(string-append "_." (number->string n))))
(define (pull $) (if (procedure? $) (pull ($)) $))
(define (take n)
(lambda ($)
(cond
((zero? n) '())
(else
(let (($ (pull $)))
(cond
((null? $) '())
(else
(cons (car $)
((take (- n 1)) (cdr $))))))))))
(define-syntax run
(syntax-rules ()
((_ n (q) g0 g ...)
(map reify-var0
((take n)
(call/empty-state (fresh (q) g0 g ...)))))))