2 % (c) The GRAP/AQUA Project, Glasgow University, 1992-1996
4 \section[PrelBase]{Module @PrelBase@}
8 {-# OPTIONS -fno-implicit-prelude #-}
13 module PrelGHC -- Re-export PrelGHC, to avoid lots of people
14 -- having to import it explicitly
18 import {-# SOURCE #-} PrelErr ( error )
26 infix 4 ==, /=, <, <=, >=, >
34 %*********************************************************
36 \subsection{Standard classes @Eq@, @Ord@, @Bounded@
38 %*********************************************************
42 (==), (/=) :: a -> a -> Bool
47 class (Eq a) => Ord a where
48 compare :: a -> a -> Ordering
49 (<), (<=), (>=), (>):: a -> a -> Bool
50 max, min :: a -> a -> a
52 -- An instance of Ord should define either compare or <=
53 -- Using compare can be more efficient for complex types.
59 x <= y = compare x y /= GT
60 x < y = compare x y == LT
61 x >= y = compare x y /= LT
62 x > y = compare x y == GT
63 max x y = case (compare x y) of { LT -> y ; EQ -> x ; GT -> x }
64 min x y = case (compare x y) of { LT -> x ; EQ -> x ; GT -> y }
67 minBound, maxBound :: a
70 %*********************************************************
72 \subsection{Monadic classes @Functor@, @Monad@ }
74 %*********************************************************
78 fmap :: (a -> b) -> f a -> f b
81 (>>=) :: m a -> (a -> m b) -> m b
82 (>>) :: m a -> m b -> m b
86 m >> k = m >>= \_ -> k
92 %*********************************************************
94 \subsection{Classes @Num@ and @Enum@}
96 %*********************************************************
103 enumFrom :: a -> [a] -- [n..]
104 enumFromThen :: a -> a -> [a] -- [n,n'..]
105 enumFromTo :: a -> a -> [a] -- [n..m]
106 enumFromThenTo :: a -> a -> a -> [a] -- [n,n'..m]
108 succ = toEnum . (+1) . fromEnum
109 pred = toEnum . (+(-1)) . fromEnum
110 enumFromTo n m = map toEnum [fromEnum n .. fromEnum m]
111 enumFromThenTo n n' m
112 = map toEnum [fromEnum n, fromEnum n' .. fromEnum m]
114 class (Eq a, Show a) => Num a where
115 (+), (-), (*) :: a -> a -> a
117 abs, signum :: a -> a
118 fromInteger :: Integer -> a
119 fromInt :: Int -> a -- partain: Glasgow extension
123 fromInt (I# i#) = fromInteger (S# i#)
124 -- Go via the standard class-op if the
125 -- non-standard one ain't provided
135 ord_0 = fromInt (ord '0')
137 {-# SPECIALISE subtract :: Int -> Int -> Int #-}
138 subtract :: (Num a) => a -> a -> a
143 %*********************************************************
145 \subsection{The @Show@ class}
147 %*********************************************************
150 type ShowS = String -> String
153 showsPrec :: Int -> a -> ShowS
155 showList :: [a] -> ShowS
157 showList ls = showList__ (showsPrec 0) ls
158 showsPrec _ x s = show x ++ s
159 show x = showsPrec 0 x ""
162 %*********************************************************
164 \subsection{The list type}
166 %*********************************************************
169 data [] a = [] | a : [a] -- do explicitly: deriving (Eq, Ord)
170 -- to avoid weird names like con2tag_[]#
174 instance (Eq a) => Eq [a] where
176 (x:xs) == (y:ys) = x == y && xs == ys
179 xs /= ys = if (xs == ys) then False else True
181 instance (Ord a) => Ord [a] where
182 a < b = case compare a b of { LT -> True; EQ -> False; GT -> False }
183 a <= b = case compare a b of { LT -> True; EQ -> True; GT -> False }
184 a >= b = case compare a b of { LT -> False; EQ -> True; GT -> True }
185 a > b = case compare a b of { LT -> False; EQ -> False; GT -> True }
187 max a b = case compare a b of { LT -> b; EQ -> a; GT -> a }
188 min a b = case compare a b of { LT -> a; EQ -> a; GT -> b }
191 compare (_:_) [] = GT
192 compare [] (_:_) = LT
193 compare (x:xs) (y:ys) = case compare x y of
198 map :: (a -> b) -> [a] -> [b]
200 map f (x:xs) = f x : map f xs
202 (++) :: [a] -> [a] -> [a]
204 (x:xs) ++ ys = x : (xs ++ ys)
206 instance Functor [] where
209 instance Monad [] where
210 m >>= k = foldr ((++) . k) [] m
211 m >> k = foldr ((++) . (\ _ -> k)) [] m
215 instance (Show a) => Show [a] where
216 showsPrec _ = showList
217 showList ls = showList__ (showsPrec 0) ls
222 A few list functions that appear here because they are used here.
223 The rest of the prelude list functions are in PrelList.
226 foldr :: (a -> b -> b) -> b -> [a] -> b
228 foldr f z (x:xs) = f x (foldr f z xs)
230 -- takeWhile, applied to a predicate p and a list xs, returns the longest
231 -- prefix (possibly empty) of xs of elements that satisfy p. dropWhile p xs
232 -- returns the remaining suffix. Span p xs is equivalent to
233 -- (takeWhile p xs, dropWhile p xs), while break p uses the negation of p.
235 takeWhile :: (a -> Bool) -> [a] -> [a]
238 | p x = x : takeWhile p xs
241 dropWhile :: (a -> Bool) -> [a] -> [a]
243 dropWhile p xs@(x:xs')
244 | p x = dropWhile p xs'
247 -- List index (subscript) operator, 0-origin
248 (!!) :: [a] -> Int -> a
249 #ifdef USE_REPORT_PRELUDE
251 (_:xs) !! n | n > 0 = xs !! (n-1)
252 (_:_) !! _ = error "Prelude.(!!): negative index"
253 [] !! _ = error "Prelude.(!!): index too large"
255 -- HBC version (stolen), then unboxified
256 -- The semantics is not quite the same for error conditions
257 -- in the more efficient version.
259 _ !! n | n < 0 = error "Prelude.(!!): negative index\n"
260 xs !! n = sub xs (case n of { I# n# -> n# })
261 where sub :: [a] -> Int# -> a
262 sub [] _ = error "Prelude.(!!): index too large\n"
263 sub (y:ys) n# = if n# ==# 0#
265 else sub ys (n# -# 1#)
270 %*********************************************************
272 \subsection{Type @Bool@}
274 %*********************************************************
277 data Bool = False | True deriving (Eq, Ord, Enum, Bounded, Show {- Read -})
281 (&&), (||) :: Bool -> Bool -> Bool
296 %*********************************************************
298 \subsection{The @()@ type}
300 %*********************************************************
302 The Unit type is here because virtually any program needs it (whereas
303 some programs may get away without consulting PrelTup). Furthermore,
304 the renamer currently *always* asks for () to be in scope, so that
305 ccalls can use () as their default type; so when compiling PrelBase we
306 need (). (We could arrange suck in () only if -fglasgow-exts, but putting
307 it here seems more direct.
310 data () = () --easier to do explicitly: deriving (Eq, Ord, Enum, Show, Bounded)
311 -- (avoids weird-named functions, e.g., con2tag_()#
317 instance Ord () where
326 instance Enum () where
327 succ x = error "Prelude.Enum.succ{()}: not possible"
328 pred x = error "Prelude.Enum.pred{()}: not possible"
330 toEnum _ = error "Prelude.Enum.toEnum{()}: argument not 0"
333 enumFromThen () () = [()]
334 enumFromTo () () = [()]
335 enumFromThenTo () () () = [()]
337 instance Show () where
338 showsPrec _ () = showString "()"
339 showList ls = showList__ (showsPrec 0) ls
342 %*********************************************************
344 \subsection{Type @Ordering@}
346 %*********************************************************
349 data Ordering = LT | EQ | GT deriving (Eq, Ord, Enum, Bounded, Show {- in PrelRead: Read -})
353 %*********************************************************
355 \subsection{Type @Char@ and @String@}
357 %*********************************************************
362 data Char = C# Char# deriving (Eq, Ord)
364 instance Enum Char where
366 | not (ord# c# ==# 255#) = C# (chr# (ord# c# +# 1#))
367 | otherwise = error ("Prelude.Enum.succ{Char}: tried to take `succ' of maxBound")
369 | not (ord# c# ==# 0#) = C# (chr# (ord# c# -# 1#))
370 | otherwise = error ("Prelude.Enum.pred{Char}: tried to to take `pred' of minBound")
372 toEnum (I# i) | i >=# 0# && i <=# 255# = C# (chr# i)
373 | otherwise = error ("Prelude.Enum.toEnum{Char}: out of range: " ++ show (I# i))
374 fromEnum (C# c) = I# (ord# c)
376 enumFrom (C# c) = efttCh (ord# c) 1# (># 255#)
377 enumFromTo (C# c1) (C# c2)
378 | c1 `leChar#` c2 = efttCh (ord# c1) 1# (># (ord# c2))
381 enumFromThen (C# c1) (C# c2)
382 | c1 `leChar#` c2 = efttCh (ord# c1) (ord# c2 -# ord# c1) (># 255#)
383 | otherwise = efttCh (ord# c1) (ord# c2 -# ord# c1) (<# 0#)
385 enumFromThenTo (C# c1) (C# c2) (C# c3)
386 | c1 `leChar#` c2 = efttCh (ord# c1) (ord# c2 -# ord# c1) (># (ord# c3))
387 | otherwise = efttCh (ord# c1) (ord# c2 -# ord# c1) (<# (ord# c3))
389 efttCh :: Int# -> Int# -> (Int# -> Bool) -> [Char]
390 efttCh init step done
393 go now | done now = []
394 | otherwise = C# (chr# now) : go (now +# step)
396 instance Show Char where
397 showsPrec _ '\'' = showString "'\\''"
398 showsPrec _ c = showChar '\'' . showLitChar c . showChar '\''
400 showList cs = showChar '"' . showl cs
401 where showl "" = showChar '"'
402 showl ('"':xs) = showString "\\\"" . showl xs
403 showl (x:xs) = showLitChar x . showl xs
408 isAscii, isLatin1, isControl, isPrint, isSpace, isUpper,
409 isLower, isAlpha, isDigit, isOctDigit, isHexDigit, isAlphaNum :: Char -> Bool
410 isAscii c = c < '\x80'
411 isLatin1 c = c <= '\xff'
412 isControl c = c < ' ' || c >= '\DEL' && c <= '\x9f'
413 isPrint c = not (isControl c)
415 -- isSpace includes non-breaking space
416 -- Done with explicit equalities both for efficiency, and to avoid a tiresome
417 -- recursion with PrelList elem
418 isSpace c = c == ' ' ||
426 -- The upper case ISO characters have the multiplication sign dumped
427 -- randomly in the middle of the range. Go figure.
428 isUpper c = c >= 'A' && c <= 'Z' ||
429 c >= '\xC0' && c <= '\xD6' ||
430 c >= '\xD8' && c <= '\xDE'
431 -- The lower case ISO characters have the division sign dumped
432 -- randomly in the middle of the range. Go figure.
433 isLower c = c >= 'a' && c <= 'z' ||
434 c >= '\xDF' && c <= '\xF6' ||
435 c >= '\xF8' && c <= '\xFF'
436 isAsciiLower c = c >= 'a' && c <= 'z'
437 isAsciiUpper c = c >= 'A' && c <= 'Z'
439 isAlpha c = isLower c || isUpper c
440 isDigit c = c >= '0' && c <= '9'
441 isOctDigit c = c >= '0' && c <= '7'
442 isHexDigit c = isDigit c || c >= 'A' && c <= 'F' ||
444 isAlphaNum c = isAlpha c || isDigit c
446 -- Case-changing operations
448 toUpper, toLower :: Char -> Char
450 | isAsciiLower c = C# (chr# (ord# c# -# 32#))
452 -- fall-through to the slower stuff.
453 | isLower c && c /= '\xDF' && c /= '\xFF'
454 = toEnum (fromEnum c - fromEnum 'a' + fromEnum 'A')
461 | isAsciiUpper c = C# (chr# (ord# c# +# 32#))
463 | isUpper c = toEnum (fromEnum c - fromEnum 'A'
468 asciiTab = -- Using an array drags in the array module. listArray ('\NUL', ' ')
469 ["NUL", "SOH", "STX", "ETX", "EOT", "ENQ", "ACK", "BEL",
470 "BS", "HT", "LF", "VT", "FF", "CR", "SO", "SI",
471 "DLE", "DC1", "DC2", "DC3", "DC4", "NAK", "SYN", "ETB",
472 "CAN", "EM", "SUB", "ESC", "FS", "GS", "RS", "US",
476 %*********************************************************
478 \subsection{Type @Int@}
480 %*********************************************************
485 instance Eq Int where
486 (==) x y = x `eqInt` y
487 (/=) x y = x `neInt` y
489 instance Ord Int where
490 compare x y = compareInt x y
496 max x y = case (compareInt x y) of { LT -> y ; EQ -> x ; GT -> x }
497 min x y = case (compareInt x y) of { LT -> x ; EQ -> x ; GT -> y }
499 compareInt :: Int -> Int -> Ordering
500 (I# x) `compareInt` (I# y) | x <# y = LT
504 instance Bounded Int where
505 minBound = -2147483648 -- GHC <= 2.09 had this at -2147483647
506 maxBound = 2147483647
508 instance Enum Int where
510 | x == maxBound = error "Prelude.Enum.succ{Int}: tried to take `succ' of maxBound"
513 | x == minBound = error "Prelude.Enum.pred{Int}: tried to take `pred' of minBound"
519 #ifndef USE_FOLDR_BUILD
520 enumFrom (I# c) = efttInt True c 1# (\ _ -> False)
522 enumFromTo (I# c1) (I# c2)
523 | c1 <=# c2 = efttInt True c1 1# (># c2)
526 enumFromThen (I# c1) (I# c2)
527 | c1 <# c2 = efttInt True c1 (c2 -# c1) (\ _ -> False)
528 | otherwise = efttInt False c1 (c2 -# c1) (\ _ -> False)
530 enumFromThenTo (I# c1) (I# c2) (I# c3)
531 | c1 <=# c2 = efttInt True c1 (c2 -# c1) (># c3)
532 | otherwise = efttInt False c1 (c2 -# c1) (<# c3)
535 {-# INLINE enumFrom #-}
536 {-# INLINE enumFromTo #-}
537 enumFrom x = build (\ c _ ->
538 let g x = x `c` g (x `plusInt` 1) in g x)
539 enumFromTo x y = build (\ c n ->
540 let g x = if x <= y then x `c` g (x `plusInt` 1) else n in g x)
543 efttInt :: Bool -> Int# -> Int# -> (Int# -> Bool) -> [Int]
544 efttInt increasing init step done = go init
548 | increasing && now ># nxt = [I# now] -- overflowed
549 | not increasing && now <# nxt = [I# now] -- underflowed
550 | otherwise = I# now : go nxt
554 instance Num Int where
555 (+) x y = plusInt x y
556 (-) x y = minusInt x y
557 negate x = negateInt x
558 (*) x y = timesInt x y
559 abs n = if n `geInt` 0 then n else (negateInt n)
561 signum n | n `ltInt` 0 = negateInt 1
565 fromInteger (S# i#) = I# i#
566 fromInteger (J# s# d#)
567 = case (integer2Int# s# d#) of { i# -> I# i# }
571 instance Show Int where
572 showsPrec p n = showSignedInt p n
573 showList ls = showList__ (showsPrec 0) ls
577 %*********************************************************
579 \subsection{Type @Integer@, @Float@, @Double@}
581 %*********************************************************
584 data Float = F# Float#
585 data Double = D# Double#
588 = S# Int# -- small integers
589 | J# Int# ByteArray# -- large integers
591 instance Eq Integer where
592 (S# i) == (S# j) = i ==# j
593 (S# i) == (J# s d) = cmpIntegerInt# s d i ==# 0#
594 (J# s d) == (S# i) = cmpIntegerInt# s d i ==# 0#
595 (J# s1 d1) == (J# s2 d2) = (cmpInteger# s1 d1 s2 d2) ==# 0#
597 (S# i) /= (S# j) = i /=# j
598 (S# i) /= (J# s d) = cmpIntegerInt# s d i /=# 0#
599 (J# s d) /= (S# i) = cmpIntegerInt# s d i /=# 0#
600 (J# s1 d1) /= (J# s2 d2) = (cmpInteger# s1 d1 s2 d2) /=# 0#
603 %*********************************************************
605 \subsection{The function type}
607 %*********************************************************
618 -- function composition
620 (.) :: (b -> c) -> (a -> b) -> a -> c
623 -- flip f takes its (first) two arguments in the reverse order of f.
624 flip :: (a -> b -> c) -> b -> a -> c
627 -- right-associating infix application operator (useful in continuation-
629 ($) :: (a -> b) -> a -> b
632 -- until p f yields the result of applying f until p holds.
633 until :: (a -> Bool) -> (a -> a) -> a -> a
634 until p f x | p x = x
635 | otherwise = until p f (f x)
637 -- asTypeOf is a type-restricted version of const. It is usually used
638 -- as an infix operator, and its typing forces its first argument
639 -- (which is usually overloaded) to have the same type as the second.
640 asTypeOf :: a -> a -> a
644 %*********************************************************
646 \subsection{Support code for @Show@}
648 %*********************************************************
651 shows :: (Show a) => a -> ShowS
654 showChar :: Char -> ShowS
657 showString :: String -> ShowS
660 showParen :: Bool -> ShowS -> ShowS
661 showParen b p = if b then showChar '(' . p . showChar ')' else p
663 showList__ :: (a -> ShowS) -> [a] -> ShowS
665 showList__ _ [] = showString "[]"
666 showList__ showx (x:xs) = showChar '[' . showx x . showl xs
668 showl [] = showChar ']'
669 showl (y:ys) = showChar ',' . showx y . showl ys
672 showSpace = {-showChar ' '-} \ xs -> ' ' : xs
675 Code specific for characters
678 showLitChar :: Char -> ShowS
679 showLitChar c | c > '\DEL' = showChar '\\' . protectEsc isDigit (shows (ord c))
680 showLitChar '\DEL' = showString "\\DEL"
681 showLitChar '\\' = showString "\\\\"
682 showLitChar c | c >= ' ' = showChar c
683 showLitChar '\a' = showString "\\a"
684 showLitChar '\b' = showString "\\b"
685 showLitChar '\f' = showString "\\f"
686 showLitChar '\n' = showString "\\n"
687 showLitChar '\r' = showString "\\r"
688 showLitChar '\t' = showString "\\t"
689 showLitChar '\v' = showString "\\v"
690 showLitChar '\SO' = protectEsc (== 'H') (showString "\\SO")
691 showLitChar c = showString ('\\' : asciiTab!!ord c)
693 protectEsc :: (Char -> Bool) -> ShowS -> ShowS
694 protectEsc p f = f . cont
695 where cont s@(c:_) | p c = "\\&" ++ s
698 intToDigit :: Int -> Char
700 | i >= 0 && i <= 9 = toEnum (fromEnum '0' + i)
701 | i >= 10 && i <= 15 = toEnum (fromEnum 'a' + i - 10)
702 | otherwise = error ("Char.intToDigit: not a digit " ++ show i)
706 Code specific for Ints.
709 showSignedInt :: Int -> Int -> ShowS
710 showSignedInt p (I# n) r
711 | n <# 0# && p > 6 = '(':itos n (')':r)
712 | otherwise = itos n r
714 itos :: Int# -> String -> String
716 | n >=# 0# = itos' n r
717 | negateInt# n <# 0# = -- n is minInt, a difficult number
718 itos (n `quotInt#` 10#) (itos' (negateInt# (n `remInt#` 10#)) r)
719 | otherwise = '-':itos' (negateInt# n) r
721 itos' :: Int# -> String -> String
723 | x <# 10# = C# (chr# (x +# ord# '0'#)) : cs
724 | otherwise = itos' (x `quotInt#` 10#)
725 (C# (chr# (x `remInt#` 10# +# ord# '0'#)) : cs)
728 %*********************************************************
730 \subsection{Numeric primops}
732 %*********************************************************
734 Definitions of the boxed PrimOps; these will be
735 used in the case of partial applications, etc.
741 plusInt, minusInt, timesInt, quotInt, remInt :: Int -> Int -> Int
742 plusInt (I# x) (I# y) = I# (x +# y)
743 minusInt(I# x) (I# y) = I# (x -# y)
744 timesInt(I# x) (I# y) = I# (x *# y)
745 quotInt (I# x) (I# y) = I# (quotInt# x y)
746 remInt (I# x) (I# y) = I# (remInt# x y)
748 negateInt :: Int -> Int
749 negateInt (I# x) = I# (negateInt# x)
751 gtInt, geInt, eqInt, neInt, ltInt, leInt :: Int -> Int -> Bool
752 gtInt (I# x) (I# y) = x ># y
753 geInt (I# x) (I# y) = x >=# y
754 eqInt (I# x) (I# y) = x ==# y
755 neInt (I# x) (I# y) = x /=# y
756 ltInt (I# x) (I# y) = x <# y
757 leInt (I# x) (I# y) = x <=# y
760 Convenient boxed Integer PrimOps. These are 'thin-air' Ids, so
761 it's nice to have them in PrelBase.
764 {-# INLINE int2Integer #-}
765 {-# INLINE addr2Integer #-}
766 int2Integer :: Int# -> Integer
768 addr2Integer :: Addr# -> Integer
769 addr2Integer x = case addr2Integer# x of (# s, d #) -> J# s d