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 %*********************************************************
99 class (Ord a) => Enum a where
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 = takeWhile (<= m) (enumFrom n)
108 enumFromThenTo n n' m
109 = takeWhile (if n' >= n then (<= m) else (>= m))
112 class (Eq a, Show a, Eval a) => Num a where
113 (+), (-), (*) :: a -> a -> a
115 abs, signum :: a -> a
116 fromInteger :: Integer -> a
117 fromInt :: Int -> a -- partain: Glasgow extension
120 fromInt (I# i#) = fromInteger (int2Integer# i#)
121 -- Go via the standard class-op if the
122 -- non-standard one ain't provided
126 succ, pred :: Enum a => a -> a
127 succ = toEnum . (+1) . fromEnum
128 pred = toEnum . (subtract 1) . fromEnum
130 chr = (toEnum :: Int -> Char)
131 ord = (fromEnum :: Char -> Int)
134 ord_0 = fromInt (ord '0')
136 {-# GENERATE_SPECS subtract a{Int#,Double#,Int,Double,Complex(Double#),Complex(Double)} #-}
137 subtract :: (Num a) => a -> a -> a
142 %*********************************************************
144 \subsection{The @Show@ class}
146 %*********************************************************
149 type ShowS = String -> String
152 showsPrec :: Int -> a -> ShowS
153 showList :: [a] -> ShowS
155 showList ls = showList__ (showsPrec 0) ls
158 %*********************************************************
160 \subsection{The list type}
162 %*********************************************************
165 data [] a = [] | a : [a] -- do explicitly: deriving (Eq, Ord)
166 -- to avoid weird names like con2tag_[]#
168 instance (Eq a) => Eq [a] where
170 (x:xs) == (y:ys) = x == y && xs == ys
172 xs /= ys = if (xs == ys) then False else True
174 instance (Ord a) => Ord [a] where
175 a < b = case compare a b of { LT -> True; EQ -> False; GT -> False }
176 a <= b = case compare a b of { LT -> True; EQ -> True; GT -> False }
177 a >= b = case compare a b of { LT -> False; EQ -> True; GT -> True }
178 a > b = case compare a b of { LT -> False; EQ -> False; GT -> True }
180 max a b = case compare a b of { LT -> b; EQ -> a; GT -> a }
181 min a b = case compare a b of { LT -> a; EQ -> a; GT -> b }
184 compare (x:xs) [] = GT
185 compare [] (y:ys) = LT
186 compare (x:xs) (y:ys) = case compare x y of
191 instance Functor [] where
193 map f (x:xs) = f x : map f xs
195 instance Monad [] where
196 m >>= k = foldr ((++) . k) [] m
197 m >> k = foldr ((++) . (\ _ -> k)) [] m
200 instance MonadZero [] where
203 instance MonadPlus [] where
204 xs ++ ys = foldr (:) ys xs
206 instance (Show a) => Show [a] where
207 showsPrec p = showList
208 showList ls = showList__ (showsPrec 0) ls
213 A few list functions that appear here because they are used here.
214 The rest of the prelude list functions are in PrelList.
217 foldr :: (a -> b -> b) -> b -> [a] -> b
219 foldr f z (x:xs) = f x (foldr f z xs)
221 -- takeWhile, applied to a predicate p and a list xs, returns the longest
222 -- prefix (possibly empty) of xs of elements that satisfy p. dropWhile p xs
223 -- returns the remaining suffix. Span p xs is equivalent to
224 -- (takeWhile p xs, dropWhile p xs), while break p uses the negation of p.
226 takeWhile :: (a -> Bool) -> [a] -> [a]
229 | p x = x : takeWhile p xs
232 dropWhile :: (a -> Bool) -> [a] -> [a]
234 dropWhile p xs@(x:xs')
235 | p x = dropWhile p xs'
238 -- List index (subscript) operator, 0-origin
239 (!!) :: [a] -> Int -> a
241 (_:xs) !! n | n > 0 = xs !! (n-1)
242 (_:_) !! _ = error "PreludeList.!!: negative index"
243 [] !! _ = error "PreludeList.!!: index too large"
247 %*********************************************************
249 \subsection{Type @Void@}
251 %*********************************************************
253 The type @Void@ is built in, but it needs a @Show@ instance.
257 void = error "You tried to evaluate void"
259 instance Show Void where
260 showsPrec p f = showString "<<void>>"
261 showList ls = showList__ (showsPrec 0) ls
265 %*********************************************************
267 \subsection{Type @Bool@}
269 %*********************************************************
272 data Bool = False | True deriving (Eq, Ord, Enum, Bounded, Show {- Read -})
276 (&&), (||) :: Bool -> Bool -> Bool
291 %*********************************************************
293 \subsection{Type @Maybe@}
295 %*********************************************************
298 data Maybe a = Nothing | Just a deriving (Eq, Ord, Show {- Read -})
300 maybe :: b -> (a -> b) -> Maybe a -> b
301 maybe n f Nothing = n
302 maybe n f (Just x) = f x
304 instance Functor Maybe where
305 map f Nothing = Nothing
306 map f (Just a) = Just (f a)
308 instance Monad Maybe where
310 Nothing >>= k = Nothing
313 Nothing >> k = Nothing
317 instance MonadZero Maybe where
320 instance MonadPlus Maybe where
326 %*********************************************************
328 \subsection{The @()@ type}
330 %*********************************************************
332 The Unit type is here because virtually any program needs it (whereas
333 some programs may get away without consulting PrelTup). Furthermore,
334 the renamer currently *always* asks for () to be in scope, so that
335 ccalls can use () as their default type; so when compiling PrelBase we
336 need (). (We could arrange suck in () only if -fglasgow-exts, but putting
337 it here seems more direct.
340 data () = () --easier to do explicitly: deriving (Eq, Ord, Enum, Show, Bounded)
341 -- (avoids weird-named functions, e.g., con2tag_()#
346 instance Ord () where
355 instance Enum () where
357 toEnum _ = error "Prelude.Enum.().toEnum: argument not 0"
360 enumFromThen () () = [()]
361 enumFromTo () () = [()]
362 enumFromThenTo () () () = [()]
364 instance Bounded () where
368 instance Show () where
369 showsPrec p () = showString "()"
370 showList ls = showList__ (showsPrec 0) ls
373 %*********************************************************
375 \subsection{Type @Either@}
377 %*********************************************************
380 data Either a b = Left a | Right b deriving (Eq, Ord, Show {- Read -} )
382 either :: (a -> c) -> (b -> c) -> Either a b -> c
383 either f g (Left x) = f x
384 either f g (Right y) = g y
388 %*********************************************************
390 \subsection{Type @Ordering@}
392 %*********************************************************
395 data Ordering = LT | EQ | GT deriving (Eq, Ord, Enum, Bounded, Show {- Read -})
399 %*********************************************************
401 \subsection{Type @Char@ and @String@}
403 %*********************************************************
408 data Char = C# Char# deriving (Eq, Ord)
410 instance Enum Char where
411 toEnum (I# i) | i >=# 0# && i <=# 255# = C# (chr# i)
412 | otherwise = error ("Prelude.Enum.Char.toEnum:out of range: " ++ show (I# i))
413 fromEnum (C# c) = I# (ord# c)
415 enumFrom (C# c) = eftt (ord# c) 1# 255#
416 enumFromThen (C# c1) (C# c2) = eftt (ord# c1) (ord# c2 -# ord# c1) 255#
417 enumFromTo (C# c1) (C# c2) = eftt (ord# c1) 1# (ord# c2)
418 enumFromThenTo (C# c1) (C# c2) (C# c3) = eftt (ord# c1) (ord# c2 -# ord# c1) (ord# c3)
420 eftt :: Int# -> Int# -> Int# -> [Char]
424 go now | now ># limit = []
425 | otherwise = C# (chr# now) : go (now +# step)
427 instance Bounded Char where
431 instance Show Char where
432 showsPrec p '\'' = showString "'\\''"
433 showsPrec p c = showChar '\'' . showLitChar c . showChar '\''
435 showList cs = showChar '"' . showl cs
436 where showl "" = showChar '"'
437 showl ('"':cs) = showString "\\\"" . showl cs
438 showl (c:cs) = showLitChar c . showl cs
443 isAscii, isLatin1, isControl, isPrint, isSpace, isUpper,
444 isLower, isAlpha, isDigit, isOctDigit, isHexDigit, isAlphanum :: Char -> Bool
445 isAscii c = fromEnum c < 128
446 isLatin1 c = c <= '\xff'
447 isControl c = c < ' ' || c >= '\DEL' && c <= '\x9f'
448 isPrint c = not (isControl c)
450 -- isSpace includes non-breaking space
451 -- Done with explicit equalities both for efficiency, and to avoid a tiresome
452 -- recursion with PrelList elem
453 isSpace c = c == ' ' ||
461 -- The upper case ISO characters have the multiplication sign dumped
462 -- randomly in the middle of the range. Go figure.
463 isUpper c = c >= 'A' && c <= 'Z' ||
464 c >= '\xC0' && c <= '\xD6' ||
465 c >= '\xD8' && c <= '\xDE'
466 -- The lower case ISO characters have the division sign dumped
467 -- randomly in the middle of the range. Go figure.
468 isLower c = c >= 'a' && c <= 'z' ||
469 c >= '\xDF' && c <= '\xF6' ||
470 c >= '\xF8' && c <= '\xFF'
471 isAlpha c = isLower c || isUpper c
472 isDigit c = c >= '0' && c <= '9'
473 isOctDigit c = c >= '0' && c <= '7'
474 isHexDigit c = isDigit c || c >= 'A' && c <= 'F' ||
476 isAlphanum c = isAlpha c || isDigit c
478 -- Case-changing operations
480 toUpper, toLower :: Char -> Char
481 toUpper c | isLower c && c /= '\xDF' && c /= '\xFF'
482 = toEnum (fromEnum c - fromEnum 'a' + fromEnum 'A')
485 toLower c | isUpper c = toEnum (fromEnum c - fromEnum 'A'
489 asciiTab = -- Using an array drags in the array module. listArray ('\NUL', ' ')
490 ["NUL", "SOH", "STX", "ETX", "EOT", "ENQ", "ACK", "BEL",
491 "BS", "HT", "LF", "VT", "FF", "CR", "SO", "SI",
492 "DLE", "DC1", "DC2", "DC3", "DC4", "NAK", "SYN", "ETB",
493 "CAN", "EM", "SUB", "ESC", "FS", "GS", "RS", "US",
497 %*********************************************************
499 \subsection{Type @Int@}
501 %*********************************************************
506 instance Eq Int where
507 (==) x y = x `eqInt` y
508 (/=) x y = x `neInt` y
510 instance Ord Int where
511 compare x y = compareInt x y
517 max x y = case (compareInt x y) of { LT -> y ; EQ -> x ; GT -> x }
518 min x y = case (compareInt x y) of { LT -> x ; EQ -> x ; GT -> y }
520 (I# x) `compareInt` (I# y) | x <# y = LT
524 instance Enum Int where
527 #ifndef USE_FOLDR_BUILD
528 enumFrom x = x : enumFrom (x `plusInt` 1)
529 enumFromTo n m = takeWhile (<= m) (enumFrom n)
531 {-# INLINE enumFrom #-}
532 {-# INLINE enumFromTo #-}
533 enumFrom x = build (\ c _ ->
534 let g x = x `c` g (x `plusInt` 1) in g x)
535 enumFromTo x y = build (\ c n ->
536 let g x = if x <= y then x `c` g (x `plusInt` 1) else n in g x)
538 enumFromThen m n = en' m (n `minusInt` m)
539 where en' m n = m : en' (m `plusInt` n) n
540 enumFromThenTo n m p = takeWhile (if m >= n then (<= p) else (>= p))
544 instance Bounded Int where
545 minBound = negate 2147483647 -- **********************
546 maxBound = 2147483647 -- **********************
548 instance Num Int where
549 (+) x y = plusInt x y
550 (-) x y = minusInt x y
551 negate x = negateInt x
552 (*) x y = timesInt x y
553 abs n = if n `geInt` 0 then n else (negateInt n)
555 signum n | n `ltInt` 0 = negateInt 1
559 fromInteger (J# a# s# d#)
560 = case (integer2Int# a# s# d#) of { i# -> I# i# }
564 instance Show Int where
565 showsPrec p n = showSignedInt p n
566 showList ls = showList__ (showsPrec 0) ls
570 %*********************************************************
572 \subsection{Type @Integer@, @Float@, @Double@}
574 %*********************************************************
576 Just the type declarations. If we don't actually use any @Integers@ we'd
577 rather not link the @Integer@ module at all; and the default-decl stuff
578 in the renamer tends to slurp in @Double@ regardless.
581 data Float = F# Float#
582 data Double = D# Double#
583 data Integer = J# Int# Int# ByteArray#
587 %*********************************************************
589 \subsection{The function type}
591 %*********************************************************
594 instance Show (a -> b) where
595 showsPrec p f = showString "<<function>>"
596 showList ls = showList__ (showsPrec 0) ls
607 -- function composition
609 {-# GENERATE_SPECS (.) a b c #-}
610 (.) :: (b -> c) -> (a -> b) -> a -> c
611 f . g = \ x -> f (g x)
613 -- flip f takes its (first) two arguments in the reverse order of f.
614 flip :: (a -> b -> c) -> b -> a -> c
617 -- right-associating infix application operator (useful in continuation-
619 ($) :: (a -> b) -> a -> b
622 -- until p f yields the result of applying f until p holds.
623 until :: (a -> Bool) -> (a -> a) -> a -> a
624 until p f x | p x = x
625 | otherwise = until p f (f x)
627 -- asTypeOf is a type-restricted version of const. It is usually used
628 -- as an infix operator, and its typing forces its first argument
629 -- (which is usually overloaded) to have the same type as the second.
630 asTypeOf :: a -> a -> a
635 %*********************************************************
637 \subsection{Miscellaneous}
639 %*********************************************************
643 data Addr = A# Addr# deriving (Eq, Ord) -- Glasgow extension
644 data Word = W# Word# deriving (Eq, Ord) -- Glasgow extension
645 data ForeignObj = ForeignObj ForeignObj# -- another one
648 {-# GENERATE_SPECS data a :: Lift a #-}
654 %*********************************************************
656 \subsection{Support code for @Show@}
658 %*********************************************************
661 shows :: (Show a) => a -> ShowS
664 show :: (Show a) => a -> String
667 showChar :: Char -> ShowS
670 showString :: String -> ShowS
673 showParen :: Bool -> ShowS -> ShowS
674 showParen b p = if b then showChar '(' . p . showChar ')' else p
676 {-# GENERATE_SPECS showList__ a #-}
677 showList__ :: (a -> ShowS) -> [a] -> ShowS
679 showList__ showx [] = showString "[]"
680 showList__ showx (x:xs) = showChar '[' . showx x . showl xs
682 showl [] = showChar ']'
683 showl (x:xs) = showString ", " . showx x . showl xs
686 showSpace = {-showChar ' '-} \ xs -> ' ' : xs
689 Code specific for characters
692 showLitChar :: Char -> ShowS
693 showLitChar c | c > '\DEL' = showChar '\\' . protectEsc isDigit (shows (ord c))
694 showLitChar '\DEL' = showString "\\DEL"
695 showLitChar '\\' = showString "\\\\"
696 showLitChar c | c >= ' ' = showChar c
697 showLitChar '\a' = showString "\\a"
698 showLitChar '\b' = showString "\\b"
699 showLitChar '\f' = showString "\\f"
700 showLitChar '\n' = showString "\\n"
701 showLitChar '\r' = showString "\\r"
702 showLitChar '\t' = showString "\\t"
703 showLitChar '\v' = showString "\\v"
704 showLitChar '\SO' = protectEsc (== 'H') (showString "\\SO")
705 showLitChar c = showString ('\\' : asciiTab!!ord c)
707 protectEsc p f = f . cont
708 where cont s@(c:_) | p c = "\\&" ++ s
712 Code specific for Ints.
715 showSignedInt :: Int -> Int -> ShowS
716 showSignedInt p (I# n) r
717 = -- from HBC version; support code follows
718 if n <# 0# && p > 6 then '(':itos n++(')':r) else itos n ++ r
720 itos :: Int# -> String
723 if negateInt# n <# 0# then
724 -- n is minInt, a difficult number
725 itos (n `quotInt#` 10#) ++ itos' (negateInt# (n `remInt#` 10#)) []
727 '-':itos' (negateInt# n) []
731 itos' :: Int# -> String -> String
734 C# (chr# (n +# ord# '0'#)) : cs
736 itos' (n `quotInt#` 10#) (C# (chr# (n `remInt#` 10# +# ord# '0'#)) : cs)
739 %*********************************************************
741 \subsection{Numeric primops}
743 %*********************************************************
745 Definitions of the boxed PrimOps; these will be
746 used in the case of partial applications, etc.
752 plusInt (I# x) (I# y) = I# (x +# y)
753 minusInt(I# x) (I# y) = I# (x -# y)
754 timesInt(I# x) (I# y) = I# (x *# y)
755 quotInt (I# x) (I# y) = I# (quotInt# x y)
756 remInt (I# x) (I# y) = I# (remInt# x y)
757 negateInt (I# x) = I# (negateInt# x)
758 gtInt (I# x) (I# y) = x ># y
759 geInt (I# x) (I# y) = x >=# y
760 eqInt (I# x) (I# y) = x ==# y
761 neInt (I# x) (I# y) = x /=# y
762 ltInt (I# x) (I# y) = x <# y
763 leInt (I# x) (I# y) = x <=# y