2 % (c) The GRASP/AQUA Project, Glasgow University, 1992-1996
4 \section[PrelBase]{Module @PrelBase@}
8 {-# OPTIONS -fno-implicit-prelude #-}
12 module PrelGHC -- Re-export PrelGHC, to avoid lots of people
13 -- having to import it explicitly
16 import {-# SOURCE #-} PrelErr ( error )
24 infix 4 ==, /=, <, <=, >=, >
35 data Bool = False | True
37 data Double = D# Double#
38 data () = () --easier to do explicitly: deriving (Eq, Ord, Enum, Show, Bounded)
39 -- (avoids weird-named functions, e.g., con2tag_()#
41 data Maybe a = Nothing | Just a
42 data Ordering = LT | EQ | GT deriving( Eq )
47 data [] a = [] | a : [a] -- do explicitly: deriving (Eq, Ord)
48 -- to avoid weird names like con2tag_[]#
51 -------------- Stage 2 -----------------------
58 maybe :: b -> (a -> b) -> Maybe a -> b
60 maybe n f (Just x) = f x
62 -------------- Stage 3 -----------------------
64 (==), (/=) :: a -> a -> Bool
68 -- f :: Eq a => a -> a -> Bool
71 g :: Eq a => a -> a -> Bool
74 -------------- Stage 4 -----------------------
76 class (Eq a) => Ord a where
77 compare :: a -> a -> Ordering
78 (<), (<=), (>=), (>):: a -> a -> Bool
79 max, min :: a -> a -> a
81 -- An instance of Ord should define either compare or <=
82 -- Using compare can be more efficient for complex types.
88 x <= y = compare x y /= GT
89 x < y = compare x y == LT
90 x >= y = compare x y /= LT
91 x > y = compare x y == GT
92 max x y = case (compare x y) of { LT -> y ; EQ -> x ; GT -> x }
93 min x y = case (compare x y) of { LT -> x ; EQ -> x ; GT -> y }
95 eqInt (I# x) (I# y) = x ==# y
98 (==) x y = x `eqInt` y
100 instance Ord Int where
101 compare x y = error "help"
103 class Bounded a where
104 minBound, maxBound :: a
107 type ShowS = String -> String
110 showsPrec :: Bool -> a -> ShowS
111 showList :: [a] -> ShowS
113 showList ls = showList__ (showsPrec True) ls
115 showList__ :: (a -> ShowS) -> [a] -> ShowS
116 showList__ showx [] = showString "[]"
118 showString :: String -> ShowS
123 shows :: (Show a) => a -> ShowS
124 shows = showsPrec True
126 -- show :: (Show a) => a -> String
127 --show x = shows x ""
132 %*********************************************************
134 \subsection{Standard classes @Eq@, @Ord@, @Bounded@, @Eval@}
136 %*********************************************************
140 (==), (/=) :: a -> a -> Bool
142 x /= y = not (x == y)
144 class (Eq a) => Ord a where
145 compare :: a -> a -> Ordering
146 (<), (<=), (>=), (>):: a -> a -> Bool
147 max, min :: a -> a -> a
149 -- An instance of Ord should define either compare or <=
150 -- Using compare can be more efficient for complex types.
156 x <= y = compare x y /= GT
157 x < y = compare x y == LT
158 x >= y = compare x y /= LT
159 x > y = compare x y == GT
160 max x y = case (compare x y) of { LT -> y ; EQ -> x ; GT -> x }
161 min x y = case (compare x y) of { LT -> x ; EQ -> x ; GT -> y }
163 class Bounded a where
164 minBound, maxBound :: a
169 %*********************************************************
171 \subsection{Monadic classes @Functor@, @Monad@, @MonadZero@, @MonadPlus@}
173 %*********************************************************
176 class Functor f where
177 map :: (a -> b) -> f a -> f b
180 (>>=) :: m a -> (a -> m b) -> m b
181 (>>) :: m a -> m b -> m b
184 m >> k = m >>= \_ -> k
186 class (Monad m) => MonadZero m where
189 class (MonadZero m) => MonadPlus m where
190 (++) :: m a -> m a -> m a
194 %*********************************************************
196 \subsection{Classes @Num@ and @Enum@}
198 %*********************************************************
204 enumFrom :: a -> [a] -- [n..]
205 enumFromThen :: a -> a -> [a] -- [n,n'..]
206 enumFromTo :: a -> a -> [a] -- [n..m]
207 enumFromThenTo :: a -> a -> a -> [a] -- [n,n'..m]
209 enumFromTo n m = map toEnum [fromEnum n .. fromEnum m]
210 enumFromThenTo n n' m
211 = map toEnum [fromEnum n, fromEnum n' .. fromEnum m]
213 class (Eq a, Show a, Eval a) => Num a where
214 (+), (-), (*) :: a -> a -> a
216 abs, signum :: a -> a
217 fromInteger :: Integer -> a
218 fromInt :: Int -> a -- partain: Glasgow extension
221 fromInt (I# i#) = fromInteger (int2Integer# i#)
222 -- Go via the standard class-op if the
223 -- non-standard one ain't provided
227 {-# SPECIALISE succ :: Int -> Int #-}
228 {-# SPECIALISE pred :: Int -> Int #-}
229 succ, pred :: Enum a => a -> a
230 succ = toEnum . (+1) . fromEnum
231 pred = toEnum . (subtract 1) . fromEnum
233 chr = (toEnum :: Int -> Char)
234 ord = (fromEnum :: Char -> Int)
237 ord_0 = fromInt (ord '0')
239 {-# SPECIALISE subtract :: Int -> Int -> Int #-}
240 subtract :: (Num a) => a -> a -> a
245 %*********************************************************
247 \subsection{The @Show@ class}
249 %*********************************************************
252 type ShowS = String -> String
255 showsPrec :: Int -> a -> ShowS
256 showList :: [a] -> ShowS
258 showList ls = showList__ (showsPrec 0) ls
261 %*********************************************************
263 \subsection{The list type}
265 %*********************************************************
268 data [] a = [] | a : [a] -- do explicitly: deriving (Eq, Ord)
269 -- to avoid weird names like con2tag_[]#
271 instance (Eq a) => Eq [a] where
273 (x:xs) == (y:ys) = x == y && xs == ys
275 xs /= ys = if (xs == ys) then False else True
277 instance (Ord a) => Ord [a] where
278 a < b = case compare a b of { LT -> True; EQ -> False; GT -> False }
279 a <= b = case compare a b of { LT -> True; EQ -> True; GT -> False }
280 a >= b = case compare a b of { LT -> False; EQ -> True; GT -> True }
281 a > b = case compare a b of { LT -> False; EQ -> False; GT -> True }
283 max a b = case compare a b of { LT -> b; EQ -> a; GT -> a }
284 min a b = case compare a b of { LT -> a; EQ -> a; GT -> b }
287 compare (x:xs) [] = GT
288 compare [] (y:ys) = LT
289 compare (x:xs) (y:ys) = case compare x y of
294 instance Functor [] where
296 map f (x:xs) = f x : map f xs
298 instance Monad [] where
299 m >>= k = foldr ((++) . k) [] m
300 m >> k = foldr ((++) . (\ _ -> k)) [] m
303 instance MonadZero [] where
306 instance MonadPlus [] where
307 #ifdef USE_REPORT_PRELUDE
308 xs ++ ys = foldr (:) ys xs
311 (x:xs) ++ ys = x : (xs ++ ys)
314 instance (Show a) => Show [a] where
315 showsPrec p = showList
316 showList ls = showList__ (showsPrec 0) ls
321 A few list functions that appear here because they are used here.
322 The rest of the prelude list functions are in PrelList.
325 foldr :: (a -> b -> b) -> b -> [a] -> b
327 foldr f z (x:xs) = f x (foldr f z xs)
329 -- takeWhile, applied to a predicate p and a list xs, returns the longest
330 -- prefix (possibly empty) of xs of elements that satisfy p. dropWhile p xs
331 -- returns the remaining suffix. Span p xs is equivalent to
332 -- (takeWhile p xs, dropWhile p xs), while break p uses the negation of p.
334 takeWhile :: (a -> Bool) -> [a] -> [a]
337 | p x = x : takeWhile p xs
340 dropWhile :: (a -> Bool) -> [a] -> [a]
342 dropWhile p xs@(x:xs')
343 | p x = dropWhile p xs'
346 -- List index (subscript) operator, 0-origin
347 (!!) :: [a] -> Int -> a
348 #ifdef USE_REPORT_PRELUDE
350 (_:xs) !! n | n > 0 = xs !! (n-1)
351 (_:_) !! _ = error "PreludeList.!!: negative index"
352 [] !! _ = error "PreludeList.!!: index too large"
354 -- HBC version (stolen), then unboxified
355 -- The semantics is not quite the same for error conditions
356 -- in the more efficient version.
358 _ !! n | n < 0 = error "(!!){PreludeList}: negative index\n"
359 xs !! n = sub xs (case n of { I# n# -> n# })
360 where sub :: [a] -> Int# -> a
361 sub [] _ = error "(!!){PreludeList}: index too large\n"
362 sub (x:xs) n# = if n# ==# 0#
364 else sub xs (n# -# 1#)
369 %*********************************************************
371 \subsection{Type @Void@}
373 %*********************************************************
375 The type @Void@ is built in, but it needs a @Show@ instance.
379 void = error "You tried to evaluate void"
381 instance Show Void where
382 showsPrec p f = showString "<<void>>"
383 showList ls = showList__ (showsPrec 0) ls
387 %*********************************************************
389 \subsection{Type @Bool@}
391 %*********************************************************
394 data Bool = False | True deriving (Eq, Ord, Enum, Bounded, Show {- Read -})
398 (&&), (||) :: Bool -> Bool -> Bool
413 %*********************************************************
415 \subsection{The @()@ type}
417 %*********************************************************
419 The Unit type is here because virtually any program needs it (whereas
420 some programs may get away without consulting PrelTup). Furthermore,
421 the renamer currently *always* asks for () to be in scope, so that
422 ccalls can use () as their default type; so when compiling PrelBase we
423 need (). (We could arrange suck in () only if -fglasgow-exts, but putting
424 it here seems more direct.
427 data () = () --easier to do explicitly: deriving (Eq, Ord, Enum, Show, Bounded)
428 -- (avoids weird-named functions, e.g., con2tag_()#
434 instance Ord () where
443 instance Enum () where
445 toEnum _ = error "Prelude.Enum.().toEnum: argument not 0"
448 enumFromThen () () = [()]
449 enumFromTo () () = [()]
450 enumFromThenTo () () () = [()]
452 instance Show () where
453 showsPrec p () = showString "()"
454 showList ls = showList__ (showsPrec 0) ls
457 %*********************************************************
459 \subsection{Type @Ordering@}
461 %*********************************************************
464 data Ordering = LT | EQ | GT deriving (Eq, Ord, Enum, Bounded, Show {- Read -})
468 %*********************************************************
470 \subsection{Type @Char@ and @String@}
472 %*********************************************************
477 data Char = C# Char# deriving (Eq, Ord)
479 instance Enum Char where
480 toEnum (I# i) | i >=# 0# && i <=# 255# = C# (chr# i)
481 | otherwise = error ("Prelude.Enum.Char.toEnum:out of range: " ++ show (I# i))
482 fromEnum (C# c) = I# (ord# c)
484 enumFrom (C# c) = efttCh (ord# c) 1# (># 255#)
485 enumFromTo (C# c1) (C# c2) = efttCh (ord# c1) 1# (># (ord# c2))
487 enumFromThen (C# c1) (C# c2)
488 | c1 `leChar#` c2 = efttCh (ord# c1) (ord# c2 -# ord# c1) (># 255#)
489 | otherwise = efttCh (ord# c1) (ord# c2 -# ord# c1) (<# 0#)
491 enumFromThenTo (C# c1) (C# c2) (C# c3)
492 | c1 `leChar#` c2 = efttCh (ord# c1) (ord# c2 -# ord# c1) (># (ord# c3))
493 | otherwise = efttCh (ord# c1) (ord# c2 -# ord# c1) (<# (ord# c3))
495 efttCh :: Int# -> Int# -> (Int# -> Bool) -> [Char]
499 go now | done now = []
500 | otherwise = C# (chr# now) : go (now +# step)
502 instance Show Char where
503 showsPrec p '\'' = showString "'\\''"
504 showsPrec p c = showChar '\'' . showLitChar c . showChar '\''
506 showList cs = showChar '"' . showl cs
507 where showl "" = showChar '"'
508 showl ('"':cs) = showString "\\\"" . showl cs
509 showl (c:cs) = showLitChar c . showl cs
514 isAscii, isLatin1, isControl, isPrint, isSpace, isUpper,
515 isLower, isAlpha, isDigit, isOctDigit, isHexDigit, isAlphanum :: Char -> Bool
516 isAscii c = fromEnum c < 128
517 isLatin1 c = c <= '\xff'
518 isControl c = c < ' ' || c >= '\DEL' && c <= '\x9f'
519 isPrint c = not (isControl c)
521 -- isSpace includes non-breaking space
522 -- Done with explicit equalities both for efficiency, and to avoid a tiresome
523 -- recursion with PrelList elem
524 isSpace c = c == ' ' ||
532 -- The upper case ISO characters have the multiplication sign dumped
533 -- randomly in the middle of the range. Go figure.
534 isUpper c = c >= 'A' && c <= 'Z' ||
535 c >= '\xC0' && c <= '\xD6' ||
536 c >= '\xD8' && c <= '\xDE'
537 -- The lower case ISO characters have the division sign dumped
538 -- randomly in the middle of the range. Go figure.
539 isLower c = c >= 'a' && c <= 'z' ||
540 c >= '\xDF' && c <= '\xF6' ||
541 c >= '\xF8' && c <= '\xFF'
542 isAlpha c = isLower c || isUpper c
543 isDigit c = c >= '0' && c <= '9'
544 isOctDigit c = c >= '0' && c <= '7'
545 isHexDigit c = isDigit c || c >= 'A' && c <= 'F' ||
547 isAlphanum c = isAlpha c || isDigit c
549 -- Case-changing operations
551 toUpper, toLower :: Char -> Char
552 toUpper c | isLower c && c /= '\xDF' && c /= '\xFF'
553 = toEnum (fromEnum c - fromEnum 'a' + fromEnum 'A')
556 toLower c | isUpper c = toEnum (fromEnum c - fromEnum 'A'
560 asciiTab = -- Using an array drags in the array module. listArray ('\NUL', ' ')
561 ["NUL", "SOH", "STX", "ETX", "EOT", "ENQ", "ACK", "BEL",
562 "BS", "HT", "LF", "VT", "FF", "CR", "SO", "SI",
563 "DLE", "DC1", "DC2", "DC3", "DC4", "NAK", "SYN", "ETB",
564 "CAN", "EM", "SUB", "ESC", "FS", "GS", "RS", "US",
568 %*********************************************************
570 \subsection{Type @Int@}
572 %*********************************************************
577 instance Eq Int where
578 (==) x y = x `eqInt` y
579 (/=) x y = x `neInt` y
581 instance Ord Int where
582 compare x y = compareInt x y
588 max x y = case (compareInt x y) of { LT -> y ; EQ -> x ; GT -> x }
589 min x y = case (compareInt x y) of { LT -> x ; EQ -> x ; GT -> y }
591 (I# x) `compareInt` (I# y) | x <# y = LT
595 instance Enum Int where
599 #ifndef USE_FOLDR_BUILD
600 enumFrom (I# c) = eftInt c 1#
601 enumFromTo (I# c1) (I# c2) = efttInt c1 1# (># c2)
602 enumFromThen (I# c1) (I# c2) = eftInt c1 (c2 -# c1)
604 enumFromThenTo (I# c1) (I# c2) (I# c3)
605 | c1 <=# c2 = efttInt c1 (c2 -# c1) (># c3)
606 | otherwise = efttInt c1 (c2 -# c1) (<# c3)
609 {-# INLINE enumFrom #-}
610 {-# INLINE enumFromTo #-}
611 enumFrom x = build (\ c _ ->
612 let g x = x `c` g (x `plusInt` 1) in g x)
613 enumFromTo x y = build (\ c n ->
614 let g x = if x <= y then x `c` g (x `plusInt` 1) else n in g x)
617 efttInt :: Int# -> Int# -> (Int# -> Bool) -> [Int]
618 efttInt now step done
621 go now | done now = []
622 | otherwise = I# now : go (now +# step)
624 eftInt :: Int# -> Int# -> [Int]
628 go now = I# now : go (now +# step)
631 instance Num Int where
632 (+) x y = plusInt x y
633 (-) x y = minusInt x y
634 negate x = negateInt x
635 (*) x y = timesInt x y
636 abs n = if n `geInt` 0 then n else (negateInt n)
638 signum n | n `ltInt` 0 = negateInt 1
642 fromInteger (J# a# s# d#)
643 = case (integer2Int# a# s# d#) of { i# -> I# i# }
647 instance Show Int where
648 showsPrec p n = showSignedInt p n
649 showList ls = showList__ (showsPrec 0) ls
653 %*********************************************************
655 \subsection{Type @Integer@, @Float@, @Double@}
657 %*********************************************************
659 Just the type declarations. If we don't actually use any @Integers@ we'd
660 rather not link the @Integer@ module at all; and the default-decl stuff
661 in the renamer tends to slurp in @Double@ regardless.
664 data Float = F# Float#
665 data Double = D# Double#
666 data Integer = J# Int# Int# ByteArray#
670 %*********************************************************
672 \subsection{The function type}
674 %*********************************************************
677 instance Eval (a -> b)
679 instance Show (a -> b) where
680 showsPrec p f = showString "<<function>>"
681 showList ls = showList__ (showsPrec 0) ls
692 -- function composition
694 (.) :: (b -> c) -> (a -> b) -> a -> c
697 -- flip f takes its (first) two arguments in the reverse order of f.
698 flip :: (a -> b -> c) -> b -> a -> c
701 -- right-associating infix application operator (useful in continuation-
703 ($) :: (a -> b) -> a -> b
706 -- until p f yields the result of applying f until p holds.
707 until :: (a -> Bool) -> (a -> a) -> a -> a
708 until p f x | p x = x
709 | otherwise = until p f (f x)
711 -- asTypeOf is a type-restricted version of const. It is usually used
712 -- as an infix operator, and its typing forces its first argument
713 -- (which is usually overloaded) to have the same type as the second.
714 asTypeOf :: a -> a -> a
719 %*********************************************************
721 \subsection{Miscellaneous}
723 %*********************************************************
733 %*********************************************************
735 \subsection{Support code for @Show@}
737 %*********************************************************
740 shows :: (Show a) => a -> ShowS
743 show :: (Show a) => a -> String
746 showChar :: Char -> ShowS
749 showString :: String -> ShowS
752 showParen :: Bool -> ShowS -> ShowS
753 showParen b p = if b then showChar '(' . p . showChar ')' else p
755 showList__ :: (a -> ShowS) -> [a] -> ShowS
757 showList__ showx [] = showString "[]"
758 showList__ showx (x:xs) = showChar '[' . showx x . showl xs
760 showl [] = showChar ']'
761 showl (x:xs) = showChar ',' . showx x . showl xs
764 showSpace = {-showChar ' '-} \ xs -> ' ' : xs
767 Code specific for characters
770 showLitChar :: Char -> ShowS
771 showLitChar c | c > '\DEL' = showChar '\\' . protectEsc isDigit (shows (ord c))
772 showLitChar '\DEL' = showString "\\DEL"
773 showLitChar '\\' = showString "\\\\"
774 showLitChar c | c >= ' ' = showChar c
775 showLitChar '\a' = showString "\\a"
776 showLitChar '\b' = showString "\\b"
777 showLitChar '\f' = showString "\\f"
778 showLitChar '\n' = showString "\\n"
779 showLitChar '\r' = showString "\\r"
780 showLitChar '\t' = showString "\\t"
781 showLitChar '\v' = showString "\\v"
782 showLitChar '\SO' = protectEsc (== 'H') (showString "\\SO")
783 showLitChar c = showString ('\\' : asciiTab!!ord c)
785 protectEsc p f = f . cont
786 where cont s@(c:_) | p c = "\\&" ++ s
789 intToDigit :: Int -> Char
791 | i >= 0 && i <= 9 = toEnum (fromEnum '0' + i)
792 | i >= 10 && i <= 15 = toEnum (fromEnum 'a' + i -10)
793 | otherwise = error ("Char.intToDigit: not a digit" ++ show i)
797 Code specific for Ints.
800 showSignedInt :: Int -> Int -> ShowS
801 showSignedInt p (I# n) r
802 = -- from HBC version; support code follows
803 if n <# 0# && p > 6 then '(':itos n++(')':r) else itos n ++ r
805 itos :: Int# -> String
808 if negateInt# n <# 0# then
809 -- n is minInt, a difficult number
810 itos (n `quotInt#` 10#) ++ itos' (negateInt# (n `remInt#` 10#)) []
812 '-':itos' (negateInt# n) []
816 itos' :: Int# -> String -> String
819 C# (chr# (n +# ord# '0'#)) : cs
821 itos' (n `quotInt#` 10#) (C# (chr# (n `remInt#` 10# +# ord# '0'#)) : cs)
824 %*********************************************************
826 \subsection{Numeric primops}
828 %*********************************************************
830 Definitions of the boxed PrimOps; these will be
831 used in the case of partial applications, etc.
837 plusInt (I# x) (I# y) = I# (x +# y)
838 minusInt(I# x) (I# y) = I# (x -# y)
839 timesInt(I# x) (I# y) = I# (x *# y)
840 quotInt (I# x) (I# y) = I# (quotInt# x y)
841 remInt (I# x) (I# y) = I# (remInt# x y)
842 negateInt (I# x) = I# (negateInt# x)
843 gtInt (I# x) (I# y) = x ># y
844 geInt (I# x) (I# y) = x >=# y
845 eqInt (I# x) (I# y) = x ==# y
846 neInt (I# x) (I# y) = x /=# y
847 ltInt (I# x) (I# y) = x <# y
848 leInt (I# x) (I# y) = x <=# y