Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
LOGIC-53: core.logic converts defrecords
records must extend `IUninitialized`. They always trump maps.
  • Loading branch information
swannodette committed Jan 3, 2013
1 parent 5013e1c commit bd3f13a
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 17 deletions.
39 changes: 22 additions & 17 deletions src/main/clojure/clojure/core/logic.clj
Expand Up @@ -25,6 +25,9 @@
(defn dissoc-dom [x k]
(assoc x :doms (dissoc (:doms x) k)))

(defn record? [x]
(instance? clojure.lang.IRecord x))

;; =============================================================================
;; Marker Interfaces

Expand Down Expand Up @@ -1667,18 +1670,24 @@

clojure.lang.IPersistentMap
(walk-term [v f]
(with-meta
;; TODO: call empty here on v to preserve the type
;; we were given, we can have the transient bit
;; for the cases where we have a concrete Clojure map
;; type, and just usy empty + assoc for everything else
(loop [v v r (transient {})]
(if (seq v)
(let [[vfk vfv] (first v)]
(recur (next v) (assoc! r (walk-term (f vfk) f)
(walk-term (f vfv) f))))
(persistent! r)))
(meta v))))
(if (record? v)
(walk-record-term v f)
(with-meta
;; TODO: call empty here on v to preserve the type
;; we were given, we can have the transient bit
;; for the cases where we have a concrete Clojure map
;; type, and just usy empty + assoc for everything else
(loop [v v r (transient {})]
(if (seq v)
(let [[vfk vfv] (first v)]
(recur (next v) (assoc! r (walk-term (f vfk) f)
(walk-term (f vfv) f))))
(persistent! r)))
(meta v))))

clojure.lang.IRecord
(walk-term [v f]
(walk-record-term v f)))

;; =============================================================================
;; Occurs Check Term
Expand Down Expand Up @@ -4029,11 +4038,7 @@
nil))

IUninitialized
(-uninitialized [_] (PMap.))

IWalkTerm
(walk-term [v f]
(walk-record-term v f)))
(-uninitialized [_] (PMap.)))

(defn partial-map
"Given map m, returns partial map that unifies with maps even if it
Expand Down
10 changes: 10 additions & 0 deletions src/test/clojure/clojure/core/logic/tests.clj
Expand Up @@ -1375,6 +1375,16 @@
(<fd d a))
'([2 3 0 1]))))

(defrecord RecordTest [a b]
IUninitialized
(-uninitialized [_]
(RecordTest. nil nil)))

(deftest test-53-lossy-records
(is (= (run* [q]
(== q (RecordTest. 1 2)))
(list #clojure.core.logic.tests.RecordTest{:a 1, :b 2}))))

;; =============================================================================
;; cKanren

Expand Down

0 comments on commit bd3f13a

Please sign in to comment.