Rand Monad and Random Class #
This module provides tools for formulating computations guided by randomness and for defining objects that can be created randomly.
Main definitions #
RandT
andRandGT
monad transformers for computations guided by randomness;Rand
andRandG
monads as special cases of the aboveRandom
class for objects that can be generated randomly;random
to generate one object;
BoundedRandom
class for objects that can be generated randomly inside a range;randomR
to generate one object inside a range;
runRand
to run a randomized computation inside any monad that has access tostdGenRef
.
References #
- Similar library in Haskell: https://hackage.haskell.org/package/MonadRandom
A monad transformer to generate random objects using the generic generator type g
Equations
- Plausible.RandGT g = StateT (ULift g)
Instances For
A monad to generate random objects using the generator type g
.
Equations
Equations
- Plausible.instMonadLiftTRandGTOfMonadLift = { monadLift := fun {α : Type ?u.38} (x : Plausible.RandGT g m α) (s : ULift g) => liftM (x s) }
Random m α
gives us machinery to generate values of type α
in the monad m
.
Note that m
is a parameter as some types may only be sampleable with access to a certain monad.
Generate a value of type
α
randomly using generatorg
.
BoundedRandom m α
gives us machinery to generate values of type α
between certain bounds in
the monad m
.
Given a random generator for α
, we can convert it to a random generator for ULift α
.
Equations
- One or more equations did not get rendered due to their size.
Given a random generator for ULift α
, we can convert it to a random generator for α
.
Equations
- One or more equations did not get rendered due to their size.
Create a new random number generator distinct from the one stored in the state
Equations
- Plausible.Rand.split = do let __do_lift ← get let rng : g := __do_lift.down match RandomGen.split rng with | (r1, r2) => do set { down := r1 } pure r2
Given a random generator for α
, we can convert it to a random generator for ULift α
.
Equations
- Plausible.Rand.up x = Plausible.RandT.up (fun {α : Type ?u.32} (x : Id α) => pure { down := x.run }) x
Given a random generator for ULift α
, we can convert it to a random generator for α
.
Equations
- Plausible.Rand.down x = Plausible.RandT.down (fun {α : Type ?u.32} (x : Id (ULift α)) => pure x.run.down) x
Generate a random value of type α
between x
and y
inclusive.
Equations
- Plausible.Random.randBound α lo hi h = Plausible.BoundedRandom.randomR lo hi h
Equations
- Plausible.Random.instFinSucc = { random := fun {g : Type} [RandomGen g] => Plausible.Random.randFin }
Equations
- Plausible.Random.instBool = { random := fun {g : Type} [RandomGen g] => Plausible.Random.randBool }
Equations
- One or more equations did not get rendered due to their size.
Equations
- One or more equations did not get rendered due to their size.
Equations
- One or more equations did not get rendered due to their size.
Equations
- One or more equations did not get rendered due to their size.
Computes a RandT m α
using the global stdGenRef
as RNG.
Note that:
stdGenRef
is not necessarily properly seeded on program startup as of now and will therefore be deterministic.stdGenRef
is not thread local, hence two threads accessing it at the same time will get the exact same generator.
Equations
- One or more equations did not get rendered due to their size.
Run the random computaton cmd
with seed
for the RNG.
Equations
- Plausible.runRandWith seed cmd = do let __do_lift ← StateT.run cmd { down := mkStdGen seed } pure __do_lift.fst