module Control.Monad.Trans.Either (
EitherT
, newEitherT
, pattern EitherT
, runEitherT
, eitherT
, left
, right
, mapEitherT
, hoistEither
, bimapEitherT
, firstEitherT
, secondEitherT
, hoistMaybe
, hoistEitherT
) where
import Control.Monad (Monad(..), (=<<))
import Control.Monad.Trans.Except (ExceptT(..))
import Data.Maybe (Maybe, maybe)
import Data.Either (Either(..), either)
import Data.Function ((.), id)
import Data.Functor (Functor(..))
type EitherT = ExceptT
pattern EitherT :: m (Either x a) -> ExceptT x m a
pattern EitherT m = ExceptT m
runEitherT :: EitherT x m a -> m (Either x a)
runEitherT (ExceptT m) = m
newEitherT :: m (Either x a) -> EitherT x m a
newEitherT =
ExceptT
eitherT :: Monad m => (x -> m b) -> (a -> m b) -> EitherT x m a -> m b
eitherT f g m =
either f g =<< runEitherT m
left :: Monad m => x -> EitherT x m a
left =
EitherT . return . Left
right :: Monad m => a -> EitherT x m a
right =
return
mapEitherT :: (m (Either x a) -> n (Either y b)) -> EitherT x m a -> EitherT y n b
mapEitherT f =
EitherT . f . runEitherT
hoistEither :: Monad m => Either x a -> EitherT x m a
hoistEither =
EitherT . return
bimapEitherT :: Functor m => (x -> y) -> (a -> b) -> EitherT x m a -> EitherT y m b
bimapEitherT f g =
let
h (Left e) = Left (f e)
h (Right a) = Right (g a)
in
mapEitherT (fmap h)
firstEitherT :: Functor m => (x -> y) -> EitherT x m a -> EitherT y m a
firstEitherT f =
bimapEitherT f id
secondEitherT :: Functor m => (a -> b) -> EitherT x m a -> EitherT x m b
secondEitherT =
bimapEitherT id
hoistMaybe :: Monad m => x -> Maybe a -> EitherT x m a
hoistMaybe x =
maybe (left x) return
hoistEitherT :: (forall b. m b -> n b) -> EitherT x m a -> EitherT x n a
hoistEitherT f =
EitherT . f . runEitherT