datascript

Immutable database and Datalog query engine for Clojure, ClojureScript and JS
2018-11-02T02:41:26.007800Z

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?

2018-11-02T02:43:37.008200Z

I feel like I'm missing something obvious

bahulneel 2018-11-09T15:03:22.014800Z

You need some kind of recursion

bahulneel 2018-11-09T15:05:21.015Z

here's the classic example

bahulneel 2018-11-09T15:05:27.015200Z

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

2018-11-09T16:21:24.015500Z

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

bahulneel 2018-11-09T16:27:11.015700Z

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

bahulneel 2018-11-09T16:28:23.015900Z

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

bahulneel 2018-11-09T16:28:39.016100Z

Also ?b is unbound in you first rule

2018-11-10T10:17:56.016300Z

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

2018-11-10T10:19:03.016500Z

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

ClashTheBunny 2018-11-02T12:08:31.010700Z

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...

2018-11-02T12:14:14.011300Z

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

ClashTheBunny 2018-11-02T13:36:07.011500Z

Good to know!

isak 2018-11-02T20:34:12.012700Z

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?

kardan 2018-11-02T20:41:04.012900Z

saf

2018-11-02T21:03:19.013Z

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.

2018-11-02T21:05:32.013200Z

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].

2018-11-02T21:05:55.013400Z

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

2018-11-02T21:06:11.013600Z

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

2018-11-02T21:07:05.013800Z

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!

isak 2018-11-02T21:52:12.014Z

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