Skip to content

Instantly share code, notes, and snippets.

@swannodette
Created January 24, 2012 04:40
Show Gist options
  • Save swannodette/1667847 to your computer and use it in GitHub Desktop.
Save swannodette/1667847 to your computer and use it in GitHub Desktop.
hierarchy.clj
(ns hierarchy.core
(:refer-clojure :exclude [==])
(:use [clojure.core.logic]))
(def order [:domain :kingdom :phylum :class :order :family :genus :species])
(def homo-sapiens
{:domain :eukarya
:kingdom :animalia-metazoa
:phylum :chordata
:class :mammalia
:order :primate
:family :hominidae
:genus :homo
:species :homo-sapiens})
(defrel rank ^:index rank ^:index name)
(defrel typeof* ^:index a ^:index b)
(defn add-to-db [data]
(doseq [[a b] (map (fn [a b]
[(find data a)
(find data b)])
order (rest order))]
(apply fact rank a)
(fact typeof* (second b) (second a))))
(defn typeof [a b]
(conde
[(typeof* a b)]
[(fresh [x]
(typeof* a x)
(typeof x b))]))
(comment
(add-to-db homo-sapiens)
;; find all domains
(run* [q]
(rank :domain q))
;; (true)
(run* [q]
(typeof :mammalia :eukarya)
(typeof :homo-sapiens :hominidae)
(typeof :homo-sapiens :primate)
(typeof :homo-sapiens :eukarya)
(== q true))
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment