10
votes

I have been enjoying Clojure core.logic but I have run into a wall. I need to be able to have it use a database, either SQL or not, rather than an in memory data structure. As I have looked around I see mention of a to-stream function but no solid examples of its use.

Does anyone have a good example of using core.logic with a database?

1
There's an example of connecting it to Dataomic in the core.logic documentation -- did you take a look at that? github.com/clojure/core.logic/wiki/… - brool
I have seen that and I think the problem is I don't use Datomic so I do not quite grok what the example in the Custom Data Sources actually does. I almost have it but I don't see how I would, for example, run a query based on the data it is trying to unify against. - M Smith

1 Answers

7
votes

As someone has already suggested in the comments look at the Datomic example in the core.logic repository. Based on the example there you could imagine writing something like this:

(defn query [db query-string out]
  (fn [a]
    (to-stream
      (map (fn [result] (unify a out result))
        (db-query db query-string)))))

All core.logic goals just return closures which take a substitution map a (you can of course call it whatever you like). Essentially you need to map over the results and unify them with out in a.

Then you could imagine writing a core.logic program like the following:

(run* [q]
  (fresh [row]
    (query some-db "... some query string ..." row)
    (some-other-goal row q)))