1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
module R = Stdlib.Random
type 'a t = R.State.t -> 'a
let run (random: 'a t): 'a =
random (R.State.make_self_init ())
let constant (a: 'a): 'a t =
fun _ -> a
let (>>=) (m: 'a t) (f: 'a -> 'b t): 'b t =
fun state -> f (m state) state
let ( let* ) = (>>=)
let map (f: 'a -> 'b) (m: 'a t): 'b t =
let* a = m in
constant (f a)
let int (bound: int): int t =
fun state ->
assert (0 < bound);
R.State.int state bound
let float (bound: float): float t =
fun state ->
assert (0.0 <= bound);
R.State.float state bound
let bool: bool t =
R.State.bool
let choose (lst: 'a list): 'a t =
let arr = Array.of_list lst in
let len = Array.length arr in
let* i = int len in
assert (i < len);
constant arr.(i)