Skip to content

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

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 16 additions & 9 deletions src/Data/Enum/Gen.purs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Copy link
Contributor

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?

Copy link
Contributor Author

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.

Copy link
Contributor Author

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 its Cardinality. Argh.

then unsafePartial $ fromJust <<< toEnum <$> chooseInt bottomInt topInt
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This actually is unsafe though, if the Enum instance is bad, right?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you excluded bottom, and added it back in fromMaybe, this could be safe though.

Copy link
Contributor

Choose a reason for hiding this comment

The 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