HORIZON HASKELLDocslts/ghc-9.10.xc74966e2026-09-27Search names, modules, packages, or :: a typeCtrl K

GHC 9.10.3 · lts/ghc-9.10.x · c74966e · 2026-09-27

ModuleQuickCheck-2.15.0.1Haskell2010

Test.QuickCheck.Arbitrary

Type classes for random generation of values.

Note: the contents of this module are re-exported by Test.QuickCheck. You do not need to import it directly.

  • 6 classes
  • 37 values

Arbitrary and CoArbitrary classes

2 declarations
classclass Arbitrary a where
#

Random generation and shrinking of values.

QuickCheck provides Arbitrary instances for most types in base, except those which incur extra dependencies. For a wider range of Arbitrary instances see the quickcheck-instances package.

Methods

  • arbitrary :: Gen a

    A generator for values of the given type.

    It is worth spending time thinking about what sort of test data you want - good generators are often the difference between finding bugs and not finding them. You can use sample, label and classify to check the quality of your test data.

    There is no generic arbitrary implementation included because we don't know how to make a high-quality one. If you want one, consider using the testing-feat or generic-random packages.

    The QuickCheck manual goes into detail on how to write good generators. Make sure to look at it, especially if your type is recursive!

  • shrink :: a -> [a]

    Produces a (possibly) empty list of all the possible immediate shrinks of the given value.

    The default implementation returns the empty list, so will not try to shrink the value. If your data type has no special invariants, you can enable shrinking by defining shrink = genericShrink, but by customising the behaviour of shrink you can often get simpler counterexamples.

    Most implementations of shrink should try at least three things:

    1. Shrink a term to any of its immediate subterms. You can use subterms to do this.

    2. Recursively apply shrink to all immediate subterms. You can use recursivelyShrink to do this.

    3. Type-specific shrinkings such as replacing a constructor by a simpler constructor.

    For example, suppose we have the following implementation of binary trees:

    data Tree a = Nil | Branch a (Tree a) (Tree a)

    We can then define shrink as follows:

    shrink Nil = []
    shrink (Branch x l r) =
      -- shrink Branch to Nil
      [Nil] ++
      -- shrink to subterms
      [l, r] ++
      -- recursively shrink subterms
      [Branch x' l' r' | (x', l', r') <- shrink (x, l, r)]

    There are a couple of subtleties here:

    • QuickCheck tries the shrinking candidates in the order they appear in the list, so we put more aggressive shrinking steps (such as replacing the whole tree by Nil) before smaller ones (such as recursively shrinking the subtrees).

    • It is tempting to write the last line as [Branch x' l' r' | x' <- shrink x, l' <- shrink l, r' <- shrink r] but this is the wrong thing! It will force QuickCheck to shrink x, l and r in tandem, and shrinking will stop once one of the three is fully shrunk.

    There is a fair bit of boilerplate in the code above. We can avoid it with the help of some generic functions. The function genericShrink tries shrinking a term to all of its subterms and, failing that, recursively shrinks the subterms. Using it, we can define shrink as:

    shrink x = shrinkToNil x ++ genericShrink x
      where
        shrinkToNil Nil = []
        shrinkToNil (Branch _ l r) = [Nil]

    genericShrink is a combination of subterms, which shrinks a term to any of its subterms, and recursivelyShrink, which shrinks all subterms of a term. These may be useful if you need a bit more control over shrinking than genericShrink gives you.

    A final gotcha: we cannot define shrink as simply shrink x = Nil:genericShrink x as this shrinks Nil to Nil, and shrinking will go into an infinite loop.

    If all this leaves you bewildered, you might try shrink = genericShrink to begin with, after deriving Generic for your type. However, if your data type has any special invariants, you will need to check that genericShrink can't break those invariants.

Instances114Arbitrary, …
classclass CoArbitrary a where
#

Used for random generation of functions. You should consider using Test.QuickCheck.Fun instead, which can show the generated functions as strings.

If you are using a recent GHC, there is a default definition of coarbitrary using genericCoarbitrary, so if your type has a Generic instance it's enough to say

instance CoArbitrary MyType

You should only use genericCoarbitrary for data types where equality is structural, i.e. if you can't have two different representations of the same value. An example where it's not safe is sets implemented using binary search trees: the same set can be represented as several different trees. Here you would have to explicitly define coarbitrary s = coarbitrary (toList s).

