[Haskell-cafe] Re: Exception handling in numeric computations

Henning Thielemann lemming at henning-thielemann.de
Tue Mar 24 17:35:30 EDT 2009

On Tue, 24 Mar 2009, Xiao-Yong Jin wrote:

> Jake McArthur <jake at pikewerks.com> writes:
>> Xiao-Yong Jin wrote:
>> | The problem is that there will be many functions using such
>> | a function to invert a matrix, making this inversion
>> | function return Either/Maybe or packing it in a monad is
>> | just a big headache.
>> I disagree. If you try to take the inverse of a noninvertable matrix,
>> this is an *error* in your code. Catching an error you created in pure
>> code and patching it with chewing gum it is just a hack. A monadic
>> approach (I'm putting Either/Maybe under the same umbrella for brevity)
>> is the only solution that makes any sense to me, and I don't think it's
>> ugly as you are making it out to be.
> Then, why is 'div' not of type 'a -> a -> ArithExceptionMonad a' ?
> Why does it throws this /ugly/ /error/ when it is applied to
> 0?

I think "throw" should be reserved to exceptions (although it is still 
strange English). Actually 'div x 0' is 'undefined', just like in 
mathematics. This is justified by the fact, that you can easily check 
whether the denominator is zero or not and it is expected that either you 
check the denominator before calling 'div' or that you proof that in your 
application the denominator is non-zero anyway and thus save repeated 
checks for zero at run-time. The deficiency is not in 'div' but in the 
type system, which does not allow to declare an Int to be non-zero. In 
contrast to that, it is not easily checked, whether a matrix is regular. 
Thus you may prefer a Maybe result.

More information about the Haskell-Cafe mailing list