Fork me on GitHub
#core-logic
<
2022-12-26
>
Damien Kick16:12:23

So I've been playing with core.logic and Malli a bit <https://github.com/dkick/mallikanren>. For example, given a Malli spec, transforming it into ...

(def Name
  [:map
   [:first :string]
   [:middle [:maybe :string]]
   [:last :string]])

(def name!? (spock/transform Name))

(def Person
  [:map
   [:id :uuid]
   [:name Name]])

(def person!? (spock/transform Person))

(def Parents
  [:map
   [:child :uuid]
   [:father :uuid]
   [:mother :uuid]])

(def parents!? (spock/transform Parents))

(def Children
  [:map
   [:parent :uuid]
   [:child :uuid]])

(def children!? (spock/transform Children))

(comment
  (let [q nil]
    (l/run 10 [q]
      (l/or* (->> [name!? person!? parents!? children!?]
                  (map #(% q))))))
  ;; => ([[:first _0] [:middle _1] [:last _2]]
  ;;     [[:id _0] [:name _1]]
  ;;     [[:parent _0] [:child _1]]
  ;;     [[:child _0] [:father _1] [:mother _2]]
  ;;     [[:id _0] [:name [[:first _1] [:middle _2] [:last _3]]]])
  :comment)

Damien Kick16:12:15

I'm just curious ... has anyone else thought that there is interesting overlap between spec-like-things and core.logic? Are there any other examples of libraries doing this kind of thing?

Ben Sless16:12:35

I'm still in the hammock phases of using logical programming to model function schemas

Ben Sless16:12:56

I think it's a great fit, schemas can totally map to relations, and functions are just joins, which also produce relations

hiredman18:12:18

The big problem is core.logic can't do set relations

Damien Kick19:12:04

It is a bother, to be sure; but I’ve gotten some useful things done with alists as maps

Damien Kick19:12:15

… list lists as sets and weaving in/out of Clojure space, core.logic space

Ben Sless20:12:55

But the new branch of #CFFTD7R6Z can 🙂

hiredman22:12:22

I am not super familiar with meander, and while it may support some pattern matching on sets and maps, I am very skeptical that it encodes a theory of finite sets like you would need for proper handling of sets in core.logic (and set like things like map keys).

Ben Sless05:12:55

I think it does, I remember @U06MDAPTP sent me the papers on it. I can't remember the term but there's a whole set of work around pattern matching collections with no canonical representations. iirc it should even work with multi sets.

hiredman05:12:18

Pattern matching is kind of half of unification. Pattern matching has a concrete(ground) structure on one side a pattern on the other. Unification between two sets that you might not know the elements of, and might themselves contain logic variables, is annoying

Ben Sless05:12:44

Here we go Backtracking pattern matching for non linear data types https://arxiv.org/abs/1808.10603

hiredman05:12:13

I mean, that is neat, but it is not relational programming. Pattern matching is unidirectional, where unification has to be done in both directions, which is significantly trickier. https://github.com/namin/clpset-miniKanren is the one implementation of constraint logic programming with sets for minikanren that I am aware of, it based on a paper that formulates set constraints as a series of rewrite rules. The paper is pretty straightforward (if you just take them at their word that the rewrites are sound and ignore any proofs), but figuring how to extend code.logics constraint system is a pain because there are no docs for it

Ben Sless06:12:13

I think that meander/zeta can be run backwards, which might satisfy this requirements But are we complaining about core logic now? I wouldn't mind a well documented implementation with better perf

hiredman06:12:23

I periodically start and never finish getting clp(set) working with core.logic, https://twitter.com/hiredman_/status/1353832401005203457 has a screen shot of using the version that was farthest along to being complete (but I think that code was lost in a a filesystem misadventure)

Ben Sless06:12:58

Anyway working on CLP in kanren assumes you read the papers and are intimately familiar with the implementation, yes

hiredman06:12:26

Sort of, core.logic's clp stuff takes the minikanren with constraints paper as a starting point, but it implements it using clojure features (protocols, types, etc) so it doesn't end up exactly like what is in the papers (a pretty bare scheme where everything is cons cells)

hiredman06:12:30

So when I say core.logic is not well documented, it is the use of clojure features like protocols.

Ben Sless06:12:01

The metadata doesn't make it easier, too

Ben Sless06:12:33

It shows the CLP stuff was added later

Ben Sless06:12:11

That's why I think an implementation with less branches and conditions will be easier to hack on