3a_asm: ghc flags
[calu.git] / 3a_asm / Control / Monad / Error / Class.hs
1 {-# LANGUAGE UndecidableInstances, MultiParamTypeClasses, FunctionalDependencies, TypeSynonymInstances #-}
2 -- Needed for the same reasons as in Reader, State etc
3
4 {- |
5 Module      :  Control.Monad.Error.Class
6 Copyright   :  (c) Michael Weber <michael.weber@post.rwth-aachen.de> 2001,
7                (c) Jeff Newbern 2003-2006,
8                (c) Andriy Palamarchuk 2006
9 License     :  BSD-style (see the file libraries/base/LICENSE)
10
11 Maintainer  :  libraries@haskell.org
12 Stability   :  experimental
13 Portability :  non-portable (multi-parameter type classes)
14
15 [Computation type:] Computations which may fail or throw exceptions.
16
17 [Binding strategy:] Failure records information about the cause\/location
18 of the failure. Failure values bypass the bound function,
19 other values are used as inputs to the bound function.
20
21 [Useful for:] Building computations from sequences of functions that may fail
22 or using exception handling to structure error handling.
23
24 [Zero and plus:] Zero is represented by an empty error and the plus operation
25 executes its second argument if the first fails.
26
27 [Example type:] @'Data.Either' String a@
28
29 The Error monad (also called the Exception monad).
30 -}
31
32 {-
33   Rendered by Michael Weber <mailto:michael.weber@post.rwth-aachen.de>,
34   inspired by the Haskell Monad Template Library from
35     Andy Gill (<http://www.cse.ogi.edu/~andy/>)
36 -}
37 module Control.Monad.Error.Class (
38     Error(..),
39     MonadError(..),
40   ) where
41
42 -- | An exception to be thrown.
43 -- An instance must redefine at least one of 'noMsg', 'strMsg'.
44 class Error a where
45     -- | Creates an exception without a message.
46     -- Default implementation is @'strMsg' \"\"@.
47     noMsg  :: a
48     -- | Creates an exception with a message.
49     -- Default implementation is 'noMsg'.
50     strMsg :: String -> a
51
52     noMsg    = strMsg ""
53     strMsg _ = noMsg
54
55 -- | A string can be thrown as an error.
56 instance Error String where
57     noMsg  = ""
58     strMsg = id
59
60 instance Error IOError where
61     strMsg = userError
62
63 {- |
64 The strategy of combining computations that can throw exceptions
65 by bypassing bound functions
66 from the point an exception is thrown to the point that it is handled.
67
68 Is parameterized over the type of error information and
69 the monad type constructor.
70 It is common to use @'Data.Either' String@ as the monad type constructor
71 for an error monad in which error descriptions take the form of strings.
72 In that case and many other common cases the resulting monad is already defined
73 as an instance of the 'MonadError' class.
74 You can also define your own error type and\/or use a monad type constructor
75 other than @'Data.Either' String@ or @'Data.Either' IOError@.
76 In these cases you will have to explicitly define instances of the 'Error'
77 and\/or 'MonadError' classes.
78 -}
79 class (Monad m) => MonadError e m | m -> e where
80     -- | Is used within a monadic computation to begin exception processing.
81     throwError :: e -> m a
82
83     {- |
84     A handler function to handle previous errors and return to normal execution.
85     A common idiom is:
86
87     > do { action1; action2; action3 } `catchError` handler
88
89     where the @action@ functions can call 'throwError'.
90     Note that @handler@ and the do-block must have the same return type.
91     -}
92     catchError :: m a -> (e -> m a) -> m a
93