- class Monad m => MonadRandom m where
- supportedPrims :: m () -> Prim t -> Bool
- getSupportedRandomPrim :: Prim t -> m t
- getRandomPrim :: Prim t -> m t
- class Monad m => RandomSource m s where
- supportedPrimsFrom :: Tagged (m ()) s -> Prim t -> Bool
- getSupportedRandomPrimFrom :: s -> Prim t -> m t
- getRandomPrimFrom :: s -> Prim t -> m t
- data Prim a where
- PrimWord8 :: Prim Word8
- PrimWord16 :: Prim Word16
- PrimWord32 :: Prim Word32
- PrimWord64 :: Prim Word64
- PrimDouble :: Prim Double
- PrimNByteInteger :: !Int -> Prim Integer
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
.
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.
MonadRandom (State PureMT) | |
MonadRandom (State StdGen) | |
MonadRandom (State PureMT) | |
MonadRandom (State StdGen) | |
MonadRandom (RVarT n) | |
Monad m => MonadRandom (StateT PureMT m) | |
Monad m => MonadRandom (StateT StdGen m) | |
Monad m => MonadRandom (StateT PureMT m) | |
Monad m => MonadRandom (StateT StdGen m) |
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
.
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.
RandomSource IO DevRandom | |
MonadRandom m => RandomSource m StdRandom | |
RandomSource IO (Gen RealWorld) | |
Monad m => RandomSource m (m Double) | |
Monad m => RandomSource m (m Word64) | |
Monad m => RandomSource m (m Word32) | |
Monad m => RandomSource m (m Word16) | |
Monad m => RandomSource m (m Word8) | |
(Monad m, ModifyRef (IORef StdGen) m StdGen) => RandomSource m (IORef StdGen) | |
(Monad m, ModifyRef (IORef PureMT) m PureMT) => RandomSource m (IORef PureMT) | |
(Monad m, ModifyRef (STRef s StdGen) m StdGen) => RandomSource m (STRef s StdGen) | |
(Monad m1, ModifyRef (Ref m2 StdGen) m1 StdGen) => RandomSource m1 (Ref m2 StdGen) | |
(Monad m, ModifyRef (STRef s PureMT) m PureMT) => RandomSource m (STRef s PureMT) | |
(Monad m1, ModifyRef (Ref m2 PureMT) m1 PureMT) => RandomSource m1 (Ref m2 PureMT) | |
RandomSource (ST s) (Gen s) |
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.
PrimWord8 :: Prim Word8 | |
PrimWord16 :: Prim Word16 | |
PrimWord32 :: Prim Word32 | |
PrimWord64 :: Prim Word64 | |
PrimDouble :: Prim Double | |
PrimNByteInteger :: !Int -> Prim Integer |