capability-0.2.0.0: Extensional capabilities and deriving combinators

Safe HaskellNone
LanguageHaskell2010

Capability.Error

Contents

Description

Defines capabilities for actions that may fail or throw exceptions, and optionally may catch exceptions.

Monads based on IO can use the underlying IO exception mechanism to that end. The details of synchronous and asynchronous exception handling depend on the strategy used. See MonadThrow, SafeExceptions, MonadUnliftIO.

The associated tag can be used to select the exception type, or to select a layer in monad transformer stacks. Note, that it is illegal to have multiple tags refer to overlapping exception types in the same layer. Consider the following example

newtype M a = M (IO a)
  deriving (HasThrow "foo" IOException) via
    MonadUnliftIO IO
  deriving (HasThrow "bar" SomeException) via
    MonadUnliftIO IO

In this case the tags "foo" and "bar" refer to overlapping exception types in the same layer, because catch @"bar" may also catch an exception thrown under "foo".

Synopsis

Interface

class Monad m => HasThrow (tag :: k) (e :: *) (m :: * -> *) | tag m -> e where Source #

Capability to throw exceptions of type e under tag.

HasThrow/HasCatch capabilities at different tags should be independent. See HasCatch.

Methods

throw_ :: Proxy# tag -> e -> m a Source #

For technical reasons, this method needs an extra proxy argument. You only need it if you are defining new instances of HasReader. Otherwise, you will want to use throw. See throw for more documentation.

Instances
(HasThrow tag e m, MonadTrans t, Monad (t m)) => HasThrow (tag :: k) e (Lift (t m)) Source #

Lift one layer in a monad transformer stack.

Instance details

Defined in Capability.Error

Methods

throw_ :: Proxy# tag -> e -> Lift (t m) a Source #

MonadError e m => HasThrow (tag :: k) e (MonadError m) Source # 
Instance details

Defined in Capability.Error

Methods

throw_ :: Proxy# tag -> e -> MonadError m a Source #

(Exception e, MonadThrow m) => HasThrow (tag :: k) e (MonadThrow e m) Source # 
Instance details

Defined in Capability.Error

Methods

throw_ :: Proxy# tag -> e -> MonadThrow e m a Source #

(Exception e, MonadThrow m) => HasThrow (tag :: k) e (SafeExceptions e m) Source # 
Instance details

Defined in Capability.Error

Methods

throw_ :: Proxy# tag -> e -> SafeExceptions e m a Source #

(Exception e, MonadIO m) => HasThrow (tag :: k) e (MonadUnliftIO e m) Source # 
Instance details

Defined in Capability.Error

Methods

throw_ :: Proxy# tag -> e -> MonadUnliftIO e m a Source #

(Exception e, MonadThrow m) => HasThrow (tag :: k) e (MonadCatch e m) Source # 
Instance details

Defined in Capability.Error

Methods

throw_ :: Proxy# tag -> e -> MonadCatch e m a Source #

HasThrow oldtag e m => HasThrow (newtag :: k1) e (Rename oldtag m) Source #

Rename the tag.

Apply cautiously. See HasCatch newtag e (Rename oldtag m).

Instance details

Defined in Capability.Error

Methods

throw_ :: Proxy# newtag -> e -> Rename oldtag m a Source #

(forall x. Coercible (m x) (t2 (t1 m) x), Monad m, HasThrow tag e (t2 (t1 m))) => HasThrow (tag :: k) e ((t2 :.: t1) m) Source #

Compose two accessors.

Instance details

Defined in Capability.Error

Methods

throw_ :: Proxy# tag -> e -> (t2 :.: t1) m a Source #

