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;
IO.runRand
to run a randomized computation inside any monad that has access tostdGenRef
.
References #
- Similar library in Haskell: https://hackage.haskell.org/package/MonadRandom
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.
Instances
BoundedRandom m α
gives us machinery to generate values of type α
between certain bounds in
the monad m
.
Instances
Create a new random number generator distinct from the one stored in the state
Equations
- 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
Instances For
def
Random.randBound
{m : Type u → Type u_1}
{g : Type}
(α : Type u)
[Preorder α]
[BoundedRandom m α]
(lo : α)
(hi : α)
(h : lo ≤ hi)
[RandomGen g]
:
Generate a random value of type α
between x
and y
inclusive.
Equations
- Random.randBound α lo hi h = BoundedRandom.randomR lo hi h
Instances For
instance
Random.instRandomULift
{m : Type u → Type u_1}
{m' : Type (max v u) → Type u_2}
{α : Type u}
[ULiftable m m']
[Random m α]
:
Random m' (ULift.{v, u} α)
Equations
- Random.instRandomULift = { random := fun {g : Type} [RandomGen g] => ULiftable.up Random.random }
instance
Random.instBoundedRandomNatToPreorderToPartialOrderStrictOrderedSemiring
{m : Type → Type u_1}
[Monad m]
:
Equations
- One or more equations did not get rendered due to their size.
instance
Random.instBoundedRandomIntToPreorderToPartialOrderToSemilatticeInfInstLatticeInt
{m : Type → Type u_1}
[Monad m]
:
Equations
- One or more equations did not get rendered due to their size.
instance
Random.instBoundedRandomFinToPreorderInstPartialOrderFin
{m : Type → Type u_1}
[Monad m]
{n : ℕ}
:
BoundedRandom m (Fin n)
Equations
- One or more equations did not get rendered due to their size.
instance
Random.instBoundedRandomULiftInstPreorderULift
{m : Type u → Type u_1}
{m' : Type (max v u) → Type u_2}
{α : Type u}
[Preorder α]
[ULiftable m m']
[BoundedRandom m α]
[Monad m']
:
BoundedRandom m' (ULift.{v, u} α)
Equations
- One or more equations did not get rendered due to their size.
def
IO.runRand
{m : Type u_1 → Type u_2}
{m₀ : Type → Type}
[Monad m]
[MonadLiftT (ST IO.RealWorld) m₀]
[ULiftable m₀ m]
{α : Type u_1}
(cmd : RandT m α)
:
m α
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.
Instances For
def
IO.runRandWith
{m : Type u_1 → Type u_2}
[Monad m]
{α : Type u_1}
(seed : ℕ)
(cmd : RandT m α)
:
m α
Equations
- IO.runRandWith seed cmd = do let __do_lift ← StateT.run cmd { down := mkStdGen seed } pure __do_lift.1