module Ether.Except
(
MonadExcept
, throw
, catch
, Except
, runExcept
, ExceptT
, exceptT
, runExceptT
, MonadExcept'
, throw'
, catch'
, Except'
, runExcept'
, ExceptT'
, exceptT'
, runExceptT'
, TAGGED
, EXCEPT
) where
import qualified Control.Monad.Except as T
import Control.Monad.Signatures (Catch)
import qualified Control.Monad.Trans.Lift.Catch as Lift
import Data.Coerce
import Data.Functor.Identity
import Ether.TaggedTrans
import Ether.Internal
class Monad m => MonadExcept tag e m | m tag -> e where
throw :: e -> m a
catch :: m a -> (e -> m a) -> m a
instance
( Lift.LiftCatch t
, Monad (t m)
, MonadExcept tag e m
) => MonadExcept tag e (t m) where
throw = Lift.lift . throw @tag
catch = Lift.liftCatch (catch @tag)
data EXCEPT
type Except tag e = ExceptT tag e Identity
type ExceptT tag e = TaggedTrans (TAGGED EXCEPT tag) (T.ExceptT e)
runExcept :: forall tag e a . Except tag e a -> Either e a
runExcept = coerce (T.runExcept @e @a)
runExceptT :: forall tag e m a . ExceptT tag e m a -> m (Either e a)
runExceptT = coerce (T.runExceptT @e @m @a)
exceptT :: forall tag e m a . m (Either e a) -> ExceptT tag e m a
exceptT = coerce (T.ExceptT @e @m @a)
type instance HandleSuper EXCEPT e trans = ()
type instance HandleConstraint EXCEPT e trans m =
T.MonadError e (trans m)
instance Handle EXCEPT e (T.ExceptT e) where
handling r = r
instance
( Handle EXCEPT e trans
, Monad m, Monad (trans m)
) => MonadExcept tag e (TaggedTrans (TAGGED EXCEPT tag) trans m)
where
throw =
handling @EXCEPT @e @trans @m $
coerce (T.throwError @e @(trans m) @a) ::
forall eff a . e -> TaggedTrans eff trans m a
catch =
handling @EXCEPT @e @trans @m $
coerce (T.catchError @e @(trans m) @a) ::
forall eff a . Catch e (TaggedTrans eff trans m) a
type MonadExcept' e = MonadExcept e e
throw' :: forall e m a . MonadExcept' e m => e -> m a
throw' = throw @e
catch' :: forall e m a . MonadExcept' e m => m a -> (e -> m a) -> m a
catch' = catch @e
type Except' e = Except e e
runExcept' :: Except' e a -> Either e a
runExcept' = runExcept
type ExceptT' e = ExceptT e e
exceptT' :: m (Either e a) -> ExceptT' e m a
exceptT' = exceptT
runExceptT' :: ExceptT' e m a -> m (Either e a)
runExceptT' = runExceptT