ModuleMonadRandom-0.6.2.1Haskell2010
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.
Contents
Synopsis
- type Rand
- value liftRand
- value runRand
- value evalRand
- value execRand
- value mapRand
- value withRand
- value evalRandIO
- newtype RandT
- value liftRandT
- value runRandT
- value evalRandT
- value execRandT
- value mapRandT
- value withRandT
- value evalRandTIO
- value liftCallCC
- value liftCallCC'
- value liftCatch
- value liftListen
- value liftPass
- data RandGen
- value withRandGen
- value withRandGen_
- PackageMonadRandom-0.6.2.1
- Exports24
- LanguageHaskell2010
- LicenceBSD-3-Clause
- SourceStrict.hs
The Rand monad transformer
liftRand :: (g -> (a, g))pure random transformer
-> Rand g aequivalent generator-passing computation
Construct a random monad computation from a function. (The inverse of runRand.)
runRand :: Rand g agenerator-passing computation to execute
-> ginitial generator
-> (a, g)return value and final generator
Unwrap a random monad computation as a function. (The inverse of liftRand.)
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
Instances22MonadRWS, MonadError, MonadReader, MonadState, MonadWriter, MonadSplit, …
(MonadReader r m, MonadWriter w m, MonadState s m) => MonadRWS r w s (RandT g m)Defined in MonadRandom-0.6.2.1 · Control.Monad.Trans.Random.StrictMonadError e m => MonadError e (RandT g m)Defined in MonadRandom-0.6.2.1 · Control.Monad.Trans.Random.StrictMonadReader r m => MonadReader r (RandT g m)Defined in MonadRandom-0.6.2.1 · Control.Monad.Trans.Random.StrictMonadState s m => MonadState s (RandT g m)Defined in MonadRandom-0.6.2.1 · Control.Monad.Trans.Random.StrictMonadWriter w m => MonadWriter w (RandT g m)Defined in MonadRandom-0.6.2.1 · Control.Monad.Trans.Random.Strict(RandomGen g, Monad m) => MonadSplit g (RandT g m)Defined in MonadRandom-0.6.2.1 · Control.Monad.Trans.Random.StrictMonadTrans (RandT g)Defined in MonadRandom-0.6.2.1 · Control.Monad.Trans.Random.Strict(Monad m, RandomGen g) => RandomGenM (RandGen g) g (RandT g m)Defined in MonadRandom-0.6.2.1 · Control.Monad.Trans.Random.Strict(Monad m, RandomGen g) => StatefulGen (RandGen g) (RandT g m)Defined in MonadRandom-0.6.2.1 · Control.Monad.Trans.Random.StrictMonad m => Monad (RandT g m)Defined in MonadRandom-0.6.2.1 · Control.Monad.Trans.Random.StrictFunctor m => Functor (RandT g m)Defined in MonadRandom-0.6.2.1 · Control.Monad.Trans.Random.StrictMonadFix m => MonadFix (RandT g m)Defined in MonadRandom-0.6.2.1 · Control.Monad.Trans.Random.StrictMonadFail m => MonadFail (RandT g m)Defined in MonadRandom-0.6.2.1 · Control.Monad.Trans.Random.StrictMonad m => Applicative (RandT g m)Defined in MonadRandom-0.6.2.1 · Control.Monad.Trans.Random.StrictMonadPlus m => Alternative (RandT g m)Defined in MonadRandom-0.6.2.1 · Control.Monad.Trans.Random.StrictMonadPlus m => MonadPlus (RandT g m)Defined in MonadRandom-0.6.2.1 · Control.Monad.Trans.Random.StrictMonadIO m => MonadIO (RandT g m)Defined in MonadRandom-0.6.2.1 · Control.Monad.Trans.Random.StrictMonadCont m => MonadCont (RandT g m)Defined in MonadRandom-0.6.2.1 · Control.Monad.Trans.Random.StrictPrimMonad m => PrimMonad (RandT s m)Defined in MonadRandom-0.6.2.1 · Control.Monad.Trans.Random.Strict(Monad m, RandomGen g) => MonadInterleave (RandT g m)Defined in MonadRandom-0.6.2.1 · Control.Monad.Trans.Random.Strict(RandomGen g, Monad m) => MonadRandom (RandT g m)Defined in MonadRandom-0.6.2.1 · Control.Monad.Trans.Random.Stricttype PrimState (RandT s m) = PrimState mDefined in MonadRandom-0.6.2.1 · Control.Monad.Trans.Random.Strict
liftRandT :: (g -> m (a, g))impure random transformer
-> RandT g m aequivalent generator-passing computation
Construct a random monad computation from an impure function. (The inverse of runRandT.)
runRandT :: RandT g m agenerator-passing computation to execute
-> ginitial generator
-> m (a, g)return value and final generator
Unwrap a random monad computation as an impure function. (The inverse of liftRandT.)
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
Uniform lifting of a callCC operation to the new monad.
This version rolls back to the original state on entering the
continuation.
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).
Lift a catchE operation to the new monad.
Lift a listen operation to the new monad.
Lift a pass operation to the new monad.
StatefulGen interface
A proxy that carries information about the type of generator to use with RandT
monad and its StatefulGen instance.
Instances4RandomGenM, StatefulGen
(Monad m, RandomGen g) => RandomGenM (RandGen g) g (RandT g m)Defined in MonadRandom-0.6.2.1 · Control.Monad.Trans.Random.Lazy(Monad m, RandomGen g) => RandomGenM (RandGen g) g (RandT g m)Defined in MonadRandom-0.6.2.1 · Control.Monad.Trans.Random.Strict(Monad m, RandomGen g) => StatefulGen (RandGen g) (RandT g m)Defined in MonadRandom-0.6.2.1 · Control.Monad.Trans.Random.Lazy(Monad m, RandomGen g) => StatefulGen (RandGen g) (RandT g m)Defined in MonadRandom-0.6.2.1 · Control.Monad.Trans.Random.Strict
withRandGen A RandT runner that allows using it with StatefulGen restricted actions. Returns the outcome of random computation and the new pseudo-random-number generator
withRandGen (mkStdGen 2021) uniformM :: IO (Int, StdGen)(6070831465987696718,StdGen {unStdGen = SMGen 4687568268719557181 4805600293067301895})
withRandGen_ Same as withRandGen, but discards the resulting generator.
withRandGen_ (mkStdGen 2021) uniformM :: IO Int6070831465987696718
Examples
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)