random-fu-0.1.0.0: Random number generation

Data.Random.Source

Synopsis

Documentation

class Monad m => MonadRandom m whereSource

A typeclass for monads with a chosen source of entropy. For example, RVar is such a monad - the source from which it is (eventually) sampled is the only source from which a random variable is permitted to draw, so when directly requesting entropy for a random variable these functions are used.

The minimal definition is supportedPrims and getSupportedRandomPrim with cases for those primitives where supportedPrims returns True.

It is recommended (despite the warnings it generates) that, even when all primitives are supported, a final wildcard case of supportedPrims is specified, as:

 supportedPrims _ _ = False

The overlapping pattern warnings can be suppressed (without suppressing other, genuine, overlapping-pattern warnings) by the GHC flag -fno-warn-simple-patterns. This is not actually the documented behavior of that flag as far as I can find in 3 google-minutes, but it works with GHC 6.12.1 anyway, and that's good enough for me.

Note that it is very important that at least supportedPrims (and preferably getSupportedRandomPrim as well) gets inlined into the default implementation of getRandomPrim. If your supportedPrims is more than about 2 or 3 cases, add an INLINE pragma so that it can be optimized out of getRandomPrim.

Methods

supportedPrims :: m () -> Prim t -> BoolSource

Predicate indicating whether a given primitive is supported by the instance. The first parameter is a phantom used to select the instance.

getSupportedRandomPrim :: Prim t -> m tSource

Generate a random value corresponding to the specified primitive. Will not be called unless supportedPrims returns true for that primitive.

getRandomPrim :: Prim t -> m tSource

Generate a random value corresponding to the specified primitive. The default implementation makes use of supportedPrims and getSupportedRandomPrim to construct any required Prim out of the supported ones.

class Monad m => RandomSource m s whereSource

A source of entropy which can be used in the given monad.

The minimal definition is supportedPrimsFrom and getSupportedRandomPrimFrom with cases for those primitives where supportedPrimsFrom returns True.

Note that it is very important that at least supportedPrimsFrom (and preferably getSupportedRandomPrimFrom as well) gets inlined into the default implementation of getRandomPrimFrom. If your supportedPrimsFrom is more than about 2 or 3 cases, add an INLINE pragma so that it can be optimized out of getRandomPrimFrom.

See also MonadRandom.

Methods

supportedPrimsFrom :: Tagged (m ()) s -> Prim t -> BoolSource

Predicate indicating whether a given primitive is supported by the instance. The tag on the first parameter is a phantom used only to select the instance, but the value itself may be inspected.

getSupportedRandomPrimFrom :: s -> Prim t -> m tSource

Generate a random value corresponding to the specified primitive

getRandomPrimFrom :: s -> Prim t -> m tSource

Generate a random value corresponding to the specified primitive. The default implementation makes use of supportedPrimsFrom and getSupportedRandomPrimFrom to construct any required Prim out of the supported ones.

data Prim a whereSource

A Prompt GADT describing a request for a primitive random variate. Random variable definitions will request their entropy via these prompts, and entropy sources will satisfy some or all of them. The decomposePrimWhere function extends an entropy source's incomplete definition to a complete definition, essentially defining a very flexible implementation-defaulting system.

Some possible future additions: PrimFloat :: Prim Float PrimInt :: Prim Int PrimPair :: Prim a -> Prim b -> Prim (a :*: b) PrimNormal :: Prim Double PrimChoice :: [(Double :*: a)] -> Prim a

Unfortunately, I cannot get Haddock to accept my comments about the data constructors, but hopefully they should be reasonably self-explanatory.

Instances