MIT licensed by Li-yao Xia
Maintained by

Module documentation for

There are no documented modules for this package.

Generic random generators Hackage Build Status

Derive simple random generators for QuickCheck using generics.

Automating the Arbitrary boilerplate also ensures that if a type changes to have more constructors, then the generator fixes itself to generate that new case (with uniform distribution) or causes a compilation error (with an explicit distribution).

A simple (optional) strategy to ensure termination for recursive types: make Test.QuickCheck.Gen’s size parameter decrease at every recursive call; when it reaches zero, sample directly from a trivially terminating generator given explicitly (genericArbitraryRec and withBaseCase) or implicitly (genericArbitrary').


{-# LANGUAGE DeriveGeneric #-}

import GHC.Generics (Generic)
import Test.QuickCheck
import Generic.Random

data Tree a = Leaf | Node (Tree a) a (Tree a)
  deriving (Show, Generic)

instance Arbitrary a => Arbitrary (Tree a) where
  arbitrary = genericArbitraryRec uniform `withBaseCase` return Leaf

-- Equivalent to
-- > arbitrary =
-- >   sized $ \n ->
-- >     if n == 0 then
-- >       return Leaf
-- >     else
-- >       oneof
-- >         [ return Leaf
-- >         , resize (n `div` 3) $
-- >             Node <$> arbitrary <*> arbitrary <*> arbitrary
-- >         ]

main = sample (arbitrary :: Gen (Tree ()))


  • Fix a bug where generators did not decrease the size parameter with single-field constructors

  • The sized generators now use a custom generator for lists. Use genericArbitraryRecG () to disable that.

  • Lists of custom generators are now constructed using (:+) instead of GenList

  • Rename Field to FieldGen

  • Add Gen1, Gen1_ (custom generators for unary type constructors)

  • Add listOf', listOf1', vectorOf'

  • Remove deprecated module Generic.Random.Generic

  • Improved performance

  • Fix build for GHC<8

  • Add option to specify custom generators for certain fields, overriding Arbitrary instances
    • Add genericArbitraryG, genericArbitraryUG, genericArbitrarySingleG, genericArbitraryRecG
  • Add GArbitrary and GUniformWeight synonyms
  • Deprecate Generic.Random.Generic
  • Remove weights from the external API

  • Make the main module Generic.Random
  • Rework generic base case generation
    • You can explicitly provide a trivial generator (e.g., returning a nullary constructor) using withBaseCase
    • Generically derive BaseCaseSearch and let BaseCase find small values, no depth parameter must be specified anymore
  • Add genericArbitrarySingle, genericArbitraryRec, genericArbitraryU'
  • Deprecate weights
  • Fixed bug with genericArbitrary' not dividing the size parameter

  • Turn off dependency on boltzmann-samplers by default
  • Add genericArbitraryU, genericArbitraryU0 and genericArbitraryU1
  • Compatible with GHC 7.8.4 and GHC 7.10.3

  • Move Boltzmann sampler modules to another package: boltzmann-samplers

  • Check well-formedness of constructor distributions at compile time.
  • No longer support GHC 7.10.3 (the above feature relies on Generic information which does not exist before GHC 8)

  • Support GHC 7.10.3
  • Replace TypeApplications with ad-hoc data types in genericArbitraryFrequency'/genericArbitrary'
Depends on 2 packages:
comments powered byDisqus