Start to actually use extensible exceptions
[ghc-base.git] / Prelude.hs
1 {-# OPTIONS_GHC -XNoImplicitPrelude #-}
2 -----------------------------------------------------------------------------
3 -- |
4 -- Module      :  Prelude
5 -- Copyright   :  (c) The University of Glasgow 2001
6 -- License     :  BSD-style (see the file libraries/base/LICENSE)
7 -- 
8 -- Maintainer  :  libraries@haskell.org
9 -- Stability   :  stable
10 -- Portability :  portable
11 --
12 -- The Prelude: a standard module imported by default into all Haskell
13 -- modules.  For more documentation, see the Haskell 98 Report
14 -- <http://www.haskell.org/onlinereport/>.
15 --
16 -----------------------------------------------------------------------------
17
18 module Prelude (
19
20     -- * Standard types, classes and related functions
21
22     -- ** Basic data types
23     Bool(False, True),
24     (&&), (||), not, otherwise,
25
26     Maybe(Nothing, Just),
27     maybe,
28
29     Either(Left, Right),
30     either,
31
32     Ordering(LT, EQ, GT),
33     Char, String,
34
35     -- *** Tuples
36     fst, snd, curry, uncurry,
37
38 #if defined(__NHC__)
39     []((:), []),        -- Not legal Haskell 98;
40                         -- ... available through built-in syntax
41     module Data.Tuple,  -- Includes tuple types
42     ()(..),             -- Not legal Haskell 98
43     (->),               -- ... available through built-in syntax
44 #endif
45 #ifdef __HUGS__
46     (:),                -- Not legal Haskell 98
47 #endif
48
49     -- ** Basic type classes
50     Eq((==), (/=)),
51     Ord(compare, (<), (<=), (>=), (>), max, min),
52     Enum(succ, pred, toEnum, fromEnum, enumFrom, enumFromThen,
53          enumFromTo, enumFromThenTo),
54     Bounded(minBound, maxBound),
55
56     -- ** Numbers
57
58     -- *** Numeric types
59     Int, Integer, Float, Double,
60     Rational,
61
62     -- *** Numeric type classes
63     Num((+), (-), (*), negate, abs, signum, fromInteger),
64     Real(toRational),
65     Integral(quot, rem, div, mod, quotRem, divMod, toInteger),
66     Fractional((/), recip, fromRational),
67     Floating(pi, exp, log, sqrt, (**), logBase, sin, cos, tan,
68              asin, acos, atan, sinh, cosh, tanh, asinh, acosh, atanh),
69     RealFrac(properFraction, truncate, round, ceiling, floor),
70     RealFloat(floatRadix, floatDigits, floatRange, decodeFloat,
71               encodeFloat, exponent, significand, scaleFloat, isNaN,
72               isInfinite, isDenormalized, isIEEE, isNegativeZero, atan2),
73
74     -- *** Numeric functions
75     subtract, even, odd, gcd, lcm, (^), (^^),
76     fromIntegral, realToFrac,
77
78     -- ** Monads and functors
79     Monad((>>=), (>>), return, fail),
80     Functor(fmap),
81     mapM, mapM_, sequence, sequence_, (=<<),
82
83     -- ** Miscellaneous functions
84     id, const, (.), flip, ($), until,
85     asTypeOf, error, undefined,
86     seq, ($!),
87
88     -- * List operations
89     map, (++), filter,
90     head, last, tail, init, null, length, (!!),
91     reverse,
92     -- ** Reducing lists (folds)
93     foldl, foldl1, foldr, foldr1,
94     -- *** Special folds
95     and, or, any, all,
96     sum, product,
97     concat, concatMap,
98     maximum, minimum,
99     -- ** Building lists
100     -- *** Scans
101     scanl, scanl1, scanr, scanr1,
102     -- *** Infinite lists
103     iterate, repeat, replicate, cycle,
104     -- ** Sublists
105     take, drop, splitAt, takeWhile, dropWhile, span, break,
106     -- ** Searching lists
107     elem, notElem, lookup,
108     -- ** Zipping and unzipping lists
109     zip, zip3, zipWith, zipWith3, unzip, unzip3,
110     -- ** Functions on strings
111     lines, words, unlines, unwords,
112
113     -- * Converting to and from @String@
114     -- ** Converting to @String@
115     ShowS,
116     Show(showsPrec, showList, show),
117     shows,
118     showChar, showString, showParen,
119     -- ** Converting from @String@
120     ReadS,
121     Read(readsPrec, readList),
122     reads, readParen, read, lex,
123
124     -- * Basic Input and output
125     IO,
126     -- ** Simple I\/O operations
127     -- All I/O functions defined here are character oriented.  The
128     -- treatment of the newline character will vary on different systems.
129     -- For example, two characters of input, return and linefeed, may
130     -- read as a single newline character.  These functions cannot be
131     -- used portably for binary I/O.
132     -- *** Output functions
133     putChar,
134     putStr, putStrLn, print,
135     -- *** Input functions
136     getChar,
137     getLine, getContents, interact,
138     -- *** Files
139     FilePath,
140     readFile, writeFile, appendFile, readIO, readLn,
141     -- ** Exception handling in the I\/O monad
142     IOError, ioError, userError, catch
143
144   ) where
145
146 #ifndef __HUGS__
147 import Control.Monad
148 import System.IO
149 import Text.Read
150 import Text.Show
151 import Data.List
152 import Data.Either
153 import Data.Maybe
154 import Data.Bool
155 import Data.Tuple
156 import Data.Eq
157 import Data.Ord
158 #endif
159
160 #ifdef __GLASGOW_HASKELL__
161 import GHC.Base
162 import GHC.IOBase
163 import GHC.Exception
164 import GHC.Read
165 import GHC.Enum
166 import GHC.Num
167 import GHC.Real
168 import GHC.Float
169 import GHC.Show
170 import GHC.Err   ( error, undefined )
171 #endif
172
173 import qualified Control.OldException as Old
174
175 #ifdef __HUGS__
176 import Hugs.Prelude
177 #endif
178
179 #ifndef __HUGS__
180 infixr 0 $!
181
182 -- -----------------------------------------------------------------------------
183 -- Miscellaneous functions
184
185 -- | Strict (call-by-value) application, defined in terms of 'seq'.
186 ($!)    :: (a -> b) -> a -> b
187 f $! x  = x `seq` f x
188 #endif
189
190 #ifdef __HADDOCK__
191 -- | The value of @'seq' a b@ is bottom if @a@ is bottom, and otherwise
192 -- equal to @b@.  'seq' is usually introduced to improve performance by
193 -- avoiding unneeded laziness.
194 seq :: a -> b -> b
195 seq _ y = y
196 #endif
197
198 -- | The 'catch' function establishes a handler that receives any 'IOError'
199 -- raised in the action protected by 'catch'.  An 'IOError' is caught by
200 -- the most recent handler established by 'catch'.  These handlers are
201 -- not selective: all 'IOError's are caught.  Exception propagation
202 -- must be explicitly provided in a handler by re-raising any unwanted
203 -- exceptions.  For example, in
204 --
205 -- > f = catch g (\e -> if IO.isEOFError e then return [] else ioError e)
206 --
207 -- the function @f@ returns @[]@ when an end-of-file exception
208 -- (cf. 'System.IO.Error.isEOFError') occurs in @g@; otherwise, the
209 -- exception is propagated to the next outer handler.
210 --
211 -- When an exception propagates outside the main program, the Haskell
212 -- system prints the associated 'IOError' value and exits the program.
213 --
214 -- Non-I\/O exceptions are not caught by this variant; to catch all
215 -- exceptions, use 'Control.Exception.catch' from "Control.Exception".
216 catch :: IO a -> (IOError -> IO a) -> IO a
217 catch io handler = io `Old.catch` handler'
218     where handler' (Old.IOException ioe) = handler ioe
219           handler' e                     = throw e
220