Skip to content

Commit ccbce47

Browse files
authored
Merge pull request #57 from natefaubion/2.0
Updates for 2.0
2 parents 63529ee + d3628ad commit ccbce47

File tree

6 files changed

+69
-78
lines changed

6 files changed

+69
-78
lines changed

bower.json

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,13 @@
2020
"package.json"
2121
],
2222
"dependencies": {
23-
"purescript-arrays": "^1.0.0",
24-
"purescript-console": "^1.0.0",
25-
"purescript-either": "^1.0.0",
26-
"purescript-exceptions": "^1.0.0",
27-
"purescript-lists": "^1.0.0",
28-
"purescript-random": "^1.0.0",
29-
"purescript-strings": "^1.0.0",
30-
"purescript-transformers": "^1.0.0"
23+
"purescript-arrays": "^3.0.0",
24+
"purescript-console": "^2.0.0",
25+
"purescript-either": "^2.0.0",
26+
"purescript-exceptions": "^2.0.0",
27+
"purescript-lists": "^2.0.0",
28+
"purescript-random": "^2.0.0",
29+
"purescript-strings": "^2.0.0",
30+
"purescript-transformers": "^2.0.0"
3131
}
3232
}

src/Test/QuickCheck.purs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,21 @@
1515
-- | ```purescript
1616
-- | main = quickCheck \n -> n + 1 > n
1717
-- | ```
18-
module Test.QuickCheck where
18+
module Test.QuickCheck
19+
( QC
20+
, quickCheck
21+
, quickCheck'
22+
, quickCheckPure
23+
, class Testable
24+
, test
25+
, Result(..)
26+
, withHelp
27+
, (<?>)
28+
, assertEquals
29+
, (===)
30+
, assertNotEquals
31+
, (/==)
32+
) where
1933

2034
import Prelude
2135

src/Test/QuickCheck/Arbitrary.purs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
1-
module Test.QuickCheck.Arbitrary where
1+
module Test.QuickCheck.Arbitrary
2+
( class Arbitrary
3+
, arbitrary
4+
, class Coarbitrary
5+
, coarbitrary
6+
) where
27

38
import Prelude
49

@@ -10,6 +15,7 @@ import Data.Int (toNumber)
1015
import Data.Lazy (Lazy, defer, force)
1116
import Data.List (List)
1217
import Data.Maybe (Maybe(..))
18+
import Data.Newtype (wrap)
1319
import Data.String (charCodeAt, fromCharArray, split)
1420
import Data.Tuple (Tuple(..))
1521

@@ -60,7 +66,7 @@ instance arbString :: Arbitrary String where
6066
arbitrary = fromCharArray <$> arbitrary
6167

6268
instance coarbString :: Coarbitrary String where
63-
coarbitrary s = coarbitrary $ (charCodeAt zero <$> split "" s)
69+
coarbitrary s = coarbitrary $ (charCodeAt zero <$> split (wrap "") s)
6470

6571
instance arbChar :: Arbitrary Char where
6672
arbitrary = fromCharCode <$> chooseInt 0 65536

src/Test/QuickCheck/Data/AlphaNumString.purs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
1-
module Test.QuickCheck.Data.AlphaNumString where
1+
module Test.QuickCheck.Data.AlphaNumString
2+
( AlphaNumString(..)
3+
) where
24

35
import Prelude
46

7+
import Data.Newtype (class Newtype)
58
import Data.String (fromCharArray, toCharArray)
69

