Fork me on GitHub
#clara
<
2017-02-16
>
Jarrod Taylor (Clojure team)21:02:12

Could anyone be so kind as to help me understand how variable definition works? This is the most concise example I have to illustrate my confusion.

(require '[clara.rules :as rule])

(rule/defrule wat
  [1 (= ?a "a")]
  [1 (= ?b "b")
   (nil? (println ?a))    ;; => "a"
   (nil? (println ?b))    ;; => "b"
   (nil? (println ?a ?b)) ;; => "a nil"
   ] => nil)

(-> (rule/mk-session [wat] :fact-type-fn (constantly 1))
    (rule/insert 1)
    (rule/fire-rules))

Jarrod Taylor (Clojure team)21:02:14

Why does ?b disappear when printed together with ?a?

wparker22:02:33

I wouldn’t expect that to happen, but I can see the behavior you describe when I drop your example in a REPL

wparker22:02:06

It is a weird case in that you’d normally expect the bindings to come off the actual fact in some way, but it still seems like incorrect behavior at first glance, probably merits a GitHub issue

Jarrod Taylor (Clojure team)22:02:33

I was experiencing the behavior with a more traditional rule in my application. This above was as too the point of an example to demonstrate it as possible. I will open a issue on GitHub.

zylox22:02:47

i know ive had issues when i try to refer to a binding in the same condition (might be the wrong term) where it was bound.

zylox22:02:04

but if i used the raw field version it worked fine

wparker23:02:33

I suspect that the reason adding ?a causes ?b to be different is that if you have ?a in the constraint it will become a join with the previous fact, and if it is absent it is a condition on a single fact

wparker23:02:56

Those are different paths in the compiler so one could have a bug the other doesn't

wparker23:02:37

Doing a quick test like

wparker23:02:42

` (rule/defrule wat
  [1 (= ?a "a")]
  [1 (= ?b "b")
   (and (nil? (println ?a))
         (nil? (println ?b))) 
   ] => nil) 
`

wparker23:02:59

makes b be nil as well

wparker23:02:18

with ?a and ?b in the same constraint

wparker23:02:54

@zylox : a “condition” contains constraints, that is you can have a condition like [FactType constraint1 constraint2]