Skip to content

Commit 6603ff4

Browse files
authored
Merge pull request #78 from purescript/gen
Add `MonadGen` instance
2 parents ce76b7f + b0d72ab commit 6603ff4

File tree

3 files changed

+17
-11
lines changed

3 files changed

+17
-11
lines changed

bower.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
"purescript-either": "^3.0.0",
2626
"purescript-enums": "^3.0.0",
2727
"purescript-exceptions": "^3.0.0",
28+
"purescript-gen": "^1.0.0",
2829
"purescript-lists": "^4.0.0",
2930
"purescript-nonempty": "^4.0.0",
3031
"purescript-partial": "^1.2.0",

src/Test/QuickCheck/Arbitrary.purs

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ module Test.QuickCheck.Arbitrary
77

88
import Prelude
99

10+
import Control.Monad.Gen.Class (chooseBool)
11+
import Control.Monad.Gen.Common as MGC
12+
1013
import Data.Char (toCharCode, fromCharCode)
1114
import Data.Either (Either(..))
1215
import Data.Foldable (foldl)
@@ -21,7 +24,7 @@ import Data.NonEmpty (NonEmpty(..), (:|))
2124
import Data.String (charCodeAt, fromCharArray, split)
2225
import Data.Tuple (Tuple(..))
2326

24-
import Test.QuickCheck.Gen (Gen, listOf, chooseInt, sized, perturbGen, repeatable, arrayOf, oneOf, uniform)
27+
import Test.QuickCheck.Gen (Gen, elements, listOf, chooseInt, sized, perturbGen, repeatable, arrayOf, uniform)
2528

2629
-- | The `Arbitrary` class represents those types whose values can be
2730
-- | _randomly-generated_.
@@ -44,9 +47,7 @@ class Coarbitrary t where
4447
coarbitrary :: forall r. t -> Gen r -> Gen r
4548

4649
instance arbBoolean :: Arbitrary Boolean where
47-
arbitrary = do
48-
n <- uniform
49-
pure $ (n * 2.0) < 1.0
50+
arbitrary = chooseBool
5051

5152
instance coarbBoolean :: Coarbitrary Boolean where
5253
coarbitrary true = perturbGen 1.0
@@ -83,7 +84,7 @@ instance coarbUnit :: Coarbitrary Unit where
8384
coarbitrary _ = perturbGen 1.0
8485

8586
instance arbOrdering :: Arbitrary Ordering where
86-
arbitrary = oneOf $ (pure LT) :| [pure EQ, pure GT]
87+
arbitrary = elements $ LT :| [EQ, GT]
8788

8889
instance coarbOrdering :: Coarbitrary Ordering where
8990
coarbitrary LT = perturbGen 1.0
@@ -111,18 +112,14 @@ instance coarbTuple :: (Coarbitrary a, Coarbitrary b) => Coarbitrary (Tuple a b)
111112
coarbitrary (Tuple a b) = coarbitrary a >>> coarbitrary b
112113

113114
instance arbMaybe :: Arbitrary a => Arbitrary (Maybe a) where
114-
arbitrary = do
115-
b <- arbitrary
116-
if b then pure Nothing else Just <$> arbitrary
115+
arbitrary = MGC.genMaybe arbitrary
117116

118117
instance coarbMaybe :: Coarbitrary a => Coarbitrary (Maybe a) where
119118
coarbitrary Nothing = perturbGen 1.0
120119
coarbitrary (Just a) = coarbitrary a
121120

122121
instance arbEither :: (Arbitrary a, Arbitrary b) => Arbitrary (Either a b) where
123-
arbitrary = do
124-
b <- arbitrary
125-
if b then Left <$> arbitrary else Right <$> arbitrary
122+
arbitrary = MGC.genEither arbitrary arbitrary
126123

127124
instance coarbEither :: (Coarbitrary a, Coarbitrary b) => Coarbitrary (Either a b) where
128125
coarbitrary (Left a) = coarbitrary a

src/Test/QuickCheck/Gen.purs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ import Control.Monad.Eff.Random (RANDOM)
3737
import Control.Monad.Rec.Class (class MonadRec, Step(..), tailRecM)
3838
import Control.Monad.State (State, runState, evalState)
3939
import Control.Monad.State.Class (state, modify)
40+
import Control.Monad.Gen.Class (class MonadGen)
4041

4142
import Data.Array ((!!), length)
4243
import Data.Enum (class BoundedEnum, fromEnum, toEnum)
@@ -75,6 +76,13 @@ derive newtype instance monadGen :: Monad Gen
7576
derive newtype instance altGen :: Alt Gen
7677
derive newtype instance monadRecGen :: MonadRec Gen
7778

79+
instance monadGenGen :: MonadGen Gen where
80+
chooseInt = chooseInt
81+
chooseFloat = choose
82+
chooseBool = (_ < 0.5) <$> uniform
83+
resize f g = stateful \state -> resize (f state.size) g
84+
sized = sized
85+
7886
-- | Exposes the underlying State implementation.
7987
unGen :: forall a. Gen a -> State GenState a
8088
unGen (Gen st) = st

0 commit comments

Comments
 (0)