-
Notifications
You must be signed in to change notification settings - Fork 119
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
Add shrinkBoundedEnum
function.
#350
Merged
nick8325
merged 2 commits into
nick8325:master
from
jonathanknowles:jonathanknowles/shrinkBoundedEnum
Mar 12, 2023
Merged
Add shrinkBoundedEnum
function.
#350
nick8325
merged 2 commits into
nick8325:master
from
jonathanknowles:jonathanknowles/shrinkBoundedEnum
Mar 12, 2023
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Closed
jonathanknowles
force-pushed
the
jonathanknowles/shrinkBoundedEnum
branch
from
January 17, 2023 10:16
0acb5e7
to
818c3c0
Compare
The `shrinkBoundedEnum` function provides a counterpart to the pre-existing `arbitraryBoundedEnum` function. Together, they can be used to define a complete `Arbitrary` instance for a bounded enumeration data type. For example, let's say we have the following type: ```hs data MyEnum = E00 | E01 | E02 | E03 | E04 | E05 | E06 | E07 | E08 | E09 | E10 | E11 | E12 | E13 | E14 | E15 | E16 | E17 | E18 | E19 | E20 | E21 | E22 | E23 | E24 | E25 | E26 | E27 | E28 | E29 | E30 | E31 | E32 | E33 | E34 | E35 | E36 | E37 | E38 | E39 | E40 | E41 | E42 | E43 | E44 | E45 | E46 | E47 | E48 | E49 | E50 | E51 | E52 | E53 | E54 | E55 | E56 | E57 | E58 | E59 | E60 | E61 | E62 | E63 | E64 | E65 | E66 | E67 | E68 | E69 | E70 | E71 | E72 | E73 | E74 | E75 | E76 | E77 | E78 | E79 | E80 | E81 | E82 | E83 | E84 | E85 | E86 | E87 | E88 | E89 | E90 | E91 | E92 | E93 | E94 | E95 | E96 | E97 | E98 | E99 deriving (Bounded, Enum, Eq, Ord, Show) ``` We can then define the following arbitrary instance: ``` instance Arbitrary MyEnum where arbitrary = arbitraryBoundedEnum shrink = shrinkBoundedEnum ``` Since `shrinkBoundedEnum` reuses `shrinkIntegral` internally, we get similar shrinking behaviour. Values earlier in the shrink list are shrunk more aggressively, followed by exponential back-off: ```hs >>> shrinkBoundedEnum E99 [E00,E50,E75,E87,E93,E96,E98] ``` ```hs >>> shrinkBoundedEnum E49 [E00,E25,E37,E43,E46,E48] ``` ```hs >>> shrinkBoundedEnum E01 [E00] ``` ```hs >>> shrinkBoundedEnum E00 [] ```
jonathanknowles
force-pushed
the
jonathanknowles/shrinkBoundedEnum
branch
from
January 17, 2023 10:54
818c3c0
to
2982eaf
Compare
Merged
4 tasks
Hi @nick8325 Just wondering if you think this function would be useful? If you like the idea, but you'd like me to make revisions to it -- let me know, would be happy to do so. All the best! |
It looks good! Thanks for the patch! |
You're welcome! |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This PR adds a new function
shrinkBoundedEnum
, which should resolve issue #343.The
shrinkBoundedEnum
function provides a counterpart to the pre-existingarbitraryBoundedEnum
function. Together, they can be used to define a completeArbitrary
instance for a bounded enumeration data type.For example, let's say we have the following (admittedly contrived) type:
We can then define the following
Arbitrary
instance:Since
shrinkBoundedEnum
reusesshrinkIntegral
internally, we get similar shrinking behaviour: values earlier in the shrink list are shrunk more aggressively, followed by the usual exponential back-off: