haskell-hedgehog icon indicating copy to clipboard operation
haskell-hedgehog copied to clipboard

`discard` related combinators should take a "reason" argument.

Open ghost opened this issue 6 years ago • 2 comments
trafficstars

I spent a few hours today digging through a big stack of generators, trying to figure out why I was getting "too many discards." It ended up that it was coming from something equivalent to:

sample $ Gen.set (Range.linear 3 5) (pure 1)

This generator will always fail - the trick is, you can't see why it's failing from the output. In my case, the 3, 5, and pure 1 were arguments to a function that had been around for a while, called in the middle of a big stack. It would have saved me a lot of digging if I had been able to get a message like "Unable to construct a set of n elements using Hedgehog.Gen#set; too few distinct values were found" or something of the sort. I propose that the "discard" and "filter" combinators have versions created such that the caller can provide a reason for the discarding, and that these reasons be collected and their distinct values reported when returning the "too many discards" error.

ghost avatar Nov 07 '19 21:11 ghost

I agree this is annoying, good idea.

Will have to ponder a good way to do this. Discards are currently implemented using MaybeT so the obvious thing would be to upgrade it to an ExceptT, not sure what the knock on effects of that will be though.

jacobstanley avatar Nov 07 '19 21:11 jacobstanley

There's also a semantic difference between "I failed to generate a value" and "I recognized that it is impossible to generate a value" that might sometimes be good to capture. In particular, the latter states that the problem is in the caller.

treeowl avatar Nov 12 '19 17:11 treeowl