defining own arbitrary :: Gen Int

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

defining own arbitrary :: Gen Int

ahrens
Hello,

I would like to define my own arbitrary which should only give naturals,
i. e. which should behave like

instance Arbitrary Integer where
        arbitrary = elements ([0..]).

This definition gives a "multiple definition" error. Is there a way to
hide the definition of arbitrary::Gen Int of QuickCheck, such that I can
use my own definition ?

Thanks a lot in advance,
ben
Reply | Threaded
Open this post in threaded view
|

defining own arbitrary :: Gen Int

Thomas Davie

On 7 Mar 2009, at 13:54, ben wrote:

> Hello,
>
> I would like to define my own arbitrary which should only give  
> naturals,
> i. e. which should behave like
>
> instance Arbitrary Integer where
>        arbitrary = elements ([0..]).
>
> This definition gives a "multiple definition" error. Is there a way to
> hide the definition of arbitrary::Gen Int of QuickCheck, such that I  
> can
> use my own definition ?

The easiest way is to newtype it, and use fromIntegral to convert:

newtype Natural = N Integer

instance Arbitrary Integer where
   arbitrary = N <$> elements [0..]

instance Integral Natural where
   fromIntegral (N x) = fromIntegral x

Bob