Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Added tests for range variables. Last release before larger perf changes

  • Loading branch information...
commit 2869b3a2ef1041a8170c1a4e0f08f4f09630a185 1 parent 2d0f0fb
@fogus fogus authored
View
2  pom.xml
@@ -18,7 +18,7 @@
<parent>
<groupId>org.clojure</groupId>
<artifactId>pom.contrib</artifactId>
- <version>0.0.20</version>
+ <version>0.0.21</version>
</parent>
<developers>
View
18 src/main/clojure/clojure/core/unify.clj
@@ -14,7 +14,7 @@
(defn ignore-variable? [sym] (= '_ sym))
-(def *variablep* #(or (ignore-variable? %)
+(def VARIABLE? #(or (ignore-variable? %)
(and (symbol? %) (re-matches #"^\?.*" (name %)))))
(defn- composite?
@@ -90,7 +90,7 @@
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 *variablep* x y))
+ ([x y] (garner-unifiers VARIABLE? x y))
([variable? x y] (garner-unifiers variable? x y {}))
([variable? x y binds] (garner-unifiers unify-variable variable? x y binds))
([uv-fn variable? x y binds]
@@ -112,7 +112,7 @@
(defn- subst-bindings
"Flattens recursive bindings in the given map."
- ([binds] (subst-bindings *variablep* binds))
+ ([binds] (subst-bindings VARIABLE? binds))
([variable? binds]
(into {} (map (fn [[k v]]
[k (loop [v v]
@@ -133,7 +133,7 @@
(defn- unifier*
"Attempts the entire unification process from garnering the bindings to substituting
the appropriate bindings."
- ([x y] (unifier* *variablep* x y))
+ ([x y] (unifier* VARIABLE? x y))
([variable? x y]
(unifier* variable? x y (garner-unifiers variable? x y)))
([variable? x y binds]
@@ -170,16 +170,16 @@
(def ^{:doc (str (:doc (meta #'garner-unifiers))
" Note: This function is implemented with an occurs-check.")
:arglists '([expression1 expression2])}
- unify (make-occurs-unify-fn *variablep*))
+ unify (make-occurs-unify-fn VARIABLE?))
(def ^{:doc (:doc (meta #'try-subst))
:arglists '([expression bindings])}
- subst (make-occurs-subst-fn *variablep*))
+ subst (make-occurs-subst-fn VARIABLE?))
(def ^{:doc (str (:doc (meta #'unifier*))
" Note: This function is implemented with an occurs-check.")
:arglists '([expression1 expression2])}
- unifier (make-occurs-unifier-fn *variablep*))
+ unifier (make-occurs-unifier-fn VARIABLE?))
;; ## NO OCCURS
@@ -209,10 +209,10 @@
(def ^{:doc (str (:doc (meta #'garner-unifiers))
" Note: This function is implemented **without** an occurs-check.")
:arglists '([expression1 expression2])}
- unify- (make-unify-fn *variablep*))
+ unify- (make-unify-fn VARIABLE?))
(def ^{:doc (str (:doc (meta #'unifier*))
" Note: This function is implemented **without** an occurs-check.")
:arglists '([expression1 expression2])}
- unifier- (make-unifier-fn *variablep*))
+ unifier- (make-unifier-fn VARIABLE?))
View
16 src/test/clojure/clojure/core/unify/tests.clj
@@ -12,7 +12,7 @@
(:use [clojure.core.unify] :reload-all)
(:use [clojure.test]))
-(def *caps* #(and (symbol? %) (Character/isUpperCase (first (name %)))))
+(def CAPS #(and (symbol? %) (Character/isUpperCase (first (name %)))))
(deftest test-garner-unifiers
(is (= {} (#'clojure.core.unify/garner-unifiers '(a b) '(a b))))
@@ -36,12 +36,12 @@
(is (= '{?z (g ?x), ?y (g ?x)} (#'clojure.core.unify/garner-unifiers '(f (g ?x) ?y) '(f ?y ?z))))
(is (= '{?a a} (#'clojure.core.unify/garner-unifiers '?a 'a)))
(is (= '{?y :bar, ?x :foo} (#'clojure.core.unify/garner-unifiers '{?x 42 ?y 108} '{:foo 42 :bar 108})))
- (is (= '{B 2, A 1} (#'clojure.core.unify/garner-unifiers *caps* '(A B) '(1 2))))
- (is (= '{Bar 2, Foo 1} (#'clojure.core.unify/garner-unifiers *caps* '(Foo Bar) '(1 2))))
+ (is (= '{B 2, A 1} (#'clojure.core.unify/garner-unifiers CAPS '(A B) '(1 2))))
+ (is (= '{Bar 2, Foo 1} (#'clojure.core.unify/garner-unifiers CAPS '(Foo Bar) '(1 2))))
(is (= '{?y a, ?x ?y} (#'clojure.core.unify/garner-unifiers '(?x ?y a) '(?y ?x ?x)))))
-#_(deftest test-range-variables
+(deftest test-range-variables
(is (= '{?x 1 ?y (2 3)} (#'clojure.core.unify/garner-unifiers '(?x & ?y) [1 2 3])))
(is (= '{?x 1 ?y 2 ?z (3)} (#'clojure.core.unify/garner-unifiers '(?x ?y & ?z) [1 2 3])))
(is (= '{?x 1 ?y ()} (#'clojure.core.unify/garner-unifiers '(?x & ?y) [1]))))
@@ -49,16 +49,16 @@
(deftest test-ignore-variables
(is (= '{?x 1 ?y 3} (#'clojure.core.unify/garner-unifiers '(?x _ ?y) [1 2 3])))
(is (= '{} (#'clojure.core.unify/garner-unifiers '(_ _ _) [1 2 3])))
- #_(is (= '{?x 1} (#'clojure.core.unify/garner-unifiers '(?x & _) [1 2 3])))
- #_(is (= '{?y (2 3)} (#'clojure.core.unify/garner-unifiers '(_ & ?y) [1 2 3]))))
+ (is (= '{?x 1} (#'clojure.core.unify/garner-unifiers '(?x & _) [1 2 3])))
+ (is (= '{?y (2 3)} (#'clojure.core.unify/garner-unifiers '(_ & ?y) [1 2 3]))))
(deftest test-subst-bindings
(is (= '{?y a, ?x a} (#'clojure.core.unify/subst-bindings '{?y a, ?x ?y}))))
(deftest test-unifier*
- (is (= '(a a a) (#'clojure.core.unify/unifier* *caps* '(X Y a) '(Y X X) '{Y a, X a})))
- (is (= '(a a a) (#'clojure.core.unify/unifier* *caps* '(X Y a) '(Y X X))))
+ (is (= '(a a a) (#'clojure.core.unify/unifier* CAPS '(X Y a) '(Y X X) '{Y a, X a})))
+ (is (= '(a a a) (#'clojure.core.unify/unifier* CAPS '(X Y a) '(Y X X))))
(is (= '((?a * 5 ** 2) + (4 * 5) + 3) (#'clojure.core.unify/unifier* '((?a * ?x ** 2) + (?b * ?x) + ?c) '(?z + (4 * 5) + 3)))))
Please sign in to comment.
Something went wrong with that request. Please try again.