Skip to content

Instantly share code, notes, and snippets.

@lynaghk
Forked from jonase/gist:2205346
Created March 26, 2012 16:17
Show Gist options
  • Save lynaghk/2206272 to your computer and use it in GitHub Desktop.
Save lynaghk/2206272 to your computer and use it in GitHub Desktop.
A more advanced rule system?
(ns termite.core
(:refer-clojure :exclude [==])
(:use [clojure.core.logic]))
(defn- meta-guard [key]
#(do
(println % ":\t" (meta %))
(-> % meta key (= true))))
;;remove anything that has {:clj true} metadata, including on var declarations.
(def cljs-rule
[#(matcha [%]
([[_ var . _]]
(pred var (meta-guard :clj)))
([x]
(pred x (meta-guard :clj))))
#(== % :exclude)])
(defn simplify-one [expr rules]
(let [alt (run* [q]
(fresh [pat subst]
(membero [pat subst] rules)
(project [pat subst]
(all (pat expr)
(subst q)))))]
(if (empty? alt) expr (first alt))))
(set! *print-meta* true)
(doall (map #(simplify-one % [cljs-rule])
['(defn ^:clj x [] 123)
'(defn ^:cljs stays [] 123)
(quote ^:clj a-var)
(quote ^:clj (a-form))
'(stays)
]))
;;Metadata is getting dropped somewhere in the matcha:
;; x : {:clj true}
;; stays : {:cljs true}
;; (defn stays [] 123) : nil
;; a-var : {:clj true}
;; (a-form) : nil
;; (stays) : nil
;;=> (:exclude ^{:line 1} (defn ^{:cljs true} stays [] 123) :exclude ^{:clj true, :line 1} (a-form) ^{:line 1} (stays))
@lynaghk
Copy link
Author

lynaghk commented Mar 26, 2012

@swannodette, I dug into core.logic a bit to see what's what, but didn't get very far.
I don't know a ton about metadata, but presumably in a destructuring somewhere there is a seq fn that doesn't preserve it?

@lynaghk
Copy link
Author

lynaghk commented Mar 27, 2012

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment