module Control.Monad.Error.Hoist
( HoistError(..)
, (<%?>)
, (<%!?>)
, (<?>)
, (<!?>)
) where
import Control.Monad.Error.Class
import Control.Monad.Trans.Either
import Control.Monad.Trans
class Monad m ⇒ HoistError m t e e' | t → e where
hoistError
∷ (e → e')
→ t α
→ m α
instance MonadError e m ⇒ HoistError m Maybe () e where
hoistError f = maybe (throwError $ f ()) return
instance MonadError e' m ⇒ HoistError m (Either e) e e' where
hoistError f = either (throwError . f) return
instance (m ~ n, MonadError e' m) ⇒ HoistError m (EitherT e n) e e' where
hoistError f = eitherT (throwError . f) return
(<%?>)
∷ HoistError m t e e'
⇒ t α
→ (e → e')
→ m α
(<%?>) = flip hoistError
infixl 8 <%?>
(<%!?>)
∷ HoistError m t e e'
⇒ m (t α)
→ (e → e')
→ m α
m <%!?> e = do
x ← m
x <%?> e
infixl 8 <%!?>
(<?>)
∷ HoistError m t e e'
⇒ t α
→ e'
→ m α
m <?> e = m <%?> const e
infixl 8 <?>
(<!?>)
∷ HoistError m t e e'
⇒ m (t α)
→ e'
→ m α
m <!?> e = do
x ← m
x <?> e
infixl 8 <!?>