Skip to content

Commit

Permalink
Rename splitFrozenGen to splitGen
Browse files Browse the repository at this point in the history
  • Loading branch information
lehins committed Nov 24, 2023
1 parent 9199b34 commit 14ce716
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 20 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* Move `thawGen` from `FreezeGen` into the new `ThawGen` type class. Fixes an issue with
an unlawful instance of `StateGen` for `FreezeGen`.
* Add `modifyGen` and `overwriteGen` to the `FrozenGen` type class
* Add `splitFrozenGen` and `splitMutableGen`
* Add `splitGen` and `splitMutableGen`
* Switch `randomM` and `randomRM` to use `FrozenGen` instead of `RandomGenM`
* Deprecate `RandomGenM` in favor of a more powerful `FrozenGen`
* Add `isInRange` to `UniformRange`: [#78](https://github.com/haskell/random/pull/78)
Expand Down
17 changes: 4 additions & 13 deletions src/System/Random/Internal.hs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ module System.Random.Internal
, StatefulGen(..)
, FrozenGen(..)
, ThawedGen(..)
, splitFrozenGen
, splitGen
, splitMutableGen

-- ** Standard pseudo-random number generator
Expand All @@ -43,7 +43,6 @@ module System.Random.Internal
-- ** Pure adapter
, StateGen(..)
, StateGenM(..)
, splitGen
, runStateGen
, runStateGen_
, runStateGenT
Expand Down Expand Up @@ -372,15 +371,15 @@ class FrozenGen f m => ThawedGen f m where
-- wrapper with one of the resulting generators and returns the other.
--
-- @since 1.3.0
splitFrozenGen :: (RandomGen f, FrozenGen f m) => MutableGen f m -> m f
splitFrozenGen = flip modifyGen split
splitGen :: (RandomGen f, FrozenGen f m) => MutableGen f m -> m f
splitGen = flip modifyGen split

-- | Splits a pseudo-random number generator into two. Overwrites the mutable wrapper with
-- one of the resulting generators and returns the other as a new mutable generator.
--
-- @since 1.3.0
splitMutableGen :: (RandomGen f, ThawedGen f m) => MutableGen f m -> m (MutableGen f m)
splitMutableGen = splitFrozenGen >=> thawGen
splitMutableGen = splitGen >=> thawGen


data MBA = MBA (MutableByteArray# RealWorld)
Expand Down Expand Up @@ -533,14 +532,6 @@ instance (RandomGen g, MonadState g m) => FrozenGen (StateGen g) m where
overwriteGen _ f = put (coerce f)
{-# INLINE overwriteGen #-}

-- | Splits a pseudo-random number generator into two. Updates the state with
-- one of the resulting generators and returns the other.
--
-- @since 1.2.0
splitGen :: (MonadState g m, RandomGen g) => m g
splitGen = state split
{-# INLINE splitGen #-}

-- | Runs a monadic generating action in the `State` monad using a pure
-- pseudo-random number generator.
--
Expand Down
2 changes: 1 addition & 1 deletion src/System/Random/Stateful.hs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ module System.Random.Stateful
, withMutableGen_
, randomM
, randomRM
, splitFrozenGen
, splitGen
, splitMutableGen

-- ** Deprecated
Expand Down
10 changes: 5 additions & 5 deletions test/Spec/Stateful.hs
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ splitMutableGenSpec ::
-> Property IO
splitMutableGenSpec toIO frozen =
monadic $ toIO $ do
(sfg1, fg1) <- withMutableGen frozen splitFrozenGen
(sfg1, fg1) <- withMutableGen frozen splitGen
(smg2, fg2) <- withMutableGen frozen splitMutableGen
sfg3 <- freezeGen smg2
pure $ fg1 == fg2 && sfg1 == sfg3
Expand All @@ -128,7 +128,7 @@ frozenGenSpecFor toIO px =
forAll $ \(fs :: [f]) -> indepMutableGenSpec toIO fs
, testProperty "immutable frozen generators" $
forAll $ \(f :: f) -> immutableFrozenGenSpec toIO f
, testProperty "splitFrozen" $
, testProperty "splitGen" $
forAll $ \(f :: f) -> splitMutableGenSpec toIO f
]

Expand Down Expand Up @@ -171,14 +171,14 @@ statefulSpecFor fromStdGen toStdGen runStatefulGen =
statefulSpec :: TestTree
statefulSpec =
testGroup
"Stateful"
[ testGroup "FrozenGen"
"StatefulGen"
[ testGroup "ThawedGen"
[ frozenGenSpecFor id (Proxy :: Proxy (IOGen StdGen))
, frozenGenSpecFor id (Proxy :: Proxy (AtomicGen StdGen))
, frozenGenSpecFor stToIO (Proxy :: Proxy (STGen StdGen))
, frozenGenSpecFor atomically (Proxy :: Proxy (TGen StdGen))
]
, testGroup "StatefulGen"
, testGroup "FrozenGen"
[ statefulSpecFor StateGen unStateGen runStateGenT
, statefulSpecFor IOGen unIOGen $ \g action -> do
mg <- newIOGenM (unIOGen g)
Expand Down

0 comments on commit 14ce716

Please sign in to comment.