[Haskell-cafe] QuickCheck's co-arbitrary

Fawzi Mohamed fmohamed at mac.com
Wed Apr 4 10:22:10 EDT 2007


Let's see, I am quite new to it, so this is a check to see if I 
understood the things correctly ;)

Joel Reymont wrote:
> Folks,
>
> I understand that arbitrary defines the possible values.
>
> How do I generally come up with co-arbitrary, though?
you need to change a generator depending on the value of your type.

variant changes the seed of the generator adding an integer to it, so if 
you can map your object to mostly different integers (like the hash 
function) you can modify the generators using something like
    coarbitrary a = variant (hash a)

then
variant 1 . coarbitrary a
is simply modify a little (variant 1) the modification that coarbitrary 
a would do.

as to why you need this, it is to generate functions that have as 
starting domain your type.

Fawzi
> Would someone kindly explain the choice of co-arbitrary in the 
> following cases, specially the very last bit with "variant 1 . 
> coarbitrary a"?
>
> instance Arbitrary Char where
>     arbitrary     = elements ([' ', '\n', '\0'] ++ ['a'..'h'])
>     coarbitrary c = variant (fromEnum c `rem` 4)
>
> instance Arbitrary Ordering where
>     arbitrary      = elements [LT, EQ, GT]
>     coarbitrary LT = variant 0
>     coarbitrary EQ = variant 1
>     coarbitrary GT = variant 2
>
> instance Arbitrary a => Arbitrary (Maybe a) where
>     arbitrary            = frequency [ (1, return Nothing)
>                                      , (3, liftM Just arbitrary) ]
>     coarbitrary Nothing  = variant 0
>     coarbitrary (Just a) = variant 1 . coarbitrary a
>
>     Thanks, Joel
>
> -- 
> http://wagerlabs.com/
>
>
>
>
>
> _______________________________________________
> 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