{-# LANGUAGE TypeFamilies #-}
module Network.Protocol.TLS.GNU.ErrorT
( ErrorT (..)
, mapErrorT
) where
import Control.Applicative (Applicative, pure, (<*>))
import Control.Monad (ap,liftM)
import Control.Monad.Trans (MonadIO, liftIO)
import Control.Monad.Trans.Class (MonadTrans, lift)
import qualified Control.Monad.Error as E
import Control.Monad.Error (ErrorType)
import qualified Control.Monad.Reader as R
import Control.Monad.Reader (EnvType)
newtype ErrorT e m a = ErrorT { ErrorT e m a -> m (Either e a)
runErrorT :: m (Either e a) }
instance Functor m => Functor (ErrorT e m) where
fmap :: (a -> b) -> ErrorT e m a -> ErrorT e m b
fmap f :: a -> b
f = m (Either e b) -> ErrorT e m b
forall e (m :: * -> *) a. m (Either e a) -> ErrorT e m a
ErrorT (m (Either e b) -> ErrorT e m b)
-> (ErrorT e m a -> m (Either e b)) -> ErrorT e m a -> ErrorT e m b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Either e a -> Either e b) -> m (Either e a) -> m (Either e b)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap ((a -> b) -> Either e a -> Either e b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap a -> b
f) (m (Either e a) -> m (Either e b))
-> (ErrorT e m a -> m (Either e a))
-> ErrorT e m a
-> m (Either e b)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ErrorT e m a -> m (Either e a)
forall e (m :: * -> *) a. ErrorT e m a -> m (Either e a)
runErrorT
instance (Functor m, Monad m) => Applicative (ErrorT e m) where
pure :: a -> ErrorT e m a
pure a :: a
a = m (Either e a) -> ErrorT e m a
forall e (m :: * -> *) a. m (Either e a) -> ErrorT e m a
ErrorT (m (Either e a) -> ErrorT e m a) -> m (Either e a) -> ErrorT e m a
forall a b. (a -> b) -> a -> b
$ Either e a -> m (Either e a)
forall (m :: * -> *) a. Monad m => a -> m a
return (a -> Either e a
forall a b. b -> Either a b
Right a
a)
f :: ErrorT e m (a -> b)
f <*> :: ErrorT e m (a -> b) -> ErrorT e m a -> ErrorT e m b
<*> v :: ErrorT e m a
v = m (Either e b) -> ErrorT e m b
forall e (m :: * -> *) a. m (Either e a) -> ErrorT e m a
ErrorT (m (Either e b) -> ErrorT e m b) -> m (Either e b) -> ErrorT e m b
forall a b. (a -> b) -> a -> b
$ do
Either e (a -> b)
mf <- ErrorT e m (a -> b) -> m (Either e (a -> b))
forall e (m :: * -> *) a. ErrorT e m a -> m (Either e a)
runErrorT ErrorT e m (a -> b)
f
case Either e (a -> b)
mf of
Left e :: e
e -> Either e b -> m (Either e b)
forall (m :: * -> *) a. Monad m => a -> m a
return (e -> Either e b
forall a b. a -> Either a b
Left e
e)
Right k :: a -> b
k -> do
Either e a
mv <- ErrorT e m a -> m (Either e a)
forall e (m :: * -> *) a. ErrorT e m a -> m (Either e a)
runErrorT ErrorT e m a
v
case Either e a
mv of
Left e :: e
e -> Either e b -> m (Either e b)
forall (m :: * -> *) a. Monad m => a -> m a
return (e -> Either e b
forall a b. a -> Either a b
Left e
e)
Right x :: a
x -> Either e b -> m (Either e b)
forall (m :: * -> *) a. Monad m => a -> m a
return (b -> Either e b
forall a b. b -> Either a b
Right (a -> b
k a
x))
instance Monad m => Monad (ErrorT e m) where
return :: a -> ErrorT e m a
return = m (Either e a) -> ErrorT e m a
forall e (m :: * -> *) a. m (Either e a) -> ErrorT e m a
ErrorT (m (Either e a) -> ErrorT e m a)
-> (a -> m (Either e a)) -> a -> ErrorT e m a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Either e a -> m (Either e a)
forall (m :: * -> *) a. Monad m => a -> m a
return (Either e a -> m (Either e a))
-> (a -> Either e a) -> a -> m (Either e a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> Either e a
forall a b. b -> Either a b
Right
>>= :: ErrorT e m a -> (a -> ErrorT e m b) -> ErrorT e m b
(>>=) m :: ErrorT e m a
m k :: a -> ErrorT e m b
k = m (Either e b) -> ErrorT e m b
forall e (m :: * -> *) a. m (Either e a) -> ErrorT e m a
ErrorT (m (Either e b) -> ErrorT e m b) -> m (Either e b) -> ErrorT e m b
forall a b. (a -> b) -> a -> b
$ do
Either e a
x <- ErrorT e m a -> m (Either e a)
forall e (m :: * -> *) a. ErrorT e m a -> m (Either e a)
runErrorT ErrorT e m a
m
case Either e a
x of
Left l :: e
l -> Either e b -> m (Either e b)
forall (m :: * -> *) a. Monad m => a -> m a
return (Either e b -> m (Either e b)) -> Either e b -> m (Either e b)
forall a b. (a -> b) -> a -> b
$ e -> Either e b
forall a b. a -> Either a b
Left e
l
Right r :: a
r -> ErrorT e m b -> m (Either e b)
forall e (m :: * -> *) a. ErrorT e m a -> m (Either e a)
runErrorT (ErrorT e m b -> m (Either e b)) -> ErrorT e m b -> m (Either e b)
forall a b. (a -> b) -> a -> b
$ a -> ErrorT e m b
k a
r
instance Monad m => E.MonadError (ErrorT e m) where
type ErrorType (ErrorT e m) = e
throwError :: ErrorType (ErrorT e m) -> ErrorT e m a
throwError = m (Either e a) -> ErrorT e m a
forall e (m :: * -> *) a. m (Either e a) -> ErrorT e m a
ErrorT (m (Either e a) -> ErrorT e m a)
-> (e -> m (Either e a)) -> e -> ErrorT e m a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Either e a -> m (Either e a)
forall (m :: * -> *) a. Monad m => a -> m a
return (Either e a -> m (Either e a))
-> (e -> Either e a) -> e -> m (Either e a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. e -> Either e a
forall a b. a -> Either a b
Left
catchError :: ErrorT e m a
-> (ErrorType (ErrorT e m) -> ErrorT e m a) -> ErrorT e m a
catchError m :: ErrorT e m a
m h :: ErrorType (ErrorT e m) -> ErrorT e m a
h = m (Either e a) -> ErrorT e m a
forall e (m :: * -> *) a. m (Either e a) -> ErrorT e m a
ErrorT (m (Either e a) -> ErrorT e m a) -> m (Either e a) -> ErrorT e m a
forall a b. (a -> b) -> a -> b
$ do
Either e a
x <- ErrorT e m a -> m (Either e a)
forall e (m :: * -> *) a. ErrorT e m a -> m (Either e a)
runErrorT ErrorT e m a
m
case Either e a
x of
Left l :: e
l -> ErrorT e m a -> m (Either e a)
forall e (m :: * -> *) a. ErrorT e m a -> m (Either e a)
runErrorT (ErrorT e m a -> m (Either e a)) -> ErrorT e m a -> m (Either e a)
forall a b. (a -> b) -> a -> b
$ ErrorType (ErrorT e m) -> ErrorT e m a
h e
ErrorType (ErrorT e m)
l
Right r :: a
r -> Either e a -> m (Either e a)
forall (m :: * -> *) a. Monad m => a -> m a
return (Either e a -> m (Either e a)) -> Either e a -> m (Either e a)
forall a b. (a -> b) -> a -> b
$ a -> Either e a
forall a b. b -> Either a b
Right a
r
instance MonadTrans (ErrorT e) where
lift :: m a -> ErrorT e m a
lift = m (Either e a) -> ErrorT e m a
forall e (m :: * -> *) a. m (Either e a) -> ErrorT e m a
ErrorT (m (Either e a) -> ErrorT e m a)
-> (m a -> m (Either e a)) -> m a -> ErrorT e m a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (a -> Either e a) -> m a -> m (Either e a)
forall (m :: * -> *) a1 r. Monad m => (a1 -> r) -> m a1 -> m r
liftM a -> Either e a
forall a b. b -> Either a b
Right
instance R.MonadReader m => R.MonadReader (ErrorT e m) where
type EnvType (ErrorT e m) = EnvType m
ask :: ErrorT e m (EnvType (ErrorT e m))
ask = m (EnvType m) -> ErrorT e m (EnvType m)
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift m (EnvType m)
forall (m :: * -> *). MonadReader m => m (EnvType m)
R.ask
local :: (EnvType (ErrorT e m) -> EnvType (ErrorT e m))
-> ErrorT e m a -> ErrorT e m a
local = (m (Either e a) -> m (Either e a)) -> ErrorT e m a -> ErrorT e m a
forall (m :: * -> *) e a (n :: * -> *) e' b.
(m (Either e a) -> n (Either e' b))
-> ErrorT e m a -> ErrorT e' n b
mapErrorT ((m (Either e a) -> m (Either e a))
-> ErrorT e m a -> ErrorT e m a)
-> ((EnvType m -> EnvType m) -> m (Either e a) -> m (Either e a))
-> (EnvType m -> EnvType m)
-> ErrorT e m a
-> ErrorT e m a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (EnvType m -> EnvType m) -> m (Either e a) -> m (Either e a)
forall (m :: * -> *) a.
MonadReader m =>
(EnvType m -> EnvType m) -> m a -> m a
R.local
instance MonadIO m => MonadIO (ErrorT e m) where
liftIO :: IO a -> ErrorT e m a
liftIO = m a -> ErrorT e m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (m a -> ErrorT e m a) -> (IO a -> m a) -> IO a -> ErrorT e m a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. IO a -> m a
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO
mapErrorT :: (m (Either e a) -> n (Either e' b))
-> ErrorT e m a
-> ErrorT e' n b
mapErrorT :: (m (Either e a) -> n (Either e' b))
-> ErrorT e m a -> ErrorT e' n b
mapErrorT f :: m (Either e a) -> n (Either e' b)
f m :: ErrorT e m a
m = n (Either e' b) -> ErrorT e' n b
forall e (m :: * -> *) a. m (Either e a) -> ErrorT e m a
ErrorT (n (Either e' b) -> ErrorT e' n b)
-> n (Either e' b) -> ErrorT e' n b
forall a b. (a -> b) -> a -> b
$ m (Either e a) -> n (Either e' b)
f (ErrorT e m a -> m (Either e a)
forall e (m :: * -> *) a. ErrorT e m a -> m (Either e a)
runErrorT ErrorT e m a
m)