quickcheck icon indicating copy to clipboard operation
quickcheck copied to clipboard

`shrinkBoundedEnum`?!

Open sjakobi opened this issue 3 years ago • 1 comments

I have a small sum type that currently uses genericShrink:

data SmallSum = A | B | C | D
  deriving (Eq, Ord, Read, Show, Generic, Enum, Bounded)

instance Arbitrary SmallSum where
  arbitrary = QC.arbitraryBoundedEnum
  shrink = QC.genericShrink

genericShrink is useless in this case though, since there are no subterms that it could shrink to. From the haddocks:

Shrink a term to any of its immediate subterms, and also recursively shrink all subterms.

What I'd like to have is a function that shrinks a constructor to the "smaller" constructors, either

f1 A = []
f1 B = [A]
f1 C = [A,B]
f1 D = [A,B,C]

or

f2 A = []
f2 B = [A]
f2 C = [B]
f2 D = [C]

Could one of these functions (or a variation) be added as shrinkBoundedEnum in analogy to arbitraryBoundedEnum?!

sjakobi avatar Apr 27 '22 00:04 sjakobi

Maybe an implementation in terms of shrinkIntegral would make sense. For a large type you possibly wouldn't want to shrink to all the smaller constructors, but shrinking to an "interesting" selection of them would be useful.

sjakobi avatar Apr 27 '22 00:04 sjakobi

@sjakobi

I've created a PR that may resolve this issue here: https://github.com/nick8325/quickcheck/pull/350

jonathanknowles avatar Jan 17 '23 10:01 jonathanknowles

Fixed by #350.

nick8325 avatar Mar 12 '23 10:03 nick8325

@sjakobi just in case you're still interested, the latest Hackage release has shrinkBoundedEnum: 🎉

https://hackage.haskell.org/package/QuickCheck-2.14.3/docs/Test-QuickCheck.html#v:shrinkBoundedEnum

jonathanknowles avatar Jun 04 '23 06:06 jonathanknowles