946ca36af6b3979d6ca5841ff2114a1339dd562d
[haskell-directory.git] / GHC / Err.lhs
1 \begin{code}
2 {-# OPTIONS_GHC -fno-implicit-prelude #-}
3 -----------------------------------------------------------------------------
4 -- |
5 -- Module      :  GHC.Err
6 -- Copyright   :  (c) The University of Glasgow, 1994-2002
7 -- License     :  see libraries/base/LICENSE
8 -- 
9 -- Maintainer  :  cvs-ghc@haskell.org
10 -- Stability   :  internal
11 -- Portability :  non-portable (GHC extensions)
12 --
13 -- The "GHC.Err" module defines the code for the wired-in error functions,
14 -- which have a special type in the compiler (with \"open tyvars\").
15 -- 
16 -- We cannot define these functions in a module where they might be used
17 -- (e.g., "GHC.Base"), because the magical wired-in type will get confused
18 -- with what the typechecker figures out.
19 -- 
20 -----------------------------------------------------------------------------
21
22 -- #hide
23 module GHC.Err 
24        (
25          irrefutPatError
26        , noMethodBindingError
27        , nonExhaustiveGuardsError
28        , patError
29        , recSelError
30        , recConError
31        , runtimeError              -- :: Addr#  -> a    -- Addr# points to UTF8 encoded C string
32
33        , absentErr                 -- :: a
34        , divZeroError              -- :: a
35        , overflowError             -- :: a
36
37        , error                     -- :: String -> a
38        , assertError               -- :: String -> Bool -> a -> a
39        
40        , undefined                 -- :: a
41        ) where
42
43 #ifndef __HADDOCK__
44 import GHC.Base
45 import GHC.List     ( span )
46 import GHC.Exception
47 #endif
48 \end{code}
49
50 %*********************************************************
51 %*                                                      *
52 \subsection{Error-ish functions}
53 %*                                                      *
54 %*********************************************************
55
56 \begin{code}
57 -- | 'error' stops execution and displays an error message.
58 error :: String -> a
59 error s = throw (ErrorCall s)
60
61 -- | A special case of 'error'.
62 -- It is expected that compilers will recognize this and insert error
63 -- messages which are more appropriate to the context in which 'undefined'
64 -- appears. 
65
66 undefined :: a
67 undefined =  error "Prelude.undefined"
68 \end{code}
69
70 %*********************************************************
71 %*                                                       *
72 \subsection{Compiler generated errors + local utils}
73 %*                                                       *
74 %*********************************************************
75
76 Used for compiler-generated error message;
77 encoding saves bytes of string junk.
78
79 \begin{code}
80 absentErr :: a
81
82 absentErr = error "Oops! The program has entered an `absent' argument!\n"
83 \end{code}
84
85 \begin{code}
86 recSelError, recConError, irrefutPatError, runtimeError,
87              nonExhaustiveGuardsError, patError, noMethodBindingError
88         :: Addr# -> a   -- All take a UTF8-encoded C string
89
90 recSelError              s = throw (RecSelError (unpackCStringUtf8# s)) -- No location info unfortunately
91 runtimeError             s = error (unpackCStringUtf8# s)               -- No location info unfortunately
92
93 nonExhaustiveGuardsError s = throw (PatternMatchFail (untangle s "Non-exhaustive guards in"))
94 irrefutPatError          s = throw (PatternMatchFail (untangle s "Irrefutable pattern failed for pattern"))
95 recConError              s = throw (RecConError      (untangle s "Missing field in record construction"))
96 noMethodBindingError     s = throw (NoMethodError    (untangle s "No instance nor default method for class operation"))
97 patError                 s = throw (PatternMatchFail (untangle s "Non-exhaustive patterns in"))
98
99 assertError :: Addr# -> Bool -> a -> a
100 assertError str pred v 
101   | pred      = v
102   | otherwise = throw (AssertionFailed (untangle str "Assertion failed"))
103 \end{code}
104
105
106 (untangle coded message) expects "coded" to be of the form 
107
108         "location|details"
109
110 It prints
111
112         location message details
113
114 \begin{code}
115 untangle :: Addr# -> String -> String
116 untangle coded message
117   =  location
118   ++ ": " 
119   ++ message
120   ++ details
121   ++ "\n"
122   where
123     coded_str = unpackCStringUtf8# coded
124
125     (location, details)
126       = case (span not_bar coded_str) of { (loc, rest) ->
127         case rest of
128           ('|':det) -> (loc, ' ' : det)
129           _         -> (loc, "")
130         }
131     not_bar c = c /= '|'
132 \end{code}
133
134 Divide by zero and arithmetic overflow.
135 We put them here because they are needed relatively early
136 in the libraries before the Exception type has been defined yet.
137
138 \begin{code}
139 {-# NOINLINE divZeroError #-}
140 divZeroError :: a
141 divZeroError = throw (ArithException DivideByZero)
142
143 {-# NOINLINE overflowError #-}
144 overflowError :: a
145 overflowError = throw (ArithException Overflow)
146 \end{code}
147