Module Random_generator
Random_generator is a combinator library to generate random values.
type random_state
= Stdlib.Random.State.t
Random.State.t is the type of the state of the standard library Random module.
Use
Random.get_state ()
to get the current (global) state of the Random module, orRandom.State.make : int array -> Random.State.tt
orRandom.State.make_self_init () : unit -> Random.State.t
to generate fresh, independent state.
type 'a gen
= random_state -> 'a
val run : 'a gen -> random_state -> 'a
Value generators and combinators
val make_char : char -> int -> char gen
val lowercase : char gen
val uppercase : char gen
val digit : char gen
val unit : unit gen
base combinators names are adapted from Kaputt.Generator
val prod : 'a gen -> 'b gen -> ('a * 'b) gen
val make_int : int -> int -> int gen
val make_nativeint : nativeint -> nativeint -> nativeint gen
val make_int32 : int32 -> int32 -> int32 gen
val make_int64 : int64 -> int64 -> int64 gen
val make_float : float -> float -> float gen
val string : int gen -> char gen -> string gen
val bool : bool gen
val bits : int gen
val split_int : int -> (int * int) gen
split_int n
returns two integers(i,k)
each in the interval[0;n]
such thati + k = n
type 'a nonempty_list
= 'a list
list combinators being of the form "pick an element such that", and random generators being total (they cannot fail), we will often require input lists to be non-empty. For convenience reasons, we won't statically enforce this, only use
nonempty_list
as a reminder of the implicit invariant.
val select : 'a nonempty_list -> 'a gen
val choose : 'a gen nonempty_list -> 'a gen
val shuffle : 'a list -> 'a list gen
returns a (uniform) permutation of the list
'a gen
is a functor
'a gen
is applicative
'a gen
is a monad
val return : 'a -> 'a gen
synonym of
pure
parametrized fixpoint
backtracking generator
type 'a backtrack_gen
= 'a option gen
Represents generator that may fail.
val succeed : 'a gen -> 'a backtrack_gen
val guard : ('a -> bool) -> 'a backtrack_gen -> 'a backtrack_gen
val cond : bool -> 'a backtrack_gen -> 'a backtrack_gen
cond b gen
should be preferred toguard (fun _ -> b) gen
for performance reasons, as the generator is not requested any output in thefalse
case.
val backtrack : 'a backtrack_gen -> 'a gen
backtrack gen
will loop until the generator returns a successful valueSome v
. This may not terminate, and more generally can be a performance concern. You should test validity as locally as possible.
fueled generators
type 'a fueled
= (int -> 'a option) gen
module Fuel : sig ... end
The
Fuel
submodule provides the basic operations on fueled generators.
convenience functions for fueled generators
val nullary : 'a -> 'a fueled
zero