-
-
Notifications
You must be signed in to change notification settings - Fork 309
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
rules aren't correctly unifying #336
Comments
Hi! To do this you need two rules: one finds all parents, the other only leaves the ones that are roots (have no :parent themselves). (db/q '[:find (pull ?root [*])
:in $ ?id %
:where [?e :id ?id]
(root ?e ?root)]
(-> (db/empty-db {:id {:db/unique :db.unique/identity}})
(db/db-with [{:id :a :root 1}
{:id :b :parent [:id :a]}
{:id :c :parent [:id :b]}
{:id :d :root 2}]))
:b
'[[(parent ?e ?p)
[?e :parent ?p]]
[(parent ?e ?p)
[?e :parent ?x]
(parent ?x ?p)]
[(root ?e ?r)
(parent ?e ?r)
(not [?r :parent])]])
;; ([{:db/id 1, :id :a, :root 1}]) |
Thank you! I can use that in my application. I still find the first 2 results unsatisfying and unsettling respectively:
|
Right now self-unification in rule signature is not supported in Datascript. Maybe it should, I don’t know. I’m not sure it’s supported by Datomic even.
Then body is confusing too
It means “Add all roots to the result set”. Which is not what you want: you want all parents INTERSECTED with all roots. This will UNIONIZE them. And then you pass |
Yes, that's what I was going for. Good to know, thanks. I couldn't figure out how to keep only the root and later realized unifying the rule arguments should do that. Your example with 2 rules works for this case, thanks.
IIUC that would happen only if
So the
Strange indeed, that was a typo. I'm thinking what is the correct step now with this issue? Both of my examples relied on unifying the rule arguments and if that's not supported maybe that should be checked and throw? |
It’s not about
In the shortest future, that would be a good warning sign, yes. Propert solution would be to implement self-unification, I guess. |
The problem: walk references until finding the root reference.
This example returns
{:id :a :root 1}
in the results as expected for datomic, but fails in datascript:This example throws an ArrayIndexOutOfBoundsException in datomic 🤷♂️ while in datascript returns too many results (however did it reach
{:id :d}
?):The text was updated successfully, but these errors were encountered: