ModuleMonadRandom-0.6.2Haskell2010
Control.Monad.Trans.Random.Lazy
Lazy random monads, passing a random number generator through a computation. See below for examples.
For a strict version with the same interface, see Control.Monad.Trans.Random.Strict.
- 3 types
- 21 values
- PackageMonadRandom-0.6.2
- Exports24
- LanguageHaskell2010
- LicenceBSD-3-Clause
- SourceLazy.hs
The Rand monad transformer
8 declarationsliftRand :: (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
7 declarationsInstances22MonadRWS, 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 · Control.Monad.Trans.Random.LazyMonadError e m => MonadError e (RandT g m)Defined in MonadRandom-0.6.2 · Control.Monad.Trans.Random.LazyMonadReader r m => MonadReader r (RandT g m)Defined in MonadRandom-0.6.2 · Control.Monad.Trans.Random.LazyMonadState s m => MonadState s (RandT g m)Defined in MonadRandom-0.6.2 · Control.Monad.Trans.Random.LazyMonadWriter w m => MonadWriter w (RandT g m)Defined in MonadRandom-0.6.2 · Control.Monad.Trans.Random.Lazy(RandomGen g, Monad m) => MonadSplit g (RandT g m)Defined in MonadRandom-0.6.2 · Control.Monad.Trans.Random.LazyMonadTrans (RandT g)Defined in MonadRandom-0.6.2 · Control.Monad.Trans.Random.Lazy(Monad m, RandomGen g) => RandomGenM (RandGen g) g (RandT g m)Defined in MonadRandom-0.6.2 · Control.Monad.Trans.Random.Lazy(Monad m, RandomGen g) => StatefulGen (RandGen g) (RandT g m)Defined in MonadRandom-0.6.2 · Control.Monad.Trans.Random.LazyMonad m => Monad (RandT g m)Defined in MonadRandom-0.6.2 · Control.Monad.Trans.Random.LazyFunctor m => Functor (RandT g m)Defined in MonadRandom-0.6.2 · Control.Monad.Trans.Random.LazyMonadFix m => MonadFix (RandT g m)Defined in MonadRandom-0.6.2 · Control.Monad.Trans.Random.LazyMonadFail m => MonadFail (RandT g m)Defined in MonadRandom-0.6.2 · Control.Monad.Trans.Random.LazyMonad m => Applicative (RandT g m)Defined in MonadRandom-0.6.2 · Control.Monad.Trans.Random.LazyMonadPlus m => Alternative (RandT g m)Defined in MonadRandom-0.6.2 · Control.Monad.Trans.Random.LazyMonadPlus m => MonadPlus (RandT g m)Defined in MonadRandom-0.6.2 · Control.Monad.Trans.Random.LazyMonadIO m => MonadIO (RandT g m)Defined in MonadRandom-0.6.2 · Control.Monad.Trans.Random.LazyMonadCont m => MonadCont (RandT g m)Defined in MonadRandom-0.6.2 · Control.Monad.Trans.Random.LazyPrimMonad m => PrimMonad (RandT s m)Defined in MonadRandom-0.6.2 · Control.Monad.Trans.Random.Lazy(Monad m, RandomGen g) => MonadInterleave (RandT g m)Defined in MonadRandom-0.6.2 · Control.Monad.Trans.Random.Lazy(RandomGen g, Monad m) => MonadRandom (RandT g m)Defined in MonadRandom-0.6.2 · Control.Monad.Trans.Random.Lazytype PrimState (RandT s m) = PrimState mDefined in MonadRandom-0.6.2 · Control.Monad.Trans.Random.Lazy
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.)
Lifting other operations
6 declarationsUniform 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.
Evaluate a random computation that is embedded in the IO monad, splitting the global standard generator to get a new one for the computation.
StatefulGen interface
3 declarationsA 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 · Control.Monad.Trans.Random.Lazy(Monad m, RandomGen g) => RandomGenM (RandGen g) g (RandT g m)Defined in MonadRandom-0.6.2 · Control.Monad.Trans.Random.Strict(Monad m, RandomGen g) => StatefulGen (RandGen g) (RandT g m)Defined in MonadRandom-0.6.2 · Control.Monad.Trans.Random.Lazy(Monad m, RandomGen g) => StatefulGen (RandGen g) (RandT g m)Defined in MonadRandom-0.6.2 · 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
0 declarationsRandom 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)