/
nominal.clj
364 lines (314 loc) · 9.88 KB
/
nominal.clj
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
353
354
355
356
357
358
359
360
361
362
363
364
(ns clojure.core.logic.nominal
(:refer-clojure :exclude [== hash])
(:use [clojure.core.logic :exclude [fresh] :as l])
(:import [java.io Writer]))
(def ^{:dynamic true} *reify-noms* true)
;; =============================================================================
;; Nominal unification with fresh, hash and tie.
;;
;; Some references / inspiration:
;; alphaKanren - http://www.cs.indiana.edu/~webyrd/alphamk/alphamk.pdf
;; Nominal Unification - http://www.cl.cam.ac.uk/~amp12/papers/nomu/nomu-jv.pdf
;; http://code.google.com/p/iucs-relational-research/source/browse/trunk/lib/minikanren/nominal.sls
;; =============================================================================
;; Nominal unification protocols
(defprotocol INomSwap
(swap-noms [t swap s]))
(defn nom-swap [a swap]
(cond
(= a (first swap)) (second swap)
(= a (second swap)) (first swap)
:else a))
(declare suspc)
(extend-protocol INomSwap
nil
(swap-noms [t swap s] [t s])
Object
(swap-noms [t swap s] [t s])
clojure.core.logic.LVar
(swap-noms [t swap s]
(let [t (walk* s t)]
(if (lvar? t)
(let [v (with-meta (lvar) (meta t))
rt (root-val s t)
s (if (subst-val? rt) (ext-no-check s v rt) s)
s (update-dom s v ::nom (fnil (fn [d] (conj d t)) #{}))
s (update-dom s t ::nom (fnil (fn [d] (conj d v)) #{}))
s (bind s (suspc v t swap))]
[v s])
(swap-noms t swap s))))
clojure.core.logic.LCons
(swap-noms [t swap s]
(let [[tfirst s] (swap-noms (lfirst t) swap s)
[tnext s] (swap-noms (lnext t) swap s)]
[(with-meta (lcons tfirst tnext) (meta t))
s]))
clojure.lang.IPersistentCollection
(swap-noms [t swap s]
(if (seq t)
(let [[tfirst s] (swap-noms (first t) swap s)
[tnext s] (swap-noms (next t) swap s)]
[(with-meta (cons tfirst tnext) (meta t))
s])
[t s])))
(extend-protocol IMergeDomains
clojure.lang.IPersistentSet
(-merge-doms [a b]
(clojure.set/union a b)))
;; =============================================================================
;; Nom
(declare nom)
(deftype Nom [lvar]
clojure.core.logic.IBindable
Object
(toString [_]
(str "<nom:" (:name lvar) ">"))
(hashCode [_]
(.hashCode lvar))
(equals [this o]
(and (.. this getClass (isInstance o))
(= lvar (:lvar o))))
clojure.lang.IObj
(withMeta [this new-meta]
(nom (with-meta lvar new-meta)))
(meta [this]
(meta lvar))
clojure.lang.ILookup
(valAt [this k]
(.valAt this k nil))
(valAt [_ k not-found]
(case k
:lvar lvar
:name (:name lvar)
:oname (:oname lvar)
not-found))
clojure.core.logic.IReifyTerm
(reify-term [v s]
(ext s v (symbol (str (if *reify-noms* "a" (:oname v)) "_" (count s)))))
INomSwap
(swap-noms [t swap s]
[(with-meta (nom-swap t swap) (meta t))
s]))
(defn nom [lvar]
(Nom. lvar))
(defn nom? [x]
(instance? clojure.core.logic.nominal.Nom x))
(defn- nom-bind [sym]
((juxt identity
(fn [s] `(nom (lvar '~s)))) sym))
(defn- nom-binds [syms]
(mapcat nom-bind syms))
(defmacro fresh
"Creates fresh noms. Goals occuring within form a logical conjunction."
[[& noms] & goals]
`(fn [a#]
(-inc
(let [~@(nom-binds noms)]
(bind* a# ~@goals)))))
(defmethod print-method Nom [x ^Writer writer]
(.write writer (str "<nom:" (:name x) ">")))
;; =============================================================================
;; hash: ensure a nom is free in a term
(declare tie? hash)
(defn- -hash
([a x] (-hash a x nil))
([a x _id]
(reify
Object
(toString [_]
(str a "#" x))
clojure.lang.IFn
(invoke [c s]
(let [a (walk* s a)
x (walk* s x)]
(if (lvar? a)
(when (and
(not (and (lvar? x) (= x a)))
(tree-term? x) (not (tie? x)))
(bind* s
(remcg c)
(constrain-tree x
(fn [t s] (bind s (hash a t))))))
(when (nom? a)
(cond
(and (tie? x) (= (:binding-nom x) a))
(bind s (remcg c))
(tree-term? x)
(bind* s
(remcg c)
(constrain-tree x
(fn [t s] (bind s (hash a t)))))
(= x a)
nil
:else
(bind s (remcg c)))))))
clojure.core.logic.IConstraintId
(id [this] _id)
clojure.core.logic.IWithConstraintId
(with-id [this _id]
(-hash a x _id))
clojure.core.logic.IConstraintOp
(rator [_] `hash)
(rands [_] [a x])
clojure.core.logic.IReifiableConstraint
(reifyc [_ v r s]
(let [x (walk* r (walk* s x))
a (walk* r (walk* s a))]
;; Filter constraints unrelated to reified variables.
(when (and (symbol? a) (empty? (->> (list x) flatten (filter lvar?))))
(symbol (str a "#" x)))))
clojure.core.logic.IRunnable
(runnable? [_ s]
(let [a (walk* s a)
x (walk* s x)]
(if (lvar? a)
(or
(and (lvar? x) (= x a))
(and (tree-term? x) (not (tie? x))))
(or
(not (nom? a))
(not (lvar? x))))))
clojure.core.logic.IConstraintWatchedStores
(watched-stores [this] #{::clojure.core.logic/subst}))))
(defn hash [a t]
(cgoal (-hash a t)))
;; =============================================================================
;; Suspensions as constraints
(defn- -do-suspc [t1 t2 swap a]
(when (loop [vs #{t2} seen #{}]
(let [vs (clojure.set/difference vs seen)]
(cond
(empty? vs)
true
(some #(occurs-check a % t1) vs)
false
:else
(let [vs2 (apply clojure.set/union
(map (fn [x] (if (nil? x) #{} x))
(map #(get-dom a (root-var a %) ::nom) vs)))
seen (clojure.set/union vs seen)]
(recur vs2 seen)))))
(let [[t1 a] (swap-noms t1 swap a)]
(bind a (== t1 t2)))))
(defn -suspc
([v1 v2 swap] (-suspc v1 v2 swap nil))
([v1 v2 swap _id]
(reify
Object
(toString [_]
(str "suspc" v1 v2 swap))
clojure.lang.IFn
(invoke [c a]
(bind* a
(fn [a]
(let [t1 (walk a v1)
t2 (walk a v2)]
(cond
(not (lvar? t1))
(-do-suspc t1 t2 swap a)
(not (lvar? t2))
(-do-suspc t2 t1 swap a)
:else
(do (assert (= t1 t2))
(loop [a* swap
a a]
(if (empty? a*) a
(recur (rest a*) (bind a (hash (first a*) t2)))))))))
(remcg c)))
clojure.core.logic.IConstraintId
(id [this] _id)
clojure.core.logic.IWithConstraintId
(with-id [this _id]
(-suspc v1 v2 swap _id))
clojure.core.logic.IConstraintOp
(rator [_] `suspc)
(rands [_] [v1 v2])
clojure.core.logic.IReifiableConstraint
(reifyc [c v r a]
(let [t1 (walk* r (walk* a v1))
t2 (walk* r (walk* a v2))
swap (walk* r swap)]
(when (and
(not (lvar? t1))
(not (lvar? t2))
(symbol? (first swap))
(symbol? (second swap)))
`(~'swap ~swap ~t1 ~t2))))
clojure.core.logic.IRunnable
(runnable? [_ a]
(let [t1 (walk a v1)
t2 (walk a v2)]
(or (not (lvar? t1)) (not (lvar? t2)) (= t1 t2))))
clojure.core.logic.IConstraintWatchedStores
(watched-stores [this] #{::clojure.core.logic/subst}))))
(defn suspc [v1 v2 swap]
(cgoal (-suspc v1 v2 swap)))
;; =============================================================================
;; tie: bind a nom in a term
(declare tie)
(deftype Tie [binding-nom body _meta]
clojure.core.logic.ITreeTerm
Object
(toString [_]
(str "<tie:" binding-nom "." body ">"))
(hashCode [_]
(.hashCode body))
(equals [this o]
(and (.. this getClass (isInstance o))
(and (= binding-nom (:binding-nom o)) (= body (:body o)))))
clojure.lang.IObj
(withMeta [this new-meta]
(Tie. binding-nom body _meta))
(meta [this]
_meta)
clojure.lang.ILookup
(valAt [this k]
(.valAt this k nil))
(valAt [_ k not-found]
(case k
:binding-nom binding-nom
:body body
not-found))
clojure.core.logic.IUnifyTerms
(unify-terms [v u s]
(cond
(tie? u)
(if (= (:binding-nom v) (:binding-nom u))
(unify s (:body v) (:body u))
(let [[t s] (swap-noms (:body v) [(:binding-nom v) (:binding-nom u)] s)]
(bind* s
(hash (:binding-nom u) (:body v))
(== t (:body u)))))
:else nil))
clojure.core.logic.IReifyTerm
(reify-term [v s]
(let [s (-reify* s binding-nom)]
(let [s (-reify* s body)]
s)))
clojure.core.logic.IWalkTerm
(walk-term [v f]
(with-meta
(tie (walk-term (:binding-nom v) f)
(walk-term (:body v) f))
(meta v)))
clojure.core.logic.IOccursCheckTerm
(occurs-check-term [v x s]
(occurs-check s x (:body v)))
clojure.core.logic.IConstrainTree
(-constrain-tree [t fc s]
(fc (:body t) s))
clojure.core.logic.IForceAnswerTerm
(-force-ans [v x]
(force-ans (:body v)))
INomSwap
(swap-noms [t swap s]
(let [[tbody s] (swap-noms (:body t) swap s)]
[(with-meta (tie (nom-swap (:binding-nom t) swap) tbody) (meta t))
s])))
(defn tie [binding-nom body]
(Tie. binding-nom body nil))
(defn tie? [x]
(instance? clojure.core.logic.nominal.Tie x))
(defmethod print-method Tie [x ^Writer writer]
(.write writer (str " [" (:binding-nom x) "] "))
(print-method (:body x) writer))