cd6a56f5f8014eb59e6ae3437ecaad084c54a903
[ghc-hetmet.git] / ghc / lib / std / PrelErr.lhs
1 % -----------------------------------------------------------------------------
2 % $Id: PrelErr.lhs,v 1.19 2001/05/18 16:54:05 simonmar Exp $
3 %
4 % (c) The University of Glasgow, 1994-2000
5 %
6
7 \section[PrelErr]{Module @PrelErr@}
8
9 The PrelErr module defines the code for the wired-in error functions,
10 which have a special type in the compiler (with "open tyvars").
11
12 We cannot define these functions in a module where they might be used
13 (e.g., PrelBase), because the magical wired-in type will get confused
14 with what the typechecker figures out.
15
16 \begin{code}
17 {-# OPTIONS -fno-implicit-prelude #-}
18 module PrelErr 
19        (
20          irrefutPatError
21        , noMethodBindingError
22        , nonExhaustiveGuardsError
23        , patError
24        , recSelError
25        , recConError
26        , recUpdError               -- :: String -> a
27
28        , absentErr, parError       -- :: a
29        , seqError                  -- :: a
30
31        , error                     -- :: String -> a
32        , assertError               -- :: String -> Bool -> a -> a
33        
34        , undefined                 -- :: a
35        ) where
36
37 import PrelBase
38 import PrelList     ( span )
39 import PrelException
40 \end{code}
41
42 %*********************************************************
43 %*                                                      *
44 \subsection{Error-ish functions}
45 %*                                                      *
46 %*********************************************************
47
48 \begin{code}
49 -- error stops execution and displays an error message
50 error :: String -> a
51 error s = throw (ErrorCall s)
52
53 -- It is expected that compilers will recognize this and insert error
54 -- messages which are more appropriate to the context in which undefined 
55 -- appears. 
56
57 undefined :: a
58 undefined =  error "Prelude.undefined"
59 \end{code}
60
61 %*********************************************************
62 %*                                                       *
63 \subsection{Compiler generated errors + local utils}
64 %*                                                       *
65 %*********************************************************
66
67 Used for compiler-generated error message;
68 encoding saves bytes of string junk.
69
70 \begin{code}
71 absentErr, parError, seqError :: a
72
73 absentErr = error "Oops! The program has entered an `absent' argument!\n"
74 parError  = error "Oops! Entered GHCerr.parError (a GHC bug -- please report it!)\n"
75 seqError = error "Oops! Entered seqError (a GHC bug -- please report it!)\n"
76
77 \end{code}
78
79 \begin{code}
80 irrefutPatError
81    , noMethodBindingError
82    , nonExhaustiveGuardsError
83    , patError
84    , recSelError
85    , recConError
86    , recUpdError :: String -> a
87
88 noMethodBindingError     s = throw (NoMethodError (untangle s "No instance nor default method for class operation"))
89 irrefutPatError          s = throw (PatternMatchFail (untangle s "Irrefutable pattern failed for pattern"))
90 nonExhaustiveGuardsError s = throw (PatternMatchFail (untangle s "Non-exhaustive guards in"))
91 patError                 s = throw (PatternMatchFail (untangle s "Non-exhaustive patterns in"))
92 recSelError              s = throw (RecSelError (untangle s "Missing field in record selection"))
93 recConError              s = throw (RecConError (untangle s "Missing field in record construction"))
94 recUpdError              s = throw (RecUpdError (untangle s "Record doesn't contain field(s) to be updated"))
95
96
97 assertError :: String -> Bool -> a -> a
98 assertError str pred v 
99   | pred      = v
100   | otherwise = throw (AssertionFailed (untangle str "Assertion failed"))
101
102 \end{code}
103
104
105 (untangle coded message) expects "coded" to be of the form 
106
107         "location|details"
108
109 It prints
110
111         location message details
112
113 \begin{code}
114 untangle :: String -> String -> String
115 untangle coded message
116   =  location
117   ++ ": " 
118   ++ message
119   ++ details
120   ++ "\n"
121   where
122     (location, details)
123       = case (span not_bar coded) of { (loc, rest) ->
124         case rest of
125           ('|':det) -> (loc, ' ' : det)
126           _         -> (loc, "")
127         }
128     not_bar c = c /= '|'
129 \end{code}