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 ==, /=, <, <=, >=, >
36 -------------- Stage 1 -----------------------
38 data [] a = [] | a : [a] -- do explicitly: deriving (Eq, Ord)
39 -- to avoid weird names like con2tag_[]#
40 instance Functor [] where
42 map f (x:xs) = f x : [] -- map f xs
45 map :: (a -> b) -> f a -> f b
47 data Bool = False | True
49 data Double = D# Double#
50 data () = () --easier to do explicitly: deriving (Eq, Ord, Enum, Show, Bounded)
51 -- (avoids weird-named functions, e.g., con2tag_()#
53 data Maybe a = Nothing | Just a
54 data Ordering = LT | EQ | GT -- deriving( Eq, Ord )
60 y = let f :: Char -> Int
64 -------------- Stage 2 -----------------------
71 maybe :: b -> (a -> b) -> Maybe a -> b
73 maybe n f (Just x) = f x
75 -------------- Stage 3 -----------------------
77 (==), (/=) :: a -> a -> Bool
81 -- f :: Eq a => a -> a -> Bool
84 g :: Eq a => a -> a -> Bool
87 -------------- Stage 4 -----------------------
89 class (Eq a) => Ord a where
90 compare :: a -> a -> Ordering
91 (<), (<=), (>=), (>):: a -> a -> Bool
92 max, min :: a -> a -> a
94 -- An instance of Ord should define either compare or <=
95 -- Using compare can be more efficient for complex types.
101 x <= y = compare x y /= GT
102 x < y = compare x y == LT
103 x >= y = compare x y /= LT
104 x > y = compare x y == GT
105 max x y = case (compare x y) of { LT -> y ; EQ -> x ; GT -> x }
106 min x y = case (compare x y) of { LT -> x ; EQ -> x ; GT -> y }
108 eqInt (I# x) (I# y) = x ==# y
110 instance Eq Int where
111 (==) x y = x `eqInt` y
113 instance Ord Int where
114 compare x y = error "help"
116 class Bounded a where
117 minBound, maxBound :: a
120 type ShowS = String -> String
123 showsPrec :: Bool -> a -> ShowS
124 showList :: [a] -> ShowS
126 showList ls = showList__ (showsPrec True) ls
128 showList__ :: (a -> ShowS) -> [a] -> ShowS
129 showList__ showx [] = showString "[]"
131 showString :: String -> ShowS
136 shows :: (Show a) => a -> ShowS
137 shows = showsPrec True
139 -- show :: (Show a) => a -> String
140 --show x = shows x ""
145 %*********************************************************
147 \subsection{Standard classes @Eq@, @Ord@, @Bounded@
149 %*********************************************************
153 (==), (/=) :: a -> a -> Bool
155 x /= y = not (x == y)
157 class (Eq a) => Ord a where
158 compare :: a -> a -> Ordering
159 (<), (<=), (>=), (>):: a -> a -> Bool
160 max, min :: a -> a -> a
162 -- An instance of Ord should define either compare or <=
163 -- Using compare can be more efficient for complex types.
169 x <= y = compare x y /= GT
170 x < y = compare x y == LT
171 x >= y = compare x y /= LT
172 x > y = compare x y == GT
173 max x y = case (compare x y) of { LT -> y ; EQ -> x ; GT -> x }
174 min x y = case (compare x y) of { LT -> x ; EQ -> x ; GT -> y }
176 class Bounded a where
177 minBound, maxBound :: a
180 %*********************************************************
182 \subsection{Monadic classes @Functor@, @Monad@, @MonadZero@, @MonadPlus@}
184 %*********************************************************
187 class Functor f where
188 map :: (a -> b) -> f a -> f b
191 (>>=) :: m a -> (a -> m b) -> m b
192 (>>) :: m a -> m b -> m b
195 m >> k = m >>= \_ -> k
197 class (Monad m) => MonadZero m where
200 class (MonadZero m) => MonadPlus m where
201 (++) :: m a -> m a -> m a
205 %*********************************************************
207 \subsection{Classes @Num@ and @Enum@}
209 %*********************************************************
215 enumFrom :: a -> [a] -- [n..]
216 enumFromThen :: a -> a -> [a] -- [n,n'..]
217 enumFromTo :: a -> a -> [a] -- [n..m]
218 enumFromThenTo :: a -> a -> a -> [a] -- [n,n'..m]
220 enumFromTo n m = map toEnum [fromEnum n .. fromEnum m]
221 enumFromThenTo n n' m
222 = map toEnum [fromEnum n, fromEnum n' .. fromEnum m]
224 class (Eq a, Show a) => Num a where
225 (+), (-), (*) :: a -> a -> a
227 abs, signum :: a -> a
228 fromInteger :: Integer -> a
229 fromInt :: Int -> a -- partain: Glasgow extension
232 fromInt (I# i#) = fromInteger (case int2Integer# i# of
233 (# a, s, d #) -> J# a s d)
234 -- Go via the standard class-op if the
235 -- non-standard one ain't provided
239 {-# SPECIALISE succ :: Int -> Int #-}
240 {-# SPECIALISE pred :: Int -> Int #-}
241 succ, pred :: Enum a => a -> a
242 succ = toEnum . (+1) . fromEnum
243 pred = toEnum . (subtract 1) . fromEnum
245 chr = (toEnum :: Int -> Char)
246 ord = (fromEnum :: Char -> Int)
249 ord_0 = fromInt (ord '0')
251 {-# SPECIALISE subtract :: Int -> Int -> Int #-}
252 subtract :: (Num a) => a -> a -> a
257 %*********************************************************
259 \subsection{The @Show@ class}
261 %*********************************************************
264 type ShowS = String -> String
267 showsPrec :: Int -> a -> ShowS
268 showList :: [a] -> ShowS
270 showList ls = showList__ (showsPrec 0) ls
273 %*********************************************************
275 \subsection{The list type}
277 %*********************************************************
280 data [] a = [] | a : [a] -- do explicitly: deriving (Eq, Ord)
281 -- to avoid weird names like con2tag_[]#
283 instance (Eq a) => Eq [a] where
285 (x:xs) == (y:ys) = x == y && xs == ys
287 xs /= ys = if (xs == ys) then False else True
289 instance (Ord a) => Ord [a] where
290 a < b = case compare a b of { LT -> True; EQ -> False; GT -> False }
291 a <= b = case compare a b of { LT -> True; EQ -> True; GT -> False }
292 a >= b = case compare a b of { LT -> False; EQ -> True; GT -> True }
293 a > b = case compare a b of { LT -> False; EQ -> False; GT -> True }
295 max a b = case compare a b of { LT -> b; EQ -> a; GT -> a }
296 min a b = case compare a b of { LT -> a; EQ -> a; GT -> b }
299 compare (x:xs) [] = GT
300 compare [] (y:ys) = LT
301 compare (x:xs) (y:ys) = case compare x y of
306 instance Functor [] where
308 map f (x:xs) = f x : map f xs
310 instance Monad [] where
311 m >>= k = foldr ((++) . k) [] m
312 m >> k = foldr ((++) . (\ _ -> k)) [] m
315 instance MonadZero [] where
318 instance MonadPlus [] where
319 #ifdef USE_REPORT_PRELUDE
320 xs ++ ys = foldr (:) ys xs
323 (x:xs) ++ ys = x : (xs ++ ys)
326 instance (Show a) => Show [a] where
327 showsPrec p = showList
328 showList ls = showList__ (showsPrec 0) ls
333 A few list functions that appear here because they are used here.
334 The rest of the prelude list functions are in PrelList.
337 foldr :: (a -> b -> b) -> b -> [a] -> b
339 foldr f z (x:xs) = f x (foldr f z xs)
341 -- takeWhile, applied to a predicate p and a list xs, returns the longest
342 -- prefix (possibly empty) of xs of elements that satisfy p. dropWhile p xs
343 -- returns the remaining suffix. Span p xs is equivalent to
344 -- (takeWhile p xs, dropWhile p xs), while break p uses the negation of p.
346 takeWhile :: (a -> Bool) -> [a] -> [a]
349 | p x = x : takeWhile p xs
352 dropWhile :: (a -> Bool) -> [a] -> [a]
354 dropWhile p xs@(x:xs')
355 | p x = dropWhile p xs'
358 -- List index (subscript) operator, 0-origin
359 (!!) :: [a] -> Int -> a
360 #ifdef USE_REPORT_PRELUDE
362 (_:xs) !! n | n > 0 = xs !! (n-1)
363 (_:_) !! _ = error "PreludeList.!!: negative index"
364 [] !! _ = error "PreludeList.!!: index too large"
366 -- HBC version (stolen), then unboxified
367 -- The semantics is not quite the same for error conditions
368 -- in the more efficient version.
370 _ !! n | n < 0 = error "(!!){PreludeList}: negative index\n"
371 xs !! n = sub xs (case n of { I# n# -> n# })
372 where sub :: [a] -> Int# -> a
373 sub [] _ = error "(!!){PreludeList}: index too large\n"
374 sub (x:xs) n# = if n# ==# 0#
376 else sub xs (n# -# 1#)
381 %*********************************************************
383 \subsection{Type @Void@}
385 %*********************************************************
387 The type @Void@ is built in, but it needs a @Show@ instance.
391 void = error "You tried to evaluate void"
393 instance Show Void where
394 showsPrec p f = showString "<<void>>"
395 showList ls = showList__ (showsPrec 0) ls
399 %*********************************************************
401 \subsection{Type @Bool@}
403 %*********************************************************
406 data Bool = False | True deriving (Eq, Ord, Enum, Bounded, Show {- Read -})
410 (&&), (||) :: Bool -> Bool -> Bool
425 %*********************************************************
427 \subsection{The @()@ type}
429 %*********************************************************
431 The Unit type is here because virtually any program needs it (whereas
432 some programs may get away without consulting PrelTup). Furthermore,
433 the renamer currently *always* asks for () to be in scope, so that
434 ccalls can use () as their default type; so when compiling PrelBase we
435 need (). (We could arrange suck in () only if -fglasgow-exts, but putting
436 it here seems more direct.
439 data () = () --easier to do explicitly: deriving (Eq, Ord, Enum, Show, Bounded)
440 -- (avoids weird-named functions, e.g., con2tag_()#
446 instance Ord () where
455 instance Enum () where
457 toEnum _ = error "Prelude.Enum.().toEnum: argument not 0"
460 enumFromThen () () = [()]
461 enumFromTo () () = [()]
462 enumFromThenTo () () () = [()]
464 instance Show () where
465 showsPrec p () = showString "()"
466 showList ls = showList__ (showsPrec 0) ls
469 %*********************************************************
471 \subsection{Type @Ordering@}
473 %*********************************************************
476 data Ordering = LT | EQ | GT deriving (Eq, Ord, Enum, Bounded, Show {- Read -})
480 %*********************************************************
482 \subsection{Type @Char@ and @String@}
484 %*********************************************************
489 data Char = C# Char# deriving (Eq, Ord)
491 instance Enum Char where
492 toEnum (I# i) | i >=# 0# && i <=# 255# = C# (chr# i)
493 | otherwise = error ("Prelude.Enum.Char.toEnum:out of range: " ++ show (I# i))
494 fromEnum (C# c) = I# (ord# c)
496 enumFrom (C# c) = efttCh (ord# c) 1# (># 255#)
497 enumFromTo (C# c1) (C# c2) = efttCh (ord# c1) 1# (># (ord# c2))
499 enumFromThen (C# c1) (C# c2)
500 | c1 `leChar#` c2 = efttCh (ord# c1) (ord# c2 -# ord# c1) (># 255#)
501 | otherwise = efttCh (ord# c1) (ord# c2 -# ord# c1) (<# 0#)
503 enumFromThenTo (C# c1) (C# c2) (C# c3)
504 | c1 `leChar#` c2 = efttCh (ord# c1) (ord# c2 -# ord# c1) (># (ord# c3))
505 | otherwise = efttCh (ord# c1) (ord# c2 -# ord# c1) (<# (ord# c3))
507 efttCh :: Int# -> Int# -> (Int# -> Bool) -> [Char]
511 go now | done now = []
512 | otherwise = C# (chr# now) : go (now +# step)
514 instance Show Char where
515 showsPrec p '\'' = showString "'\\''"
516 showsPrec p c = showChar '\'' . showLitChar c . showChar '\''
518 showList cs = showChar '"' . showl cs
519 where showl "" = showChar '"'
520 showl ('"':cs) = showString "\\\"" . showl cs
521 showl (c:cs) = showLitChar c . showl cs
526 isAscii, isLatin1, isControl, isPrint, isSpace, isUpper,
527 isLower, isAlpha, isDigit, isOctDigit, isHexDigit, isAlphanum :: Char -> Bool
528 isAscii c = fromEnum c < 128
529 isLatin1 c = c <= '\xff'
530 isControl c = c < ' ' || c >= '\DEL' && c <= '\x9f'
531 isPrint c = not (isControl c)
533 -- isSpace includes non-breaking space
534 -- Done with explicit equalities both for efficiency, and to avoid a tiresome
535 -- recursion with PrelList elem
536 isSpace c = c == ' ' ||
544 -- The upper case ISO characters have the multiplication sign dumped
545 -- randomly in the middle of the range. Go figure.
546 isUpper c = c >= 'A' && c <= 'Z' ||
547 c >= '\xC0' && c <= '\xD6' ||
548 c >= '\xD8' && c <= '\xDE'
549 -- The lower case ISO characters have the division sign dumped
550 -- randomly in the middle of the range. Go figure.
551 isLower c = c >= 'a' && c <= 'z' ||
552 c >= '\xDF' && c <= '\xF6' ||
553 c >= '\xF8' && c <= '\xFF'
554 isAlpha c = isLower c || isUpper c
555 isDigit c = c >= '0' && c <= '9'
556 isOctDigit c = c >= '0' && c <= '7'
557 isHexDigit c = isDigit c || c >= 'A' && c <= 'F' ||
559 isAlphanum c = isAlpha c || isDigit c
561 -- Case-changing operations
563 toUpper, toLower :: Char -> Char
564 toUpper c | isLower c && c /= '\xDF' && c /= '\xFF'
565 = toEnum (fromEnum c - fromEnum 'a' + fromEnum 'A')
568 toLower c | isUpper c = toEnum (fromEnum c - fromEnum 'A'
572 asciiTab = -- Using an array drags in the array module. listArray ('\NUL', ' ')
573 ["NUL", "SOH", "STX", "ETX", "EOT", "ENQ", "ACK", "BEL",
574 "BS", "HT", "LF", "VT", "FF", "CR", "SO", "SI",
575 "DLE", "DC1", "DC2", "DC3", "DC4", "NAK", "SYN", "ETB",
576 "CAN", "EM", "SUB", "ESC", "FS", "GS", "RS", "US",
580 %*********************************************************
582 \subsection{Type @Int@}
584 %*********************************************************
589 instance Eq Int where
590 (==) x y = x `eqInt` y
591 (/=) x y = x `neInt` y
593 instance Ord Int where
594 compare x y = compareInt x y
600 max x y = case (compareInt x y) of { LT -> y ; EQ -> x ; GT -> x }
601 min x y = case (compareInt x y) of { LT -> x ; EQ -> x ; GT -> y }
603 (I# x) `compareInt` (I# y) | x <# y = LT
607 instance Enum Int where
611 #ifndef USE_FOLDR_BUILD
612 enumFrom (I# c) = eftInt c 1#
613 enumFromTo (I# c1) (I# c2) = efttInt c1 1# (># c2)
614 enumFromThen (I# c1) (I# c2) = eftInt c1 (c2 -# c1)
616 enumFromThenTo (I# c1) (I# c2) (I# c3)
617 | c1 <=# c2 = efttInt c1 (c2 -# c1) (># c3)
618 | otherwise = efttInt c1 (c2 -# c1) (<# c3)
621 {-# INLINE enumFrom #-}
622 {-# INLINE enumFromTo #-}
623 enumFrom x = build (\ c _ ->
624 let g x = x `c` g (x `plusInt` 1) in g x)
625 enumFromTo x y = build (\ c n ->
626 let g x = if x <= y then x `c` g (x `plusInt` 1) else n in g x)
629 efttInt :: Int# -> Int# -> (Int# -> Bool) -> [Int]
630 efttInt now step done
633 go now | done now = []
634 | otherwise = I# now : go (now +# step)
636 eftInt :: Int# -> Int# -> [Int]
640 go now = I# now : go (now +# step)
643 instance Num Int where
644 (+) x y = plusInt x y
645 (-) x y = minusInt x y
646 negate x = negateInt x
647 (*) x y = timesInt x y
648 abs n = if n `geInt` 0 then n else (negateInt n)
650 signum n | n `ltInt` 0 = negateInt 1
654 fromInteger (J# a# s# d#)
655 = case (integer2Int# a# s# d#) of { i# -> I# i# }
659 instance Show Int where
660 showsPrec p n = showSignedInt p n
661 showList ls = showList__ (showsPrec 0) ls
665 %*********************************************************
667 \subsection{Type @Integer@, @Float@, @Double@}
669 %*********************************************************
672 data Float = F# Float#
673 data Double = D# Double#
674 data Integer = J# Int# Int# ByteArray#
676 instance Eq Integer where
677 (J# a1 s1 d1) == (J# a2 s2 d2)
678 = (cmpInteger# a1 s1 d1 a2 s2 d2) ==# 0#
680 (J# a1 s1 d1) /= (J# a2 s2 d2)
681 = (cmpInteger# a1 s1 d1 a2 s2 d2) /=# 0#
684 %*********************************************************
686 \subsection{The function type}
688 %*********************************************************
691 instance Show (a -> b) where
692 showsPrec p f = showString "<<function>>"
693 showList ls = showList__ (showsPrec 0) ls
704 -- function composition
706 (.) :: (b -> c) -> (a -> b) -> a -> c
709 -- flip f takes its (first) two arguments in the reverse order of f.
710 flip :: (a -> b -> c) -> b -> a -> c
713 -- right-associating infix application operator (useful in continuation-
715 ($) :: (a -> b) -> a -> b
718 -- until p f yields the result of applying f until p holds.
719 until :: (a -> Bool) -> (a -> a) -> a -> a
720 until p f x | p x = x
721 | otherwise = until p f (f x)
723 -- asTypeOf is a type-restricted version of const. It is usually used
724 -- as an infix operator, and its typing forces its first argument
725 -- (which is usually overloaded) to have the same type as the second.
726 asTypeOf :: a -> a -> a
731 %*********************************************************
733 \subsection{Miscellaneous}
735 %*********************************************************
745 %*********************************************************
747 \subsection{Support code for @Show@}
749 %*********************************************************
752 shows :: (Show a) => a -> ShowS
755 show :: (Show a) => a -> String
758 showChar :: Char -> ShowS
761 showString :: String -> ShowS
764 showParen :: Bool -> ShowS -> ShowS
765 showParen b p = if b then showChar '(' . p . showChar ')' else p
767 showList__ :: (a -> ShowS) -> [a] -> ShowS
769 showList__ showx [] = showString "[]"
770 showList__ showx (x:xs) = showChar '[' . showx x . showl xs
772 showl [] = showChar ']'
773 showl (x:xs) = showChar ',' . showx x . showl xs
776 showSpace = {-showChar ' '-} \ xs -> ' ' : xs
779 Code specific for characters
782 showLitChar :: Char -> ShowS
783 showLitChar c | c > '\DEL' = showChar '\\' . protectEsc isDigit (shows (ord c))
784 showLitChar '\DEL' = showString "\\DEL"
785 showLitChar '\\' = showString "\\\\"
786 showLitChar c | c >= ' ' = showChar c
787 showLitChar '\a' = showString "\\a"
788 showLitChar '\b' = showString "\\b"
789 showLitChar '\f' = showString "\\f"
790 showLitChar '\n' = showString "\\n"
791 showLitChar '\r' = showString "\\r"
792 showLitChar '\t' = showString "\\t"
793 showLitChar '\v' = showString "\\v"
794 showLitChar '\SO' = protectEsc (== 'H') (showString "\\SO")
795 showLitChar c = showString ('\\' : asciiTab!!ord c)
797 protectEsc p f = f . cont
798 where cont s@(c:_) | p c = "\\&" ++ s
801 intToDigit :: Int -> Char
803 | i >= 0 && i <= 9 = toEnum (fromEnum '0' + i)
804 | i >= 10 && i <= 15 = toEnum (fromEnum 'a' + i -10)
805 | otherwise = error ("Char.intToDigit: not a digit" ++ show i)
809 Code specific for Ints.
812 showSignedInt :: Int -> Int -> ShowS
813 showSignedInt p (I# n) r
814 = -- from HBC version; support code follows
815 if n <# 0# && p > 6 then '(':itos n++(')':r) else itos n ++ r
817 itos :: Int# -> String
820 if negateInt# n <# 0# then
821 -- n is minInt, a difficult number
822 itos (n `quotInt#` 10#) ++ itos' (negateInt# (n `remInt#` 10#)) []
824 '-':itos' (negateInt# n) []
828 itos' :: Int# -> String -> String
831 C# (chr# (n +# ord# '0'#)) : cs
833 itos' (n `quotInt#` 10#) (C# (chr# (n `remInt#` 10# +# ord# '0'#)) : cs)
836 %*********************************************************
838 \subsection{Numeric primops}
840 %*********************************************************
842 Definitions of the boxed PrimOps; these will be
843 used in the case of partial applications, etc.
849 plusInt (I# x) (I# y) = I# (x +# y)
850 minusInt(I# x) (I# y) = I# (x -# y)
851 timesInt(I# x) (I# y) = I# (x *# y)
852 quotInt (I# x) (I# y) = I# (quotInt# x y)
853 remInt (I# x) (I# y) = I# (remInt# x y)
854 negateInt (I# x) = I# (negateInt# x)
855 gtInt (I# x) (I# y) = x ># y
856 geInt (I# x) (I# y) = x >=# y
857 eqInt (I# x) (I# y) = x ==# y
858 neInt (I# x) (I# y) = x /=# y
859 ltInt (I# x) (I# y) = x <# y
860 leInt (I# x) (I# y) = x <=# y
863 Convenient boxed Integer PrimOps. These are 'thin-air' Ids, so
864 it's nice to have them in PrelBase.
867 {-# INLINE int2Integer #-}
868 {-# INLINE addr2Integer #-}
869 int2Integer i = case int2Integer# i of (# a, s, d #) -> J# a s d
870 addr2Integer s = case addr2Integer# s of (# a, s, d #) -> J# a s d
872 integer_0, integer_1, integer_2, integer_m1 :: Integer
873 integer_0 = int2Integer 0#
874 integer_1 = int2Integer 1#
875 integer_2 = int2Integer 2#
876 integer_m1 = int2Integer (negateInt# 1#)