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 )
23 infix 4 ==, /=, <, <=, >=, >
30 %*********************************************************
32 \subsection{Standard classes @Eq@, @Ord@, @Bounded@, @Eval@}
34 %*********************************************************
38 (==), (/=) :: a -> a -> Bool
42 class (Eq a) => Ord a where
43 compare :: a -> a -> Ordering
44 (<), (<=), (>=), (>):: a -> a -> Bool
45 max, min :: a -> a -> a
47 -- An instance of Ord should define either compare or <=
48 -- Using compare can be more efficient for complex types.
54 x <= y = compare x y /= GT
55 x < y = compare x y == LT
56 x >= y = compare x y /= LT
57 x > y = compare x y == GT
58 max x y = case (compare x y) of { LT -> y ; EQ -> x ; GT -> x }
59 min x y = case (compare x y) of { LT -> x ; EQ -> x ; GT -> y }
62 minBound, maxBound :: a
67 %*********************************************************
69 \subsection{Monadic classes @Functor@, @Monad@, @MonadZero@, @MonadPlus@}
71 %*********************************************************
75 map :: (a -> b) -> f a -> f b
78 (>>=) :: m a -> (a -> m b) -> m b
79 (>>) :: m a -> m b -> m b
82 m >> k = m >>= \_ -> k
84 class (Monad m) => MonadZero m where
87 class (MonadZero m) => MonadPlus m where
88 (++) :: m a -> m a -> m a
92 %*********************************************************
94 \subsection{Classes @Num@ and @Enum@}
96 %*********************************************************
102 enumFrom :: a -> [a] -- [n..]
103 enumFromThen :: a -> a -> [a] -- [n,n'..]
104 enumFromTo :: a -> a -> [a] -- [n..m]
105 enumFromThenTo :: a -> a -> a -> [a] -- [n,n'..m]
107 enumFromTo n m = map toEnum [fromEnum n .. fromEnum m]
108 enumFromThenTo n n' m
109 = map toEnum [fromEnum n, fromEnum n' .. fromEnum m]
111 class (Eq a, Show a, Eval a) => Num a where
112 (+), (-), (*) :: a -> a -> a
114 abs, signum :: a -> a
115 fromInteger :: Integer -> a
116 fromInt :: Int -> a -- partain: Glasgow extension
119 fromInt (I# i#) = fromInteger (int2Integer# i#)
120 -- Go via the standard class-op if the
121 -- non-standard one ain't provided
125 succ, pred :: Enum a => a -> a
126 succ = toEnum . (+1) . fromEnum
127 pred = toEnum . (subtract 1) . fromEnum
129 chr = (toEnum :: Int -> Char)
130 ord = (fromEnum :: Char -> Int)
133 ord_0 = fromInt (ord '0')
135 {-# GENERATE_SPECS subtract a{Int#,Double#,Int,Double,Complex(Double#),Complex(Double)} #-}
136 subtract :: (Num a) => a -> a -> a
141 %*********************************************************
143 \subsection{The @Show@ class}
145 %*********************************************************
148 type ShowS = String -> String
151 showsPrec :: Int -> a -> ShowS
152 showList :: [a] -> ShowS
154 showList ls = showList__ (showsPrec 0) ls
157 %*********************************************************
159 \subsection{The list type}
161 %*********************************************************
164 data [] a = [] | a : [a] -- do explicitly: deriving (Eq, Ord)
165 -- to avoid weird names like con2tag_[]#
167 instance (Eq a) => Eq [a] where
169 (x:xs) == (y:ys) = x == y && xs == ys
171 xs /= ys = if (xs == ys) then False else True
173 instance (Ord a) => Ord [a] where
174 a < b = case compare a b of { LT -> True; EQ -> False; GT -> False }
175 a <= b = case compare a b of { LT -> True; EQ -> True; GT -> False }
176 a >= b = case compare a b of { LT -> False; EQ -> True; GT -> True }
177 a > b = case compare a b of { LT -> False; EQ -> False; GT -> True }
179 max a b = case compare a b of { LT -> b; EQ -> a; GT -> a }
180 min a b = case compare a b of { LT -> a; EQ -> a; GT -> b }
183 compare (x:xs) [] = GT
184 compare [] (y:ys) = LT
185 compare (x:xs) (y:ys) = case compare x y of
190 instance Functor [] where
192 map f (x:xs) = f x : map f xs
194 instance Monad [] where
195 m >>= k = foldr ((++) . k) [] m
196 m >> k = foldr ((++) . (\ _ -> k)) [] m
199 instance MonadZero [] where
202 instance MonadPlus [] where
203 #ifdef USE_REPORT_PRELUDE
204 xs ++ ys = foldr (:) ys xs
207 (x:xs) ++ ys = x : (xs ++ ys)
210 instance (Show a) => Show [a] where
211 showsPrec p = showList
212 showList ls = showList__ (showsPrec 0) ls
217 A few list functions that appear here because they are used here.
218 The rest of the prelude list functions are in PrelList.
221 foldr :: (a -> b -> b) -> b -> [a] -> b
223 foldr f z (x:xs) = f x (foldr f z xs)
225 -- takeWhile, applied to a predicate p and a list xs, returns the longest
226 -- prefix (possibly empty) of xs of elements that satisfy p. dropWhile p xs
227 -- returns the remaining suffix. Span p xs is equivalent to
228 -- (takeWhile p xs, dropWhile p xs), while break p uses the negation of p.
230 takeWhile :: (a -> Bool) -> [a] -> [a]
233 | p x = x : takeWhile p xs
236 dropWhile :: (a -> Bool) -> [a] -> [a]
238 dropWhile p xs@(x:xs')
239 | p x = dropWhile p xs'
242 -- List index (subscript) operator, 0-origin
243 (!!) :: [a] -> Int -> a
244 #ifdef USE_REPORT_PRELUDE
246 (_:xs) !! n | n > 0 = xs !! (n-1)
247 (_:_) !! _ = error "PreludeList.!!: negative index"
248 [] !! _ = error "PreludeList.!!: index too large"
250 -- HBC version (stolen), then unboxified
251 -- The semantics is not quite the same for error conditions
252 -- in the more efficient version.
254 _ !! n | n < 0 = error "(!!){PreludeList}: negative index\n"
255 xs !! n = sub xs (case n of { I# n# -> n# })
256 where sub :: [a] -> Int# -> a
257 sub [] _ = error "(!!){PreludeList}: index too large\n"
258 sub (x:xs) n# = if n# ==# 0#
260 else sub xs (n# -# 1#)
265 %*********************************************************
267 \subsection{Type @Void@}
269 %*********************************************************
271 The type @Void@ is built in, but it needs a @Show@ instance.
275 void = error "You tried to evaluate void"
277 instance Show Void where
278 showsPrec p f = showString "<<void>>"
279 showList ls = showList__ (showsPrec 0) ls
283 %*********************************************************
285 \subsection{Type @Bool@}
287 %*********************************************************
290 data Bool = False | True deriving (Eq, Ord, Enum, Bounded, Show {- Read -})
294 (&&), (||) :: Bool -> Bool -> Bool
309 %*********************************************************
311 \subsection{Type @Maybe@}
313 %*********************************************************
316 data Maybe a = Nothing | Just a deriving (Eq, Ord, Show {- Read -})
318 maybe :: b -> (a -> b) -> Maybe a -> b
319 maybe n f Nothing = n
320 maybe n f (Just x) = f x
322 instance Functor Maybe where
323 map f Nothing = Nothing
324 map f (Just a) = Just (f a)
326 instance Monad Maybe where
328 Nothing >>= k = Nothing
331 Nothing >> k = Nothing
335 instance MonadZero Maybe where
338 instance MonadPlus Maybe where
344 %*********************************************************
346 \subsection{The @()@ type}
348 %*********************************************************
350 The Unit type is here because virtually any program needs it (whereas
351 some programs may get away without consulting PrelTup). Furthermore,
352 the renamer currently *always* asks for () to be in scope, so that
353 ccalls can use () as their default type; so when compiling PrelBase we
354 need (). (We could arrange suck in () only if -fglasgow-exts, but putting
355 it here seems more direct.
358 data () = () --easier to do explicitly: deriving (Eq, Ord, Enum, Show, Bounded)
359 -- (avoids weird-named functions, e.g., con2tag_()#
364 instance Ord () where
373 instance Enum () where
375 toEnum _ = error "Prelude.Enum.().toEnum: argument not 0"
378 enumFromThen () () = [()]
379 enumFromTo () () = [()]
380 enumFromThenTo () () () = [()]
382 instance Bounded () where
386 instance Show () where
387 showsPrec p () = showString "()"
388 showList ls = showList__ (showsPrec 0) ls
391 %*********************************************************
393 \subsection{Type @Either@}
395 %*********************************************************
398 data Either a b = Left a | Right b deriving (Eq, Ord, Show {- Read -} )
400 either :: (a -> c) -> (b -> c) -> Either a b -> c
401 either f g (Left x) = f x
402 either f g (Right y) = g y
406 %*********************************************************
408 \subsection{Type @Ordering@}
410 %*********************************************************
413 data Ordering = LT | EQ | GT deriving (Eq, Ord, Enum, Bounded, Show {- Read -})
417 %*********************************************************
419 \subsection{Type @Char@ and @String@}
421 %*********************************************************
426 data Char = C# Char# deriving (Eq, Ord)
428 instance Enum Char where
429 toEnum (I# i) | i >=# 0# && i <=# 255# = C# (chr# i)
430 | otherwise = error ("Prelude.Enum.Char.toEnum:out of range: " ++ show (I# i))
431 fromEnum (C# c) = I# (ord# c)
433 enumFrom (C# c) = efttCh (ord# c) 1# (># 255#)
434 enumFromTo (C# c1) (C# c2) = efttCh (ord# c1) 1# (># (ord# c2))
436 enumFromThen (C# c1) (C# c2)
437 | c1 `leChar#` c2 = efttCh (ord# c1) (ord# c2 -# ord# c1) (># 255#)
438 | otherwise = efttCh (ord# c1) (ord# c2 -# ord# c1) (<# 0#)
440 enumFromThenTo (C# c1) (C# c2) (C# c3)
441 | c1 `leChar#` c2 = efttCh (ord# c1) (ord# c2 -# ord# c1) (># (ord# c3))
442 | otherwise = efttCh (ord# c1) (ord# c2 -# ord# c1) (<# (ord# c3))
444 efttCh :: Int# -> Int# -> (Int# -> Bool) -> [Char]
448 go now | done now = []
449 | otherwise = C# (chr# now) : go (now +# step)
451 instance Bounded Char where
455 instance Show Char where
456 showsPrec p '\'' = showString "'\\''"
457 showsPrec p c = showChar '\'' . showLitChar c . showChar '\''
459 showList cs = showChar '"' . showl cs
460 where showl "" = showChar '"'
461 showl ('"':cs) = showString "\\\"" . showl cs
462 showl (c:cs) = showLitChar c . showl cs
467 isAscii, isLatin1, isControl, isPrint, isSpace, isUpper,
468 isLower, isAlpha, isDigit, isOctDigit, isHexDigit, isAlphanum :: Char -> Bool
469 isAscii c = fromEnum c < 128
470 isLatin1 c = c <= '\xff'
471 isControl c = c < ' ' || c >= '\DEL' && c <= '\x9f'
472 isPrint c = not (isControl c)
474 -- isSpace includes non-breaking space
475 -- Done with explicit equalities both for efficiency, and to avoid a tiresome
476 -- recursion with PrelList elem
477 isSpace c = c == ' ' ||
485 -- The upper case ISO characters have the multiplication sign dumped
486 -- randomly in the middle of the range. Go figure.
487 isUpper c = c >= 'A' && c <= 'Z' ||
488 c >= '\xC0' && c <= '\xD6' ||
489 c >= '\xD8' && c <= '\xDE'
490 -- The lower case ISO characters have the division sign dumped
491 -- randomly in the middle of the range. Go figure.
492 isLower c = c >= 'a' && c <= 'z' ||
493 c >= '\xDF' && c <= '\xF6' ||
494 c >= '\xF8' && c <= '\xFF'
495 isAlpha c = isLower c || isUpper c
496 isDigit c = c >= '0' && c <= '9'
497 isOctDigit c = c >= '0' && c <= '7'
498 isHexDigit c = isDigit c || c >= 'A' && c <= 'F' ||
500 isAlphanum c = isAlpha c || isDigit c
502 -- Case-changing operations
504 toUpper, toLower :: Char -> Char
505 toUpper c | isLower c && c /= '\xDF' && c /= '\xFF'
506 = toEnum (fromEnum c - fromEnum 'a' + fromEnum 'A')
509 toLower c | isUpper c = toEnum (fromEnum c - fromEnum 'A'
513 asciiTab = -- Using an array drags in the array module. listArray ('\NUL', ' ')
514 ["NUL", "SOH", "STX", "ETX", "EOT", "ENQ", "ACK", "BEL",
515 "BS", "HT", "LF", "VT", "FF", "CR", "SO", "SI",
516 "DLE", "DC1", "DC2", "DC3", "DC4", "NAK", "SYN", "ETB",
517 "CAN", "EM", "SUB", "ESC", "FS", "GS", "RS", "US",
521 %*********************************************************
523 \subsection{Type @Int@}
525 %*********************************************************
530 instance Eq Int where
531 (==) x y = x `eqInt` y
532 (/=) x y = x `neInt` y
534 instance Ord Int where
535 compare x y = compareInt x y
541 max x y = case (compareInt x y) of { LT -> y ; EQ -> x ; GT -> x }
542 min x y = case (compareInt x y) of { LT -> x ; EQ -> x ; GT -> y }
544 (I# x) `compareInt` (I# y) | x <# y = LT
548 instance Enum Int where
552 #ifndef USE_FOLDR_BUILD
553 enumFrom (I# c) = eftInt c 1#
554 enumFromTo (I# c1) (I# c2) = efttInt c1 1# (># c2)
555 enumFromThen (I# c1) (I# c2) = eftInt c1 (c2 -# c1)
557 enumFromThenTo (I# c1) (I# c2) (I# c3)
558 | c1 <=# c2 = efttInt c1 (c2 -# c1) (># c3)
559 | otherwise = efttInt c1 (c2 -# c1) (<# c3)
562 {-# INLINE enumFrom #-}
563 {-# INLINE enumFromTo #-}
564 enumFrom x = build (\ c _ ->
565 let g x = x `c` g (x `plusInt` 1) in g x)
566 enumFromTo x y = build (\ c n ->
567 let g x = if x <= y then x `c` g (x `plusInt` 1) else n in g x)
570 efttInt :: Int# -> Int# -> (Int# -> Bool) -> [Int]
571 efttInt now step done
574 go now | done now = []
575 | otherwise = I# now : go (now +# step)
577 eftInt :: Int# -> Int# -> [Int]
581 go now = I# now : go (now +# step)
584 instance Bounded Int where
585 minBound = negate 2147483647 -- **********************
586 maxBound = 2147483647 -- **********************
588 instance Num Int where
589 (+) x y = plusInt x y
590 (-) x y = minusInt x y
591 negate x = negateInt x
592 (*) x y = timesInt x y
593 abs n = if n `geInt` 0 then n else (negateInt n)
595 signum n | n `ltInt` 0 = negateInt 1
599 fromInteger (J# a# s# d#)
600 = case (integer2Int# a# s# d#) of { i# -> I# i# }
604 instance Show Int where
605 showsPrec p n = showSignedInt p n
606 showList ls = showList__ (showsPrec 0) ls
610 %*********************************************************
612 \subsection{Type @Integer@, @Float@, @Double@}
614 %*********************************************************
616 Just the type declarations. If we don't actually use any @Integers@ we'd
617 rather not link the @Integer@ module at all; and the default-decl stuff
618 in the renamer tends to slurp in @Double@ regardless.
621 data Float = F# Float#
622 data Double = D# Double#
623 data Integer = J# Int# Int# ByteArray#
627 %*********************************************************
629 \subsection{The function type}
631 %*********************************************************
634 instance Show (a -> b) where
635 showsPrec p f = showString "<<function>>"
636 showList ls = showList__ (showsPrec 0) ls
647 -- function composition
649 {-# GENERATE_SPECS (.) a b c #-}
650 (.) :: (b -> c) -> (a -> b) -> a -> c
653 -- flip f takes its (first) two arguments in the reverse order of f.
654 flip :: (a -> b -> c) -> b -> a -> c
657 -- right-associating infix application operator (useful in continuation-
659 ($) :: (a -> b) -> a -> b
662 -- until p f yields the result of applying f until p holds.
663 until :: (a -> Bool) -> (a -> a) -> a -> a
664 until p f x | p x = x
665 | otherwise = until p f (f x)
667 -- asTypeOf is a type-restricted version of const. It is usually used
668 -- as an infix operator, and its typing forces its first argument
669 -- (which is usually overloaded) to have the same type as the second.
670 asTypeOf :: a -> a -> a
675 %*********************************************************
677 \subsection{Miscellaneous}
679 %*********************************************************
684 {-# GENERATE_SPECS data a :: Lift a #-}
690 %*********************************************************
692 \subsection{Support code for @Show@}
694 %*********************************************************
697 shows :: (Show a) => a -> ShowS
700 show :: (Show a) => a -> String
703 showChar :: Char -> ShowS
706 showString :: String -> ShowS
709 showParen :: Bool -> ShowS -> ShowS
710 showParen b p = if b then showChar '(' . p . showChar ')' else p
712 {-# GENERATE_SPECS showList__ a #-}
713 showList__ :: (a -> ShowS) -> [a] -> ShowS
715 showList__ showx [] = showString "[]"
716 showList__ showx (x:xs) = showChar '[' . showx x . showl xs
718 showl [] = showChar ']'
719 showl (x:xs) = showString ", " . showx x . showl xs
722 showSpace = {-showChar ' '-} \ xs -> ' ' : xs
725 Code specific for characters
728 showLitChar :: Char -> ShowS
729 showLitChar c | c > '\DEL' = showChar '\\' . protectEsc isDigit (shows (ord c))
730 showLitChar '\DEL' = showString "\\DEL"
731 showLitChar '\\' = showString "\\\\"
732 showLitChar c | c >= ' ' = showChar c
733 showLitChar '\a' = showString "\\a"
734 showLitChar '\b' = showString "\\b"
735 showLitChar '\f' = showString "\\f"
736 showLitChar '\n' = showString "\\n"
737 showLitChar '\r' = showString "\\r"
738 showLitChar '\t' = showString "\\t"
739 showLitChar '\v' = showString "\\v"
740 showLitChar '\SO' = protectEsc (== 'H') (showString "\\SO")
741 showLitChar c = showString ('\\' : asciiTab!!ord c)
743 protectEsc p f = f . cont
744 where cont s@(c:_) | p c = "\\&" ++ s
747 intToDigit :: Int -> Char
749 | i >= 0 && i <= 9 = toEnum (fromEnum '0' + i)
750 | i >= 10 && i <= 15 = toEnum (fromEnum 'a' + i -10)
751 | otherwise = error "Char.intToDigit: not a digit" -- ....
755 Code specific for Ints.
758 showSignedInt :: Int -> Int -> ShowS
759 showSignedInt p (I# n) r
760 = -- from HBC version; support code follows
761 if n <# 0# && p > 6 then '(':itos n++(')':r) else itos n ++ r
763 itos :: Int# -> String
766 if negateInt# n <# 0# then
767 -- n is minInt, a difficult number
768 itos (n `quotInt#` 10#) ++ itos' (negateInt# (n `remInt#` 10#)) []
770 '-':itos' (negateInt# n) []
774 itos' :: Int# -> String -> String
777 C# (chr# (n +# ord# '0'#)) : cs
779 itos' (n `quotInt#` 10#) (C# (chr# (n `remInt#` 10# +# ord# '0'#)) : cs)
782 %*********************************************************
784 \subsection{Numeric primops}
786 %*********************************************************
788 Definitions of the boxed PrimOps; these will be
789 used in the case of partial applications, etc.
795 plusInt (I# x) (I# y) = I# (x +# y)
796 minusInt(I# x) (I# y) = I# (x -# y)
797 timesInt(I# x) (I# y) = I# (x *# y)
798 quotInt (I# x) (I# y) = I# (quotInt# x y)
799 remInt (I# x) (I# y) = I# (remInt# x y)
800 negateInt (I# x) = I# (negateInt# x)
801 gtInt (I# x) (I# y) = x ># y
802 geInt (I# x) (I# y) = x >=# y
803 eqInt (I# x) (I# y) = x ==# y
804 neInt (I# x) (I# y) = x /=# y
805 ltInt (I# x) (I# y) = x <# y
806 leInt (I# x) (I# y) = x <=# y