package windtrap

  1. Overview
  2. Docs

Module Windtrap_prop.GenSource

Random value generators with integrated shrinking.

Random value generators with integrated shrinking.

Generators produce shrink trees, coupling generated values with their shrink candidates. This ensures shrinks automatically respect generator invariants (e.g., int_range 10 100 shrinks stay within bounds).

Sourcetype 'a t = Random.State.t -> 'a Windtrap_prop__.Tree.t

A generator takes random state and produces a shrink tree.

Combinators

Sourceval pure : 'a -> 'a t

pure x always generates x with no shrinks.

Sourceval map : ('a -> 'b) -> 'a t -> 'b t

map f gen applies f to generated values.

Sourceval bind : 'a t -> ('a -> 'b t) -> 'b t

bind gen f generates with gen, then uses result to select next generator.

Sourceval ap : ('a -> 'b) t -> 'a t -> 'b t

ap fgen xgen applies generated function to generated value. Shrinks are interleaved from both generators.

Primitives

Sourceval make_primitive : gen:(Random.State.t -> 'a) -> shrink:('a -> 'a Seq.t) -> 'a t

make_primitive ~gen ~shrink builds a generator from a raw generation function and a shrink function. This is the escape hatch for building custom generators when the standard combinators are not sufficient.

Sourceval unit : unit t

Always generates ().

Sourceval bool : bool t

Generates true or false. Shrinks toward false.

Sourceval int : int t

Generates integers in full range. Shrinks toward 0.

Sourceval int_range : ?origin:int -> int -> int -> int t

int_range low high generates integers in [low, high]. Shrinks toward origin (default: closest to 0 within range).

Sourceval nat : int t

Generates non-negative integers in [0, 10_000], biased toward small values. Shrinks toward 0.

Sourceval small_int : int t

Generates integers in [-10_000, 10_000], biased toward small absolute values. Shrinks toward 0.

Sourceval int32 : int32 t

Generates 32-bit integers using full range. Shrinks toward 0l.

Sourceval int32_range : ?origin:int32 -> int32 -> int32 -> int32 t

int32_range low high generates 32-bit integers in [low, high]. Shrinks toward origin (default: closest to 0l within range).

Sourceval int64 : int64 t

Generates 64-bit integers using full range. Shrinks toward 0L.

Sourceval int64_range : ?origin:int64 -> int64 -> int64 -> int64 t

int64_range low high generates 64-bit integers in [low, high]. Shrinks toward origin (default: closest to 0L within range).

Sourceval nativeint : nativeint t

Generates native integers using full platform range. Shrinks toward 0n.

Sourceval float : float t

Generates floats using bit manipulation over the full IEEE 754 range, including NaN, infinity, and subnormal values. Shrinks toward 0.0.

Sourceval float_range : ?origin:float -> float -> float -> float t

float_range low high generates floats between low (inclusive) and high (exclusive). Shrinks toward origin (default: closest to 0.0 within range).

  • raises Invalid_argument

    if high < low, high -. low > max_float, origin < low, or origin > high.

Sourceval char : char t

Generates characters in the 0..255 byte range. Shrinks toward 'a'.

Sourceval char_range : ?origin:char -> char -> char -> char t

char_range low high generates characters in [low, high]. Shrinks toward origin (default: low).

Sourceval string : string t

Generates strings of bytes. Length biased toward small values (see nat).

Sourceval string_of : char t -> string t

string_of char_gen generates strings using char_gen for characters. Length biased toward small values (see nat).

Sourceval string_size : int t -> char t -> string t

string_size size_gen char_gen generates strings with length from size_gen and characters from char_gen.

Sourceval bytes : bytes t

Generates bytes. Length biased toward small values.

Containers

Sourceval option : ?ratio:float -> 'a t -> 'a option t

option gen generates None or Some x.

  • parameter ratio

    Probability of Some (default: 0.85). Shrinks toward None.

Sourceval result : ?ratio:float -> 'a t -> 'e t -> ('a, 'e) result t

result ok_gen err_gen generates Ok or Error.

  • parameter ratio

    Probability of Ok (default: 0.75).

Sourceval either : ?ratio:float -> 'a t -> 'b t -> ('a, 'b) Either.t t

either left_gen right_gen generates Left or Right.

  • parameter ratio

    Probability of Left (default: 0.5).

Sourceval list : 'a t -> 'a list t

list gen generates lists. Length biased toward small. Uses efficient bisection shrinking for large lists.

Sourceval list_size : int t -> 'a t -> 'a list t

list_size size_gen gen generates lists with size from size_gen.

Sourceval array : 'a t -> 'a array t

array gen generates arrays. Like list but returns array.

Sourceval pair : 'a t -> 'b t -> ('a * 'b) t

pair a b generates pairs.

Sourceval triple : 'a t -> 'b t -> 'c t -> ('a * 'b * 'c) t

triple a b c generates triples.

Sourceval quad : 'a t -> 'b t -> 'c t -> 'd t -> ('a * 'b * 'c * 'd) t

quad a b c d generates 4-tuples.

Choice

Sourceval oneof : 'a t list -> 'a t

oneof gens picks one generator uniformly at random.

Sourceval oneofl : 'a list -> 'a t

oneofl xs picks one element uniformly at random. Shrinks toward earlier elements in the list.

Sourceval frequency : (int * 'a t) list -> 'a t

frequency weighted_gens picks a generator with probability proportional to its weight.

  • raises Invalid_argument

    if weighted_gens is empty or total weight is less than 1.

Size Control

Sourceval sized : (int -> 'a t) -> 'a t

sized f generates a size in [0, 1000] biased toward small values, then calls f with that size.

Shrink Control

Sourceval no_shrink : 'a t -> 'a t

no_shrink gen removes all shrinks from generated trees.

Sourceval add_shrink_invariant : ('a -> bool) -> 'a t -> 'a t

add_shrink_invariant p gen filters shrinks to only those satisfying p.

Recursive Generators

Sourceval delay : (unit -> 'a t) -> 'a t

delay f delays generator construction. Useful for recursive generators.

Sourceval fix : ('a t -> 'a t) -> 'a t

fix f creates a recursive generator. f receives the generator being defined as its argument.

  type tree = Leaf | Node of tree * tree

  let tree_gen =
    Gen.fix (fun self ->
        Gen.frequency
          [
            (3, Gen.pure Leaf);
            ( 1,
              let+ l, r = Gen.pair self self in
              Node (l, r) );
          ])
Sourceval find : ?count:int -> f:('a -> bool) -> 'a t -> Random.State.t -> 'a option

find ~f gen state attempts to find a generated value satisfying f. Tries up to count generations (default: 100). Returns None if no match is found. Does not shrink; returns the first match.

Let Syntax

Sourceval (let+) : 'a t -> ('a -> 'b) -> 'b t

let+ x = gen in e is map (fun x -> e) gen.

Sourceval (and+) : 'a t -> 'b t -> ('a * 'b) t

gen1 and+ gen2 is pair gen1 gen2.

Sourceval (let*) : 'a t -> ('a -> 'b t) -> 'b t

let* x = gen in e is bind gen (fun x -> e).

Operators

Sourceval (>|=) : 'a t -> ('a -> 'b) -> 'b t

gen >|= f is map f gen.

Sourceval (>>=) : 'a t -> ('a -> 'b t) -> 'b t

gen >>= f is bind gen f.