Fork me on GitHub
#datomic
<
2022-01-14
>
enn16:01:17

Is there a way to express something like a case in Datalog? That is, I want to choose one of a number of different sets of clauses to use in the query by comparing a logic variable against a number of value literals.

Lennart Buit17:01:42

You can ‘or-join’, and in each branch do something like this:

(and [(identity ?condition) :SENTINEL] …)

favila17:01:23

This is better (and [(ground :SENTINEL) ?condition] …) but the same thing

👍 2
favila17:01:56

(or-join [?dispatch-val ?out]
         (and [(ground :case1) ?dispatch-val]
              [(ground 1) ?out])
         (and [(ground :case2) ?dispatch-val]
              [(ground 2) ?out]))

favila17:01:49

or as named rules:

[[(myrule ?dispatch-val ?out)
  [(ground :case1) ?dispatch-val]
  [(ground 1) ?out]]
 [(myrule ?dispatch-val ?out)
  [(ground :case2) ?dispatch-val]
  [(ground 2) ?out]]]

favila17:01:27

The basic approach is just to implement a rule multiple times, and have some condition in each implementation that makes them non-overlapping for a given input

Lennart Buit17:01:25

^important to realize: the results of the branches of an or-join are union’ed, that's why you need to make them disjoint. (I’ve fallen in that hole before :p)

enn18:01:01

Thanks, this makes sense. Much appreciated.