Why is Clojure Spec going into an infinite loop here? - clojure

This is an application that represents visual patterns as a collection of Sshapes.
An Sshape (styled shape) is a list of points and a map of style information.
An APattern is a record containing a list of Sshapes.
Here's the spec :
In sshape.clj
(spec/def ::stroke-weight int?)
(spec/def ::color (spec/* int?))
(spec/def ::stroke ::color)
(spec/def ::fill ::color)
(spec/def ::hidden boolean?)
(spec/def ::bezier boolean?)
(spec/def ::style (spec/keys :opt-un [::stroke-weight ::stroke ::fill ::hidden ::bezier]))
(spec/def ::point (spec/* number?))
(spec/def ::points (spec/* ::point))
(spec/def ::SShape (spec/keys :req-un [::style ::points]))
In groups.clj
(spec/def ::sshapes (spec/* :patterning.sshapes/SShape))
(spec/def ::APattern (spec/keys :req-un [::sshapes]))
Then in another file, I try to test that a superimpose function that puts two APatterns together is accepting APatterns
(defn superimpose-layout "simplest layout, two patterns located on top of each other "
[pat1 pat2]
{:pre [(spec/valid? :patterning.groups/APattern pat1)]}
(->APattern (concat (:sshapes pat1) (:sshapes pat2))) )
Without the pre-condition this runs.
With the pre-condition, I get this infinite recursion and stack overflow.
Exception in thread "main" java.lang.StackOverflowError, compiling:(/tmp/form-init7774655152686087762.clj:1:73)
at clojure.lang.Compiler.load(Compiler.java:7526)
at clojure.lang.Compiler.loadFile(Compiler.java:7452)
at clojure.main$load_script.invokeStatic(main.clj:278)
at clojure.main$init_opt.invokeStatic(main.clj:280)
at clojure.main$init_opt.invoke(main.clj:280)
at clojure.main$initialize.invokeStatic(main.clj:311)
at clojure.main$null_opt.invokeStatic(main.clj:345)
at clojure.main$null_opt.invoke(main.clj:342)
at clojure.main$main.invokeStatic(main.clj:424)
at clojure.main$main.doInvoke(main.clj:387)
at clojure.lang.RestFn.applyTo(RestFn.java:137)
at clojure.lang.Var.applyTo(Var.java:702)
at clojure.main.main(main.java:37)
Caused by: java.lang.StackOverflowError
at clojure.spec.alpha$regex_QMARK_.invokeStatic(alpha.clj:81)
at clojure.spec.alpha$regex_QMARK_.invoke(alpha.clj:78)
at clojure.spec.alpha$maybe_spec.invokeStatic(alpha.clj:108)
at clojure.spec.alpha$maybe_spec.invoke(alpha.clj:103)
at clojure.spec.alpha$the_spec.invokeStatic(alpha.clj:117)
at clojure.spec.alpha$the_spec.invoke(alpha.clj:114)
at clojure.spec.alpha$dt.invokeStatic(alpha.clj:742)
at clojure.spec.alpha$dt.invoke(alpha.clj:738)
at clojure.spec.alpha$dt.invokeStatic(alpha.clj:739)
at clojure.spec.alpha$dt.invoke(alpha.clj:738)
at clojure.spec.alpha$deriv.invokeStatic(alpha.clj:1480)
at clojure.spec.alpha$deriv.invoke(alpha.clj:1474)
at clojure.spec.alpha$deriv.invokeStatic(alpha.clj:1491)
at clojure.spec.alpha$deriv.invoke(alpha.clj:1474)
at clojure.spec.alpha$deriv.invokeStatic(alpha.clj:1491)
at clojure.spec.alpha$deriv.invoke(alpha.clj:1474)
at clojure.spec.alpha$deriv.invokeStatic(alpha.clj:1492)
at clojure.spec.alpha$deriv.invoke(alpha.clj:1474)
at clojure.spec.alpha$deriv.invokeStatic(alpha.clj:1492)
at clojure.spec.alpha$deriv.invoke(alpha.clj:1474)
at clojure.spec.alpha$deriv.invokeStatic(alpha.clj:1492)
etc.
Update :
OK. I've narrowed this down a bit in the repl.
Let's say a vector of points is defined so that pts is
[[-0.3 -3.6739403974420595E-17] [1.3113417037298127E-8 -0.2999999999999997] [0.2999999999999989 2.6226834037856828E-8] [-3.934025103841547E-8 0.29999999999999744] [-0.3 -3.6739403974420595E-17]]
Then calling
(spec/valid? :patterning.sshapes/points pts)
gives me the stack overflow :
StackOverflowError clojure.spec.alpha/regex? (alpha.clj:81)
So it looks like it just because I'm trying to match a spec/* of a spec/* of numbers.
Is there some reason that nested vectors trigger this kind of infinite recursion?

You should probably use spec/coll-of instead of s/* for this purpose:
(s/def ::point (s/coll-of number?))
(s/def ::points (s/coll-of ::point))
(s/def ::SShape (s/keys :req-un [::style ::points]))
(s/exercise (s/coll-of ::SShape))
;; => ([[] []] [[{:style {:hidden false, :bezier false}, :points [[1.0 -3.0 0 0.75 -1.0 -1.0 0 -1.5 1.0 3.0 -1 0] [-2.0 -1 2.0 2.0 0 ...

There are a couple of bugs in Clojure spec in this area, I believe.
This one looks like an instance of https://dev.clojure.org/jira/browse/CLJ-2002. It is triggered on conform:
(s/conform (s/* (s/* number?)) [[]]) ; => StackOverflowError

Related

Test for a Valid Instance of java.time.LocalDate using Clojure Spec

I am trying to use Clojure Spec to define a data structure containing a java.time.LocalDate element:
(s/def :ex/first-name string?)
(s/def :ex/last-name string?)
(s/def :ex/birth-date (s/valid? inst? (java.time.LocalDate/now)))
(s/def :ex/person
(s/keys :req [:ex/first-name
:ex/last-name
:ex/birth-date]))
(def p1 #:ex{:first-name "Jenny"
:last-name "Barnes"
:birth-date (java.time.LocalDate/parse "1910-03-15")})
(println p1)
produces the following output
#:ex{:first-name Jenny, :last-name Barnes, :birth-date #object[java.time.LocalDate 0x4ed4f9db 1910-03-15]}
However, when I test to see if p1 conforms to the :ex/person spec, it fails:
(s/valid? :ex/person p1)
ClassCastException java.lang.Boolean cannot be cast to clojure.lang.IFn clojure.spec.alpha/spec-impl/reify--1987 (alpha.clj:875)
Looking closer at the Clojure examples for inst?, I see:
(inst? (java.time.Instant/now))
;;=> true
(inst? (java.time.LocalDateTime/now))
;;=> false
However, I don't see an obvious reason as to why that returns false. This seems to be the root of my issue, but I have not found a solution and would like some help.
You're probably looking for instance?- and your example fails, because in:
(s/def :ex/birth-date (s/valid? inst? (java.time.LocalDate/now)))
this part (s/valid? inst? (java.time.LocalDate/now)) should be a function (predicate), not boolean. The full code:
(s/def :ex/first-name string?)
(s/def :ex/last-name string?)
(s/def :ex/birth-date #(instance? java.time.LocalDate %))
(s/def :ex/person
(s/keys :req [:ex/first-name
:ex/last-name
:ex/birth-date]))
(def p1 #:ex{:first-name "Jenny"
:last-name "Barnes"
:birth-date (java.time.LocalDate/parse "1910-03-15")})
(s/valid? :ex/person p1)
=> true
inst? won't work here, because Inst is a protocol, used to extend java.util.Date and java.time.Instant:
(defprotocol Inst
(inst-ms* [inst]))
(extend-protocol Inst
java.util.Date
(inst-ms* [inst] (.getTime ^java.util.Date inst)))
(defn inst?
"Return true if x satisfies Inst"
{:added "1.9"}
[x]
(satisfies? Inst x))
(extend-protocol clojure.core/Inst
java.time.Instant
(inst-ms* [inst] (.toEpochMilli ^java.time.Instant inst)))
And you can use satisfies? to check whether some object satisfies given protocol:
(satisfies? Inst (java.time.LocalDate/parse "1910-03-15"))
=> false

I have complex Spec for my data - how to generate samples?

My Clojure spec looks like :
(spec/def ::global-id string?)
(spec/def ::part-of string?)
(spec/def ::type string?)
(spec/def ::value string?)
(spec/def ::name string?)
(spec/def ::text string?)
(spec/def ::date (spec/nilable (spec/and string? #(re-matches #"^\d{4}-\d{2}-\d{2}$" %))))
(spec/def ::interaction-name string?)
(spec/def ::center (spec/coll-of string? :kind vector? :count 2))
(spec/def ::context- (spec/keys :req [::global-id ::type]
:opt [::part-of ::center]))
(spec/def ::contexts (spec/coll-of ::context-))
(spec/def ::datasource string?)
(spec/def ::datasource- (spec/nilable (spec/keys :req [::global-id ::name])))
(spec/def ::datasources (spec/coll-of ::datasource-))
(spec/def ::location string?)
(spec/def ::location-meaning- (spec/keys :req [::global-id ::location ::contexts ::type]))
(spec/def ::location-meanings (spec/coll-of ::location-meaning-))
(spec/def ::context string?)
(spec/def ::context-association-type string?)
(spec/def ::context-association-name string?)
(spec/def ::priority string?)
(spec/def ::has-context- (spec/keys :req [::context ::context-association-type ::context-association-name ::priority]))
(spec/def ::has-contexts (spec/coll-of ::has-context-))
(spec/def ::fact- (spec/keys :req [::global-id ::type ::name ::value]))
(spec/def ::facts (spec/coll-of ::fact-))
(spec/def ::attribute- (spec/keys :req [::name ::type ::value]))
(spec/def ::attributes (spec/coll-of ::attribute-))
(spec/def ::fulltext (spec/keys :req [::global-id ::text]))
(spec/def ::feature- (spec/keys :req [::global-id ::date ::location-meanings ::has-contexts ::facts ::attributes ::interaction-name]
:opt [::fulltext]))
(spec/def ::features (spec/coll-of ::feature-))
(spec/def ::attribute- (spec/keys :req [::name ::type ::value]))
(spec/def ::attributes (spec/coll-of ::attribute-))
(spec/def ::ioi-slice string?)
(spec/def ::ioi- (spec/keys :req [::global-id ::type ::datasource ::features ::attributes ::ioi-slice]))
(spec/def ::iois (spec/coll-of ::ioi-))
(spec/def ::data (spec/keys :req [::contexts ::datasources ::iois]))
(spec/def ::data- ::data)
But it fails to generate samples with:
(spec/fdef data->graph
:args (spec/cat :data ::xml-spec/data-))
(println (stest/check `data->graph))
then it will fail to generate with an exception:
Couldn't satisfy such-that predicate after 100 tries.
It is very convenient to generate spec automatically with stest/check but how to beside spec also have generators?
When you see the error Couldn't satisfy such-that predicate after 100 tries. when generating data from specs, a common cause is an s/and spec because spec builds generators for s/and specs based solely on the first inner spec.
This spec seemed most likely to cause this, because the first inner spec/predicate in the s/and is string?, and the following predicate is a regex:
(s/def ::date (s/nilable (s/and string? #(re-matches #"^\d{4}-\d{2}-\d{2}$" %))))
If you sample a string? generator, you'll see what it produces is unlikely to ever match your regex:
(gen/sample (s/gen string?))
=> ("" "" "X" "" "" "hT9" "7x97" "S" "9" "1Z")
test.check will try (100 times by default) to get a value that satisfies such-that conditions, then throw the exception you're seeing if it doesn't.
Generating Dates
You can implement a custom generator for this spec in several ways. Here's a test.check generator that will create ISO local date strings:
(def gen-local-date-str
(let [day-range (.range (ChronoField/EPOCH_DAY))
day-min (.getMinimum day-range)
day-max (.getMaximum day-range)]
(gen/fmap #(str (LocalDate/ofEpochDay %))
(gen/large-integer* {:min day-min :max day-max}))))
This approach gets the range of valid epoch days, uses that to control the range of large-integer* generator, then fmaps LocalDate/ofEpochDay over the generated integers.
(def gen-local-date-str
(gen/fmap #(-> (Instant/ofEpochMilli %)
(LocalDateTime/ofInstant ZoneOffset/UTC)
(.toLocalDate)
(str))
gen/large-integer))
This starts with the default large-integer generator and uses fmap to provide a function that creates a java.time.Instant from the generated integer, converts it to a java.time.LocalDate, and converts that to a string which happens to conveniently match your date string format. (This is slightly simpler on Java 9 and above with java.time.LocalDate/ofInstant.)
Another approach might use test.chuck's regex-based string generator, or different date classes/formatters. Note that both of my examples will generate years that are eons before/after -9999/+9999, which won't match your \d{4} year regex, but the generator should produce satisfactory values often enough that it may not matter for your use case. There are many ways to generate date values!
(gen/sample gen-local-date-str)
=>
("1969-12-31"
"1970-01-01"
"1970-01-01"
...)
Using Custom Generators with Specs
Then you can associate this generator with your spec using s/with-gen:
(s/def ::date
(s/nilable
(s/with-gen
(s/and string? #(re-matches #"^\d{4}-\d{2}-\d{2}$" %))
(constantly gen-local-date-str))))
(gen/sample (s/gen ::date))
=>
("1969-12-31"
nil ;; note that it also makes nils b/c it's wrapped in s/nilable
"1970-01-01"
...)
You can also provide "standalone" custom generators to certain spec functions that take an overrides map, if you don't want to tie the custom generator directly to the spec definition:
(gen/sample (s/gen ::data {::date (constantly gen-local-date-str)}))
Using this spec and generator I was able to generate your larger ::data spec, although the outputs were very large due to some of the collection specs. You can also control the size of those during generation using :gen-max options in the specs.

Clojure using value from another required key in validation

I'm relatively new to clojure and I'm looking for a way to use the value of one required key in the validation of another. I can do it by creating another map with the two values and passing that, but I was hoping there was a simpler way. Thanks
(s/def ::country string?)
(s/def ::postal-code
;sudo-code
;(if (= ::country "Canda")
;(re-matches #"^[A-Z0-9]{5}$")
;(re-matches #"^[0-9]{5}$"))
)
(s/def ::address
(s/keys :req-un [
::country
::postal-code
::street
::state
]))
Here's a way to do it with multi-spec:
(defmulti country :country)
(defmethod country "Canada" [_]
(s/spec #(re-matches #"^[A-Z0-9]{5}$" (:postal-code %))))
(defmethod country :default [_]
(s/spec #(re-matches #"^[0-9]{5}$" (:postal-code %))))
(s/def ::country string?)
(s/def ::postal-code string?)
(s/def ::address
(s/merge
(s/keys :req-un [::country ::postal-code])
(s/multi-spec country :country)))
(s/explain ::address {:country "USA" :postal-code "A2345"})
;; val: {:country "USA", :postal-code "A2345"} fails spec: :sandbox.so/address at: ["USA"] predicate: (re-matches #"^[0-9]{5}$" (:postal-code %))
(s/explain ::address {:country "Canada" :postal-code "A2345"})
;; Success!
Another option is and-ing another predicate on your keys spec:
(s/def ::address
(s/and
(s/keys :req-un [::country ::postal-code])
#(case (:country %)
"Canada" (re-matches #"^[A-Z0-9]{5}$" (:postal-code %))
(re-matches #"^[0-9]{5}$" (:postal-code %)))))
You might prefer the multi-spec approach because it's open for extension i.e. you can define more defmethods for country later as opposed to keeping all the logic in the and predicate.

clojure.spec/unform returning non-conforming values

I've been getting on quite well with clojure.spec for the most part. However, I came to a problem that I couldn't figure out when dealing with unform. Here's a loose spec for Hiccup to get us moving:
(require '[clojure.spec :as s])
(s/def ::hiccup
(s/and
vector?
(s/cat
:name keyword?
:attributes (s/? map?)
:contents (s/* ::contents))))
(s/def ::contents
(s/or
:element-seq (s/* ::hiccup)
:element ::hiccup
:text string?))
Now before we get carried away, let's see if it works with a small passing case.
(def example [:div])
(->> example
(s/conform ::hiccup))
;;=> {:name :h1}
Works like a charm. But can we then undo our conformance?
(->> example
(s/conform ::hiccup)
(s/unform ::hiccup))
;;=> (:div)
Hmm, that should be a vector. Am I missing something? Let's see what spec has to say about this.
(->> example
(s/conform ::hiccup)
(s/unform ::hiccup)
(s/explain ::hiccup))
;; val: (:div) fails spec: :user/hiccup predicate: vector?
;;=> nil
Indeed, it fails. So the question: How do I get this to work correctly?
Late response here. I had not realised how old this question was but since I had already written a reply I might as well submit it.
Taking the spec that you provide for ::hiccup:
(s/def ::hiccup
(s/and
vector?
(s/cat
:name keyword?
:attributes (s/? map?)
:contents (s/* ::contents))))
The vector? spec within and will test the input data against that predicate. Unluckily as you have experienced, it doesn't unform to a vector.
Something that you can do to remedy this is to add an intermediate spec that will act as the identity when conforming and will act as desired when unforming. E.g.
(s/def ::hiccup
(s/and vector?
(s/conformer vec vec)
(s/cat
:name keyword?
:attributes (s/? map?)
:contents (s/* ::contents))))
(s/unform ::hiccup (s/conform ::hiccup [:div]))
;;=> [:div]
(s/conformer vec vec) Will be a no-op for everything that satisfies vector? and using vec as the unforming function in the conformer will make sure that the result to unforming the whole and spec stays a vector.
I am new to spec myself and this was how I got it working as you intend. Bear in mind it might not be the way in which it was intended to be used by spec's designers.
For what is worth, I made a spec that checks for a vector and unforms to a vector:
(defn vector-spec
"Create a spec that it is a vector and other conditions and unforms to a vector.
Ex (vector-spec (s/spec ::binding-form))
(vector-spec (s/* integer?))"
[form]
(let [s (s/spec (s/and vector? form))]
(reify
s/Specize
(specize* [_] s)
(specize* [_ _] s)
s/Spec
(conform* [_ x] (s/conform* s x))
(unform* [_ x] (vec (s/unform* s x))) ;; <-- important
(explain* [_ path via in x] (s/explain s path via in x))
(gen* [_ overrides path rmap] (s/gen* s overrides path rmap))
(with-gen* [_ gfn] (s/with-gen s gfn))
(describe* [_] (s/describe* s)))))
In your example, you would use it like this:
(s/def ::hiccup
(vector-spec
(s/cat
:name keyword?
:attributes (s/? map?)
:contents (s/* ::contents))))

How can I spec a hybrid map?

After writing this answer, I was inspired to try to specify Clojure's destructuring language using spec:
(require '[clojure.spec :as s])
(s/def ::binding (s/or :sym ::sym :assoc ::assoc :seq ::seq))
(s/def ::sym (s/and simple-symbol? (complement #{'&})))
The sequential destructuring part is easy to spec with a regex (so I'm ignoring it here), but I got stuck at associative destructuring. The most basic case is a map from binding forms to key expressions:
(s/def ::mappings (s/map-of ::binding ::s/any :conform-keys true))
But Clojure provides several special keys as well:
(s/def ::as ::sym)
(s/def ::or ::mappings)
(s/def ::ident-vec (s/coll-of ident? :kind vector?))
(s/def ::keys ::ident-vec)
(s/def ::strs ::ident-vec)
(s/def ::syms ::ident-vec)
(s/def ::opts (s/keys :opt-un [::as ::or ::keys ::strs ::syms]))
How can I create an ::assoc spec for maps that could be created by merging together a map that conforms to ::mappings and a map that conforms to ::opts? I know that there's merge:
(s/def ::assoc (s/merge ::opts ::mappings))
But this doesn't work, because merge is basically an analogue of and. I'm looking for something that's analogous to or, but for maps.
You can spec hybrid maps using an s/merge of s/keys and s/every of the map as tuples. Here's a simpler example:
(s/def ::a keyword?)
(s/def ::b string?)
(s/def ::m
(s/merge (s/keys :opt-un [::a ::b])
(s/every (s/or :int (s/tuple int? int?)
:option (s/tuple keyword? any?))
:into {})))
(s/valid? ::m {1 2, 3 4, :a :foo, :b "abc"}) ;; true
This simpler formulation has several benefits over a conformer approach. Most importantly, it states the truth. Additionally, it should generate, conform, and unform without further effort.
You can use s/conformer as an intermediate step in s/and to transform your map to the form that’s easy to validate:
(s/def ::assoc
(s/and
map?
(s/conformer #(array-map
::mappings (dissoc % :as :or :keys :strs :syms)
::opts (select-keys % [:as :or :keys :strs :syms])))
(s/keys :opt [::mappings ::opts])))
That will get you from e.g.
{ key :key
:as name }
to
{ ::mappings { key :key }
::opts { :as name } }