How to generate dependend values with QuickCheck

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

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
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@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe

Excerpts from Jürgen Nicklisch-Franken's message of Wed Aug 25 23:32:51 -0400 2010:
instance Arbitrary QCExample where arbitrary = let i1 = arbitrary i2 = fmap (* 2) i1 in liftM2 QCExample i1 i2
What's happening here is that you are "evaluating" the random value generator too late in the game: when you set i1 = arbitrary, no random value has been generated yet, and i2 is thus another generator which has no relation to what i1 might produce. You could instead do this: arbitrary = do i1 <- arbitrary let i2 = i1 * 2 return (QCExample i1 i2) Cheers, Edward

Thank you all, (you are responding so quick that I consider to stop thinking myself in the future and just ask). Jürgen Am Donnerstag, den 26.08.2010, 00:02 -0400 schrieb Edward Z. Yang:
Excerpts from Jürgen Nicklisch-Franken's message of Wed Aug 25 23:32:51 -0400 2010:
instance Arbitrary QCExample where arbitrary = let i1 = arbitrary i2 = fmap (* 2) i1 in liftM2 QCExample i1 i2
What's happening here is that you are "evaluating" the random value generator too late in the game: when you set i1 = arbitrary, no random value has been generated yet, and i2 is thus another generator which has no relation to what i1 might produce. You could instead do this:
arbitrary = do i1 <- arbitrary let i2 = i1 * 2 return (QCExample i1 i2)
Cheers, Edward

2010/8/26 Jürgen Nicklisch-Franken
Thank you all, (you are responding so quick that I consider to stop thinking myself in the future and just ask).
No, no, don't stop thinking: if you stop thinking, how are you going to be able to instantly answer someone else's question? (You are aware that a condition of asking anything on this mailing list requires you to answer at least 3 questions from other people in return, aren't you? :p) -- Ivan Lazar Miljenovic Ivan.Miljenovic@gmail.com IvanMiljenovic.wordpress.com

2010/8/26 Jürgen Nicklisch-Franken
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
You're saying that i1 is equal to the arbitrary action, not the result of such an action. As such, i2 is based upon a different arbitrary action. Consider this: instance Arbitrary QCExample where arbitrary = do i1 <- arbitrary let i2 = 2*i1 return $ QCExample i1 i2 -- Ivan Lazar Miljenovic Ivan.Miljenovic@gmail.com IvanMiljenovic.wordpress.com
participants (4)
-
Edward Z. Yang
-
Ivan Lazar Miljenovic
-
John Millikin
-
Jürgen Nicklisch-Franken