Skip to content

Commit

Permalink
Add laws for MonadError
Browse files Browse the repository at this point in the history
  • Loading branch information
Lysxia committed Mar 4, 2019
1 parent 5ae7255 commit 58262b1
Showing 1 changed file with 16 additions and 22 deletions.
38 changes: 16 additions & 22 deletions Control/Monad/Error/Class.hs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ Maintainer : [email protected]
Stability : experimental
Portability : non-portable (multi-parameter type classes)
[Computation type:] Computations which may fail or throw exceptions.
[Computation type:] Computations which may throw exceptions.
[Binding strategy:] Failure records information about the cause\/location
of the failure. Failure values bypass the bound function,
Expand Down Expand Up @@ -72,30 +72,24 @@ import Control.Monad.Instances ()
import Data.Monoid
import Prelude (Either(..), Maybe(..), either, (.), IO)

{- |
The strategy of combining computations that can throw exceptions
by bypassing bound functions
from the point an exception is thrown to the point that it is handled.
Is parameterized over the type of error information and
the monad type constructor.
It is common to use @'Either' String@ as the monad type constructor
for an error monad in which error descriptions take the form of strings.
In that case and many other common cases the resulting monad is already defined
as an instance of the 'MonadError' class.
You can also define your own error type and\/or use a monad type constructor
other than @'Either' 'String'@ or @'Either' 'IOError'@.
In these cases you will have to explicitly define instances of the 'MonadError'
class.
(If you are using the deprecated "Control.Monad.Error" or
"Control.Monad.Trans.Error", you may also have to define an 'Error' instance.)
-}
-- | Monads with a notion of exceptions which can be thrown and caught.
--
-- === Laws
--
-- @
-- 'catchError' ('throwError' e) h = h e
-- 'catchError' ('catchError' m k) h = 'catchError' m (\\e -> 'catchError' (k e) h)
-- 'catchError' ('pure' a) h = 'pure' a
-- 'catchError' (m '>>=' k) h = 'tryError' m '>>=' 'either' h k
--
-- 'catchError' m 'throwError' = m
-- 'throwError' e '>>=' k = 'throwError' e
-- @
class (Monad m) => MonadError e m | m -> e where
-- | Is used within a monadic computation to begin exception processing.
-- | Throw an exception.
throwError :: e -> m a

{- |
A handler function to handle previous errors and return to normal execution.
{- | Handle an exception and return to normal execution.
A common idiom is:
> do { action1; action2; action3 } `catchError` handler
Expand Down

0 comments on commit 58262b1

Please sign in to comment.