[Haskell-beginners] exception, not in IO
Ertugrul Söylemez
es at ertes.de
Mon Jul 15 13:47:18 CEST 2013
Lyndon Maydwell <maydwell at gmail.com> wrote:
> I agree that it seems uncomfortable for a seemingly pure function to
> require handling exceptions in the IO monad, but if the code in
> question really does have the capability of raising IO exceptions,
> then I think it is reasonable to consider it as impure and belonging
> in the IO monad and that it probably should not have been given a pure
> type signature to start with.
No, pure computations can throw Haskell exceptions just fine, and the
exceptions can be caught and handled without IO. Using Either
SomeException instead of IO is the recommended way to do it.
> I'd double check to see if the function in question has the capability
> of raising an exception in a more pure monad as per Kees's
> suggestion. The other option is to perform the actions I've suggested
> and then wrap the whole lot in unsafePerformIO to make it pure again
> but with a new signature of (a -> Either YourException b), however
> that just makes me feel even more queazy...
That's about the worst solution you could consider. It's not a valid
use case for unsafePerformIO. You can just use Either right away. I
don't understand why you insist on IO and use hazardously unsafe
constructs to satisfy the insistence.
There is a natural transformation from Either SomeException to IO:
liftEither :: (Exception e) => Either e a -> IO a
liftEither = either throwIO return
Greets,
Ertugrul
--
Not to be or to be and (not to be or to be and (not to be or to be and
(not to be or to be and ... that is the list monad.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 836 bytes
Desc: not available
URL: <http://www.haskell.org/pipermail/beginners/attachments/20130715/23e65944/attachment.pgp>
More information about the Beginners
mailing list