Methods

  • coarbitrary :: a -> Gen b -> Gen b

    Used to generate a function of type a -> b. The first argument is a value, the second a generator. You should use variant to perturb the random generator; the goal is that different values for the first argument will lead to different calls to variant. An example will help:

    instance CoArbitrary a => CoArbitrary [a] where
      coarbitrary []     = variant 0
      coarbitrary (x:xs) = variant 1 . coarbitrary (x,xs)
    
Instances56CoArbitrary, …

Unary and Binary classes

classclass Arbitrary1 (f :: Type -> Type) where
#

Lifting of the Arbitrary class to unary type constructors.

Methods

Instances15Arbitrary1, …
classclass Arbitrary2 (f :: Type -> Type -> Type) where
#

Lifting of the Arbitrary class to binary type constructors.

Methods

Instances4Arbitrary2

Helper functions for implementing arbitrary

Generates an integral number. The number can be positive or negative and its maximum absolute value depends on the size parameter.

Generates an integral number from a bounded domain. The number is chosen from the entire range of the type, but small numbers are generated more often than big numbers. Inspired by demands from Phil Wadler.

Uniformly generates a fractional number. The number can be positive or negative and its maximum absolute value depends on the size parameter.

Generators for various kinds of character

Helper functions for implementing shrink

classclass RecursivelyShrink (f :: k -> Type) where
#
Instances6RecursivelyShrink
classclass GSubterms (f :: Type -> Type) a where
#
Instances6GSubterms
  • GSubterms U1 aDefined in QuickCheck-2.15.0.1 · Test.QuickCheck.Arbitrary
  • GSubterms V1 aDefined in QuickCheck-2.15.0.1 · Test.QuickCheck.Arbitrary
  • GSubterms (K1 i a) bDefined in QuickCheck-2.15.0.1 · Test.QuickCheck.Arbitrary
  • (GSubtermsIncl f a, GSubtermsIncl g a) => GSubterms (f :*: g) aDefined in QuickCheck-2.15.0.1 · Test.QuickCheck.Arbitrary
  • (GSubtermsIncl f a, GSubtermsIncl g a) => GSubterms (f :+: g) aDefined in QuickCheck-2.15.0.1 · Test.QuickCheck.Arbitrary
  • GSubterms f a => GSubterms (M1 i c f) aDefined in QuickCheck-2.15.0.1 · Test.QuickCheck.Arbitrary
valueshrinkNothing :: a -> [a]
#

Returns no shrinking alternatives.

valueshrinkList :: (a -> [a]) -> [a] -> [[a]]
#

Shrink a list of values given a shrinking function for individual values.

valueshrinkMap :: Arbitrary a => (a -> b) -> (b -> a) -> b -> [b]
#

Map a shrink function to another domain. This is handy if your data type has special invariants, but is almost isomorphic to some other type.

shrinkOrderedList :: (Ord a, Arbitrary a) => [a] -> [[a]]
shrinkOrderedList = shrinkMap sort id

shrinkSet :: (Ord a, Arbitrary a) => Set a -> [Set a]
shrinkSet = shrinkMap fromList toList
valueshrinkMapBy :: (a -> b) -> (b -> a) -> (a -> [a]) -> b -> [b]
#

Non-overloaded version of shrinkMap.

valueshrinkBoundedEnum :: (Bounded a, Enum a, Eq a) => a -> [a]
#

Shrink an element of a bounded enumeration.

Example
data MyEnum = E0 | E1 | E2 | E3 | E4 | E5 | E6 | E7 | E8 | E9
   deriving (Bounded, Enum, Eq, Ord, Show)
Example1 expression
shrinkBoundedEnum E9[E0,E5,E7,E8]
Example1 expression
shrinkBoundedEnum E5[E0,E3,E4]
Example1 expression
shrinkBoundedEnum E0[]

Helper functions for implementing coarbitrary

value(><) :: (Gen a -> Gen a) -> (Gen a -> Gen a) -> Gen a -> Gen a
#

Deprecated. Use ordinary function composition instead

Combine two generator perturbing functions, for example the results of calls to variant or coarbitrary.

Generators which use arbitrary