[Haskell-cafe] How to generate dependend values with QuickCheck

Jürgen Nicklisch-Franken jnf at arcor.de
Wed Aug 25 23:32:51 EDT 2010

I want to generate values, so that i have some arbitrary object, which has a
certain set of signals, and each set of signals has a certain set of sensor
value pairs, were the values are arbitrary.
However to demonstrate my problem I have an easy example, were I want to
generate an instance of

data QCExample =  QCExample Int Int
    deriving Show

and the second value should always be the double of the first. So I tried

instance Arbitrary QCExample where
    arbitrary =
        let i1 = arbitrary
            i2 = fmap (* 2) i1
        in liftM2 QCExample i1 i2

but showing some of the generated test cases in ghci does not give me what I

let gen :: Gen (QCExample) =  arbitrary
Test.QuickCheck.Gen.sample gen

> QCExample (-2) 0
> QCExample (-4) (-6)
> QCExample 3 30
> ...
I know that I can filter, but this would be to inefficient.

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.haskell.org/pipermail/haskell-cafe/attachments/20100825/0bc2d83f/attachment.html

More information about the Haskell-Cafe mailing list