haskell-hedgehog
haskell-hedgehog copied to clipboard
Discard on failed pattern matches within a Generator
As discussed in #233.
A similar issue exists for PropertyT.
See this conversation regarding PropertyT https://github.com/hedgehogqa/haskell-hedgehog/pull/233/files#r234163334
I've spent some time thinking about this and the problem I have with it is that ideally one should aim to write tests / generators that don't ever discard.
Discarding slows things down and I would consider its use a bit of an anti-pattern. I think it should only really be used as a last resort. You're better of using the just / filter functions which locally retry the generation so that your "discard" is limited in scope, rather than throwing away the entire test because some deeply nested generator has failed.
just :: MonadGen m => m (Maybe a) -> m a
filter :: MonadGen m => (a -> Bool) -> m a -> m a
So I'm just a bit unsure about making it syntactically convenient to discard, as often there are more appropriate options. I actually wonder if it would be better to remove the MonadFail instance for Gen and have it be a compile error. This would mean you can't use it for the "this should never happen" scenario though, which you could argue isn't going to cause any huge issues (like discarding does).
I actually wonder if it would be better to remove the MonadFail instance for Gen and have it be a compile error.
This aligns pretty well with @sol's comment on the same topic (QC): https://github.com/nick8325/quickcheck/pull/228#issuecomment-427535945
If we decide to get rid of MonadFail perhaps we better do now, before next major release is out.
If we decide to get rid of
MonadFailperhaps we better do now, before next major release is out.
Too late :sweat_smile:
I think this topic has too many trade-offs to rush the decision, so I'm not that concerned. What we ended up releasing is the same as what it has always been.