clojure.spec generation question

274 vaatamist
Liigu esimese lugemata sõnumi juurde

Jeroen van Dijk

lugemata,
30. mai 2016, 17:05:5230.05.16
kuni clo...@googlegroups.com
I'm trying to generate logical predicates in order to test a function that should return the predicate in DNF. The generation seems to be biased towards one of the predicates. What am I doing wrong?

(require '[clojure.spec :as s])
(require '[clojure.spec.gen :as gen])

(s/def ::atom string?)

(s/def ::predicate (s/or
                    ::not-predicate
                    ::and-predicate
                    ::or-predicate
                    ::atom))

(s/def ::and-predicate (s/cat :pred #{:and} :args (s/+ ::predicate)))
(s/def ::or-predicate  (s/cat :pred #{:or} :args (s/+ ::predicate)))
(s/def ::not-predicate (s/tuple :pred #{:not} ::predicate))

(prn (take 5 (gen/sample (s/gen ::predicate))))
;;=> 
((:and (:and "")) "" "X" "G" (:and "yd" (:and "c" "" (:and "F" (:and "" "8" "Hb" "U0d")) "C") (:and (:and "1" "e" (:and "ME01" "w" "Y4" "" "P4") "J4m4" "8") "Q7c" "") (:and (:and (:and "" "dG"))) "gw5"))


No :or's or :not's here. If I change the order of s/or above the bias changes. What's a better approach?

Thanks,
Jeroen

Gary Fredericks

lugemata,
30. mai 2016, 21:14:1830.05.16
kuni Clojure
At a glance, this is probably the normal increasing-size behavior of test.check, and the bias should only be present for the first few samples. E.g., if you do a (take 1000 (gen/sample ...)), it should be more uniform.

Whether it's a real problem depends on how you're running your tests. I haven't yet looked into whether clojure.spec provides some default way of running test.check properties, so I'm not sure about that. But as long as you're running "enough" tests, e.g. >100, it should be fine.

Jeroen van Dijk

lugemata,
31. mai 2016, 11:32:5731.05.16
kuni clo...@googlegroups.com
Hi Gary,

Thanks for the feedback. I've tried it with test.check itself and I get better results (see below). So I'm guessing clojure.spec needs different input or it works differently underneath.

Thanks,
Jeroen

(require '[clojure.test.check :as tc])
(require '[clojure.test.check.generators :as gen])
(require '[clojure.test.check.properties :as prop])

(def gen-atom (gen/elements (mapcat (juxt (partial str "f_a")
                                          (partial str "t_a")) (range 10))))

(def gen-atom (gen/elements (mapcat (juxt (partial str "f_a")
                                          (partial str "t_a")) (range 10))))

(defn gen-cat
  "Returns a generator of a sequence catenated from results of
gens, each of which should generate something sequential."
  [& gens]
  (gen/fmap #(vec (apply concat %))
        (apply gen/tuple gens)))

(def compound (fn [inner-gen]
                (gen/one-of [(gen-cat (gen/elements [[:not]]) (gen/tuple inner-gen))
                             (gen-cat (gen/elements [[:or]])  (gen/tuple inner-gen)  (gen/not-empty (gen/list inner-gen)))
                             (gen-cat (gen/elements [[:and]]) (gen/tuple inner-gen) (gen/not-empty (gen/list inner-gen)))])))
(def gen-predicate (gen/recursive-gen compound gen-atom))

([:or [:and [:or "t_a3" "f_a5"] [:and "t_a5" "t_a6"]] [:or [:not "t_a5"] [:not "t_a1"]]] [:and [:and [:and "f_a4" "f_a4" "f_a9"] [:or "f_a2" "t_a5"]] [:and [:not "f_a3"] [:not "f_a1"]]] [:or "t_a5" "f_a6" "t_a7" "t_a3"] [:not [:and [:and [:not [:not "t_a5"]] [:and [:or "t_a4" "f_a7"] [:not "t_a2"] [:not "f_a6"] [:and "t_a5" "t_a4"] [:or "t_a8" "f_a0"]] [:not [:or "f_a8" "f_a2"]]] [:or [:or [:not "f_a0"] [:or "t_a9" "t_a7"]] [:not [:or "t_a5" "f_a6" "f_a1"]]] [:not [:and [:and "t_a0" "t_a3"] [:or "f_a2" "t_a5"]]]]] [:or [:not [:and [:or [:not "f_a1"] [:and "f_a0" "t_a8"]] [:not [:not "t_a7"]] [:or [:or "f_a1" "f_a8" "f_a7"] [:and "f_a9" "t_a2"]]]] [:not [:or [:or [:not "t_a7"] [:not "t_a8"]] [:or [:and "t_a3" "t_a0"] [:not "t_a6"]]]] [:not [:and [:and [:and "f_a9" "t_a7"] [:and "f_a6" "f_a9"] [:or "f_a9" "t_a0" "t_a8"]] [:and [:not "f_a4"] [:or "t_a3" "f_a4"] [:or "t_a3" "f_a3"]]]]])

--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clo...@googlegroups.com
Note that posts from new members are moderated - please be patient with your first post.
To unsubscribe from this group, send email to
clojure+u...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
---
You received this message because you are subscribed to the Google Groups "Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email to clojure+u...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Rich Hickey

lugemata,
31. mai 2016, 13:12:4531.05.16
kuni clo...@googlegroups.com
You are not labeling your preds, so ::not-predicate is taken as the label for ::and-predicate etc.

Also, tuples are not labeled:

(require '[clojure.spec :as s])
(require '[clojure.spec.gen :as gen])

(s/def ::atom string?)

(s/def ::predicate (s/or
:not ::not-predicate
:and ::and-predicate
:or ::or-predicate
:atom ::atom))

(s/def ::and-predicate (s/cat :pred #{:and} :args (s/+ ::predicate)))
(s/def ::or-predicate (s/cat :pred #{:or} :args (s/+ ::predicate)))
(s/def ::not-predicate (s/tuple #{:not} ::predicate))

user=> (prn (take 5 (gen/sample (s/gen ::predicate))))
([:not [:not (:or "")]] "L" [:not (:and (:and (:and "" (:or "tQ" "s" ""))) [:not [:not "T"]])] "19f" [:not "8lC”])

spec works the same under the hood as what Gary showed, e.g. s/or turns into gen/one-of etc.

Rich

Jeroen van Dijk

lugemata,
1. juuni 2016, 11:04:4201.06.16
kuni clo...@googlegroups.com
Thank you Rich!

Op di 31 mei 2016 om 19:12 schreef Rich Hickey <richh...@gmail.com>:
Vasta kõigile
Vasta autorile
Saada edasi
0 uut sõnumit