1 We add the option -fno-implicit-prelude here to tell the reader that
2 special names such as () and -> shouldn't be resolved to Prelude.()
3 and Prelude.-> (as they are normally). -- SDM 8/10/97
6 {-# OPTIONS -fno-implicit-prelude #-}
10 -- Everything corresponding to the Report's PreludeList
12 lines, words, unlines, unwords,
15 -- Everything corresponding to the Report's PreludeText
17 Read(readsPrec, readList),
18 Show(showsPrec, showList, show),
19 reads, shows, read, lex,
20 showChar, showString, readParen, showParen,
22 -- Everything corresponding to the Report's PreludeIO
24 ioError, userError, catch,
25 putChar, putStr, putStrLn, print,
26 getChar, getLine, getContents, interact,
27 readFile, writeFile, appendFile, readIO, readLn,
33 Char, String, Int, Integer, Float, Double, IO,
38 -- Includes tuple types + fst, snd, curry, uncurry
39 ()(..), -- The unit type
46 Num((+), (-), (*), negate, abs, signum, fromInteger),
47 -- The fromInt method is exposed only by GlaExts
49 Integral(quot, rem, div, mod, quotRem, divMod, toInteger),
50 -- The toInt method is exposed only by GlaExts
56 -- Monad stuff, from PrelBase, and defined here
59 mapM, mapM_, sequence, sequence_, (=<<),
62 (&&), (||), not, otherwise,
63 subtract, even, odd, gcd, lcm, (^), (^^),
64 fromIntegral, realToFrac,
65 --exported by PrelTup: fst, snd, curry, uncurry,
66 id, const, (.), flip, ($), until,
67 asTypeOf, error, undefined,
74 #ifndef USE_REPORT_PRELUDE
75 hiding ( takeUInt_append )
88 import PrelErr ( error )
95 %*********************************************************
97 \subsection{Miscellaneous functions}
99 %*********************************************************
102 ($!) :: (a -> b) -> a -> b
105 -- It is expected that compilers will recognize this and insert error
106 -- messages which are more appropriate to the context in which undefined
110 undefined = error "Prelude.undefined"
114 %*********************************************************
116 \subsection{List sum and product}
118 %*********************************************************
120 List sum and product are defined here because PrelList is too far
121 down the compilation chain to "see" the Num class.
124 -- sum and product compute the sum or product of a finite list of numbers.
125 {-# SPECIALISE sum :: [Int] -> Int #-}
126 {-# SPECIALISE sum :: [Integer] -> Integer #-}
127 {-# SPECIALISE product :: [Int] -> Int #-}
128 {-# SPECIALISE product :: [Integer] -> Integer #-}
129 sum, product :: (Num a) => [a] -> a
130 #ifdef USE_REPORT_PRELUDE
132 product = foldl (*) 1
137 sum' (x:xs) a = sum' xs (a+x)
141 prod (x:xs) a = prod xs (a*x)
146 %*********************************************************
148 \subsection{Prelude monad functions}
150 %*********************************************************
153 {-# SPECIALISE (=<<) :: (a -> [b]) -> [a] -> [b] #-}
154 (=<<) :: Monad m => (a -> m b) -> m a -> m b
157 sequence :: Monad m => [m a] -> m [a]
158 {-# INLINE sequence #-}
159 sequence ms = foldr k (return []) ms
161 k m m' = do { x <- m; xs <- m'; return (x:xs) }
163 sequence_ :: Monad m => [m a] -> m ()
164 {-# INLINE sequence_ #-}
165 sequence_ ms = foldr (>>) (return ()) ms
167 mapM :: Monad m => (a -> m b) -> [a] -> m [b]
169 mapM f as = sequence (map f as)
171 mapM_ :: Monad m => (a -> m b) -> [a] -> m ()
173 mapM_ f as = sequence_ (map f as)
177 %*********************************************************
179 \subsection{Coercions}
181 %*********************************************************
184 {-# SPECIALIZE fromIntegral ::
194 Integer -> Double #-}
195 fromIntegral :: (Integral a, Num b) => a -> b
196 fromIntegral = fromInteger . toInteger
198 {-# SPECIALIZE realToFrac ::
203 Rational -> Rational,
208 realToFrac :: (Real a, Fractional b) => a -> b
209 realToFrac = fromRational . toRational
213 %*********************************************************
215 \subsection{Standard IO}
217 %*********************************************************
219 The Prelude has from Day 1 provided a collection of common
220 IO functions. We define these here, but let the Prelude
224 putChar :: Char -> IO ()
225 putChar c = hPutChar stdout c
227 putStr :: String -> IO ()
228 putStr s = hPutStr stdout s
230 putStrLn :: String -> IO ()
231 putStrLn s = do putStr s
234 print :: Show a => a -> IO ()
235 print x = putStrLn (show x)
238 getChar = hGetChar stdin
241 getLine = hGetLine stdin
243 getContents :: IO String
244 getContents = hGetContents stdin
246 interact :: (String -> String) -> IO ()
247 interact f = do s <- getContents
250 readFile :: FilePath -> IO String
251 readFile name = openFile name ReadMode >>= hGetContents
253 writeFile :: FilePath -> String -> IO ()
254 writeFile name str = do
255 hdl <- openFile name WriteMode
259 appendFile :: FilePath -> String -> IO ()
260 appendFile name str = do
261 hdl <- openFile name AppendMode
265 readLn :: Read a => IO a
266 readLn = do l <- getLine