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