1 % -----------------------------------------------------------------------------
2 % $Id: PrelBase.lhs,v 1.34 2000/08/02 14:13:27 rrt Exp $
4 % (c) The University of Glasgow, 1992-2000
6 \section[PrelBase]{Module @PrelBase@}
9 The overall structure of the GHC Prelude is a bit tricky.
11 a) We want to avoid "orphan modules", i.e. ones with instance
12 decls that don't belong either to a tycon or a class
13 defined in the same module
15 b) We want to avoid giant modules
17 So the rough structure is as follows, in (linearised) dependency order
20 PrelGHC Has no implementation. It defines built-in things, and
21 by importing it you bring them into scope.
22 The source file is PrelGHC.hi-boot, which is just
23 copied to make PrelGHC.hi
25 Classes: CCallable, CReturnable
27 PrelBase Classes: Eq, Ord, Functor, Monad
28 Types: list, (), Int, Bool, Ordering, Char, String
30 PrelTup Types: tuples, plus instances for PrelBase classes
32 PrelShow Class: Show, plus instances for PrelBase/PrelTup types
34 PrelEnum Class: Enum, plus instances for PrelBase/PrelTup types
36 PrelMaybe Type: Maybe, plus instances for PrelBase classes
38 PrelNum Class: Num, plus instances for Int
39 Type: Integer, plus instances for all classes so far (Eq, Ord, Num, Show)
41 Integer is needed here because it is mentioned in the signature
42 of 'fromInteger' in class Num
44 PrelReal Classes: Real, Integral, Fractional, RealFrac
45 plus instances for Int, Integer
46 Types: Ratio, Rational
47 plus intances for classes so far
49 Rational is needed here because it is mentioned in the signature
50 of 'toRational' in class Real
52 Ix Classes: Ix, plus instances for Int, Bool, Char, Integer, Ordering, tuples
54 PrelArr Types: Array, MutableArray, MutableVar
56 Does *not* contain any ByteArray stuff (see PrelByteArr)
57 Arrays are used by a function in PrelFloat
59 PrelFloat Classes: Floating, RealFloat
60 Types: Float, Double, plus instances of all classes so far
62 This module contains everything to do with floating point.
63 It is a big module (900 lines)
64 With a bit of luck, many modules can be compiled without ever reading PrelFloat.hi
66 PrelByteArr Types: ByteArray, MutableByteArray
68 We want this one to be after PrelFloat, because it defines arrays
72 Other Prelude modules are much easier with fewer complex dependencies.
76 {-# OPTIONS -fno-implicit-prelude #-}
81 module PrelGHC, -- Re-export PrelGHC, PrelErr & PrelNum, to avoid lots
82 module PrelErr, -- of people having to import it explicitly
88 import {-# SOURCE #-} PrelErr
89 import {-# SOURCE #-} PrelNum
93 infix 4 ==, /=, <, <=, >=, >
99 default () -- Double isn't available yet
103 %*********************************************************
105 \subsection{DEBUGGING STUFF}
106 %* (for use when compiling PrelBase itself doesn't work)
108 %*********************************************************
112 data Bool = False | True
113 data Ordering = LT | EQ | GT
121 (&&) True True = True
127 unpackCString# :: Addr# -> [Char]
128 unpackFoldrCString# :: Addr# -> (Char -> a -> a) -> a -> a
129 unpackAppendCString# :: Addr# -> [Char] -> [Char]
130 unpackNBytes# :: Addr# -> Int# -> [Char]
131 unpackNBytes# a b = error "urk"
132 unpackCString# a = error "urk"
133 unpackFoldrCString# a = error "urk"
134 unpackAppendCString# a = error "urk"
139 %*********************************************************
141 \subsection{Standard classes @Eq@, @Ord@}
143 %*********************************************************
147 (==), (/=) :: a -> a -> Bool
149 -- x /= y = not (x == y)
150 -- x == y = not (x /= y)
152 (/=) x y = not ((==) x y)
155 class (Eq a) => Ord a where
156 compare :: a -> a -> Ordering
157 (<), (<=), (>=), (>):: a -> a -> Bool
158 max, min :: a -> a -> a
160 -- An instance of Ord should define either compare or <=
161 -- Using compare can be more efficient for complex types.
164 | x <= y = LT -- NB: must be '<=' not '<' to validate the
165 -- above claim about the minimal things that can
166 -- be defined for an instance of Ord
169 x <= y = case compare x y of { GT -> False; _other -> True }
170 x < y = case compare x y of { LT -> True; _other -> False }
171 x >= y = case compare x y of { LT -> False; _other -> True }
172 x > y = case compare x y of { GT -> True; _other -> False }
174 -- These two default methods use '>' rather than compare
175 -- because the latter is often more expensive
176 max x y = if x > y then x else y
177 min x y = if x > y then y else x
180 %*********************************************************
182 \subsection{Monadic classes @Functor@, @Monad@ }
184 %*********************************************************
187 class Functor f where
188 fmap :: (a -> b) -> f a -> f b
191 (>>=) :: m a -> (a -> m b) -> m b
192 (>>) :: m a -> m b -> m b
194 fail :: String -> m a
196 m >> k = m >>= \_ -> k
202 %*********************************************************
204 \subsection{The list type}
206 %*********************************************************
209 data [] a = [] | a : [a] -- do explicitly: deriving (Eq, Ord)
210 -- to avoid weird names like con2tag_[]#
213 instance (Eq a) => Eq [a] where
215 {-# SPECIALISE instance Eq [Char] #-}
218 (x:xs) == (y:ys) = x == y && xs == ys
221 xs /= ys = if (xs == ys) then False else True
223 instance (Ord a) => Ord [a] where
225 {-# SPECIALISE instance Ord [Char] #-}
227 a < b = case compare a b of { LT -> True; EQ -> False; GT -> False }
228 a <= b = case compare a b of { LT -> True; EQ -> True; GT -> False }
229 a >= b = case compare a b of { LT -> False; EQ -> True; GT -> True }
230 a > b = case compare a b of { LT -> False; EQ -> False; GT -> True }
233 compare (_:_) [] = GT
234 compare [] (_:_) = LT
235 compare (x:xs) (y:ys) = case compare x y of
240 instance Functor [] where
243 instance Monad [] where
244 m >>= k = foldr ((++) . k) [] m
245 m >> k = foldr ((++) . (\ _ -> k)) [] m
250 A few list functions that appear here because they are used here.
251 The rest of the prelude list functions are in PrelList.
253 ----------------------------------------------
254 -- foldr/build/augment
255 ----------------------------------------------
258 foldr :: (a -> b -> b) -> b -> [a] -> b
260 -- foldr f z (x:xs) = f x (foldr f z xs)
265 go (x:xs) = x `k` go xs
267 build :: forall a. (forall b. (a -> b -> b) -> b -> b) -> [a]
268 {-# INLINE 2 build #-}
269 -- The INLINE is important, even though build is tiny,
270 -- because it prevents [] getting inlined in the version that
271 -- appears in the interface file. If [] *is* inlined, it
272 -- won't match with [] appearing in rules in an importing module.
274 -- The "2" says to inline in phase 2
278 augment :: forall a. (forall b. (a->b->b) -> b -> b) -> [a] -> [a]
279 {-# INLINE 2 augment #-}
280 augment g xs = g (:) xs
283 "fold/build" forall k z (g::forall b. (a->b->b) -> b -> b) .
284 foldr k z (build g) = g k z
286 "foldr/augment" forall k z xs (g::forall b. (a->b->b) -> b -> b) .
287 foldr k z (augment g xs) = g k (foldr k z xs)
289 "foldr/id" foldr (:) [] = \x->x
290 "foldr/app" forall xs ys. foldr (:) ys xs = append xs ys
292 "foldr/cons" forall k z x xs. foldr k z (x:xs) = k x (foldr k z xs)
293 "foldr/nil" forall k z. foldr k z [] = z
295 "augment/build" forall (g::forall b. (a->b->b) -> b -> b)
296 (h::forall b. (a->b->b) -> b -> b) .
297 augment g (build h) = build (\c n -> g c (h c n))
298 "augment/nil" forall (g::forall b. (a->b->b) -> b -> b) .
299 augment g [] = build g
302 -- This rule is true, but not (I think) useful:
303 -- augment g (augment h t) = augment (\cn -> g c (h c n)) t
307 ----------------------------------------------
309 ----------------------------------------------
312 map :: (a -> b) -> [a] -> [b]
316 mapFB c f x ys = c (f x) ys
318 mapList :: (a -> b) -> [a] -> [b]
320 mapList f (x:xs) = f x : mapList f xs
323 "map" forall f xs. map f xs = build (\c n -> foldr (mapFB c f) n xs)
324 "mapFB" forall c f g. mapFB (mapFB c f) g = mapFB c (f.g)
325 "mapList" forall f. foldr (mapFB (:) f) [] = mapList f
330 ----------------------------------------------
332 ----------------------------------------------
334 (++) :: [a] -> [a] -> [a]
338 "++" forall xs ys. (++) xs ys = augment (\c n -> foldr c n xs) ys
341 append :: [a] -> [a] -> [a]
343 append (x:xs) ys = x : append xs ys
347 %*********************************************************
349 \subsection{Type @Bool@}
351 %*********************************************************
354 data Bool = False | True deriving (Eq, Ord)
355 -- Read in PrelRead, Show in PrelShow
359 (&&), (||) :: Bool -> Bool -> Bool
374 %*********************************************************
376 \subsection{The @()@ type}
378 %*********************************************************
380 The Unit type is here because virtually any program needs it (whereas
381 some programs may get away without consulting PrelTup). Furthermore,
382 the renamer currently *always* asks for () to be in scope, so that
383 ccalls can use () as their default type; so when compiling PrelBase we
384 need (). (We could arrange suck in () only if -fglasgow-exts, but putting
385 it here seems more direct.)
394 instance Ord () where
405 %*********************************************************
407 \subsection{Type @Ordering@}
409 %*********************************************************
412 data Ordering = LT | EQ | GT deriving (Eq, Ord)
413 -- Read in PrelRead, Show in PrelShow
417 %*********************************************************
419 \subsection{Type @Char@ and @String@}
421 %*********************************************************
428 -- We don't use deriving for Eq and Ord, because for Ord the derived
429 -- instance defines only compare, which takes two primops. Then
430 -- '>' uses compare, and therefore takes two primops instead of one.
432 instance Eq Char where
433 (C# c1) == (C# c2) = c1 `eqChar#` c2
434 (C# c1) /= (C# c2) = c1 `neChar#` c2
436 instance Ord Char where
437 (C# c1) > (C# c2) = c1 `gtChar#` c2
438 (C# c1) >= (C# c2) = c1 `geChar#` c2
439 (C# c1) <= (C# c2) = c1 `leChar#` c2
440 (C# c1) < (C# c2) = c1 `ltChar#` c2
443 chr (I# i) | i >=# 0# && i <=# 255# = C# (chr# i)
444 | otherwise = error ("Prelude.chr: bad argument")
446 unsafeChr :: Int -> Char
447 unsafeChr (I# i) = C# (chr# i)
450 ord (C# c) = I# (ord# c)
454 %*********************************************************
456 \subsection{Type @Int@}
458 %*********************************************************
463 zeroInt, oneInt, twoInt, maxInt, minInt :: Int
467 minInt = I# (-2147483648#) -- GHC <= 2.09 had this at -2147483647
468 maxInt = I# 2147483647#
470 instance Eq Int where
471 (==) x y = x `eqInt` y
472 (/=) x y = x `neInt` y
474 instance Ord Int where
475 compare x y = compareInt x y
482 compareInt :: Int -> Int -> Ordering
483 (I# x) `compareInt` (I# y) | x <# y = LT
489 %*********************************************************
491 \subsection{The function type}
493 %*********************************************************
504 -- function composition
506 (.) :: (b -> c) -> (a -> b) -> a -> c
509 -- flip f takes its (first) two arguments in the reverse order of f.
510 flip :: (a -> b -> c) -> b -> a -> c
513 -- right-associating infix application operator (useful in continuation-
515 ($) :: (a -> b) -> a -> b
518 -- until p f yields the result of applying f until p holds.
519 until :: (a -> Bool) -> (a -> a) -> a -> a
520 until p f x | p x = x
521 | otherwise = until p f (f x)
523 -- asTypeOf is a type-restricted version of const. It is usually used
524 -- as an infix operator, and its typing forces its first argument
525 -- (which is usually overloaded) to have the same type as the second.
526 asTypeOf :: a -> a -> a
530 %*********************************************************
532 \subsection{CCallable instances}
534 %*********************************************************
536 Defined here to avoid orphans
539 instance CCallable Char
540 instance CReturnable Char
542 instance CCallable Int
543 instance CReturnable Int
545 instance CReturnable () -- Why, exactly?
549 %*********************************************************
551 \subsection{Numeric primops}
553 %*********************************************************
555 Definitions of the boxed PrimOps; these will be
556 used in the case of partial applications, etc.
565 {-# INLINE plusInt #-}
566 {-# INLINE minusInt #-}
567 {-# INLINE timesInt #-}
568 {-# INLINE quotInt #-}
569 {-# INLINE remInt #-}
570 {-# INLINE negateInt #-}
572 plusInt, minusInt, timesInt, quotInt, remInt, gcdInt :: Int -> Int -> Int
573 plusInt (I# x) (I# y) = I# (x +# y)
574 minusInt(I# x) (I# y) = I# (x -# y)
575 timesInt(I# x) (I# y) = I# (x *# y)
576 quotInt (I# x) (I# y) = I# (quotInt# x y)
577 remInt (I# x) (I# y) = I# (remInt# x y)
579 gcdInt (I# a) (I# b) = g a b
580 where g 0# 0# = error "PrelBase.gcdInt: gcd 0 0 is undefined"
583 g _ _ = I# (gcdInt# absA absB)
585 absInt x = if x <# 0# then negateInt# x else x
590 negateInt :: Int -> Int
591 negateInt (I# x) = I# (negateInt# x)
593 divInt, modInt :: Int -> Int -> Int
595 | x > zeroInt && y < zeroInt = quotInt ((x `minusInt` y) `minusInt` oneInt) y
596 | x < zeroInt && y > zeroInt = quotInt ((x `minusInt` y) `plusInt` oneInt) y
597 | otherwise = quotInt x y
600 | x > zeroInt && y < zeroInt ||
601 x < zeroInt && y > zeroInt = if r/=zeroInt then r `plusInt` y else zeroInt
606 gtInt, geInt, eqInt, neInt, ltInt, leInt :: Int -> Int -> Bool
607 gtInt (I# x) (I# y) = x ># y
608 geInt (I# x) (I# y) = x >=# y
609 eqInt (I# x) (I# y) = x ==# y
610 neInt (I# x) (I# y) = x /=# y
611 ltInt (I# x) (I# y) = x <# y
612 leInt (I# x) (I# y) = x <=# y
616 %********************************************************
618 \subsection{Unpacking C strings}
620 %********************************************************
622 This code is needed for virtually all programs, since it's used for
623 unpacking the strings of error messages.
626 unpackCString# :: Addr# -> [Char]
627 unpackCString# a = unpackCStringList# a
629 unpackCStringList# :: Addr# -> [Char]
630 unpackCStringList# addr
634 | ch `eqChar#` '\0'# = []
635 | otherwise = C# ch : unpack (nh +# 1#)
637 ch = indexCharOffAddr# addr nh
639 unpackAppendCString# :: Addr# -> [Char] -> [Char]
640 unpackAppendCString# addr rest
644 | ch `eqChar#` '\0'# = rest
645 | otherwise = C# ch : unpack (nh +# 1#)
647 ch = indexCharOffAddr# addr nh
649 unpackFoldrCString# :: Addr# -> (Char -> a -> a) -> a -> a
650 unpackFoldrCString# addr f z
654 | ch `eqChar#` '\0'# = z
655 | otherwise = C# ch `f` unpack (nh +# 1#)
657 ch = indexCharOffAddr# addr nh
659 unpackNBytes# :: Addr# -> Int# -> [Char]
660 -- This one is called by the compiler to unpack literal
661 -- strings with NULs in them; rare. It's strict!
662 -- We don't try to do list deforestation for this one
664 unpackNBytes# _addr 0# = []
665 unpackNBytes# addr len# = unpack [] (len# -# 1#)
670 case indexCharOffAddr# addr i# of
671 ch -> unpack (C# ch : acc) (i# -# 1#)
674 "unpack" forall a . unpackCString# a = build (unpackFoldrCString# a)
675 "unpack-list" forall a . unpackFoldrCString# a (:) [] = unpackCStringList# a
676 "unpack-append" forall a n . unpackFoldrCString# a (:) n = unpackAppendCString# a n
678 -- There's a built-in rule (in PrelRules.lhs) for
679 -- unpackFoldr "foo" c (unpackFoldr "baz" c n) = unpackFoldr "foobaz" c n