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