2 % (c) The GRASP/AQUA Project, Glasgow University, 1992-1996
4 \section[PrelBase]{Module @PrelBase@}
11 import IOBase ( error ) {-# SOURCE #-}
18 infix 4 ==, /=, <, <=, >=, >
25 %*********************************************************
27 \subsection{Standard classes @Eq@, @Ord@, @Bounded@, @Eval@}
29 %*********************************************************
33 (==), (/=) :: a -> a -> Bool
37 class (Eq a) => Ord a where
38 compare :: a -> a -> Ordering
39 (<), (<=), (>=), (>):: a -> a -> Bool
40 max, min :: a -> a -> a
42 -- An instance of Ord should define either compare or <=
43 -- Using compare can be more efficient for complex types.
49 x <= y = compare x y /= GT
50 x < y = compare x y == LT
51 x >= y = compare x y /= LT
52 x > y = compare x y == GT
53 max x y = case (compare x y) of { LT -> y ; EQ -> x ; GT -> x }
54 min x y = case (compare x y) of { LT -> x ; EQ -> x ; GT -> y }
57 minBound, maxBound :: a
62 %*********************************************************
64 \subsection{Monadic classes @Functor@, @Monad@, @MonadZero@, @MonadPlus@}
66 %*********************************************************
70 map :: (a -> b) -> f a -> f b
73 (>>=) :: m a -> (a -> m b) -> m b
74 (>>) :: m a -> m b -> m b
77 m >> k = m >>= \_ -> k
79 class (Monad m) => MonadZero m where
82 class (MonadZero m) => MonadPlus m where
83 (++) :: m a -> m a -> m a
87 %*********************************************************
89 \subsection{Classes @Num@ and @Enum@}
91 %*********************************************************
94 class (Ord a) => Enum a where
97 enumFrom :: a -> [a] -- [n..]
98 enumFromThen :: a -> a -> [a] -- [n,n'..]
99 enumFromTo :: a -> a -> [a] -- [n..m]
100 enumFromThenTo :: a -> a -> a -> [a] -- [n,n'..m]
102 enumFromTo n m = takeWhile (<= m) (enumFrom n)
103 enumFromThenTo n n' m
104 = takeWhile (if n' >= n then (<= m) else (>= m))
107 class (Eq a, Show a, Eval a) => Num a where
108 (+), (-), (*) :: a -> a -> a
110 abs, signum :: a -> a
111 fromInteger :: Integer -> a
112 fromInt :: Int -> a -- partain: Glasgow extension
115 fromInt (I# i#) = fromInteger (int2Integer# i#)
116 -- Go via the standard class-op if the
117 -- non-standard one ain't provided
121 succ, pred :: Enum a => a -> a
122 succ = toEnum . (+1) . fromEnum
123 pred = toEnum . (subtract 1) . fromEnum
125 chr = (toEnum :: Int -> Char)
126 ord = (fromEnum :: Char -> Int)
129 ord_0 = fromInt (ord '0')
131 {-# GENERATE_SPECS subtract a{Int#,Double#,Int,Double,Complex(Double#),Complex(Double)} #-}
132 subtract :: (Num a) => a -> a -> a
137 %*********************************************************
139 \subsection{The @Show@ class}
141 %*********************************************************
144 type ShowS = String -> String
147 showsPrec :: Int -> a -> ShowS
148 showList :: [a] -> ShowS
150 showList [] = showString "[]"
152 = showChar '[' . shows x . showl xs
153 where showl [] = showChar ']'
154 showl (x:xs) = showString ", " . shows x . showl xs
157 %*********************************************************
159 \subsection{The list type}
161 %*********************************************************
164 data [] a = [] | a : [a] -- do explicitly: deriving (Eq, Ord)
165 -- to avoid weird names like con2tag_[]#
167 instance (Eq a) => Eq [a] where
169 (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
199 instance MonadZero [] where
202 instance MonadPlus [] where
203 xs ++ ys = foldr (:) ys xs
205 instance (Show a) => Show [a] where
206 showsPrec p = showList
207 showList = showList__ (showsPrec 0)
212 A few list functions that appear here because they are used here.
213 The rest of the prelude list functions are in PrelList.
216 foldr :: (a -> b -> b) -> b -> [a] -> b
218 foldr f z (x:xs) = f x (foldr f z xs)
220 -- takeWhile, applied to a predicate p and a list xs, returns the longest
221 -- prefix (possibly empty) of xs of elements that satisfy p. dropWhile p xs
222 -- returns the remaining suffix. Span p xs is equivalent to
223 -- (takeWhile p xs, dropWhile p xs), while break p uses the negation of p.
225 takeWhile :: (a -> Bool) -> [a] -> [a]
228 | p x = x : takeWhile p xs
231 dropWhile :: (a -> Bool) -> [a] -> [a]
233 dropWhile p xs@(x:xs')
234 | p x = dropWhile p xs'
237 -- List index (subscript) operator, 0-origin
238 (!!) :: [a] -> Int -> a
240 (_:xs) !! n | n > 0 = xs !! (n-1)
241 (_:_) !! _ = error "PreludeList.!!: negative index"
242 [] !! _ = error "PreludeList.!!: index too large"
246 %*********************************************************
248 \subsection{Type @Void@}
250 %*********************************************************
252 The type @Void@ is built in, but it needs a @Show@ instance.
255 instance Show Void where
256 showsPrec p f = showString "<<void>>"
257 showList = showList__ (showsPrec 0)
261 %*********************************************************
263 \subsection{Type @Bool@}
265 %*********************************************************
268 data Bool = False | True deriving (Eq, Ord, Enum, Bounded, Show {- Read -})
272 (&&), (||) :: Bool -> Bool -> Bool
287 %*********************************************************
289 \subsection{Type @Maybe@}
291 %*********************************************************
294 data Maybe a = Nothing | Just a deriving (Eq, Ord, Show {- Read -})
296 instance Functor Maybe where
297 map f Nothing = Nothing
298 map f (Just a) = Just (f a)
300 instance Monad Maybe where
302 Nothing >>= k = Nothing
305 instance MonadZero Maybe where
308 instance MonadPlus Maybe where
314 %*********************************************************
316 \subsection{The @()@ type}
318 %*********************************************************
320 The Unit type is here because virtually any program needs it (whereas
321 some programs may get away without consulting PrelTup). Furthermore,
322 the renamer currently *always* asks for () to be in scope, so that
323 ccalls can use () as their default type; so when compiling PrelBase we
324 need (). (We could arrange suck in () only if -fglasgow-exts, but putting
325 it here seems more direct.
328 data () = () --easier to do explicitly: deriving (Eq, Ord, Enum, Show, Bounded)
329 -- (avoids weird-named functions, e.g., con2tag_()#
335 instance Ord () where
344 instance Enum () where
346 toEnum _ = error "Prelude.Enum.().toEnum: argument not 0"
349 enumFromThen () () = [()]
350 enumFromTo () () = [()]
351 enumFromThenTo () () () = [()]
353 instance Bounded () where
357 instance Show () where
358 showsPrec p () = showString "()"
361 %*********************************************************
363 \subsection{Type @Either@}
365 %*********************************************************
368 data Either a b = Left a | Right b deriving (Eq, Ord, Show {- Read -} )
370 either :: (a -> c) -> (b -> c) -> Either a b -> c
371 either f g (Left x) = f x
372 either f g (Right y) = g y
376 %*********************************************************
378 \subsection{Type @Ordering@}
380 %*********************************************************
383 data Ordering = LT | EQ | GT deriving (Eq, Ord, Enum, Bounded, Show {- Read -})
387 %*********************************************************
389 \subsection{Type @Char@ and @String@}
391 %*********************************************************
396 data Char = C# Char# deriving (Eq, Ord)
398 instance Enum Char where
399 toEnum (I# i) | i >=# 0# && i <=# 255# = C# (chr# i)
400 | otherwise = error "Prelude.Enum.Char.toEnum:out of range"
401 fromEnum (C# c) = I# (ord# c)
403 enumFrom (C# c) = eftt (ord# c) 1# 255#
404 enumFromThen (C# c1) (C# c2) = eftt (ord# c1) (ord# c2 -# ord# c1) 255#
405 enumFromThenTo (C# c1) (C# c2) (C# c3) = eftt (ord# c1) (ord# c2 -# ord# c1) (ord# c3)
407 eftt :: Int# -> Int# -> Int# -> [Char]
411 go now | now ># limit = []
412 | otherwise = C# (chr# now) : go (now +# step)
414 instance Bounded Char where
418 instance Show Char where
419 showsPrec p '\'' = showString "'\\''"
420 showsPrec p c = showChar '\'' . showLitChar c . showChar '\''
422 showList cs = showChar '"' . showl cs
423 where showl "" = showChar '"'
424 showl ('"':cs) = showString "\\\"" . showl cs
425 showl (c:cs) = showLitChar c . showl cs
430 isAscii, isControl, isPrint, isSpace, isUpper,
431 isLower, isAlpha, isDigit, isOctDigit, isHexDigit, isAlphanum :: Char -> Bool
432 isAscii c = fromEnum c < 128
433 isControl c = c < ' ' || c >= '\DEL' && c <= '\x9f'
434 isPrint c = not (isControl c)
436 -- isSpace includes non-breaking space
437 -- Done with explicit equalities both for efficiency, and to avoid a tiresome
438 -- recursion with PrelList elem
439 isSpace c = c == ' ' ||
447 -- The upper case ISO characters have the multiplication sign dumped
448 -- randomly in the middle of the range. Go figure.
449 isUpper c = c >= 'A' && c <= 'Z' ||
450 c >= '\xC0' && c <= '\xD6' ||
451 c >= '\xD8' && c <= '\xDE'
452 -- The lower case ISO characters have the division sign dumped
453 -- randomly in the middle of the range. Go figure.
454 isLower c = c >= 'a' && c <= 'z' ||
455 c >= '\xDF' && c <= '\xF6' ||
456 c >= '\xF8' && c <= '\xFF'
457 isAlpha c = isUpper c || isLower c
458 isDigit c = c >= '0' && c <= '9'
459 isOctDigit c = c >= '0' && c <= '7'
460 isHexDigit c = isDigit c || c >= 'A' && c <= 'F' ||
462 isAlphanum c = isAlpha c || isDigit c
464 -- These almost work for ISO-Latin-1 (except for =DF <-> =FF)
466 toUpper, toLower :: Char -> Char
467 toUpper c | isLower c = toEnum (fromEnum c - fromEnum 'a'
471 toLower c | isUpper c = toEnum (fromEnum c - fromEnum 'A'
475 asciiTab = -- Using an array drags in the array module. listArray ('\NUL', ' ')
476 ["NUL", "SOH", "STX", "ETX", "EOT", "ENQ", "ACK", "BEL",
477 "BS", "HT", "LF", "VT", "FF", "CR", "SO", "SI",
478 "DLE", "DC1", "DC2", "DC3", "DC4", "NAK", "SYN", "ETB",
479 "CAN", "EM", "SUB", "ESC", "FS", "GS", "RS", "US",
483 %*********************************************************
485 \subsection{Type @Int@}
487 %*********************************************************
490 data Int = I# Int# deriving (Eq,Ord)
492 instance Enum Int where
495 #ifndef USE_FOLDR_BUILD
496 enumFrom x = x : enumFrom (x `plusInt` 1)
497 enumFromTo n m = takeWhile (<= m) (enumFrom n)
499 {-# INLINE enumFrom #-}
500 {-# INLINE enumFromTo #-}
501 enumFrom x = build (\ c _ ->
502 let g x = x `c` g (x `plusInt` 1) in g x)
503 enumFromTo x y = build (\ c n ->
504 let g x = if x <= y then x `c` g (x `plusInt` 1) else n in g x)
506 enumFromThen m n = en' m (n `minusInt` m)
507 where en' m n = m : en' (m `plusInt` n) n
508 enumFromThenTo n m p = takeWhile (if m >= n then (<= p) else (>= p))
511 instance Bounded Int where
512 minBound = negate 2147483647 -- **********************
513 maxBound = 2147483647 -- **********************
515 instance Num Int where
516 (+) x y = plusInt x y
517 (-) x y = minusInt x y
518 negate x = negateInt x
519 (*) x y = timesInt x y
520 abs n = if n `geInt` 0 then n else (negateInt n)
522 signum n | n `ltInt` 0 = negateInt 1
526 fromInteger (J# a# s# d#)
527 = case (integer2Int# a# s# d#) of { i# -> I# i# }
531 instance Show Int where
532 showsPrec p n = showSignedInt p n
533 showList = showList__ (showsPrec 0)
537 %*********************************************************
539 \subsection{Type @Integer@, @Float@, @Double@}
541 %*********************************************************
543 Just the type declarations. If we don't actually use any @Integers@ we'd
544 rather not link the @Integer@ module at all; and the default-decl stuff
545 in the renamer tends to slurp in @Double@ regardless.
548 data Float = F# Float# deriving (Eq, Ord)
549 data Double = D# Double# deriving (Eq, Ord)
550 data Integer = J# Int# Int# ByteArray#
554 %*********************************************************
556 \subsection{The function type}
558 %*********************************************************
561 instance Show (a -> b) where
562 showsPrec p f = showString "<<function>>"
563 showList = showList__ (showsPrec 0)
573 -- function composition
575 {-# GENERATE_SPECS (.) a b c #-}
576 (.) :: (b -> c) -> (a -> b) -> a -> c
577 f . g = \ x -> f (g x)
579 -- flip f takes its (first) two arguments in the reverse order of f.
580 flip :: (a -> b -> c) -> b -> a -> c
583 -- right-associating infix application operator (useful in continuation-
585 ($) :: (a -> b) -> a -> b
588 -- until p f yields the result of applying f until p holds.
589 until :: (a -> Bool) -> (a -> a) -> a -> a
590 until p f x | p x = x
591 | otherwise = until p f (f x)
593 -- asTypeOf is a type-restricted version of const. It is usually used
594 -- as an infix operator, and its typing forces its first argument
595 -- (which is usually overloaded) to have the same type as the second.
596 asTypeOf :: a -> a -> a
601 %*********************************************************
603 \subsection{Miscellaneous}
605 %*********************************************************
609 data Addr = A# Addr# deriving (Eq, Ord) -- Glasgow extension
610 data Word = W# Word# deriving (Eq, Ord) -- Glasgow extension
613 {-# GENERATE_SPECS data a :: Lift a #-}
619 %*********************************************************
621 \subsection{Support code for @Show@}
623 %*********************************************************
626 shows :: (Show a) => a -> ShowS
629 show :: (Show a) => a -> String
632 showChar :: Char -> ShowS
635 showString :: String -> ShowS
638 showParen :: Bool -> ShowS -> ShowS
639 showParen b p = if b then showChar '(' . p . showChar ')' else p
641 {-# GENERATE_SPECS showList__ a #-}
642 showList__ :: (a -> ShowS) -> [a] -> ShowS
644 showList__ showx [] = showString "[]"
645 showList__ showx (x:xs) = showChar '[' . showx x . showl xs
647 showl [] = showChar ']'
648 showl (x:xs) = showString ", " . showx x . showl xs
651 showSpace = {-showChar ' '-} \ xs -> ' ' : xs
654 Code specific for characters
657 showLitChar :: Char -> ShowS
658 showLitChar c | c > '\DEL' = showChar '\\' . protectEsc isDigit (shows (ord c))
659 showLitChar '\DEL' = showString "\\DEL"
660 showLitChar '\\' = showString "\\\\"
661 showLitChar c | c >= ' ' = showChar c
662 showLitChar '\a' = showString "\\a"
663 showLitChar '\b' = showString "\\b"
664 showLitChar '\f' = showString "\\f"
665 showLitChar '\n' = showString "\\n"
666 showLitChar '\r' = showString "\\r"
667 showLitChar '\t' = showString "\\t"
668 showLitChar '\v' = showString "\\v"
669 showLitChar '\SO' = protectEsc (== 'H') (showString "\\SO")
670 showLitChar c = showString ('\\' : asciiTab!!ord c)
672 protectEsc p f = f . cont
673 where cont s@(c:_) | p c = "\\&" ++ s
677 Code specific for Ints.
680 showSignedInt :: Int -> Int -> ShowS
681 showSignedInt p (I# n) r
682 = -- from HBC version; support code follows
683 if n <# 0# && p > 6 then '(':itos n++(')':r) else itos n ++ r
685 itos :: Int# -> String
688 if negateInt# n <# 0# then
689 -- n is minInt, a difficult number
690 itos (n `quotInt#` 10#) ++ itos' (negateInt# (n `remInt#` 10#)) []
692 '-':itos' (negateInt# n) []
696 itos' :: Int# -> String -> String
699 C# (chr# (n +# ord# '0'#)) : cs
701 itos' (n `quotInt#` 10#) (C# (chr# (n `remInt#` 10# +# ord# '0'#)) : cs)
704 %*********************************************************
706 \subsection{Numeric primops}
708 %*********************************************************
710 Definitions of the boxed PrimOps; these will be
711 used in the case of partial applications, etc.
714 plusInt (I# x) (I# y) = I# (x +# y)
715 minusInt(I# x) (I# y) = I# (x -# y)
716 timesInt(I# x) (I# y) = I# (x *# y)
717 quotInt (I# x) (I# y) = I# (quotInt# x y)
718 remInt (I# x) (I# y) = I# (remInt# x y)
719 negateInt (I# x) = I# (negateInt# x)
720 gtInt (I# x) (I# y) = x ># y
721 geInt (I# x) (I# y) = x >=# y
722 eqInt (I# x) (I# y) = x ==# y
723 neInt (I# x) (I# y) = x /=# y
724 ltInt (I# x) (I# y) = x <# y
725 leInt (I# x) (I# y) = x <=# y