Library
Module
Module type
Parameter
Class
Class type
type 'a t = Random.State.t -> 'a
A generator of arbitrary values of type 'a
val return : 'a -> 'a t
Return always the same value (e.g. 4
)
val int : int -> int t
Any integer between 0 (inclusive) and the given higher bound (exclusive)
val int_range : start:int -> stop:int -> int t
val small_int : int t
Ints lower than 100
split_int gen
generates a number n
from gen
, and returns i, j
where i + j = n
val bool : bool t
Arbitrary boolean
val char : char t
A (printable) char
val alpha : char t
Alphabetic char
val float : float -> float t
Random float
val string : string t
Random strings of small length
List of arbitrary length. Default len
is between 0 and 10.
val among : 'a list -> 'a t
Choose an element among those of the list
val among_array : 'a array -> 'a t
Choose in the array
val shuffle : 'a array -> unit t
Shuffle the array in place
Recursive arbitrary values. The optional value max
defines the maximal depth, if needed (default 15). base
is the base case.
Recursive values of at most given random depth
Function used to indicate that, in fix_fuel
, a sub-case is impossible to fulfill.
type 'a recursive_case = [
| `Base of 'a t
base case, no fuel
*)| `Base_fuel of int -> 'a t
base case, using fuel for its own purpose
*)| `Rec of (int -> 'a list t) -> 'a t
recursive case. must call the function exactly once
*)| `Rec_fuel of (int -> 'a list t) -> int -> 'a t
the function is given self
and max
and shall call self i
exactly once for some i <= max
.
| `Rec1 of 'a t -> 'a t
recursive case with exactly one subcase
*)| `Rec2 of 'a t -> 'a t -> 'a t
recursive case with exactly two subcases
*) ]
What is a recursive case for a fueled fixpoint?
val fix_fuel : 'a recursive_case list -> int -> 'a option t
fix_fuel l fuel
consumes fuel
in recursive subcases. The idea is that l
contains one or more recursive builders, such that every f
in l
is given a function f' : int -> 'a list t
, to call with an integer n
so as to obtain n
recursive subcases. The function f'
MUST be called exactly once per case f
in l
..
Example:
type tree = Node of tree * tree | Leaf of int;;
let leaf_ x = Leaf x;;
let node_ x y = Node (x,y);;
let rand_tree =
fix_fuel [
`Base (small_int >|= leaf_);
`Rec (fun self ->
self 2 >>= function [x;y] ->
return (node_ x y))
];;
generate (rand_tree 20);; (* generate trees with 20 nodes *)
type tree' = Node2 of tree' * tree' | Node1 of tree' | Leaf' of int;;
let leaf' x = Leaf' x ;;
let node2 x y = Node2(x,y) ;;
let node1 x = Node1 x;;
(* alternative with [`Rec2] *)
let rand_tree' =
fix_fuel [
`Base (small_int >|= leaf');
`Rec1 (fun self -> self >|= node1);
`Rec2 (fun self1 self2 -> pure node2 <*> self1 <*> self2)
];;
generate ~n:1 (rand_tree' 20);;
type ('a, 'state) general_recursive_case = [
| `Base of 'state -> 'a t
| `Base_fuel of int -> 'state -> 'a t
| `Rec of (int -> ('state -> 'a) list t) -> 'state -> 'a t
| `Rec_fuel of (int -> ('state -> 'a) list t) -> int -> 'state -> 'a t
| `Rec1 of ('state -> 'a t) -> 'state -> 'a t
| `Rec2 of ('state -> 'a t) -> ('state -> 'a t) -> 'state -> 'a t
]
What is a recursive case for a general fueled fixpoint?
val fix_fuel_gen :
('a, 'state) general_recursive_case list ->
int ->
'state ->
'a option t
fix_fuel_gen l state fuel
consumes fuel
in recursive subcases, similar to what fix_fuel l fuel
would do, but more general because a "state" is passed bottom-up. In recursive subcases, is that l
contains one or more recursive builders, such that every f
in l
is given a function f' : int -> ('state -> 'a) list t
, to call with an integer n
so as to obtain n
recursive subcases. The function f'
MUST be called exactly once per case f
in l
..
val pure : 'a -> 'a t
val generate : ?n:int -> ?rand:Random.State.t -> 'a t -> 'a list
Generate n
random values of the given type