{-# LANGUAGE FlexibleContexts #-}
module Control.Monad.CatchIO.Try
  (
    tryIO
  , eitherIO
{-  
  , MonadCatchIO
  , MonadIO
  , liftIO
  , MonadError
  , Error
  , ErrorType
  , throwError
  , strMsg
  , ErrorT
  , runErrorT-}
  ) where


import           Control.Exception         (IOException)
import           Control.Monad.CatchIO     (MonadCatchIO,tryJust)
import           Control.Monad.Trans.Error (strMsg)
import           Control.Monad.Error       (MonadError,Error,ErrorType,throwError,MonadIO,liftIO)


tryIO :: (Error (ErrorType m),MonadError m,MonadCatchIO m,Functor m) => IO a -> m a
tryIO :: forall (m :: * -> *) a.
(Error (ErrorType m), MonadError m, MonadCatchIO m, Functor m) =>
IO a -> m a
tryIO = (Either IOException a -> m a) -> m (Either IOException a) -> m a
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
(=<<) ((IOException -> m a) -> (a -> m a) -> Either IOException a -> m a
forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either (ErrorType m -> m a
forall a. ErrorType m -> m a
forall (m :: * -> *) a. MonadError m => ErrorType m -> m a
throwError (ErrorType m -> m a)
-> (IOException -> ErrorType m) -> IOException -> m a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> ErrorType m
forall a. Error a => String -> a
strMsg (String -> ErrorType m)
-> (IOException -> String) -> IOException -> ErrorType m
forall b c a. (b -> c) -> (a -> b) -> a -> c
. IOException -> String
forall a. Show a => a -> String
show) a -> m a
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return) (m (Either IOException a) -> m a)
-> (IO a -> m (Either IOException a)) -> IO a -> m a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. m a -> m (Either IOException a)
forall (m :: * -> *) a.
(MonadCatchIO m, Functor m) =>
m a -> m (Either IOException a)
eitherIO (m a -> m (Either IOException a))
-> (IO a -> m a) -> IO a -> m (Either IOException a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. IO a -> m a
forall a. IO a -> m a
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO

eitherIO :: (MonadCatchIO m,Functor m) => m a -> m (Either IOException a)
eitherIO :: forall (m :: * -> *) a.
(MonadCatchIO m, Functor m) =>
m a -> m (Either IOException a)
eitherIO = (IOException -> Maybe IOException)
-> m a -> m (Either IOException a)
forall (m :: * -> *) e b a.
(MonadCatchIO m, Functor m, Exception e) =>
(e -> Maybe b) -> m a -> m (Either b a)
tryJust (IOException -> Maybe IOException
forall a. a -> Maybe a
Just :: IOException -> Maybe IOException)