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 (case int2Integer# i# of
124 (# a, s, d #) -> J# a s d)
125 -- Go via the standard class-op if the
126 -- non-standard one ain't provided
136 ord_0 = fromInt (ord '0')
138 {-# SPECIALISE subtract :: Int -> Int -> Int #-}
139 subtract :: (Num a) => a -> a -> a
144 %*********************************************************
146 \subsection{The @Show@ class}
148 %*********************************************************
151 type ShowS = String -> String
154 showsPrec :: Int -> a -> ShowS
156 showList :: [a] -> ShowS
158 showList ls = showList__ (showsPrec 0) ls
159 showsPrec _ x s = show x ++ s
160 show x = showsPrec 0 x ""
163 %*********************************************************
165 \subsection{The list type}
167 %*********************************************************
170 data [] a = [] | a : [a] -- do explicitly: deriving (Eq, Ord)
171 -- to avoid weird names like con2tag_[]#
175 instance (Eq a) => Eq [a] where
177 (x:xs) == (y:ys) = x == y && xs == ys
180 xs /= ys = if (xs == ys) then False else True
182 instance (Ord a) => Ord [a] where
183 a < b = case compare a b of { LT -> True; EQ -> False; GT -> False }
184 a <= b = case compare a b of { LT -> True; EQ -> True; GT -> False }
185 a >= b = case compare a b of { LT -> False; EQ -> True; GT -> True }
186 a > b = case compare a b of { LT -> False; EQ -> False; GT -> True }
188 max a b = case compare a b of { LT -> b; EQ -> a; GT -> a }
189 min a b = case compare a b of { LT -> a; EQ -> a; GT -> b }
192 compare (_:_) [] = GT
193 compare [] (_:_) = LT
194 compare (x:xs) (y:ys) = case compare x y of
199 map :: (a -> b) -> [a] -> [b]
201 map f (x:xs) = f x : map f xs
203 (++) :: [a] -> [a] -> [a]
205 (x:xs) ++ ys = x : (xs ++ ys)
207 instance Functor [] where
210 instance Monad [] where
211 m >>= k = foldr ((++) . k) [] m
212 m >> k = foldr ((++) . (\ _ -> k)) [] m
216 instance (Show a) => Show [a] where
217 showsPrec _ = showList
218 showList ls = showList__ (showsPrec 0) ls
223 A few list functions that appear here because they are used here.
224 The rest of the prelude list functions are in PrelList.
227 foldr :: (a -> b -> b) -> b -> [a] -> b
229 foldr f z (x:xs) = f x (foldr f z xs)
231 -- takeWhile, applied to a predicate p and a list xs, returns the longest
232 -- prefix (possibly empty) of xs of elements that satisfy p. dropWhile p xs
233 -- returns the remaining suffix. Span p xs is equivalent to
234 -- (takeWhile p xs, dropWhile p xs), while break p uses the negation of p.
236 takeWhile :: (a -> Bool) -> [a] -> [a]
239 | p x = x : takeWhile p xs
242 dropWhile :: (a -> Bool) -> [a] -> [a]
244 dropWhile p xs@(x:xs')
245 | p x = dropWhile p xs'
248 -- List index (subscript) operator, 0-origin
249 (!!) :: [a] -> Int -> a
250 #ifdef USE_REPORT_PRELUDE
252 (_:xs) !! n | n > 0 = xs !! (n-1)
253 (_:_) !! _ = error "Prelude.(!!): negative index"
254 [] !! _ = error "Prelude.(!!): index too large"
256 -- HBC version (stolen), then unboxified
257 -- The semantics is not quite the same for error conditions
258 -- in the more efficient version.
260 _ !! n | n < 0 = error "Prelude.(!!): negative index\n"
261 xs !! n = sub xs (case n of { I# n# -> n# })
262 where sub :: [a] -> Int# -> a
263 sub [] _ = error "Prelude.(!!): index too large\n"
264 sub (y:ys) n# = if n# ==# 0#
266 else sub ys (n# -# 1#)
271 %*********************************************************
273 \subsection{Type @Bool@}
275 %*********************************************************
278 data Bool = False | True deriving (Eq, Ord, Enum, Bounded, Show {- Read -})
282 (&&), (||) :: Bool -> Bool -> Bool
297 %*********************************************************
299 \subsection{The @()@ type}
301 %*********************************************************
303 The Unit type is here because virtually any program needs it (whereas
304 some programs may get away without consulting PrelTup). Furthermore,
305 the renamer currently *always* asks for () to be in scope, so that
306 ccalls can use () as their default type; so when compiling PrelBase we
307 need (). (We could arrange suck in () only if -fglasgow-exts, but putting
308 it here seems more direct.
311 data () = () --easier to do explicitly: deriving (Eq, Ord, Enum, Show, Bounded)
312 -- (avoids weird-named functions, e.g., con2tag_()#
318 instance Ord () where
327 instance Enum () where
331 toEnum _ = error "Prelude.Enum.().toEnum: argument not 0"
334 enumFromThen () () = [()]
335 enumFromTo () () = [()]
336 enumFromThenTo () () () = [()]
338 instance Show () where
339 showsPrec _ () = showString "()"
340 showList ls = showList__ (showsPrec 0) ls
343 %*********************************************************
345 \subsection{Type @Ordering@}
347 %*********************************************************
350 data Ordering = LT | EQ | GT deriving (Eq, Ord, Enum, Bounded, Show {- Read -})
354 %*********************************************************
356 \subsection{Type @Char@ and @String@}
358 %*********************************************************
363 data Char = C# Char# deriving (Eq, Ord)
365 instance Enum Char where
367 | not (ord# c# ==# 255#) = C# (chr# (ord# c# +# 1#))
368 | otherwise = error ("Prelude.Enum{Char}.succ: out of range " ++ show c)
370 | not (ord# c# ==# 0#) = C# (chr# (ord# c# -# 1#))
371 | otherwise = error ("Prelude.Enum{Char}.succ: out of range " ++ show c)
373 toEnum (I# i) | i >=# 0# && i <=# 255# = C# (chr# i)
374 | otherwise = error ("Prelude.Enum.Char.toEnum:out of range: " ++ show (I# i))
375 fromEnum (C# c) = I# (ord# c)
377 enumFrom (C# c) = efttCh (ord# c) 1# (># 255#)
378 enumFromTo (C# c1) (C# c2) = efttCh (ord# c1) 1# (># (ord# c2))
380 enumFromThen (C# c1) (C# c2)
381 | c1 `leChar#` c2 = efttCh (ord# c1) (ord# c2 -# ord# c1) (># 255#)
382 | otherwise = efttCh (ord# c1) (ord# c2 -# ord# c1) (<# 0#)
384 enumFromThenTo (C# c1) (C# c2) (C# c3)
385 | c1 `leChar#` c2 = efttCh (ord# c1) (ord# c2 -# ord# c1) (># (ord# c3))
386 | otherwise = efttCh (ord# c1) (ord# c2 -# ord# c1) (<# (ord# c3))
388 efttCh :: Int# -> Int# -> (Int# -> Bool) -> [Char]
389 efttCh init step done
392 go now | done now = []
393 | otherwise = C# (chr# now) : go (now +# step)
395 instance Show Char where
396 showsPrec _ '\'' = showString "'\\''"
397 showsPrec _ c = showChar '\'' . showLitChar c . showChar '\''
399 showList cs = showChar '"' . showl cs
400 where showl "" = showChar '"'
401 showl ('"':xs) = showString "\\\"" . showl xs
402 showl (x:xs) = showLitChar x . showl xs
407 isAscii, isLatin1, isControl, isPrint, isSpace, isUpper,
408 isLower, isAlpha, isDigit, isOctDigit, isHexDigit, isAlphaNum :: Char -> Bool
409 isAscii c = c < '\x80'
410 isLatin1 c = c <= '\xff'
411 isControl c = c < ' ' || c >= '\DEL' && c <= '\x9f'
412 isPrint c = not (isControl c)
414 -- isSpace includes non-breaking space
415 -- Done with explicit equalities both for efficiency, and to avoid a tiresome
416 -- recursion with PrelList elem
417 isSpace c = c == ' ' ||
425 -- The upper case ISO characters have the multiplication sign dumped
426 -- randomly in the middle of the range. Go figure.
427 isUpper c = c >= 'A' && c <= 'Z' ||
428 c >= '\xC0' && c <= '\xD6' ||
429 c >= '\xD8' && c <= '\xDE'
430 -- The lower case ISO characters have the division sign dumped
431 -- randomly in the middle of the range. Go figure.
432 isLower c = c >= 'a' && c <= 'z' ||
433 c >= '\xDF' && c <= '\xF6' ||
434 c >= '\xF8' && c <= '\xFF'
435 isAlpha c = isLower c || isUpper c
436 isDigit c = c >= '0' && c <= '9'
437 isOctDigit c = c >= '0' && c <= '7'
438 isHexDigit c = isDigit c || c >= 'A' && c <= 'F' ||
440 isAlphaNum c = isAlpha c || isDigit c
442 -- Case-changing operations
444 toUpper, toLower :: Char -> Char
446 | isLower c && c /= '\xDF' && c /= '\xFF'
447 = toEnum (fromEnum c - fromEnum 'a' + fromEnum 'A')
451 toLower c | isUpper c = toEnum (fromEnum c - fromEnum 'A'
456 asciiTab = -- Using an array drags in the array module. listArray ('\NUL', ' ')
457 ["NUL", "SOH", "STX", "ETX", "EOT", "ENQ", "ACK", "BEL",
458 "BS", "HT", "LF", "VT", "FF", "CR", "SO", "SI",
459 "DLE", "DC1", "DC2", "DC3", "DC4", "NAK", "SYN", "ETB",
460 "CAN", "EM", "SUB", "ESC", "FS", "GS", "RS", "US",
464 %*********************************************************
466 \subsection{Type @Int@}
468 %*********************************************************
473 instance Eq Int where
474 (==) x y = x `eqInt` y
475 (/=) x y = x `neInt` y
477 instance Ord Int where
478 compare x y = compareInt x y
484 max x y = case (compareInt x y) of { LT -> y ; EQ -> x ; GT -> x }
485 min x y = case (compareInt x y) of { LT -> x ; EQ -> x ; GT -> y }
487 compareInt :: Int -> Int -> Ordering
488 (I# x) `compareInt` (I# y) | x <# y = LT
492 instance Enum Int where
498 #ifndef USE_FOLDR_BUILD
499 enumFrom (I# c) = eftInt c 1#
500 enumFromTo (I# c1) (I# c2) = efttInt c1 1# (># c2)
501 enumFromThen (I# c1) (I# c2) = eftInt c1 (c2 -# c1)
503 enumFromThenTo (I# c1) (I# c2) (I# c3)
504 | c1 <=# c2 = efttInt c1 (c2 -# c1) (># c3)
505 | otherwise = efttInt c1 (c2 -# c1) (<# c3)
508 {-# INLINE enumFrom #-}
509 {-# INLINE enumFromTo #-}
510 enumFrom x = build (\ c _ ->
511 let g x = x `c` g (x `plusInt` 1) in g x)
512 enumFromTo x y = build (\ c n ->
513 let g x = if x <= y then x `c` g (x `plusInt` 1) else n in g x)
516 efttInt :: Int# -> Int# -> (Int# -> Bool) -> [Int]
517 efttInt init step done
520 go now | done now = []
521 | otherwise = I# now : go (now +# step)
523 eftInt :: Int# -> Int# -> [Int]
527 go now = I# now : go (now +# step)
530 instance Num Int where
531 (+) x y = plusInt x y
532 (-) x y = minusInt x y
533 negate x = negateInt x
534 (*) x y = timesInt x y
535 abs n = if n `geInt` 0 then n else (negateInt n)
537 signum n | n `ltInt` 0 = negateInt 1
541 fromInteger (J# a# s# d#)
542 = case (integer2Int# a# s# d#) of { i# -> I# i# }
546 instance Show Int where
547 showsPrec p n = showSignedInt p n
548 showList ls = showList__ (showsPrec 0) ls
552 %*********************************************************
554 \subsection{Type @Integer@, @Float@, @Double@}
556 %*********************************************************
559 data Float = F# Float#
560 data Double = D# Double#
561 data Integer = J# Int# Int# ByteArray#
563 instance Eq Integer where
564 (J# a1 s1 d1) == (J# a2 s2 d2)
565 = (cmpInteger# a1 s1 d1 a2 s2 d2) ==# 0#
567 (J# a1 s1 d1) /= (J# a2 s2 d2)
568 = (cmpInteger# a1 s1 d1 a2 s2 d2) /=# 0#
572 %*********************************************************
574 \subsection{The function type}
576 %*********************************************************
579 instance Show (a -> b) where
580 showsPrec _ _ = showString "<<function>>"
581 showList ls = showList__ (showsPrec 0) ls
592 -- function composition
594 (.) :: (b -> c) -> (a -> b) -> a -> c
597 -- flip f takes its (first) two arguments in the reverse order of f.
598 flip :: (a -> b -> c) -> b -> a -> c
601 -- right-associating infix application operator (useful in continuation-
603 ($) :: (a -> b) -> a -> b
606 -- until p f yields the result of applying f until p holds.
607 until :: (a -> Bool) -> (a -> a) -> a -> a
608 until p f x | p x = x
609 | otherwise = until p f (f x)
611 -- asTypeOf is a type-restricted version of const. It is usually used
612 -- as an infix operator, and its typing forces its first argument
613 -- (which is usually overloaded) to have the same type as the second.
614 asTypeOf :: a -> a -> a
618 %*********************************************************
620 \subsection{Support code for @Show@}
622 %*********************************************************
625 shows :: (Show a) => a -> ShowS
628 showChar :: Char -> ShowS
631 showString :: String -> ShowS
634 showParen :: Bool -> ShowS -> ShowS
635 showParen b p = if b then showChar '(' . p . showChar ')' else p
637 showList__ :: (a -> ShowS) -> [a] -> ShowS
639 showList__ _ [] = showString "[]"
640 showList__ showx (x:xs) = showChar '[' . showx x . showl xs
642 showl [] = showChar ']'
643 showl (y:ys) = showChar ',' . showx y . showl ys
646 showSpace = {-showChar ' '-} \ xs -> ' ' : xs
649 Code specific for characters
652 showLitChar :: Char -> ShowS
653 showLitChar c | c > '\DEL' = showChar '\\' . protectEsc isDigit (shows (ord c))
654 showLitChar '\DEL' = showString "\\DEL"
655 showLitChar '\\' = showString "\\\\"
656 showLitChar c | c >= ' ' = showChar c
657 showLitChar '\a' = showString "\\a"
658 showLitChar '\b' = showString "\\b"
659 showLitChar '\f' = showString "\\f"
660 showLitChar '\n' = showString "\\n"
661 showLitChar '\r' = showString "\\r"
662 showLitChar '\t' = showString "\\t"
663 showLitChar '\v' = showString "\\v"
664 showLitChar '\SO' = protectEsc (== 'H') (showString "\\SO")
665 showLitChar c = showString ('\\' : asciiTab!!ord c)
667 protectEsc :: (Char -> Bool) -> ShowS -> ShowS
668 protectEsc p f = f . cont
669 where cont s@(c:_) | p c = "\\&" ++ s
672 intToDigit :: Int -> Char
674 | i >= 0 && i <= 9 = toEnum (fromEnum '0' + i)
675 | i >= 10 && i <= 15 = toEnum (fromEnum 'a' + i - 10)
676 | otherwise = error ("Char.intToDigit: not a digit " ++ show i)
680 Code specific for Ints.
683 showSignedInt :: Int -> Int -> ShowS
684 showSignedInt p (I# n) r
685 | n <# 0# && p > 6 = '(':itos n (')':r)
686 | otherwise = itos n r
688 itos :: Int# -> String -> String
690 | n >=# 0# = itos' n r
691 | negateInt# n <# 0# = -- n is minInt, a difficult number
692 itos (n `quotInt#` 10#) (itos' (negateInt# (n `remInt#` 10#)) r)
693 | otherwise = '-':itos' (negateInt# n) r
695 itos' :: Int# -> String -> String
697 | x <# 10# = C# (chr# (x +# ord# '0'#)) : cs
698 | otherwise = itos' (x `quotInt#` 10#)
699 (C# (chr# (x `remInt#` 10# +# ord# '0'#)) : cs)
702 %*********************************************************
704 \subsection{Numeric primops}
706 %*********************************************************
708 Definitions of the boxed PrimOps; these will be
709 used in the case of partial applications, etc.
715 plusInt, minusInt, timesInt, quotInt, remInt :: Int -> Int -> Int
716 plusInt (I# x) (I# y) = I# (x +# y)
717 minusInt(I# x) (I# y) = I# (x -# y)
718 timesInt(I# x) (I# y) = I# (x *# y)
719 quotInt (I# x) (I# y) = I# (quotInt# x y)
720 remInt (I# x) (I# y) = I# (remInt# x y)
722 negateInt :: Int -> Int
723 negateInt (I# x) = I# (negateInt# x)
725 gtInt, geInt, eqInt, neInt, ltInt, leInt :: Int -> Int -> Bool
726 gtInt (I# x) (I# y) = x ># y
727 geInt (I# x) (I# y) = x >=# y
728 eqInt (I# x) (I# y) = x ==# y
729 neInt (I# x) (I# y) = x /=# y
730 ltInt (I# x) (I# y) = x <# y
731 leInt (I# x) (I# y) = x <=# y
734 Convenient boxed Integer PrimOps. These are 'thin-air' Ids, so
735 it's nice to have them in PrelBase.
738 {-# INLINE int2Integer #-}
739 {-# INLINE addr2Integer #-}
740 int2Integer :: Int# -> Integer
741 int2Integer i = case int2Integer# i of (# a, s, d #) -> J# a s d
742 addr2Integer :: Addr# -> Integer
743 addr2Integer x = case addr2Integer# x of (# a, s, d #) -> J# a s d
745 integer_0, integer_1, integer_2, integer_m1 :: Integer
746 integer_0 = int2Integer 0#
747 integer_1 = int2Integer 1#
748 integer_2 = int2Integer 2#
749 integer_m1 = int2Integer (negateInt# 1#)