[Haskell-cafe] How to generate dependend values with QuickCheck
John Millikin
jmillikin at gmail.com
Wed Aug 25 23:51:59 EDT 2010
You're generating two random values, where you probably want to just
generate one and then calculate the second from it. Try this:
instance Arbitrary QCExample where
arbitrary = do
i1 <- arbitrary
return (QCExample i1 (i1 * 2))
2010/8/25 Jürgen Nicklisch-Franken <jnf at arcor.de>:
> 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
> this:
>
> 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
> expected:
>
> 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.
>
> Jürgen
>
> _______________________________________________
> Haskell-Cafe mailing list
> Haskell-Cafe at haskell.org
> http://www.haskell.org/mailman/listinfo/haskell-cafe
>
>
More information about the Haskell-Cafe
mailing list