[Haskell-cafe] Quickcheck examples and Data.Word32

Joel Reymont joelr1 at gmail.com
Thu Oct 27 12:47:32 EDT 2005


On Oct 27, 2005, at 5:34 PM, Sebastian Sylvan wrote:

> import Data.Word
> import Test.QuickCheck
>
> instance Arbitrary Word32 where
>   arbitrary = do let mx,mn :: Integer
>              mx = fromIntegral (maxBound :: Word32)
>                  mn = fromIntegral (minBound :: Word32)
>          c <- choose (mx, mn)
>          return (fromIntegral c)

Awesome! This actually works!


> instance Arbitrary Word32 where
>   arbitrary = do c <- arbitrary :: Gen Integer
>               return (fromIntegral c)
>
> Though I'm not sure of the range and distribution of the generated
> Word32's (since it would depend on how fromIntegral behaves
> transforming an Integer to a Word32 when the Integer is larger than
> maxBound::Word32).

It wraps around, apparently

*Foo> maxBound :: Word32
4294967295
*Foo> let x :: Integer = 4294967295 + 1
*Foo> x
4294967296
*Foo> fromIntegral x :: Word32
0
*Foo> let x :: Integer = 4294967295 + 10
*Foo> x
4294967305
*Foo> fromIntegral x :: Word32
9
*Foo> let x :: Integer = 4294967295 * 2
*Foo> x
8589934590
*Foo> fromIntegral x :: Word32
4294967294

     Thanks, Joel

--
http://wagerlabs.com/







More information about the Haskell-Cafe mailing list