Gen Monad #
This monad is used to formulate randomized computations with a parameter to specify the desired size of the result.
Main definitions #
Genmonad
References #
Error thrown on generation failure, e.g. because you've run out of resources.
- genError : String → GenError
Instances For
Equations
Instances For
Equations
- Plausible.instInhabitedGenError = { default := Plausible.instInhabitedGenError.default }
Equations
- Plausible.instReprGenError = { reprPrec := Plausible.instReprGenError.repr }
Equations
- One or more equations did not get rendered due to their size.
Instances For
Equations
- Plausible.instBEqGenError.beq (Plausible.GenError.genError a) (Plausible.GenError.genError b) = (a == b)
- Plausible.instBEqGenError.beq x✝¹ x✝ = false
Instances For
Equations
- Plausible.instBEqGenError = { beq := Plausible.instBEqGenError.beq }
Equations
- Plausible.Gen.genericFailure = Plausible.GenError.genError "Generation failure."
Instances For
Monad to generate random examples to test properties with.
It has a Nat parameter so that the caller can decide on the
size of the examples. It allows failure to generate via the Except monad
Equations
- Plausible.Gen α = Plausible.RandT (ReaderT (ULift Nat) (Except Plausible.GenError)) α
Instances For
Equations
- Plausible.instMonadLiftGen = { monadLift := fun {α : Type ?u.21} (m_1 : Plausible.RandGT StdGen m α) => liftM ∘ StateT.run m_1 }
Equations
- Plausible.instMonadErrorGen = inferInstance
Equations
- Plausible.Gen.genFailure (Plausible.GenError.genError a) = IO.userError (toString "generation failure: " ++ toString a)
Instances For
Equations
- One or more equations did not get rendered due to their size.
Instances For
Equations
- One or more equations did not get rendered due to their size.
Instances For
Lift Random.random to the Gen monad.
Equations
- Plausible.Gen.chooseAny α = liftM (Plausible.Random.rand α)
Instances For
Lift BoundedRandom.randomR to the Gen monad.
Equations
- Plausible.Gen.choose α lo hi h = liftM (Plausible.Random.randBound α lo hi h)
Instances For
Generate a Nat example between lo and hi (exclusively).
Equations
- Plausible.Gen.chooseNatLt lo hi h = do let __discr ← Plausible.Gen.choose Nat (lo + 1) hi ⋯ match __discr with | ⟨val, h⟩ => pure ⟨val - 1, ⋯⟩
Instances For
Get access to the size parameter of the Gen monad.
Equations
- Plausible.Gen.getSize = do let __do_lift ← read pure __do_lift.down
Instances For
Apply a function to the size parameter.
Equations
- Plausible.Gen.resize f x = withReader (ULift.up ∘ f ∘ ULift.down) x
Instances For
Choose a Nat between 0 and getSize.
Equations
- Plausible.Gen.chooseNat = do let __do_lift ← Plausible.Gen.getSize let a ← Plausible.Gen.choose Nat 0 __do_lift ⋯ pure a.val
Instances For
The following section defines various combinators for generators, which are used
in the body of derived generators (for derived Arbitrary instances).
The code for these combinators closely mirrors those used in Rocq/Coq QuickChick (see link in the References section below).
References #
Raised when a fueled generator fails due to insufficient fuel.
Equations
- Plausible.Gen.outOfFuel = Plausible.GenError.genError "out of fuel"
Instances For
Equations
- Plausible.Gen.inhabitedGen = { default := throw (Plausible.GenError.genError "inhabitedWitness") }
Tries all generators until one returns a Some value or all the generators failed once with None.
The generators are picked at random according to their weights (like frequency in Haskell QuickCheck),
and each generator is run at most once.
Equations
- Plausible.Gen.backtrack gs = Plausible.Gen.backtrackFuel✝ gs.length (Plausible.Gen.sumFst✝ gs) gs
Instances For
Picks one of the generators in gs at random, returning the default generator
if gs is empty.
(This is a more ergonomic version of Plausible's Gen.oneOf which doesn't
require the caller to supply a proof that the list index is in bounds.)
Equations
- default.oneOfWithDefault [] = default
- default.oneOfWithDefault gs = do let idx ← Plausible.Gen.choose Nat 0 (gs.length - 1) ⋯ gs.getD idx.val default
Instances For
frequency picks a generator from the list gs according to the weights in gs.
If gs is empty, the default generator is returned.
Equations
- default.frequency gs = do let n ← Plausible.Gen.choose Nat 0 ((List.map Prod.fst gs).sum - 1) ⋯ (default.pick gs n.val).snd
Instances For
sized f constructs a generator that depends on its size parameter
Equations
- Plausible.Gen.sized f = Plausible.Gen.getSize >>= f
Instances For
Create an Array of examples using x. The size is controlled
by the size parameter of Gen.
Equations
- One or more equations did not get rendered due to their size.
Instances For
Create a List of examples using x. The size is controlled
by the size parameter of Gen.
Instances For
Given a list of example generators, choose one to create an example.
Equations
- Plausible.Gen.oneOf xs pos = do let __discr ← (Plausible.Gen.chooseNatLt 0 xs.size pos).up match __discr with | { down := ⟨x, ⋯⟩ } => xs[x]
Instances For
Given a list of examples, choose one to create an example.
Equations
- Plausible.Gen.elements xs pos = do let __discr ← (Plausible.Gen.chooseNatLt 0 xs.length pos).up match __discr with | { down := ⟨x, ⋯⟩ } => pure xs[x]
Instances For
Generate a random permutation of a given list.
Equations
- One or more equations did not get rendered due to their size.
- Plausible.Gen.permutationOf [] = pure ⟨[], ⋯⟩
Instances For
Given two generators produces a tuple consisting out of the result of both
Equations
Instances For
Equations
- Plausible.instMonadLiftStateIOGen = { monadLift := fun {α : Type} (m : ReaderT (ULift Nat) (Except Plausible.GenError) α) => Plausible.instMonadLiftStateIOGen._private_1 m }
Execute a Gen inside the IO monad using size as the example size
Equations
- x.run size = Plausible.runRand (liftM x)
Instances For
Print (at most) 10 samples of a given type to stdout for debugging. Sadly specialized to Type 0
Equations
- One or more equations did not get rendered due to their size.
Instances For
Execute a Gen until it actually produces an output. May diverge for bad generators!
Equations
- Plausible.Gen.runUntil attempts x size = (Plausible.Gen.runUntil.repeatGen✝ attempts x).run size