1 % ------------------------------------------------------------------------------
2 % $Id: Prelude.lhs,v 1.24 2001/02/22 16:48:24 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)
184 %*********************************************************
186 \subsection{Coercions}
188 %*********************************************************
192 "fromIntegral/Int->Int" fromIntegral = id :: Int -> Int
193 "fromIntegral/Integer->Integer" fromIntegral = id :: Integer -> Integer
194 "fromIntegral/Int->Integer" fromIntegral = int2Integer
195 "fromIntegral/Integer->Int" fromIntegral = integer2Int
196 "fromIntegral/Int->Rational" forall n . fromIntegral n = int2Integer n :% 1
197 "fromIntegral/Integer->Rational" forall n . fromIntegral n = n :% (1 :: Integer)
198 "fromIntegral/Int->Float" fromIntegral = int2Float
199 "fromIntegral/Int->Double" fromIntegral = int2Double
200 "fromIntegral/Integer->Float" forall n . fromIntegral n = encodeFloat n 0 :: Float
201 "fromIntegral/Integer->Double" forall n . fromIntegral n = encodeFloat n 0 :: Double
203 fromIntegral :: (Integral a, Num b) => a -> b
204 fromIntegral = fromInteger . toInteger
207 "realToFrac/Float->Double" realToFrac = floatToDouble
208 "realToFrac/Double->Float" realToFrac = doubleToFloat
209 "realToFrac/Float->Float" realToFrac = id :: Float -> Float
210 "realToFrac/Double->Double" realToFrac = id :: Double -> Double
211 "realToFrac/Rational->Rational" realToFrac = id :: Rational -> Rational
212 "realToFrac/Float->Rational" realToFrac = rf2rat :: Float -> Rational
213 "realToFrac/Double->Rational" realToFrac = rf2rat :: Double -> Rational
214 "realToFrac/Rational->Float" realToFrac = fromRat :: Rational -> Float
215 "realToFrac/Rational->Double" realToFrac = fromRat :: Rational -> Double
217 realToFrac :: (Real a, Fractional b) => a -> b
218 realToFrac = fromRational . toRational
220 doubleToFloat :: Double -> Float
221 doubleToFloat (D# d) = F# (double2Float# d)
223 floatToDouble :: Float -> Double
224 floatToDouble (F# f) = D# (float2Double# f)
226 {-# SPECIALIZE rf2rat ::
230 rf2rat :: RealFloat a => a -> Rational
231 rf2rat x = if n >= 0 then (m * (b ^ n)) :% 1 else m :% (b ^ (-n))
232 where (m,n) = decodeFloat x