Skip to content

Commit

Permalink
clean up featurec
Browse files Browse the repository at this point in the history
Fix horrible argument order. Update docstring for `partial-map`,
`partial-map` is no longer storable so cannot unify with logic
vars. Added docstring to `featurec`. Added `featurec` tests.
  • Loading branch information
swannodette committed Dec 27, 2012
1 parent 9b45999 commit 8c36a64
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 16 deletions.
29 changes: 13 additions & 16 deletions src/main/clojure/clojure/core/logic.clj
Original file line number Diff line number Diff line change
Expand Up @@ -4006,24 +4006,16 @@

(defn partial-map
"Given map m, returns partial map that unifies with maps even if it
doesn't share all of the keys of that map.
Only the keys of the partial map will be unified:
(run* [q]
(fresh [pm x]
(== pm (partial-map {:a x}))
(== pm {:a 1 :b 2})
(== pm q)))
;;=> ({:a 1})"
doesn't share all of the keys of that map."
[m]
(map->PMap m))

(defn partial-map? [x]
(instance? PMap x))

(defn -featurec
([fs x] (-featurec (partial-map fs) x nil))
([fs x _id]
([x fs] (-featurec x (partial-map fs) nil))
([x fs _id]
(reify
clojure.lang.IFn
(invoke [this a]
Expand All @@ -4034,15 +4026,15 @@
(id [this] _id)
IWithConstraintId
(with-id [this _id]
(-featurec fs x _id))
(-featurec x fs _id))
IConstraintOp
(rator [_] `featurec)
(rands [_] [fs x])
(rands [_] [x])
IReifiableConstraint
(reifyc [_ v r a]
(let [fs (into {} fs)
r (-reify* r (walk* a fs))]
`(featurec ~(walk* r fs) ~(walk* r x))))
`(featurec ~(walk* r x) ~(walk* r fs))))
IRelevant
(-relevant? [_ a] true)
IRunnable
Expand All @@ -4051,8 +4043,13 @@
IConstraintWatchedStores
(watched-stores [this] #{::subst}))))

(defn featurec [fs x]
(cgoal (-featurec fs x)))
(defn featurec
"Ensure that a map contains at least the key-value pairs
in the map fs. fs must be partially instantiated - that is,
it may contain values which are logic variables to support
feature extraction."
[x fs]
(cgoal (-featurec x fs)))

;; =============================================================================
;; defc
Expand Down
21 changes: 21 additions & 0 deletions src/test/clojure/clojure/core/logic/tests.clj
Original file line number Diff line number Diff line change
Expand Up @@ -2635,6 +2635,27 @@
(is (= (safefd)
'([4 3 1 8 9 2 6 7 5]))))

;; =============================================================================
;; Feature Constraints

(deftest test-featurec-1
(is (= (run* [q]
(featurec q {:foo 1}))
'((_0 :- (clojure.core.logic/featurec _0 {:foo 1})))))
(is (= (run* [q]
(featurec q {:foo 1})
(== q {:foo 1 :bar 2}))
'({:foo 1 :bar 2})))
(is (= (run* [q]
(featurec q {:foo 1})
(== q {:bar 2}))
()))
(is (= (run* [q]
(fresh [x]
(featurec x {:foo q})
(== x {:foo 1})))
'(1))))

;; =============================================================================
;; Deep Constraints

Expand Down

0 comments on commit 8c36a64

Please sign in to comment.