On Tue, Mar 24, 2009 at 3:28 PM, Luke Palmer <span dir="ltr">&lt;<a href="mailto:lrpalmer@gmail.com">lrpalmer@gmail.com</a>&gt;</span> wrote:<br><div class="gmail_quote"><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
<div class="im">On Tue, Mar 24, 2009 at 3:14 PM, Xiao-Yong Jin <span dir="ltr">&lt;<a href="mailto:xj2106@columbia.edu" target="_blank">xj2106@columbia.edu</a>&gt;</span> wrote:<br></div><div class="gmail_quote"><div class="im">
<blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
Jake McArthur &lt;<a href="mailto:jake@pikewerks.com" target="_blank">jake@pikewerks.com</a>&gt; writes:<br>
<br>
&gt; Xiao-Yong Jin wrote:<br>
&gt; | The problem is that there will be many functions using such<br>
&gt; | a function to invert a matrix, making this inversion<br>
&gt; | function return Either/Maybe or packing it in a monad is<br>
&gt; | just a big headache.<br>
&gt;<br>
&gt; I disagree. If you try to take the inverse of a noninvertable matrix,<br>
&gt; this is an *error* in your code. Catching an error you created in pure<br>
&gt; code and patching it with chewing gum it is just a hack. A monadic<br>
&gt; approach (I&#39;m putting Either/Maybe under the same umbrella for brevity)<br>
&gt; is the only solution that makes any sense to me, and I don&#39;t think it&#39;s<br>
&gt; ugly as you are making it out to be.<br>
&gt;<br>
<br>
Then, why is &#39;div&#39; not of type &#39;a -&gt; a -&gt; ArithExceptionMonad a&#39; ?<br>
Why does it throws this /ugly/ /error/ when it is applied to<br>
0?  Why is it not using some beautiful<br>
&#39;ArithExceptinoMonad&#39;?  Is &#39;Control.Exception&#39; just pure<br>
/ugly/ and doesn&#39;t make any sense?</blockquote></div><div><br>It&#39;s a proof obligation, like using unsafePerformIO.  It is &quot;okay&quot; to use unsafePerformIO when it exhibits purely functional semantics, but it&#39;s possible to use it incorrectly, and there is no ImpureSemanticsException.  If you are being rigorous, you simply have to prove that the denominator will not be zero, rather than relying on it to be caught at runtime.  You can move the check to runtime easily:<br>

<br>safeDiv x 0 = Nothing<br>safeDiv x y = Just (x `div` y)<br><br>Going the other way, from a runtime check to an obligation, is impossible.</div></div></blockquote><div><br>(well, except for div x y = fromJust (safeDiv x y).. but the runtime check is still there in terms of operation) <br>
</div></div><br>