(AsConstructor' ctor sum e, HasThrow oldtag sum m) => HasThrow (ctor :: Symbol) e (Ctor ctor oldtag m) Source #

Wrap the exception e with the constructor ctor to throw an exception of type sum.

Instance details

Defined in Capability.Error

Methods

throw_ :: Proxy# ctor -> e -> Ctor ctor oldtag m a Source #

throw :: forall tag e m a. HasThrow tag e m => e -> m a Source #

Throw an exception in the specified exception capability.

class HasThrow tag e m => HasCatch (tag :: k) (e :: *) (m :: * -> *) | tag m -> e where Source #

Capability to catch exceptions of type e under tag.

HasThrow/HasCatch capabilities at different tags should be independent. In particular, the following program should throw SomeError and not return (). > example :: > (HasThrow Left SomeError m, HasCatch Right SomeError m) > => m () > example = > catch Left > (throw Right SomeError) > _ -> pure ()

See wrapError for a way to combine multiple exception types into one.

Methods

catch_ :: Proxy# tag -> m a -> (e -> m a) -> m a Source #

For technical reasons, this method needs an extra proxy argument. You only need it if you are defining new instances of HasReader. Otherwise, you will want to use catch. See catch for more documentation.

catchJust_ :: Proxy# tag -> (e -> Maybe b) -> m a -> (b -> m a) -> m a Source #

For technical reasons, this method needs an extra proxy argument. You only need it if you are defining new instances of HasReader. Otherwise, you will want to use catchJust. See catchJust for more documentation.

Instances
(HasCatch tag e m, MonadTransControl t, Monad (t m)) => HasCatch (tag :: k) e (Lift (t m)) Source #

Lift one layer in a monad transformer stack.

Instance details

Defined in Capability.Error

Methods

catch_ :: Proxy# tag -> Lift (t m) a -> (e -> Lift (t m) a) -> Lift (t m) a Source #

catchJust_ :: Proxy# tag -> (e -> Maybe b) -> Lift (t m) a -> (b -> Lift (t m) a) -> Lift (t m) a Source #

MonadError e m => HasCatch (tag :: k) e (MonadError m) Source # 
Instance details

Defined in Capability.Error

Methods

catch_ :: Proxy# tag -> MonadError m a -> (e -> MonadError m a) -> MonadError m a Source #

catchJust_ :: Proxy# tag -> (e -> Maybe b) -> MonadError m a -> (b -> MonadError m a) -> MonadError m a Source #

(Exception e, MonadCatch m) => HasCatch (tag :: k) e (MonadCatch e m) Source # 
Instance details

Defined in Capability.Error

Methods

catch_ :: Proxy# tag -> MonadCatch e m a -> (e -> MonadCatch e m a) -> MonadCatch e m a Source #

catchJust_ :: Proxy# tag -> (e -> Maybe b) -> MonadCatch e m a -> (b -> MonadCatch e m a) -> MonadCatch e m a Source #

(Exception e, MonadCatch m) => HasCatch (tag :: k) e (SafeExceptions e m) Source # 
Instance details

Defined in Capability.Error

Methods

catch_ :: Proxy# tag -> SafeExceptions e m a -> (e -> SafeExceptions e m a) -> SafeExceptions e m a Source #

catchJust_ :: Proxy# tag -> (e -> Maybe b) -> SafeExceptions e m a -> (b -> SafeExceptions e m a) -> SafeExceptions e m a Source #

(Exception e, MonadUnliftIO m) => HasCatch (tag :: k) e (MonadUnliftIO e m) Source # 
Instance details

Defined in Capability.Error

Methods

catch_ :: Proxy# tag -> MonadUnliftIO e m a -> (e -> MonadUnliftIO e m a) -> MonadUnliftIO e m a Source #

catchJust_ :: Proxy# tag -> (e -> Maybe b) -> MonadUnliftIO e m a -> (b -> MonadUnliftIO e m a) -> MonadUnliftIO e m a Source #

HasCatch oldtag e m => HasCatch (newtag :: k1) e (Rename oldtag m) Source #

Rename the tag.

Apply cautiously. E.g. the following code produces colliding instances, where exceptions thrown in "Foo" cannot be distinguished from exceptions thrown in "Bar" and vice-versa.

newtype Bad a = Bad (IO a)
  deriving (Functor, Applicative, Monad)
  deriving
    ( HasThrow "Foo" m
    , HasCatch "Foo" m
    ) via Rename () (MonadUnliftIO SomeError IO)
  deriving
    ( HasThrow "Bar" m
    , HasCatch "Bar" m
    ) via Rename () (MonadUnliftIO SomeError IO)
Instance details

Defined in Capability.Error

Methods

catch_ :: Proxy# newtag -> Rename oldtag m a -> (e -> Rename oldtag m a) -> Rename oldtag m a Source #

catchJust_ :: Proxy# newtag -> (e -> Maybe b) -> Rename oldtag m a -> (b -> Rename oldtag m a) -> Rename oldtag m a Source #

(forall x. Coercible (m x) (t2 (t1 m) x), Monad m, HasCatch tag e (t2 (t1 m))) => HasCatch (tag :: k) e ((t2 :.: t1) m) Source #

Compose two accessors.

Instance details

Defined in Capability.Error

Methods

catch_ :: Proxy# tag -> (t2 :.: t1) m a -> (e -> (t2 :.: t1) m a) -> (t2 :.: t1) m a Source #

catchJust_ :: Proxy# tag -> (e -> Maybe b) -> (t2 :.: t1) m a -> (b -> (t2 :.: t1) m a) -> (t2 :.: t1) m a Source #

(AsConstructor' ctor sum e, HasCatch oldtag sum m) => HasCatch (ctor :: Symbol) e (Ctor ctor oldtag m) Source #

Catch an exception of type sum if its constructor matches ctor.

Instance details

Defined in Capability.Error

Methods

catch_ :: Proxy# ctor -> Ctor ctor oldtag m a -> (e -> Ctor ctor oldtag m a) -> Ctor ctor oldtag m a Source #

catchJust_ :: Proxy# ctor -> (e -> Maybe b) -> Ctor ctor oldtag m a -> (b -> Ctor ctor oldtag m a) -> Ctor ctor oldtag m a Source #

catch :: forall tag e m a. HasCatch tag e m => m a -> (e -> m a) -> m a Source #

Provide a handler for exceptions thrown in the given action in the given exception capability.

catchJust :: forall tag e m a b. HasCatch tag e m => (e -> Maybe b) -> m a -> (b -> m a) -> m a Source #

Like catch, but only handle the exception if the provided function returns Just.

wrapError :: forall outertag innertag t outer inner m a. (forall x. Coercible (t m x) (m x), forall m'. HasCatch outertag outer m' => HasCatch innertag inner (t m'), HasCatch outertag outer m) => (forall m'. HasCatch innertag inner m' => m' a) -> m a Source #

Wrap exceptions inner originating from the given action according to the accessor t.

Example:

wrapError
  @"AppError" @"ComponentError" @(Ctor "ComponentError" "AppError")
  component

component :: HasError "ComponentError" ComponentError m => m ()
data AppError = ComponentError ComponentError

This function is experimental and subject to change. See https://github.com/tweag/capability/issues/46.

Strategies

newtype MonadError m (a :: *) Source #

Derive 'HasError from m's MonadError instance.

Constructors

MonadError (m a) 
Instances
MonadError e m => HasCatch (tag :: k) e (MonadError m) Source # 
Instance details

Defined in Capability.Error

Methods

catch_ :: Proxy# tag -> MonadError m a -> (e -> MonadError m a) -> MonadError m a Source #

catchJust_ :: Proxy# tag -> (e -> Maybe b) -> MonadError m a -> (b -> MonadError m a) -> MonadError m a Source #

MonadError e m => HasThrow (tag :: k) e (MonadError m) Source # 
Instance details

Defined in Capability.Error

Methods

throw_ :: Proxy# tag -> e -> MonadError m a Source #

Monad m => Monad (MonadError m) Source # 
Instance details

Defined in Capability.Error

Methods

(>>=) :: MonadError m a -> (a -> MonadError m b) -> MonadError m b #

(>>) :: MonadError m a -> MonadError m b -> MonadError m b #

return :: a -> MonadError m a #

fail :: String -> MonadError m a #

Functor m => Functor (MonadError m) Source # 
Instance details

Defined in Capability.Error

Methods

fmap :: (a -> b) -> MonadError m a -> MonadError m b #

(<$) :: a -> MonadError m b -> MonadError m a #

Applicative m => Applicative (MonadError m) Source # 
Instance details

Defined in Capability.Error

Methods

pure :: a -> MonadError m a #

(<*>) :: MonadError m (a -> b) -> MonadError m a -> MonadError m b #

liftA2 :: (a -> b -> c) -> MonadError m a -> MonadError m b -> MonadError m c #

(*>) :: MonadError m a -> MonadError m b -> MonadError m b #

(<*) :: MonadError m a -> MonadError m b -> MonadError m a #

MonadIO m => MonadIO (MonadError m) Source # 
Instance details

Defined in Capability.Error

Methods

liftIO :: IO a -> MonadError m a #

PrimMonad m => PrimMonad (MonadError m) Source # 
Instance details

Defined in Capability.Error

Associated Types

type PrimState (MonadError m) :: Type #

Methods

primitive :: (State# (PrimState (MonadError m)) -> (#State# (PrimState (MonadError m)), a#)) -> MonadError m a #

type PrimState (MonadError m) Source # 
Instance details

Defined in Capability.Error

newtype MonadThrow (e :: *) m (a :: *) Source #

Derive HasThrow from m's MonadThrow instance.

Constructors

MonadThrow (m a) 
Instances
(Exception e, MonadThrow m) => HasThrow (tag :: k) e (MonadThrow e m) Source # 
Instance details

Defined in Capability.Error

Methods

throw_ :: Proxy# tag -> e -> MonadThrow e m a Source #

Monad m => Monad (MonadThrow e m) Source # 
Instance details

Defined in Capability.Error

Methods

(>>=) :: MonadThrow e m a -> (a -> MonadThrow e m b) -> MonadThrow e m b #

(>>) :: MonadThrow e m a -> MonadThrow e m b -> MonadThrow e m b #

return :: a -> MonadThrow e m a #

fail :: String -> MonadThrow e m a #

Functor m => Functor (MonadThrow e m) Source # 
Instance details

Defined in Capability.Error

Methods

fmap :: (a -> b) -> MonadThrow e m a -> MonadThrow e m b #

(<$) :: a -> MonadThrow e m b -> MonadThrow e m a #

Applicative m => Applicative (MonadThrow e m) Source # 
Instance details

Defined in Capability.Error

Methods

pure :: a -> MonadThrow e m a #

(<*>) :: MonadThrow e m (a -> b) -> MonadThrow e m a -> MonadThrow e m b #

liftA2 :: (a -> b -> c) -> MonadThrow e m a -> MonadThrow e m b -> MonadThrow e m c #

(*>) :: MonadThrow e m a -> MonadThrow e m b -> MonadThrow e m b #

(<*) :: MonadThrow e m a -> MonadThrow e m b -> MonadThrow e m a #

MonadIO m => MonadIO (MonadThrow e m) Source # 
Instance details

Defined in Capability.Error

Methods

liftIO :: IO a -> MonadThrow e m a #

PrimMonad m => PrimMonad (MonadThrow e m) Source # 
Instance details

Defined in Capability.Error

Associated Types

type PrimState (MonadThrow e m) :: Type #

Methods

primitive :: (State# (PrimState (MonadThrow e m)) -> (#State# (PrimState (MonadThrow e m)), a#)) -> MonadThrow e m a #

type PrimState (MonadThrow e m) Source # 
Instance details

Defined in Capability.Error

newtype MonadCatch (e :: *) m (a :: *) Source #

Derive 'HasCatch from m's 'Control.Monad.Catch.MonadCatch instance.

Constructors

MonadCatch (m a) 
Instances
(Exception e, MonadCatch m) => HasCatch (tag :: k) e (MonadCatch e m) Source # 
Instance details

Defined in Capability.Error

Methods

catch_ :: Proxy# tag -> MonadCatch e m a -> (e -> MonadCatch e m a) -> MonadCatch e m a Source #

catchJust_ :: Proxy# tag -> (e -> Maybe b) -> MonadCatch e m a -> (b -> MonadCatch e m a) -> MonadCatch e m a Source #

(Exception e, MonadThrow m) => HasThrow (tag :: k) e (MonadCatch e m) Source # 
Instance details

Defined in Capability.Error

Methods

throw_ :: Proxy# tag -> e -> MonadCatch e m a Source #

Monad m => Monad (MonadCatch e m) Source # 
Instance details

Defined in Capability.Error

Methods

(>>=) :: MonadCatch e m a -> (a -> MonadCatch e m b) -> MonadCatch e m b #

(>>) :: MonadCatch e m a -> MonadCatch e m b -> MonadCatch e m b #

return :: a -> MonadCatch e m a #

fail :: String -> MonadCatch e m a #

Functor m => Functor (MonadCatch e m) Source # 
Instance details

Defined in Capability.Error

Methods

fmap :: (a -> b) -> MonadCatch e m a -> MonadCatch e m b #

(<$) :: a -> MonadCatch e m b -> MonadCatch e m a #

Applicative m => Applicative (MonadCatch e m) Source # 
Instance details

Defined in Capability.Error

Methods

pure :: a -> MonadCatch e m a #

(<*>) :: MonadCatch e m (a -> b) -> MonadCatch e m a -> MonadCatch e m b #

liftA2 :: (a -> b -> c) -> MonadCatch e m a -> MonadCatch e m b -> MonadCatch e m c #

(*>) :: MonadCatch e m a -> MonadCatch e m b -> MonadCatch e m b #

(<*) :: MonadCatch e m a -> MonadCatch e m b -> MonadCatch e m a #

MonadIO m => MonadIO (MonadCatch e m) Source # 
Instance details

Defined in Capability.Error

Methods

liftIO :: IO a -> MonadCatch e m a #

PrimMonad m => PrimMonad (MonadCatch e m) Source # 
Instance details

Defined in Capability.Error

Associated Types

type PrimState (MonadCatch e m) :: Type #

Methods

primitive :: (State# (PrimState (MonadCatch e m)) -> (#State# (PrimState (MonadCatch e m)), a#)) -> MonadCatch e m a #

type PrimState (MonadCatch e m) Source # 
Instance details

Defined in Capability.Error

newtype SafeExceptions (e :: *) m (a :: *) Source #

Derive HasError using the functionality from the safe-exceptions package.

Constructors

SafeExceptions (m a) 
Instances
(Exception e, MonadCatch m) => HasCatch (tag :: k) e (SafeExceptions e m) Source # 
Instance details

Defined in Capability.Error

Methods

catch_ :: Proxy# tag -> SafeExceptions e m a -> (e -> SafeExceptions e m a) -> SafeExceptions e m a Source #

catchJust_ :: Proxy# tag -> (e -> Maybe b) -> SafeExceptions e m a -> (b -> SafeExceptions e m a) -> SafeExceptions e m a Source #

(Exception e, MonadThrow m) => HasThrow (tag :: k) e (SafeExceptions e m) Source # 
Instance details

Defined in Capability.Error

Methods

throw_ :: Proxy# tag -> e -> SafeExceptions e m a Source #

Monad m => Monad (SafeExceptions e m) Source # 
Instance details

Defined in Capability.Error

Methods

(>>=) :: SafeExceptions e m a -> (a -> SafeExceptions e m b) -> SafeExceptions e m b #

(>>) :: SafeExceptions e m a -> SafeExceptions e m b -> SafeExceptions e m b #

return :: a -> SafeExceptions e m a #

fail :: String -> SafeExceptions e m a #

Functor m => Functor (SafeExceptions e m) Source # 
Instance details

Defined in Capability.Error

Methods

fmap :: (a -> b) -> SafeExceptions e m a -> SafeExceptions e m b #

(<$) :: a -> SafeExceptions e m b -> SafeExceptions e m a #

Applicative m => Applicative (SafeExceptions e m) Source # 
Instance details

Defined in Capability.Error

Methods

pure :: a -> SafeExceptions e m a #

(<*>) :: SafeExceptions e m (a -> b) -> SafeExceptions e m a -> SafeExceptions e m b #

liftA2 :: (a -> b -> c) -> SafeExceptions e m a -> SafeExceptions e m b -> SafeExceptions e m c #

(*>) :: SafeExceptions e m a -> SafeExceptions e m b -> SafeExceptions e m b #

(<*) :: SafeExceptions e m a -> SafeExceptions e m b -> SafeExceptions e m a #

MonadIO m => MonadIO (SafeExceptions e m) Source # 
Instance details

Defined in Capability.Error

Methods

liftIO :: IO a -> SafeExceptions e m a #

PrimMonad m => PrimMonad (SafeExceptions e m) Source # 
Instance details

Defined in Capability.Error

Associated Types

type PrimState (SafeExceptions e m) :: Type #

Methods

primitive :: (State# (PrimState (SafeExceptions e m)) -> (#State# (PrimState (SafeExceptions e m)), a#)) -> SafeExceptions e m a #

type PrimState (SafeExceptions e m) Source # 
Instance details

Defined in Capability.Error

newtype MonadUnliftIO (e :: *) m (a :: *) Source #

Derive HasError using the functionality from the unliftio package.

Constructors

MonadUnliftIO (m a) 
Instances
(Exception e, MonadUnliftIO m) => HasCatch (tag :: k) e (MonadUnliftIO e m) Source # 
Instance details

Defined in Capability.Error

Methods

catch_ :: Proxy# tag -> MonadUnliftIO e m a -> (e -> MonadUnliftIO e m a) -> MonadUnliftIO e m a Source #

catchJust_ :: Proxy# tag -> (e -> Maybe b) -> MonadUnliftIO e m a -> (b -> MonadUnliftIO e m a) -> MonadUnliftIO e m a Source #

(Exception e, MonadIO m) => HasThrow (tag :: k) e (MonadUnliftIO e m) Source # 
Instance details

Defined in Capability.Error

Methods

throw_ :: Proxy# tag -> e -> MonadUnliftIO e m a Source #

Monad m => Monad (MonadUnliftIO e m) Source # 
Instance details

Defined in Capability.Error

Methods

(>>=) :: MonadUnliftIO e m a -> (a -> MonadUnliftIO e m b) -> MonadUnliftIO e m b #

(>>) :: MonadUnliftIO e m a -> MonadUnliftIO e m b -> MonadUnliftIO e m b #

return :: a -> MonadUnliftIO e m a #

fail :: String -> MonadUnliftIO e m a #

Functor m => Functor (MonadUnliftIO e m) Source # 
Instance details

Defined in Capability.Error

Methods

fmap :: (a -> b) -> MonadUnliftIO e m a -> MonadUnliftIO e m b #

(<$) :: a -> MonadUnliftIO e m b -> MonadUnliftIO e m a #

Applicative m => Applicative (MonadUnliftIO e m) Source # 
Instance details

Defined in Capability.Error

Methods

pure :: a -> MonadUnliftIO e m a #

(<*>) :: MonadUnliftIO e m (a -> b) -> MonadUnliftIO e m a -> MonadUnliftIO e m b #

liftA2 :: (a -> b -> c) -> MonadUnliftIO e m a -> MonadUnliftIO e m b -> MonadUnliftIO e m c #

(*>) :: MonadUnliftIO e m a -> MonadUnliftIO e m b -> MonadUnliftIO e m b #

(<*) :: MonadUnliftIO e m a -> MonadUnliftIO e m b -> MonadUnliftIO e m a #

MonadIO m => MonadIO (MonadUnliftIO e m) Source # 
Instance details

Defined in Capability.Error

Methods

liftIO :: IO a -> MonadUnliftIO e m a #

PrimMonad m => PrimMonad (MonadUnliftIO e m) Source # 
Instance details

Defined in Capability.Error

Associated Types

type PrimState (MonadUnliftIO e m) :: Type #

Methods

primitive :: (State# (PrimState (MonadUnliftIO e m)) -> (#State# (PrimState (MonadUnliftIO e m)), a#)) -> MonadUnliftIO e m a #

type PrimState (MonadUnliftIO e m) Source # 
Instance details

Defined in Capability.Error

Modifiers

Re-exported

class (Typeable e, Show e) => Exception e where #

Any type that you wish to throw or catch as an exception must be an instance of the Exception class. The simplest case is a new exception type directly below the root:

data MyException = ThisException | ThatException
    deriving Show

instance Exception MyException

The default method definitions in the Exception class do what we need in this case. You can now throw and catch ThisException and ThatException as exceptions:

*Main> throw ThisException `catch` \e -> putStrLn ("Caught " ++ show (e :: MyException))
Caught ThisException

In more complicated examples, you may wish to define a whole hierarchy of exceptions:

---------------------------------------------------------------------
-- Make the root exception type for all the exceptions in a compiler

data SomeCompilerException = forall e . Exception e => SomeCompilerException e

instance Show SomeCompilerException where
    show (SomeCompilerException e) = show e

instance Exception SomeCompilerException

compilerExceptionToException :: Exception e => e -> SomeException
compilerExceptionToException = toException . SomeCompilerException

compilerExceptionFromException :: Exception e => SomeException -> Maybe e
compilerExceptionFromException x = do
    SomeCompilerException a <- fromException x
    cast a

---------------------------------------------------------------------
-- Make a subhierarchy for exceptions in the frontend of the compiler

data SomeFrontendException = forall e . Exception e => SomeFrontendException e

instance Show SomeFrontendException where
    show (SomeFrontendException e) = show e

instance Exception SomeFrontendException where
    toException = compilerExceptionToException
    fromException = compilerExceptionFromException

frontendExceptionToException :: Exception e => e -> SomeException
frontendExceptionToException = toException . SomeFrontendException

frontendExceptionFromException :: Exception e => SomeException -> Maybe e
frontendExceptionFromException x = do
    SomeFrontendException a <- fromException x
    cast a

---------------------------------------------------------------------
-- Make an exception type for a particular frontend compiler exception

data MismatchedParentheses = MismatchedParentheses
    deriving Show

instance Exception MismatchedParentheses where
    toException   = frontendExceptionToException
    fromException = frontendExceptionFromException

We can now catch a MismatchedParentheses exception as MismatchedParentheses, SomeFrontendException or SomeCompilerException, but not other types, e.g. IOException:

*Main> throw MismatchedParentheses `catch` \e -> putStrLn ("Caught " ++ show (e :: MismatchedParentheses))
Caught MismatchedParentheses
*Main> throw MismatchedParentheses `catch` \e -> putStrLn ("Caught " ++ show (e :: SomeFrontendException))
Caught MismatchedParentheses
*Main> throw MismatchedParentheses `catch` \e -> putStrLn ("Caught " ++ show (e :: SomeCompilerException))
Caught MismatchedParentheses
*Main> throw MismatchedParentheses `catch` \e -> putStrLn ("Caught " ++ show (e :: IOException))
*** Exception: MismatchedParentheses

Minimal complete definition

Nothing

Methods

toException :: e -> SomeException #

fromException :: SomeException -> Maybe e #

displayException :: e -> String #

Render this exception value in a human-friendly manner.

Default implementation: show.

Since: base-4.8.0.0

Instances
Exception Void

Since: base-4.8.0.0

Instance details

Defined in Data.Void

Exception BlockedIndefinitelyOnMVar

Since: base-4.1.0.0

Instance details

Defined in GHC.IO.Exception

Exception BlockedIndefinitelyOnSTM

Since: base-4.1.0.0

Instance details

Defined in GHC.IO.Exception

Exception Deadlock

Since: base-4.1.0.0

Instance details

Defined in GHC.IO.Exception

Exception AllocationLimitExceeded

Since: base-4.8.0.0

Instance details

Defined in GHC.IO.Exception

Exception CompactionFailed

Since: base-4.10.0.0

Instance details

Defined in GHC.IO.Exception

Exception AssertionFailed

Since: base-4.1.0.0

Instance details

Defined in GHC.IO.Exception

Exception SomeAsyncException

Since: base-4.7.0.0

Instance details

Defined in GHC.IO.Exception

Exception AsyncException

Since: base-4.7.0.0

Instance details

Defined in GHC.IO.Exception

Exception ArrayException

Since: base-4.1.0.0

Instance details

Defined in GHC.IO.Exception

Exception FixIOException

Since: base-4.11.0.0

Instance details

Defined in GHC.IO.Exception

Exception ExitCode

Since: base-4.1.0.0

Instance details

Defined in GHC.IO.Exception

Exception IOException

Since: base-4.1.0.0

Instance details

Defined in GHC.IO.Exception

Exception ArithException

Since: base-4.0.0.0

Instance details

Defined in GHC.Exception.Type

Exception SomeException

Since: base-3.0

Instance details

Defined in GHC.Exception.Type

Exception StringException 
Instance details

Defined in Control.Exception.Safe

Exception SyncExceptionWrapper 
Instance details

Defined in Control.Exception.Safe

Exception AsyncExceptionWrapper 
Instance details

Defined in Control.Exception.Safe

Exception SyncExceptionWrapper

Since: unliftio-0.1.0.0

Instance details

Defined in UnliftIO.Exception

Exception AsyncExceptionWrapper

Since: unliftio-0.1.0.0

Instance details

Defined in UnliftIO.Exception

Exception StringException

Since: unliftio-0.1.0.0

Instance details

Defined in UnliftIO.Exception

class Typeable (a :: k) #

The class Typeable allows a concrete representation of a type to be calculated.

Minimal complete definition

typeRep#