Search code examples
clojureclojure.spec

Why is Clojure Spec going into an infinite loop here?


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?


Solution

  • 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 ...