1 -- Standard value bindings
4 -- NB: not the "real" Prelude.hi export list
47 #if defined(__UNBOXED_INSTANCES__)
51 , isAscii#, isControl#, isPrint#, isSpace#
52 , isUpper#, isLower#, isAlpha#, isDigit#, isAlphanum#
57 import UTypes ( Bin ) -- so we don't get any data constructors!
71 import PS ( _PackedString, _unpackPS )
80 ---------------------------------------------------------------
82 ---------------------------------------------------------------
85 isNullBin :: Bin -> Bool
86 appendBin :: Bin -> Bin -> Bin
89 nullBin = error "nullBin{Prelude}\n"
90 isNullBin = error "isNullBin{Prelude}\n"
91 appendBin = error "appendBin{Prelude}\n"
93 ---------------------------------------------------------------
95 ---------------------------------------------------------------
99 (&&), (||) :: Bool -> Bool -> Bool
110 {-# INLINE otherwise #-}
114 ---------------------------------------------------------------
116 ---------------------------------------------------------------
118 minInt, maxInt :: Int
119 minInt = -2147483647 -- **********************
120 maxInt = 2147483647 -- **********************
122 ---------------------------------------------------------------
124 ---------------------------------------------------------------
126 minChar, maxChar :: Char
131 ord c = case c of { C# ch -> I# (ord# ch) }
134 chr i = case i of { I# ih -> C# (chr# ih) }
136 isAscii, isControl, isPrint, isSpace :: Char -> Bool
137 isUpper, isLower, isAlpha, isDigit, isAlphanum :: Char -> Bool
139 isAscii c = ord c < 128
140 isControl c = c < ' ' || c == '\DEL'
141 isPrint c = c >= ' ' && c <= '~'
142 isSpace c = c == ' ' || c == '\t' || c == '\n' ||
143 c == '\r' || c == '\f' || c == '\v'
144 isUpper c = c >= 'A' && c <= 'Z'
145 isLower c = c >= 'a' && c <= 'z'
146 isAlpha c = isUpper c || isLower c
147 isDigit c = c >= '0' && c <= '9'
148 isAlphanum c = isAlpha c || isDigit c
151 toUpper, toLower :: Char -> Char
152 toUpper c | isLower c = chr ((ord c - ord 'a') + ord 'A')
155 toLower c | isUpper c = chr ((ord c - ord 'A') + ord 'a')
158 #if defined(__UNBOXED_INSTANCES__)
159 ---------------------------------------------------------------
161 ---------------------------------------------------------------
163 -- ToDo: Preferable to overload minInt and maxInt
164 -- minInt, maxInt :: Num a => a
165 -- Solution: place in class Num (as pi is in Floating)
167 minInt#, maxInt# :: Int#
168 minInt# = -2147483647#
169 maxInt# = 2147483647#
171 ---------------------------------------------------------------
172 -- Char# functions -- ToDo: class Chr ???
173 ---------------------------------------------------------------
175 toChar# :: Char -> Char#
178 fromChar# :: Char# -> Char
181 -- ord# and chr# are builtin
183 minChar#, maxChar# :: Char#
187 isAscii#, isControl#, isPrint#, isSpace# :: Char# -> Bool
188 isUpper#, isLower#, isAlpha#, isDigit#, isAlphanum# :: Char# -> Bool
190 isAscii# c = ord# c < 128#
191 isControl# c = c < ' '# || c == '\DEL'#
192 isPrint# c = c >= ' '# && c <= '~'#
193 isSpace# c = c == ' '# || c == '\t'# || c == '\n'# ||
194 c == '\r'# || c == '\f'# || c == '\v'#
195 isUpper# c = c >= 'A'# && c <= 'Z'#
196 isLower# c = c >= 'a'# && c <= 'z'#
197 isAlpha# c = isUpper# c || isLower# c
198 isDigit# c = c >= '0'# && c <= '9'#
199 isAlphanum# c = isAlpha# c || isDigit# c
202 toUpper#, toLower# :: Char# -> Char#
203 toUpper# c | isLower# c = chr# ((ord# c - ord# 'a'#) + ord# 'A'#)
206 toLower# c | isUpper# c = chr# ((ord# c - ord# 'A'#) + ord# 'a'#)
209 #endif {-UNBOXED INSTANCES-}
211 ---------------------------------------------------------------
213 ---------------------------------------------------------------
215 --{-# GENERATE_SPECS subtract a{Int#,Double#} #-}
216 {-# GENERATE_SPECS subtract a{~,Int,Double} #-}
217 subtract :: (Num a) => a -> a -> a
218 #ifdef USE_REPORT_PRELUDE
222 #endif /* ! USE_REPORT_PRELUDE */
224 --{-# GENERATE_SPECS gcd a{Int#,Int,Integer} #-}
225 {-# GENERATE_SPECS gcd a{~,Int,Integer} #-}
226 gcd :: (Integral a) => a -> a -> a
227 gcd 0 0 = error "gcd{Prelude}: gcd 0 0 is undefined\n"
228 gcd x y = gcd' (abs x) (abs y)
230 gcd' x y = gcd' y (x `rem` y)
232 --{-# GENERATE_SPECS lcm a{Int#,Int,Integer} #-}
233 {-# GENERATE_SPECS lcm a{~,Int,Integer} #-}
234 lcm :: (Integral a) => a -> a -> a
237 lcm x y = abs ((x `quot` (gcd x y)) * y)
239 --{-# GENERATE_SPECS (^) a{~,Int#,Double#,Int,Integer,Double,Complex(Double#),Complex(Double)} b{~,Int#,Int} #-}
240 {-# GENERATE_SPECS (^) a{~,Int,Integer,Double,Rational,Complex(Double)} b{~,Int} #-}
241 (^) :: (Num a, Integral b) => a -> b -> a
245 f x n y = g x n where
246 g x n | odd n = f x (n-1) (x*y)
247 | otherwise = g (x*x) (n `div` 2)
248 _ ^ _ = error "(^){Prelude}: negative exponent\n"
250 --{-# GENERATE_SPECS (^^) a{~,Double#,Double,Complex(Double#),Complex(Double)} b{~,Int#,Int} #-}
251 {-# GENERATE_SPECS (^^) a{~,Double,Rational} b{~,Int} #-}
252 (^^) :: (Fractional a, Integral b) => a -> b -> a
253 x ^^ n = if n >= 0 then x^n else recip (x^(-n))
255 --{-# GENERATE_SPECS atan2 a{Double#,Double} #-}
256 {-# GENERATE_SPECS atan2 a{~,Double} #-}
257 atan2 :: (RealFloat a) => a -> a -> a
258 #if USE_REPORT_PRELUDE
259 atan2 y x = case (signum y, signum x) of
264 ( _, 1) -> atan (y/x)
265 ( _,-1) -> atan (y/x) + pi
266 ( 0, 0) -> error "atan2{Prelude}: atan2 of origin\n"
267 #else {- steal Lennart's version -}
271 else if x < 0 then pi
272 else {- x == 0 -} error "Prelude.atan2: atan2 of origin"
275 else {- y < 0 -} -pi/2
277 atan (y/x) -- 1st and 4th quadrant
280 atan (y/x) + pi -- 2nd quadrant
282 atan (y/x) - pi -- 3rd quadrant
285 ---------------------------------------------------------------
286 -- Some standard functions:
287 ---------------------------------------------------------------
289 -- component projections for pairs:
290 --{-# GENERATE_SPECS fst a b #-}
294 --{-# GENERATE_SPECS snd a b #-}
299 --{-# GENERATE_SPECS id a #-}
304 --{-# GENERATE_SPECS const a b #-}
308 -- function composition
310 --{-# GENERATE_SPECS (.) a b c #-}
311 (.) :: (b -> c) -> (a -> b) -> a -> c
314 -- flip f takes its (first) two arguments in the reverse order of f.
315 --{-# GENERATE_SPECS flip a b c #-}
316 flip :: (a -> b -> c) -> b -> a -> c
319 -- right-associating infix application operator (useful in continuation-
321 --{-# GENERATE_SPECS ($) a b #-}
322 ($) :: (a -> b) -> a -> b
325 -- until p f yields the result of applying f until p holds.
326 --{-# GENERATE_SPECS until a #-}
327 until :: (a -> Bool) -> (a -> a) -> a -> a
328 until p f x | p x = x
329 | otherwise = until p f (f x)
331 -- asTypeOf is a type-restricted version of const. It is usually used
332 -- as an infix operator, and its typing forces its first argument
333 -- (which is usually overloaded) to have the same type as the second.
334 --{-# GENERATE_SPECS asTypeOf a #-}
335 asTypeOf :: a -> a -> a
338 ---------------------------------------------------------------
339 -- fromIntegral and fromRealFrac with explicit specialisations
340 ---------------------------------------------------------------
343 {-# SPECIALIZE fromIntegral ::
345 Int# -> Double# = int2Double#,
347 Int# -> Integer = int2Integer#,
348 Int# -> Double = i2D#,
350 Int -> Double# = i2d,
352 Int -> Integer = i2Integer,
354 Integer -> Int# = integer2i,
355 Integer -> Double# = integer2d,
356 Integer -> Int = integer2I,
357 Integer -> Integer = id,
358 Integer -> Double = integer2D #-}
361 {-# SPECIALIZE fromIntegral ::
363 Int -> Integer = i2Integer,
365 Integer -> Int = integer2I,
366 Integer -> Integer = id,
367 Integer -> Double = integer2D #-}
370 i2D# i# = D# (int2Double# i#)
373 i2d (I# i#) = int2Double# i#
374 i2D (I# i#) = D# (int2Double# i#)
375 i2Integer (I# i#) = int2Integer# i#
377 integer2i (J# a# s# d#) = integer2Int# a# s# d#
378 integer2d (J# a# s# d#) = encodeDouble# a# s# d# 0#
379 integer2I (J# a# s# d#) = I# (integer2Int# a# s# d#)
380 integer2F (J# a# s# d#) = F# (encodeFloat# a# s# d# 0#)
381 integer2D (J# a# s# d#) = D# (encodeDouble# a# s# d# 0#)
383 fromIntegral :: (Integral a, Num b) => a -> b
384 fromIntegral = fromInteger . toInteger
387 {-# SPECIALIZE fromRealFrac ::
388 Double# -> Double# = id,
389 Double# -> Double = d2D#,
390 Double -> Double# = d2d,
391 Double -> Double = id #-}
394 {-# SPECIALIZE fromRealFrac ::
396 Float -> Double = f2D,
397 Double -> Float = d2F,
398 Double -> Double = id #-}
400 d2F# d# = F# (double2Float# d#)
403 f2d (F# f#) = float2Double# f#
404 f2D (F# f#) = D# (float2Double# f#)
407 d2F (D# d#) = F# (double2Float# d#)
409 fromRealFrac :: (RealFrac a, Fractional b) => a -> b
410 fromRealFrac = fromRational . toRational