Given the definitions below:
(ns ck.repro
(:require [clara.rules :as rules :refer [defsession defrule defquery]]))
(defrecord MakeStuff [stuff])
(defrecord Stuff [foo])
(defrule add-stuff
[:not [:exists [Stuff]]]
=>
(println "Add stuff")
(rules/insert! (->MakeStuff (->Stuff "foo"))))
(defrule make-stuff
[?make-stuff <- MakeStuff (= ?stuff stuff)]
=>
(println "Make stuff")
(rules/insert-unconditional! ?stuff))
(defquery make-stuff?
[]
(?make-stuff <- MakeStuff))
(defrule exists-stuff
[:exists [Stuff]]
=>
(println "Found stuff"))
(def session (-> (rules/mk-session 'ck.repro)
(rules/fire-rules)))
(println "make-stuff?" (rules/query session make-stuff?))
I get the following output:
Add stuff
Make stuff
Found stuff
make-stuff? ({:?make-stuff #ck.repro.MakeStuff{:stuff #ck.repro.Stuff{:foo foo}}})
I would have expected that the make-stuff rule would cause the MakeStuff fact inserted by add-stuff to be retracted by truth maintenance. Am I missing something?The make-stuff rule is using insert-unconditional! which will prevent the fact inserted from being retracted. Otherwise you would have an infinite loop with the above stated rules
bizarre…. At first blush i would say that something about the “Exists” accumulator is acting suspiciously in the retraction path. For example, written differently:
(r/defrule add-stuff
[:not [Stuff]]
=>
(println "Add stuff")
(r/insert! (->MakeStuff (->Stuff "foo"))))
Add stuff
Make stuff
Found stuff
make-stuff? ()additional sanity:
(r/defquery stuff?
[]
(?stuff <- Stuff))
nets the expected:
Add stuff
Make stuff
Found stuff
make-stuff? ()
stuff? ({:?stuff #Stuff{:foo foo}})@dave.dixon what version of clara are you running?
0.21.1