-
Notifications
You must be signed in to change notification settings - Fork 3
/
dkanren-arithmetic.rkt
352 lines (330 loc) · 13.7 KB
/
dkanren-arithmetic.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
#lang racket/base
(provide
arith
build-num
pluso
*o
)
(require
"dkanren.rkt"
racket/set
)
(module+ test
(require
rackunit
)
(define-syntax mk-test-cont
(syntax-rules ()
((_ test-name exact? query expected)
(let* ((result-set (list->set query))
(expected-set (list->set expected))
(overlap (set-intersect result-set expected-set)))
(if exact?
(begin
(when (not (equal? result-set expected-set))
(displayln (format "failed test: ~a" test-name)))
;(check-equal? (set-subtract expected-set result-set) (set))
;(check-equal? (set-subtract result-set expected-set) (set))
(check-equal? result-set expected-set))
(check-equal? overlap expected-set))))))
(define-syntax mk-test
(syntax-rules ()
((_ test-name query expected)
(mk-test-cont test-name #t query expected))))
(define-syntax mk-test-subsumed
(syntax-rules ()
((_ test-name query expected)
(mk-test-cont test-name #f query expected))))
(define-syntax mk-test-time
(syntax-rules ()
((_ test-name query expected)
(begin
(displayln test-name)
(time (mk-test-cont test-name #t query expected))))))
(require racket/pretty)
(define-syntax test
(syntax-rules ()
((_ name expr expected)
(let ((actual expr))
(when (not (equal? actual expected))
(display name)
(newline)
(pretty-print actual)
(newline))
(check-equal? actual expected)))))
)
(define build-num
(lambda (n)
(cond
((odd? n)
(cons 1
(build-num (quotient (- n 1) 2))))
((and (not (zero? n)) (even? n))
(cons 0
(build-num (quotient n 2))))
((zero? n) '()))))
(define (arith body)
`(letrec ((append (lambda (xs ys)
(if (null? xs)
ys
(cons (car xs) (append (cdr xs) ys)))))
(pos? (lambda (n) (pair? n)))
(>1? (lambda (n) (match n
(`(,a ,ad . ,dd) #t)
(_ #f))))
(full-adder
(lambda (b x y)
(match `(,b ,x ,y)
('(0 0 0) '(0 0))
('(1 0 0) '(1 0))
('(0 1 0) '(1 0))
('(1 1 0) '(0 1))
('(0 0 1) '(1 0))
('(1 0 1) '(0 1))
('(0 1 1) '(0 1))
('(1 1 1) '(1 1)))))
(adder
(lambda (d n m)
(match `(,d ,n ,m)
(`(0 ,_ () ) n)
(`(0 () (,_ . ,_)) m)
(`(1 ,_ () ) (adder 0 n '(1)))
(`(1 () (,_ . ,_)) (adder 0 '(1) m))
(`(,_ (1) (1) ) (full-adder d 1 1))
(`(,_ (1) ,_ ) (gen-adder d n m))
;; TODO: ideally, this could be written as two separate
;; patterns without sacrificing performance. See the
;; commented clauses.
(`(,_ (,_ ,_ . ,_) ,_)
(match m
('(1)
(match (adder d '(1) n)
((and `(,_ ,_ . ,_) r) r)))
(`(,_ ,_ . ,_) (gen-adder d n m))))
;; TODO: ideally, these two clauses, which share a common
;; pattern prefix, would allow the prefix to be learned when
;; these were the only two clauses remaining.
;(`(,_ (,_ ,_ . ,_) (1))
;(match (adder d '(1) n)
;((and `(,_ ,_ . ,_) r) r)))
;(`(,_ (,_ ,_ . ,_) (,_ ,_ . ,_)) (gen-adder d n m))
)))
(gen-adder
(lambda (d n m)
(match `(,n ,m)
(`((,a . ,x) (,b . ,(and `(,_ . ,_) y)))
(match (full-adder d a b)
(`(,c ,e)
(match (adder e x y)
((and `(,_ . ,_) z) `(,c . ,z)))))))))
(plus (lambda (n m) (adder 0 n m)))
(minus (lambda (n m) (fresh (k)
(match `(,(plus m k) ,n)
(`(,e ,e) k)
(_ #f)))))
(* (lambda (n m)
(match `(,n ,m)
(`(() ,_ ) '())
(`(,_ () ) '())
(`((1) ,_ ) m)
(`(,_ (1) ) n)
(`((0 . ,x) ,_ ) `(0 . ,(* x m)))
(`((1 . ,x) (0 . ,y)) (* m n))
(`((1 . ,x) (1 . ,y)) (odd-* x n m)))))
(odd-* (lambda (x n m)
(let ((q (* x m)))
(let ((p (plus `(0 . ,q) m)))
(and (bound-*? q p n m) p)))))
(bound-*? (lambda (q p n m)
(match `(,q ,p)
(`(() (,_ . ,_)) #t)
(`((,a0 . ,x) (,a1 . ,y))
(match `(,n ,m)
(`(() (,a2 . ,z)) (bound-*? x y z '()))
(`((,a3 . ,z) ,_) (bound-*? x y z m)))))))
;(=l (lambda (n m)
;(match `(,n ,m)
;(`(() ()) #t)
;(`((1) (1)) #t)
;(`((,a . ,(and `(,_ . ,_) x)) (,b . ,(and `(,_ . ,_) y)))
;(=l x y))
;(_ #f))))
;(<l (lambda (n m)
;(match `(,n ,m)
;(`(() `(,_ . ,_)) #t)
;(`((1) `(,_ ,_ . ,_)) #t)
;(`((,a . ,(and `(,_ . ,_) x)) (,b . ,(and `(,_ . ,_) y)))
;(<l x y))
;(_ #f))))
;(<=l (lambda (n m) (or (=l n m) (<l n m))))
;(< (lambda (n m)
;(or (<l n m)
;(and (=l n m)
;(match (minus m n)
;(`(,_ . ,_) #t)
;(_ #f))))))
;(<= (lambda (n m) (or (equal? n m) (< n m))))
;(split (lambda (n r)
;(match `(,n ,r)
;(`(() ,_) '(() ()))
;(`((0 ,b . ,n^) ()) `(() (,b . ,n^)))
;(`((1 . ,n^) ()) `((1) ,n^))
;(`((0 ,b . ,n^) (,a . ,r^))
;(match (split `(,b . ,n^) r^)
;((and `(() ,h) result) result)
;(_ #f)))
;(`((1 . ,n^) (,a . ,r^))
;(match (split n^ r^)
;((and `(() ,h)) `((1) ,h))
;(_ #f)))
;(`((,b . ,n^) (,a . ,r^))
;(match (split n^ r^)
;(`(,(and `(,_ . ,_) l^) ,h) `((,b . ,l^) ,h))
;(_ #f)))
;(_ #f))))
;(/ (lambda (n m)
;(if (< n m) `(() ,n)
;(let ((r (minus n m)))
;(if (and r (=l n m) (< r m)) `((1) ,r)
;(fresh (q r qlmr rr rh)
;(and (<l m n) (< r m) (pos? q)
;(match (split n r)
;(`(,nl ,nh)
;(match (split q r)
;(`(,ql ,qh)
;(match `(,nh ,qh)
;('(() ())
;(and (equal? (minus nl r) (* ql m))))
;(`((,_ . ,_) ,_)
;(match (split (minus (plus (* ql m) r) nl) r)
;(`(() ,rh)
;(and (equal? (/ nh m) `(,qh ,rh))))
;(_ #f)))
;(_ #f)))
;(_ #f)))
;(_ #f))
;`(,q ,r))))))))
;(exp2
;(lambda (n b)
;(let ((k (lambda ()
;(fresh (q)
;(let ((b2 (append b `(1 . ,b))))
;(and (match q
;(`(0 . ,(and `(,_ . ,_) q1))
;(and (<l b n) (equal? (exp2 n b2) q1)))
;(`(1 . ,(and `(,_ . ,_) q1))
;(match (split n b)
;(`(,s ,(and `(,_ . ,_) nh))
;(equal? (exp2 nh b2) q1))
;(_ #f)))
;(_ #f))
;q))))))
;(match n
;('(1) '())
;(`(,_ ,_ . ,_) (match (split n b)
;(`(,s (1)) '(1))
;(_ (k))))
;(_ (k))))))
;(repeated-mul
;(lambda (n q)
;(match `(,n ,q)
;(`((,_ . ,_) ()) '(1))
;(`(,_ (1)) n)
;(`(,_ (,_ ,_ . ,_))
;(* (repeated-mul n (minus q '(1))) n)))))
;(log (lambda (n b)
;(fresh (q r)
;(and (match `(,n ,b ,q ,r)
;(`((1) (,_ . ,_) () ()) #t)
;(`(,_ ,_ () ,_)
;(and (< n b) (equal? (plus r '(1)) n)))
;(`(,_ (,_ ,_ . ,_) (1) ,_)
;(and (=l n b) (equal? (plus r b) n)))
;(`(,_ (1) (,_ . ,_) ,_) (equal? (plus r '(1)) n))
;(`(,e () (,_ . ,_) ,e) #t)
;(`((,a ,ad . ,(and `(,_ . ,_) dd)) (0 1) ,_ ,_)
;(and (equal? (exp2 n '()) q)
;(match (split n dd)
;(`(,sr ,s) (equal? sr r))
;(_ #f))))
;(`(,_ ,(or '(1 1) `(,_ ,_ ,_ . ,_)) ,_ ,_)
;(and (let ((bw1 (exp2 b '()))
;(nw1 (exp2 n '())))
;(let ((bw (plus bw1 '(1)))
;(nw (plus nw1 '(1))))
;(match `(,(/ nw bw) ,(/ nw bw1))
;(`((,ql1 ,_) (,qh ,_))
;(let ((ql (minus ql1 '(1))))
;(let ((qd (minus q ql))
;(qdh (minus qh ql)))
;(let ((bq (* (repeated-mul b ql)
;(repeated-mul b qd))))
;(and (<l b n) (<l q n)
;(< nw1 (* bw (plus q '(1))))
;(<= ql q) (<= qd qdh)
;(equal? (plus bq r) n)
;(< n (* b bq)))))))
;(_ #f))))))
;(_ #f))
;`(,q ,r)))))
;(exp (lambda (b q)
;(fresh (n)
;(match `(,q ,(log n b))
;(`(,q (,q ())) #t)
;(_ #f)))))
)
,body))
(define (pluso a b c) (dk-evalo (arith `(plus ',a ',b)) c))
(define (*o a b c) (dk-evalo (arith `(* ',a ',b)) c))
(module+ test
(mk-test "test 1"
(run* (q) (pluso (build-num 2) (build-num 3) q) )
'(((1 0 1))))
(mk-test "test 2"
(run* (q) (*o (build-num 2) (build-num 3) q))
'(((0 1 1))))
(mk-test "test 3"
(run* (n m) (*o n m (build-num 6)))
'(((1) (0 1 1)) ((0 1 1) (1)) ((0 1) (1 1)) ((1 1) (0 1))))
(mk-test-subsumed "sums"
(run 12 (x y z) (pluso x y z))
'((_.0 () _.0)
(() (_.0 . _.1) (_.0 . _.1))
((1) (1) (0 1))
((1) (0 _.0 . _.1) (1 _.0 . _.1))
((1) (1 1) (0 0 1))
((0 1) (0 1) (0 0 1))))
;(mk-test "factors"
;(run* (q)
;(fresh (x y)
;(*o x y (build-num 24))
;(== `(,x ,y ,(build-num 24)) q)))
;'(((((1) (0 0 0 1 1) (0 0 0 1 1))))
;((((0 0 0 1 1) (1) (0 0 0 1 1))))
;((((0 1) (0 0 1 1) (0 0 0 1 1))))
;((((0 0 1) (0 1 1) (0 0 0 1 1))))
;((((0 0 0 1) (1 1) (0 0 0 1 1))))
;((((1 1) (0 0 0 1) (0 0 0 1 1))))
;((((0 1 1) (0 0 1) (0 0 0 1 1))))
;((((0 0 1 1) (0 1) (0 0 0 1 1))))))
;(mk-test-time "logo 3 answers"
;(run 3 (b q r)
;(logo '(0 0 1 0 0 0 1) b q r)
;(>1o q))
;'(((() (_.0 _.1 . _.2) (0 0 1 0 0 0 1)))
;(((1) (_.0 _.1 . _.2) (1 1 0 0 0 0 1)))
;(((0 1) (0 1 1) (0 0 1)))))
;(mk-test-time "logo 9 answers"
;(run 9 (b q r)
;(logo '(0 0 1 0 0 0 1) b q r)
;(>1o q))
;'(((() (_.0 _.1 . _.2) (0 0 1 0 0 0 1)))
;(((1) (_.0 _.1 . _.2) (1 1 0 0 0 0 1)))
;(((0 1) (0 1 1) (0 0 1)))
;(((1 1) (1 1) (1 0 0 1 0 1)))
;(((0 0 1) (1 1) (0 0 1)))
;(((0 0 0 1) (0 1) (0 0 1)))
;(((1 0 1) (0 1) (1 1 0 1 0 1)))
;(((0 1 1) (0 1) (0 0 0 0 0 1)))
;(((1 1 1) (0 1) (1 1 0 0 1)))))
)