module Agda.Utils.IO where
import Control.Exception
import Control.Monad.State
import Control.Monad.Writer
class CatchIO m where
catchIO :: m a -> (IOException -> m a) -> m a
instance CatchIO IO where
catchIO :: forall a. IO a -> (IOException -> IO a) -> IO a
catchIO = forall e a. Exception e => IO a -> (e -> IO a) -> IO a
catch
instance CatchIO m => CatchIO (WriterT w m) where
catchIO :: forall a.
WriterT w m a -> (IOException -> WriterT w m a) -> WriterT w m a
catchIO WriterT w m a
m IOException -> WriterT w m a
h = forall w (m :: * -> *) a. m (a, w) -> WriterT w m a
WriterT forall a b. (a -> b) -> a -> b
$ forall w (m :: * -> *) a. WriterT w m a -> m (a, w)
runWriterT WriterT w m a
m forall (m :: * -> *) a.
CatchIO m =>
m a -> (IOException -> m a) -> m a
`catchIO` \ IOException
e -> forall w (m :: * -> *) a. WriterT w m a -> m (a, w)
runWriterT (IOException -> WriterT w m a
h IOException
e)
instance CatchIO m => CatchIO (StateT s m) where
catchIO :: forall a.
StateT s m a -> (IOException -> StateT s m a) -> StateT s m a
catchIO StateT s m a
m IOException -> StateT s m a
h = forall s (m :: * -> *) a. (s -> m (a, s)) -> StateT s m a
StateT forall a b. (a -> b) -> a -> b
$ \s
s -> forall s (m :: * -> *) a. StateT s m a -> s -> m (a, s)
runStateT StateT s m a
m s
s forall (m :: * -> *) a.
CatchIO m =>
m a -> (IOException -> m a) -> m a
`catchIO` \ IOException
e -> forall s (m :: * -> *) a. StateT s m a -> s -> m (a, s)
runStateT (IOException -> StateT s m a
h IOException
e) s
s