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

Ivan Lazar Miljenovic ivan.miljenovic at gmail.com
Thu Aug 26 00:03:01 EDT 2010


2010/8/26 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

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 at gmail.com
IvanMiljenovic.wordpress.com


More information about the Haskell-Cafe mailing list