Gen
Module Hedgehog.Gen
Generators for random values with integrated shrinking.
A generator takes a size parameter and a seed, and produces a shrink tree where the root is the generated value and children are shrunk alternatives.
The option in the return type supports discarding: None means the generated value was discarded (e.g. by filter).
type 'a t = int -> Seed.t -> 'a Tree.t optionMonad
val return : 'a -> 'a tval bind : 'a t -> ('a -> 'b t) -> 'b tval map : ('a -> 'b) -> 'a t -> 'b tval apply : ('a -> 'b) t -> 'a t -> 'b tval (let*) : 'a t -> ('a -> 'b t) -> 'b tval (let+) : 'a t -> ('a -> 'b) -> 'b tval (and+) : 'a t -> 'b t -> ('a * 'b) tNumeric generators
val integral : int Range.t -> int tGenerate a random integer in the given range, with shrinking towards the range’s origin via binary search.
val int : int Range.t -> int tAlias for integral.
val float : float Range.t -> float tGenerate a random float in the given range, shrinking towards the range’s origin.
val bool : bool tGenerate a random boolean, shrinking towards false.
Characters & strings
val char : int Range.t -> char tGenerate a character from a range of character codes.
val digit : char tval lower : char tval upper : char tval alpha : char tval alpha_num : char tval ascii : char tval string : int Range.t -> char t -> string tGenerate a string using a range for the length and a char generator.
Choice combinators
val element : 'a list -> 'a tRandomly select an element from a list. Shrinks towards the first element.
val choice : 'a t list -> 'a tRandomly select a generator from a list. Shrinks towards the first generator.
val frequency : (int * 'a t) list -> 'a tUse weighted distribution to select a generator. Shrinks towards generators with smaller indices.
val recursive : ('a t list -> 'a t) -> 'a t list -> 'a t list -> 'a trecursive f nonrec rec_ selects from nonrec and rec_ generators. When size <= 1, only nonrec generators are used. Recursive generators have their size halved.
Collections
val list : int Range.t -> 'a t -> 'a list tGenerate a list using a range for the length. Uses Tree.interleave for optimal shrinking.
val non_empty : int Range.t -> 'a t -> 'a list tGenerate a non-empty list. The first element is always present; the rest uses list with the given range. Shrinks will never produce [].
val shuffle : 'a list -> 'a list tGenerate a random permutation of a list. Shrinks towards the original order.
val subsequence : 'a list -> 'a list tGenerate a random subsequence of a list, preserving order. Shrinks towards [].
val option : 'a t -> 'a option tGenerates None some of the time.
val either : 'a t -> 'b t -> ('a, 'b) Stdlib.Either.t tGenerate either a Left or Right value with 50/50 probability.
val unique : ('a -> 'a -> int) -> int Range.t -> 'a t -> 'a list tGenerate a list of unique elements (no duplicates according to the given comparison function). Shrinks via the underlying list shrinking.
val pair : 'a t -> 'b t -> ('a * 'b) tGenerate a pair with parallel shrinking.
Freeze
val freeze : 'a t -> ('a * 'a t) tfreeze gen captures the generator’s output. Returns a pair of the generated value and a frozen generator that always produces that value.
Integer width variants
val int32 : int32 Range.t -> int32 tGenerate a random int32 in the given range, with shrinking towards the origin. Converts through int internally (safe on 64-bit OCaml).
val int64 : int64 Range.t -> int64 tGenerate a random int64 in the given range, with shrinking towards the origin. Uses native int64 arithmetic.
Subterm combinators
Generate values that shrink to structural subterms. Useful for recursive data types (ASTs, trees, JSON, etc.): if Neg(e) fails, the shrinker tries e directly before trying Neg(e') for smaller e'.
val subterm : 'a t -> ('a -> 'a) -> 'a tsubterm gen f generates a value by applying f to a value from gen. Shrinks include the raw subterm at every level.
val subterm2 : 'a t -> 'a t -> ('a -> 'a -> 'a) -> 'a tsubterm2 g1 g2 f generates f x y from g1 and g2. Shrinks include both raw subterms.
val subterm3 : 'a t -> 'a t -> 'a t -> ('a -> 'a -> 'a -> 'a) -> 'a tsubterm3 g1 g2 g3 f generates f x y z. Shrinks include all three raw subterms.
val subterm_m : 'a t -> ('a -> 'a t) -> 'a tLike subterm but f returns a generator.
val subterm_m2 : 'a t -> 'a t -> ('a -> 'a -> 'a t) -> 'a tLike subterm2 but f returns a generator.
val subterm_m3 : 'a t -> 'a t -> 'a t -> ('a -> 'a -> 'a -> 'a t) -> 'a tLike subterm3 but f returns a generator.
Conditional
val filter : ('a -> bool) -> 'a t -> 'a tGenerate values satisfying a predicate. Retries with growing size. After too many retries, discards.
val discard : 'a tAlways discards.
val ensure : ('a -> bool) -> 'a t -> 'a tDiscard if the generated value doesn’t satisfy the predicate.
Size control
val sized : (int -> 'a t) -> 'a tConstruct a generator that depends on the size parameter.
val resize : int -> 'a t -> 'a tOverride the size parameter.
val scale : (int -> int) -> 'a t -> 'a tAdjust the size parameter with a function.
val small : 'a t -> 'a tMake a generator smaller by scaling with the golden ratio.
Shrinking control
val shrink : ('a -> 'a list) -> 'a t -> 'a tAdd extra shrinks to a generator.
val prune : 'a t -> 'a tRemove all shrinks from a generator.
val no_shrink : 'a t -> 'a tAlias for prune.
Sampling / debugging
val sample : ?size:int -> ?seed:Seed.t -> 'a t -> 'aGenerate a single sample. Raises if the generator always discards.
val print_tree : ?size:int -> ?seed:Seed.t -> ('a -> string) -> 'a t -> unitPrint the shrink tree for debugging.
val generate : (int -> Seed.t -> 'a) -> 'a tCreate a generator with no shrinks from a size and seed function.