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

Chris Kuklewicz haskell at list.mightyreason.com
Fri Mar 27 08:24:22 EDT 2009

Jonathan Cast wrote:
> Sure.  Which also points out that the original safeDiv wasn't actually
> safe, since there's no guarantee of what evaluate will do with x and y.
> (Actually, there's not much guarantee of what evaluate does anyway ---
> just that any errors in e's definition get turned into exceptions by the
> time evaluate e finishes running, or don't turn into exceptions at all).

That is not true if you mean "any errors" as "any and all errors".

The 'evaluate' operation only forces the argument into weak head normal form 
(WHNF) not normal form (NF).  Thus 'evaluate' may succeed and then its return 
value could be examined further and only then trigger an exception.

I could easily define a new "Integral" type where WHNF is not NF and demonstrate 
the problem.

The solution is to use 'evaluate' only on known primitive types like Int, or on 
polymorphic data constrained to be NFDATA and use the 'rnf' strategy within the 
call to 'evaluate'.


More information about the Haskell-Cafe mailing list