-
Notifications
You must be signed in to change notification settings - Fork 9
/
unify.clj
358 lines (280 loc) · 10.9 KB
/
unify.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
; Copyright (c) Rich Hickey. All rights reserved.
; The use and distribution terms for this software are covered by the
; Eclipse Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php)
; which can be found in the file epl-v10.html at the root of this distribution.
; By using this software in any fashion, you are agreeing to be bound by
; the terms of this license.
; You must not remove this notice, or any other, from this software.
(ns ^{:doc "A unification library for Clojure."
:author "Michael Fogus"}
clojure.core.unify
(:require [clojure.zip :as zip]
[clojure.walk :as walk]))
(defn ignore-variable? [sym] (= '_ sym))
;; ### Utilities
(def lvar? #(or (ignore-variable? %)
(and (symbol? %) (re-matches #"^\?.*" (name %)))))
(defn extract-lvars
"Takes a datastructure and returns a distinct set of the logical
variables found within."
([form]
(extract-lvars lvar? form))
([lv-fn form]
(set
(walk/walk #(when (lv-fn %) %)
#(keep identity %)
form))))
(defn- composite?
"Taken from the old `contrib.core/seqable?`. Since the meaning of 'seqable' is
questionable, I will work on phasing it out and using a more meaningful
predicate. At the moment, the only meaning of `composite?` is:
Returns true if `(seq x)` will succeed, false otherwise."
[x]
(or (coll? x)
(nil? x)
(instance? Iterable x)
(-> x class .isArray)
(string? x)
(instance? java.util.Map x)))
(declare garner-unifiers)
(defn- occurs?
"Does v occur anywhere inside expr?"
[variable? v expr binds]
(loop [z (zip/zipper composite? seq #(do % %2) [expr])]
(let [current (zip/node z)]
(cond
(zip/end? z) false
(= v current) true
(and (variable? current)
(find binds current))
(recur (zip/next (zip/insert-right z (binds current))))
(zip/end? z) false
:else (recur (zip/next z))))))
(defn- bind-phase
[binds variable expr]
(if (or (nil? expr)
(ignore-variable? variable))
binds
(assoc binds variable expr)))
(defn- determine-occursness
[want-occurs? variable? v expr binds]
(if want-occurs?
`(if (occurs? ~variable? ~v ~expr ~binds)
(throw (IllegalStateException. (str "Cycle found in the path " ~expr)))
(bind-phase ~binds ~v ~expr))
`(bind-phase ~binds ~v ~expr)))
(defmacro create-var-unification-fn
[want-occurs?]
(let [varp (gensym)
v (gensym)
expr (gensym)
binds (gensym)]
`(fn ~'var-unify
[~varp ~v ~expr ~binds]
(if-let [vb# (~binds ~v)]
(garner-unifiers ~varp vb# ~expr ~binds)
(if-let [vexpr# (and (~varp ~expr) (~binds ~expr))]
(garner-unifiers ~varp ~v vexpr# ~binds)
~(determine-occursness want-occurs? varp v expr binds))))))
(def ^{:doc "Unify the variable v with expr. Uses the bindings supplied and possibly returns an extended bindings map."
:private true}
unify-variable (create-var-unification-fn true))
(def ^{:doc "Unify the variable v with expr. Uses the bindings supplied and possibly returns an extended bindings map."
:private true}
unify-variable- (create-var-unification-fn false))
(defn wildcard? [form]
(and (composite? form)
(#{'&} (first form))))
(defn- garner-unifiers
"Attempt to unify x and y with the given bindings (if any). Potentially returns a map of the
unifiers (bindings) found. Will throw an `IllegalStateException` if the expressions
contain a cycle relationship. Will also throw an `IllegalArgumentException` if the
sub-expressions clash."
([x y] (garner-unifiers unify-variable lvar? x y {}))
([variable? x y] (garner-unifiers unify-variable variable? x y {}))
([variable? x y binds] (garner-unifiers unify-variable variable? x y binds))
([uv-fn variable? x y binds]
(cond
(not binds) nil
(= x y) binds
(variable? x) (uv-fn variable? x y binds)
(variable? y) (uv-fn variable? y x binds)
(wildcard? x) (uv-fn variable? (second x) (seq y) binds)
(wildcard? y) (uv-fn variable? (second y) (seq x) binds)
(every? composite? [x y]) (garner-unifiers uv-fn
variable?
(rest x)
(rest y)
(garner-unifiers uv-fn
variable?
(first x)
(first y)
binds)))))
(defn flatten-bindings
"Flattens recursive bindings in the given map to the same ground (if possible)."
([binds] (flatten-bindings lvar? binds))
([variable? binds]
(into {}
(remove (comp nil? second)
(map (fn [[k v]]
[k (loop [v v]
(if (variable? v)
(recur (binds v))
v))])
binds)))))
(defn- try-subst
"Attempts to substitute the bindings in the appropriate locations in the given expression."
[variable? x binds]
{:pre [(map? binds) (fn? variable?)]}
(walk/prewalk (fn [expr]
(if (variable? expr)
(or (binds expr) expr)
expr))
x))
(defn- unifier*
"Attempts the entire unification process from garnering the bindings to substituting
the appropriate bindings."
([x y] (unifier* lvar? x y))
([variable? x y]
(unifier* variable? x y (garner-unifiers variable? x y)))
([variable? x y binds]
(->> binds
(flatten-bindings variable?)
(try-subst variable? y)))) ;; y is arbitrary
;; # PUBLIC API
;; ## OCCURS
(defn make-occurs-unify-fn
"Given a function to recognize unification variables, returns a function to
return a bindings map for two expressions. This function uses an 'occurs check'
methodology for detecting cycles."
[variable-fn]
(fn
([x y] (garner-unifiers unify-variable variable-fn x y {}))
([x y binds] (garner-unifiers unify-variable variable-fn x y binds))))
(defn make-occurs-subst-fn
"Given a function to recognize unification variables, returns a function that
will attempt to substitute unification bindings between two expressions.
This function uses an 'occurs check' methodology for detecting cycles."
[variable-fn]
(partial try-subst variable-fn))
(defn make-occurs-unifier-fn
"Given a function to recognize unification variables, returns a function to
perform the unification of two expressions. This function uses an 'occurs check'
methodology for detecting cycles."
[variable-fn]
(partial unifier* variable-fn))
(def ^{:doc (str (:doc (meta #'garner-unifiers))
" Note: This function is implemented with an occurs-check.")
:arglists '([expression1 expression2])}
unify (make-occurs-unify-fn lvar?))
(def ^{:doc (:doc (meta #'try-subst))
:arglists '([expression bindings])}
subst (make-occurs-subst-fn lvar?))
(def ^{:doc (str (:doc (meta #'unifier*))
" Note: This function is implemented with an occurs-check.")
:arglists '([expression1 expression2])}
unifier (make-occurs-unifier-fn lvar?))
;; ## NO OCCURS
(defn make-unify-fn
"Given a function to recognize unification variables, returns a function to
return a bindings map for two expressions."
[variable-fn]
(fn
([x y] (garner-unifiers unify-variable- variable-fn x y {}))
([x y binds] (garner-unifiers unify-variable- variable-fn x y binds))))
(defn make-subst-fn
"Given a function to recognize unification variables, returns a function that
will attempt to substitute unification bindings between two expressions."
[variable-fn]
(partial try-subst variable-fn))
(defn make-unifier-fn
"Given a function to recognize unification variables, returns a function to
perform the unification of two expressions."
[variable-fn]
(fn [x y]
(unifier* variable-fn
x
y
(garner-unifiers unify-variable- variable-fn x y {}))))
(def ^{:doc (str (:doc (meta #'garner-unifiers))
" Note: This function is implemented **without** an occurs-check.")
:arglists '([expression1 expression2])}
unify- (make-unify-fn lvar?))
(def ^{:doc (str (:doc (meta #'unifier*))
" Note: This function is implemented **without** an occurs-check.")
:arglists '([expression1 expression2])}
unifier- (make-unifier-fn lvar?))
(comment
(def T '[?a ?b])
(defn make-matcher
[root]
(fn [tmpl req]
(-> req
:url
(.replace root "")
(.split "/")
seq
(unify tmpl))))
(def match (make-matcher "http://foo.com/foo/"))
(match T {:url "http://foo.com/foo/fee/fi"})
;;=> {?a "fee", ?b "fi"}
(match T {:url "http://foo.com/foo/flub"})
;;=> {?a "flub"}
(defn make-genr
[root]
(fn [tmpl binds]
(->> binds
(subst tmpl)
(interpose "/")
(cons root)
(apply str))))
(def gen (make-genr "http://foo.com/foo/"))
(gen T '{?a 1, ?b 2})
;;=> "http://foo.com/foo/1/2"
(unify {:first '?first
:last '?last
:genre :giallo}
{:first "Dario"
:last "Argento"
:genre :giallo})
;;=> {?first "Dario", ?last "Argento"}
(unifier ['?first "Argento"]
["Dario" '?last])
;;=> ["Dario" "Argento"]
(subst '[1 2 ?x ?y]
'{?x [3 4 ?y 6]})
;;=> [1 2 [3 4 ?y 6] ?y]
(unifier '[(?a * ?x | 2) + (?b * ?x) + ?c]
'[?z + (4 * 5) + 3])
;;=> [(?a * 5 | 2) + (4 * 5) + 3]
(unify '[(?a * ?x | 2) + (?b * ?x) + ?c]
'[?z + (4 * 5) + 3])
;;=> {?c 3, ?x 5, ?b 4, ?z (?a * ?x | 2)}
(unify '[(?a * ?x | 2) + (?b * ?x) + ?c]
'[(?a * 5 | 2) + (4 * 5) + 3])
;;=> {?c 3, ?b 4, ?x 5}
(unify '[(?a * 5 | 2) + (4 * 5) + 3]
'[?z + (4 * 5) + 3])
;;=> {?z (?a * 5 | 2)}
(= (subst '[?z + (4 * 5) + 3]
'{?c 3, ?b 4, ?x 5
?z (?a * 5 | 2)})
(subst '[(?a * ?x | 2) + (?b * ?x) + ?c]
'{?c 3, ?b 4, ?x 5
?z (?a * 5 | 2)}))
;;=> true
(subst '[(?a * ?x | 2) + (?b * ?x) + ?c]
'{?c 3, ?b 4, ?x 5})
;;=> [(?a * 5 | 2) + (4 * 5) + 3]
(unify [1 2 3] '[?x & ?more])
;;=> {?more (2 3), ?x 1}
(unify [1 2 3] '[_ _ _ & ?more])
;;=> {}
(unify [1 2 3 4 5] '[_ _ _ & ?more])
;;=> {?more (4 5)}
(unify [1 2 3 4 5] '[_ ?b _ & ?more])
;;=> {?more (4 5), ?b 2}
(unify [:foo 1 2] '[?head & _])
;;=> {?head :foo}
(unifier [1 2 3] '[?x & ?more])
)