From cd3b9d6217d48328703d14d91a883f2cd638adeb Mon Sep 17 00:00:00 2001 From: Leonhard Markert Date: Thu, 7 May 2020 14:53:25 +0200 Subject: [PATCH] StdGen: constructor accessible via Internal only Fixes https://github.com/haskell/random/issues/59 by making 'StdGen' not an instance of 'Read'. --- System/Random.hs | 2 +- System/Random/Internal.hs | 17 ++++++++++------- System/Random/Monad.hs | 2 +- 3 files changed, 12 insertions(+), 9 deletions(-) diff --git a/System/Random.hs b/System/Random.hs index 3741ce0d3..8a93e5468 100644 --- a/System/Random.hs +++ b/System/Random.hs @@ -250,7 +250,7 @@ getStdGen :: MonadIO m => m StdGen getStdGen = liftIO $ readIORef theStdGen theStdGen :: IORef StdGen -theStdGen = unsafePerformIO $ SM.initSMGen >>= newIORef +theStdGen = unsafePerformIO $ SM.initSMGen >>= newIORef . StdGen {-# NOINLINE theStdGen #-} -- |Applies 'split' to the current global pseudo-random generator, diff --git a/System/Random/Internal.hs b/System/Random/Internal.hs index 76e101660..42682d395 100644 --- a/System/Random/Internal.hs +++ b/System/Random/Internal.hs @@ -32,7 +32,7 @@ module System.Random.Internal , Frozen(..) -- ** Standard pseudo-random number generator - , StdGen + , StdGen(..) , mkStdGen -- * Monadic adapters for pure pseudo-random number generators @@ -402,13 +402,16 @@ runPureGenST g action = runST $ runGenStateT g $ action -- | The standard pseudo-random number generator. -type StdGen = SM.SMGen +newtype StdGen = StdGen { unStdGen :: SM.SMGen } + deriving (Show) instance RandomGen StdGen where - next = SM.nextInt - genWord32 = SM.nextWord32 - genWord64 = SM.nextWord64 - split = SM.splitSMGen + next = second StdGen . SM.nextInt . unStdGen + genWord32 = second StdGen . SM.nextWord32 . unStdGen + genWord64 = second StdGen . SM.nextWord64 . unStdGen + split g = (StdGen g1, StdGen g2) + where + (g1, g2) = SM.splitSMGen $ unStdGen g instance RandomGen SM32.SMGen where next = SM32.nextInt @@ -418,7 +421,7 @@ instance RandomGen SM32.SMGen where -- | Constructs a 'StdGen' deterministically. mkStdGen :: Int -> StdGen -mkStdGen s = SM.mkSMGen $ fromIntegral s +mkStdGen = StdGen . SM.mkSMGen . fromIntegral -- | The class of types for which a uniformly distributed value can be drawn -- from all possible values of the type. diff --git a/System/Random/Monad.hs b/System/Random/Monad.hs index 309e8bfad..f5250e346 100644 --- a/System/Random/Monad.hs +++ b/System/Random/Monad.hs @@ -216,7 +216,7 @@ instance RandomGen r => RandomGenM STGen r s (ST s) where -- -- >>> import Data.Int (Int8) -- >>> runGenM (IOGen (mkStdGen 217)) (`uniformListM` 5) :: IO ([Int8], Frozen (IOGen StdGen)) --- ([-74,37,-50,-2,3],IOGen {unIOGen = SMGen 4273268533320920145 15251669095119325999}) +-- ([-74,37,-50,-2,3],IOGen {unIOGen = StdGen {unStdGen = SMGen 4273268533320920145 15251669095119325999}}) -- -- @since 1.2 runGenM :: MonadRandom g s m => Frozen g -> (g s -> m a) -> m (a, Frozen g)