{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE DefaultSignatures #-}
{-# LANGUAGE FunctionalDependencies #-}
{-# LANGUAGE GADTs #-}
{-# LANGUAGE TypeOperators #-}
{-# LANGUAGE UndecidableInstances #-}
module Control.Monad.Error.Hoist
( hoistError
, hoistErrorM
, (<%?>)
, (<%!?>)
, (<?>)
, (<!?>)
, PluckError(..)
) where
import Control.Monad ((<=<))
import Control.Monad.Error.Class (MonadError (..))
import Data.Either (Either, either)
import Control.Monad.Except (Except, ExceptT, runExcept,
runExceptT)
hoistError
:: (PluckError e t m, MonadError e' m)
=> (e -> e')
-> t a
-> m a
hoistError :: forall e (t :: * -> *) (m :: * -> *) e' a.
(PluckError e t m, MonadError e' m) =>
(e -> e') -> t a -> m a
hoistError e -> e'
f = (e -> m a) -> (a -> m a) -> t a -> m a
forall r a. (e -> m r) -> (a -> m r) -> t a -> m r
forall e (t :: * -> *) (m :: * -> *) r a.
PluckError e t m =>
(e -> m r) -> (a -> m r) -> t a -> m r
foldError (e' -> m a
forall a. e' -> m a
forall e (m :: * -> *) a. MonadError e m => e -> m a
throwError (e' -> m a) -> (e -> e') -> e -> m a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. e -> e'
f) a -> m a
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure
hoistErrorM
:: (PluckError e t m, MonadError e' m)
=> (e -> e')
-> m (t a)
-> m a
hoistErrorM :: forall e (t :: * -> *) (m :: * -> *) e' a.
(PluckError e t m, MonadError e' m) =>
(e -> e') -> m (t a) -> m a
hoistErrorM e -> e'
e m (t a)
m = m (t a)
m m (t a) -> (t a -> m a) -> m a
forall a b. m a -> (a -> m b) -> m b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= (e -> e') -> t a -> m a
forall e (t :: * -> *) (m :: * -> *) e' a.
(PluckError e t m, MonadError e' m) =>
(e -> e') -> t a -> m a
hoistError e -> e'
e
(<%?>)
:: (PluckError e t m, MonadError e' m)
=> t a
-> (e -> e')
-> m a
<%?> :: forall e (t :: * -> *) (m :: * -> *) e' a.
(PluckError e t m, MonadError e' m) =>
t a -> (e -> e') -> m a
(<%?>) = ((e -> e') -> t a -> m a) -> t a -> (e -> e') -> m a
forall a b c. (a -> b -> c) -> b -> a -> c
flip (e -> e') -> t a -> m a
forall e (t :: * -> *) (m :: * -> *) e' a.
(PluckError e t m, MonadError e' m) =>
(e -> e') -> t a -> m a
hoistError
infixl 8 <%?>
{-# INLINE (<%?>) #-}
(<%!?>)
:: (PluckError e t m, MonadError e' m)
=> m (t a)
-> (e -> e')
-> m a
<%!?> :: forall e (t :: * -> *) (m :: * -> *) e' a.
(PluckError e t m, MonadError e' m) =>
m (t a) -> (e -> e') -> m a
(<%!?>) = ((e -> e') -> m (t a) -> m a) -> m (t a) -> (e -> e') -> m a
forall a b c. (a -> b -> c) -> b -> a -> c
flip (e -> e') -> m (t a) -> m a
forall e (t :: * -> *) (m :: * -> *) e' a.
(PluckError e t m, MonadError e' m) =>
(e -> e') -> m (t a) -> m a
hoistErrorM
infixl 8 <%!?>
{-# INLINE (<%!?>) #-}
(<?>)
:: (PluckError e t m, MonadError e' m)
=> t a
-> e'
-> m a
t a
m <?> :: forall e (t :: * -> *) (m :: * -> *) e' a.
(PluckError e t m, MonadError e' m) =>
t a -> e' -> m a
<?> e'
e = t a
m t a -> (e -> e') -> m a
forall e (t :: * -> *) (m :: * -> *) e' a.
(PluckError e t m, MonadError e' m) =>
t a -> (e -> e') -> m a
<%?> e' -> e -> e'
forall a b. a -> b -> a
const e'
e
infixl 8 <?>
{-# INLINE (<?>) #-}
(<!?>)
:: (PluckError e t m, MonadError e' m)
=> m (t a)
-> e'
-> m a
m (t a)
m <!?> :: forall e (t :: * -> *) (m :: * -> *) e' a.
(PluckError e t m, MonadError e' m) =>
m (t a) -> e' -> m a
<!?> e'
e = do
t a
x <- m (t a)
m
t a
x t a -> e' -> m a
forall e (t :: * -> *) (m :: * -> *) e' a.
(PluckError e t m, MonadError e' m) =>
t a -> e' -> m a
<?> e'
e
infixl 8 <!?>
{-# INLINE (<!?>) #-}
class PluckError e t m | t -> e where
pluckError :: t a -> m (Either e a)
default pluckError :: Applicative m => t a -> m (Either e a)
pluckError = (e -> m (Either e a))
-> (a -> m (Either e a)) -> t a -> m (Either e a)
forall r a. (e -> m r) -> (a -> m r) -> t a -> m r
forall e (t :: * -> *) (m :: * -> *) r a.
PluckError e t m =>
(e -> m r) -> (a -> m r) -> t a -> m r
foldError (Either e a -> m (Either e a)
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (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) (Either e a -> m (Either e a)
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (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)
foldError :: (e -> m r) -> (a -> m r) -> t a -> m r
default foldError :: Monad m => (e -> m r) -> (a -> m r) -> t a -> m r
foldError e -> m r
f a -> m r
g = (e -> m r) -> (a -> m r) -> Either e a -> m r
forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either e -> m r
f a -> m r
g (Either e a -> m r) -> (t a -> m (Either e a)) -> t a -> m r
forall (m :: * -> *) b c a.
Monad m =>
(b -> m c) -> (a -> m b) -> a -> m c
<=< t a -> m (Either e a)
forall a. t a -> m (Either e a)
forall e (t :: * -> *) (m :: * -> *) a.
PluckError e t m =>
t a -> m (Either e a)
pluckError
{-# MINIMAL pluckError | foldError #-}
instance (Applicative m, e ~ ()) => PluckError e Maybe m where
pluckError :: forall a. Maybe a -> m (Either e a)
pluckError = Either e a -> m (Either e a)
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Either e a -> m (Either e a))
-> (Maybe a -> Either e a) -> Maybe a -> m (Either e a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Either e a -> (a -> Either e a) -> Maybe a -> Either e a
forall b a. b -> (a -> b) -> Maybe a -> b
maybe (e -> Either e a
forall a b. a -> Either a b
Left ()) a -> Either e a
forall a b. b -> Either a b
Right
foldError :: forall r a. (e -> m r) -> (a -> m r) -> Maybe a -> m r
foldError e -> m r
f = m r -> (a -> m r) -> Maybe a -> m r
forall b a. b -> (a -> b) -> Maybe a -> b
maybe (e -> m r
f ())
instance Applicative m => PluckError e (Either e) m where
pluckError :: forall a. Either e a -> m (Either e a)
pluckError = Either e a -> m (Either e a)
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure
foldError :: forall r a. (e -> m r) -> (a -> m r) -> Either e a -> m r
foldError = (e -> m r) -> (a -> m r) -> Either e a -> m r
forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either
instance Monad m => PluckError e (ExceptT e m) m where
pluckError :: forall a. ExceptT e m a -> m (Either e a)
pluckError = ExceptT e m a -> m (Either e a)
forall e (m :: * -> *) a. ExceptT e m a -> m (Either e a)
runExceptT
foldError :: forall r a. (e -> m r) -> (a -> m r) -> ExceptT e m a -> m r
foldError e -> m r
f a -> m r
g = (e -> m r) -> (a -> m r) -> Either e a -> m r
forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either e -> m r
f a -> m r
g (Either e a -> m r)
-> (ExceptT e m a -> m (Either e a)) -> ExceptT e m a -> m r
forall (m :: * -> *) b c a.
Monad m =>
(b -> m c) -> (a -> m b) -> a -> m c
<=< ExceptT e m a -> m (Either e a)
forall e (m :: * -> *) a. ExceptT e m a -> m (Either e a)
runExceptT