|
1 | 1 | (ns palletops.locos
|
2 | 2 | "Allow use of declarative rules for specifying configuration options"
|
3 | 3 | (:refer-clojure :exclude [==])
|
4 |
| - (:use |
| 4 | + (:require |
5 | 5 | [clojure.core.logic
|
6 |
| - :only [all defc fresh membero partial-map prep project run* s# walk-term |
7 |
| - == >fd <fd]] |
8 |
| - [clojure.tools.logging :only [debugf warnf]] |
9 |
| - [clojure.walk :only [postwalk]])) |
| 6 | + :refer [all defnc featurec partial-map fresh membero project run* s# ==]] |
| 7 | + [clojure.core.logic.fd :as fd] |
| 8 | + [clojure.core.logic.protocols :refer [walk-term]] |
| 9 | + [clojure.core.logic.unifier :refer [prep]] |
| 10 | + [clojure.tools.logging :refer [debugf tracef warnf]] |
| 11 | + [clojure.walk :refer [postwalk]])) |
10 | 12 |
|
11 | 13 | (defn deep-merge
|
12 | 14 | "Recursively merge maps."
|
|
45 | 47 |
|
46 | 48 | (def ^{:private true :doc "Translate to logic functions"}
|
47 | 49 | op-map
|
48 |
| - {`> >fd |
49 |
| - `< <fd |
50 |
| - '> >fd |
51 |
| - '< <fd}) |
| 50 | + {`> fd/> |
| 51 | + `< fd/< |
| 52 | + '> fd/> |
| 53 | + '< fd/<}) |
52 | 54 |
|
53 | 55 | (defn ->pmap
|
54 | 56 | "Return a value that will use partial-map unification"
|
55 |
| - [x] |
56 |
| - (if (and (map? x) |
57 |
| - (not (instance? clojure.lang.IRecord x)) |
58 |
| - (not (instance? clojure.core.logic.PMap x))) |
59 |
| - (partial-map x) |
60 |
| - x)) |
| 57 | + [x] x) |
61 | 58 |
|
62 | 59 | (defn recursive-partial-map
|
63 | 60 | "Return a value that will use partial-map unification on sub-maps"
|
|
70 | 67 | [rule]
|
71 | 68 | (let [[pattern production & guards] (prep rule)]
|
72 | 69 | {:rule (or (-> rule meta :name) (first rule))
|
73 |
| - :pattern (recursive-partial-map pattern) |
| 70 | + :pattern (->pmap pattern) |
74 | 71 | :production production
|
75 | 72 | :guards (fn []
|
76 | 73 | (if (seq guards)
|
|
132 | 129 | `(def ~name (rules->logic-terms ~(vec (quote-rules rules)))))
|
133 | 130 |
|
134 | 131 | ;; guarantee that a path of keys does not occur in map x
|
135 |
| -(defc not-pathc [x path] |
| 132 | +(defnc not-pathc [x path] |
136 | 133 | (= (get-in x path ::not-found) ::not-found))
|
137 | 134 |
|
138 |
| -(defc get-c [x s] |
| 135 | +(defnc get-c [x s] |
139 | 136 | (not= (get s x ::not-found) ::not-found))
|
140 | 137 |
|
141 | 138 | (defn matching-productions
|
142 | 139 | "Takes an expression, and applies rules to it, returning a sequence
|
143 | 140 | of valid productions."
|
144 | 141 | [expr rules]
|
| 142 | + (tracef "matching-productions" expr rules) |
145 | 143 | (run* [q]
|
146 | 144 | (fresh [pattern production guards rule]
|
147 | 145 | (membero
|
148 | 146 | {:pattern pattern :production production :guards guards :rule rule}
|
149 | 147 | rules)
|
150 |
| - (== expr pattern) |
| 148 | + (featurec expr pattern) |
151 | 149 | (== q {:production production :rule rule})
|
152 | 150 | (project [guards] (guards)))))
|
153 | 151 |
|
|
156 | 154 | [expr rules]
|
157 | 155 | (if-let [productions (seq (matching-productions expr rules))]
|
158 | 156 | (do
|
| 157 | + (tracef "productions" productions) |
159 | 158 | (when-let [invalid (seq (remove map? productions))]
|
160 | 159 | (warnf "Skipping locos productions %s" (vec invalid)))
|
161 | 160 | (reduce
|
|
0 commit comments