Apply a shrinking function to a generator.
This will give the generator additional shrinking options, while keeping the existing shrinks intact.
:: a typeCtrl KGHC 9.10.3 · lts/ghc-9.10.x · c74966e · 2026-09-27
Modulehedgehog-1.7Haskell2010
Apply a shrinking function to a generator.
This will give the generator additional shrinking options, while keeping the existing shrinks intact.
Throw away a generator's shrink tree.
Make a generator smaller by scaling its size parameter.
Adjust the size parameter by transforming it with the given function.
Override the size parameter. Returns a generator which uses the given size instead of the runtime-size parameter.
Construct a generator that depends on the size parameter.
Generates a random integral number in the given [inclusive,inclusive] range.
When the generator tries to shrink, it will shrink towards the origin of the specified Range.
For example, the following generator will produce a number between 1970
and 2100, but will shrink towards 2000:
integral (Range.constantFrom 2000 1970 2100) :: Gen Int
Some sample outputs from this generator might look like:
=== Outcome ===
1973
=== Shrinks ===
2000
1987
1980
1976
1974=== Outcome ===
2061
=== Shrinks ===
2000
2031
2046
2054
2058
2060Generates a random integral number in the [inclusive,inclusive] range.
This generator does not shrink.
Generates a random machine integer in the given [inclusive,inclusive] range.
This is a specialization of integral, offered for convenience.
Generates a random 8-bit integer in the given [inclusive,inclusive] range.
This is a specialization of integral, offered for convenience.
Generates a random 16-bit integer in the given [inclusive,inclusive] range.
This is a specialization of integral, offered for convenience.
Generates a random 32-bit integer in the given [inclusive,inclusive] range.
This is a specialization of integral, offered for convenience.
Generates a random 64-bit integer in the given [inclusive,inclusive] range.
This is a specialization of integral, offered for convenience.
Generates a random machine word in the given [inclusive,inclusive] range.
This is a specialization of integral, offered for convenience.
Generates a random byte in the given [inclusive,inclusive] range.
This is a specialization of integral, offered for convenience.
Generates a random 16-bit word in the given [inclusive,inclusive] range.
This is a specialization of integral, offered for convenience.
Generates a random 32-bit word in the given [inclusive,inclusive] range.
This is a specialization of integral, offered for convenience.
Generates a random 64-bit word in the given [inclusive,inclusive] range.
This is a specialization of integral, offered for convenience.
Generates a random floating-point number in the [inclusive,exclusive) range.
This generator works the same as integral, but for floating point numbers.
Generates a random fractional number in the [inclusive,exclusive) range.
This generator does not shrink.
Generates a random floating-point number in the [inclusive,exclusive) range.
This is a specialization of realFloat, offered for convenience.
Generates a random floating-point number in the [inclusive,exclusive) range.
This is a specialization of realFloat, offered for convenience.
Generates a random boolean.
This generator shrinks to False.
This is a specialization of enumBounded, offered for convenience.
Generates a random boolean.
This generator does not shrink.
Generates an ASCII binit: '0..1'
Generates an ASCII octit: '0..7'
Generates an ASCII digit: '0..9'
Generates an ASCII hexit: '0..9', 'a'..'f', 'A'..'F'
Generates an ASCII lowercase letter: 'a'..'z'
Generates an ASCII uppercase letter: 'A'..'Z'
Generates an ASCII letter: 'a'..'z', 'A'..'Z'
Generates an ASCII letter or digit: 'a'..'z', 'A'..'Z', '0'..'9'
Generates an ASCII character: '0..127'
Generates a Latin-1 character: '0..255'
Generates a Unicode character, excluding noncharacters and invalid standalone surrogates:
'0..1114111' (excluding '55296..57343', '65534', '65535')
Generates a Unicode character, including noncharacters and invalid standalone surrogates:
'0..1114111'
Generates a string using Range to determine the length.
Generates a UTF-8 encoded string, using Range to determine the length.
Generates a random ByteString, using Range to determine the length.
Randomly selects one of the elements in the list.
This generator shrinks towards the first element in the list.
The input list must be non-empty.
Randomly selects one of the generators in the list.
This generator shrinks towards the first generator in the list.
The input list must be non-empty.
Uses a weighted distribution to randomly select one of the generators in the list.
This generator shrinks towards the first generator in the list.
The input list must be non-empty.
Modifies combinators which choose from a list of generators, like choice or frequency, so that they can be used in recursive scenarios.
This combinator modifies its target to select one of the generators in either the non-recursive or the recursive list. When a selection is made from the recursive list, the Size is halved. When the Size gets to one or less, selections are no longer made from the recursive list, this ensures termination.
A good example of where this might be useful is abstract syntax trees:
data Expr =
Var String
| Lam String Expr
| App Expr Expr
-- Assuming we have a name generator
genName :: MonadGen m => m String
-- We can write a generator for expressions
genExpr :: MonadGen m => m Expr
genExpr =
Gen.recursive Gen.choice [
-- non-recursive generators
Var <$> genName
] [
-- recursive generators
Gen.subtermM genExpr (x -> Lam <$> genName <*> pure x)
, Gen.subterm2 genExpr genExpr App
]
If we wrote the above example using only choice, it is likely that it
would fail to terminate. This is because for every call to genExpr,
there is a 2 in 3 chance that we will recurse again.
Discards the whole generator.
Generates a value that satisfies a predicate.
Shrinks of the generated value will also satisfy the predicate. From the original generator's shrink tree, any values that fail the predicate will be removed, but any subsequent shrinks that satisfy it will be retained. Compared to filter, shrinking may be slower but will be optimal.
It's possible that the predicate will never pass, or will only pass at a larger size than we're currently running at. To avoid looping forever, we limit the number of retries, and grow the size with each retry. If we retry too many times then the whole generator is discarded.
Generates a value that satisfies a predicate.
Shrinks of the generated value will also satisfy the predicate. From the original generator's shrink tree, any values that fail the predicate will be removed, along with their subsequent shrinks. Compared to filter, shrinking may be faster but may also be less optimal.
The type is also more general, because the shrink behavior from filter would force the entire shrink tree to be evaluated when applied to an impure tree.
This is essentially:
filterT p gen = mfilter p gen <|> filterT p gen
But that could loop forever, if the predicate will never pass or will only pass at a larger size than we're currently running at. We differ from the above in keeping some state to avoid that. We limit the number of retries, and grow the size with each retry. If we retry too many times then the whole generator is discarded.
Generates a value which is the result of the given function returning a Just.
The original generator's shrink tree will be retained, with values returning Nothing removed. Subsequent shrinks of those values will be retained. Compared to mapMaybeT, shrinking may be slower but will be optimal.
It's possible that the function will never return Just, or will only do so a larger size than we're currently running at. To avoid looping forever, we limit the number of retries, and grow the size with each retry. If we retry too many times then the whole generator is discarded.
Generates a value which is the result of the given function returning a Just.
The original generator's shrink tree will be retained, with values returning Nothing removed. Subsequent shrinks of those values will be retained. Compared to mapMaybeT, shrinking may be slower but will be optimal.
The type is also more general, because the shrink behavior from mapMaybe would force the entire shrink tree to be evaluated when applied to an impure tree.
It's possible that the function will never return Just, or will only do so a larger size than we're currently running at. To avoid looping forever, we limit the number of retries, and grow the size with each retry. If we retry too many times then the whole generator is discarded.
Generates a Nothing some of the time.
Generates either an a or a b.
As the size grows, this generator generates Rights more often than Lefts.
Generates either an a or a b, without bias.
This generator generates as many Rights as it does Lefts.
Generates a list using a Range to determine the length.
Generates a seq using a Range to determine the length.
Generates a non-empty list using a Range to determine the length.
Generates a set using a Range to determine the length.
This may fail to generate anything if the element generator cannot produce a large enough number of unique items to satify the required set size.
Generates a map using a Range to determine the length.
This may fail to generate anything if the keys produced by the generator do not account for a large enough number of unique items to satify the required map size.
Freeze the size and seed used by a generator, so we can inspect the value which it will produce.
This is used for implementing list and subtermMVec. It allows us to shrink the list itself before trying to shrink the values inside the list.
Constructs a generator from a sub-term generator.
Shrinks to the sub-term if possible.
Constructs a generator from a sub-term generator.
Shrinks to the sub-term if possible.
Constructs a generator from two sub-term generators.
Shrinks to one of the sub-terms if possible.
Constructs a generator from two sub-term generators.
Shrinks to one of the sub-terms if possible.
Constructs a generator from three sub-term generators.
Shrinks to one of the sub-terms if possible.
Constructs a generator from three sub-term generators.
Shrinks to one of the sub-terms if possible.
Generates a random subsequence of a list.
For example:
Gen.print (Gen.subsequence [1..5])
=== Outcome ===
[1,2,4]
=== Shrinks ===
[]
[2,4]
[1,4]
[1,2]Generates a random subset of a set.
This shrinks towards the empty set.
Generates a random permutation of a list.
This shrinks towards the order of the list being identical to the input list.
Generates a sequence of actions from an initial model state and set of commands.
Given the initial model state and set of commands, generates prefix actions to be run sequentially, followed by two branches to be run in parallel.
Generate a sample from a generator.
This function is useful for examining a Gen in GHCi or other contexts. It is not appropriate for use in a test suite directly. You will only get a single sample from this function, and it will not give you a property test. The seed is random, so the test is not deterministic.
If you only want a single test to run, then use :withTests 1
prop_OnlyRunOnce :: Property
prop_OnlyRunOnce =
withTests 1 $ property $ do
i <- Gen.int
i /== 0
Run a generator with a random seed and print the outcome, and the first level of shrinks.
Gen.print (Gen.enum 'a' 'f')
=== Outcome ===
'd'
=== Shrinks ===
'a'
'b'
'c'Run a generator with a random seed and print the resulting shrink tree.
Gen.printTree (Gen.enum 'a' 'f')
'd'
├╼'a'
├╼'b'
│ └╼'a'
└╼'c'
├╼'a'
└╼'b'
└╼'a'This may not terminate when the tree is very large.
Print the value produced by a generator, and the first level of shrinks, for the given size and seed.
Use print to generate a value from a random seed.
Print the shrink tree produced by a generator, for the given size and seed.
Use printTree to generate a value from a random seed.