1 -- Standard value bindings
4 -- NB: not the "real" Prelude.hi export list
51 isAscii#, isControl#, isPrint#, isSpace#,
52 isUpper#, isLower#, isAlpha#, isDigit#, isAlphanum#,
57 import UTypes ( Bin ) -- so we don't get any data constructors!
72 import PS ( _PackedString, _unpackPS )
81 ---------------------------------------------------------------
83 ---------------------------------------------------------------
86 isNullBin :: Bin -> Bool
87 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 ---------------------------------------------------------------
160 ---------------------------------------------------------------
162 toInt# :: Int -> Int#
165 fromInt# :: Int# -> Int
168 -- ToDo: Preferable to overload minInt and maxInt
169 -- minInt, maxInt :: Num a => a
170 -- Solution: place in class Num (as pi is in Floating)
172 minInt#, maxInt# :: Int#
173 minInt# = -2147483647#
174 maxInt# = 2147483647#
176 ---------------------------------------------------------------
177 -- Char# functions -- ToDo: class Chr ???
178 ---------------------------------------------------------------
180 toChar# :: Char -> Char#
183 fromChar# :: Char# -> Char
186 -- ord# and chr# are builtin
188 minChar#, maxChar# :: Char#
192 isAscii#, isControl#, isPrint#, isSpace# :: Char# -> Bool
193 isUpper#, isLower#, isAlpha#, isDigit#, isAlphanum# :: Char# -> Bool
195 isAscii# c = ord# c `ltInt#` 128#
196 isControl# c = c `ltChar#` ' '# || c `eqChar#` '\DEL'#
197 isPrint# c = c `geChar#` ' '# && c `leChar#` '~'#
198 isSpace# c = c `eqChar#` ' '# || c `eqChar#` '\t'# || c `eqChar#` '\n'# ||
199 c `eqChar#` '\r'# || c `eqChar#` '\f'# || c `eqChar#` '\v'#
200 isUpper# c = c `geChar#` 'A'# && c `leChar#` 'Z'#
201 isLower# c = c `geChar#` 'a'# && c `leChar#` 'z'#
202 isAlpha# c = isUpper# c || isLower# c
203 isDigit# c = c `geChar#` '0'# && c `leChar#` '9'#
204 isAlphanum# c = isAlpha# c || isDigit# c
207 toUpper#, toLower# :: Char# -> Char#
208 toUpper# c | isLower# c = chr# ((ord# c `minusInt#` ord# 'a'#) `plusInt#` ord# 'A'#)
210 toLower# c | isUpper# c = chr# ((ord# c `minusInt#` ord# 'A'#) `plusInt#` ord# 'a'#)
213 ---------------------------------------------------------------
215 ---------------------------------------------------------------
217 {-# GENERATE_SPECS subtract a{Int#,Double#,Int,Double,Complex(Double#),Complex(Double)} #-}
218 subtract :: (Num a) => a -> a -> a
219 #ifdef USE_REPORT_PRELUDE
223 #endif /* ! USE_REPORT_PRELUDE */
225 {-# GENERATE_SPECS gcd a{Int#,Int,Integer} #-}
226 gcd :: (Integral a) => a -> a -> a
227 gcd x y | x == __i0 && y == __i0
228 = error "gcd{Prelude}: gcd 0 0 is undefined\n"
230 = gcd' (abs x) (abs y)
231 where gcd' x y | y == __i0
236 {-# GENERATE_SPECS lcm a{Int#,Int,Integer} #-}
237 lcm :: (Integral a) => a -> a -> a
243 = abs ((x `quot` (gcd x y)) * y)
245 {-# SPECIALIZE (^) :: Integer -> Integer -> Integer #-}
246 {-# GENERATE_SPECS (^) a{~,Int#,Double#,Int,Integer,Double,Rational,Complex(Double#),Complex(Double)} b{~,Int#,Int} #-}
247 (^) :: (Num a, Integral b) => a -> b -> a
253 = error "(^){Prelude}: negative exponent\n"
260 = f x (n - __i1) (x*y)
262 = g (x*x) (n `div` __i2) y
264 {-# GENERATE_SPECS (^^) a{~,Double#,Double,Rational,Complex(Double#),Complex(Double)} b{~,Int#,Int} #-}
265 (^^) :: (Fractional a, Integral b) => a -> b -> a
266 x ^^ n = if n >= 0 then x^n else recip (x^(-n))
268 {-# GENERATE_SPECS atan2 a{Double#,Double} #-}
269 atan2 :: (RealFloat a) => a -> a -> a
270 #if USE_REPORT_PRELUDE
271 atan2 y x = case (signum y, signum x) of
276 ( _, 1) -> atan (y/x)
277 ( _,-1) -> atan (y/x) + pi
278 ( 0, 0) -> error "atan2{Prelude}: atan2 of origin\n"
279 #else {- steal Lennart's version -}
283 else if x < 0 then pi
284 else {- x == 0 -} error "Prelude.atan2: atan2 of origin"
287 else {- y < 0 -} -pi/2
289 atan (y/x) -- 1st and 4th quadrant
292 atan (y/x) + pi -- 2nd quadrant
294 atan (y/x) - pi -- 3rd quadrant
297 ---------------------------------------------------------------
298 -- Some standard functions:
299 ---------------------------------------------------------------
301 -- component projections for pairs:
302 {-# GENERATE_SPECS fst a b #-}
306 {-# GENERATE_SPECS snd a b #-}
311 {-# GENERATE_SPECS id a #-}
316 {-# GENERATE_SPECS const a b #-}
320 -- function composition
322 {-# GENERATE_SPECS (.) a b c #-}
323 (.) :: (b -> c) -> (a -> b) -> a -> c
326 -- flip f takes its (first) two arguments in the reverse order of f.
327 {-# GENERATE_SPECS flip a b c #-}
328 flip :: (a -> b -> c) -> b -> a -> c
331 -- right-associating infix application operator (useful in continuation-
333 {-# GENERATE_SPECS ($) a b #-}
334 ($) :: (a -> b) -> a -> b
337 -- until p f yields the result of applying f until p holds.
338 {-# GENERATE_SPECS until a #-}
339 until :: (a -> Bool) -> (a -> a) -> a -> a
340 until p f x | p x = x
341 | otherwise = until p f (f x)
343 -- asTypeOf is a type-restricted version of const. It is usually used
344 -- as an infix operator, and its typing forces its first argument
345 -- (which is usually overloaded) to have the same type as the second.
346 {-# GENERATE_SPECS asTypeOf a #-}
347 asTypeOf :: a -> a -> a
350 ---------------------------------------------------------------
351 -- fromIntegral and fromRealFrac with explicit specialisations
352 ---------------------------------------------------------------
354 {-# SPECIALIZE fromIntegral ::
358 Int -> Integer = i2Integer,
361 Integer -> Int = integer2I,
362 Integer -> Integer = id,
363 Integer -> Float = integer2F,
364 Integer -> Double = integer2D #-}
366 #if defined(__UNBOXED_INSTANCES__)
367 {-# SPECIALIZE fromIntegral ::
370 Int# -> Double# = i2d#,
372 Int# -> Integer = i2Integer#,
373 Int# -> Float = i2F#,
374 Int# -> Double = i2D#,
376 Int -> Double# = i2d,
377 Integer -> Int# = integer2i,
378 Integer -> Double# = integer2d #-}
381 i2d# i# = int2Double# i#
383 i2Integer# i# = int2Integer# i#
384 i2F# i# = F# (int2Float# i#)
385 i2D# i# = D# (int2Double# i#)
388 i2d (I# i#) = int2Double# i#
389 i2Integer (I# i#) = int2Integer# i#
390 i2F (I# i#) = F# (int2Float# i#)
391 i2D (I# i#) = D# (int2Double# i#)
393 integer2i (J# a# s# d#) = integer2Int# a# s# d#
394 integer2d (J# a# s# d#) = encodeDouble# a# s# d# 0#
395 integer2I (J# a# s# d#) = I# (integer2Int# a# s# d#)
396 integer2F (J# a# s# d#) = F# (encodeFloat# a# s# d# 0#)
397 integer2D (J# a# s# d#) = D# (encodeDouble# a# s# d# 0#)
399 fromIntegral :: (Integral a, Num b) => a -> b
400 fromIntegral = fromInteger . toInteger
402 {-# SPECIALIZE fromRealFrac ::
407 Rational -> Rational = id,
408 Double -> Double = id,
409 Double -> Float = d2F,
411 Float -> Double = f2D #-}
413 #if defined(__UNBOXED_INSTANCES__)
414 {-# SPECIALIZE fromRealFrac ::
417 Double# -> Double# = id,
418 Double# -> Float = d2F#,
419 Double# -> Double = d2D#,
420 Double -> Double# = d2d,
421 Float -> Double# = f2d #-}
424 d2F# d# = F# (double2Float# d#)
427 f2d (F# f#) = float2Double# f#
428 f2D (F# f#) = D# (float2Double# f#)
431 d2F (D# d#) = F# (double2Float# d#)
433 fromRealFrac :: (RealFrac a, Fractional b) => a -> b
434 fromRealFrac = fromRational . toRational