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