Created
May 17, 2012 15:35
-
-
Save swannodette/2719676 to your computer and use it in GitHub Desktop.
unify_datums.clj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(ns datomic-play.core | |
(:use [datomic.api :only [db q] :as d]) | |
(:require [clojure.core.logic :as l] | |
[clojure.pprint :as pp])) | |
(def uri "datomic:dev://localhost:4334/hello") | |
(defprotocol IUnifyWithDatum | |
(unify-with-datum [u v s])) | |
(extend-type datomic.db.Datum | |
l/IUnifyTerms | |
(l/unify-terms [u v s] | |
(unify-with-datum v u s))) | |
(extend-protocol IUnifyWithDatum | |
nil | |
(unify-with-datum [v u s] false)) | |
(extend-type Object | |
IUnifyWithDatum | |
(unify-with-datum [v u s] false)) | |
(defn unify-with-datum* [v u s] | |
(loop [i 0 v v s s] | |
(if (== i 4) | |
(if (seq v) false s) | |
(if-let [s (l/unify s (first v) (nth u i))] | |
(recur (inc i) (next v) s) | |
false)))) | |
(extend-type clojure.lang.Sequential | |
IUnifyWithDatum | |
(unify-with-datum [v u s] | |
(unify-with-datum* v u s))) | |
(extend-type datomic.db.Datum | |
l/IUnifyWithSequential | |
(l/unify-with-seq [v u s] | |
(unify-with-datum* u v s))) | |
;; with core.logic 0.7.4 | |
(defn datomic-rel [q] | |
(fn [a] | |
(l/to-stream | |
(map #(l/unify a % q) (d/datoms (db conn) :eavt))))) | |
;; ---- | |
(comment | |
;; only the first time | |
(d/create-database uri) | |
) | |
(def conn (d/connect uri)) | |
(comment | |
(d/transact conn [[:db/add #db/id[:db.part/user] :db/doc "hello world"]]) | |
(d/transact conn [[:db/add #db/id[:db.part/user] :db/doc "goodbye world"]]) | |
) | |
(comment | |
(l/run* [q] | |
(l/fresh [e a t] | |
(l/== q [e a true t]) | |
(datomic-rel q))) | |
;; ([39 44 true 13194139533313] | |
;; [47 51 true 13194139533313] | |
;; [50 44 true 13194139533313] | |
;; [61 51 true 13194139533367]) | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment