Skip to content

Commit bd3f13a

Browse files
committed
LOGIC-53: core.logic converts defrecords
records must extend `IUninitialized`. They always trump maps.
1 parent 5013e1c commit bd3f13a

File tree

2 files changed

+32
-17
lines changed

2 files changed

+32
-17
lines changed

src/main/clojure/clojure/core/logic.clj

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@
2525
(defn dissoc-dom [x k]
2626
(assoc x :doms (dissoc (:doms x) k)))
2727

28+
(defn record? [x]
29+
(instance? clojure.lang.IRecord x))
30+
2831
;; =============================================================================
2932
;; Marker Interfaces
3033

@@ -1667,18 +1670,24 @@
16671670

16681671
clojure.lang.IPersistentMap
16691672
(walk-term [v f]
1670-
(with-meta
1671-
;; TODO: call empty here on v to preserve the type
1672-
;; we were given, we can have the transient bit
1673-
;; for the cases where we have a concrete Clojure map
1674-
;; type, and just usy empty + assoc for everything else
1675-
(loop [v v r (transient {})]
1676-
(if (seq v)
1677-
(let [[vfk vfv] (first v)]
1678-
(recur (next v) (assoc! r (walk-term (f vfk) f)
1679-
(walk-term (f vfv) f))))
1680-
(persistent! r)))
1681-
(meta v))))
1673+
(if (record? v)
1674+
(walk-record-term v f)
1675+
(with-meta
1676+
;; TODO: call empty here on v to preserve the type
1677+
;; we were given, we can have the transient bit
1678+
;; for the cases where we have a concrete Clojure map
1679+
;; type, and just usy empty + assoc for everything else
1680+
(loop [v v r (transient {})]
1681+
(if (seq v)
1682+
(let [[vfk vfv] (first v)]
1683+
(recur (next v) (assoc! r (walk-term (f vfk) f)
1684+
(walk-term (f vfv) f))))
1685+
(persistent! r)))
1686+
(meta v))))
1687+
1688+
clojure.lang.IRecord
1689+
(walk-term [v f]
1690+
(walk-record-term v f)))
16821691

16831692
;; =============================================================================
16841693
;; Occurs Check Term
@@ -4029,11 +4038,7 @@
40294038
nil))
40304039

40314040
IUninitialized
4032-
(-uninitialized [_] (PMap.))
4033-
4034-
IWalkTerm
4035-
(walk-term [v f]
4036-
(walk-record-term v f)))
4041+
(-uninitialized [_] (PMap.)))
40374042

40384043
(defn partial-map
40394044
"Given map m, returns partial map that unifies with maps even if it

src/test/clojure/clojure/core/logic/tests.clj

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1375,6 +1375,16 @@
13751375
(<fd d a))
13761376
'([2 3 0 1]))))
13771377

1378+
(defrecord RecordTest [a b]
1379+
IUninitialized
1380+
(-uninitialized [_]
1381+
(RecordTest. nil nil)))
1382+
1383+
(deftest test-53-lossy-records
1384+
(is (= (run* [q]
1385+
(== q (RecordTest. 1 2)))
1386+
(list #clojure.core.logic.tests.RecordTest{:a 1, :b 2}))))
1387+
13781388
;; =============================================================================
13791389
;; cKanren
13801390

0 commit comments

Comments
 (0)