-
Notifications
You must be signed in to change notification settings - Fork 24
more efficient genBoundedEnum #33
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,17 +2,24 @@ module Data.Enum.Gen where | |
|
||
import Prelude | ||
|
||
import Control.Monad.Gen (class MonadGen, elements) | ||
import Data.Enum (class BoundedEnum, succ, enumFromTo) | ||
import Data.Maybe (Maybe(..)) | ||
import Control.Monad.Gen (class MonadGen, chooseInt, elements) | ||
import Data.Enum (class BoundedEnum, Cardinality, cardinality, enumFromTo, fromEnum, succ, toEnum) | ||
import Data.Maybe (Maybe(..), fromJust) | ||
import Data.Newtype (unwrap) | ||
import Data.NonEmpty ((:|)) | ||
import Partial.Unsafe (unsafePartial) | ||
|
||
-- | Create a random generator for a finite enumeration. | ||
genBoundedEnum :: forall m a. MonadGen m => BoundedEnum a => m a | ||
genBoundedEnum = | ||
case succ bottom of | ||
Just a → | ||
let possibilities = enumFromTo a top :: Array a | ||
in elements (bottom :| possibilities) | ||
Nothing → | ||
pure bottom | ||
let topInt = fromEnum (top :: a) | ||
bottomInt = fromEnum (bottom :: a) | ||
enumRange = topInt - bottomInt | ||
in if enumRange == unwrap (cardinality :: Cardinality a) | ||
then unsafePartial $ fromJust <<< toEnum <$> chooseInt bottomInt topInt | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This actually is unsafe though, if the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If you excluded There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. But then it might be harder to ensure this is uniformly distributed :( |
||
else case succ bottom of | ||
Just a → | ||
let possibilities = enumFromTo a top :: Array a | ||
in elements (bottom :| possibilities) | ||
Nothing → | ||
pure bottom |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Shouldn't this always be the case?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
My idea here was that if the
Enum
instance is bad, this test will fail. If it's good, then the test passes and the following line is safe. I'll rethink this... I'm probably missing something.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, I see what you mean... A bad
Enum
instance could just be lying about itsCardinality
. Argh.