Skip to content

Commit

Permalink
Working
Browse files Browse the repository at this point in the history
  • Loading branch information
marick committed May 29, 2012
1 parent 002346a commit 4460a76
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 8 deletions.
7 changes: 7 additions & 0 deletions src/peano/blank_filling.clj
Expand Up @@ -25,6 +25,13 @@
(symbol? form) :presupplied-lvar (symbol? form) :presupplied-lvar
(map? form) :blank-with-properties)) (map? form) :blank-with-properties))


(defn generate-run-form
([guidance tree]
(let [q (gensym "q")]
`(l/run false [~q]
(l/fresh [~@(guidance :lvars-needed)]
(l/== ~tree ~q)
~@(guidance :narrowers))))))




(comment (comment
Expand Down
5 changes: 4 additions & 1 deletion src/peano/guidance.clj
Expand Up @@ -30,8 +30,11 @@
(with-lvar new-symbol)) (with-lvar new-symbol))
new-symbol))) new-symbol)))


(defn typecast-lvar [lvar relation]
`(~(query-symbol relation) ~lvar))

(defn property-narrower [[relation property] lvar required-value] (defn property-narrower [[relation property] lvar required-value]
`(l/== (~(query-symbol relation property) ~lvar ~required-value))) `(~(query-symbol relation property) ~lvar ~required-value))


(defn with-narrower [guidance narrower] (defn with-narrower [guidance narrower]
(assoc-into-vector guidance :narrowers narrower)) (assoc-into-vector guidance :narrowers narrower))
Expand Down
3 changes: 2 additions & 1 deletion src/peano/selectors.clj
Expand Up @@ -10,7 +10,8 @@
;; -- ;; --


;; TODO: These `generate` functions are similar enough that an ;; TODO: These `generate` functions are similar enough that an
;; abstraction should be abstracted from them. ;; abstraction should be abstracted from them. See also
;; `generate-run-form` in blank-filling.


(defn generate-did-run-form (defn generate-did-run-form
([run-count relation kvs] ([run-count relation kvs]
Expand Down
1 change: 1 addition & 0 deletions test/peano/t_blank_filling.clj
Expand Up @@ -54,6 +54,7 @@







(comment (comment


(fact "trivial case produces no change" (fact "trivial case produces no change"
Expand Down
6 changes: 4 additions & 2 deletions test/peano/t_guidance.clj
Expand Up @@ -39,10 +39,12 @@


(fact "you can write a logic clause that forces an lvar to associate with a property" (fact "you can write a logic clause that forces an lvar to associate with a property"
(property-narrower [:animal :name] 'animal-0 "bessy") (property-narrower [:animal :name] 'animal-0 "bessy")
=> '(clojure.core.logic/== (animal-name?? animal-0 "bessy"))) => '(animal-name?? animal-0 "bessy"))


(fact "narrowers can be added into the guidance" (fact "narrowers can be added into the guidance"
(with-narrower {:key 1} '(a narrower)) => {:key 1, :narrowers '[(a narrower)]}) (with-narrower {:key 1} '(a narrower)) => {:key 1, :narrowers '[(a narrower)]})



(fact "you can 'typecast' a variable to a relation"
(typecast-lvar 'animal-0 :animal)
=> '(animal?? animal-0))


65 changes: 61 additions & 4 deletions test/peano/t_reservation_example.clj
Expand Up @@ -2,9 +2,10 @@
(:require [clojure.core.logic :as l]) (:require [clojure.core.logic :as l])
(:use midje.sweet (:use midje.sweet
clojure.pprint clojure.pprint
[clojure.math.combinatorics :only [combinations]]
peano.core peano.core
peano.guidance peano.guidance
[peano.blank-filling :only [suggested-classifier]])) [peano.blank-filling :only [suggested-classifier generate-run-form]]))


(defmulti processor (defmulti processor
(fn [guidance blank & _] ((:classifier guidance) blank))) (fn [guidance blank & _] ((:classifier guidance) blank)))
Expand Down Expand Up @@ -42,11 +43,35 @@


(defn simplify-and-process [guidance blank _ count-to-left] (defn simplify-and-process [guidance blank _ count-to-left]
(processor guidance blank (processor guidance blank
(if (= 0 count-to-left) :procedure :animal))) (if (= 0 count-to-left) :procedure :animal)))

(defn permitted-pairs-narrowers [guidance]
(map (fn [procedure animal] `(permitted?? ~procedure ~animal))
(guidance :procedure)
(guidance :animal)))

(defn no-duplicate-groups-narrowers [uses]
(map (fn [[one two]] `(l/!= ~one ~two))
(combinations uses 2)))

(fact "remove duplicate groups"
(no-duplicate-groups-narrowers [ [1 1] ]) => []
(no-duplicate-groups-narrowers [ [1 1] [2 2] [3 3]])
=> (just '(clojure.core.logic/!= [1 1] [2 2])
'(clojure.core.logic/!= [1 1] [3 3])
'(clojure.core.logic/!= [2 2] [3 3])
:in-any-order))

(defn postprocessor [guidance tree]
(let [narrowers (concat (permitted-pairs-narrowers guidance)
(no-duplicate-groups-narrowers tree))
]
(vector (merge-with concat guidance {:narrowers narrowers})
tree)))


(def guidance {:classifier suggested-classifier (def guidance {:classifier suggested-classifier
:processor simplify-and-process :processor simplify-and-process
:postprocessor (fn [x y] [x y])}) :postprocessor postprocessor})


;;; About processing ;;; About processing


Expand Down Expand Up @@ -94,4 +119,36 @@
:narrowers [narrower]}))) :narrowers [narrower]})))




(prn (fill-in-the-blanks guidance '[[_ "hank"] [myproc {:legs 4}]]))
(data [animal :by :name]
{:name "betty" :species :bovine :legs 4}
{:name "julie" :species :bovine :legs 4}
{:name "jeff" :species :equine :legs 4}
{:name "hank" :species :equine :legs 3}) ; poor hank

(data [procedure :by :name]
{:name "hoof trim" :species :equine :days-delay 0}
{:name "casting teeth" :species :equine :days-delay 0}
{:name "superovulation" :species :bovine :days-delay 90})

(defn permitted?? [procedure animal]
(l/fresh [species]
(procedure-species?? procedure species)
(animal-species?? animal species)))

(defmacro reservations [& tree]
(apply generate-run-form (fill-in-the-blanks guidance (vec tree))))

(println "============== SIMPLE")
(pprint (reservations [- -]))


(println "============== BIG")
(pprint (reservations [- "hank"] [- {:legs 3}]))




; (pprint (apply generate-run-form (fill-in-the-blanks guidance '[[- "hank"] [- {:legs 3}]])))


0 comments on commit 4460a76

Please sign in to comment.