datascript

2022-11-05T16:12:41.196879Z

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?

RJ Sheperd 2022-12-15T16:30:16.902889Z

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

👍 1
Niki 2022-12-15T22:36:45.757759Z

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

Niki 2022-11-05T16:46:08.147679Z

Weird, probably a bug

Lone Ranger 2022-11-05T21:42:43.222699Z

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

Lone Ranger 2022-11-05T21:45:14.993579Z

Oh I see

Lone Ranger 2022-11-05T21:47:04.289009Z

@jjttjj 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