-
Notifications
You must be signed in to change notification settings - Fork 1
/
logical-combinator-function-definitions.rkt
277 lines (212 loc) · 6.31 KB
/
logical-combinator-function-definitions.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
#lang racket
(require (except-in rackunit fail))
(provide (all-defined-out))
;; We can implement conj2/disj2 primitives with:
;;
;; - macros or varargs
;; - left recursive or right recursive
;; - consume args list l-to-r or r-to-l
;;
;; All four of the varags versions seem to require apply, at least in
;; the built-over-conj2/disj2 versions.
;;
;; This file contains a single core implementation of the
;; non-conj/disj functionality, which is exported in all examples.
;;
;; That can be required separately from these specific implementations
;; of the conjunction/disjunction behavior.
;;
(module* macros-1+-left-assoc #f
(provide (all-defined-out))
(define-syntax conj
(syntax-rules ()
((conj g) g)
((conj g g1 gs ...) (conj (conj2 g g1) gs ...))))
(define-syntax disj
(syntax-rules ()
((disj g) g)
((disj g g1 gs ...) (disj (disj2 g g1) gs ...))))
)
(module* macros-1+-right-assoc #f
(provide (all-defined-out))
(define-syntax conj
(syntax-rules ()
((conj g) g)
((conj g g1 gs ...) (conj2 g (conj g1 gs ...)))))
(define-syntax disj
(syntax-rules ()
((disj g) g)
((disj g g1 gs ...) (disj2 g (disj g1 gs ...)))))
)
(module* macros-1+-left-assoc-flip #f
(provide (all-defined-out))
(define-syntax conj
(syntax-rules ()
((conj g) g)
((conj g g1 gs ...) (conj (conj2 g1 g) gs ...))))
(define-syntax disj
(syntax-rules ()
((disj g) g)
((disj g g1 gs ...) (disj (disj2 g1 g) gs ...))))
)
(module* macros-1+-right-assoc-flip #f
(provide (all-defined-out))
(define-syntax conj
(syntax-rules ()
((conj g) g)
((conj g g1 gs ...) (conj2 (conj g1 gs ...) g))))
(define-syntax disj
(syntax-rules ()
((disj g) g)
((disj g g1 gs ...) (disj2 (disj g1 gs ...) g))))
)
(module* varargs-1+-left-assoc #f
(provide (all-defined-out))
(define (conj g . gs)
(cond
((null? gs) g)
(else (apply conj (cons (conj2 g (car gs)) (cdr gs))))))
(define (disj g . gs)
(cond
((null? gs) g)
(else (apply disj (cons (disj2 g (car gs)) (cdr gs))))))
)
(module* varargs-conj-left-disj-right #f
(provide (all-defined-out))
(define (conj g . gs)
(cond
((null? gs) g)
(else (apply conj (cons (conj2 g (car gs)) (cdr gs))))))
(define (disj g . gs)
(cond
((null? gs) g)
(else (disj2 g (apply disj (cons (car gs) (cdr gs)))))))
)
(module* varargs-1+-right-assoc #f
(provide (all-defined-out))
(define (conj g . gs)
(cond
((null? gs) g)
(else (conj2 g (apply conj (cons (car gs) (cdr gs)))))))
(define (disj g . gs)
(cond
((null? gs) g)
(else (disj2 g (apply disj (cons (car gs) (cdr gs)))))))
)
(module* varargs-1+-left-assoc-flip #f
(provide (all-defined-out))
(define (conj g . gs)
(cond
((null? gs) g)
(else (apply conj (cons (conj2 (car gs) g) (cdr gs))))))
(define (disj g . gs)
(cond
((null? gs) g)
(else (apply disj (cons (disj2 (car gs) g) (cdr gs))))))
)
(module* varargs-1+-right-assoc-flip #f
(provide (all-defined-out))
(define (conj g . gs)
(cond
((null? gs) g)
(else (conj2 (apply conj (cons (car gs) (cdr gs))) g))))
(define (disj g . gs)
(cond
((null? gs) g)
(else (disj2 (apply disj (cons (car gs) (cdr gs))) g))))
)
(module* varargs-conj-left-disj-right-flip #f
(provide (all-defined-out))
(define (conj g . gs)
(cond
((null? gs) g)
(else (apply conj (cons (conj2 g (car gs)) (cdr gs))))))
(define (disj g . gs)
(cond
((null? gs) g)
(else (disj2 (apply disj (cons (car gs) (cdr gs))) g))))
)
;; Implementation basis, for subsequent testing.
(define (var x) x)
(define (var? x) (number? x))
(define (find u s)
(let ((pr (and (var? u) (assv u s))))
(if pr (find (cdr pr) s) u)))
(define (ext-s x u s)
(cond
((occurs? x u s) #f)
(else `((,x . ,u) . ,s))))
(define (occurs? x u s)
(cond
((var? u) (eqv? x u))
((pair? u) (or (occurs? x (find (car u) s) s)
(occurs? x (find (cdr u) s) s)))
(else #f)))
(define (unify u v s)
(cond
((eqv? u v) s)
((var? u) (ext-s u v s))
((var? v) (unify v u s))
((and (pair? u) (pair? v))
(let ((s (unify (find (car u) s) (find (car v) s) s)))
(and s (unify (find (cdr u) s) (find (cdr v) s) s))))
(else #f)))
(define (== u v)
(lambda (st)
(let ((s (state->σ st)))
(let ((s (unify (find u s) (find v s) s)))
(if s (return s (state->≠ st) (state->ct st))
'())))))
(define (invalid? s d)
(ormap (lambda (pr) (equal? (unify (find (car pr) s) (find (cdr pr) s) s) s)) d)) ;; type kludge
(define (return s d c) (if (invalid? s d) '() (list (state s d c))))
(define (=/= u v)
(lambda (st)
(return (state->σ st) (cons `(,u . ,v) (state->≠ st)) (state->ct st))))
(struct state (>σ >≠ >ct) #:transparent)
(define empty-state
(state '() '() 0))
(define (call/initial-state n g)
(take n (pull (g empty-state))))
(define ((disj2 g1 g2) s/c)
($append (g1 s/c) (g2 s/c)))
(define ((conj2 g1 g2) s/c)
($append-map g2 (g1 s/c)))
(define succeed (λ (st) (list st)))
(define fail (λ (st) (list)))
(define ($append $1 $2)
(cond
((null? $1) $2)
((promise? $1) (delay/name ($append $2 (force $1))))
(else (cons (car $1) ($append (cdr $1) $2)))))
(define ($append-map g $)
(cond
((null? $) `())
((promise? $) (delay/name ($append-map g (force $))))
(else ($append (g (car $)) ($append-map g (cdr $))))))
(define-syntax-rule (defrel (defname . args) g)
(define ((defname . args) st) (delay/name (g st))))
(define (take n $)
(cond
((null? $) '())
((and n (zero? (- n 1))) (list (car $)))
(else (cons (car $)
(take (and n (- n 1)) (pull (cdr $)))))))
(define (pull $) (if (promise? $) (pull (force $)) $))
(define ((call/fresh f) st)
(let ((c (state->ct st)))
((f (var c)) (state (state->σ st)
(state->≠ st)
(+ c 1)))))
(define ((ifte g0 g1 g2) st)
(let loop (($ (g0 st)))
(cond
((null? $) (g2 st))
((promise? $) (delay/name (loop (force $))))
(else ($append-map g1 $)))))
(define ((once g) st)
(let loop (($ (g st)))
(cond
((null? $) '())
((promise? $) (delay/name (loop (force $))))
(else (list (car $))))))