Fork me on GitHub

Ok, I got considerably further, but now I want to write a rule to get all dependency-graph edges relevant for a particular id. I wrote

[(dep-for ?p ?a ?b)
      [(= ?p ?a)]
      (depends ?a ?b)]
     [(dep-for ?p ?a ?b)
      (depends ?p ?a)
      (depends ?a ?b)]
but it only returns matches for the second definition of dep-for, so I suspect that there's something wrong with my use of = here. Any idea what it might be?


I feel like I'm missing something obvious


You need some kind of recursion


here's the classic example


[(ancestor ?a ?b)
 (parent ?a ?b)]
[(ancestor ?a ?b)
 (parent ?a ?c)
 (ancestor ?c ?b)]


Thanks for replying, but I don't think it solves the problem I had, if you check my code, I have almost exactly the same structure, but what I need, in your example, is the list of all parent/child pairs, where the parent is either me or a descendant of me


What are ?p, ?a and ?b in dep-for


Also the difference between what I have and you is that you don't recur back to dep-for


Also ?b is unbound in you first rule


Yes, but that's deliberate, I want all pairs ?a ?b where ?a is a parent of ?b and either identical or a descendant of ?p


Why is it unbound? It's used in the body? Maybe it helps when I post the full code, will do that later when I'm back home


Does order matter? I feel like a question or two in learndatalogtoday were only wrong based on order of the statements. That doesn't make sense to me though...


Order shouldn't matter except as relates to performance details (which queries get run first, thereby reducing the amount of downstream query work which has to be done), at least as far as Datomic goes.


Hm, as far as I understood it, that only matters performance-wise and should not influence the result set


is it possible to filter on :db/id in :where with datalog queries ? Looks like no - I always get an empty result set if I do. Is there an alternative?


I guess I've never tried, but I would have expected it to work; Would be interesting to see what Datomic does here. In any case, you should basically never need to do this; If [?e :db/id ?id] matches a triple, then indeed, (= ?e ?id), so you can replace with [?e].


Which raises the question: why would you want to do this?


If ?e is being bound as a query input, then you already know what the eid is.


If it's being bound from another relation, like [?a :friend ?e], then why do you also need [?e] as a separate relation? You already know what the eid is; it's ?e!


@U05100J3V makes sense, thanks for the help. Changing it be a query input made it work.