Safe Haskell | None |
---|---|
Language | Haskell2010 |
Synopsis
- data Random m k
- = Random a => Random (a -> k)
- | Random a => RandomR (a, a) (a -> k)
- | Interleave (m a) (a -> k)
- runRandom :: (Carrier sig m, Effect sig, Monad m, RandomGen g) => g -> Eff (RandomC g m) a -> m (g, a)
- evalRandom :: (Carrier sig m, Effect sig, Monad m, RandomGen g) => g -> Eff (RandomC g m) a -> m a
- execRandom :: (Carrier sig m, Effect sig, Monad m, RandomGen g) => g -> Eff (RandomC g m) a -> m g
- evalRandomIO :: (Carrier sig m, Effect sig, MonadIO m) => Eff (RandomC StdGen m) a -> m a
- newtype RandomC g m a = RandomC {
- runRandomC :: g -> m (g, a)
- class Monad m => MonadRandom (m :: * -> *) where
- class MonadRandom m => MonadInterleave (m :: * -> *) where
Documentation
Random a => Random (a -> k) | |
Random a => RandomR (a, a) (a -> k) | |
Interleave (m a) (a -> k) |
runRandom :: (Carrier sig m, Effect sig, Monad m, RandomGen g) => g -> Eff (RandomC g m) a -> m (g, a) Source #
Run a random computation starting from a given generator.
run (runRandom (PureGen a) (pure b)) == (PureGen a, b)
evalRandom :: (Carrier sig m, Effect sig, Monad m, RandomGen g) => g -> Eff (RandomC g m) a -> m a Source #
Run a random computation starting from a given generator and discarding the final generator.
run (evalRandom (PureGen a) (pure b)) == b
execRandom :: (Carrier sig m, Effect sig, Monad m, RandomGen g) => g -> Eff (RandomC g m) a -> m g Source #
Run a random computation starting from a given generator and discarding the final result.
run (execRandom (PureGen a) (pure b)) == PureGen a
evalRandomIO :: (Carrier sig m, Effect sig, MonadIO m) => Eff (RandomC StdGen m) a -> m a Source #
Run a random computation in IO
, splitting the global standard generator to get a new one for the computation.
newtype RandomC g m a Source #
RandomC | |
|
class Monad m => MonadRandom (m :: * -> *) where #
With a source of random number supply in hand, the MonadRandom
class
allows the programmer to extract random values of a variety of types.
getRandomR :: Random a => (a, a) -> m a #
Takes a range (lo,hi) and a random number generator g, and returns a computation that returns a random value uniformly distributed in the closed interval [lo,hi], together with a new generator. It is unspecified what happens if lo>hi. For continuous types there is no requirement that the values lo and hi are ever produced, but they may be, depending on the implementation and the interval.
See randomR
for details.
getRandom :: Random a => m a #
The same as getRandomR
, but using a default range determined by the type:
- For bounded types (instances of
Bounded
, such asChar
), the range is normally the whole type. - For fractional types, the range is normally the semi-closed interval
[0,1)
. - For
Integer
, the range is (arbitrarily) the range ofInt
.
See random
for details.
getRandomRs :: Random a => (a, a) -> m [a] #
Plural variant of getRandomR
, producing an infinite list of
random values instead of returning a new generator.
See randomRs
for details.
getRandoms :: Random a => m [a] #
Instances
class MonadRandom m => MonadInterleave (m :: * -> *) where #
The class MonadInterleave
proivides a convenient interface atop
a split
operation on a random generator.
interleave :: m a -> m a #
If x :: m a
is a computation in some random monad, then
interleave x
works by splitting the generator, running x
using one half, and using the other half as the final generator
state of interleave x
(replacing whatever the final generator
state otherwise would have been). This means that computation
needing random values which comes after interleave x
does not
necessarily depend on the computation of x
. For example:
>>> evalRandIO $ snd <$> ((,) <$> undefined <*> getRandom) *** Exception: Prelude.undefined >>> evalRandIO $ snd <$> ((,) <$> interleave undefined <*> getRandom) 6192322188769041625
This can be used, for example, to allow random computations to
run in parallel, or to create lazy infinite structures of
random values. In the example below, the infinite tree
randTree
cannot be evaluated lazily: even though it is cut
off at two levels deep by hew 2
, the random value in the
right subtree still depends on generation of all the random
values in the (infinite) left subtree, even though they are
ultimately unneeded. Inserting a call to interleave
, as in
randTreeI
, solves the problem: the generator splits at each
Node
, so random values in the left and right subtrees are
generated independently.
data Tree = Leaf | Node Int Tree Tree deriving Show hew :: Int -> Tree -> Tree hew 0 _ = Leaf hew _ Leaf = Leaf hew n (Node x l r) = Node x (hew (n-1) l) (hew (n-1) r) randTree :: Rand StdGen Tree randTree = Node <$> getRandom <*> randTree <*> randTree randTreeI :: Rand StdGen Tree randTreeI = interleave $ Node <$> getRandom <*> randTreeI <*> randTreeI
>>> hew 2 <$> evalRandIO randTree Node 2168685089479838995 (Node (-1040559818952481847) Leaf Leaf) (Node ^CInterrupted. >>> hew 2 <$> evalRandIO randTreeI Node 8243316398511136358 (Node 4139784028141790719 Leaf Leaf) (Node 4473998613878251948 Leaf Leaf)