Enum class

Ketil Malde ketil@ii.uib.no
24 Oct 2001 08:31:53 +0200


Pixel <pixel@mandrakesoft.com> writes:

> Quite a few pbs with overflow on Int too:

> i :: Int
> i = 0x7fffffff

> i_plus_1 = i+1
>   -- ghc : -2147483648
>   -- hugs: -2147483648

Given that Int represents modulo arithmetic, this is all right, but

> i_succ = succ i
>   -- ghc : *** Exception: Prelude.Enum.succ{Int}: tried to take `succ' of maxBound
>   -- hugs: -2147483648

GHC's behavior here is inconsequent, I think.  I suppose it is trying
to maintain the invariant

        succ i > i

> j :: Int
> j = 0x80000000
>   -- ghc : -2147483648
>   -- hugs: Program error: {primIntegerToInt 2147483648}

I'm not sure which behavior I prefer.  You're specifying a constant
out of range for the datatype.

> k :: Int
> k = 0x100000000
>   -- ghc : 0
>   -- hugs: Program error: {primIntegerToInt 4294967296}

Same as above.

> i':: Integer
> i'= 0x7fffffff
> i_plus_1' = i+1
>   -- ghc : 2147483648
>   -- hugs: 2147483648

> i_succ' = succ i'
>   -- ghc : 2147483648
>   -- hugs: -2147483648

I think Hugs is wrong.  Integer shouldn't wrap.

The greatest problem with Integer seem to be different
implementations, IMHO.

-kzm
-- 
If I haven't seen further, it is by standing in the footprints of giants