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

Add `MonadFail` instance to `Gen` data type

Open chshersh opened this issue 7 years ago • 7 comments

Without this instance it's impossible to perform partial pattern-matching inside property-based tests. Maybe this instance can be somehow smart, like, discard all cases that doesn't match the case?

chshersh avatar Sep 27 '18 06:09 chshersh

If you switch to GHC 8.6 you'll also get a compiler error because of the MonadFail proposal, as discussed in https://github.com/hedgehogqa/haskell-hedgehog/pull/233. In fact, @HuwCampbell proposed

instance Monad m => MonadFail (GenT m) where
  fail _ =
    mzero

which actually discards ━ a change we don't consider making at the moment, as it requires a major version bump. So, for this reason (still in #233) we lean towards

instance Monad m => MonadFail (GenT m) where
  fail =
    error

which at least won't break your code when switching to GHC 8.6. ━ It'd be nice to have your thoughts around this.

moodmosaic avatar Nov 29 '18 22:11 moodmosaic

@moodmosaic I'm actually okay with having error for now, works for me as well :+1:

chshersh avatar Nov 30 '18 06:11 chshersh

I think discarding is the correct thing to do, personally. One could imagine doing something like

Just foo <- forAll ...

And having tests run, rather than the test crash.

ocharles avatar Jan 16 '19 16:01 ocharles

Yes, discarding looks good, but also requires a major version bump, because it's a breaking change. In reality though, it's more or less what @sol tries to explain here:

MonadFail is still fairly new and we as a community don't have the collective experience yet on how things will pan out in practice.

That you get compile-time errors instead of runtime errors in some situations now is an intentional change. It's the whole point of introducing MonadFail.

I'm really curious to see what happens with https://github.com/nick8325/quickcheck/pull/228.

moodmosaic avatar Jan 17 '19 14:01 moodmosaic

Any more thoughts here? Or should we just consider it incorrect to pattern match in this way in tests?

fosskers avatar May 03 '19 16:05 fosskers

Follow https://github.com/hedgehogqa/haskell-hedgehog/pull/257 for updates.

moodmosaic avatar May 03 '19 16:05 moodmosaic

Thanks!

fosskers avatar May 03 '19 16:05 fosskers