
On Thu, 2009-03-26 at 21:57 -0400, wren ng thornton wrote:
Jonathan Cast wrote:
Xiao-Yong Jin wrote:
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
safeDiv' :: (Exception e, Integral a) => a -> a -> Either e a safeDiv' _ 0 = Left e safeDiv' x y = Right $ div x y
[...] Other than that, I think the imprecise exceptions paper guarantees that these two functions are equivalent (albeit unwisely: see below).
I don't think so. The evaluation of x and y may throw errors before we get around to div.
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).
* safeDiv' will evaluate y (to pattern match against 0) and may return an error, e, whereas safeDiv will return Left e if div is strict in y.
* safeDiv' postpones evaluating x and so may return Right e, whereas safeDiv will return Left e if div is strict in x.
jcc