[Haskell-cafe] Simple quirk in behavior of `mod`

Nathan Bloomfield nbloomf at gmail.com
Tue Jul 21 22:52:08 EDT 2009

Hello haskell-cafe;

I'm fiddling with
post about inverting elements of Z/(p), trying to write the inversion
function in pointfree style. This led me to try executing statements like

   n `mod` 0

which in the ring theoretic sense should be n, at least for integers*.
agrees. <http://mathworld.wolfram.com/Congruence.html>) But Hugs gives a
division by zero error! I'm more of a recreational haskell user and not too
familiar with how the Prelude works. But I dug around a bit and saw
this inGHC.Real: (

>  a `mod` b
>   | b == 0                     = divZeroError
>   | a == minBound && b == (-1) = overflowError
>   | otherwise                  =  a `modInt` b

Is there a reason why n `mod` 0 is undefined in Haskell? Maybe this
has already been considered for Haskell' and I'm just unaware.
I did some digging in the archives and this discussion
<http://markmail.org/message/5dmehw4lhu56x4zw> from 2002 is the most
relevant one I could find; it is suggested there that n `mod` 0 should
be an error.

Thanks all-
Nathan Bloomfield

*- The mod function is defined in the Integral class, and I'm not even
sure how to interpret that. It looks kind of like a Euclidean domain.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.haskell.org/pipermail/haskell-cafe/attachments/20090721/31b031e9/attachment.html

More information about the Haskell-Cafe mailing list