[Haskell-cafe] Co-arbitrary

Neil Mitchell ndmitchell at gmail.com
Thu May 10 09:46:01 EDT 2007


Hi

> > Would someone kindly explain why we need co-arbitrary in QuickCheck
> > and how to define it?
>

> To write your own 'coarbitrary', you are aiming to convert values of
> your datatype into a disjoint partition of the set of all Ints.  In
> the end it is rather mechanical: e.g.
>
>     data Foo a = Foo String a
>                | Bar Bool
>
>     instance Arbitrary (Foo a) where
>         coarbitrary (Foo s a) = variant 0 . coarbitrary s . coarbitrary a
>         coarbitrary (Bar b)   = variant 1 . coarbitrary b

The Derive tool now implements coarbitrary for the instances it
generates - following the scheme outlined in this email.

http://www-users.cs.york.ac.uk/~ndm/derive/

Thanks

Neil


More information about the Haskell-Cafe mailing list