Skip to content

Commit 5e2b736

Browse files
committed
Add laws for MonadError
1 parent 5ae7255 commit 5e2b736

File tree

1 file changed

+16
-22
lines changed

1 file changed

+16
-22
lines changed

Control/Monad/Error/Class.hs

Lines changed: 16 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ Maintainer : libraries@haskell.org
1616
Stability : experimental
1717
Portability : non-portable (multi-parameter type classes)
1818
19-
[Computation type:] Computations which may fail or throw exceptions.
19+
[Computation type:] Computations which may throw exceptions.
2020
2121
[Binding strategy:] Failure records information about the cause\/location
2222
of the failure. Failure values bypass the bound function,
@@ -72,30 +72,24 @@ import Control.Monad.Instances ()
7272
import Data.Monoid
7373
import Prelude (Either(..), Maybe(..), either, (.), IO)
7474

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

97-
{- |
98-
A handler function to handle previous errors and return to normal execution.
92+
{- | Handle an exception and return to normal execution.
9993
A common idiom is:
10094
10195
> do { action1; action2; action3 } `catchError` handler

0 commit comments

Comments
 (0)