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

Claus Reinke claus.reinke at talk21.com
Thu Mar 26 15:08:49 EDT 2009


>>>> safeDiv :: (Exception e, Integral a) =>
>>>>            a -> a -> Either e a
>>>> safeDiv x y = unsafePerformIO . try . evaluate $ div x y
> 
> 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

No. Firstly, safeDiv' doesn't compile!-) Then, if you replace
'e' by 'DivideByZero' and adjust the types:

    *Main> safeDiv 1 (throw Overflow)
    Left arithmetic overflow
    *Main> safeDiv' 1 (throw Overflow)
    *** Exception: arithmetic overflow

Try ':info ArithException' for more in the same group. You
could use other functions in Control.Exceptions to get more
control about which exceptions you want to handle and how,
but so far, there is no indication that 'unsafePerformIO' is
the right hammer to use here..

Claus

-- unsagePerformIO: some things are just not wise to do



More information about the Haskell-Cafe mailing list