FW: MonadIO needs catch?
Ross Paterson
ross@soi.city.ac.uk
Thu, 6 Feb 2003 12:53:12 +0000
On Thu, Feb 06, 2003 at 09:38:07AM -0000, Simon Peyton-Jones wrote:
> This question from John Meacham is really a Libraries Team question.
> Should the MonadIO class get an extra method? His case looks plausible
> to me.
>
> Simon
>
> -----Original Message-----
> From: John Meacham [mailto:john@repetae.net]
> Sent: 28 January 2003 06:35
> To: haskell@haskell.org
> Subject: MonadIO needs catch?
>
> while genericizing my utility libraries to work within any MonadIO
> (http://haskell.cs.yale.edu/ghc/docs/latest/html/base/Control.Monad.Tran
> s.html)
> I ran into a problem, 'liftIO' is not powerful enough to catch
> exceptions in a MonadIO.
> the closest one can get is
>
> > genCatch :: MonadIO m => IO a -> (Exception -> IO a) -> m a
> when we want..
> > genCatch :: MonadIO m => m a -> (Exception -> m a) -> m a
>
> The only way i can think of to solve this is add 'catch' (perhaps
> 'catchIO'? 'genCatch'?) to the MonadIO class.
>
> some open questions are whether it is possible to implement catch for
> any MonadIO? I would think so, since you need only store the 'internal
> state' of your monad at the genCatch and continue with that if an
> exception was raised. (this might mix badly with other extensions
> though?) at worst a MonadIOCatchable deriving from MonadIO could be
> added...
Such operations should satisfy
genCatch (liftIO m) (liftIO . k) = liftIO (catch m k)
It's fairly straightforward to define instances for StateT, ReaderT and
WriterT, but I can't think what they should be for ErrorT, ContT or
ListT. (Then again, I don't think ListT IO is a monad.)