Last active
July 13, 2020 05:33
-
-
Save boogie666/a32fbf79c28196066e815966fee4dde6 to your computer and use it in GitHub Desktop.
Core logic example
This file contains hidden or 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 logical-db.core | |
(:require [clojure.core.logic :as l] | |
[clojure.core.logic.protocols :refer [walk]] | |
[clojure.core.logic.pldb :as db])) | |
; [org.clojure/clojure "1.8.0"] | |
; [org.clojure/core.logic "0.8.10"] | |
(def db { :city->zip {"City1" [1 2 3 4] "City2" [5 6]} | |
:zip->city { 1 "City1" 2 "City1" 3 "City1" 4 "City1" | |
5 "City2" 6 "City2"}}) | |
(defn testo [city zip-code] | |
(fn [a] | |
(let [zip (walk a zip-code) | |
city (walk a city)] | |
(condp = [(not (l/lvar? zip)) | |
(-> city l/lvar? not)] | |
[true false] (l/unify a city (-> db :zip->city (get zip-code))) | |
[false true] (let [zips-for-city (-> db :city->zip (get city))] | |
(->> zips-for-city | |
(map #(l/unify a zip %)) | |
(l/to-stream))) | |
[true true] (when (= city (-> db :zip->city (get zip))) | |
a))))) | |
(l/run* [q] | |
(testo "City2" q)) | |
(l/run* [q] | |
(l/fresh [x] | |
(l/membero x ["City1" "City2"]) | |
(l/== q 1) | |
(testo x q))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment