Copyright (c) Brent Yorgey 2016 BSD3 (see LICENSE) byorgey@gmail.com experimental non-portable (multi-param classes, functional dependencies, undecidable instances) Safe Haskell2010

Description

Random monads that are strict in the generator state. For a lazy version, see Control.Monad.Random.Lazy, which has the same interface.

Synopsis

type 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.

Arguments

 :: (g -> (a, g)) pure random transformer -> Rand g a equivalent generator-passing computation

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

Arguments

 :: Rand g a generator-passing computation to execute -> g initial generator -> (a, g) return value and final generator

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

Arguments

 :: Rand g a generator-passing computation to execute -> g initial generator -> 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.

• evalRand m s = fst (runRand m s)

Arguments

 :: Rand g a generator-passing computation to execute -> g initial generator -> g final generator

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

• execRand m s = snd (runRand m s)

mapRand :: ((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.

• runRand (mapRand f m) = f . runRand m

withRand :: (g -> g) -> Rand g a -> Rand g a #

withRand f m executes action m on a generator modified by applying f.

• withRand f m = modify f >> m

evalRandIO :: 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.

data RandT g m 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.

Instances

Arguments

 :: (g -> m (a, g)) impure random transformer -> RandT g m a equivalent generator-passing computation

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

Arguments

 :: RandT g m a generator-passing computation to execute -> g initial generator -> m (a, g) return value and final generator

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

evalRandT :: 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.

• evalRandT m g = liftM fst (runRandT m g)

execRandT :: 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.

• execRandT m g = liftM snd (runRandT m g)

mapRandT :: (m (a, g) -> n (b, g)) -> RandT g m a -> RandT g n b #

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

• runRandT (mapRandT f m) = f . runRandT m

withRandT :: (g -> g) -> RandT g m a -> RandT g m a #

withRandT f m executes action m on a generator modified by applying f.

• withRandT f m = modify f >> m

evalRandTIO :: 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.