12

I'm using the EitherT monad transformer. Combining it with the IO monad, I'm afraid I would get an exception and it would not be caught.

Indeed the exception just passes through:

import Control.Monad.Trans
import Control.Error
import System.Directory

main = runEitherT testEx >>= print

testEx :: EitherT String IO ()
testEx = lift $ removeFile "non existing filename"

But the EitherT otherwise fits the bill perfectly to convey to callers the error. So I want to use that, not throw exceptions...

I looked at try from Control.Exception:

try :: Exception e => IO a -> IO (Either e a) 

It looks to be exactly what I want, it would fit in my EitherT IO stack... (probably with an added hoistEither and maybe fmapL and it starts looking verbose though) But a naive lift $ try doesn't typecheck.

I'm sure this problem has been solved thousands of times, but I can't find any good link describing this exact issue. How is this supposed to be solved?

EDIT By "how is this supposed to be solved", I was interested in the idiomatic solution, what would be the standard way to handle that in haskell. From the answers so far, it seems the idiomatic way is to let the exceptions be thown and handle them higher-up. Seems like a bit counter-intuitive to have two flows of control and return paths, but it is apparently the way it's meant to be done.

2
  • The lifted-base seems to be what you're looking for, which I found thanks to this post.
    – bheklilr
    Sep 9, 2014 at 20:40
  • Thank you, that's very interesting. Too bad he doesn't give the link to the discussion he triggered on haskell-cafe. I hope there's more that that, though, a "better" solution, otherwise it seems it would be idiomatic to ignore the problem, if the answer is not immediate and used by almost everybody... Sep 9, 2014 at 20:59

3 Answers 3

18

I actually think EitherT is not the right thing to do here. What you're trying to say is "IO is for side-effects, and EitherT is for exceptions." But that's not true: IO always has the potential to result in an exception, so all you're doing is adding a false sense of security to your API, and introducing two ways that exceptions can be thrown instead of one. In addition, instead of using the well structured SomeException favored by IO, you're reducing down to String, which throws away information.

Anyway, if you're convinced that this is what you want to do, it's not too difficult. It looks something like:

eres <- liftIO $ try x
case eres of
    Left e -> throwError $ show (e :: SomeException)
    Right x -> return x

Note, however, that this will also swallow up async exceptions, which is usually not what you want to do. I think a better approach for that is enclosed-exceptions.

5
  • well I need the Either for some other aspects of the function anyway. If I didn't need IO code, that would be a Either function. I started with a pure Either function let's say, added the IO part, now I have a EitherT IO and thinking, it's a shame to have the exceptions while I can take advantage of the EitherT. On the other hand, it's true that if I start wrapping all my IO functions with a try, I'm not using the language as it was intended. So it seems the answer is, the idiomatic way is to let it go and have a try higher-up. I didn't read your links yet though, will do ASAP. Sep 9, 2014 at 21:09
  • 1
    so in the end, I guess I'm a bit confused by the double message exceptions vs sum types in haskell. when which one, and why. Sep 9, 2014 at 21:12
  • 2
    I'd also recommend having a look at the exceptions package. Instead of having your pure code live directly in Either, have it live in any instance of MonadThrow. Sep 9, 2014 at 21:13
  • 1
    Looks like I have lots of researching to do. What is wrong with pure code living in Either? But I guess I can answer myself by googling and reading... Sep 9, 2014 at 21:19
  • 2
    The only problem is that it won't unify with code in IO. By programming against a typeclass, your code will automatically work for both Either and IO without any conversion. Sep 10, 2014 at 4:24
5

You don't want to lift trying the computation, then you'd get an Exception e => EitherT a IO (Either e ()).

testEx :: (Exception e, MonadTrans m) => m IO (Either e ())
testEx = lift . try $ fails

You don't want the error in the result, you want to integrate the error into the EitherT. You want to integrate trying somethign with your EitherT

testEx :: (Exception e) => EitherT e IO ()
testEx = EitherT . try $ fails

We'll do this in general, then get just the message you want.

Integrate try with EitherT

You can extract the idea of integrating try with EitherT

tryIO :: (Exception e) => IO a -> EitherT e IO a
tryIO = EitherT . try

Or, for any underlying MonadIO as

tryIO :: (Exception e, MonadIO m) => IO a -> EitherT e m a
tryIO = EitherT . liftIO . try

