Hello haskell-cafe;<br><br>I&#39;m fiddling with <a href="http://cdsmith.wordpress.com/2009/07/20/calculating-multiplicative-inverses-in-modular-arithmetic/">this</a> blog post about inverting elements of Z/(p), trying to write the inversion function in pointfree style. This led me to try executing statements like <br>
<br><font face="courier new,monospace">   n `mod` 0<br><font face="arial,helvetica,sans-serif"><br>which in the ring theoretic sense should be n, at least for integers*. (<a href="http://mathworld.wolfram.com/Congruence.html">MathWorld agrees.</a>) But Hugs gives a division by zero error! I&#39;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 in</font></font> GHC.Real: (<a href="http://www.haskell.org/ghc/docs/latest/html/libraries/base/src/GHC-Real.html#mod">link</a>)<br>
<pre>&gt;  <span class="varid">a</span> <span class="varop">`mod`</span> <span class="varid">b</span><br><a name="(line253)"></a>&gt;   <span class="keyglyph">|</span> <span class="varid">b</span> <span class="varop">==</span> <span class="num">0</span>                     <span class="keyglyph">=</span> <span class="varid">divZeroError</span><br>
<a name="(line254)"></a>&gt;   <span class="keyglyph">|</span> <span class="varid">a</span> <span class="varop">==</span> <span class="varid">minBound</span> <span class="varop">&amp;&amp;</span> <span class="varid">b</span> <span class="varop">==</span> <span class="layout">(</span><span class="comment">-</span><span class="num">1</span><span class="layout">)</span> <span class="keyglyph">=</span> <span class="varid">overflowError</span><br>
<a name="(line255)"></a>&gt;   <span class="keyglyph">|</span> <span class="varid">otherwise</span>                  <span class="keyglyph">=</span>  <span class="varid">a</span> <span class="varop">`modInt`</span> <span class="varid">b</span><br>
<font face="arial,helvetica,sans-serif"><br><font size="2">Is there a reason why n `mod` 0 is undefined in Haskell? Maybe this has already been considered for Haskell&#39; and I&#39;m just unaware. <br>I did some digging in the archives and <a href="http://markmail.org/message/5dmehw4lhu56x4zw#query:haskell%20%22%60mod%60%200%22+page:1+mid:7alg3hdlndapyxg6+state:results">this discussion</a><a href="http://markmail.org/message/5dmehw4lhu56x4zw"></a> from 2002 is the most relevant one I could find; it is suggested there that n `mod` 0 should be an error.<br>
<br></font><font size="2">Thanks all-<br>Nathan Bloomfield<br></font><font size="2"><br>*- The mod function is defined in the Integral class, and I&#39;m not even sure how to interpret that. It looks kind of like a Euclidean domain.<br>
</font></font></pre>