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 data [] a = [] | a : [a] -- do explicitly: deriving (Eq, Ord)
37 -- to avoid weird names like con2tag_[]#
38 instance Functor [] where
40 map f (x:xs) = f x : [] -- map f xs
43 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 )
61 -------------- Stage 2 -----------------------
68 maybe :: b -> (a -> b) -> Maybe a -> b
70 maybe n f (Just x) = f x
72 -------------- Stage 3 -----------------------
74 (==), (/=) :: a -> a -> Bool
78 -- f :: Eq a => a -> a -> Bool
81 g :: Eq a => a -> a -> Bool
84 -------------- Stage 4 -----------------------
86 class (Eq a) => Ord a where
87 compare :: a -> a -> Ordering
88 (<), (<=), (>=), (>):: a -> a -> Bool
89 max, min :: a -> a -> a
91 -- An instance of Ord should define either compare or <=
92 -- Using compare can be more efficient for complex types.
98 x <= y = compare x y /= GT
99 x < y = compare x y == LT
100 x >= y = compare x y /= LT
101 x > y = compare x y == GT
102 max x y = case (compare x y) of { LT -> y ; EQ -> x ; GT -> x }
103 min x y = case (compare x y) of { LT -> x ; EQ -> x ; GT -> y }
105 eqInt (I# x) (I# y) = x ==# y
107 instance Eq Int where
108 (==) x y = x `eqInt` y
110 instance Ord Int where
111 compare x y = error "help"
113 class Bounded a where
114 minBound, maxBound :: a
117 type ShowS = String -> String
120 showsPrec :: Bool -> a -> ShowS
121 showList :: [a] -> ShowS
123 showList ls = showList__ (showsPrec True) ls
125 showList__ :: (a -> ShowS) -> [a] -> ShowS
126 showList__ showx [] = showString "[]"
128 showString :: String -> ShowS
133 shows :: (Show a) => a -> ShowS
134 shows = showsPrec True
136 -- show :: (Show a) => a -> String
137 --show x = shows x ""
142 %*********************************************************
144 \subsection{Standard classes @Eq@, @Ord@, @Bounded@, @Eval@}
146 %*********************************************************
150 (==), (/=) :: a -> a -> Bool
152 x /= y = not (x == y)
154 class (Eq a) => Ord a where
155 compare :: a -> a -> Ordering
156 (<), (<=), (>=), (>):: a -> a -> Bool
157 max, min :: a -> a -> a
159 -- An instance of Ord should define either compare or <=
160 -- Using compare can be more efficient for complex types.
166 x <= y = compare x y /= GT
167 x < y = compare x y == LT
168 x >= y = compare x y /= LT
169 x > y = compare x y == GT
170 max x y = case (compare x y) of { LT -> y ; EQ -> x ; GT -> x }
171 min x y = case (compare x y) of { LT -> x ; EQ -> x ; GT -> y }
173 class Bounded a where
174 minBound, maxBound :: a
176 -- Leave this in for now; to make it easier to silently
177 -- discard Evals from Haskell 1.4 contexts
181 %*********************************************************
183 \subsection{Monadic classes @Functor@, @Monad@, @MonadZero@, @MonadPlus@}
185 %*********************************************************
188 class Functor f where
189 map :: (a -> b) -> f a -> f b
192 (>>=) :: m a -> (a -> m b) -> m b
193 (>>) :: m a -> m b -> m b
196 m >> k = m >>= \_ -> k
198 class (Monad m) => MonadZero m where
201 class (MonadZero m) => MonadPlus m where
202 (++) :: m a -> m a -> m a
206 %*********************************************************
208 \subsection{Classes @Num@ and @Enum@}
210 %*********************************************************
216 enumFrom :: a -> [a] -- [n..]
217 enumFromThen :: a -> a -> [a] -- [n,n'..]
218 enumFromTo :: a -> a -> [a] -- [n..m]
219 enumFromThenTo :: a -> a -> a -> [a] -- [n,n'..m]
221 enumFromTo n m = map toEnum [fromEnum n .. fromEnum m]
222 enumFromThenTo n n' m
223 = map toEnum [fromEnum n, fromEnum n' .. fromEnum m]
225 class (Eq a, Show a) => Num a where
226 (+), (-), (*) :: a -> a -> a
228 abs, signum :: a -> a
229 fromInteger :: Integer -> a
230 fromInt :: Int -> a -- partain: Glasgow extension
233 fromInt (I# i#) = fromInteger (case int2Integer# i# of
234 (# a, s, d #) -> J# a s d)
235 -- Go via the standard class-op if the
236 -- non-standard one ain't provided
240 {-# SPECIALISE succ :: Int -> Int #-}
241 {-# SPECIALISE pred :: Int -> Int #-}
242 succ, pred :: Enum a => a -> a
243 succ = toEnum . (+1) . fromEnum
244 pred = toEnum . (subtract 1) . fromEnum
246 chr = (toEnum :: Int -> Char)
247 ord = (fromEnum :: Char -> Int)
250 ord_0 = fromInt (ord '0')
252 {-# SPECIALISE subtract :: Int -> Int -> Int #-}
253 subtract :: (Num a) => a -> a -> a
258 %*********************************************************
260 \subsection{The @Show@ class}
262 %*********************************************************
265 type ShowS = String -> String
268 showsPrec :: Int -> a -> ShowS
269 showList :: [a] -> ShowS
271 showList ls = showList__ (showsPrec 0) ls
274 %*********************************************************
276 \subsection{The list type}
278 %*********************************************************
281 data [] a = [] | a : [a] -- do explicitly: deriving (Eq, Ord)
282 -- to avoid weird names like con2tag_[]#
284 instance (Eq a) => Eq [a] where
286 (x:xs) == (y:ys) = x == y && xs == ys
288 xs /= ys = if (xs == ys) then False else True
290 instance (Ord a) => Ord [a] where
291 a < b = case compare a b of { LT -> True; EQ -> False; GT -> False }
292 a <= b = case compare a b of { LT -> True; EQ -> True; GT -> False }
293 a >= b = case compare a b of { LT -> False; EQ -> True; GT -> True }
294 a > b = case compare a b of { LT -> False; EQ -> False; GT -> True }
296 max a b = case compare a b of { LT -> b; EQ -> a; GT -> a }
297 min a b = case compare a b of { LT -> a; EQ -> a; GT -> b }
300 compare (x:xs) [] = GT
301 compare [] (y:ys) = LT
302 compare (x:xs) (y:ys) = case compare x y of
307 instance Functor [] where
309 map f (x:xs) = f x : map f xs
311 instance Monad [] where
312 m >>= k = foldr ((++) . k) [] m
313 m >> k = foldr ((++) . (\ _ -> k)) [] m
316 instance MonadZero [] where
319 instance MonadPlus [] where
320 #ifdef USE_REPORT_PRELUDE
321 xs ++ ys = foldr (:) ys xs
324 (x:xs) ++ ys = x : (xs ++ ys)
327 instance (Show a) => Show [a] where
328 showsPrec p = showList
329 showList ls = showList__ (showsPrec 0) ls
334 A few list functions that appear here because they are used here.
335 The rest of the prelude list functions are in PrelList.
338 foldr :: (a -> b -> b) -> b -> [a] -> b
340 foldr f z (x:xs) = f x (foldr f z xs)
342 -- takeWhile, applied to a predicate p and a list xs, returns the longest
343 -- prefix (possibly empty) of xs of elements that satisfy p. dropWhile p xs
344 -- returns the remaining suffix. Span p xs is equivalent to
345 -- (takeWhile p xs, dropWhile p xs), while break p uses the negation of p.
347 takeWhile :: (a -> Bool) -> [a] -> [a]
350 | p x = x : takeWhile p xs
353 dropWhile :: (a -> Bool) -> [a] -> [a]
355 dropWhile p xs@(x:xs')
356 | p x = dropWhile p xs'
359 -- List index (subscript) operator, 0-origin
360 (!!) :: [a] -> Int -> a
361 #ifdef USE_REPORT_PRELUDE
363 (_:xs) !! n | n > 0 = xs !! (n-1)
364 (_:_) !! _ = error "PreludeList.!!: negative index"
365 [] !! _ = error "PreludeList.!!: index too large"
367 -- HBC version (stolen), then unboxified
368 -- The semantics is not quite the same for error conditions
369 -- in the more efficient version.
371 _ !! n | n < 0 = error "(!!){PreludeList}: negative index\n"
372 xs !! n = sub xs (case n of { I# n# -> n# })
373 where sub :: [a] -> Int# -> a
374 sub [] _ = error "(!!){PreludeList}: index too large\n"
375 sub (x:xs) n# = if n# ==# 0#
377 else sub xs (n# -# 1#)
382 %*********************************************************
384 \subsection{Type @Void@}
386 %*********************************************************
388 The type @Void@ is built in, but it needs a @Show@ instance.
392 void = error "You tried to evaluate void"
394 instance Show Void where
395 showsPrec p f = showString "<<void>>"
396 showList ls = showList__ (showsPrec 0) ls
400 %*********************************************************
402 \subsection{Type @Bool@}
404 %*********************************************************
407 data Bool = False | True deriving (Eq, Ord, Enum, Bounded, Show {- Read -})
411 (&&), (||) :: Bool -> Bool -> Bool
426 %*********************************************************
428 \subsection{The @()@ type}
430 %*********************************************************
432 The Unit type is here because virtually any program needs it (whereas
433 some programs may get away without consulting PrelTup). Furthermore,
434 the renamer currently *always* asks for () to be in scope, so that
435 ccalls can use () as their default type; so when compiling PrelBase we
436 need (). (We could arrange suck in () only if -fglasgow-exts, but putting
437 it here seems more direct.
440 data () = () --easier to do explicitly: deriving (Eq, Ord, Enum, Show, Bounded)
441 -- (avoids weird-named functions, e.g., con2tag_()#
447 instance Ord () where
456 instance Enum () where
458 toEnum _ = error "Prelude.Enum.().toEnum: argument not 0"
461 enumFromThen () () = [()]
462 enumFromTo () () = [()]
463 enumFromThenTo () () () = [()]
465 instance Show () where
466 showsPrec p () = showString "()"
467 showList ls = showList__ (showsPrec 0) ls
470 %*********************************************************
472 \subsection{Type @Ordering@}
474 %*********************************************************
477 data Ordering = LT | EQ | GT deriving (Eq, Ord, Enum, Bounded, Show {- Read -})
481 %*********************************************************
483 \subsection{Type @Char@ and @String@}
485 %*********************************************************
490 data Char = C# Char# deriving (Eq, Ord)
492 instance Enum Char where
493 toEnum (I# i) | i >=# 0# && i <=# 255# = C# (chr# i)
494 | otherwise = error ("Prelude.Enum.Char.toEnum:out of range: " ++ show (I# i))
495 fromEnum (C# c) = I# (ord# c)
497 enumFrom (C# c) = efttCh (ord# c) 1# (># 255#)
498 enumFromTo (C# c1) (C# c2) = efttCh (ord# c1) 1# (># (ord# c2))
500 enumFromThen (C# c1) (C# c2)
501 | c1 `leChar#` c2 = efttCh (ord# c1) (ord# c2 -# ord# c1) (># 255#)
502 | otherwise = efttCh (ord# c1) (ord# c2 -# ord# c1) (<# 0#)
504 enumFromThenTo (C# c1) (C# c2) (C# c3)
505 | c1 `leChar#` c2 = efttCh (ord# c1) (ord# c2 -# ord# c1) (># (ord# c3))
506 | otherwise = efttCh (ord# c1) (ord# c2 -# ord# c1) (<# (ord# c3))
508 efttCh :: Int# -> Int# -> (Int# -> Bool) -> [Char]
512 go now | done now = []
513 | otherwise = C# (chr# now) : go (now +# step)
515 instance Show Char where
516 showsPrec p '\'' = showString "'\\''"
517 showsPrec p c = showChar '\'' . showLitChar c . showChar '\''
519 showList cs = showChar '"' . showl cs
520 where showl "" = showChar '"'
521 showl ('"':cs) = showString "\\\"" . showl cs
522 showl (c:cs) = showLitChar c . showl cs
527 isAscii, isLatin1, isControl, isPrint, isSpace, isUpper,
528 isLower, isAlpha, isDigit, isOctDigit, isHexDigit, isAlphanum :: Char -> Bool
529 isAscii c = fromEnum c < 128
530 isLatin1 c = c <= '\xff'
531 isControl c = c < ' ' || c >= '\DEL' && c <= '\x9f'
532 isPrint c = not (isControl c)
534 -- isSpace includes non-breaking space
535 -- Done with explicit equalities both for efficiency, and to avoid a tiresome
536 -- recursion with PrelList elem
537 isSpace c = c == ' ' ||
545 -- The upper case ISO characters have the multiplication sign dumped
546 -- randomly in the middle of the range. Go figure.
547 isUpper c = c >= 'A' && c <= 'Z' ||
548 c >= '\xC0' && c <= '\xD6' ||
549 c >= '\xD8' && c <= '\xDE'
550 -- The lower case ISO characters have the division sign dumped
551 -- randomly in the middle of the range. Go figure.
552 isLower c = c >= 'a' && c <= 'z' ||
553 c >= '\xDF' && c <= '\xF6' ||
554 c >= '\xF8' && c <= '\xFF'
555 isAlpha c = isLower c || isUpper c
556 isDigit c = c >= '0' && c <= '9'
557 isOctDigit c = c >= '0' && c <= '7'
558 isHexDigit c = isDigit c || c >= 'A' && c <= 'F' ||
560 isAlphanum c = isAlpha c || isDigit c
562 -- Case-changing operations
564 toUpper, toLower :: Char -> Char
565 toUpper c | isLower c && c /= '\xDF' && c /= '\xFF'
566 = toEnum (fromEnum c - fromEnum 'a' + fromEnum 'A')
569 toLower c | isUpper c = toEnum (fromEnum c - fromEnum 'A'
573 asciiTab = -- Using an array drags in the array module. listArray ('\NUL', ' ')
574 ["NUL", "SOH", "STX", "ETX", "EOT", "ENQ", "ACK", "BEL",
575 "BS", "HT", "LF", "VT", "FF", "CR", "SO", "SI",
576 "DLE", "DC1", "DC2", "DC3", "DC4", "NAK", "SYN", "ETB",
577 "CAN", "EM", "SUB", "ESC", "FS", "GS", "RS", "US",
581 %*********************************************************
583 \subsection{Type @Int@}
585 %*********************************************************
590 instance Eq Int where
591 (==) x y = x `eqInt` y
592 (/=) x y = x `neInt` y
594 instance Ord Int where
595 compare x y = compareInt x y
601 max x y = case (compareInt x y) of { LT -> y ; EQ -> x ; GT -> x }
602 min x y = case (compareInt x y) of { LT -> x ; EQ -> x ; GT -> y }
604 (I# x) `compareInt` (I# y) | x <# y = LT
608 instance Enum Int where
612 #ifndef USE_FOLDR_BUILD
613 enumFrom (I# c) = eftInt c 1#
614 enumFromTo (I# c1) (I# c2) = efttInt c1 1# (># c2)
615 enumFromThen (I# c1) (I# c2) = eftInt c1 (c2 -# c1)
617 enumFromThenTo (I# c1) (I# c2) (I# c3)
618 | c1 <=# c2 = efttInt c1 (c2 -# c1) (># c3)
619 | otherwise = efttInt c1 (c2 -# c1) (<# c3)
622 {-# INLINE enumFrom #-}
623 {-# INLINE enumFromTo #-}
624 enumFrom x = build (\ c _ ->
625 let g x = x `c` g (x `plusInt` 1) in g x)
626 enumFromTo x y = build (\ c n ->
627 let g x = if x <= y then x `c` g (x `plusInt` 1) else n in g x)
630 efttInt :: Int# -> Int# -> (Int# -> Bool) -> [Int]
631 efttInt now step done
634 go now | done now = []
635 | otherwise = I# now : go (now +# step)
637 eftInt :: Int# -> Int# -> [Int]
641 go now = I# now : go (now +# step)
644 instance Num Int where
645 (+) x y = plusInt x y
646 (-) x y = minusInt x y
647 negate x = negateInt x
648 (*) x y = timesInt x y
649 abs n = if n `geInt` 0 then n else (negateInt n)
651 signum n | n `ltInt` 0 = negateInt 1
655 fromInteger (J# a# s# d#)
656 = case (integer2Int# a# s# d#) of { i# -> I# i# }
660 instance Show Int where
661 showsPrec p n = showSignedInt p n
662 showList ls = showList__ (showsPrec 0) ls
666 %*********************************************************
668 \subsection{Type @Integer@, @Float@, @Double@}
670 %*********************************************************
673 data Float = F# Float#
674 data Double = D# Double#
675 data Integer = J# Int# Int# ByteArray#
677 instance Eq Integer where
678 (J# a1 s1 d1) == (J# a2 s2 d2)
679 = (cmpInteger# a1 s1 d1 a2 s2 d2) ==# 0#
681 (J# a1 s1 d1) /= (J# a2 s2 d2)
682 = (cmpInteger# a1 s1 d1 a2 s2 d2) /=# 0#
685 %*********************************************************
687 \subsection{The function type}
689 %*********************************************************
692 instance Show (a -> b) where
693 showsPrec p f = showString "<<function>>"
694 showList ls = showList__ (showsPrec 0) ls
705 -- function composition
707 (.) :: (b -> c) -> (a -> b) -> a -> c
710 -- flip f takes its (first) two arguments in the reverse order of f.
711 flip :: (a -> b -> c) -> b -> a -> c
714 -- right-associating infix application operator (useful in continuation-
716 ($) :: (a -> b) -> a -> b
719 -- until p f yields the result of applying f until p holds.
720 until :: (a -> Bool) -> (a -> a) -> a -> a
721 until p f x | p x = x
722 | otherwise = until p f (f x)
724 -- asTypeOf is a type-restricted version of const. It is usually used
725 -- as an infix operator, and its typing forces its first argument
726 -- (which is usually overloaded) to have the same type as the second.
727 asTypeOf :: a -> a -> a
732 %*********************************************************
734 \subsection{Miscellaneous}
736 %*********************************************************
746 %*********************************************************
748 \subsection{Support code for @Show@}
750 %*********************************************************
753 shows :: (Show a) => a -> ShowS
756 show :: (Show a) => a -> String
759 showChar :: Char -> ShowS
762 showString :: String -> ShowS
765 showParen :: Bool -> ShowS -> ShowS
766 showParen b p = if b then showChar '(' . p . showChar ')' else p
768 showList__ :: (a -> ShowS) -> [a] -> ShowS
770 showList__ showx [] = showString "[]"
771 showList__ showx (x:xs) = showChar '[' . showx x . showl xs
773 showl [] = showChar ']'
774 showl (x:xs) = showChar ',' . showx x . showl xs
777 showSpace = {-showChar ' '-} \ xs -> ' ' : xs
780 Code specific for characters
783 showLitChar :: Char -> ShowS
784 showLitChar c | c > '\DEL' = showChar '\\' . protectEsc isDigit (shows (ord c))
785 showLitChar '\DEL' = showString "\\DEL"
786 showLitChar '\\' = showString "\\\\"
787 showLitChar c | c >= ' ' = showChar c
788 showLitChar '\a' = showString "\\a"
789 showLitChar '\b' = showString "\\b"
790 showLitChar '\f' = showString "\\f"
791 showLitChar '\n' = showString "\\n"
792 showLitChar '\r' = showString "\\r"
793 showLitChar '\t' = showString "\\t"
794 showLitChar '\v' = showString "\\v"
795 showLitChar '\SO' = protectEsc (== 'H') (showString "\\SO")
796 showLitChar c = showString ('\\' : asciiTab!!ord c)
798 protectEsc p f = f . cont
799 where cont s@(c:_) | p c = "\\&" ++ s
802 intToDigit :: Int -> Char
804 | i >= 0 && i <= 9 = toEnum (fromEnum '0' + i)
805 | i >= 10 && i <= 15 = toEnum (fromEnum 'a' + i -10)
806 | otherwise = error ("Char.intToDigit: not a digit" ++ show i)
810 Code specific for Ints.
813 showSignedInt :: Int -> Int -> ShowS
814 showSignedInt p (I# n) r
815 = -- from HBC version; support code follows
816 if n <# 0# && p > 6 then '(':itos n++(')':r) else itos n ++ r
818 itos :: Int# -> String
821 if negateInt# n <# 0# then
822 -- n is minInt, a difficult number
823 itos (n `quotInt#` 10#) ++ itos' (negateInt# (n `remInt#` 10#)) []
825 '-':itos' (negateInt# n) []
829 itos' :: Int# -> String -> String
832 C# (chr# (n +# ord# '0'#)) : cs
834 itos' (n `quotInt#` 10#) (C# (chr# (n `remInt#` 10# +# ord# '0'#)) : cs)
837 %*********************************************************
839 \subsection{Numeric primops}
841 %*********************************************************
843 Definitions of the boxed PrimOps; these will be
844 used in the case of partial applications, etc.
850 plusInt (I# x) (I# y) = I# (x +# y)
851 minusInt(I# x) (I# y) = I# (x -# y)
852 timesInt(I# x) (I# y) = I# (x *# y)
853 quotInt (I# x) (I# y) = I# (quotInt# x y)
854 remInt (I# x) (I# y) = I# (remInt# x y)
855 negateInt (I# x) = I# (negateInt# x)
856 gtInt (I# x) (I# y) = x ># y
857 geInt (I# x) (I# y) = x >=# y
858 eqInt (I# x) (I# y) = x ==# y
859 neInt (I# x) (I# y) = x /=# y
860 ltInt (I# x) (I# y) = x <# y
861 leInt (I# x) (I# y) = x <=# y
864 Convenient boxed Integer PrimOps. These are 'thin-air' Ids, so
865 it's nice to have them in PrelBase.
868 {-# INLINE int2Integer #-}
869 {-# INLINE addr2Integer #-}
870 int2Integer i = case int2Integer# i of (# a, s, d #) -> J# a s d
871 addr2Integer s = case addr2Integer# s of (# a, s, d #) -> J# a s d
873 integer_0, integer_1, integer_2, integer_m1 :: Integer
874 integer_0 = int2Integer 0#
875 integer_1 = int2Integer 1#
876 integer_2 = int2Integer 2#
877 integer_m1 = int2Integer (negateInt# 1#)