Exception handling in numeric computations (was Re: [Haskell-cafe] Use unsafePerformIO to catch Exception?)

Jake McArthur jake at pikewerks.com
Tue Mar 24 12:57:03 EDT 2009

Hash: SHA1

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.

| It is impractical to use method (a),
| because not every function that uses 'invMat' knows how to
| deal with 'invMat' not giving an answer.  So we need to use
| method (b), to use monad to parse our matrix around.
|> > invMat :: Matrix -> NumericCancerMonad Matrix
| It hides the exceptional nature of numerical computations
| very well, but it is cancer in the code.  Whenever any
| function wants to use invMat, it is mutated.  This is just
| madness.  You don't want to make all the code to be monadic
| just because of singularities in numeric calculation.

For functions that don't know or don't care about failure, just use fmap
or one of its synonyms.

~    scalarMult 2 <$> invMat x

See? The scalarMult function is pure, as it should be. There is no
madness here.

- - Jake
Version: GnuPG v1.4.9 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org


More information about the Haskell-Cafe mailing list