@@ -26,15 +26,18 @@ module Test.QuickCheck.Gen
2626 , showSample'
2727 ) where
2828
29+ import Prelude
30+
2931import Console (CONSOLE (), print )
3032import Control.Monad.Eff (Eff ())
3133import Data.Array ((!!), length , range )
3234import Data.Foldable (fold )
33- import Data.Int (Int (), fromNumber , toNumber )
35+ import Data.Int (fromNumber , toNumber )
3436import Data.Maybe (fromMaybe )
3537import Data.Monoid.Additive (Additive (..), runAdditive )
3638import Data.Traversable (sequence )
3739import Data.Tuple (Tuple (..), fst , snd )
40+ import Data.List (List (..))
3841import Test.QuickCheck.LCG
3942import qualified Math as M
4043
@@ -86,44 +89,44 @@ chooseInt a b = fromNumber <$> choose (toNumber a) (toNumber b + 0.999999999)
8689
8790-- | Create a random generator which selects and executes a random generator from
8891-- | a non-empty collection of random generators with uniform probability.
89- oneOf :: forall a . Gen a -> [ Gen a ] -> Gen a
92+ oneOf :: forall a . Gen a -> Array ( Gen a ) -> Gen a
9093oneOf x xs = do
9194 n <- chooseInt zero (length xs)
9295 if n < one then x else fromMaybe x (xs !! (n - one))
9396
9497-- | Create a random generator which selects and executes a random generator from
9598-- | a non-empty, weighted collection of random generators.
96- frequency :: forall a . Tuple Number (Gen a ) -> [ Tuple Number (Gen a )] -> Gen a
99+ frequency :: forall a . Tuple Number (Gen a ) -> List ( Tuple Number (Gen a )) -> Gen a
97100frequency x xs = let
98- xxs = x : xs
99- total = runAdditive $ fold ((( Additive <<< fst) <$> xxs) :: [ Additive Number ] )
100- pick n d [] = d
101- pick n d ((Tuple k x) : xs) = if n <= k then x else pick (n - k) d xs
101+ xxs = Cons x xs
102+ total = runAdditive $ fold (map ( Additive <<< fst) xxs :: List ( Additive Number ) )
103+ pick n d Nil = d
104+ pick n d (Cons (Tuple k x) xs) = if n <= k then x else pick (n - k) d xs
102105 in do
103- n <- choose 0 total
106+ n <- choose zero total
104107 pick n (snd x) xxs
105108
106109-- | Create a random generator which generates an array of random values.
107- arrayOf :: forall a . Gen a -> Gen [ a ]
110+ arrayOf :: forall a . Gen a -> Gen ( Array a )
108111arrayOf g = sized $ \n ->
109112 do k <- chooseInt zero n
110113 vectorOf k g
111114
112115-- | Create a random generator which generates a non-empty array of random values.
113- arrayOf1 :: forall a . Gen a -> Gen (Tuple a [ a ] )
116+ arrayOf1 :: forall a . Gen a -> Gen (Tuple a ( Array a ) )
114117arrayOf1 g = sized $ \n ->
115118 do k <- chooseInt zero n
116119 x <- g
117120 xs <- vectorOf (k - one) g
118121 return $ Tuple x xs
119122
120123-- | Create a random generator which generates a vector of random values of a specified size.
121- vectorOf :: forall a . Int -> Gen a -> Gen [ a ]
124+ vectorOf :: forall a . Int -> Gen a -> Gen ( Array a )
122125vectorOf k g = sequence $ const g <$> range one k
123126
124127-- | Create a random generator which selects a value from a non-empty collection with
125128-- | uniform probability.
126- elements :: forall a . a -> [ a ] -> Gen a
129+ elements :: forall a . a -> Array a -> Gen a
127130elements x xs = do
128131 n <- chooseInt zero (length xs)
129132 pure if n == zero then x else fromMaybe x (xs !! (n - one))
@@ -137,7 +140,7 @@ evalGen :: forall a. Gen a -> GenState -> a
137140evalGen gen st = (runGen gen st).value
138141
139142-- | Sample a random generator
140- sample :: forall r a . Size -> Gen a -> [ a ]
143+ sample :: forall r a . Size -> Gen a -> Array a
141144sample sz g = evalGen (vectorOf sz g) { newSeed: zero, size: sz }
142145
143146-- | Print a random sample to the console
@@ -146,7 +149,7 @@ showSample' n g = print $ sample n g
146149
147150-- | Print a random sample of 10 values to the console
148151showSample :: forall r a . (Show a ) => Gen a -> Eff (console :: CONSOLE | r ) Unit
149- showSample = showSample' (fromNumber 10 )
152+ showSample = showSample' 10
150153
151154-- | A random generator which simply outputs the current seed
152155lcgStep :: Gen Int
@@ -157,25 +160,18 @@ lcgStep = Gen f where
157160uniform :: Gen Number
158161uniform = (\n -> toNumber n / toNumber lcgN) <$> lcgStep
159162
160- foreign import float32ToInt32
161- " function float32ToInt32(n) {\
162- \ var arr = new ArrayBuffer(4);\
163- \ var fv = new Float32Array(arr);\
164- \ var iv = new Int32Array(arr);\
165- \ fv[0] = n;\
166- \ return iv[0];\
167- \}" :: Number -> Int
163+ foreign import float32ToInt32 :: Number -> Int
168164
169165-- | Perturb a random generator by modifying the current seed
170166perturbGen :: forall a . Number -> Gen a -> Gen a
171167perturbGen n (Gen f) = Gen $ \s -> f (s { newSeed = lcgNext (float32ToInt32 n) + s.newSeed })
172168
173169instance functorGen :: Functor Gen where
174- (<$>) f (Gen g) = Gen $ \s -> case g s of
170+ map f (Gen g) = Gen $ \s -> case g s of
175171 { value = value, state = state } -> { value: f value, state: state }
176172
177173instance applyGen :: Apply Gen where
178- (<*>) (Gen f) (Gen x) = Gen $ \s ->
174+ apply (Gen f) (Gen x) = Gen $ \s ->
179175 case f s of
180176 { value = f', state = s' } -> case x s' of
181177 { value = x', state = s'' } -> { value: f' x', state: s'' }
@@ -184,7 +180,7 @@ instance applicativeGen :: Applicative Gen where
184180 pure a = Gen (\s -> { value: a, state: s })
185181
186182instance bindGen :: Bind Gen where
187- (>>=) (Gen f) g = Gen $ \s -> case f s of
183+ bind (Gen f) g = Gen $ \s -> case f s of
188184 { value = value, state = state } -> runGen (g value) state
189185
190186instance monadGen :: Monad Gen
0 commit comments