Skip to content

Commit

Permalink
StdGen: constructor accessible via Internal only
Browse files Browse the repository at this point in the history
Fixes haskell#59 by making 'StdGen' not
an instance of 'Read'.
  • Loading branch information
curiousleo committed May 7, 2020
1 parent a091bfc commit cd3b9d6
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 9 deletions.
2 changes: 1 addition & 1 deletion System/Random.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
17 changes: 10 additions & 7 deletions System/Random/Internal.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion System/Random/Monad.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit cd3b9d6

Please sign in to comment.