[project @ 1999-04-27 17:41:17 by sof]
[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
38 \end{code}
39
40 %*********************************************************
41 %*                                                      *
42 \subsection{Error-ish functions}
43 %*                                                      *
44 %*********************************************************
45
46 \begin{code}
47 -- error stops execution and displays an error message
48 error :: String -> a
49 error s = throw (ErrorCall s)
50 \end{code}
51
52 %*********************************************************
53 %*                                                       *
54 \subsection{Compiler generated errors + local utils}
55 %*                                                       *
56 %*********************************************************
57
58 Used for compiler-generated error message;
59 encoding saves bytes of string junk.
60
61 \begin{code}
62 absentErr, parError, seqError :: a
63
64 absentErr = error "Oops! The program has entered an `absent' argument!\n"
65 parError  = error "Oops! Entered GHCerr.parError (a GHC bug -- please report it!)\n"
66 seqError = error "Oops! Entered seqError (a GHC bug -- please report it!)\n"
67
68 \end{code}
69
70 \begin{code}
71 irrefutPatError
72    , noMethodBindingError
73    , nonExhaustiveGuardsError
74    , patError
75    , recSelError
76    , recConError
77    , recUpdError :: String -> a
78
79 noMethodBindingError     s = throw (NoMethodError (untangle s "No instance nor default method for class operation"))
80 irrefutPatError          s = throw (PatternMatchFail (untangle s "Irrefutable pattern failed for pattern"))
81 nonExhaustiveGuardsError s = throw (NonExhaustiveGuards (untangle s "Non-exhaustive guards in"))
82 patError                 s = throw (PatternMatchFail (untangle s "Non-exhaustive patterns in"))
83 recSelError              s = throw (RecSelError (untangle s "Missing field in record selection"))
84 recConError              s = throw (RecConError (untangle s "Missing field in record construction"))
85 recUpdError              s = throw (RecUpdError (untangle s "Record doesn't contain field(s) to be updated"))
86
87
88 assertError :: String -> Bool -> a -> a
89 assertError str pred v 
90   | pred      = v
91   | otherwise = throw (AssertionFailed (untangle str "Assertion failed"))
92
93 \end{code}
94
95
96 (untangle coded message) expects "coded" to be of the form 
97
98         "location|details"
99
100 It prints
101
102         location message details
103
104 \begin{code}
105 untangle :: String -> String -> String
106 untangle coded message
107   =  location
108   ++ ": " 
109   ++ message
110   ++ details
111   ++ "\n"
112   where
113     (location, details)
114       = case (span not_bar coded) of { (loc, rest) ->
115         case rest of
116           ('|':det) -> (loc, ' ' : det)
117           _         -> (loc, "")
118         }
119     not_bar c = c /= '|'
120 \end{code}