1 % ------------------------------------------------------------------------------
2 % $Id: Prelude.lhs,v 1.23 2001/02/22 13:17:59 simonpj 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
54 Num((+), (-), (*), negate, abs, signum, fromInteger),
56 Integral(quot, rem, div, mod, quotRem, divMod, toInteger),
57 -- The toInt method is exposed only by GlaExts
63 -- Monad stuff, from PrelBase, and defined here
66 mapM, mapM_, sequence, sequence_, (=<<),
69 (&&), (||), not, otherwise,
70 subtract, even, odd, gcd, lcm, (^), (^^),
71 fromIntegral, realToFrac,
72 --exported by PrelTup: fst, snd, curry, uncurry,
73 id, const, (.), flip, ($), until,
74 asTypeOf, error, undefined,
81 #ifndef USE_REPORT_PRELUDE
82 hiding ( takeUInt_append )
96 import PrelErr ( error )
103 %*********************************************************
105 \subsection{Miscellaneous functions}
107 %*********************************************************
110 ($!) :: (a -> b) -> a -> b
113 -- It is expected that compilers will recognize this and insert error
114 -- messages which are more appropriate to the context in which undefined
118 undefined = error "Prelude.undefined"
122 %*********************************************************
124 \subsection{List sum and product}
126 %*********************************************************
128 List sum and product are defined here because PrelList is too far
129 down the compilation chain to "see" the Num class.
132 -- sum and product compute the sum or product of a finite list of numbers.
133 {-# SPECIALISE sum :: [Int] -> Int #-}
134 {-# SPECIALISE sum :: [Integer] -> Integer #-}
135 {-# SPECIALISE product :: [Int] -> Int #-}
136 {-# SPECIALISE product :: [Integer] -> Integer #-}
137 sum, product :: (Num a) => [a] -> a
138 #ifdef USE_REPORT_PRELUDE
140 product = foldl (*) 1
145 sum' (x:xs) a = sum' xs (a+x)
149 prod (x:xs) a = prod xs (a*x)
154 %*********************************************************
156 \subsection{Prelude monad functions}
158 %*********************************************************
161 {-# SPECIALISE (=<<) :: (a -> [b]) -> [a] -> [b] #-}
162 (=<<) :: Monad m => (a -> m b) -> m a -> m b
165 sequence :: Monad m => [m a] -> m [a]
166 {-# INLINE sequence #-}
167 sequence ms = foldr k (return []) ms
169 k m m' = do { x <- m; xs <- m'; return (x:xs) }
171 sequence_ :: Monad m => [m a] -> m ()
172 {-# INLINE sequence_ #-}
173 sequence_ ms = foldr (>>) (return ()) ms
175 mapM :: Monad m => (a -> m b) -> [a] -> m [b]
177 mapM f as = sequence (map f as)
179 mapM_ :: Monad m => (a -> m b) -> [a] -> m ()
181 mapM_ f as = sequence_ (map f as)
185 %*********************************************************
187 \subsection{Coercions}
189 %*********************************************************
193 "fromIntegral/Int->Int" fromIntegral = id :: Int -> Int
194 "fromIntegral/Integer->Integer" fromIntegral = id :: Integer -> Integer
195 "fromIntegral/Int->Integer" fromIntegral = int2Integer
196 "fromIntegral/Integer->Int" fromIntegral = integer2Int
197 "fromIntegral/Int->Rational" forall n . fromIntegral n = int2Integer n :% 1
198 "fromIntegral/Integer->Rational" forall n . fromIntegral n = n :% (1 :: Integer)
199 "fromIntegral/Int->Float" fromIntegral = int2Float
200 "fromIntegral/Int->Double" fromIntegral = int2Double
201 "fromIntegral/Integer->Float" forall n . fromIntegral n = encodeFloat n 0 :: Float
202 "fromIntegral/Integer->Double" forall n . fromIntegral n = encodeFloat n 0 :: Double
204 fromIntegral :: (Integral a, Num b) => a -> b
205 fromIntegral = fromInteger . toInteger
208 "realToFrac/Float->Double" realToFrac = floatToDouble
209 "realToFrac/Double->Float" realToFrac = doubleToFloat
210 "realToFrac/Float->Float" realToFrac = id :: Float -> Float
211 "realToFrac/Double->Double" realToFrac = id :: Double -> Double
212 "realToFrac/Rational->Rational" realToFrac = id :: Rational -> Rational
213 "realToFrac/Float->Rational" realToFrac = rf2rat :: Float -> Rational
214 "realToFrac/Double->Rational" realToFrac = rf2rat :: Double -> Rational
215 "realToFrac/Rational->Float" realToFrac = fromRat :: Rational -> Float
216 "realToFrac/Rational->Double" realToFrac = fromRat :: Rational -> Double
218 realToFrac :: (Real a, Fractional b) => a -> b
219 realToFrac = fromRational . toRational
221 doubleToFloat :: Double -> Float
222 doubleToFloat (D# d) = F# (double2Float# d)
224 floatToDouble :: Float -> Double
225 floatToDouble (F# f) = D# (float2Double# f)
227 {-# SPECIALIZE rf2rat ::
231 rf2rat :: RealFloat a => a -> Rational
232 rf2rat x = if n >= 0 then (m * (b ^ n)) :% 1 else m :% (b ^ (-n))
233 where (m,n) = decodeFloat x