Rob Leslie | 24 Jan 07:33 2014

Arithmetic overflow

Greetings,

I have a question about arithmetic overflow in Haskell -- actually, probably specifically in GHC.

It’s my understanding from the Haskell 2010 Language Report that “The results of exceptional
conditions (such as overflow or underflow) on the fixed-precision numeric types [e.g. Int] are
undefined; an implementation may choose error (⊥, semantically), a truncated value, or a special
value such as infinity, indefinite, etc.”

In the documentation for Data.Int from the current release of the Haskell Platform, I read that “All
arithmetic is performed modulo 2^n, where n is the number of bits in the type.”

However, there seems to be at least one exception to this promise, namely:

  λ> (minBound :: Int) `quot` (-1)
  *** Exception: arithmetic overflow

Is this a bug?

More specifically, is it intended to be safe to rely on the modulo arithmetic behavior of GHC in spite of the
Language Report? And if so, should the above witnessed behavior still be expected?

Thanks,

--

-- 
Rob Leslie
rob <at> mars.org

_______________________________________________
Haskell-Cafe mailing list
(Continue reading)

Levent Erkok | 24 Jan 17:42 2014
Picon

Re: Arithmetic overflow

I think this is a bug. It also seems to impact explicitly sized types:

Prelude Data.Int> (minBound::Int8) `quot` (-1)
*** Exception: arithmetic overflow
Prelude Data.Int> (minBound::Int16) `quot` (-1)
*** Exception: arithmetic overflow

You should definitely report it. I think there's a pending release of GHC, so they might be able to fix it shortly.

-Levent.


On Thu, Jan 23, 2014 at 10:33 PM, Rob Leslie <rob <at> mars.org> wrote:
Greetings,

I have a question about arithmetic overflow in Haskell -- actually, probably specifically in GHC.

It’s my understanding from the Haskell 2010 Language Report that “The results of exceptional conditions (such as overflow or underflow) on the fixed-precision numeric types [e.g. Int] are undefined; an implementation may choose error (⊥, semantically), a truncated value, or a special value such as infinity, indefinite, etc.”

In the documentation for Data.Int from the current release of the Haskell Platform, I read that “All arithmetic is performed modulo 2^n, where n is the number of bits in the type.”

However, there seems to be at least one exception to this promise, namely:

  λ> (minBound :: Int) `quot` (-1)
  *** Exception: arithmetic overflow

Is this a bug?

More specifically, is it intended to be safe to rely on the modulo arithmetic behavior of GHC in spite of the Language Report? And if so, should the above witnessed behavior still be expected?

Thanks,

--
Rob Leslie
rob <at> mars.org


_______________________________________________
Haskell-Cafe mailing list
Haskell-Cafe <at> haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe

_______________________________________________
Haskell-Cafe mailing list
Haskell-Cafe <at> haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe
Rob Leslie | 24 Jan 23:55 2014

Re: Arithmetic overflow

On Jan 24, 2014, at 8:42 AM, Levent Erkok <erkokl <at> gmail.com> wrote:

> On Thu, Jan 23, 2014 at 10:33 PM, Rob Leslie <rob <at> mars.org> wrote:
> 
>>  λ> (minBound :: Int) `quot` (-1)
>>  *** Exception: arithmetic overflow
>> 
>> Is this a bug?
> 
> I think this is a bug. It also seems to impact explicitly sized types:
> 
> Prelude Data.Int> (minBound::Int8) `quot` (-1)
> *** Exception: arithmetic overflow
> Prelude Data.Int> (minBound::Int16) `quot` (-1)
> *** Exception: arithmetic overflow
> 
> You should definitely report it. I think there’s a pending release of GHC, so they might be able to fix it shortly.

Thanks for the feedback; I went ahead and created a ticket:

  https://ghc.haskell.org/trac/ghc/ticket/8695

Cheers,

--

-- 
Rob Leslie
rob <at> mars.org

_______________________________________________
Haskell-Cafe mailing list
Haskell-Cafe <at> haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe

Gmane