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

Edward Z. Yang ezyang at MIT.EDU
Thu Aug 26 00:02:12 EDT 2010


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


More information about the Haskell-Cafe mailing list