-
Notifications
You must be signed in to change notification settings - Fork 1
/
profiling-quines-no-disequality.rkt
272 lines (238 loc) · 7.59 KB
/
profiling-quines-no-disequality.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
#lang racket
(require (except-in rackunit fail))
(require (for-syntax syntax/parse))
(require "./logical-combinator-function-definitions.rkt")
;; Testing different implementations of underlying logical combinators
;; for miniKanren quines without disequality constraints.
;; left associative conjunction ((((a & b) & c) & d) & e)
;; right associative conjunction (a & (b & (c & (d & e))))
;;
;; execute goals from leftmost argument to rightmost argument
;; (require (submod "./logical-combinator-function-definitions.rkt" macros-1+-left-assoc))
;; (require (submod "./logical-combinator-function-definitions.rkt" macros-1+-right-assoc))
;; (require (submod "./logical-combinator-function-definitions.rkt" varargs-1+-left-assoc))
;; (require (submod "./logical-combinator-function-definitions.rkt" varargs-1+-right-assoc))
;;
;; execute goals from rightmost argument to leftmost argument
;; (require (submod "./logical-combinator-function-definitions.rkt" macros-1+-left-assoc-flip))
;; (require (submod "./logical-combinator-function-definitions.rkt" macros-1+-right-assoc-flip))
;; (require (submod "./logical-combinator-function-definitions.rkt" varargs-1+-left-assoc-flip))
;; (require (submod "./logical-combinator-function-definitions.rkt" varargs-1+-right-assoc-flip))
;;
;; mixed
(require (submod "./logical-combinator-function-definitions.rkt" varargs-conj-left-disj-right))
;; (require (submod "./logical-combinator-function-definitions.rkt" varargs-conj-left-disj-right-flip))
;; External Interface
;; ---------------------------------------------------------------------------------------------------
;; We use this definition of fresh to doubly ensure we have the right
;; ’cond’ and call it right here.
(define-syntax fresh
(λ (stx)
(syntax-parse stx
[(_ (x ...) g)
(let ((n (length (syntax->list #'(x ...)))))
#`(λ (st)
(let* ((c (state->ct st))
(nc (+ #,n c)))
((apply
(λ (x ...) g)
(range c nc))
(state (state->σ st) (state->≠ st) nc)))))])))
(define-syntax-rule (run* (q) g0 g ...)
(call/initial-state
-1
(fresh (q) g0 g ...)))
(define-syntax-rule (run n (q) g0 g ...)
(call/initial-state
n
(fresh (q) g0 g ...)))
;; Interpreter Implementation
;; ---------------------------------------------------------------------------------------------------
;; Peano numbers w/1 cons cell saved
(defrel (nat? o)
(disj
(== o '())
(fresh (n)
(conj
(== o `(s . ,n))
(nat? n)))))
;; Terms
(defrel (expr? o)
(disj
(fresh (n)
(conj
(== o `(x . ,n))
(nat? n)))
(== o 'quote) ;; why isn't this a special form?
(fresh (n t)
(conj
(== o `(λ (x . ,n) ,t))
(nat? n)
(expr? t)))
(fresh (t₁ t₂)
(conj
(== o `(,t₁ ,t₂))
(expr? t₁)
(expr? t₂)))
(fresh (t₁ t₂)
(conj
(== o `(list₂ ,t₁ ,t₂))
(expr? t₁)
(expr? t₂)))))
;; Values
(defrel (val? o)
(disj
(fresh (e n t)
(conj
(== `(closure ,e ,n ,t) o)
(env? e)
(nat? n)
(expr? t)))
(fresh (t)
(conj
(== `(code ,t) o)
(expr? t)))))
;; Environment
(defrel (env? o)
(disj
(== o '())
(fresh (n v e)
(conj
(== o `((,n . ,v) . ,e))
(nat? n)
(val? v)
(env? e)))))
;;; Helpers
;; Disequality for Peano Numbers
(defrel (=/= n₁ n₂)
(disj
(fresh (pn₂)
(conj
(== n₂ `(s . ,pn₂))
(== n₁ '())))
(fresh (pn₁)
(conj
(== n₁ `(s . ,pn₁))
(== n₂ '())))
(fresh (pn₁ pn₂)
(conj
(== n₁ `(s . ,pn₁))
(== n₂ `(s . ,pn₂))
(=/= pn₁ pn₂)))))
;; Environment Lookup (where keys are indices)
(defrel (lookup e i v)
(fresh (j vj er)
(conj
(== e `((,j . ,vj) . ,er))
(disj
(conj (== i j)
(== v vj))
(conj (=/= i j)
(lookup er i v))))))
;; Evaluation
(defrel (valof e t v)
(disj
(fresh (x)
(conj
(== t `(x . ,x))
(lookup e x v)))
(fresh (x t0)
(conj
(== t `(λ (x . ,x) ,t0))
(== v `(closure ,e ,x ,t0))))
(fresh (t0)
(conj
(== t `(quote ,t0))
(== v `(code ,t0))))
(fresh (t₁ t₂ e0 x0 t0 v₂)
(conj
(== t `(,t₁ ,t₂))
(valof e t₁ `(closure ,e0 ,x0 ,t0))
(valof e t₂ v₂)
(valof `((,x0 . ,v₂) . ,e0) t0 v)))
(fresh (t₁ t₂ c₁ c₂)
(conj
(== t `(list₂ ,t₁ ,t₂))
(valof e t₁ `(code ,c₁))
(valof e t₂ `(code ,c₂))
(== v `(code (,c₁ ,c₂)))))))
;; Correctness Tests
;; ---------------------------------------------------------------------------------------------------
(run 1 (q)
(valof '()
'(((λ (x) (λ (x) (x))) (quote quote)) (quote λ))
q))
(run 1 (q)
(valof '()
'(((λ (x) (λ (x s) (x))) (quote λ)) (quote quote))
q))
(let ((res (run 1 (q) (valof '() q '(code (I love you))))))
(and (not (null? res)) (null? (cdr res))))
;; Quine verification.
(define quine
'((λ (x) (list₂ (x) (list₂ (quote quote) (x))))
(quote (λ (x) (list₂ (x) (list₂ (quote quote) (x)))))))
(let ((res (run* (q) (valof '() quine `(code ,quine)))))
(and (not (null? res)) (null? (cdr res))))
;; Benchmarking tests
;; ---------------------------------------------------------------------------------------------------
(define 40quines
(thunk
(void (run 40 (q) (valof '() q `(code ,q))))))
(define 5twines
(thunk
(void
(run 5 (q)
(fresh (a b)
(conj
(== q `(,a ,b))
(valof '() a `(code ,b))
(valof '() b `(code ,a))))))))
(define 1thrine
(thunk
(void
(run 1 (q)
(fresh (a b c)
(conj
(== q `(,a ,b ,c))
(valof '() a `(code ,b))
(valof '() b `(code ,c))
(valof '() c `(code ,a))))))))
;; varargs-conj-left-disj-right
;; 40quines cpu time: 82475 real time: 83550 gc time: 3859
;; 5twines cpu time: 301890 real time: 304926 gc time: 13996
;; 1thrine cpu time: 50114 real time: 50652 gc time: 4291
(time (40quines))
(time (5twines))
(time (1thrine))
;; varargs-1+-right-assoc
;; 40quines cpu time: 134356 real time: 135632 gc time: 13496
;; 5twines cpu time: 363616 real time: 366425 gc time: 35026
;; 1thrine cpu time: 200798 real time: 201933 gc time: 43823
(module+ norm
(require (only-in rnrs/base-6 assert))
;;; Normalization (of terms, so that they can be valofuated in Scheme)
;; Our language is a subset of Scheme, except that we need to turn
;; our peano-encoded variables into Scheme symbols.
;; Normalizes the list (s ...(s <x>)) to s...s<x>,
;; where s can be applied 0 or more times to the symbol <x>,
;; which will be either z or an unbound logic variable such as _.0.
(define normalize-var-name
(λ (n)
(if (and (list? n) (eq? 's (car n)) (null? (cddr n)))
(string->symbol (string-append
(symbol->string (car n))
(symbol->string (normalize-var-name (cadr n)))))
(begin
(assert (symbol? n))
n))))
;; Normalizes all occurrences of (x <peano>) to a symbol.
(define normalize
(λ (t)
(if (list? t)
(if (and (eq? 'x (car t)) (null? (cddr t)))
(normalize-var-name (cadr t))
(map normalize t))
(begin
(assert (not (eq? 'x t)))
t)))))