Skip to content
Browse files

LOGIC-53: core.logic converts defrecords

records must extend `IUninitialized`. They always trump maps.
  • Loading branch information...
1 parent 5013e1c commit bd3f13a1cd3214e5e15e52383b49f0a54ec8a502 @swannodette swannodette committed
Showing with 32 additions and 17 deletions.
  1. +22 −17 src/main/clojure/clojure/core/logic.clj
  2. +10 −0 src/test/clojure/clojure/core/logic/tests.clj
View
39 src/main/clojure/clojure/core/logic.clj
@@ -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
@@ -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
@@ -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
View
10 src/test/clojure/clojure/core/logic/tests.clj
@@ -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

0 comments on commit bd3f13a

Please sign in to comment.
Something went wrong with that request. Please try again.