Fork me on GitHub
#datascript
<
2022-11-05
>
jjttjj16:11:41

I'm trying to learn how datascript/datomic rules work.

(def conn (ds/create-conn {}))
  
  (ds/transact! conn
    [{:id 1 :size 2}
     {:id 2 :size 3}])

  (def query '[:find ?size
               :in $ % ?id
               :where
               (size ?id ?size)])
  
  (ds/q query (ds/db conn)
    '[[(size ?id ?size)
       [?x :id ?id]
       [?x :size ?size]]]
    1)
  ;;returns #{[2]} (expected)

  (ds/q query (ds/db conn)
    '[[(size [?id] ?size) ;; require id be bound by enclosing in vector
       [?x :id ?id]
       [?x :size ?size]]]
    1)
  ;; returns #{[3] [2]}. not sure why.
  
Why does requiring the ?id variable be bound in the rule definition prevent the results from being narrowed with the ?id I'm passing in to the query?

Niki16:11:08

Weird, probably a bug

Lone Ranger21:11:43

I didn’t even know could do in-line rule queries like that :thinking_face:

Lone Ranger21:11:04

@U064UGEUQ what happens if you transact 3 diatoms there. Do you get all 3 back? If so, maybe [?id] is resolving truthy? Just a wild guess

RJ Sheperd16:12:16

Just ran into this issue. I’ve opened a ticket here: https://github.com/tonsky/datascript/issues/441

👍 1
Niki22:12:45

You know what, I don’t think we support this. There’s some work in the parser but it’s not used in current query impl. There are no tests on this either