Skip to content
This repository was archived by the owner on Oct 4, 2020. It is now read-only.

Commit 562a031

Browse files
authored
Merge pull request #45 from garyb/gen
Add MonadGen instance
2 parents a2e2a7d + a0c78ad commit 562a031

File tree

3 files changed

+18
-12
lines changed

3 files changed

+18
-12
lines changed

bower.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
"purescript-machines": "^4.0.0",
2626
"purescript-random": "^3.0.0",
2727
"purescript-arrays": "^4.0.1",
28-
"purescript-datetime": "^3.0.0"
28+
"purescript-datetime": "^3.0.0",
29+
"purescript-gen": "^1.0.0"
2930
}
3031
}

src/Test/StrongCheck/Arbitrary.purs

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@ module Test.StrongCheck.Arbitrary where
22

33
import Prelude
44

5+
import Control.Monad.Gen as CMG
6+
import Control.Monad.Gen.Common as CMGC
7+
58
import Data.Array as A
69
import Data.Array.Partial as AP
710
import Data.Char (toCharCode)
@@ -41,9 +44,7 @@ class Coarbitrary t where
4144
coarbitrary :: forall r. t -> Gen r -> Gen r
4245

4346
instance arbBoolean :: Arbitrary Boolean where
44-
arbitrary = do
45-
n <- uniform
46-
pure (n < 0.5)
47+
arbitrary = CMG.chooseBool
4748

4849
instance coarbBoolean :: Coarbitrary Boolean where
4950
coarbitrary true = perturbGen 1.0
@@ -114,24 +115,20 @@ instance coarbFunction :: (Arbitrary a, Coarbitrary b) => Coarbitrary (a -> b) w
114115
coarbitrary (f xs) gen
115116

116117
instance arbTuple :: (Arbitrary a, Arbitrary b) => Arbitrary (Tuple a b) where
117-
arbitrary = Tuple <$> arbitrary <*> arbitrary
118+
arbitrary = CMGC.genTuple arbitrary arbitrary
118119

119120
instance coarbTuple :: (Coarbitrary a, Coarbitrary b) => Coarbitrary (Tuple a b) where
120121
coarbitrary (Tuple a b) = coarbitrary a >>> coarbitrary b
121122

122123
instance arbMaybe :: Arbitrary a => Arbitrary (Maybe a) where
123-
arbitrary = do
124-
b <- arbitrary
125-
if b then pure Nothing else Just <$> arbitrary
124+
arbitrary = CMGC.genMaybe arbitrary
126125

127126
instance coarbMaybe :: Coarbitrary a => Coarbitrary (Maybe a) where
128127
coarbitrary Nothing = perturbGen 1.0
129128
coarbitrary (Just a) = coarbitrary a
130129

131130
instance arbEither :: (Arbitrary a, Arbitrary b) => Arbitrary (Either a b) where
132-
arbitrary = do
133-
b <- arbitrary
134-
if b then Left <$> arbitrary else Right <$> arbitrary
131+
arbitrary = CMGC.genEither arbitrary arbitrary
135132

136133
instance coarbEither :: (Coarbitrary a, Coarbitrary b) => Coarbitrary (Either a b) where
137134
coarbitrary (Left a) = coarbitrary a
@@ -144,7 +141,7 @@ instance coarbList :: Coarbitrary a => Coarbitrary (List a) where
144141
coarbitrary = coarbitrary <<< A.fromFoldable
145142

146143
instance arbitraryIdentity :: Arbitrary a => Arbitrary (Identity a) where
147-
arbitrary = Identity <$> arbitrary
144+
arbitrary = CMGC.genIdentity arbitrary
148145

149146
instance coarbIdentity :: Coarbitrary a => Coarbitrary (Identity a) where
150147
coarbitrary (Identity a) = coarbitrary a

src/Test/StrongCheck/Gen.purs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ import Control.Monad.Eff (Eff)
6363
import Control.Monad.Eff.Console (logShow, CONSOLE)
6464
import Control.Monad.List.Trans as ListT
6565
import Control.Monad.Trampoline (runTrampoline, Trampoline)
66+
import Control.Monad.Gen as CMG
6667
import Control.MonadPlus (class MonadPlus)
6768
import Control.MonadZero (class MonadZero)
6869
import Control.Plus (class Plus)
@@ -598,3 +599,10 @@ instance monadPlusGenT :: Monad f => MonadPlus (GenT f)
598599

599600
instance lazyGenT :: Monad f => CL.Lazy (GenT f a) where
600601
defer f = GenT $ CL.defer (unGen <<< f)
602+
603+
instance monadGenGenT :: Monad f => CMG.MonadGen (GenT f) where
604+
chooseInt = chooseInt
605+
chooseFloat = choose
606+
chooseBool = (_ < 0.5) <$> uniform
607+
resize f g = stateful \(GenState state) -> resize (f state.size) g
608+
sized = sized

0 commit comments

Comments
 (0)