3 #include "../includes/ieee-flpt.h"
5 --partain: module PreludeList,
6 head, last, tail, init, null, length, (!!),
7 foldl, foldl1, scanl, scanl1, foldr, foldr1, scanr, scanr1,
8 iterate, repeat, replicate, cycle,
9 take, drop, splitAt, takeWhile, dropWhile, span, break,
10 lines, words, unlines, unwords, reverse, and, or,
11 any, all, elem, notElem, lookup,
12 sum, product, maximum, minimum, concatMap,
13 zip, zip3, zipWith, zipWith3, unzip, unzip3,
15 --partain:module PreludeText,
17 Read(readsPrec, readList),
18 Show(showsPrec, showList),
19 reads, shows, show, read, lex,
20 showChar, showString, readParen, showParen,
21 --partain:module PreludeIO,
22 FilePath, IOError, fail, userError, catch,
23 putChar, putStr, putStrLn, print,
24 getChar, getLine, getContents, interact,
25 readFile, writeFile, appendFile, readIO, readLn,
29 Either(Left, Right), either,
31 Char, String, Int, Integer, Float, Double, IO, Void,
33 ()(..), -- Trivial type
34 -- Tuple types: (,), (,,), etc.
43 (,,,,,,,,,)(..),
44 (,,,,,,,,,,)(..),
45 (,,,,,,,,,,,)(..),
46 (,,,,,,,,,,,,)(..),
47 (,,,,,,,,,,,,,)(..),
48 (,,,,,,,,,,,,,,)(..),
49 (,,,,,,,,,,,,,,,)(..),
50 (,,,,,,,,,,,,,,,,)(..),
51 (,,,,,,,,,,,,,,,,,)(..),
52 (,,,,,,,,,,,,,,,,,,)(..),
53 (,,,,,,,,,,,,,,,,,,,)(..),
54 (,,,,,,,,,,,,,,,,,,,,)(..),
55 (,,,,,,,,,,,,,,,,,,,,,)(..),
56 (,,,,,,,,,,,,,,,,,,,,,,)(..),
57 (,,,,,,,,,,,,,,,,,,,,,,,)(..),
58 (,,,,,,,,,,,,,,,,,,,,,,,,)(..),
59 (,,,,,,,,,,,,,,,,,,,,,,,,,)(..),
60 (,,,,,,,,,,,,,,,,,,,,,,,,,,)(..),
61 (,,,,,,,,,,,,,,,,,,,,,,,,,,,)(..),
62 (,,,,,,,,,,,,,,,,,,,,,,,,,,,,)(..),
63 (,,,,,,,,,,,,,,,,,,,,,,,,,,,,,)(..),
64 (,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,)(..),
65 (,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,)(..),
66 (,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,)(..),
67 (,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,)(..),
68 (,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,)(..),
69 (,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,)(..),
70 (,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,)(..),
73 Ord(compare, (<), (<=), (>=), (>), max, min),
74 Enum(toEnum, fromEnum, enumFrom, enumFromThen,
75 enumFromTo, enumFromThenTo),
76 Bounded(minBound, maxBound),
77 Eval(..{-seq, strict-}), seq, strict, -- NB: glasgow hack
78 Num((+), (-), (*), negate, abs, signum, fromInteger, fromInt{-partain-}),
80 Integral(quot, rem, div, mod, quotRem, divMod, toInteger, toInt{-partain-}),
81 Fractional((/), recip, fromRational),
82 Floating(pi, exp, log, sqrt, (**), logBase, sin, cos, tan,
83 asin, acos, atan, sinh, cosh, tanh, asinh, acosh, atanh),
84 RealFrac(properFraction, truncate, round, ceiling, floor),
85 RealFloat(floatRadix, floatDigits, floatRange, decodeFloat,
86 encodeFloat, exponent, significand, scaleFloat, isNaN,
87 isInfinite, isDenormalized, isIEEE, isNegativeZero),
88 Monad((>>=), (>>), return),
93 mapM, mapM_, guard, accumulate, sequence, filter, concat, applyM,
95 (&&), (||), not, otherwise,
96 subtract, even, odd, gcd, lcm, (^), (^^),
97 fromIntegral, fromRealFrac, atan2,
98 fst, snd, curry, uncurry, id, const, (.), flip, ($), until,
99 asTypeOf, error, undefined ) where
101 import GHCbase -- all the GHC basics
102 import GHCio -- I/O basics
103 import Ratio(Ratio, Rational, (%), numerator, denominator)
106 import Char ( isSpace )
107 import IO ( hPutChar, hPutStr, hGetChar, hGetContents )
110 infix 4 `elem`, `notElem`
115 infixl 7 *, /, `quot`, `rem`, `div`, `mod`
118 infix 4 ==, /=, <, <=, >=, >
124 -- Standard types, classes, instances and related functions
126 -- Equality and Ordered classes
129 (==), (/=) :: a -> a -> Bool
131 x /= y = not (x == y)
133 class (Eq a) => Ord a where
134 compare :: a -> a -> Ordering
135 (<), (<=), (>=), (>):: a -> a -> Bool
136 max, min :: a -> a -> a
138 -- An instance of Ord should define either compare or <=
139 -- Using compare can be more efficient for complex types.
145 x <= y = compare x y /= GT
146 x < y = compare x y == LT
147 x >= y = compare x y /= LT
148 x > y = compare x y == GT
149 max x y = case (compare x y) of { LT -> y ; EQ -> x ; GT -> x }
150 min x y = case (compare x y) of { LT -> x ; EQ -> x ; GT -> y }
152 -- Enumeration and Bounded classes
154 class (Ord a) => Enum a where
157 enumFrom :: a -> [a] -- [n..]
158 enumFromThen :: a -> a -> [a] -- [n,n'..]
159 enumFromTo :: a -> a -> [a] -- [n..m]
160 enumFromThenTo :: a -> a -> a -> [a] -- [n,n'..m]
162 enumFromTo n m = takeWhile (<= m) (enumFrom n)
163 enumFromThenTo n n' m
164 = takeWhile (if n' >= n then (<= m) else (>= m))
167 succ, pred :: Enum a => a -> a
168 succ = toEnum . (+1) . fromEnum
169 pred = toEnum . (subtract 1) . fromEnum
171 class Bounded a where
172 minBound, maxBound :: a
176 class (Eq a, Show a, Eval a) => Num a where
177 (+), (-), (*) :: a -> a -> a
179 abs, signum :: a -> a
180 fromInteger :: Integer -> a
181 fromInt :: Int -> a -- partain: Glasgow extension
184 fromInt i = fromInteger (int2Integer i)
186 int2Integer (I# i#) = int2Integer# i#
187 -- Go via the standard class-op if the
188 -- non-standard one ain't provided
190 class (Num a, Ord a) => Real a where
191 toRational :: a -> Rational
193 class (Real a, Enum a) => Integral a where
194 quot, rem, div, mod :: a -> a -> a
195 quotRem, divMod :: a -> a -> (a,a)
196 toInteger :: a -> Integer
197 toInt :: a -> Int -- partain: Glasgow extension
199 n `quot` d = q where (q,r) = quotRem n d
200 n `rem` d = r where (q,r) = quotRem n d
201 n `div` d = q where (q,r) = divMod n d
202 n `mod` d = r where (q,r) = divMod n d
203 divMod n d = if signum r == negate (signum d) then (q-1, r+d) else qr
204 where qr@(q,r) = quotRem n d
206 class (Num a) => Fractional a where
209 fromRational :: Rational -> a
213 class (Fractional a) => Floating a where
215 exp, log, sqrt :: a -> a
216 (**), logBase :: a -> a -> a
217 sin, cos, tan :: a -> a
218 asin, acos, atan :: a -> a
219 sinh, cosh, tanh :: a -> a
220 asinh, acosh, atanh :: a -> a
222 x ** y = exp (log x * y)
223 logBase x y = log y / log x
225 tan x = sin x / cos x
226 tanh x = sinh x / cosh x
228 class (Real a, Fractional a) => RealFrac a where
229 properFraction :: (Integral b) => a -> (b,a)
230 truncate, round :: (Integral b) => a -> b
231 ceiling, floor :: (Integral b) => a -> b
233 truncate x = m where (m,_) = properFraction x
235 round x = let (n,r) = properFraction x
236 m = if r < 0 then n - 1 else n + 1
237 in case signum (abs r - 0.5) of
239 0 -> if even n then n else m
242 ceiling x = if r > 0 then n + 1 else n
243 where (n,r) = properFraction x
245 floor x = if r < 0 then n - 1 else n
246 where (n,r) = properFraction x
248 class (RealFrac a, Floating a) => RealFloat a where
249 floatRadix :: a -> Integer
250 floatDigits :: a -> Int
251 floatRange :: a -> (Int,Int)
252 decodeFloat :: a -> (Integer,Int)
253 encodeFloat :: Integer -> Int -> a
255 significand :: a -> a
256 scaleFloat :: Int -> a -> a
257 isNaN, isInfinite, isDenormalized, isNegativeZero, isIEEE
260 exponent x = if m == 0 then 0 else n + floatDigits x
261 where (m,n) = decodeFloat x
263 significand x = encodeFloat m (negate (floatDigits x))
264 where (m,_) = decodeFloat x
266 scaleFloat k x = encodeFloat m (n+k)
267 where (m,n) = decodeFloat x
271 {-# GENERATE_SPECS subtract a{Int#,Double#,Int,Double,Complex(Double#),Complex(Double)} #-}
272 subtract :: (Num a) => a -> a -> a
275 even, odd :: (Integral a) => a -> Bool
276 even n = n `rem` 2 == 0
279 {-# GENERATE_SPECS gcd a{Int#,Int,Integer} #-}
280 gcd :: (Integral a) => a -> a -> a
281 gcd 0 0 = error "Prelude.gcd: gcd 0 0 is undefined"
282 gcd x y = gcd' (abs x) (abs y)
284 gcd' x y = gcd' y (x `rem` y)
286 {-# GENERATE_SPECS lcm a{Int#,Int,Integer} #-}
287 lcm :: (Integral a) => a -> a -> a
290 lcm x y = abs ((x `quot` (gcd x y)) * y)
292 (^) :: (Num a, Integral b) => a -> b -> a
294 x ^ n | n > 0 = f x (n-1) x
296 f x n y = g x n where
297 g x n | even n = g (x*x) (n `quot` 2)
298 | otherwise = f x (n-1) (x*y)
299 _ ^ _ = error "Prelude.^: negative exponent"
301 (^^) :: (Fractional a, Integral b) => a -> b -> a
302 x ^^ n = if n >= 0 then x^n else recip (x^(negate n))
304 fromIntegral :: (Integral a, Num b) => a -> b
305 fromIntegral = fromInteger . toInteger
307 fromRealFrac :: (RealFrac a, Fractional b) => a -> b
308 fromRealFrac = fromRational . toRational
310 atan2 :: (RealFloat a) => a -> a -> a
311 atan2 y x = case (signum y, signum x) of
315 (-1, 0) -> (negate pi)/2
316 ( _, 1) -> atan (y/x)
317 ( _,-1) -> atan (y/x) + pi
318 ( 0, 0) -> error "Prelude.atan2: atan2 of origin"
323 class Functor f where
324 map :: (a -> b) -> f a -> f b
327 (>>=) :: m a -> (a -> m b) -> m b
328 (>>) :: m a -> m b -> m b
331 m >> k = m >>= \_ -> k
333 class (Monad m) => MonadZero m where
336 class (MonadZero m) => MonadPlus m where
337 (++) :: m a -> m a -> m a
339 accumulate :: Monad m => [m a] -> m [a]
340 accumulate [] = return []
341 accumulate (m:ms) = do { x <- m; xs <- accumulate ms; return (x:xs) }
342 {- partain: this may be right, but I'm going w/ a more-certainly-right version
343 accumulate = foldr mcons (return [])
344 where mcons p q = p >>= \x -> q >>= \y -> return (x:y)
346 sequence :: Monad m => [m a] -> m ()
347 sequence = foldr (>>) (return ())
349 mapM :: Monad m => (a -> m b) -> [a] -> m [b]
350 mapM f as = accumulate (map f as)
352 mapM_ :: Monad m => (a -> m b) -> [a] -> m ()
353 mapM_ f as = sequence (map f as)
355 guard :: MonadZero m => Bool -> m ()
356 guard p = if p then return () else zero
358 -- This subsumes the list-based filter function.
360 filter :: MonadZero m => (a -> Bool) -> m a -> m a
361 filter p = applyM (\x -> if p x then return x else zero)
363 -- This subsumes the list-based concat function.
365 concat :: MonadPlus m => [m a] -> m a
366 concat = foldr (++) zero
368 applyM :: Monad m => (a -> m b) -> m a -> m b
374 class Eval a {-not Glasgow: where
376 strict :: (a -> b) -> a -> b
377 strict f x = x `seq` f x -}
380 strict :: Eval a => (a -> b) -> a -> b
381 strict f x = x `seq` f x
383 ---------------------------------------------------------------
386 data () = () --easier to do explicitly: deriving (Eq, Ord, Enum, Bounded)
387 -- (avoids weird-named functions, e.g., con2tag_()#
389 instance CReturnable () -- Why, exactly?
395 instance Ord () where
404 instance Enum () where
406 toEnum _ = error "Prelude.Enum.().toEnum: argument not 0"
409 enumFromThen () () = [()]
410 enumFromTo () () = [()]
411 enumFromThenTo () () () = [()]
413 instance Bounded () where
417 instance Show () where
418 showsPrec p () = showString "()"
420 instance Read () where
421 readsPrec p = readParen False
422 (\r -> [((),t) | ("(",s) <- lex r,
425 ---------------------------------------------------------------
428 --data a -> b -- No constructor for functions is exported.
430 instance Show (a -> b) where
431 showsPrec p f = showString "<<function>>"
432 showList = showList__ (showsPrec 0)
434 ---------------------------------------------------------------
437 --partain:data Void -- No constructor for Void is exported. Import/Export
438 -- lists must use Void instead of Void(..) or Void()
440 ---------------------------------------------------------------
443 data Bool = False | True deriving (Eq, Ord, Enum, Read, Show, Bounded)
447 (&&), (||) :: Bool -> Bool -> Bool
460 ---------------------------------------------------------------
463 data Char = C# Char# deriving (Eq, Ord)
464 --partain:data Char = ... 'a' | 'b' ... -- 265 ISO values
465 instance CCallable Char
466 instance CReturnable Char
468 instance Enum Char where
469 toEnum (I# i) | i >=# 0# && i <=# 255# = C# (chr# i)
470 | otherwise = error "Prelude.Enum.Char.toEnum:out of range"
471 fromEnum (C# c) = I# (ord# c)
472 enumFrom c = map toEnum [fromEnum c .. fromEnum (maxBound::Char)]
473 enumFromThen c c' = map toEnum [fromEnum c,
474 fromEnum c' .. fromEnum lastChar]
475 where lastChar :: Char
476 lastChar | c' < c = minBound
477 | otherwise = maxBound
479 instance Bounded Char where
483 instance Read Char where
484 readsPrec p = readParen False
485 (\r -> [(c,t) | ('\'':s,t)<- lex r,
486 (c,_) <- readLitChar s])
488 readList = readParen False (\r -> [(l,t) | ('"':s, t) <- lex r,
490 where readl ('"':s) = [("",s)]
491 readl ('\\':'&':s) = readl s
492 readl s = [(c:cs,u) | (c ,t) <- readLitChar s,
494 instance Show Char where
495 showsPrec p '\'' = showString "'\\''"
496 showsPrec p c = showChar '\'' . showLitChar c . showChar '\''
498 showList cs = showChar '"' . showl cs
499 where showl "" = showChar '"'
500 showl ('"':cs) = showString "\\\"" . showl cs
501 showl (c:cs) = showLitChar c . showl cs
505 ---------------------------------------------------------------
508 data Maybe a = Nothing | Just a deriving (Eq, Ord, Read, Show)
510 maybe :: b -> (a -> b) -> Maybe a -> b
511 maybe n f Nothing = n
512 maybe n f (Just x) = f x
514 instance Functor Maybe where
515 map f Nothing = Nothing
516 map f (Just a) = Just (f a)
518 instance Monad Maybe where
520 Nothing >>= k = Nothing
523 instance MonadZero Maybe where
526 instance MonadPlus Maybe where
530 ---------------------------------------------------------------
533 data Either a b = Left a | Right b deriving (Eq, Ord, Read, Show)
535 either :: (a -> c) -> (b -> c) -> Either a b -> c
536 either f g (Left x) = f x
537 either f g (Right y) = g y
539 ---------------------------------------------------------------
540 -- IO type: moved to GHCbase
542 --partain: data IO a = -- abstract
544 ---------------------------------------------------------------
547 data Ordering = LT | EQ | GT deriving (Eq, Ord, Enum, Read, Show, Bounded)
549 ---------------------------------------------------------------
550 -- Standard numeric types. The data declarations for these types
551 -- cannot be expressed directly in (standard) Haskell since the
552 -- constructor lists would be far too large.
554 ---------------------------------------------------------------
555 data Int = I# Int# deriving (Eq,Ord)
556 --partain:data Int = minBound ... -1 | 0 | 1 ... maxBound
558 instance CCallable Int
559 instance CReturnable Int
561 instance Bounded Int where
562 minBound = negate 2147483647 -- **********************
563 maxBound = 2147483647 -- **********************
565 instance Num Int where
566 (+) x y = plusInt x y
567 (-) x y = minusInt x y
568 negate x = negateInt x
569 (*) x y = timesInt x y
570 abs n = if n `geInt` 0 then n else (negateInt n)
572 signum n | n `ltInt` 0 = negateInt 1
576 fromInteger (J# a# s# d#)
577 = case (integer2Int# a# s# d#) of { i# -> I# i# }
581 instance Real Int where
582 toRational x = toInteger x % 1
584 instance Integral Int where
585 a@(I# _) `quotRem` b@(I# _) = (a `quotInt` b, a `remInt` b)
586 -- OK, so I made it a little stricter. Shoot me. (WDP 94/10)
588 -- following chks for zero divisor are non-standard (WDP)
589 a `quot` b = if b /= 0
591 else error "Integral.Int.quot{PreludeCore}: divide by 0\n"
592 a `rem` b = if b /= 0
594 else error "Integral.Int.rem{PreludeCore}: divide by 0\n"
596 x `div` y = if x > 0 && y < 0 then quotInt (x-y-1) y
597 else if x < 0 && y > 0 then quotInt (x-y+1) y
599 x `mod` y = if x > 0 && y < 0 || x < 0 && y > 0 then
600 if r/=0 then r+y else 0
605 divMod x@(I# _) y@(I# _) = (x `div` y, x `mod` y)
606 -- Stricter. Sorry if you don't like it. (WDP 94/10)
608 --OLD: even x = eqInt (x `mod` 2) 0
609 --OLD: odd x = neInt (x `mod` 2) 0
611 toInteger (I# n#) = int2Integer# n# -- give back a full-blown Integer
614 instance Enum Int where
617 #ifndef USE_FOLDR_BUILD
618 enumFrom x = x : enumFrom (x `plusInt` 1)
619 enumFromTo n m = takeWhile (<= m) (enumFrom n)
621 {-# INLINE enumFrom #-}
622 {-# INLINE enumFromTo #-}
623 enumFrom x = build (\ c _ ->
624 let g x = x `c` g (x `plusInt` 1) in g x)
625 enumFromTo x y = build (\ c n ->
626 let g x = if x <= y then x `c` g (x `plusInt` 1) else n in g x)
628 enumFromThen m n = en' m (n `minusInt` m)
629 where en' m n = m : en' (m `plusInt` n) n
630 enumFromThenTo n m p = takeWhile (if m >= n then (<= p) else (>= p))
633 instance Read Int where
634 readsPrec p x = readSigned readDec x
635 readList = readList__ (readsPrec 0)
637 instance Show Int where
638 showsPrec x = showSigned showInt x
639 showList = showList__ (showsPrec 0)
641 ---------------------------------------------------------------
642 data Integer = J# Int# Int# ByteArray#
643 --partain:data Integer = ... -1 | 0 | 1 ...
645 instance Eq Integer where
646 (J# a1 s1 d1) == (J# a2 s2 d2)
647 = (cmpInteger# a1 s1 d1 a2 s2 d2) ==# 0#
649 (J# a1 s1 d1) /= (J# a2 s2 d2)
650 = (cmpInteger# a1 s1 d1 a2 s2 d2) /=# 0#
652 instance Ord Integer where
653 (J# a1 s1 d1) <= (J# a2 s2 d2)
654 = (cmpInteger# a1 s1 d1 a2 s2 d2) <=# 0#
656 (J# a1 s1 d1) < (J# a2 s2 d2)
657 = (cmpInteger# a1 s1 d1 a2 s2 d2) <# 0#
659 (J# a1 s1 d1) >= (J# a2 s2 d2)
660 = (cmpInteger# a1 s1 d1 a2 s2 d2) >=# 0#
662 (J# a1 s1 d1) > (J# a2 s2 d2)
663 = (cmpInteger# a1 s1 d1 a2 s2 d2) ># 0#
665 x@(J# a1 s1 d1) `max` y@(J# a2 s2 d2)
666 = if ((cmpInteger# a1 s1 d1 a2 s2 d2) ># 0#) then x else y
668 x@(J# a1 s1 d1) `min` y@(J# a2 s2 d2)
669 = if ((cmpInteger# a1 s1 d1 a2 s2 d2) <# 0#) then x else y
671 compare (J# a1 s1 d1) (J# a2 s2 d2)
672 = case cmpInteger# a1 s1 d1 a2 s2 d2 of { res# ->
673 if res# <# 0# then LT else
674 if res# ># 0# then GT else EQ
677 instance Num Integer where
678 (+) (J# a1 s1 d1) (J# a2 s2 d2)
679 = plusInteger# a1 s1 d1 a2 s2 d2
681 (-) (J# a1 s1 d1) (J# a2 s2 d2)
682 = minusInteger# a1 s1 d1 a2 s2 d2
684 negate (J# a s d) = negateInteger# a s d
686 (*) (J# a1 s1 d1) (J# a2 s2 d2)
687 = timesInteger# a1 s1 d1 a2 s2 d2
689 -- ORIG: abs n = if n >= 0 then n else -n
692 = case 0 of { J# a2 s2 d2 ->
693 if (cmpInteger# a1 s1 d1 a2 s2 d2) >=# 0#
695 else negateInteger# a1 s1 d1
698 signum n@(J# a1 s1 d1)
699 = case 0 of { J# a2 s2 d2 ->
701 cmp = cmpInteger# a1 s1 d1 a2 s2 d2
704 else if cmp ==# 0# then 0
710 fromInt (I# n#) = int2Integer# n# -- gives back a full-blown Integer
712 instance Real Integer where
715 instance Integral Integer where
716 quotRem (J# a1 s1 d1) (J# a2 s2 d2)
717 = case (quotRemInteger# a1 s1 d1 a2 s2 d2) of
718 Return2GMPs a3 s3 d3 a4 s4 d4
719 -> (J# a3 s3 d3, J# a4 s4 d4)
721 {- USING THE UNDERLYING "GMP" CODE IS DUBIOUS FOR NOW:
723 divMod (J# a1 s1 d1) (J# a2 s2 d2)
724 = case (divModInteger# a1 s1 d1 a2 s2 d2) of
725 Return2GMPs a3 s3 d3 a4 s4 d4
726 -> (J# a3 s3 d3, J# a4 s4 d4)
729 toInt (J# a s d) = case (integer2Int# a s d) of { n# -> I# n# }
731 -- the rest are identical to the report default methods;
732 -- you get slightly better code if you let the compiler
733 -- see them right here:
734 n `quot` d = q where (q,r) = quotRem n d
735 n `rem` d = r where (q,r) = quotRem n d
736 n `div` d = q where (q,r) = divMod n d
737 n `mod` d = r where (q,r) = divMod n d
739 divMod n d = case (quotRem n d) of { qr@(q,r) ->
740 if signum r == negate (signum d) then (q - 1, r+d) else qr }
741 -- Case-ified by WDP 94/10
743 instance Enum Integer where
744 enumFrom n = n : enumFrom (n + 1)
745 enumFromThen m n = en' m (n - m)
746 where en' m n = m : en' (m + n) n
747 enumFromTo n m = takeWhile (<= m) (enumFrom n)
748 enumFromThenTo n m p = takeWhile (if m >= n then (<= p) else (>= p))
751 instance Read Integer where
752 readsPrec p x = readSigned readDec x
753 readList = readList__ (readsPrec 0)
755 instance Show Integer where
756 showsPrec x = showSigned showInt x
757 showList = showList__ (showsPrec 0)
759 ---------------------------------------------------------------
760 data Float = F# Float# deriving (Eq, Ord)
761 instance CCallable Float
762 instance CReturnable Float
764 ---------------------------------------------------------------
766 instance Num Float where
767 (+) x y = plusFloat x y
768 (-) x y = minusFloat x y
769 negate x = negateFloat x
770 (*) x y = timesFloat x y
772 | otherwise = negateFloat x
773 signum x | x == 0.0 = 0
775 | otherwise = negate 1
776 fromInteger n = encodeFloat n 0
777 fromInt i = int2Float i
779 instance Real Float where
780 toRational x = (m%1)*(b%1)^^n
781 where (m,n) = decodeFloat x
784 instance Fractional Float where
785 (/) x y = divideFloat x y
786 fromRational x = fromRational__ x
789 instance Floating Float where
790 pi = 3.141592653589793238
803 (**) x y = powerFloat x y
804 logBase x y = log y / log x
806 asinh x = log (x + sqrt (1.0+x*x))
807 acosh x = log (x + (x+1.0) * sqrt ((x-1.0)/(x+1.0)))
808 atanh x = log ((x+1.0) / sqrt (1.0-x*x))
810 instance RealFrac Float where
812 {-# SPECIALIZE properFraction :: Float -> (Int, Float) #-}
813 {-# SPECIALIZE truncate :: Float -> Int #-}
814 {-# SPECIALIZE round :: Float -> Int #-}
815 {-# SPECIALIZE ceiling :: Float -> Int #-}
816 {-# SPECIALIZE floor :: Float -> Int #-}
818 {-# SPECIALIZE properFraction :: Float -> (Integer, Float) #-}
819 {-# SPECIALIZE truncate :: Float -> Integer #-}
820 {-# SPECIALIZE round :: Float -> Integer #-}
821 {-# SPECIALIZE ceiling :: Float -> Integer #-}
822 {-# SPECIALIZE floor :: Float -> Integer #-}
825 = case (decodeFloat x) of { (m,n) ->
826 let b = floatRadix x in
828 (fromInteger m * fromInteger b ^ n, 0.0)
830 case (quotRem m (b^(negate n))) of { (w,r) ->
831 (fromInteger w, encodeFloat r n)
835 truncate x = case properFraction x of
838 round x = case properFraction x of
840 m = if r < 0.0 then n - 1 else n + 1
841 half_down = abs r - 0.5
843 case (compare half_down 0.0) of
845 EQ -> if even n then n else m
848 ceiling x = case properFraction x of
849 (n,r) -> if r > 0.0 then n + 1 else n
851 floor x = case properFraction x of
852 (n,r) -> if r < 0.0 then n - 1 else n
854 instance RealFloat Float where
855 floatRadix _ = FLT_RADIX -- from float.h
856 floatDigits _ = FLT_MANT_DIG -- ditto
857 floatRange _ = (FLT_MIN_EXP, FLT_MAX_EXP) -- ditto
860 = case decodeFloat# f# of
861 ReturnIntAndGMP exp# a# s# d# ->
862 (J# a# s# d#, I# exp#)
864 encodeFloat (J# a# s# d#) (I# e#)
865 = case encodeFloat# a# s# d# e# of { flt# -> F# flt# }
867 exponent x = case decodeFloat x of
868 (m,n) -> if m == 0 then 0 else n + floatDigits x
870 significand x = case decodeFloat x of
871 (m,_) -> encodeFloat m (negate (floatDigits x))
873 scaleFloat k x = case decodeFloat x of
874 (m,n) -> encodeFloat m (n+k)
876 instance Read Float where
877 readsPrec p x = readSigned readFloat x
878 readList = readList__ (readsPrec 0)
880 instance Show Float where
881 showsPrec x = showSigned showFloat x
882 showList = showList__ (showsPrec 0)
884 ---------------------------------------------------------------
885 data Double = D# Double# deriving (Eq, Ord)
886 instance CCallable Double
887 instance CReturnable Double
889 ---------------------------------------------------------------
891 instance Num Double where
892 (+) x y = plusDouble x y
893 (-) x y = minusDouble x y
894 negate x = negateDouble x
895 (*) x y = timesDouble x y
897 | otherwise = negateDouble x
898 signum x | x == 0.0 = 0
900 | otherwise = negate 1
901 fromInteger n = encodeFloat n 0
902 fromInt (I# n#) = case (int2Double# n#) of { d# -> D# d# }
904 instance Real Double where
905 toRational x = (m%1)*(b%1)^^n
906 where (m,n) = decodeFloat x
909 instance Fractional Double where
910 (/) x y = divideDouble x y
911 fromRational x = fromRational__ x
914 instance Floating Double where
915 pi = 3.141592653589793238
918 sqrt x = sqrtDouble x
922 asin x = asinDouble x
923 acos x = acosDouble x
924 atan x = atanDouble x
925 sinh x = sinhDouble x
926 cosh x = coshDouble x
927 tanh x = tanhDouble x
928 (**) x y = powerDouble x y
929 logBase x y = log y / log x
931 asinh x = log (x + sqrt (1.0+x*x))
932 acosh x = log (x + (x+1.0) * sqrt ((x-1.0)/(x+1.0)))
933 atanh x = log ((x+1.0) / sqrt (1.0-x*x))
935 instance RealFrac Double where
937 {-# SPECIALIZE properFraction :: Double -> (Int, Double) #-}
938 {-# SPECIALIZE truncate :: Double -> Int #-}
939 {-# SPECIALIZE round :: Double -> Int #-}
940 {-# SPECIALIZE ceiling :: Double -> Int #-}
941 {-# SPECIALIZE floor :: Double -> Int #-}
943 {-# SPECIALIZE properFraction :: Double -> (Integer, Double) #-}
944 {-# SPECIALIZE truncate :: Double -> Integer #-}
945 {-# SPECIALIZE round :: Double -> Integer #-}
946 {-# SPECIALIZE ceiling :: Double -> Integer #-}
947 {-# SPECIALIZE floor :: Double -> Integer #-}
949 #if defined(__UNBOXED_INSTANCES__)
950 {-# SPECIALIZE properFraction :: Double -> (Int#, Double) #-}
951 {-# SPECIALIZE truncate :: Double -> Int# #-}
952 {-# SPECIALIZE round :: Double -> Int# #-}
953 {-# SPECIALIZE ceiling :: Double -> Int# #-}
954 {-# SPECIALIZE floor :: Double -> Int# #-}
958 = case (decodeFloat x) of { (m,n) ->
959 let b = floatRadix x in
961 (fromInteger m * fromInteger b ^ n, 0.0)
963 case (quotRem m (b^(negate n))) of { (w,r) ->
964 (fromInteger w, encodeFloat r n)
968 truncate x = case properFraction x of
971 round x = case properFraction x of
973 m = if r < 0.0 then n - 1 else n + 1
974 half_down = abs r - 0.5
976 case (compare half_down 0.0) of
978 EQ -> if even n then n else m
981 ceiling x = case properFraction x of
982 (n,r) -> if r > 0.0 then n + 1 else n
984 floor x = case properFraction x of
985 (n,r) -> if r < 0.0 then n - 1 else n
987 instance RealFloat Double where
988 floatRadix _ = FLT_RADIX -- from float.h
989 floatDigits _ = DBL_MANT_DIG -- ditto
990 floatRange _ = (DBL_MIN_EXP, DBL_MAX_EXP) -- ditto
993 = case decodeDouble# d# of
994 ReturnIntAndGMP exp# a# s# d# ->
995 (J# a# s# d#, I# exp#)
997 encodeFloat (J# a# s# d#) (I# e#)
998 = case encodeDouble# a# s# d# e# of { dbl# -> D# dbl# }
1000 exponent x = case decodeFloat x of
1001 (m,n) -> if m == 0 then 0 else n + floatDigits x
1003 significand x = case decodeFloat x of
1004 (m,_) -> encodeFloat m (negate (floatDigits x))
1006 scaleFloat k x = case decodeFloat x of
1007 (m,n) -> encodeFloat m (n+k)
1009 instance Read Double where
1010 readsPrec p x = readSigned readFloat x
1011 readList = readList__ (readsPrec 0)
1013 instance Show Double where
1014 showsPrec x = showSigned showFloat x
1015 showList = showList__ (showsPrec 0)
1017 ---------------------------------------------------------------
1018 -- The Enum instances for Floats and Doubles are slightly unusual.
1019 -- The `toEnum' function truncates numbers to Int. The definitions
1020 -- of enumFrom and enumFromThen allow floats to be used in arithmetic
1021 -- series: [0,0.1 .. 1.0]. However, roundoff errors make these somewhat
1022 -- dubious. This example may have either 10 or 11 elements, depending on
1023 -- how 0.1 is represented.
1025 instance Enum Float where
1026 toEnum = fromIntegral
1027 fromEnum = fromInteger . truncate -- may overflow
1028 enumFrom = numericEnumFrom
1029 enumFromThen = numericEnumFromThen
1031 instance Enum Double where
1032 toEnum = fromIntegral
1033 fromEnum = fromInteger . truncate -- may overflow
1034 enumFrom = numericEnumFrom
1035 enumFromThen = numericEnumFromThen
1037 numericEnumFrom :: (Real a) => a -> [a]
1038 numericEnumFromThen :: (Real a) => a -> a -> [a]
1039 numericEnumFrom = iterate (+1)
1040 numericEnumFromThen n m = iterate (+(m-n)) n
1042 ---------------------------------------------------------------
1045 data [] a = [] | a : [a] -- do explicitly: deriving (Eq, Ord)
1046 -- to avoid weird names like con2tag_[]#
1048 instance CCallable [Char]
1049 instance CReturnable [Char]
1051 instance (Eq a) => Eq [a] where
1053 (x:xs) == (y:ys) = x == y && xs == ys
1056 xs /= ys = if (xs == ys) then False else True
1058 instance (Ord a) => Ord [a] where
1059 a < b = case compare a b of { LT -> True; EQ -> False; GT -> False }
1060 a <= b = case compare a b of { LT -> True; EQ -> True; GT -> False }
1061 a >= b = case compare a b of { LT -> False; EQ -> True; GT -> True }
1062 a > b = case compare a b of { LT -> False; EQ -> False; GT -> True }
1064 max a b = case compare a b of { LT -> b; EQ -> a; GT -> a }
1065 min a b = case compare a b of { LT -> a; EQ -> a; GT -> b }
1068 compare (x:xs) [] = GT
1069 compare [] (y:ys) = LT
1070 compare (x:xs) (y:ys) = case compare x y of
1075 instance Functor [] where
1077 map f (x:xs) = f x : map f xs
1079 instance Monad [] where
1080 m >>= k = concat (map k m)
1083 instance MonadZero [] where
1086 instance MonadPlus [] where
1087 xs ++ ys = foldr (:) ys xs
1089 instance (Show a) => Show [a] where
1090 showsPrec p = showList
1091 showList = showList__ (showsPrec 0)
1093 instance (Read a) => Read [a] where
1094 readsPrec p = readList
1095 readList = readList__ (readsPrec 0)
1097 ---------------------------------------------------------------
1100 data (,) a b = (,) a b deriving (Eq, Ord, Bounded)
1101 data (,,) a b c = (,,) a b c deriving (Eq, Ord, Bounded)
1102 data (,,,) a b c d = (,,,) a b c d deriving (Eq, Ord, Bounded)
1103 data (,,,,) a b c d e = (,,,,) a b c d e deriving (Eq, Ord, Bounded)
1104 data (,,,,,) a b c d e f = (,,,,,) a b c d e f
1105 data (,,,,,,) a b c d e f g = (,,,,,,) a b c d e f g
1106 data (,,,,,,,) a b c d e f g h = (,,,,,,,) a b c d e f g h
1107 data (,,,,,,,,) a b c d e f g h i = (,,,,,,,,) a b c d e f g h i
1108 data (,,,,,,,,,) a b c d e f g h i j = (,,,,,,,,,) a b c d e f g h i j
1109 data (,,,,,,,,,,) a b c d e f g h i j k = (,,,,,,,,,,) a b c d e f g h i j k
1110 data (,,,,,,,,,,,) a b c d e f g h i j k l = (,,,,,,,,,,,) a b c d e f g h i j k l
1111 data (,,,,,,,,,,,,) a b c d e f g h i j k l m = (,,,,,,,,,,,,) a b c d e f g h i j k l m
1112 data (,,,,,,,,,,,,,) a b c d e f g h i j k l m n = (,,,,,,,,,,,,,) a b c d e f g h i j k l m n
1113 data (,,,,,,,,,,,,,,) a b c d e f g h i j k l m n o = (,,,,,,,,,,,,,,) a b c d e f g h i j k l m n o
1114 data (,,,,,,,,,,,,,,,) a b c d e f g h i j k l m n o p = (,,,,,,,,,,,,,,,) a b c d e f g h i j k l m n o p
1115 data (,,,,,,,,,,,,,,,,) a b c d e f g h i j k l m n o p q
1116 = (,,,,,,,,,,,,,,,,) a b c d e f g h i j k l m n o p q
1117 data (,,,,,,,,,,,,,,,,,) a b c d e f g h i j k l m n o p q r
1118 = (,,,,,,,,,,,,,,,,,) a b c d e f g h i j k l m n o p q r
1119 data (,,,,,,,,,,,,,,,,,,) a b c d e f g h i j k l m n o p q r s
1120 = (,,,,,,,,,,,,,,,,,,) a b c d e f g h i j k l m n o p q r s
1121 data (,,,,,,,,,,,,,,,,,,,) a b c d e f g h i j k l m n o p q r s t
1122 = (,,,,,,,,,,,,,,,,,,,) a b c d e f g h i j k l m n o p q r s t
1123 data (,,,,,,,,,,,,,,,,,,,,) a b c d e f g h i j k l m n o p q r s t u
1124 = (,,,,,,,,,,,,,,,,,,,,) a b c d e f g h i j k l m n o p q r s t u
1125 data (,,,,,,,,,,,,,,,,,,,,,) a b c d e f g h i j k l m n o p q r s t u v
1126 = (,,,,,,,,,,,,,,,,,,,,,) a b c d e f g h i j k l m n o p q r s t u v
1127 data (,,,,,,,,,,,,,,,,,,,,,,) a b c d e f g h i j k l m n o p q r s t u v w
1128 = (,,,,,,,,,,,,,,,,,,,,,,) a b c d e f g h i j k l m n o p q r s t u v w
1129 data (,,,,,,,,,,,,,,,,,,,,,,,) a b c d e f g h i j k l m n o p q r s t u v w x
1130 = (,,,,,,,,,,,,,,,,,,,,,,,) a b c d e f g h i j k l m n o p q r s t u v w x
1131 data (,,,,,,,,,,,,,,,,,,,,,,,,) a b c d e f g h i j k l m n o p q r s t u v w x y
1132 = (,,,,,,,,,,,,,,,,,,,,,,,,) a b c d e f g h i j k l m n o p q r s t u v w x y
1133 data (,,,,,,,,,,,,,,,,,,,,,,,,,) a b c d e f g h i j k l m n o p q r s t u v w x y z
1134 = (,,,,,,,,,,,,,,,,,,,,,,,,,) a b c d e f g h i j k l m n o p q r s t u v w x y z
1135 data (,,,,,,,,,,,,,,,,,,,,,,,,,,) a b c d e f g h i j k l m n o p q r s t u v w x y z a_
1136 = (,,,,,,,,,,,,,,,,,,,,,,,,,,) a b c d e f g h i j k l m n o p q r s t u v w x y z a_
1137 data (,,,,,,,,,,,,,,,,,,,,,,,,,,,) a b c d e f g h i j k l m n o p q r s t u v w x y z a_ b_
1138 = (,,,,,,,,,,,,,,,,,,,,,,,,,,,) a b c d e f g h i j k l m n o p q r s t u v w x y z a_ b_
1139 data (,,,,,,,,,,,,,,,,,,,,,,,,,,,,) a b c d e f g h i j k l m n o p q r s t u v w x y z a_ b_ c_
1140 = (,,,,,,,,,,,,,,,,,,,,,,,,,,,,) a b c d e f g h i j k l m n o p q r s t u v w x y z a_ b_ c_
1141 data (,,,,,,,,,,,,,,,,,,,,,,,,,,,,,) a b c d e f g h i j k l m n o p q r s t u v w x y z a_ b_ c_ d_
1142 = (,,,,,,,,,,,,,,,,,,,,,,,,,,,,,) a b c d e f g h i j k l m n o p q r s t u v w x y z a_ b_ c_ d_
1143 data (,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,) a b c d e f g h i j k l m n o p q r s t u v w x y z a_ b_ c_ d_ e_
1144 = (,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,) a b c d e f g h i j k l m n o p q r s t u v w x y z a_ b_ c_ d_ e_
1145 data (,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,) a b c d e f g h i j k l m n o p q r s t u v w x y z a_ b_ c_ d_ e_ f_
1146 = (,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,) a b c d e f g h i j k l m n o p q r s t u v w x y z a_ b_ c_ d_ e_ f_
1147 data (,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,) a b c d e f g h i j k l m n o p q r s t u v w x y z a_ b_ c_ d_ e_ f_ g_
1148 = (,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,) a b c d e f g h i j k l m n o p q r s t u v w x y z a_ b_ c_ d_ e_ f_ g_
1149 data (,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,) a b c d e f g h i j k l m n o p q r s t u v w x y z a_ b_ c_ d_ e_ f_ g_ h_
1150 = (,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,) a b c d e f g h i j k l m n o p q r s t u v w x y z a_ b_ c_ d_ e_ f_ g_ h_
1151 data (,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,) a b c d e f g h i j k l m n o p q r s t u v w x y z a_ b_ c_ d_ e_ f_ g_ h_ i_
1152 = (,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,) a b c d e f g h i j k l m n o p q r s t u v w x y z a_ b_ c_ d_ e_ f_ g_ h_ i_
1153 data (,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,) a b c d e f g h i j k l m n o p q r s t u v w x y z a_ b_ c_ d_ e_ f_ g_ h_ i_ j_
1154 = (,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,) a b c d e f g h i j k l m n o p q r s t u v w x y z a_ b_ c_ d_ e_ f_ g_ h_ i_ j_
1155 data (,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,) a b c d e f g h i j k l m n o p q r s t u v w x y z a_ b_ c_ d_ e_ f_ g_ h_ i_ j_ k_
1156 = (,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,) a b c d e f g h i j k l m n o p q r s t u v w x y z a_ b_ c_ d_ e_ f_ g_ h_ i_ j_ k_
1157 -- if you add more tuples, you need to change the compiler, too
1158 -- (it has a wired-in number: 37)
1160 instance (Read a, Read b) => Read (a,b) where
1161 readsPrec p = readParen False
1162 (\r -> [((x,y), w) | ("(",s) <- lex r,
1166 (")",w) <- lex v ] )
1167 readList = readList__ (readsPrec 0)
1169 instance (Read a, Read b, Read c) => Read (a, b, c) where
1170 readsPrec p = readParen False
1171 (\a -> [((x,y,z), h) | ("(",b) <- lex a,
1172 (x,c) <- readsPrec 0 b,
1174 (y,e) <- readsPrec 0 d,
1176 (z,g) <- readsPrec 0 f,
1177 (")",h) <- lex g ] )
1178 readList = readList__ (readsPrec 0)
1180 instance (Read a, Read b, Read c, Read d) => Read (a, b, c, d) where
1181 readsPrec p = readParen False
1182 (\a -> [((w,x,y,z), j) | ("(",b) <- lex a,
1183 (w,c) <- readsPrec 0 b,
1185 (x,e) <- readsPrec 0 d,
1187 (y,g) <- readsPrec 0 f,
1189 (z,i) <- readsPrec 0 h,
1190 (")",j) <- lex i ] )
1191 readList = readList__ (readsPrec 0)
1193 instance (Read a, Read b, Read c, Read d, Read e) => Read (a, b, c, d, e) where
1194 readsPrec p = readParen False
1195 (\a -> [((w,x,y,z,v), l) | ("(",b) <- lex a,
1196 (w,c) <- readsPrec 0 b,
1198 (x,e) <- readsPrec 0 d,
1200 (y,g) <- readsPrec 0 f,
1202 (z,i) <- readsPrec 0 h,
1204 (v,k) <- readsPrec 0 j,
1205 (")",l) <- lex k ] )
1206 readList = readList__ (readsPrec 0)
1208 instance (Show a, Show b) => Show (a,b) where
1209 showsPrec p (x,y) = showChar '(' . shows x . showString ", " .
1210 shows y . showChar ')'
1211 showList = showList__ (showsPrec 0)
1213 instance (Show a, Show b, Show c) => Show (a, b, c) where
1214 showsPrec p (x,y,z) = showChar '(' . showsPrec 0 x . showString ", " .
1215 showsPrec 0 y . showString ", " .
1216 showsPrec 0 z . showChar ')'
1217 showList = showList__ (showsPrec 0)
1219 instance (Show a, Show b, Show c, Show d) => Show (a, b, c, d) where
1220 showsPrec p (w,x,y,z) = showChar '(' . showsPrec 0 w . showString ", " .
1221 showsPrec 0 x . showString ", " .
1222 showsPrec 0 y . showString ", " .
1223 showsPrec 0 z . showChar ')'
1225 showList = showList__ (showsPrec 0)
1227 instance (Show a, Show b, Show c, Show d, Show e) => Show (a, b, c, d, e) where
1228 showsPrec p (v,w,x,y,z) = showChar '(' . showsPrec 0 v . showString ", " .
1229 showsPrec 0 w . showString ", " .
1230 showsPrec 0 x . showString ", " .
1231 showsPrec 0 y . showString ", " .
1232 showsPrec 0 z . showChar ')'
1233 showList = showList__ (showsPrec 0)
1235 ---------------------------------------------------------------------
1236 -- component projections for pairs:
1237 -- (NB: not provided for triples, quadruples, etc.)
1244 -- curry converts an uncurried function to a curried function;
1245 -- uncurry converts a curried function to a function on pairs.
1246 curry :: ((a, b) -> c) -> a -> b -> c
1247 curry f x y = f (x, y)
1249 uncurry :: (a -> b -> c) -> ((a, b) -> c)
1250 uncurry f p = f (fst p) (snd p)
1254 -- Standard value bindings
1256 -- identity function
1260 -- constant function
1261 const :: a -> b -> a
1264 -- function composition
1266 {-# GENERATE_SPECS (.) a b c #-}
1267 (.) :: (b -> c) -> (a -> b) -> a -> c
1268 f . g = \ x -> f (g x)
1270 -- flip f takes its (first) two arguments in the reverse order of f.
1271 flip :: (a -> b -> c) -> b -> a -> c
1274 -- right-associating infix application operator (useful in continuation-
1276 ($) :: (a -> b) -> a -> b
1279 -- until p f yields the result of applying f until p holds.
1280 until :: (a -> Bool) -> (a -> a) -> a -> a
1281 until p f x | p x = x
1282 | otherwise = until p f (f x)
1284 -- asTypeOf is a type-restricted version of const. It is usually used
1285 -- as an infix operator, and its typing forces its first argument
1286 -- (which is usually overloaded) to have the same type as the second.
1287 asTypeOf :: a -> a -> a
1290 -- error stops execution and displays an error message
1292 error :: String -> a
1293 error s = error__ ( \ x -> _ccall_ ErrorHdrHook x ) s
1295 -- It is expected that compilers will recognize this and insert error
1296 -- messages which are more appropriate to the context in which undefined
1300 undefined = error "Prelude.undefined"
1302 -- ============================================================
1303 -- Standard list functions
1304 -- ============================================================
1306 {- module PreludeList -}
1308 -- head and tail extract the first element and remaining elements,
1309 -- respectively, of a list, which must be non-empty. last and init
1310 -- are the dual functions working from the end of a finite list,
1311 -- rather than the beginning.
1315 head [] = error "PreludeList.head: empty list"
1319 last (_:xs) = last xs
1320 last [] = error "PreludeList.last: empty list"
1324 tail [] = error "PreludeList.tail: empty list"
1328 init (x:xs) = x : init xs
1329 init [] = error "PreludeList.init: empty list"
1335 -- length returns the length of a finite list as an Int; it is an instance
1336 -- of the more general genericLength, the result type of which may be
1337 -- any kind of number.
1338 length :: [a] -> Int
1340 length (_:l) = 1 + length l
1342 -- List index (subscript) operator, 0-origin
1343 (!!) :: [a] -> Int -> a
1345 (_:xs) !! n | n > 0 = xs !! (n-1)
1346 (_:_) !! _ = error "PreludeList.!!: negative index"
1347 [] !! _ = error "PreludeList.!!: index too large"
1349 -- foldl, applied to a binary operator, a starting value (typically the
1350 -- left-identity of the operator), and a list, reduces the list using
1351 -- the binary operator, from left to right:
1352 -- foldl f z [x1, x2, ..., xn] == (...((z `f` x1) `f` x2) `f`...) `f` xn
1353 -- foldl1 is a variant that has no starting value argument, and thus must
1354 -- be applied to non-empty lists. scanl is similar to foldl, but returns
1355 -- a list of successive reduced values from the left:
1356 -- scanl f z [x1, x2, ...] == [z, z `f` x1, (z `f` x1) `f` x2, ...]
1357 -- Note that last (scanl f z xs) == foldl f z xs.
1358 -- scanl1 is similar, again without the starting element:
1359 -- scanl1 f [x1, x2, ...] == [x1, x1 `f` x2, ...]
1361 foldl :: (a -> b -> a) -> a -> [b] -> a
1363 foldl f z (x:xs) = foldl f (f z x) xs
1365 foldl1 :: (a -> a -> a) -> [a] -> a
1366 foldl1 f (x:xs) = foldl f x xs
1367 foldl1 _ [] = error "PreludeList.foldl1: empty list"
1369 scanl :: (a -> b -> a) -> a -> [b] -> [a]
1370 scanl f q xs = q : (case xs of
1372 x:xs -> scanl f (f q x) xs)
1374 scanl1 :: (a -> a -> a) -> [a] -> [a]
1375 scanl1 f (x:xs) = scanl f x xs
1376 scanl1 _ [] = error "PreludeList.scanl1: empty list"
1378 -- foldr, foldr1, scanr, and scanr1 are the right-to-left duals of the
1381 foldr :: (a -> b -> b) -> b -> [a] -> b
1383 foldr f z (x:xs) = f x (foldr f z xs)
1385 foldr1 :: (a -> a -> a) -> [a] -> a
1387 foldr1 f (x:xs) = f x (foldr1 f xs)
1388 foldr1 _ [] = error "PreludeList.foldr1: empty list"
1390 scanr :: (a -> b -> b) -> b -> [a] -> [b]
1391 scanr f q0 [] = [q0]
1392 scanr f q0 (x:xs) = f x q : qs
1393 where qs@(q:_) = scanr f q0 xs
1395 scanr1 :: (a -> a -> a) -> [a] -> [a]
1397 scanr1 f (x:xs) = f x q : qs
1398 where qs@(q:_) = scanr1 f xs
1399 scanr1 _ [] = error "PreludeList.scanr1: empty list"
1401 -- iterate f x returns an infinite list of repeated applications of f to x:
1402 -- iterate f x == [x, f x, f (f x), ...]
1403 iterate :: (a -> a) -> a -> [a]
1404 iterate f x = x : iterate f (f x)
1406 -- repeat x is an infinite list, with x the value of every element.
1408 repeat x = xs where xs = x:xs
1410 -- replicate n x is a list of length n with x the value of every element
1411 replicate :: Int -> a -> [a]
1412 replicate n x = take n (repeat x)
1414 -- cycle ties a finite list into a circular one, or equivalently,
1415 -- the infinite repetition of the original list. It is the identity
1416 -- on infinite lists.
1419 cycle xs = xs' where xs' = xs ++ xs'
1421 -- take n, applied to a list xs, returns the prefix of xs of length n,
1422 -- or xs itself if n > length xs. drop n xs returns the suffix of xs
1423 -- after the first n elements, or [] if n > length xs. splitAt n xs
1424 -- is equivalent to (take n xs, drop n xs).
1426 take :: Int -> [a] -> [a]
1429 take n (x:xs) | n > 0 = x : take (n-1) xs
1430 take _ _ = error "PreludeList.take: negative argument"
1432 drop :: Int -> [a] -> [a]
1435 drop n (_:xs) | n > 0 = drop (n-1) xs
1436 drop _ _ = error "PreludeList.drop: negative argument"
1438 splitAt :: Int -> [a] -> ([a],[a])
1439 splitAt 0 xs = ([],xs)
1440 splitAt _ [] = ([],[])
1441 splitAt n (x:xs) | n > 0 = (x:xs',xs'') where (xs',xs'') = splitAt (n-1) xs
1442 splitAt _ _ = error "PreludeList.splitAt: negative argument"
1444 -- takeWhile, applied to a predicate p and a list xs, returns the longest
1445 -- prefix (possibly empty) of xs of elements that satisfy p. dropWhile p xs
1446 -- returns the remaining suffix. Span p xs is equivalent to
1447 -- (takeWhile p xs, dropWhile p xs), while break p uses the negation of p.
1449 takeWhile :: (a -> Bool) -> [a] -> [a]
1452 | p x = x : takeWhile p xs
1455 dropWhile :: (a -> Bool) -> [a] -> [a]
1457 dropWhile p xs@(x:xs')
1458 | p x = dropWhile p xs'
1461 span, break :: (a -> Bool) -> [a] -> ([a],[a])
1464 | p x = let (ys,zs) = span p xs' in (x:ys,zs)
1465 | otherwise = ([],xs)
1466 break p = span (not . p)
1468 -- lines breaks a string up into a list of strings at newline characters.
1469 -- The resulting strings do not contain newlines. Similary, words
1470 -- breaks a string up into a list of words, which were delimited by
1471 -- white space. unlines and unwords are the inverse operations.
1472 -- unlines joins lines with terminating newlines, and unwords joins
1473 -- words with separating spaces.
1475 lines :: String -> [String]
1477 lines s = let (l, s') = break (== '\n') s
1480 (_:s'') -> lines s''
1482 words :: String -> [String]
1483 words s = case dropWhile {-partain:Char.-}isSpace s of
1487 break {-partain:Char.-}isSpace s'
1489 unlines :: [String] -> String
1490 unlines = concatMap (++ "\n")
1492 unwords :: [String] -> String
1494 unwords ws = foldr1 (\w s -> w ++ ' ':s) ws
1496 -- reverse xs returns the elements of xs in reverse order. xs must be finite.
1497 reverse :: [a] -> [a]
1498 reverse = foldl (flip (:)) []
1500 -- and returns the conjunction of a Boolean list. For the result to be
1501 -- True, the list must be finite; False, however, results from a False
1502 -- value at a finite index of a finite or infinite list. or is the
1503 -- disjunctive dual of and.
1504 and, or :: [Bool] -> Bool
1505 and = foldr (&&) True
1506 or = foldr (||) False
1508 -- Applied to a predicate and a list, any determines if any element
1509 -- of the list satisfies the predicate. Similarly, for all.
1510 any, all :: (a -> Bool) -> [a] -> Bool
1514 -- elem is the list membership predicate, usually written in infix form,
1515 -- e.g., x `elem` xs. notElem is the negation.
1516 elem, notElem :: (Eq a) => a -> [a] -> Bool
1518 notElem x = all (not . (/= x))
1520 -- lookup key assocs looks up a key in an association list.
1521 lookup :: (Eq a) => a -> [(a,b)] -> Maybe b
1522 lookup key [] = Nothing
1523 lookup key ((x,y):xys)
1525 | otherwise = lookup key xys
1527 -- sum and product compute the sum or product of a finite list of numbers.
1528 sum, product :: (Num a) => [a] -> a
1530 product = foldl (*) 1
1532 -- maximum and minimum return the maximum or minimum value from a list,
1533 -- which must be non-empty, finite, and of an ordered type.
1534 maximum, minimum :: (Ord a) => [a] -> a
1535 maximum [] = error "PreludeList.maximum: empty list"
1536 maximum xs = foldl1 max xs
1538 minimum [] = error "PreludeList.minimum: empty list"
1539 minimum xs = foldl1 min xs
1541 concatMap :: (a -> [b]) -> [a] -> [b]
1542 concatMap f = concat . map f
1544 -- zip takes two lists and returns a list of corresponding pairs. If one
1545 -- input list is short, excess elements of the longer list are discarded.
1546 -- zip3 takes three lists and returns a list of triples. Zips for larger
1547 -- tuples are in the List library
1549 zip :: [a] -> [b] -> [(a,b)]
1552 zip3 :: [a] -> [b] -> [c] -> [(a,b,c)]
1553 zip3 = zipWith3 (,,)
1555 -- The zipWith family generalises the zip family by zipping with the
1556 -- function given as the first argument, instead of a tupling function.
1557 -- For example, zipWith (+) is applied to two lists to produce the list
1558 -- of corresponding sums.
1560 zipWith :: (a->b->c) -> [a]->[b]->[c]
1561 zipWith z (a:as) (b:bs) = z a b : zipWith z as bs
1564 zipWith3 :: (a->b->c->d) -> [a]->[b]->[c]->[d]
1565 zipWith3 z (a:as) (b:bs) (c:cs)
1566 = z a b c : zipWith3 z as bs cs
1567 zipWith3 _ _ _ _ = []
1570 -- unzip transforms a list of pairs into a pair of lists.
1572 unzip :: [(a,b)] -> ([a],[b])
1573 unzip = foldr (\(a,b) ~(as,bs) -> (a:as,b:bs)) ([],[])
1575 unzip3 :: [(a,b,c)] -> ([a],[b],[c])
1576 unzip3 = foldr (\(a,b,c) ~(as,bs,cs) -> (a:as,b:bs,c:cs))
1579 {- module PreludeText -}
1581 type ReadS a = String -> [(a,String)]
1582 type ShowS = String -> String
1585 readsPrec :: Int -> ReadS a
1586 readList :: ReadS [a]
1588 readList = readParen False (\r -> [pr | ("[",s) <- lex r,
1590 where readl s = [([],t) | ("]",t) <- lex s] ++
1591 [(x:xs,u) | (x,t) <- reads s,
1593 readl' s = [([],t) | ("]",t) <- lex s] ++
1594 [(x:xs,v) | (",",t) <- lex s,
1599 showsPrec :: Int -> a -> ShowS
1600 showList :: [a] -> ShowS
1602 showList [] = showString "[]"
1604 = showChar '[' . shows x . showl xs
1605 where showl [] = showChar ']'
1606 showl (x:xs) = showString ", " . shows x . showl xs
1608 reads :: (Read a) => ReadS a
1611 shows :: (Show a) => a -> ShowS
1614 read :: (Read a) => String -> a
1615 read s = case [x | (x,t) <- reads s, ("","") <- lex t] of
1617 [] -> error "PreludeText.read: no parse"
1618 _ -> error "PreludeText.read: ambiguous parse"
1620 show :: (Show a) => a -> String
1623 showChar :: Char -> ShowS
1626 showString :: String -> ShowS
1629 showParen :: Bool -> ShowS -> ShowS
1630 showParen b p = if b then showChar '(' . p . showChar ')' else p
1632 readParen :: Bool -> ReadS a -> ReadS a
1633 readParen b g = if b then mandatory else optional
1634 where optional r = g r ++ mandatory r
1635 mandatory r = [(x,u) | ("(",s) <- lex r,
1636 (x,t) <- optional s,
1639 -- lex: moved to GHCbase
1641 {- module PreludeIO -}
1643 -- in GHCio: type FilePath = String
1645 fail :: IOError -> IO a
1646 fail err = IO $ ST $ \ s -> (Left err, s)
1648 userError :: String -> IOError
1649 userError str = UserError str
1651 catch :: IO a -> (IOError -> IO a) -> IO a
1652 catch (IO (ST m)) k = IO $ ST $ \ s ->
1653 case (m s) of { (r, new_s) ->
1655 Right _ -> (r, new_s)
1656 Left err -> case (k err) of { IO (ST k_err) ->
1659 putChar :: Char -> IO ()
1660 putChar c = hPutChar stdout c
1662 putStr :: String -> IO ()
1663 putStr s = hPutStr stdout s
1665 putStrLn :: String -> IO ()
1666 putStrLn s = do putStr s
1669 print :: Show a => a -> IO ()
1670 print x = putStrLn (show x)
1673 getChar = hGetChar stdin
1675 getLine :: IO String
1676 getLine = do c <- getChar
1677 if c == '\n' then return "" else
1681 getContents :: IO String
1682 getContents = hGetContents stdin
1684 interact :: (String -> String) -> IO ()
1685 interact f = do s <- getContents
1688 readFile :: FilePath -> IO String
1689 readFile name = openFile name ReadMode >>= hGetContents
1691 writeFile :: FilePath -> String -> IO ()
1693 = openFile name WriteMode >>= \hdl -> hPutStr hdl str >> hClose hdl
1695 appendFile :: FilePath -> String -> IO ()
1697 = openFile name AppendMode >>= \hdl -> hPutStr hdl str >> hClose hdl
1699 readIO :: Read a => String -> IO a
1700 -- raises an exception instead of an error
1701 readIO s = case [x | (x,t) <- reads s, ("","") <- lex t] of
1703 [] -> fail (userError "PreludeIO.readIO: no parse")
1704 _ -> fail (userError
1705 "PreludeIO.readIO: ambiguous parse")
1707 readLn :: Read a => IO a
1708 readLn = do l <- getLine