Safe Haskell | None |
---|---|
Language | Haskell2010 |
Control.Monad.CheckedExcept
Description
Basic API of CheckedExceptT
Synopsis
- newtype CheckedExceptT (exceptions :: [Type]) (m :: Type -> Type) a = CheckedExceptT {
- runCheckedExceptT :: m (Either (OneOf exceptions) a)
- type CheckedExcept (es :: [Type]) a = CheckedExceptT es Identity a
- data OneOf (es :: [Type]) where
- data CaseException x (es :: [Type]) where
- CaseEndWith :: forall x. x -> CaseException x ('[] :: [Type])
- CaseCons :: forall e x (es1 :: [Type]). Typeable e => (e -> x) -> CaseException x es1 -> CaseException x (e ': es1)
- CaseAny :: forall x (es :: [Type]). (forall e. CheckedException e => e -> x) -> CaseException x es
- pattern CaseEnd :: CaseException x ('[] :: [Type])
- newtype ShowException a = ShowException a
- newtype ExceptionException a = ExceptionException a
- class Typeable e => CheckedException e where
- runCheckedExcept :: forall (es :: [Type]) a. CheckedExcept es a -> Either (OneOf es) a
- throwCheckedException :: forall e (es :: [Type]) (m :: Type -> Type) a. (Elem e es, CheckedException e, Applicative m) => e -> CheckedExceptT es m a
- applyAll :: forall b (es :: [Type]). (forall e. CheckedException e => e -> b) -> OneOf es -> b
- weakenExceptions :: forall (exceptions1 :: [Type]) (exceptions2 :: [Type]) (m :: Type -> Type) a. (Functor m, Contains exceptions1 exceptions2) => CheckedExceptT exceptions1 m a -> CheckedExceptT exceptions2 m a
- weakenOneOf :: forall (exceptions1 :: [Type]) (exceptions2 :: [Type]). Contains exceptions1 exceptions2 => OneOf exceptions1 -> OneOf exceptions2
- withOneOf :: forall e (es :: [Type]) a. (Elem e es, Monoid a, CheckedException e) => OneOf es -> (e -> a) -> a
- withOneOf' :: forall (es :: [Type]) a. OneOf es -> (forall e. (Elem e es, CheckedException e, Typeable e) => e -> a) -> a
- caseException :: forall (es :: [Type]) x. NonEmpty es => OneOf es -> CaseException x (Nub es) -> x
- (<:) :: forall e x (es :: [Type]). Typeable e => (e -> x) -> CaseException x es -> CaseException x (e ': es)
- catchSomeException :: forall (m :: Type -> Type) (es :: [Type]) a. (Monad m, MonadCatch m, Elem SomeException es) => CheckedExceptT es m a -> CheckedExceptT es m a
- type family Contains (as :: [k]) (bs :: [k]) where ...
- type family Elem (x :: t) (xs :: [t]) where ...
- type family Elem' (x :: a) (xs :: [a]) :: Bool where ...
- type family NonEmpty (xs :: [a]) where ...
- type NotElemTypeError (x :: t) (xs :: t1) = TypeError (('ShowType x ':<>: 'Text " is not a member of ") ':<>: 'ShowType xs) :: k
- type family Nub (xs :: [a]) :: [a] where ...
- type family Remove (x :: a) (xs :: [a]) :: [a] where ...
- type family (xs :: [k]) ++ (ys :: [k]) :: [k] where ...
Types
newtype CheckedExceptT (exceptions :: [Type]) (m :: Type -> Type) a Source #
Isomorphic to ExceptT
over our open-union exceptions type
.
Because many effects systems have an OneOf
esExceptT
analogue, this would be pretty simple to port to any effects system.
See Control.Monad.CheckedExcept.QualifiedDo for example usages.
Constructors
CheckedExceptT | |
Fields
|
Instances
MonadTrans (CheckedExceptT exceptions) Source # | |
Defined in Control.Monad.CheckedExcept Methods lift :: Monad m => m a -> CheckedExceptT exceptions m a # | |
Monad m => MonadError (OneOf exceptions) (CheckedExceptT exceptions m) Source # | |
Defined in Control.Monad.CheckedExcept Methods throwError :: OneOf exceptions -> CheckedExceptT exceptions m a # catchError :: CheckedExceptT exceptions m a -> (OneOf exceptions -> CheckedExceptT exceptions m a) -> CheckedExceptT exceptions m a # | |
MonadIO m => MonadIO (CheckedExceptT exceptions m) Source # | |
Defined in Control.Monad.CheckedExcept Methods liftIO :: IO a -> CheckedExceptT exceptions m a # | |
Monad m => Applicative (CheckedExceptT exceptions m) Source # | |
Defined in Control.Monad.CheckedExcept Methods pure :: a -> CheckedExceptT exceptions m a # (<*>) :: CheckedExceptT exceptions m (a -> b) -> CheckedExceptT exceptions m a -> CheckedExceptT exceptions m b # liftA2 :: (a -> b -> c) -> CheckedExceptT exceptions m a -> CheckedExceptT exceptions m b -> CheckedExceptT exceptions m c # (*>) :: CheckedExceptT exceptions m a -> CheckedExceptT exceptions m b -> CheckedExceptT exceptions m b # (<*) :: CheckedExceptT exceptions m a -> CheckedExceptT exceptions m b -> CheckedExceptT exceptions m a # | |
Functor m => Functor (CheckedExceptT exceptions m) Source # | |
Defined in Control.Monad.CheckedExcept Methods fmap :: (a -> b) -> CheckedExceptT exceptions m a -> CheckedExceptT exceptions m b # (<$) :: a -> CheckedExceptT exceptions m b -> CheckedExceptT exceptions m a # | |
Monad m => Monad (CheckedExceptT exceptions m) Source # | |
Defined in Control.Monad.CheckedExcept Methods (>>=) :: CheckedExceptT exceptions m a -> (a -> CheckedExceptT exceptions m b) -> CheckedExceptT exceptions m b # (>>) :: CheckedExceptT exceptions m a -> CheckedExceptT exceptions m b -> CheckedExceptT exceptions m b # return :: a -> CheckedExceptT exceptions m a # | |
MonadFail m => MonadFail (CheckedExceptT exceptions m) Source # | |
Defined in Control.Monad.CheckedExcept Methods fail :: String -> CheckedExceptT exceptions m a # |
type CheckedExcept (es :: [Type]) a = CheckedExceptT es Identity a Source #
Pure checked exceptions.
data OneOf (es :: [Type]) where Source #
A sort of pseudo-open union that is easy to construct but difficult to
deconstruct. In lieu of singletons we opt for Typeable
to prove the type
of the existentially quantified exception e
in the set es
.
Constructors
OneOf :: forall e (es :: [Type]). (Elem e es, CheckedException e, Typeable e) => !e -> OneOf es |
Instances
Monad m => MonadError (OneOf exceptions) (CheckedExceptT exceptions m) Source # | |
Defined in Control.Monad.CheckedExcept Methods throwError :: OneOf exceptions -> CheckedExceptT exceptions m a # catchError :: CheckedExceptT exceptions m a -> (OneOf exceptions -> CheckedExceptT exceptions m a) -> CheckedExceptT exceptions m a # |
data CaseException x (es :: [Type]) where Source #
Data type used for constructing a coverage checked case-like catch
.
Constructors
CaseEndWith :: forall x. x -> CaseException x ('[] :: [Type]) | |
CaseCons :: forall e x (es1 :: [Type]). Typeable e => (e -> x) -> CaseException x es1 -> CaseException x (e ': es1) | |
CaseAny :: forall x (es :: [Type]). (forall e. CheckedException e => e -> x) -> CaseException x es |
pattern CaseEnd :: CaseException x ('[] :: [Type]) Source #
Pattern synonym for CaseEndWith (error "impossible")
.
This should never be evaluated since caseException
does not accept empty lists.
newtype ShowException a Source #
DerivingVia newtype wrapper to derive CheckedException
from a Show
instance declaration.
Useful for prototyping, but I wouldn't recommend this for serious work.
Constructors
ShowException a |
Instances
(Show a, Typeable a) => CheckedException (ShowException a) Source # | |
Defined in Control.Monad.CheckedExcept Methods encodeException :: ShowException a -> String Source # fromOneOf :: forall (es :: [Type]). OneOf es -> Maybe (ShowException a) Source # |
newtype ExceptionException a Source #
DerivingVia newtype wrapper to derive CheckedException
from Exception
.
Constructors
ExceptionException a |
Instances
(Show a, Typeable a, Exception a) => CheckedException (ExceptionException a) Source # | |
Defined in Control.Monad.CheckedExcept Methods encodeException :: ExceptionException a -> String Source # fromOneOf :: forall (es :: [Type]). OneOf es -> Maybe (ExceptionException a) Source # |
Typeclass
class Typeable e => CheckedException e where Source #
The class for checked exceptions.
Minimal complete definition
Nothing
Methods
encodeException :: e -> String Source #
Encode an exception to String
. Defaults to displayException
when available.
default encodeException :: Exception e => e -> String Source #
fromOneOf :: forall (es :: [Type]). OneOf es -> Maybe e Source #
Reify the exception. Defaults to 'withOneOf'' e cast
.
Instances
CheckedException SomeException Source # | |
Defined in Control.Monad.CheckedExcept Methods encodeException :: SomeException -> String Source # fromOneOf :: forall (es :: [Type]). OneOf es -> Maybe SomeException Source # | |
(Show a, Typeable a, Exception a) => CheckedException (ExceptionException a) Source # | |
Defined in Control.Monad.CheckedExcept Methods encodeException :: ExceptionException a -> String Source # fromOneOf :: forall (es :: [Type]). OneOf es -> Maybe (ExceptionException a) Source # | |
(Show a, Typeable a) => CheckedException (ShowException a) Source # | |
Defined in Control.Monad.CheckedExcept Methods encodeException :: ShowException a -> String Source # fromOneOf :: forall (es :: [Type]). OneOf es -> Maybe (ShowException a) Source # |
Utility functions
runCheckedExcept :: forall (es :: [Type]) a. CheckedExcept es a -> Either (OneOf es) a Source #
Get the error from CheckedExcept
.
throwCheckedException :: forall e (es :: [Type]) (m :: Type -> Type) a. (Elem e es, CheckedException e, Applicative m) => e -> CheckedExceptT es m a Source #
Throw a checked exception e
that is a member of the exception set es
.
applyAll :: forall b (es :: [Type]). (forall e. CheckedException e => e -> b) -> OneOf es -> b Source #
Apply a function f
over a checked exception, using methods from the CheckedException
typeclass.
weakenExceptions :: forall (exceptions1 :: [Type]) (exceptions2 :: [Type]) (m :: Type -> Type) a. (Functor m, Contains exceptions1 exceptions2) => CheckedExceptT exceptions1 m a -> CheckedExceptT exceptions2 m a Source #
See weakenOneOf
.
weakenOneOf :: forall (exceptions1 :: [Type]) (exceptions2 :: [Type]). Contains exceptions1 exceptions2 => OneOf exceptions1 -> OneOf exceptions2 Source #
Given a proof that exceptions1
is a subset of exceptions2
,
reconstruct the value of the
open union to be part of the larger
OneOf
exceptions1
open union. This allows us to compose OneOf
exceptions2CheckedExceptT
stacks
with differing exception sets.
withOneOf :: forall e (es :: [Type]) a. (Elem e es, Monoid a, CheckedException e) => OneOf es -> (e -> a) -> a Source #
Catch an exception or mempty
(think pure ()
or Nothing
).
withOneOf' :: forall (es :: [Type]) a. OneOf es -> (forall e. (Elem e es, CheckedException e, Typeable e) => e -> a) -> a Source #
Catch an exception, totally.
caseException :: forall (es :: [Type]) x. NonEmpty es => OneOf es -> CaseException x (Nub es) -> x Source #
Case on a checked exception with coverage checking. Note: while es
may not be a set,
the CaseException
you supply must be.
(<:) :: forall e x (es :: [Type]). Typeable e => (e -> x) -> CaseException x es -> CaseException x (e ': es) infixr 7 Source #
Infix CaseCons
with proper fixity.
catchSomeException :: forall (m :: Type -> Type) (es :: [Type]) a. (Monad m, MonadCatch m, Elem SomeException es) => CheckedExceptT es m a -> CheckedExceptT es m a Source #
Add SomeException
to the exceptions set. Preferably, call this before catching the checked
exceptions so there are no surprising exceptions.
Type families / constraints
type family Contains (as :: [k]) (bs :: [k]) where ... Source #
Constraint that the list as
is a subset of list bs
.
type family Elem (x :: t) (xs :: [t]) where ... Source #
type Elem x xs = Elem' x xs ~ 'True
Equations
Elem (x :: t) (xs :: [t]) = If (Elem' x xs) () (NotElemTypeError x xs :: Constraint) |
type family NonEmpty (xs :: [a]) where ... Source #
Type-level proof that a list is non-empty, used for constraining caseException
so that you don't
pointlessly throw
.error
Equations
NonEmpty ('[] :: [a]) = TypeError ('Text "type level list must be non-empty") :: Constraint | |
NonEmpty (_1 :: [a]) = () |
type NotElemTypeError (x :: t) (xs :: t1) = TypeError (('ShowType x ':<>: 'Text " is not a member of ") ':<>: 'ShowType xs) :: k Source #
Type error for when
fails to hold.Elem
e es'