datascript

Immutable database and Datalog query engine for Clojure, ClojureScript and JS
urzds 2019-05-17T10:18:40.138500Z

Does DataScript have a set difference operator?

urzds 2019-05-17T10:19:16.138900Z

i.e. every element of result set A that is not also in result set B?

2019-05-17T12:22:05.139900Z

In this rule:

[(subproject-or-self ?e1 ?e2)
     [?e1]
     (subproject ?e1 ?e2)]]
What does [?e1] do? I’ve yet to encounter that syntax in my limited experimentations with Datascript.

urzds 2019-05-17T15:21:09.140800Z

@d4hines The intention was that subproject-or-self evaluates to either ?e1 or subprojects thereof.

urzds 2019-05-17T15:21:28.141200Z

Though I just realise it should be [?e2] instead.

2019-05-17T16:42:36.141900Z

@urzds What does that [ ] syntax do?

2019-05-17T16:42:54.142200Z

Does it just fill in attribute and value with blanks?

urzds 2019-05-17T16:44:22.143200Z

@d4hines I believe so. I just took the recursive example from the datascript readme and adapted it to my needs as appeared logical to me. Lucky me: It worked. 😉

urzds 2019-05-17T16:45:23.144700Z

I think rules exist to create relations automatically? So [?e1] is a relation. The one with AVT=blank. That's how I understand it, without being an expert.

2019-05-17T16:46:20.145800Z

That’s definitely how I would read that, but what does [?e1 _ _] buy you? That there’s an entity with that id? isn’t that implicit in the query?

urzds 2019-05-17T16:47:22.147Z

Well, I can tell you that (subproject ?e ?existing-project) finds you all subprojects ?e, but not including ?existing-project.

urzds 2019-05-17T16:47:47.147500Z

So I added another rule that would also match ?existing-project in addition to the subprojects the other rule found. I kept subproject and subproject-or-self separate in order to prevent funny stuff from happening (infinite recursions?), without having a specific reason.

2019-05-17T16:51:17.149700Z

I guess I’m not convinced that this query:

[(subproject-or-self ?e1 ?e2)
     [?e2]
     (subproject ?e1 ?e2)]]
could ever return a different set than this query:
[(subproject-or-self ?e1 ?e2)
     (subproject ?e1 ?e2)]]
Or indeed, that the addition of any form with the pattern [?e] could ever change any result set.

2019-05-17T16:51:41.150200Z

And I’m interested because if I’m wrong, I need to reevaluate how I think queries…evaluate.

urzds 2019-05-17T16:52:15.150600Z

Maybe I really should write some tests that check these assumptions...

urzds 2019-05-17T16:53:26.152300Z

How else would you answer my question from yesterday? How to make ?project the union of ?top-level-project and some list of [?subproject]s?

2019-05-17T17:26:24.154200Z

I would have tried

[(or [?project :unique-id ?top-level-project-id]   (subproject ?project ?top-level-project-id)]

2019-05-17T17:26:40.154600Z

But I’m new at this - that’s just a hunch.

2019-05-17T17:26:54.154900Z

You’ve gotta just try this at the REPL.

urzds 2019-05-17T17:34:11.155600Z

I think I got error messages regarding unbound variables when I did it with or. So I tried or-join, but that resulted in what I posted above.

2019-05-17T17:57:36.156400Z

Gotcha. Reading, the docs on or-join, that makes sense now. This has been a helpful excursion - thanks for sharing it with us!

2019-05-17T17:58:10.157Z

It’s probably just so much spam for the ol’ timers, but I know I learned something 😛