2 % (c) The GRAP/AQUA Project, Glasgow University, 1992-1996
4 \section[PrelBase]{Module @PrelBase@}
7 The overall structure of the GHC Prelude is a bit tricky.
9 a) We want to avoid "orphan modules", i.e. ones with instance
10 decls that don't belong either to a tycon or a class
11 defined in the same module
13 b) We want to avoid giant modules
15 So the rough structure is as follows, in (linearised) dependency order
18 PrelGHC Has no implementation. It defines built-in things, and
19 by importing it you bring them into scope.
20 The source file is PrelGHC.hi-boot, which is just
21 copied to make PrelGHC.hi
23 Classes: CCallable, CReturnable
25 PrelBase Classes: Eq, Ord, Functor, Monad
26 Types: list, (), Int, Bool, Ordering, Char, String
28 PrelTup Types: tuples, plus instances for PrelBase classes
30 PrelShow Class: Show, plus instances for PrelBase/PrelTup types
32 PrelEnum Class: Enum, plus instances for PrelBase/PrelTup types
34 PrelMaybe Type: Maybe, plus instances for PrelBase classes
36 PrelNum Class: Num, plus instances for Int
37 Type: Integer, plus instances for all classes so far (Eq, Ord, Num, Show)
39 Integer is needed here because it is mentioned in the signature
40 of 'fromInteger' in class Num
42 PrelReal Classes: Real, Integral, Fractional, RealFrac
43 plus instances for Int, Integer
44 Types: Ratio, Rational
45 plus intances for classes so far
47 Rational is needed here because it is mentioned in the signature
48 of 'toRational' in class Real
50 Ix Classes: Ix, plus instances for Int, Bool, Char, Integer, Ordering, tuples
52 PrelArr Types: Array, MutableArray, MutableVar
54 Does *not* contain any ByteArray stuff (see PrelByteArr)
55 Arrays are used by a function in PrelFloat
57 PrelFloat Classes: Floating, RealFloat
58 Types: Float, Double, plus instances of all classes so far
60 This module contains everything to do with floating point.
61 It is a big module (900 lines)
62 With a bit of luck, many modules can be compiled without ever reading PrelFloat.hi
64 PrelByteArr Types: ByteArray, MutableByteArray
66 We want this one to be after PrelFloat, because it defines arrays
70 Other Prelude modules are much easier with fewer complex dependencies.
74 {-# OPTIONS -fno-implicit-prelude #-}
79 module PrelGHC -- Re-export PrelGHC, to avoid lots of people
80 -- having to import it explicitly
84 import {-# SOURCE #-} PrelErr ( error )
89 infix 4 ==, /=, <, <=, >=, >
95 default () -- Double isn't available yet
99 %*********************************************************
101 \subsection{Standard classes @Eq@, @Ord@}
103 %*********************************************************
107 (==), (/=) :: a -> a -> Bool
109 x /= y = not (x == y)
110 x == y = not (x /= y)
112 class (Eq a) => Ord a where
113 compare :: a -> a -> Ordering
114 (<), (<=), (>=), (>):: a -> a -> Bool
115 max, min :: a -> a -> a
117 -- An instance of Ord should define either compare or <=
118 -- Using compare can be more efficient for complex types.
121 | x <= y = LT -- NB: must be '<=' not '<' to validate the
122 -- above claim about the minimal things that can
123 -- be defined for an instance of Ord
126 x <= y = case compare x y of { GT -> False; _other -> True }
127 x < y = case compare x y of { LT -> True; _other -> False }
128 x >= y = case compare x y of { LT -> False; _other -> True }
129 x > y = case compare x y of { GT -> True; _other -> False }
131 -- These two default methods use '>' rather than compare
132 -- because the latter is often more expensive
133 max x y = if x > y then x else y
134 min x y = if x > y then y else x
137 %*********************************************************
139 \subsection{Monadic classes @Functor@, @Monad@ }
141 %*********************************************************
144 class Functor f where
145 fmap :: (a -> b) -> f a -> f b
148 (>>=) :: m a -> (a -> m b) -> m b
149 (>>) :: m a -> m b -> m b
151 fail :: String -> m a
153 m >> k = m >>= \_ -> k
159 %*********************************************************
161 \subsection{The list type}
163 %*********************************************************
166 data [] a = [] | a : [a] -- do explicitly: deriving (Eq, Ord)
167 -- to avoid weird names like con2tag_[]#
169 instance (Eq a) => Eq [a] where
170 {-# SPECIALISE instance Eq [Char] #-}
172 (x:xs) == (y:ys) = x == y && xs == ys
175 xs /= ys = if (xs == ys) then False else True
177 instance (Ord a) => Ord [a] where
178 {-# SPECIALISE instance Ord [Char] #-}
179 a < b = case compare a b of { LT -> True; EQ -> False; GT -> False }
180 a <= b = case compare a b of { LT -> True; EQ -> True; GT -> False }
181 a >= b = case compare a b of { LT -> False; EQ -> True; GT -> True }
182 a > b = case compare a b of { LT -> False; EQ -> False; GT -> True }
185 compare (_:_) [] = GT
186 compare [] (_:_) = LT
187 compare (x:xs) (y:ys) = case compare x y of
192 instance Functor [] where
195 instance Monad [] where
196 m >>= k = foldr ((++) . k) [] m
197 m >> k = foldr ((++) . (\ _ -> k)) [] m
202 A few list functions that appear here because they are used here.
203 The rest of the prelude list functions are in PrelList.
205 ----------------------------------------------
206 -- foldr/build/augment
207 ----------------------------------------------
210 foldr :: (a -> b -> b) -> b -> [a] -> b
212 -- foldr f z (x:xs) = f x (foldr f z xs)
217 go (x:xs) = x `k` go xs
219 build :: forall a. (forall b. (a -> b -> b) -> b -> b) -> [a]
220 {-# INLINE 2 build #-}
221 -- The INLINE is important, even though build is tiny,
222 -- because it prevents [] getting inlined in the version that
223 -- appears in the interface file. If [] *is* inlined, it
224 -- won't match with [] appearing in rules in an importing module.
226 -- The "2" says to inline in phase 2
230 augment :: forall a. (forall b. (a->b->b) -> b -> b) -> [a] -> [a]
231 {-# INLINE 2 augment #-}
232 augment g xs = g (:) xs
235 "fold/build" forall k z (g::forall b. (a->b->b) -> b -> b) .
236 foldr k z (build g) = g k z
238 "foldr/augment" forall k z xs (g::forall b. (a->b->b) -> b -> b) .
239 foldr k z (augment g xs) = g k (foldr k z xs)
241 "foldr/id" foldr (:) [] = \x->x
242 "foldr/app" forall xs ys. foldr (:) ys xs = append xs ys
244 "foldr/cons" forall k z x xs. foldr k z (x:xs) = k x (foldr k z xs)
245 "foldr/nil" forall k z. foldr k z [] = z
247 "augment/build" forall (g::forall b. (a->b->b) -> b -> b)
248 (h::forall b. (a->b->b) -> b -> b) .
249 augment g (build h) = build (\c n -> g c (h c n))
250 "augment/nil" forall (g::forall b. (a->b->b) -> b -> b) .
251 augment g [] = build g
254 -- This rule is true, but not (I think) useful:
255 -- augment g (augment h t) = augment (\cn -> g c (h c n)) t
259 ----------------------------------------------
261 ----------------------------------------------
264 map :: (a -> b) -> [a] -> [b]
266 map f xs = build (\c n -> foldr (mapFB c f) n xs)
269 mapFB c f x ys = c (f x) ys
271 mapList :: (a -> b) -> [a] -> [b]
273 mapList f (x:xs) = f x : mapList f xs
276 "mapFB" forall c f g. mapFB (mapFB c f) g = mapFB c (f.g)
277 "mapList" forall f. foldr (mapFB (:) f) [] = mapList f
282 ----------------------------------------------
284 ----------------------------------------------
286 (++) :: [a] -> [a] -> [a]
288 xs ++ ys = augment (\c n -> foldr c n xs) ys
290 append :: [a] -> [a] -> [a]
292 append (x:xs) ys = x : append xs ys
296 %*********************************************************
298 \subsection{Type @Bool@}
300 %*********************************************************
303 data Bool = False | True deriving (Eq, Ord)
304 -- Read in PrelRead, Show in PrelShow
308 (&&), (||) :: Bool -> Bool -> Bool
323 %*********************************************************
325 \subsection{The @()@ type}
327 %*********************************************************
329 The Unit type is here because virtually any program needs it (whereas
330 some programs may get away without consulting PrelTup). Furthermore,
331 the renamer currently *always* asks for () to be in scope, so that
332 ccalls can use () as their default type; so when compiling PrelBase we
333 need (). (We could arrange suck in () only if -fglasgow-exts, but putting
334 it here seems more direct.)
343 instance Ord () where
354 %*********************************************************
356 \subsection{Type @Ordering@}
358 %*********************************************************
361 data Ordering = LT | EQ | GT deriving (Eq, Ord)
362 -- Read in PrelRead, Show in PrelShow
366 %*********************************************************
368 \subsection{Type @Char@ and @String@}
370 %*********************************************************
377 -- We don't use deriving for Eq and Ord, because for Ord the derived
378 -- instance defines only compare, which takes two primops. Then
379 -- '>' uses compare, and therefore takes two primops instead of one.
381 instance Eq Char where
382 (C# c1) == (C# c2) = c1 `eqChar#` c2
383 (C# c1) /= (C# c2) = c1 `neChar#` c2
385 instance Ord Char where
386 (C# c1) > (C# c2) = c1 `gtChar#` c2
387 (C# c1) >= (C# c2) = c1 `geChar#` c2
388 (C# c1) <= (C# c2) = c1 `leChar#` c2
389 (C# c1) < (C# c2) = c1 `ltChar#` c2
392 chr (I# i) | i >=# 0# && i <=# 255# = C# (chr# i)
393 | otherwise = error ("Prelude.chr: bad argument")
395 unsafeChr :: Int -> Char
396 unsafeChr (I# i) = C# (chr# i)
399 ord (C# c) = I# (ord# c)
403 %*********************************************************
405 \subsection{Type @Int@}
407 %*********************************************************
412 zeroInt, oneInt, twoInt, maxInt, minInt :: Int
416 minInt = I# (-2147483648#) -- GHC <= 2.09 had this at -2147483647
417 maxInt = I# 2147483647#
419 instance Eq Int where
420 (==) x y = x `eqInt` y
421 (/=) x y = x `neInt` y
423 instance Ord Int where
424 compare x y = compareInt x y
431 compareInt :: Int -> Int -> Ordering
432 (I# x) `compareInt` (I# y) | x <# y = LT
438 %*********************************************************
440 \subsection{The function type}
442 %*********************************************************
453 -- function composition
455 (.) :: (b -> c) -> (a -> b) -> a -> c
458 -- flip f takes its (first) two arguments in the reverse order of f.
459 flip :: (a -> b -> c) -> b -> a -> c
462 -- right-associating infix application operator (useful in continuation-
464 ($) :: (a -> b) -> a -> b
467 -- until p f yields the result of applying f until p holds.
468 until :: (a -> Bool) -> (a -> a) -> a -> a
469 until p f x | p x = x
470 | otherwise = until p f (f x)
472 -- asTypeOf is a type-restricted version of const. It is usually used
473 -- as an infix operator, and its typing forces its first argument
474 -- (which is usually overloaded) to have the same type as the second.
475 asTypeOf :: a -> a -> a
479 %*********************************************************
481 \subsection{CCallable instances}
483 %*********************************************************
485 Defined here to avoid orphans
488 instance CCallable Char
489 instance CReturnable Char
491 instance CCallable Int
492 instance CReturnable Int
494 -- DsCCall knows how to pass strings...
495 instance CCallable [Char]
497 instance CReturnable () -- Why, exactly?
501 %*********************************************************
503 \subsection{Numeric primops}
505 %*********************************************************
507 Definitions of the boxed PrimOps; these will be
508 used in the case of partial applications, etc.
517 {-# INLINE plusInt #-}
518 {-# INLINE minusInt #-}
519 {-# INLINE timesInt #-}
520 {-# INLINE quotInt #-}
521 {-# INLINE remInt #-}
522 {-# INLINE negateInt #-}
524 plusInt, minusInt, timesInt, quotInt, remInt, gcdInt :: Int -> Int -> Int
525 plusInt (I# x) (I# y) = I# (x +# y)
526 minusInt(I# x) (I# y) = I# (x -# y)
527 timesInt(I# x) (I# y) = I# (x *# y)
528 quotInt (I# x) (I# y) = I# (quotInt# x y)
529 remInt (I# x) (I# y) = I# (remInt# x y)
530 gcdInt (I# a) (I# b) = I# (gcdInt# a b)
532 negateInt :: Int -> Int
533 negateInt (I# x) = I# (negateInt# x)
535 divInt, modInt :: Int -> Int -> Int
537 | x > zeroInt && y < zeroInt = quotInt ((x `minusInt` y) `minusInt` oneInt) y
538 | x < zeroInt && y > zeroInt = quotInt ((x `minusInt` y) `plusInt` oneInt) y
539 | otherwise = quotInt x y
542 | x > zeroInt && y < zeroInt ||
543 x < zeroInt && y > zeroInt = if r/=zeroInt then r `plusInt` y else zeroInt
548 gtInt, geInt, eqInt, neInt, ltInt, leInt :: Int -> Int -> Bool
549 gtInt (I# x) (I# y) = x ># y
550 geInt (I# x) (I# y) = x >=# y
551 eqInt (I# x) (I# y) = x ==# y
552 neInt (I# x) (I# y) = x /=# y
553 ltInt (I# x) (I# y) = x <# y
554 leInt (I# x) (I# y) = x <=# y
558 %********************************************************
560 \subsection{Unpacking C strings}
562 %********************************************************
564 This code is needed for virtually all programs, since it's used for
565 unpacking the strings of error messages.
568 unpackCString# :: Addr# -> [Char]
569 {-# INLINE unpackCString# #-}
570 unpackCString# a = build (unpackFoldrCString# a)
572 unpackCStringList# :: Addr# -> [Char]
573 unpackCStringList# addr
577 | ch `eqChar#` '\0'# = []
578 | otherwise = C# ch : unpack (nh +# 1#)
580 ch = indexCharOffAddr# addr nh
582 unpackAppendCString# :: Addr# -> [Char] -> [Char]
583 unpackAppendCString# addr rest
587 | ch `eqChar#` '\0'# = rest
588 | otherwise = C# ch : unpack (nh +# 1#)
590 ch = indexCharOffAddr# addr nh
592 unpackFoldrCString# :: Addr# -> (Char -> a -> a) -> a -> a
593 unpackFoldrCString# addr f z
597 | ch `eqChar#` '\0'# = z
598 | otherwise = C# ch `f` unpack (nh +# 1#)
600 ch = indexCharOffAddr# addr nh
602 unpackNBytes# :: Addr# -> Int# -> [Char]
603 -- This one is called by the compiler to unpack literal
604 -- strings with NULs in them; rare. It's strict!
605 -- We don't try to do list deforestation for this one
607 unpackNBytes# _addr 0# = []
608 unpackNBytes# addr len# = unpack [] (len# -# 1#)
613 case indexCharOffAddr# addr i# of
614 ch -> unpack (C# ch : acc) (i# -# 1#)
617 "unpack-list" forall a . unpackFoldrCString# a (:) [] = unpackCStringList# a
618 "unpack-append" forall a n . unpackFoldrCString# a (:) n = unpackAppendCString# a n
620 -- There's a built-in rule (in PrelRules.lhs) for
621 -- unpackFoldr "foo" c (unpackFoldr "baz" c n) = unpackFoldr "foobaz" c n