Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

shrinkBoundedEnum?! #343

Closed
sjakobi opened this issue Apr 27, 2022 · 4 comments
Closed

shrinkBoundedEnum?! #343

sjakobi opened this issue Apr 27, 2022 · 4 comments

Comments

@sjakobi
Copy link

sjakobi commented Apr 27, 2022

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
Copy link
Author

sjakobi commented Apr 27, 2022

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.

jonathanknowles added a commit to cardano-foundation/cardano-wallet that referenced this issue Jan 17, 2023
Though the `QuickCheck` library provides a handy `arbitraryBoundedEnum`
function, it currently doesn't provide a `shrinkBoundedEnum`
counterpart. Users have asked for a function like this here:

nick8325/quickcheck#343

This commit adds our own version, for now. Perhaps, later on, we can
submit a PR to the `QuickCheck` repository to add this function, and
delete our own version.
jonathanknowles added a commit to cardano-foundation/cardano-wallet that referenced this issue Jan 17, 2023
Though the `QuickCheck` library provides a handy `arbitraryBoundedEnum`
function, it currently doesn't provide a `shrinkBoundedEnum`
counterpart. Users have asked for a function like this here:

nick8325/quickcheck#343

This commit adds our own version, for now. Perhaps, later on, we can
submit a PR to the `QuickCheck` repository to add this function, and
delete our own version.
@jonathanknowles
Copy link
Contributor

@sjakobi

I've created a PR that may resolve this issue here: #350

@nick8325
Copy link
Owner

Fixed by #350.

@jonathanknowles
Copy link
Contributor

@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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants