HORIZON HASKELLDocslts/ghc-10.0.x2b276322026-10-31Search packagesCtrl K
Theme: |

GHC 10.0.0.20260917 · lts/ghc-10.0.x · 2b27632 · 2026-10-31

ModuleQuickCheck-2.18.0.0Haskell2010

Test.QuickCheck.Gen

Test case generation.

Note: the contents of this module (except for the definition of Gen) are re-exported by Test.QuickCheck. You probably do not need to import it directly.

Contents
  1. Generator type
  2. Primitive generator combinators
  3. Floating point
  4. Common generator combinators
Synopsis

Generator type

newtypenewtype Gen a

A generator for values of type a.

The third-party packages QuickCheck-GenT and quickcheck-transformer provide monad transformer versions of Gen.

Constructors

  • MkGen
    • unGen :: QCGen -> Int -> a

      Run the generator on a particular seed and size. If you just want to get a random value out, consider using generate.

Instances5Monad, Functor, MonadFix, Applicative, Testable
  • Monad GenDefined in QuickCheck-2.18.0.0 · Test.QuickCheck.Gen
  • Functor GenDefined in QuickCheck-2.18.0.0 · Test.QuickCheck.Gen
  • MonadFix GenDefined in QuickCheck-2.18.0.0 · Test.QuickCheck.Gen
  • Applicative GenDefined in QuickCheck-2.18.0.0 · Test.QuickCheck.Gen
  • Testable prop => Testable (Gen prop)Defined in QuickCheck-2.18.0.0 · Test.QuickCheck.Property

Primitive generator combinators

valuesized :: (Int -> Gen a) -> Gen a

Used to construct generators that depend on the size parameter.

For example, listOf, which uses the size parameter as an upper bound on length of lists it generates, can be defined like this:

listOf :: Gen a -> Gen [a]
listOf gen = sized $ \n ->
  do k <- choose (0,n)
     vectorOf k gen

You can also do this using getSize.

valuegetSize :: Gen Int

Returns the size parameter. Used to construct generators that depend on the size parameter.

For example, listOf, which uses the size parameter as an upper bound on length of lists it generates, can be defined like this:

listOf :: Gen a -> Gen [a]
listOf gen = do
  n <- getSize
  k <- choose (0,n)
  vectorOf k gen

You can also do this using sized.

valueresize :: HasCallStack => Int -> Gen a -> Gen a

Overrides the size parameter. Returns a generator which uses the given size instead of the runtime-size parameter.

valuescale :: (Int -> Int) -> Gen a -> Gen a

Adjust the size parameter, by transforming it with the given function.

valuechoose :: Random a => (a, a) -> Gen a

Generates a random element in the given inclusive range. For integral and enumerated types, the specialised variants of choose below run much quicker.

valuegenerate :: Gen a -> IO a

Run a generator. The size passed to the generator is always 30; if you want another size then you should explicitly use resize.

valuesample :: Show a => Gen a -> IO ()

Generates some example values and prints them to System.IO.stdout.

Floating point

Common generator combinators

valuesuchThatMaybe :: Gen a -> (a -> Bool) -> Gen (Maybe a)

Tries to generate a value that satisfies a predicate. If it fails to do so after enough attempts, returns Nothing.

valueshuffle :: [a] -> Gen [a]

Generates a random permutation of the given list.

valuegrowingElements :: HasCallStack => [a] -> Gen a

Takes a list of elements of increasing size, and chooses among an initial segment of the list. The size of this initial segment increases with the size parameter. The input list must be non-empty.

valuelistOf :: Gen a -> Gen [a]

Generates a list of random length. The maximum length depends on the size parameter.

valuelistOf1 :: Gen a -> Gen [a]

Generates a non-empty list of random length. The maximum length depends on the size parameter.