Behavior of fromInteger on out-of-range arguments

George Russell
Wed, 27 Feb 2002 18:16:20 +0100

Dean Herrington wrote
> It seems to me that the behavior for `(fromInteger x)::Int`, where x is
> an Integer outside Int range, should be the same (in terms of the
> Haskell report) as that for `y + y`, where y is an Int such that
> `toInteger y + toInteger y` is outside Int range.
I agree almost entirely.  Note that Haskell 98 does not define
what happens on overflow (section 6.4).  Personally my preference would
be for making overflowing operations on Ints cause an error, and on Words wrap,
as happens with SML.  I think GHC just wraps everything.

There is one exception though.  According to section 6.4.1 a numeric literal
should be interpreted as "the application of the function fromInteger to the        
appropriate value of type Integer".  In my opinion a numeric literal in an
exact type (such as Int or Ratio Int) which does not fit in the target representation
should ALWAYS raise an error, either at run-time or compile-time, since I can
conceive of no conceivable use for typing such a literal, and so it is almost
certainly a user error.  (The situation is different for inexact types such as
Float, since it is understood that say a literal like 0.1 may not
be exactly representable, and there is no other convenient way of typing it.)