HORIZON HASKELLDocslts/ghc-9.10.xc74966e2026-09-27Search names, modules, packages, or :: a typeCtrl K

GHC 9.10.3 · lts/ghc-9.10.x · c74966e · 2026-09-27

ModuleMonadRandom-0.6.2Haskell2010

Control.Monad.Trans.Random.Strict

Strict random monads, passing a random number generator through a computation. See below for examples.

In this version, sequencing of computations is strict (but computations are not strict in the state unless you force it with seq or the like). For a lazy version with the same interface, see Control.Monad.Trans.Random.Lazy.

  • 3 types
  • 21 values
  • PackageMonadRandom-0.6.2
  • Exports24
  • LanguageHaskell2010
  • LicenceBSD-3-Clause
  • SourceStrict.hs

The Rand monad transformer

8 declarations
typetype Rand g = RandT g Identity
#

A random monad parameterized by the type g of the generator to carry.

The return function leaves the generator unchanged, while >>= uses the final generator of the first computation as the initial generator of the second.

valueliftRand
  1. :: (g -> (a, g))

    pure random transformer

  2. -> Rand g a

    equivalent generator-passing computation

#

Construct a random monad computation from a function. (The inverse of runRand.)

valuerunRand
  1. :: Rand g a

    generator-passing computation to execute

  2. -> g

    initial generator

  3. -> (a, g)

    return value and final generator

#

Unwrap a random monad computation as a function. (The inverse of liftRand.)

valueevalRand
  1. :: Rand g a

    generator-passing computation to execute

  2. -> g

    initial generator

  3. -> a

    return value of the random computation

#

Evaluate a random computation with the given initial generator and return the final value, discarding the final generator.

valueexecRand
  1. :: Rand g a

    generator-passing computation to execute

  2. -> g

    initial generator

  3. -> g

    final generator

#

Evaluate a random computation with the given initial generator and return the final generator, discarding the final value.

valuemapRand :: ((a, g) -> (b, g)) -> Rand g a -> Rand g b
#

Map both the return value and final generator of a computation using the given function.

valueevalRandIO :: Rand StdGen a -> IO a
#

Evaluate a random computation in the IO monad, splitting the global standard generator to get a new one for the computation.

The RandT monad transformer

8 declarations
newtypenewtype RandT g (m :: Type -> Type) a
#

A random transformer monad parameterized by:

  • g - The generator.

  • m - The inner monad.

The return function leaves the generator unchanged, while >>= uses the final generator of the first computation as the initial generator of the second.

Instances22MonadRWS, MonadError, MonadReader, MonadState, MonadWriter, MonadSplit, …
valueliftRandT
  1. :: (g -> m (a, g))

    impure random transformer

  2. -> RandT g m a

    equivalent generator-passing computation

#

Construct a random monad computation from an impure function. (The inverse of runRandT.)

valuerunRandT
  1. :: RandT g m a

    generator-passing computation to execute

  2. -> g

    initial generator

  3. -> m (a, g)

    return value and final generator

#

Unwrap a random monad computation as an impure function. (The inverse of liftRandT.)

valueevalRandT :: Monad m => RandT g m a -> g -> m a
#

Evaluate a random computation with the given initial generator and return the final value, discarding the final generator.

valueexecRandT :: Monad m => RandT g m a -> g -> m g
#

Evaluate a random computation with the given initial generator and return the final generator, discarding the final value.

valueevalRandTIO :: MonadIO m => RandT StdGen m a -> m a
#

Evaluate a random computation that is embedded in the IO monad, splitting the global standard generator to get a new one for the computation.

Lifting other operations

5 declarations
valueliftCallCC :: CallCC m (a, g) (b, g) -> CallCC (RandT g m) a b
#

Uniform lifting of a callCC operation to the new monad. This version rolls back to the original state on entering the continuation.

valueliftCallCC' :: CallCC m (a, g) (b, g) -> CallCC (RandT g m) a b
#

In-situ lifting of a callCC operation to the new monad. This version uses the current state on entering the continuation. It does not satisfy the uniformity property (see Control.Monad.Signatures).

StatefulGen interface

3 declarations
datadata RandGen g
#

A proxy that carries information about the type of generator to use with RandT monad and its StatefulGen instance.

Instances4RandomGenM, StatefulGen
valuewithRandGen
  1. :: g

    initial generator

  2. -> (RandGen g -> RandT g m a)
  3. -> m (a, g)

    return value and final generator

#

A RandT runner that allows using it with StatefulGen restricted actions. Returns the outcome of random computation and the new pseudo-random-number generator

Example1 expression
withRandGen (mkStdGen 2021) uniformM :: IO (Int, StdGen)(6070831465987696718,StdGen {unStdGen = SMGen 4687568268719557181 4805600293067301895})
valuewithRandGen_
  1. :: Monad m
  2. => g

    initial generator

  3. -> (RandGen g -> RandT g m a)
  4. -> m a

    return value and final generator

#

Same as withRandGen, but discards the resulting generator.

Example1 expression
withRandGen_ (mkStdGen 2021) uniformM :: IO Int6070831465987696718

Examples

0 declarations

Random monads

The die function simulates the roll of a die, picking a number between 1 and 6, inclusive, and returning it in the Rand monad transformer. Notice that this code will work with any random number generator g.

die :: (RandomGen g) => Rand g Int
die = getRandomR (1, 6)

The dice function uses replicate and sequence to simulate the roll of n dice.

dice :: (RandomGen g) => Int -> Rand g [Int]
dice n = sequence (replicate n die)

To extract a value from the Rand monad transformer, we can use evalRandIO.

main = do
  values <- evalRandIO (dice 2)
  putStrLn (show values)