checked-exceptions-0.2.0.0: mtl-style checked exceptions
Safe HaskellNone
LanguageHaskell2010

Control.Monad.CheckedExcept

Description

Basic API of CheckedExceptT

Synopsis

Types

newtype CheckedExceptT (exceptions :: [Type]) (m :: Type -> Type) a Source #

Isomorphic to ExceptT over our open-union exceptions type OneOf es. Because many effects systems have an ExceptT analogue, this would be pretty simple to port to any effects system. See Control.Monad.CheckedExcept.QualifiedDo for example usages.

Constructors

CheckedExceptT 

Fields

Instances

Instances details
MonadTrans (CheckedExceptT exceptions) Source # 
Instance details

Defined in Control.Monad.CheckedExcept

Methods

lift :: Monad m => m a -> CheckedExceptT exceptions m a #

Monad m => MonadError (OneOf exceptions) (CheckedExceptT exceptions m) Source # 
Instance details

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 # 
Instance details

Defined in Control.Monad.CheckedExcept

Methods

liftIO :: IO a -> CheckedExceptT exceptions m a #

Monad m => Applicative (CheckedExceptT exceptions m) Source # 
Instance details

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 # 
Instance details

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 # 
Instance details

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 # 
Instance details

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

Instances details
Monad m => MonadError (OneOf exceptions) (CheckedExceptT exceptions m) Source # 
Instance details

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

Instances details
(Show a, Typeable a) => CheckedException (ShowException a) Source # 
Instance details

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

Instances details
(Show a, Typeable a, Exception a) => CheckedException (ExceptionException a) Source # 
Instance details

Defined in Control.Monad.CheckedExcept

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.

default fromOneOf :: forall (es :: [Type]). Typeable e => OneOf es -> Maybe e Source #

Instances

Instances details
CheckedException SomeException Source # 
Instance details

Defined in Control.Monad.CheckedExcept

(Show a, Typeable a, Exception a) => CheckedException (ExceptionException a) Source # 
Instance details

Defined in Control.Monad.CheckedExcept

(Show a, Typeable a) => CheckedException (ShowException a) Source # 
Instance details

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 #

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 OneOf exceptions1 open union to be part of the larger OneOf exceptions2 open union. This allows us to compose CheckedExceptT 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.

Equations

Contains ('[] :: [k]) (_1 :: [k]) = () 
Contains (as :: [k]) (as :: [k]) = () 
Contains (a ': as :: [k]) (bs :: [k]) = (Elem' a bs ~ 'True, Contains as 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 Elem' (x :: a) (xs :: [a]) :: Bool where ... Source #

Is x present in the list xs?

Equations

Elem' (x :: a) ('[] :: [a]) = 'False 
Elem' (x :: a) (x ': xs :: [a]) = 'True 
Elem' (x :: a) (y ': xs :: [a]) = Elem' x xs 

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 Elem e es' fails to hold.

type family Nub (xs :: [a]) :: [a] where ... Source #

Remove duplicates from a type-level list.

Equations

Nub ('[] :: [a]) = '[] :: [a] 
Nub (x ': xs :: [a]) = x ': Nub (Remove x xs) 

type family Remove (x :: a) (xs :: [a]) :: [a] where ... Source #

Remove element from a type-level list.

Equations

Remove (x :: a) ('[] :: [a]) = '[] :: [a] 
Remove (x :: a) (x ': ys :: [a]) = Remove x ys 
Remove (x :: a) (y ': ys :: [a]) = y ': Remove x ys 

type family (xs :: [k]) ++ (ys :: [k]) :: [k] where ... infixr 5 Source #

Type-level list concatenation.

Equations

('[] :: [k]) ++ (ys :: [k]) = ys 
(x ': xs :: [k]) ++ (ys :: [k]) = x ': (xs ++ ys)