1 % ------------------------------------------------------------------------------
2 % $Id: Prelude.lhs,v 1.25 2001/02/28 00:01:03 qrczak Exp $
4 % (c) The University of Glasgow, 1992-2000
7 \section[Prelude]{Module @Prelude@}
9 We add the option -fno-implicit-prelude here to tell the reader that
10 special names such as () and -> shouldn't be resolved to Prelude.()
11 and Prelude.-> (as they are normally). -- SDM 8/10/97
14 {-# OPTIONS -fno-implicit-prelude #-}
18 -- Everything corresponding to the Report's PreludeList
20 lines, words, unlines, unwords,
23 -- Everything corresponding to the Report's PreludeText
25 Read(readsPrec, readList),
26 Show(showsPrec, showList, show),
27 reads, shows, read, lex,
28 showChar, showString, readParen, showParen,
30 -- Everything corresponding to the Report's PreludeIO
32 ioError, userError, catch,
33 putChar, putStr, putStrLn, print,
34 getChar, getLine, getContents, interact,
35 readFile, writeFile, appendFile, readIO, readLn,
41 Char, String, Int, Integer, Float, Double, IO,
46 -- Includes tuple types + fst, snd, curry, uncurry
47 ()(..), -- The unit type
62 -- Monad stuff, from PrelBase, and defined here
65 mapM, mapM_, sequence, sequence_, (=<<),
68 (&&), (||), not, otherwise,
69 subtract, even, odd, gcd, lcm, (^), (^^),
70 fromIntegral, realToFrac,
71 --exported by PrelTup: fst, snd, curry, uncurry,
72 id, const, (.), flip, ($), until,
73 asTypeOf, error, undefined,
80 #ifndef USE_REPORT_PRELUDE
81 hiding ( takeUInt_append )
95 import PrelErr ( error )
102 %*********************************************************
104 \subsection{Miscellaneous functions}
106 %*********************************************************
109 ($!) :: (a -> b) -> a -> b
112 -- It is expected that compilers will recognize this and insert error
113 -- messages which are more appropriate to the context in which undefined
117 undefined = error "Prelude.undefined"
121 %*********************************************************
123 \subsection{List sum and product}
125 %*********************************************************
127 List sum and product are defined here because PrelList is too far
128 down the compilation chain to "see" the Num class.
131 -- sum and product compute the sum or product of a finite list of numbers.
132 {-# SPECIALISE sum :: [Int] -> Int #-}
133 {-# SPECIALISE sum :: [Integer] -> Integer #-}
134 {-# SPECIALISE product :: [Int] -> Int #-}
135 {-# SPECIALISE product :: [Integer] -> Integer #-}
136 sum, product :: (Num a) => [a] -> a
137 #ifdef USE_REPORT_PRELUDE
139 product = foldl (*) 1
144 sum' (x:xs) a = sum' xs (a+x)
148 prod (x:xs) a = prod xs (a*x)
153 %*********************************************************
155 \subsection{Prelude monad functions}
157 %*********************************************************
160 {-# SPECIALISE (=<<) :: (a -> [b]) -> [a] -> [b] #-}
161 (=<<) :: Monad m => (a -> m b) -> m a -> m b
164 sequence :: Monad m => [m a] -> m [a]
165 {-# INLINE sequence #-}
166 sequence ms = foldr k (return []) ms
168 k m m' = do { x <- m; xs <- m'; return (x:xs) }
170 sequence_ :: Monad m => [m a] -> m ()
171 {-# INLINE sequence_ #-}
172 sequence_ ms = foldr (>>) (return ()) ms
174 mapM :: Monad m => (a -> m b) -> [a] -> m [b]
176 mapM f as = sequence (map f as)
178 mapM_ :: Monad m => (a -> m b) -> [a] -> m ()
180 mapM_ f as = sequence_ (map f as)