710
import Test.QuickCheck.Gen (Gen, arrayOf, oneOf)
@@ -11,8 +14,7 @@ import Test.QuickCheck.Arbitrary (class Coarbitrary, class Arbitrary, coarbitrar
1114
-- | alphanumeric strings.
1215
newtype AlphaNumString = AlphaNumString String
1316

14-
runAlphaNumString :: AlphaNumString -> String
15-
runAlphaNumString (AlphaNumString s) = s
17+
derive instance newtypeAlphaNumString :: Newtype AlphaNumString _
1618

1719
instance arbAlphaNumString :: Arbitrary AlphaNumString where
1820
arbitrary = AlphaNumString <<< fromCharArray <$> arrayOf anyChar

src/Test/QuickCheck/Data/ApproxNumber.purs

Lines changed: 0 additions & 47 deletions
This file was deleted.

src/Test/QuickCheck/Gen.purs

Lines changed: 33 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
-- | module, as well as helper functions for constructing random generators.
33
module Test.QuickCheck.Gen
44
( Gen
5+
, unGen
56
, GenState
67
, Size
78
, repeatable
@@ -29,19 +30,22 @@ module Test.QuickCheck.Gen
2930

3031
import Prelude
3132

33+
import Control.Alt (class Alt)
3234
import Control.Monad.Eff (Eff)
3335
import Control.Monad.Eff.Random (RANDOM)
34-
import Control.Monad.Rec.Class (class MonadRec, tailRecM)
36+
import Control.Monad.Rec.Class (class MonadRec, Step(..), tailRecM)
3537
import Control.Monad.State (State, runState, evalState)
3638
import Control.Monad.State.Class (state, modify)
39+
import Control.Monad.State.Trans (StateT)
3740

3841
import Data.Array ((!!), length)
39-
import Data.Either (Either(..))
4042
import Data.Foldable (fold)
43+
import Data.Identity (Identity)
4144
import Data.Int (toNumber)
4245
import Data.List (List(..), toUnfoldable)
4346
import Data.Maybe (fromMaybe)
44-
import Data.Monoid.Additive (Additive(..), runAdditive)
47+
import Data.Monoid.Additive (Additive(..))
48+
import Data.Newtype (unwrap)
4549
import Data.Tuple (Tuple(..), fst, snd)
4650

4751
import Math as M
@@ -58,27 +62,39 @@ type GenState = { newSeed :: Seed, size :: Size }
5862
-- | The random generator monad
5963
-- |
6064
-- | `Gen` is a state monad which encodes a linear congruential generator.
61-
type Gen a = State GenState a
65+
newtype Gen a = Gen (StateT GenState Identity a)
66+
67+
derive newtype instance functorGen :: Functor Gen
68+
derive newtype instance applyGen :: Apply Gen
69+
derive newtype instance applicativeGen :: Applicative Gen
70+
derive newtype instance bindGen :: Bind Gen
71+
derive newtype instance monadGen :: Monad Gen
72+
derive newtype instance altGen :: Alt Gen
73+
derive newtype instance monadRecGen :: MonadRec Gen
74+
75+
-- | Exposes the underlying State implementation.
76+
unGen :: forall a. Gen a -> State GenState a
77+
unGen (Gen st) = st
6278

6379
-- | Create a random generator for a function type.
6480
repeatable :: forall a b. (a -> Gen b) -> Gen (a -> b)
65-
repeatable f = state $ \s -> Tuple (\a -> fst (runGen (f a) s)) (s { newSeed = lcgNext s.newSeed })
81+
repeatable f = Gen $ state \s -> Tuple (\a -> fst (runGen (f a) s)) (s { newSeed = lcgNext s.newSeed })
6682

6783
-- | Create a random generator which uses the generator state explicitly.
6884
stateful :: forall a. (GenState -> Gen a) -> Gen a
69-
stateful f = state $ \s -> runGen (f s) s
85+
stateful f = Gen $ state \s -> runGen (f s) s
7086

7187
-- | Modify a random generator by setting a new random seed.
7288
variant :: forall a. Seed -> Gen a -> Gen a
73-
variant n g = state $ \s -> runGen g s { newSeed = n }
89+
variant n g = Gen $ state \s -> runGen g s { newSeed = n }
7490

7591
-- | Create a random generator which depends on the size parameter.
7692
sized :: forall a. (Size -> Gen a) -> Gen a
7793
sized f = stateful (\s -> f s.size)
7894

7995
-- | Modify a random generator by setting a new size parameter.
8096
resize :: forall a. Size -> Gen a -> Gen a
81-
resize sz g = state $ \s -> runGen g s { size = sz }
97+
resize sz g = Gen $ state \s -> runGen g s { size = sz }
8298

8399
-- | Create a random generator which samples a range of `Number`s i
84100
-- | with uniform probability.
@@ -109,7 +125,7 @@ oneOf x xs = do
109125
frequency :: forall a. Tuple Number (Gen a) -> List (Tuple Number (Gen a)) -> Gen a
110126
frequency x xs = let
111127
xxs = Cons x xs
112-
total = runAdditive $ fold (map (Additive <<< fst) xxs :: List (Additive Number))
128+
total = unwrap $ fold (map (Additive <<< fst) xxs :: List (Additive Number))
113129
pick n d Nil = d
114130
pick n d (Cons (Tuple k x) xs) = if n <= k then x else pick (n - k) d xs
115131
in do
@@ -134,9 +150,9 @@ replicateMRec :: forall m a. MonadRec m => Int -> m a -> m (List a)
134150
replicateMRec k _ | k <= 0 = pure Nil
135151
replicateMRec k gen = tailRecM go (Tuple Nil k)
136152
where
137-
go :: (Tuple (List a) Int) -> m (Either (Tuple (List a) Int) (List a))
138-
go (Tuple acc 0) = pure $ Right acc
139-
go (Tuple acc n) = gen <#> \x -> Left (Tuple (Cons x acc) (n - 1))
153+
go :: (Tuple (List a) Int) -> m (Step (Tuple (List a) Int) (List a))
154+
go (Tuple acc 0) = pure $ Done acc
155+
go (Tuple acc n) = gen <#> \x -> Loop (Tuple (Cons x acc) (n - 1))
140156

141157
-- | Create a random generator which generates a list of random values of the specified size.
142158
listOf :: forall a. Int -> Gen a -> Gen (List a)
@@ -155,11 +171,11 @@ elements x xs = do
155171

156172
-- | Run a random generator
157173
runGen :: forall a. Gen a -> GenState -> Tuple a GenState
158-
runGen = runState
174+
runGen = runState <<< unGen
159175

160176
-- | Run a random generator, keeping only the randomly-generated result
161177
evalGen :: forall a. Gen a -> GenState -> a
162-
evalGen = evalState
178+
evalGen = evalState <<< unGen
163179

164180
-- | Sample a random generator
165181
sample :: forall a. Seed -> Size -> Gen a -> Array a
@@ -177,7 +193,7 @@ randomSample = randomSample' 10
177193

178194
-- | A random generator which simply outputs the current seed
179195
lcgStep :: Gen Int
180-
lcgStep = state f where
196+
lcgStep = Gen $ state f where
181197
f s = Tuple (runSeed s.newSeed) (s { newSeed = lcgNext s.newSeed })
182198

183199
-- | A random generator which approximates a uniform random variable on `[0, 1]`
@@ -188,6 +204,6 @@ foreign import float32ToInt32 :: Number -> Int
188204

189205
-- | Perturb a random generator by modifying the current seed
190206
perturbGen :: forall a. Number -> Gen a -> Gen a
191-
perturbGen n gen = do
207+
perturbGen n gen = Gen do
192208
modify \s -> s { newSeed = lcgPerturb (toNumber (float32ToInt32 n)) s.newSeed }
193-
gen
209+
unGen gen

0 commit comments

Comments
 (0)