Fork me on GitHub
#datomic
<
2018-06-12
>
mg01:06:12

Is there a way to do equality in rules? My use case here is a rule for a hierarchy, where I'd want to match in the case that: the two args are equal, the second arg is a parent of the first (via :parent relationship), or the second arg is a grand-parent (or recursively great-grandparent) of the first arg. The parent and grandparent cases are pretty straightforward, it's that equal case that's tripping me up

mg01:06:17

I have now:

'[[(in-hierarchy ?child ?parent)
     [(= ?child ?parent)]]
    [(in-hierarchy ?child ?parent)
     [?child :parent ?parent]]
    [(in-hierarchy ?child ?parent)
     [?child :parent ?p1]
     (in-hierarchy ?p1 ?parent)]]
The clause with = is throwing, though

favila01:06:59

Throwing what?

mg01:06:15

IllegalArgumentExceptionInfo :db.error/insufficient-binding [?child] not bound in expression clause: [(= ?child ?parent)] datomic.error/arg (error.clj:57)

favila01:06:50

Ah, so where you are using that rule in your query child isn’t forced to be bound

favila01:06:22

Either force it (square bracket around args) or ensure it some other way

favila01:06:42

You might also be able to name both args the same and have no rule body?

favila01:06:54

Thus making it unify with itself

mg01:06:29

ahh that's interesting

favila01:06:32

No you still need to ensure binding

favila01:06:42

Both must be bound for an equality check

favila01:06:56

It’s in the semantic of what you are doing

mg01:06:41

I suppose I could do something hacky here and round-trip via a reference attribute

favila01:06:49

Do you mean “ancestor or self”?

favila01:06:30

Given a reference element, you want to match itself and all its parents transitively?

favila01:06:00

Three impala as you have here

favila01:06:25

All have first arg bound as the reference node

favila01:06:52

Your “equality” impl should just rename the arg using the identity fn

favila01:06:02

The other two impls are correct

favila01:06:27

[(identity ?ref) ?matched]

mg01:06:36

ah yes I see

mg01:06:29

there we go! thanks

favila01:06:46

No it took me a while to get the first time

favila01:06:25

That identity arg renaming trick was key but I don’t really see people talking about it

mg01:06:45

there doesn't seem to be a good set of reference examples for rules generally