-
Notifications
You must be signed in to change notification settings - Fork 3
/
dkanren-simple-interp.rkt
197 lines (170 loc) · 6.32 KB
/
dkanren-simple-interp.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/base
(require
"dkanren.rkt"
)
(module+ test
(require
rackunit
)
(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-syntax test-time
(syntax-rules ()
((_ test-name query expected)
(begin
(displayln test-name)
(time (test test-name query expected))))))
(define-syntax test-any
(syntax-rules ()
((_ name expr expecteds)
(let* ((actual expr)
(found (member actual expecteds))
(expected (if (not found) (car expecteds) (car found))))
(test name actual expected)))))
(define-syntax test-time-any
(syntax-rules ()
((_ name body ...)
(begin
(displayln name)
(time (test-any name body ...))))))
)
(define closure-tag (gensym "#%closure"))
(define (si body)
`(letrec
((eval
(lambda (expr env)
(let ((bound? (lambda (sym) (in-env? sym env))))
(match expr
(`(,(and 'quote (not (? bound?))) ,(? quotable? datum)) datum)
(`(,(and 'list (not (? bound?))) . ,a*) (proper-list a* env))
((symbol) (lookup expr env))
(`(,(and op (or (not (symbol)) (? bound?))) ,rand)
(match (eval op env)
(`(,(? ct?) ,x ,body ,env^)
(eval body `((,x . ,(eval rand env)) . ,env^)))))
(`(lambda (,(symbol x)) ,body)
(list ',closure-tag x body env))))))
(in-env? (lambda (x env)
(match/lazy env
('() #f)
(`((,a . ,_) . ,d)
(or (equal? x a) (in-env? x d))))))
(lookup (lambda (x env)
(match env
(`((,y . ,v) . ,rest)
(if (equal? x y)
v
(lookup x rest))))))
(ct? (lambda (datum) (equal? ',closure-tag datum)))
(quotable?
(lambda (datum)
(match/lazy datum
(',closure-tag #f)
(`(,a . ,d) (and (quotable? a) (quotable? d)))
(_ #t))))
(proper-list
(lambda (expr env)
(match expr
;; TODO: ultimately, this clause should be: ('() '())
;; Until quotas are enforced, we obfuscate the rhs to prevent
;; unbounded deterministic elimination of this clause.
('() (car (list '())))
(`(,a . ,d) `(,(eval a env) . ,(proper-list d env)))))))
,body))
(define (evalo expr result) (dk-evalo (si `(eval ',expr '())) result))
(define quinec
'((lambda (_.0)
(list _.0 (list (quote quote) _.0)))
(quote
(lambda (_.0)
(list _.0 (list (quote quote) _.0))))))
(define twine1
'((lambda (_.0) (list 'quote (list _.0 (list 'quote _.0))))
'(lambda (_.0) (list 'quote (list _.0 (list 'quote _.0))))))
(define twine0 (list 'quote twine1))
(define thrine2
'((lambda (_.0) (list 'quote (list 'quote (list _.0 (list 'quote _.0)))))
'(lambda (_.0) (list 'quote (list 'quote (list _.0 (list 'quote _.0)))))))
(define thrine1 (list 'quote thrine2))
(define thrine0 (list 'quote thrine1))
(module+ test
(test "quote"
(run* (q) (evalo '(quote 5) q))
'((5)))
(test "list"
(run* (q) (evalo '(list '5 (list '4 '3)) q))
'(((5 (4 3)))))
(test "lambda"
(run* (q) (evalo '(lambda (x) x) q))
`(((,closure-tag x x ()))))
(test "application-1"
(run* (q) (evalo '((lambda (x) x) '2) q))
'((2)))
(test "application-2"
(run* (q) (evalo '(((lambda (x) (lambda (y) x)) '1) '2) q))
'((1)))
(test-time "quine parts"
(run 1 (q) (evalo
`((lambda (x) (list x ,q))
(quote
(lambda (x) (list x ,q))))
`((lambda (x) (list x ,q))
(quote
(lambda (x) (list x ,q))))))
'(((list (quote quote) x))))
(test-time "quine more parts"
(run 1 (q) (evalo
`((lambda (x) ,q)
(quote (lambda (x) ,q)))
`((lambda (x) ,q)
(quote (lambda (x) ,q)))))
'(((list x (list (quote quote) x)))))
;; TODO: reify constraints on _.0
(test-time "quine full"
(run 1 (q) (evalo q q))
`((,quinec)))
(test-time-any "twine"
(run 1 (p q) (=/= p q) (evalo p q) (evalo q p))
`(((,twine0 ,twine1))
((,twine1 ,twine0))))
(test-time-any "thrine"
(run 1 (p q r) (=/= p q) (=/= q r) (=/= r p)
(evalo p q) (evalo q r) (evalo r p))
`(((,thrine0 ,thrine1 ,thrine2))
((,thrine2 ,thrine0 ,thrine1))
((,thrine1 ,thrine2 ,thrine0))))
;(time
;(test "tetrine"
;(run 1 (p q r s) (=/= s p) (=/= s q) (=/= s r) (=/= p q) (=/= q r) (=/= r p)
;(evalo p q) (evalo q r) (evalo r s) (evalo s p))
;`(ok)))
;(time
;(test "pentine"
;(run 1 (p q r s t) (=/= t p) (=/= t q) (=/= t r) (=/= t s) (=/= s p) (=/= s q) (=/= s r) (=/= p q) (=/= q r) (=/= r p)
;(evalo p q) (evalo q r) (evalo r s) (evalo s t) (evalo t p))
;`(ok)))
;(time
;(test "hexine"
;(run 1 (p q r s t u) (=/= u p) (=/= u q) (=/= u r) (=/= u s) (=/= u t) (=/= t p) (=/= t q) (=/= t r) (=/= t s) (=/= s p) (=/= s q) (=/= s r) (=/= p q) (=/= q r) (=/= r p)
;(evalo p q) (evalo q r) (evalo r s) (evalo s t) (evalo t u) (evalo u p))
;`(ok)))
;(time
;(test "heptine"
;(run 1 (p q r s t u v) (=/= v p) (=/= v q) (=/= v r) (=/= v s) (=/= v t) (=/= v u) (=/= u p) (=/= u q) (=/= u r) (=/= u s) (=/= u t) (=/= t p) (=/= t q) (=/= t r) (=/= t s) (=/= s p) (=/= s q) (=/= s r) (=/= p q) (=/= q r) (=/= r p)
;(evalo p q) (evalo q r) (evalo r s) (evalo s t) (evalo t u) (evalo u v) (evalo v p))
;`(ok)))
;(time
;(test "octine"
;(run 1 (p q r s t u v w) (=/= w p) (=/= w q) (=/= w r) (=/= w s) (=/= w t) (=/= w u) (=/= w v) (=/= v p) (=/= v q) (=/= v r) (=/= v s) (=/= v t) (=/= v u) (=/= u p) (=/= u q) (=/= u r) (=/= u s) (=/= u t) (=/= t p) (=/= t q) (=/= t r) (=/= t s) (=/= s p) (=/= s q) (=/= s r) (=/= p q) (=/= q r) (=/= r p)
;(evalo p q) (evalo q r) (evalo r s) (evalo s t) (evalo t u) (evalo u v) (evalo v w) (evalo w p))
;`(ok)))
)