Generic random generators
Go to file
2017-07-27 23:49:32 +02:00
examples Remove Boltzmann stuff 2017-03-05 15:26:15 -05:00
src/Generic/Random Fine grained imports to avoid warning 2017-07-27 23:49:32 +02:00
test Remove Boltzmann stuff 2017-03-05 15:26:15 -05:00
.travis.yml Test GHC 7.8 and 7.10 on Travis 2017-04-10 11:32:42 -04:00
CHANGELOG.md Compatibility with GHC 7.8 and 7.10 2017-04-10 11:24:26 -04:00
generic-random.cabal Lift upper bound on base 2017-07-27 23:31:26 +02:00
LICENSE Initial commit 2016-04-06 23:34:02 +02:00
README.md Remove Boltzmann stuff 2017-03-05 15:26:15 -05:00
Setup.hs Initial commit 2016-04-06 23:34:02 +02:00

Generic random generators Hackage Build Status

Say goodbye to Constructor <$> arbitrary <*> arbitrary <*> arbitrary-boilerplate.

    {-# LANGUAGE DeriveGeneric #-}

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

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

    instance Arbitrary a => Arbitrary (Tree a) where
      arbitrary = genericArbitrary' Z uniform

    -- Equivalent to
    -- > arbitrary =
    -- >   sized $ \n ->
    -- >     if n == 0 then
    -- >       return Leaf
    -- >     else
    -- >       oneof
    -- >         [ return Leaf
    -- >         , Node <$> arbitrary <*> arbitrary <*> arbitrary
    -- >         ]

    main = sample (arbitrary :: Gen (Tree ()))
  • User-specified distribution of constructors, with a compile-time check that weights have been specified for all constructors.
  • A simple (optional) strategy to ensure termination: Test.QuickCheck.Gen's size parameter decreases at every recursive genericArbitrary' call; when it reaches zero, sample directly from a finite set of finite values.