Fork me on GitHub
#clojure-spec
<
2020-10-30
>
teodorlu20:10:28

Good evening! I've written specs to validate a small math language with addition and multiplication. Now I want to generate values! But I'm hitting Stack Overflow. I assume that's because my specs/generators are mutually recursive. Any tips for writing mutually recursive spec generators that don't blow the stack? Code attached.

teodorlu22:10:16

Idea: write generators as functions rather than directly on specs. Argument: recursion depth. Call generator functions from the spec.

teodorlu22:10:42

Followed through on a generator namespace with recursion limit. That fixed my StackOverflow. Sample for add and multiply:

(defn add [depth]
  (gen/fmap to-list
            (gen/tuple (gen/return '+) (expression (dec depth)) (expression (dec depth)))))

(defn expression [depth]
  (if-not (pos? depth)
    (number)
    (gen/one-of [(number)
                 (multiply depth)
                 (add depth)])))

🎉 3
teodorlu13:10:58

Note: Tree depth can also be controlled probabilistically. Make it more probable to take the non-recursive path than the recursive path. Example from the docs:

(gen/frequency [[9 gen/small-integer] [1 (gen/return nil)]])
The probability of the non-recursive path would have to counterbalance the fanout for the recursive paths.

teodorlu14:10:13

From reading test.check source, I see that many generators accept a size parameter. I suspect that I could have used that instead of inventing my own depth.

borkdude22:10:32

Grasp now supports finding keywords using clojure specs while also preserving location metadata! https://github.com/borkdude/grasp#finding-keywords This should come in handy for finding re-frame events and subscriptions in CLJS apps. I also made a #grasp channel

🎉 9