QuickCheck is missing Arbitrary instances for fixed-size integral types

Nick Smallbone nick.smallbone at gmail.com
Mon Aug 30 09:23:00 EDT 2010


Hi John,

Sorry for the delay! I adopted your patch after some tweaking to
arbitrarySizedBoundedIntegral (it had a distribution that was really
only suited for Int32 before).

I've also packaged it up into QuickCheck 2.1.2, which is now on Hackage.

Nick

On Sat, Jun 19, 2010 at 11:27 PM, John Millikin <jmillikin at gmail.com> wrote:
> The 'Data.Int' and 'Data.Word' modules in 'base' define several
> fixed-size integral types (Int8-Int64, Word8-Word64). QuickCheck does
> not define Arbitrary instances for these. It'd be pretty easy to
> include them in QuickCheck, which would make client code less fragile
> (since it won't have to define orphan instances).
>
> There's a function already defined in 'Test.QuickCheck.Arbitrary'
> which can be used -- just redefine 'arbitrarySizedBoundedInt' as:
>
>        arbitrarySizedBoundedIntegral :: (Bounded a, Integral a) => Gen a
>        arbitrarySizedBoundedIntegral =
>            # .. function body remains the same ..
>
> and add the following instances:
>
>        import Data.Int
>        import Data.Word
>
>        instance Arbitrary Int8 where
>                arbitrary = arbitrarySizedBoundedIntegral
>                shrink    = shrinkIntegral
>
>        instance Arbitrary Int16 where
>                arbitrary = arbitrarySizedBoundedIntegral
>                shrink    = shrinkIntegral
>
>        instance Arbitrary Int32 where
>                arbitrary = arbitrarySizedBoundedIntegral
>                shrink    = shrinkIntegral
>
>        instance Arbitrary Int64 where
>                arbitrary = arbitrarySizedBoundedIntegral
>                shrink    = shrinkIntegral
>
>        instance Arbitrary Word where
>                arbitrary = arbitrarySizedBoundedIntegral
>                shrink    = shrinkIntegral
>
>        instance Arbitrary Word8 where
>                arbitrary = arbitrarySizedBoundedIntegral
>                shrink    = shrinkIntegral
>
>        instance Arbitrary Word16 where
>                arbitrary = arbitrarySizedBoundedIntegral
>                shrink    = shrinkIntegral
>
>        instance Arbitrary Word32 where
>                arbitrary = arbitrarySizedBoundedIntegral
>                shrink    = shrinkIntegral
>
>        instance Arbitrary Word64 where
>                arbitrary = arbitrarySizedBoundedIntegral
>                shrink    = shrinkIntegral
>
> Thanks!
>
> _______________________________________________
> QuickCheck mailing list
> QuickCheck at projects.haskell.org
> http://projects.haskell.org/cgi-bin/mailman/listinfo/quickcheck
>



More information about the QuickCheck mailing list