1 % ------------------------------------------------------------------------------
2 % $Id: Prelude.lhs,v 1.22 2000/06/30 13:39:36 simonmar 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),
55 -- The fromInt method is exposed only by GlaExts
57 Integral(quot, rem, div, mod, quotRem, divMod, toInteger),
58 -- The toInt method is exposed only by GlaExts
64 -- Monad stuff, from PrelBase, and defined here
67 mapM, mapM_, sequence, sequence_, (=<<),
70 (&&), (||), not, otherwise,
71 subtract, even, odd, gcd, lcm, (^), (^^),
72 fromIntegral, realToFrac,
73 --exported by PrelTup: fst, snd, curry, uncurry,
74 id, const, (.), flip, ($), until,
75 asTypeOf, error, undefined,
82 #ifndef USE_REPORT_PRELUDE
83 hiding ( takeUInt_append )
97 import PrelErr ( error )
104 %*********************************************************
106 \subsection{Miscellaneous functions}
108 %*********************************************************
111 ($!) :: (a -> b) -> a -> b
114 -- It is expected that compilers will recognize this and insert error
115 -- messages which are more appropriate to the context in which undefined
119 undefined = error "Prelude.undefined"
123 %*********************************************************
125 \subsection{List sum and product}
127 %*********************************************************
129 List sum and product are defined here because PrelList is too far
130 down the compilation chain to "see" the Num class.
133 -- sum and product compute the sum or product of a finite list of numbers.
134 {-# SPECIALISE sum :: [Int] -> Int #-}
135 {-# SPECIALISE sum :: [Integer] -> Integer #-}
136 {-# SPECIALISE product :: [Int] -> Int #-}
137 {-# SPECIALISE product :: [Integer] -> Integer #-}
138 sum, product :: (Num a) => [a] -> a
139 #ifdef USE_REPORT_PRELUDE
141 product = foldl (*) 1
146 sum' (x:xs) a = sum' xs (a+x)
150 prod (x:xs) a = prod xs (a*x)
155 %*********************************************************
157 \subsection{Prelude monad functions}
159 %*********************************************************
162 {-# SPECIALISE (=<<) :: (a -> [b]) -> [a] -> [b] #-}
163 (=<<) :: Monad m => (a -> m b) -> m a -> m b
166 sequence :: Monad m => [m a] -> m [a]
167 {-# INLINE sequence #-}
168 sequence ms = foldr k (return []) ms
170 k m m' = do { x <- m; xs <- m'; return (x:xs) }
172 sequence_ :: Monad m => [m a] -> m ()
173 {-# INLINE sequence_ #-}
174 sequence_ ms = foldr (>>) (return ()) ms
176 mapM :: Monad m => (a -> m b) -> [a] -> m [b]
178 mapM f as = sequence (map f as)
180 mapM_ :: Monad m => (a -> m b) -> [a] -> m ()
182 mapM_ f as = sequence_ (map f as)
186 %*********************************************************
188 \subsection{Coercions}
190 %*********************************************************
194 "fromIntegral/Int->Int" fromIntegral = id :: Int -> Int
195 "fromIntegral/Integer->Integer" fromIntegral = id :: Integer -> Integer
196 "fromIntegral/Int->Integer" fromIntegral = int2Integer
197 "fromIntegral/Integer->Int" fromIntegral = integer2Int
198 "fromIntegral/Int->Rational" forall n . fromIntegral n = int2Integer n :% 1
199 "fromIntegral/Integer->Rational" forall n . fromIntegral n = n :% (1 :: Integer)
200 "fromIntegral/Int->Float" fromIntegral = int2Float
201 "fromIntegral/Int->Double" fromIntegral = int2Double
202 "fromIntegral/Integer->Float" forall n . fromIntegral n = encodeFloat n 0 :: Float
203 "fromIntegral/Integer->Double" forall n . fromIntegral n = encodeFloat n 0 :: Double
205 fromIntegral :: (Integral a, Num b) => a -> b
206 fromIntegral = fromInteger . toInteger
209 "realToFrac/Float->Double" realToFrac = floatToDouble
210 "realToFrac/Double->Float" realToFrac = doubleToFloat
211 "realToFrac/Float->Float" realToFrac = id :: Float -> Float
212 "realToFrac/Double->Double" realToFrac = id :: Double -> Double
213 "realToFrac/Rational->Rational" realToFrac = id :: Rational -> Rational
214 "realToFrac/Float->Rational" realToFrac = rf2rat :: Float -> Rational
215 "realToFrac/Double->Rational" realToFrac = rf2rat :: Double -> Rational
216 "realToFrac/Rational->Float" realToFrac = fromRat :: Rational -> Float
217 "realToFrac/Rational->Double" realToFrac = fromRat :: Rational -> Double
219 realToFrac :: (Real a, Fractional b) => a -> b
220 realToFrac = fromRational . toRational
222 doubleToFloat :: Double -> Float
223 doubleToFloat (D# d) = F# (double2Float# d)
225 floatToDouble :: Float -> Double
226 floatToDouble (F# f) = D# (float2Double# f)
228 {-# SPECIALIZE rf2rat ::
232 rf2rat :: RealFloat a => a -> Rational
233 rf2rat x = if n >= 0 then (m * (b ^ n)) :% 1 else m :% (b ^ (-n))
234 where (m,n) = decodeFloat x