datomic

Ask questions on the official Q&A site at https://ask.datomic.com!
joshkh 2021-06-17T10:45:26.232900Z

hey that's a great idea, thanks @favila

vlaaad 2021-06-17T14:53:46.234500Z

Anyone doing something like that for "dynamic" rules?

(defmacro eval-rules [form]
  (walk/prewalk
    (fn [e]
      (if (and (seq? e) (= `unquote (first e)))
        (eval (second e))
        e))
    form))

(eval-rules
  '[[(reverse-edge ?from-concept ?type ?to-concept ?relation)
     [(ground ~(set/map-invert some-config-map))
      [[?type ?reverse-type]]]
     [?relation :relation/concept-2 ?from-concept]
     [?relation :relation/type ?reverse-type]
     [?relation :relation/concept-1 ?to-concept]]])
I'm trying to create rules configured from some code...

vlaaad 2021-06-17T14:54:32.235300Z

...and I don't want to pass this config map in addition to rules to queries since it's static. Any advice how to do that?

Joe Lane 2021-06-17T15:06:46.235400Z

(let [some-config-map {:bar :foo :baz :bin}]
  [['(reverse-edge ?from-concept ?type ?to-concept ?relation)
     [(list 'ground (clojure.set/map-invert some-config-map))
      '[[?type ?reverse-type]]]
     '[?relation :relation/concept-2 ?from-concept]
     '[?relation :relation/type ?reverse-type]
     '[?relation :relation/concept-1 ?to-concept]]])
For more inspiration See the https://github.com/Datomic/mbrainz-sample/blob/master/src/clj/datomic/samples/mbrainz/rules.clj .

2021-06-17T15:07:35.235700Z

yeah โ€œjust quote the quoted bitsโ€ seems most straightforward to me

vlaaad 2021-06-17T15:17:51.235900Z

That's a lot of quoting...

Joe Lane 2021-06-17T15:22:06.236100Z

If you come up with something you REALLY like @vlaaad I'd be interested in seeing it.

vlaaad 2021-06-17T15:23:00.236300Z

Well, I've been thinking about it for a couple is hours, and so far eval-rules is the best thing I have...

vlaaad 2021-06-17T15:25:43.236500Z

Other things I considered is what you suggested, macro that does the same thing as eval-rules, but that was much more cumbersome than fn version, and syntax quoting with a lot of ~'?type -like symbols

vlaaad 2021-06-17T15:28:47.236700Z

Ah, I also tried #=, but I couldn't reference the config-map with it, it was interpreted as symbol

Joe Lane 2021-06-17T15:33:01.236900Z

Ha, first rule of #= club, don't talk about #= club ๐Ÿ˜‰

vlaaad 2021-06-17T15:34:18.237200Z

This #= club membership didn't bear any fruits so far...