Fork me on GitHub
#clojure-spec
<
2019-08-06
>
dorab04:08:51

Can someone please explain this behavior?

dorab04:08:17

$ clj -Srepro
Clojure 1.10.1
user=> (require '[clojure.spec.alpha :as s])
nil
user=> (require '[clojure.spec.gen.alpha :as sg])
nil
user=> (require '[clojure.spec.test.alpha :as st])
nil
user=> (s/def ::vec vector?)
:user/vec
user=> (s/def ::str string?)
:user/str
user=> (s/def ::str-vec (s/coll-of ::str :kind ::vec))
:user/str-vec
user=> (sg/generate (s/gen (s/coll-of string? :kind vector?)))
["2m4A8TKjUU5TJ61KkKRzbnk2z21" "xAvkdc78i5qo6O8Gf6" "cHbZwfz88d6xZj5qp54L5g" "K6MdrEaiFWm2P27BCD1913h9r1" "2jpUYE74Vvf" "PG9SrB729W5pWo6G5xt7K28tR5M33" "DTAJijHs2t9mEC" "6zkimTFtp51q698N" "eAgGEU08gJF7R5C2tb6ZdcTRO" "Ewx60Cb94NWxMMXjQJn"]
user=> (sg/generate (s/gen ::str-vec))
Execution error (ExceptionInfo) at clojure.test.check.generators/such-that-helper (generators.cljc:320).
Couldn't satisfy such-that predicate after 100 tries.
user=> 

dorab04:08:11

Why does the sg/generate work in one case, but not the other?

seancorfield04:08:30

@dorab :kind should not be a spec, it should be a predicate

seancorfield04:08:51

If you define ::str-vec with :kind vector? it works just as expected.

seancorfield04:08:51

If you read the docstring for s/every (which applies to s/coll-of), it says

:kind - a pred that the collection type must satisfy, e.g. vector?
        (default nil) Note that if :kind is specified and :into is
        not, this pred must generate in order for every to generate.

seancorfield04:08:49

Predicates can be treated as specs, but specs are not predicates (in general).