forked from yinwang0/tinyKanren
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mk.rkt
197 lines (161 loc) · 3.92 KB
/
mk.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
#lang racket
(provide run* run == exist conde succeed fail display-code)
;;--------------------- substitution ----------------------
(struct var (name) #:transparent)
(define empty-s '())
(define size-s length)
(define ext-s
(lambda (x v s)
(cons `(,x . ,v) s)))
(define walk
(lambda (v s)
(cond
[(var? v)
(let ([p (assq v s)])
(cond
[(not p) v]
[else
(walk (cdr p) s)]))]
[else v])))
(define walk*
(lambda (v s)
(let ([v (walk v s)])
(cond
[(var? v) v]
[(pair? v)
(cons
(walk* (car v) s)
(walk* (cdr v) s))]
[else v]))))
(define unify
(lambda (u v s)
(let ([u (walk u s)]
[v (walk v s)])
(cond
[(eq? u v) s]
[(var? u) (ext-s u v s)]
[(var? v) (ext-s v u s)]
[(and (pair? u) (pair? v))
(let ([s (unify (car u) (car v) s)])
(and s (unify (cdr u) (cdr v) s)))]
[(equal? u v) s]
[else #f]))))
(define name
(lambda (n)
(string->symbol
(string-append "_" (number->string n)))))
(define reify-s
(lambda (v s)
(let ([v (walk v s)])
(cond
[(var? v)
(ext-s v (name (size-s s)) s)]
[(pair? v)
(reify-s (cdr v)
(reify-s (car v) s))]
[else s]))))
(define reify
(lambda (v s)
(let ([v (walk* v s)])
(walk* v (reify-s v empty-s)))))
;;---------------------- composition ----------------------
(struct stream (head tail) #:transparent)
(struct thunk (func) #:transparent)
(define-syntax delay
(syntax-rules ()
[(_ e ...)
(thunk (lambda () e ...))]))
(define force
(lambda (th)
((thunk-func th))))
(define flip
(lambda (f)
(lambda (x y)
(f y x))))
(define bind
(lambda (v g)
(match v
[#f #f]
[(stream head tail)
(mplus (g head)
(delay (bind (force tail) g)))]
[_ (g v)])))
(define bind*
(lambda (v gs)
(foldl (flip bind) v gs)))
(define mplus
(lambda (v f)
(match v
[#f (force f)]
[(stream head tail)
(stream head
(delay (mplus (force f) tail)))]
[_ (stream v f)])))
(define-syntax mplus*
(syntax-rules ()
[(_ e) e]
[(_ e0 e ...)
(mplus e0 (delay (mplus* e ...)))]))
;;------------------- goal constructors -------------------
(define ==
(lambda (u v)
(lambda (s)
(unify u v s))))
(define succeed (== #f #f))
(define fail (== #f #t))
(define-syntax exist
(syntax-rules ()
[(_ (x ...) g0 g ...)
(lambda (s)
(let ([x (var 'x)] ...)
(bind* s (list g0 g ...))))]))
(define-syntax conde
(syntax-rules ()
[(_ [g0 g ...]
[g1 g^ ...] ...)
(lambda (s)
(mplus*
(bind* s (list g0 g ...))
(bind* s (list g1 g^ ...)) ...))]))
;;----------------------- top level -----------------------
(define take
(lambda (n v)
(cond
[(zero? n) '()]
[else
(match v
[#f '()]
[(stream head tail)
(cond
[(zero? (- n 1)) ; avoid (force tail)
(list head)]
[else
(cons head (take (- n 1) (force tail)))])]
[_ (list v)])])))
(define do-display #f)
(define display-code
(lambda (v)
(set! do-display v)))
(define debug-display
(lambda (n contents)
(cond
[do-display
(display "-----------------------------------\n")
(if (= n +inf.0)
(pretty-print `(run* ,@contents))
(pretty-print `(run ,n ,@contents)))]
[else (void)])))
(define-syntax run
(syntax-rules ()
[(_ n (x) g0 g ...)
(begin
(debug-display n '(x g0 g ...))
(let ([top-g (exist (x)
g0 g ...
(lambda (s)
(reify x s)))])
(take n (top-g empty-s))))]))
(define-syntax run*
(syntax-rules ()
[(_ (x) g ...)
(run +inf.0 (x) g ...)]))