Generic random generators
Go to file
lyxia c0e6915484 Update README
[ci skip]
2017-08-23 10:35:09 +02:00
examples Increase context-stack to compile example with GHC 7.8 2017-08-22 23:03:15 +02:00
src/Generic Update haddocks 2017-08-22 22:56:37 +02:00
test Spare user from boilerplate instances 2017-08-22 18:46:41 +02:00
.travis.yml Update travis 2017-08-21 22:43:46 +02:00
CHANGELOG.md Update CHANGELOG 2017-08-22 23:14:43 +02:00
generic-random.cabal Update tested-with list 2017-08-23 09:28:43 +02:00
LICENSE Initial commit 2016-04-06 23:34:02 +02:00
README.md Update README 2017-08-23 10:35:09 +02:00
Setup.hs Initial commit 2016-04-06 23:34:02 +02:00

Generic random generators Hackage Build Status

Derive simple random generators for QuickCheck using generics.

Example

    {-# 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 ()))

Features

  • User-specified distribution of constructors.
  • A simple (optional) strategy to ensure termination for recursive types: using genericArbitrary', Test.QuickCheck.Gen's size parameter decreases at every recursive call; when it reaches zero, sample directly from a trivially terminating generator.