haskell / mtl

The Monad Transformer Library
http://www.haskell.org/haskellwiki/Monad_Transformers
Other
366 stars 65 forks source link

Encapsulate IO exceptions in Monad IO m, MonadError e m #28

Closed Denommus closed 8 years ago

Denommus commented 8 years ago

I'm actually not sure whether this is an issue, or if it fits the project goals at all, but I keep wondering why we can't have an encapsulation of core exceptions from IO actions (since they are far too common) under a more type-safe approach such as MonadError e m.

What I'm thinking about is something like

import Control.Monad.Except
import Control.Exception (Exception (..), try)

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

liftedBracket :: (Exception e, MonadIO m, MonadError e m) =>
                 m a
              -> (a -> m b)
              -> (a -> m c)
              -> m c
liftedBracket acquire release comp = do
  resource <- acquire
  result <- catchError (comp resource) (\e -> release resource >> throwError e)
  _ <- release resource
  return result
ekmett commented 8 years ago

Control.Exception steps well outside of the language extensions that the mtl has used to date and would tie the library very closely to GHC.

ekmett commented 8 years ago

Closing this out based on our discussion on IRC.