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 succ, pred :: Enum a => a -> a
228 succ = toEnum . (+1) . fromEnum
229 pred = toEnum . (subtract 1) . fromEnum
231 chr = (toEnum :: Int -> Char)
232 ord = (fromEnum :: Char -> Int)
235 ord_0 = fromInt (ord '0')
237 {-# SPECIALISE subtract :: Int -> Int -> Int #-}
238 subtract :: (Num a) => a -> a -> a
243 %*********************************************************
245 \subsection{The @Show@ class}
247 %*********************************************************
250 type ShowS = String -> String
253 showsPrec :: Int -> a -> ShowS
254 showList :: [a] -> ShowS
256 showList ls = showList__ (showsPrec 0) ls
259 %*********************************************************
261 \subsection{The list type}
263 %*********************************************************
266 data [] a = [] | a : [a] -- do explicitly: deriving (Eq, Ord)
267 -- to avoid weird names like con2tag_[]#
269 instance (Eq a) => Eq [a] where
271 (x:xs) == (y:ys) = x == y && xs == ys
273 xs /= ys = if (xs == ys) then False else True
275 instance (Ord a) => Ord [a] where
276 a < b = case compare a b of { LT -> True; EQ -> False; GT -> False }
277 a <= b = case compare a b of { LT -> True; EQ -> True; GT -> False }
278 a >= b = case compare a b of { LT -> False; EQ -> True; GT -> True }
279 a > b = case compare a b of { LT -> False; EQ -> False; GT -> True }
281 max a b = case compare a b of { LT -> b; EQ -> a; GT -> a }
282 min a b = case compare a b of { LT -> a; EQ -> a; GT -> b }
285 compare (x:xs) [] = GT
286 compare [] (y:ys) = LT
287 compare (x:xs) (y:ys) = case compare x y of
292 instance Functor [] where
294 map f (x:xs) = f x : map f xs
296 instance Monad [] where
297 m >>= k = foldr ((++) . k) [] m
298 m >> k = foldr ((++) . (\ _ -> k)) [] m
301 instance MonadZero [] where
304 instance MonadPlus [] where
305 #ifdef USE_REPORT_PRELUDE
306 xs ++ ys = foldr (:) ys xs
309 (x:xs) ++ ys = x : (xs ++ ys)
312 instance (Show a) => Show [a] where
313 showsPrec p = showList
314 showList ls = showList__ (showsPrec 0) ls
319 A few list functions that appear here because they are used here.
320 The rest of the prelude list functions are in PrelList.
323 foldr :: (a -> b -> b) -> b -> [a] -> b
325 foldr f z (x:xs) = f x (foldr f z xs)
327 -- takeWhile, applied to a predicate p and a list xs, returns the longest
328 -- prefix (possibly empty) of xs of elements that satisfy p. dropWhile p xs
329 -- returns the remaining suffix. Span p xs is equivalent to
330 -- (takeWhile p xs, dropWhile p xs), while break p uses the negation of p.
332 takeWhile :: (a -> Bool) -> [a] -> [a]
335 | p x = x : takeWhile p xs
338 dropWhile :: (a -> Bool) -> [a] -> [a]
340 dropWhile p xs@(x:xs')
341 | p x = dropWhile p xs'
344 -- List index (subscript) operator, 0-origin
345 (!!) :: [a] -> Int -> a
346 #ifdef USE_REPORT_PRELUDE
348 (_:xs) !! n | n > 0 = xs !! (n-1)
349 (_:_) !! _ = error "PreludeList.!!: negative index"
350 [] !! _ = error "PreludeList.!!: index too large"
352 -- HBC version (stolen), then unboxified
353 -- The semantics is not quite the same for error conditions
354 -- in the more efficient version.
356 _ !! n | n < 0 = error "(!!){PreludeList}: negative index\n"
357 xs !! n = sub xs (case n of { I# n# -> n# })
358 where sub :: [a] -> Int# -> a
359 sub [] _ = error "(!!){PreludeList}: index too large\n"
360 sub (x:xs) n# = if n# ==# 0#
362 else sub xs (n# -# 1#)
367 %*********************************************************
369 \subsection{Type @Void@}
371 %*********************************************************
373 The type @Void@ is built in, but it needs a @Show@ instance.
377 void = error "You tried to evaluate void"
379 instance Show Void where
380 showsPrec p f = showString "<<void>>"
381 showList ls = showList__ (showsPrec 0) ls
385 %*********************************************************
387 \subsection{Type @Bool@}
389 %*********************************************************
392 data Bool = False | True deriving (Eq, Ord, Enum, Bounded, Show {- Read -})
396 (&&), (||) :: Bool -> Bool -> Bool
411 %*********************************************************
413 \subsection{The @()@ type}
415 %*********************************************************
417 The Unit type is here because virtually any program needs it (whereas
418 some programs may get away without consulting PrelTup). Furthermore,
419 the renamer currently *always* asks for () to be in scope, so that
420 ccalls can use () as their default type; so when compiling PrelBase we
421 need (). (We could arrange suck in () only if -fglasgow-exts, but putting
422 it here seems more direct.
425 data () = () --easier to do explicitly: deriving (Eq, Ord, Enum, Show, Bounded)
426 -- (avoids weird-named functions, e.g., con2tag_()#
432 instance Ord () where
441 instance Enum () where
443 toEnum _ = error "Prelude.Enum.().toEnum: argument not 0"
446 enumFromThen () () = [()]
447 enumFromTo () () = [()]
448 enumFromThenTo () () () = [()]
450 instance Show () where
451 showsPrec p () = showString "()"
452 showList ls = showList__ (showsPrec 0) ls
455 %*********************************************************
457 \subsection{Type @Ordering@}
459 %*********************************************************
462 data Ordering = LT | EQ | GT deriving (Eq, Ord, Enum, Bounded, Show {- Read -})
466 %*********************************************************
468 \subsection{Type @Char@ and @String@}
470 %*********************************************************
475 data Char = C# Char# deriving (Eq, Ord)
477 instance Enum Char where
478 toEnum (I# i) | i >=# 0# && i <=# 255# = C# (chr# i)
479 | otherwise = error ("Prelude.Enum.Char.toEnum:out of range: " ++ show (I# i))
480 fromEnum (C# c) = I# (ord# c)
482 enumFrom (C# c) = efttCh (ord# c) 1# (># 255#)
483 enumFromTo (C# c1) (C# c2) = efttCh (ord# c1) 1# (># (ord# c2))
485 enumFromThen (C# c1) (C# c2)
486 | c1 `leChar#` c2 = efttCh (ord# c1) (ord# c2 -# ord# c1) (># 255#)
487 | otherwise = efttCh (ord# c1) (ord# c2 -# ord# c1) (<# 0#)
489 enumFromThenTo (C# c1) (C# c2) (C# c3)
490 | c1 `leChar#` c2 = efttCh (ord# c1) (ord# c2 -# ord# c1) (># (ord# c3))
491 | otherwise = efttCh (ord# c1) (ord# c2 -# ord# c1) (<# (ord# c3))
493 efttCh :: Int# -> Int# -> (Int# -> Bool) -> [Char]
497 go now | done now = []
498 | otherwise = C# (chr# now) : go (now +# step)
500 instance Show Char where
501 showsPrec p '\'' = showString "'\\''"
502 showsPrec p c = showChar '\'' . showLitChar c . showChar '\''
504 showList cs = showChar '"' . showl cs
505 where showl "" = showChar '"'
506 showl ('"':cs) = showString "\\\"" . showl cs
507 showl (c:cs) = showLitChar c . showl cs
512 isAscii, isLatin1, isControl, isPrint, isSpace, isUpper,
513 isLower, isAlpha, isDigit, isOctDigit, isHexDigit, isAlphanum :: Char -> Bool
514 isAscii c = fromEnum c < 128
515 isLatin1 c = c <= '\xff'
516 isControl c = c < ' ' || c >= '\DEL' && c <= '\x9f'
517 isPrint c = not (isControl c)
519 -- isSpace includes non-breaking space
520 -- Done with explicit equalities both for efficiency, and to avoid a tiresome
521 -- recursion with PrelList elem
522 isSpace c = c == ' ' ||
530 -- The upper case ISO characters have the multiplication sign dumped
531 -- randomly in the middle of the range. Go figure.
532 isUpper c = c >= 'A' && c <= 'Z' ||
533 c >= '\xC0' && c <= '\xD6' ||
534 c >= '\xD8' && c <= '\xDE'
535 -- The lower case ISO characters have the division sign dumped
536 -- randomly in the middle of the range. Go figure.
537 isLower c = c >= 'a' && c <= 'z' ||
538 c >= '\xDF' && c <= '\xF6' ||
539 c >= '\xF8' && c <= '\xFF'
540 isAlpha c = isLower c || isUpper c
541 isDigit c = c >= '0' && c <= '9'
542 isOctDigit c = c >= '0' && c <= '7'
543 isHexDigit c = isDigit c || c >= 'A' && c <= 'F' ||
545 isAlphanum c = isAlpha c || isDigit c
547 -- Case-changing operations
549 toUpper, toLower :: Char -> Char
550 toUpper c | isLower c && c /= '\xDF' && c /= '\xFF'
551 = toEnum (fromEnum c - fromEnum 'a' + fromEnum 'A')
554 toLower c | isUpper c = toEnum (fromEnum c - fromEnum 'A'
558 asciiTab = -- Using an array drags in the array module. listArray ('\NUL', ' ')
559 ["NUL", "SOH", "STX", "ETX", "EOT", "ENQ", "ACK", "BEL",
560 "BS", "HT", "LF", "VT", "FF", "CR", "SO", "SI",
561 "DLE", "DC1", "DC2", "DC3", "DC4", "NAK", "SYN", "ETB",
562 "CAN", "EM", "SUB", "ESC", "FS", "GS", "RS", "US",
566 %*********************************************************
568 \subsection{Type @Int@}
570 %*********************************************************
575 instance Eq Int where
576 (==) x y = x `eqInt` y
577 (/=) x y = x `neInt` y
579 instance Ord Int where
580 compare x y = compareInt x y
586 max x y = case (compareInt x y) of { LT -> y ; EQ -> x ; GT -> x }
587 min x y = case (compareInt x y) of { LT -> x ; EQ -> x ; GT -> y }
589 (I# x) `compareInt` (I# y) | x <# y = LT
593 instance Enum Int where
597 #ifndef USE_FOLDR_BUILD
598 enumFrom (I# c) = eftInt c 1#
599 enumFromTo (I# c1) (I# c2) = efttInt c1 1# (># c2)
600 enumFromThen (I# c1) (I# c2) = eftInt c1 (c2 -# c1)
602 enumFromThenTo (I# c1) (I# c2) (I# c3)
603 | c1 <=# c2 = efttInt c1 (c2 -# c1) (># c3)
604 | otherwise = efttInt c1 (c2 -# c1) (<# c3)
607 {-# INLINE enumFrom #-}
608 {-# INLINE enumFromTo #-}
609 enumFrom x = build (\ c _ ->
610 let g x = x `c` g (x `plusInt` 1) in g x)
611 enumFromTo x y = build (\ c n ->
612 let g x = if x <= y then x `c` g (x `plusInt` 1) else n in g x)
615 efttInt :: Int# -> Int# -> (Int# -> Bool) -> [Int]
616 efttInt now step done
619 go now | done now = []
620 | otherwise = I# now : go (now +# step)
622 eftInt :: Int# -> Int# -> [Int]
626 go now = I# now : go (now +# step)
629 instance Num Int where
630 (+) x y = plusInt x y
631 (-) x y = minusInt x y
632 negate x = negateInt x
633 (*) x y = timesInt x y
634 abs n = if n `geInt` 0 then n else (negateInt n)
636 signum n | n `ltInt` 0 = negateInt 1
640 fromInteger (J# a# s# d#)
641 = case (integer2Int# a# s# d#) of { i# -> I# i# }
645 instance Show Int where
646 showsPrec p n = showSignedInt p n
647 showList ls = showList__ (showsPrec 0) ls
651 %*********************************************************
653 \subsection{Type @Integer@, @Float@, @Double@}
655 %*********************************************************
657 Just the type declarations. If we don't actually use any @Integers@ we'd
658 rather not link the @Integer@ module at all; and the default-decl stuff
659 in the renamer tends to slurp in @Double@ regardless.
662 data Float = F# Float#
663 data Double = D# Double#
664 data Integer = J# Int# Int# ByteArray#
668 %*********************************************************
670 \subsection{The function type}
672 %*********************************************************
675 instance Eval (a -> b)
677 instance Show (a -> b) where
678 showsPrec p f = showString "<<function>>"
679 showList ls = showList__ (showsPrec 0) ls
690 -- function composition
692 (.) :: (b -> c) -> (a -> b) -> a -> c
695 -- flip f takes its (first) two arguments in the reverse order of f.
696 flip :: (a -> b -> c) -> b -> a -> c
699 -- right-associating infix application operator (useful in continuation-
701 ($) :: (a -> b) -> a -> b
704 -- until p f yields the result of applying f until p holds.
705 until :: (a -> Bool) -> (a -> a) -> a -> a
706 until p f x | p x = x
707 | otherwise = until p f (f x)
709 -- asTypeOf is a type-restricted version of const. It is usually used
710 -- as an infix operator, and its typing forces its first argument
711 -- (which is usually overloaded) to have the same type as the second.
712 asTypeOf :: a -> a -> a
717 %*********************************************************
719 \subsection{Miscellaneous}
721 %*********************************************************
731 %*********************************************************
733 \subsection{Support code for @Show@}
735 %*********************************************************
738 shows :: (Show a) => a -> ShowS
741 show :: (Show a) => a -> String
744 showChar :: Char -> ShowS
747 showString :: String -> ShowS
750 showParen :: Bool -> ShowS -> ShowS
751 showParen b p = if b then showChar '(' . p . showChar ')' else p
753 showList__ :: (a -> ShowS) -> [a] -> ShowS
755 showList__ showx [] = showString "[]"
756 showList__ showx (x:xs) = showChar '[' . showx x . showl xs
758 showl [] = showChar ']'
759 showl (x:xs) = showChar ',' . showx x . showl xs
762 showSpace = {-showChar ' '-} \ xs -> ' ' : xs
765 Code specific for characters
768 showLitChar :: Char -> ShowS
769 showLitChar c | c > '\DEL' = showChar '\\' . protectEsc isDigit (shows (ord c))
770 showLitChar '\DEL' = showString "\\DEL"
771 showLitChar '\\' = showString "\\\\"
772 showLitChar c | c >= ' ' = showChar c
773 showLitChar '\a' = showString "\\a"
774 showLitChar '\b' = showString "\\b"
775 showLitChar '\f' = showString "\\f"
776 showLitChar '\n' = showString "\\n"
777 showLitChar '\r' = showString "\\r"
778 showLitChar '\t' = showString "\\t"
779 showLitChar '\v' = showString "\\v"
780 showLitChar '\SO' = protectEsc (== 'H') (showString "\\SO")
781 showLitChar c = showString ('\\' : asciiTab!!ord c)
783 protectEsc p f = f . cont
784 where cont s@(c:_) | p c = "\\&" ++ s
787 intToDigit :: Int -> Char
789 | i >= 0 && i <= 9 = toEnum (fromEnum '0' + i)
790 | i >= 10 && i <= 15 = toEnum (fromEnum 'a' + i -10)
791 | otherwise = error ("Char.intToDigit: not a digit" ++ show i)
795 Code specific for Ints.
798 showSignedInt :: Int -> Int -> ShowS
799 showSignedInt p (I# n) r
800 = -- from HBC version; support code follows
801 if n <# 0# && p > 6 then '(':itos n++(')':r) else itos n ++ r
803 itos :: Int# -> String
806 if negateInt# n <# 0# then
807 -- n is minInt, a difficult number
808 itos (n `quotInt#` 10#) ++ itos' (negateInt# (n `remInt#` 10#)) []
810 '-':itos' (negateInt# n) []
814 itos' :: Int# -> String -> String
817 C# (chr# (n +# ord# '0'#)) : cs
819 itos' (n `quotInt#` 10#) (C# (chr# (n `remInt#` 10# +# ord# '0'#)) : cs)
822 %*********************************************************
824 \subsection{Numeric primops}
826 %*********************************************************
828 Definitions of the boxed PrimOps; these will be
829 used in the case of partial applications, etc.
835 plusInt (I# x) (I# y) = I# (x +# y)
836 minusInt(I# x) (I# y) = I# (x -# y)
837 timesInt(I# x) (I# y) = I# (x *# y)
838 quotInt (I# x) (I# y) = I# (quotInt# x y)
839 remInt (I# x) (I# y) = I# (remInt# x y)
840 negateInt (I# x) = I# (negateInt# x)
841 gtInt (I# x) (I# y) = x ># y
842 geInt (I# x) (I# y) = x >=# y
843 eqInt (I# x) (I# y) = x ==# y
844 neInt (I# x) (I# y) = x /=# y
845 ltInt (I# x) (I# y) = x <# y
846 leInt (I# x) (I# y) = x <=# y