gen
Monad
This monad is used to formulate randomized computations with a parameter to specify the desired size of the result.
This is a port of the Haskell QuickCheck library.
Main definitions
gen
monad
Local notation
i .. j
:Icc i j
, the set of values betweeni
andj
inclusively;
Tags
random testing
References
- https://hackage.haskell.org/package/QuickCheck
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.
Execute a gen
inside the io
monad using i
as the example
size and with a fresh random number generator.
Equations
- slim_check.io.run_gen x i = io.run_rand (x.run {down := i})
Lift random.random
to the gen
monad.
Equations
- slim_check.gen.choose_any α = ⟨λ (_x : ulift ℕ), rand.random α⟩
Lift random.random_r
to the gen
monad.
Equations
- slim_check.gen.choose x y p = ⟨λ (_x : ulift ℕ), rand.random_r x y p⟩
Generate a nat
example between x
and y
.
Equations
- slim_check.gen.choose_nat x y p = slim_check.gen.choose x y p
Equations
- slim_check.gen.has_orelse = {orelse := λ (α : Type u) (x y : slim_check.gen α), uliftable.up (slim_check.gen.choose_any bool) >>= λ (b : ulift bool), ite ↥(b.down) x y}
Get access to the size parameter of the gen
monad. For
reasons of universe polymorphism, it is specified in
continuation passing style.
Create n
examples using cmd
.
Equations
- slim_check.gen.vector_of n.succ cmd = vector.cons <$> cmd <*> slim_check.gen.vector_of n cmd
- slim_check.gen.vector_of 0 _x = return vector.nil
Create a list of examples using cmd
. The size is controlled
by the size parameter of gen
.
Equations
- cmd.list_of = slim_check.gen.sized (λ (sz : ℕ), uliftable.up (slim_check.gen.choose_nat 0 (sz + 1) _) >>= λ (_p : ulift ↥(set.Icc 0 (sz + 1))), slim_check.gen.list_of._match_1 cmd sz _p)
- slim_check.gen.list_of._match_1 cmd sz {down := n} = slim_check.gen.vector_of n.val cmd >>= λ (v : vector α n.val), return v.to_list
Given a list of example generators, choose one to create an example.
This definition is needed for to provide a local fact $ 0 < xs.length
instance to the type resolution system.
Equations
- slim_check.gen.one_of_aux xs pos = uliftable.up (slim_check.gen.choose_any (fin xs.length)) >>= λ (n : ulift (fin xs.length)), xs.nth_le n.down.val _
Given a list of example generators, choose one to create an example.
Equations
- slim_check.gen.one_of xs pos = slim_check.gen.one_of_aux xs pos
Generate a random permutation of a given list.
Equations
- slim_check.gen.permutation_of (x :: xs) = slim_check.gen.permutation_of xs >>= λ (_p : subtype xs.perm), slim_check.gen.permutation_of._match_2 x xs _p
- slim_check.gen.permutation_of list.nil = pure ⟨list.nil α, _⟩
- slim_check.gen.permutation_of._match_2 x xs ⟨xs', h⟩ = uliftable.up (slim_check.gen.choose_nat 0 xs'.length _) >>= λ (_p : ulift ↥(set.Icc 0 xs'.length)), slim_check.gen.permutation_of._match_1 x xs xs' h _p
- slim_check.gen.permutation_of._match_1 x xs xs' h {down := ⟨n, _⟩} = pure ⟨list.insert_nth n x xs', _⟩