[Haskell-cafe] Use unsafePerformIO to catch Exception?

Duncan Coutts duncan.coutts at worc.ox.ac.uk
Tue Mar 24 22:52:00 EDT 2009

On Mon, 2009-03-23 at 08:11 -0400, Xiao-Yong Jin wrote:
> Hi,
> I just feel it is not comfortable to deal with exceptions
> only within IO monad, so I defined
> > tryArith :: a -> Either ArithException a
> > tryArith = unsafePerformIO . try . evaluate

You must not do this. It breaks the semantics of the language.

Other people have given practical reasons why you should not but a
theoretical reason is that you've defined a non-continuous function.
That is impossible in the normal semantics of pure functional languages.
So you're breaking a promise which we rely on.

It is not "safe". It's almost as bad as a function isBottom, which is
the canonical non-continuous function. It's defined by:

isBottom _|_ = True
isBottom _   = False

Of course your tryArith only tests for certain kinds of _|_ value, but
in principle the problem is the same.

It is not safe because it distinguishes values that are not supposed to
be distinguishable. This invalidates many properties and


More information about the Haskell-Cafe mailing list