A monad is an abstraction of the concept of sequencing of computations. A value of type 'a monad represents a computation that returns a value of type 'a.
return v
returns the (trivial) computation that returns v.
Produce any of the given values, weighted equally.
of_list [ v1 ; ... ; vN ] = union [ singleton v1 ; ... ; singleton vN ]
Generator for the values from a potentially infinite sequence. Chooses each value
with probability p
, or continues with probability 1-p
. Defaults to p=0.25
.
size
produces a geometric distribution (think "radioactive decay") starting at 0
and increasing with probability 0.75. It produces natural numbers and is weighted
toward low values, making it a good default for, e.g., data structure sizes.
Generators for functions; take observers for inputs and a generator for outputs. The observer splits the space of possible inputs into a number of "buckets"; each randomly generated function applies the observer to an input, and produces a different output value for each bucket.
The ?branching_factor
argument determines how many branches the function's
decision tree has, and thus how many categories of inputs it can distinguish. If
absent, branching_factor
defaults to a geometric distribution similar to size
but capped by the maximum branching factor of the domain observers.
Generators for functions, annotated with sexps that describe the functions.
Intended for debugging purposes. These generators each produce a fn_with_sexp
,
whose description can be extracted using fn_sexp
.
filter_map t ~f
produces y
for every x
in t
such that f x = Some y
.
filter t ~f
produces every x
in t
such that f x = true
.
Caveat: Use filter
and filter_map
sparingly. Every time f
rejects a value, it
counts as a failed attempt to produce a value. Too many failures can cause
Quickcheck to take a long time to generate values, or fail a test if it fails more
times than the maximum configured number of attempts.
'a Choice.t
represents the choice of a value from a generator.
bind_choice t f
is like bind t f
, except f
is passed an 'a Choice.t
and can
thus use subsets of t
by using Choice.gen
with the ~keep
option.
bind t f
is equal to bind_choice t (fun c -> f (Choice.value c))
, although
bind
is cheaper than bind_choice
.
Empty generator that is guaranteed to fail to produce a value.
of_fun f
produces a generator that lazily applies f
.
f
*MUST* be deterministic or else random value generation will fail. However, it
is recommended that f
not be memoized. Instead, spread out the work of generating
a whole distribution over many of_fun
calls combined with weighted_union
. This
allows lazily generated generators to be garbage collected after each test and the
relevant portions cheaply recomputed in subsequent tests, rather than accumulating
without bound over time.
choose t ~random_float_between_zero_and_one
makes a choice in t
at random, using
random_float_between_zero_and_one
to produce random floats between 0. (inclusive)
and 1. (exclusive) for each weighted choice. If t
has been fully explored, it
produces `No_choices_remain
. Otherwise it produces `Choice c
for some choice
c
in t
.