2 % (c) The GRASP/AQUA Project, Glasgow University, 1992-1996
4 \section[PrelBase]{Module @PrelBase@}
8 {-# OPTIONS -fno-implicit-prelude #-}
12 module GHC -- Re-export GHC, to avoid lots of people having
13 -- to import it explicitly
16 import {-# SOURCE #-} IOBase ( error )
24 infix 4 ==, /=, <, <=, >=, >
31 %*********************************************************
33 \subsection{Standard classes @Eq@, @Ord@, @Bounded@, @Eval@}
35 %*********************************************************
39 (==), (/=) :: a -> a -> Bool
43 class (Eq a) => Ord a where
44 compare :: a -> a -> Ordering
45 (<), (<=), (>=), (>):: a -> a -> Bool
46 max, min :: a -> a -> a
48 -- An instance of Ord should define either compare or <=
49 -- Using compare can be more efficient for complex types.
55 x <= y = compare x y /= GT
56 x < y = compare x y == LT
57 x >= y = compare x y /= LT
58 x > y = compare x y == GT
59 max x y = case (compare x y) of { LT -> y ; EQ -> x ; GT -> x }
60 min x y = case (compare x y) of { LT -> x ; EQ -> x ; GT -> y }
63 minBound, maxBound :: a
68 %*********************************************************
70 \subsection{Monadic classes @Functor@, @Monad@, @MonadZero@, @MonadPlus@}
72 %*********************************************************
76 map :: (a -> b) -> f a -> f b
79 (>>=) :: m a -> (a -> m b) -> m b
80 (>>) :: m a -> m b -> m b
83 m >> k = m >>= \_ -> k
85 class (Monad m) => MonadZero m where
88 class (MonadZero m) => MonadPlus m where
89 (++) :: m a -> m a -> m a
93 %*********************************************************
95 \subsection{Classes @Num@ and @Enum@}
97 %*********************************************************
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 enumFromTo n m = map toEnum [fromEnum n .. fromEnum m]
109 enumFromThenTo n n' m
110 = map toEnum [fromEnum n, fromEnum n' .. fromEnum m]
112 class (Eq a, Show a, Eval a) => Num a where
113 (+), (-), (*) :: a -> a -> a
115 abs, signum :: a -> a
116 fromInteger :: Integer -> a
117 fromInt :: Int -> a -- partain: Glasgow extension
120 fromInt (I# i#) = fromInteger (int2Integer# i#)
121 -- Go via the standard class-op if the
122 -- non-standard one ain't provided
126 succ, pred :: Enum a => a -> a
127 succ = toEnum . (+1) . fromEnum
128 pred = toEnum . (subtract 1) . fromEnum
130 chr = (toEnum :: Int -> Char)
131 ord = (fromEnum :: Char -> Int)
134 ord_0 = fromInt (ord '0')
136 {-# GENERATE_SPECS subtract a{Int#,Double#,Int,Double,Complex(Double#),Complex(Double)} #-}
137 subtract :: (Num a) => a -> a -> a
142 %*********************************************************
144 \subsection{The @Show@ class}
146 %*********************************************************
149 type ShowS = String -> String
152 showsPrec :: Int -> a -> ShowS
153 showList :: [a] -> ShowS
155 showList ls = showList__ (showsPrec 0) ls
158 %*********************************************************
160 \subsection{The list type}
162 %*********************************************************
165 data [] a = [] | a : [a] -- do explicitly: deriving (Eq, Ord)
166 -- to avoid weird names like con2tag_[]#
168 instance (Eq a) => Eq [a] where
170 (x:xs) == (y:ys) = x == y && xs == ys
172 xs /= ys = if (xs == ys) then False else True
174 instance (Ord a) => Ord [a] where
175 a < b = case compare a b of { LT -> True; EQ -> False; GT -> False }
176 a <= b = case compare a b of { LT -> True; EQ -> True; GT -> False }
177 a >= b = case compare a b of { LT -> False; EQ -> True; GT -> True }
178 a > b = case compare a b of { LT -> False; EQ -> False; GT -> True }
180 max a b = case compare a b of { LT -> b; EQ -> a; GT -> a }
181 min a b = case compare a b of { LT -> a; EQ -> a; GT -> b }
184 compare (x:xs) [] = GT
185 compare [] (y:ys) = LT
186 compare (x:xs) (y:ys) = case compare x y of
191 instance Functor [] where
193 map f (x:xs) = f x : map f xs
195 instance Monad [] where
196 m >>= k = foldr ((++) . k) [] m
197 m >> k = foldr ((++) . (\ _ -> k)) [] m
200 instance MonadZero [] where
203 instance MonadPlus [] where
204 #ifdef USE_REPORT_PRELUDE
205 xs ++ ys = foldr (:) ys xs
208 (x:xs) ++ ys = x : (xs ++ ys)
211 instance (Show a) => Show [a] where
212 showsPrec p = showList
213 showList ls = showList__ (showsPrec 0) ls
218 A few list functions that appear here because they are used here.
219 The rest of the prelude list functions are in PrelList.
222 foldr :: (a -> b -> b) -> b -> [a] -> b
224 foldr f z (x:xs) = f x (foldr f z xs)
226 -- takeWhile, applied to a predicate p and a list xs, returns the longest
227 -- prefix (possibly empty) of xs of elements that satisfy p. dropWhile p xs
228 -- returns the remaining suffix. Span p xs is equivalent to
229 -- (takeWhile p xs, dropWhile p xs), while break p uses the negation of p.
231 takeWhile :: (a -> Bool) -> [a] -> [a]
234 | p x = x : takeWhile p xs
237 dropWhile :: (a -> Bool) -> [a] -> [a]
239 dropWhile p xs@(x:xs')
240 | p x = dropWhile p xs'
243 -- List index (subscript) operator, 0-origin
244 (!!) :: [a] -> Int -> a
245 #ifdef USE_REPORT_PRELUDE
247 (_:xs) !! n | n > 0 = xs !! (n-1)
248 (_:_) !! _ = error "PreludeList.!!: negative index"
249 [] !! _ = error "PreludeList.!!: index too large"
251 -- HBC version (stolen), then unboxified
252 -- The semantics is not quite the same for error conditions
253 -- in the more efficient version.
255 _ !! n | n < 0 = error "(!!){PreludeList}: negative index\n"
256 xs !! n = sub xs (case n of { I# n# -> n# })
257 where sub :: [a] -> Int# -> a
258 sub [] _ = error "(!!){PreludeList}: index too large\n"
259 sub (x:xs) n# = if n# ==# 0#
261 else sub xs (n# -# 1#)
266 %*********************************************************
268 \subsection{Type @Void@}
270 %*********************************************************
272 The type @Void@ is built in, but it needs a @Show@ instance.
276 void = error "You tried to evaluate void"
278 instance Show Void where
279 showsPrec p f = showString "<<void>>"
280 showList ls = showList__ (showsPrec 0) ls
284 %*********************************************************
286 \subsection{Type @Bool@}
288 %*********************************************************
291 data Bool = False | True deriving (Eq, Ord, Enum, Bounded, Show {- Read -})
295 (&&), (||) :: Bool -> Bool -> Bool
310 %*********************************************************
312 \subsection{Type @Maybe@}
314 %*********************************************************
317 data Maybe a = Nothing | Just a deriving (Eq, Ord, Show {- Read -})
319 maybe :: b -> (a -> b) -> Maybe a -> b
320 maybe n f Nothing = n
321 maybe n f (Just x) = f x
323 instance Functor Maybe where
324 map f Nothing = Nothing
325 map f (Just a) = Just (f a)
327 instance Monad Maybe where
329 Nothing >>= k = Nothing
332 Nothing >> k = Nothing
336 instance MonadZero Maybe where
339 instance MonadPlus Maybe where
345 %*********************************************************
347 \subsection{The @()@ type}
349 %*********************************************************
351 The Unit type is here because virtually any program needs it (whereas
352 some programs may get away without consulting PrelTup). Furthermore,
353 the renamer currently *always* asks for () to be in scope, so that
354 ccalls can use () as their default type; so when compiling PrelBase we
355 need (). (We could arrange suck in () only if -fglasgow-exts, but putting
356 it here seems more direct.
359 data () = () --easier to do explicitly: deriving (Eq, Ord, Enum, Show, Bounded)
360 -- (avoids weird-named functions, e.g., con2tag_()#
365 instance Ord () where
374 instance Enum () where
376 toEnum _ = error "Prelude.Enum.().toEnum: argument not 0"
379 enumFromThen () () = [()]
380 enumFromTo () () = [()]
381 enumFromThenTo () () () = [()]
383 instance Bounded () where
387 instance Show () where
388 showsPrec p () = showString "()"
389 showList ls = showList__ (showsPrec 0) ls
392 %*********************************************************
394 \subsection{Type @Either@}
396 %*********************************************************
399 data Either a b = Left a | Right b deriving (Eq, Ord, Show {- Read -} )
401 either :: (a -> c) -> (b -> c) -> Either a b -> c
402 either f g (Left x) = f x
403 either f g (Right y) = g y
407 %*********************************************************
409 \subsection{Type @Ordering@}
411 %*********************************************************
414 data Ordering = LT | EQ | GT deriving (Eq, Ord, Enum, Bounded, Show {- Read -})
418 %*********************************************************
420 \subsection{Type @Char@ and @String@}
422 %*********************************************************
427 data Char = C# Char# deriving (Eq, Ord)
429 instance Enum Char where
430 toEnum (I# i) | i >=# 0# && i <=# 255# = C# (chr# i)
431 | otherwise = error ("Prelude.Enum.Char.toEnum:out of range: " ++ show (I# i))
432 fromEnum (C# c) = I# (ord# c)
434 enumFrom (C# c) = efttCh (ord# c) 1# (># 255#)
435 enumFromTo (C# c1) (C# c2) = efttCh (ord# c1) 1# (># (ord# c2))
437 enumFromThen (C# c1) (C# c2)
438 | c1 `leChar#` c2 = efttCh (ord# c1) (ord# c2 -# ord# c1) (># 255#)
439 | otherwise = efttCh (ord# c1) (ord# c2 -# ord# c1) (<# 0#)
441 enumFromThenTo (C# c1) (C# c2) (C# c3)
442 | c1 `leChar#` c2 = efttCh (ord# c1) (ord# c2 -# ord# c1) (># (ord# c3))
443 | otherwise = efttCh (ord# c1) (ord# c2 -# ord# c1) (<# (ord# c3))
445 efttCh :: Int# -> Int# -> (Int# -> Bool) -> [Char]
449 go now | done now = []
450 | otherwise = C# (chr# now) : go (now +# step)
452 instance Bounded Char where
456 instance Show Char where
457 showsPrec p '\'' = showString "'\\''"
458 showsPrec p c = showChar '\'' . showLitChar c . showChar '\''
460 showList cs = showChar '"' . showl cs
461 where showl "" = showChar '"'
462 showl ('"':cs) = showString "\\\"" . showl cs
463 showl (c:cs) = showLitChar c . showl cs
468 isAscii, isLatin1, isControl, isPrint, isSpace, isUpper,
469 isLower, isAlpha, isDigit, isOctDigit, isHexDigit, isAlphanum :: Char -> Bool
470 isAscii c = fromEnum c < 128
471 isLatin1 c = c <= '\xff'
472 isControl c = c < ' ' || c >= '\DEL' && c <= '\x9f'
473 isPrint c = not (isControl c)
475 -- isSpace includes non-breaking space
476 -- Done with explicit equalities both for efficiency, and to avoid a tiresome
477 -- recursion with PrelList elem
478 isSpace c = c == ' ' ||
486 -- The upper case ISO characters have the multiplication sign dumped
487 -- randomly in the middle of the range. Go figure.
488 isUpper c = c >= 'A' && c <= 'Z' ||
489 c >= '\xC0' && c <= '\xD6' ||
490 c >= '\xD8' && c <= '\xDE'
491 -- The lower case ISO characters have the division sign dumped
492 -- randomly in the middle of the range. Go figure.
493 isLower c = c >= 'a' && c <= 'z' ||
494 c >= '\xDF' && c <= '\xF6' ||
495 c >= '\xF8' && c <= '\xFF'
496 isAlpha c = isLower c || isUpper c
497 isDigit c = c >= '0' && c <= '9'
498 isOctDigit c = c >= '0' && c <= '7'
499 isHexDigit c = isDigit c || c >= 'A' && c <= 'F' ||
501 isAlphanum c = isAlpha c || isDigit c
503 -- Case-changing operations
505 toUpper, toLower :: Char -> Char
506 toUpper c | isLower c && c /= '\xDF' && c /= '\xFF'
507 = toEnum (fromEnum c - fromEnum 'a' + fromEnum 'A')
510 toLower c | isUpper c = toEnum (fromEnum c - fromEnum 'A'
514 asciiTab = -- Using an array drags in the array module. listArray ('\NUL', ' ')
515 ["NUL", "SOH", "STX", "ETX", "EOT", "ENQ", "ACK", "BEL",
516 "BS", "HT", "LF", "VT", "FF", "CR", "SO", "SI",
517 "DLE", "DC1", "DC2", "DC3", "DC4", "NAK", "SYN", "ETB",
518 "CAN", "EM", "SUB", "ESC", "FS", "GS", "RS", "US",
522 %*********************************************************
524 \subsection{Type @Int@}
526 %*********************************************************
531 instance Eq Int where
532 (==) x y = x `eqInt` y
533 (/=) x y = x `neInt` y
535 instance Ord Int where
536 compare x y = compareInt x y
542 max x y = case (compareInt x y) of { LT -> y ; EQ -> x ; GT -> x }
543 min x y = case (compareInt x y) of { LT -> x ; EQ -> x ; GT -> y }
545 (I# x) `compareInt` (I# y) | x <# y = LT
549 instance Enum Int where
553 #ifndef USE_FOLDR_BUILD
554 enumFrom (I# c) = eftInt c 1#
555 enumFromTo (I# c1) (I# c2) = efttInt c1 1# (># c2)
556 enumFromThen (I# c1) (I# c2) = eftInt c1 (c2 -# c1)
558 enumFromThenTo (I# c1) (I# c2) (I# c3)
559 | c1 <=# c2 = efttInt c1 (c2 -# c1) (># c3)
560 | otherwise = efttInt c1 (c2 -# c1) (<# c3)
563 {-# INLINE enumFrom #-}
564 {-# INLINE enumFromTo #-}
565 enumFrom x = build (\ c _ ->
566 let g x = x `c` g (x `plusInt` 1) in g x)
567 enumFromTo x y = build (\ c n ->
568 let g x = if x <= y then x `c` g (x `plusInt` 1) else n in g x)
571 efttInt :: Int# -> Int# -> (Int# -> Bool) -> [Int]
572 efttInt now step done
575 go now | done now = []
576 | otherwise = I# now : go (now +# step)
578 eftInt :: Int# -> Int# -> [Int]
582 go now = I# now : go (now +# step)
585 instance Bounded Int where
586 minBound = negate 2147483647 -- **********************
587 maxBound = 2147483647 -- **********************
589 instance Num Int where
590 (+) x y = plusInt x y
591 (-) x y = minusInt x y
592 negate x = negateInt x
593 (*) x y = timesInt x y
594 abs n = if n `geInt` 0 then n else (negateInt n)
596 signum n | n `ltInt` 0 = negateInt 1
600 fromInteger (J# a# s# d#)
601 = case (integer2Int# a# s# d#) of { i# -> I# i# }
605 instance Show Int where
606 showsPrec p n = showSignedInt p n
607 showList ls = showList__ (showsPrec 0) ls
611 %*********************************************************
613 \subsection{Type @Integer@, @Float@, @Double@}
615 %*********************************************************
617 Just the type declarations. If we don't actually use any @Integers@ we'd
618 rather not link the @Integer@ module at all; and the default-decl stuff
619 in the renamer tends to slurp in @Double@ regardless.
622 data Float = F# Float#
623 data Double = D# Double#
624 data Integer = J# Int# Int# ByteArray#
628 %*********************************************************
630 \subsection{The function type}
632 %*********************************************************
635 instance Eval (a -> b)
637 instance Show (a -> b) where
638 showsPrec p f = showString "<<function>>"
639 showList ls = showList__ (showsPrec 0) ls
650 -- function composition
652 {-# GENERATE_SPECS (.) a b c #-}
653 (.) :: (b -> c) -> (a -> b) -> a -> c
656 -- flip f takes its (first) two arguments in the reverse order of f.
657 flip :: (a -> b -> c) -> b -> a -> c
660 -- right-associating infix application operator (useful in continuation-
662 ($) :: (a -> b) -> a -> b
665 -- until p f yields the result of applying f until p holds.
666 until :: (a -> Bool) -> (a -> a) -> a -> a
667 until p f x | p x = x
668 | otherwise = until p f (f x)
670 -- asTypeOf is a type-restricted version of const. It is usually used
671 -- as an infix operator, and its typing forces its first argument
672 -- (which is usually overloaded) to have the same type as the second.
673 asTypeOf :: a -> a -> a
678 %*********************************************************
680 \subsection{Miscellaneous}
682 %*********************************************************
687 {-# GENERATE_SPECS data a :: Lift a #-}
693 %*********************************************************
695 \subsection{Support code for @Show@}
697 %*********************************************************
700 shows :: (Show a) => a -> ShowS
703 show :: (Show a) => a -> String
706 showChar :: Char -> ShowS
709 showString :: String -> ShowS
712 showParen :: Bool -> ShowS -> ShowS
713 showParen b p = if b then showChar '(' . p . showChar ')' else p
715 {-# GENERATE_SPECS showList__ a #-}
716 showList__ :: (a -> ShowS) -> [a] -> ShowS
718 showList__ showx [] = showString "[]"
719 showList__ showx (x:xs) = showChar '[' . showx x . showl xs
721 showl [] = showChar ']'
722 showl (x:xs) = showString ", " . showx x . showl xs
725 showSpace = {-showChar ' '-} \ xs -> ' ' : xs
728 Code specific for characters
731 showLitChar :: Char -> ShowS
732 showLitChar c | c > '\DEL' = showChar '\\' . protectEsc isDigit (shows (ord c))
733 showLitChar '\DEL' = showString "\\DEL"
734 showLitChar '\\' = showString "\\\\"
735 showLitChar c | c >= ' ' = showChar c
736 showLitChar '\a' = showString "\\a"
737 showLitChar '\b' = showString "\\b"
738 showLitChar '\f' = showString "\\f"
739 showLitChar '\n' = showString "\\n"
740 showLitChar '\r' = showString "\\r"
741 showLitChar '\t' = showString "\\t"
742 showLitChar '\v' = showString "\\v"
743 showLitChar '\SO' = protectEsc (== 'H') (showString "\\SO")
744 showLitChar c = showString ('\\' : asciiTab!!ord c)
746 protectEsc p f = f . cont
747 where cont s@(c:_) | p c = "\\&" ++ s
750 intToDigit :: Int -> Char
752 | i >= 0 && i <= 9 = toEnum (fromEnum '0' + i)
753 | i >= 10 && i <= 15 = toEnum (fromEnum 'a' + i -10)
754 | otherwise = error "Char.intToDigit: not a digit" -- ....
758 Code specific for Ints.
761 showSignedInt :: Int -> Int -> ShowS
762 showSignedInt p (I# n) r
763 = -- from HBC version; support code follows
764 if n <# 0# && p > 6 then '(':itos n++(')':r) else itos n ++ r
766 itos :: Int# -> String
769 if negateInt# n <# 0# then
770 -- n is minInt, a difficult number
771 itos (n `quotInt#` 10#) ++ itos' (negateInt# (n `remInt#` 10#)) []
773 '-':itos' (negateInt# n) []
777 itos' :: Int# -> String -> String
780 C# (chr# (n +# ord# '0'#)) : cs
782 itos' (n `quotInt#` 10#) (C# (chr# (n `remInt#` 10# +# ord# '0'#)) : cs)
785 %*********************************************************
787 \subsection{Numeric primops}
789 %*********************************************************
791 Definitions of the boxed PrimOps; these will be
792 used in the case of partial applications, etc.
798 plusInt (I# x) (I# y) = I# (x +# y)
799 minusInt(I# x) (I# y) = I# (x -# y)
800 timesInt(I# x) (I# y) = I# (x *# y)
801 quotInt (I# x) (I# y) = I# (quotInt# x y)
802 remInt (I# x) (I# y) = I# (remInt# x y)
803 negateInt (I# x) = I# (negateInt# x)
804 gtInt (I# x) (I# y) = x ># y
805 geInt (I# x) (I# y) = x >=# y
806 eqInt (I# x) (I# y) = x ==# y
807 neInt (I# x) (I# y) = x /=# y
808 ltInt (I# x) (I# y) = x <# y
809 leInt (I# x) (I# y) = x <=# y