803fdf0b518a5a6f1865227642a2451e0b2f7555
[ghc-base.git] / GHC / Err.lhs
1 \begin{code}
2 {-# OPTIONS -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 module GHC.Err 
23        (
24          irrefutPatError
25        , noMethodBindingError
26        , nonExhaustiveGuardsError
27        , patError
28        , recSelError
29        , recConError
30        , runtimeError              -- :: Addr#  -> a    -- Addr# points to UTF8 encoded C string
31
32        , absentErr                 -- :: a
33        , divZeroError              -- :: a
34
35        , error                     -- :: String -> a
36        , assertError               -- :: String -> Bool -> a -> a
37        
38        , undefined                 -- :: a
39        ) where
40
41 #ifndef __HADDOCK__
42 import GHC.Base
43 import GHC.List     ( span )
44 import GHC.Exception
45 #endif
46 \end{code}
47
48 %*********************************************************
49 %*                                                      *
50 \subsection{Error-ish functions}
51 %*                                                      *
52 %*********************************************************
53
54 \begin{code}
55 -- | 'error' stops execution and displays an error message.
56 error :: String -> a
57 error s = throw (ErrorCall s)
58
59 -- | A special case of 'error'.
60 -- It is expected that compilers will recognize this and insert error
61 -- messages which are more appropriate to the context in which 'undefined'
62 -- appears. 
63
64 undefined :: a
65 undefined =  error "Prelude.undefined"
66 \end{code}
67
68 %*********************************************************
69 %*                                                       *
70 \subsection{Compiler generated errors + local utils}
71 %*                                                       *
72 %*********************************************************
73
74 Used for compiler-generated error message;
75 encoding saves bytes of string junk.
76
77 \begin{code}
78 absentErr :: a
79
80 absentErr = error "Oops! The program has entered an `absent' argument!\n"
81 \end{code}
82
83 \begin{code}
84 recSelError, recConError, irrefutPatError, runtimeError,
85              nonExhaustiveGuardsError, patError, noMethodBindingError
86         :: Addr# -> a   -- All take a UTF8-encoded C string
87
88 recSelError              s = throw (RecSelError (unpackCStringUtf8# s)) -- No location info unfortunately
89 runtimeError             s = error (unpackCStringUtf8# s)               -- No location info unfortunately
90
91 nonExhaustiveGuardsError s = throw (PatternMatchFail (untangle s "Non-exhaustive guards in"))
92 irrefutPatError          s = throw (PatternMatchFail (untangle s "Irrefutable pattern failed for pattern"))
93 recConError              s = throw (RecConError      (untangle s "Missing field in record construction"))
94 noMethodBindingError     s = throw (NoMethodError    (untangle s "No instance nor default method for class operation"))
95 patError                 s = throw (PatternMatchFail (untangle s "Non-exhaustive patterns in"))
96
97 assertError :: Addr# -> Bool -> a -> a
98 assertError str pred v 
99   | pred      = v
100   | otherwise = throw (AssertionFailed (untangle str "Assertion failed"))
101 \end{code}
102
103
104 (untangle coded message) expects "coded" to be of the form 
105
106         "location|details"
107
108 It prints
109
110         location message details
111
112 \begin{code}
113 untangle :: Addr# -> String -> String
114 untangle coded message
115   =  location
116   ++ ": " 
117   ++ message
118   ++ details
119   ++ "\n"
120   where
121     coded_str = unpackCStringUtf8# coded
122
123     (location, details)
124       = case (span not_bar coded_str) of { (loc, rest) ->
125         case rest of
126           ('|':det) -> (loc, ' ' : det)
127           _         -> (loc, "")
128         }
129     not_bar c = c /= '|'
130 \end{code}
131
132 Divide by zero.  We put it here because it is needed relatively early
133 in the libraries before the Exception type has been defined yet.
134
135 \begin{code}
136 {-# NOINLINE divZeroError #-}
137 divZeroError :: a
138 divZeroError = throw (ArithException DivideByZero)
139 \end{code}
140