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

Xiao-Yong Jin xj2106 at columbia.edu
Thu Mar 26 14:23:09 EDT 2009

Henning Thielemann <lemming at henning-thielemann.de> writes:

> On Thu, 26 Mar 2009, Xiao-Yong Jin wrote:
>> So I have another question.  Is the following function safe
>> and legitimate?
>>> safeDiv :: (Exception e, Integral a) =>
>>>            a -> a -> Either e a
>>> safeDiv x y = unsafePerformIO . try . evaluate $ div x y
>> I believe it should be okay to use this 'safeDiv'.  What do
> I think that question is wrong way around. The real question is, why
> do you want to solve your problem using unsafePerformIO?

I just want to know, from a theoretical point of view,
whether this 'safeDiv' in above definition is the same as

> safeDiv' :: (Exception e, Integral a) =>
>             a -> a -> Either e a
> safeDiv' _ 0 = Left e
> safeDiv' x y = Right $ div x y

For the question why do I want to do that, I am not sure.  I
guess if the function which has an error call inside is
provided by other library package, and I don't have a clear
and easy way to tell whether the function will make the
error call or not, it would be easy just to make a wrapper
like that.  It's also a possible situation that I don't know
how to test the input to a foreign function call.
    c/*    __o/*
    <\     * (__
    */\      <

More information about the Haskell-Cafe mailing list