(tryIO conflicts with a name from Control.Error. I couldn't come up with another name for this.)

You can then say you are willing to catch any exception. SomeException will catch all exceptions. If you are only interested in specific exceptions, use a different type. See Control.Exception for the details. If you aren't sure what you want to catch, you probably only want to catch IOExceptions; this is what tryIO from Control.Error does; see the last section.

anyException :: EitherT SomeException m a -> EitherT SomeException m a
anyException = id

You only want to keep the error message from the exception

message :: (Show e, Functor m) => EitherT e m a -> EitherT String m a
message = bimapEitherT show id

Then you can write

testEx :: EitherT String IO ()
testEx = message . anyException . tryIO $ fails

Integrate try with MonadError

You can instead integrate trying something with any MonadError, using MonadError and MonadIO to penetrate the transformer stack.

import Control.Monad.Except

tryIO :: (MonadError e m, MonadIO m, Exception e) => IO a -> m a
tryIO = (>>= either throwError return) . liftIO . try

You can write testEx in terms of this tryIO and anyException and message from the previous section

testEx :: EitherT String IO ()
testEx = message . anyException . tryIO $ fails

tryIO from Control.Error

The tryIO from Control.Error is essentially our first tryIO, except it only catches IOExceptions instead of any exception. It's actually defined as

tryIO :: (MonadIO m) => IO a -> EitherT IOException m a
tryIO = EitherT . liftIO . try

We can use it with message to write testEx as

testEx :: EitherT String IO ()
testEx = message . tryIO $ fails
4
  • thank you. i'll review the explanations in details ASAP. i think i did badly formulate my question in a way. I assumed that doing that is something I should do, which is good and idiomatic. At this point I start to think that the idiomatic way is to handle these exceptions higher-up. But your answer is interesting nonetheless, thank you! Sep 10, 2014 at 4:27
  • You might be interested in reading Gabriel Gonzalez's post when he announced errors, the package where the Control.Error you are using came from. haskellforall.com/2012/07/… The comments discuss a lot about which exceptions it's ok to catch, which influenced the definition of tryIO in Control.Error. It's amusing that we reached almost identical definitions with exactly the same name.
    – Cirdec
    Sep 10, 2014 at 5:44
  • Using typeclasses to deal with transformer stacks is idiomatic but unnecessary. The transformers package deliberately omits typeclasses because there are multiple different ways to deal with them: mtl, monads-tf, monads-fd, etc., though we seem to be standardizing on mtl. There are multiple type classes for something like EitherT. mtl has two: there's the MonadError in Control.Monad.Except and another MonadError in Control.Monad.Error.Class. I'd use the one from Control.Monad.Except. I don't know where the MonadThrow typeclass Michael suggests comes from.
    – Cirdec
    Sep 10, 2014 at 6:06
  • As my comment mentions, MonadThrow comes from the exceptions package. Sep 10, 2014 at 11:17
1

This is another simple approach: Let's define a custom monad transformer just like EitherT is defined:

{-# LANGUAGE FlexibleInstances, FunctionalDependencies #-}
import Control.Arrow (left)
import Control.Exception
import Control.Monad
import Control.Monad.Trans
import Control.Monad.Error
import Control.Monad.IO.Class

newtype ErrT a m b = ErrT { runErrT :: m (Either a b) }

instance (Monad m) => Monad (ErrT a m) where
    -- ...

instance (Monad m) => MonadError a (ErrT a m) where
    -- ...

instance MonadTrans (ErrT a) where
    lift = ErrT . liftM Right

together with the appropriate Applicative, Monad and MonadError instances.

Now let's add a means for an IOError to be converted to our error type. We can have a type class for this so that we're free in how we use the transformer.

class FromIOError e where
    fromIOException :: IOError -> e

Finally, we'll implement MonadIO in such a way that liftIO always catches IOErrors and converts them to the pure data type in the left part:

instance (MonadIO m, FromIOError a) => MonadIO (ErrT a m) where
    liftIO = ErrT . liftIO . liftM (left fromIOException)
                  . (try :: IO a -> IO (Either IOError a))

Now if we put all this in a module and export just the data type, runErrT, but not the constructor ErrT, everything that does IO within ErrT will have the exceptions properly handled, because IO actions can be introduced only through liftIO.

It'd be also possible to replace IOError with SomeException and handle all exceptions, if desired.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.