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
250 ----------------------------------------------
252 ----------------------------------------------
255 map :: (a -> b) -> [a] -> [b]
257 map f xs = build (\c n -> foldr (mapFB c f) n xs)
260 mapFB c f x ys = c (f x) ys
262 mapList :: (a -> b) -> [a] -> [b]
264 mapList f (x:xs) = f x : mapList f xs
267 "mapFB" forall c f g. mapFB (mapFB c f) g = mapFB c (f.g)
268 "mapList" forall f. foldr (mapFB (:) f) [] = mapList f
273 ----------------------------------------------
275 ----------------------------------------------
277 (++) :: [a] -> [a] -> [a]
279 xs ++ ys = augment (\c n -> foldr c n xs) ys
281 append :: [a] -> [a] -> [a]
283 append (x:xs) ys = x : append xs ys
287 %*********************************************************
289 \subsection{Type @Bool@}
291 %*********************************************************
294 data Bool = False | True deriving (Eq, Ord)
295 -- Read in PrelRead, Show in PrelShow
299 (&&), (||) :: Bool -> Bool -> Bool
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.)
334 instance Ord () where
345 %*********************************************************
347 \subsection{Type @Ordering@}
349 %*********************************************************
352 data Ordering = LT | EQ | GT deriving (Eq, Ord)
353 -- Read in PrelRead, Show in PrelShow
357 %*********************************************************
359 \subsection{Type @Char@ and @String@}
361 %*********************************************************
368 -- We don't use deriving for Eq and Ord, because for Ord the derived
369 -- instance defines only compare, which takes two primops. Then
370 -- '>' uses compare, and therefore takes two primops instead of one.
372 instance Eq Char where
373 (C# c1) == (C# c2) = c1 `eqChar#` c2
374 (C# c1) /= (C# c2) = c1 `neChar#` c2
376 instance Ord Char where
377 (C# c1) > (C# c2) = c1 `gtChar#` c2
378 (C# c1) >= (C# c2) = c1 `geChar#` c2
379 (C# c1) <= (C# c2) = c1 `leChar#` c2
380 (C# c1) < (C# c2) = c1 `ltChar#` c2
383 chr (I# i) | i >=# 0# && i <=# 255# = C# (chr# i)
384 | otherwise = error ("Prelude.chr: bad argument")
386 unsafeChr :: Int -> Char
387 unsafeChr (I# i) = C# (chr# i)
390 ord (C# c) = I# (ord# c)
394 %*********************************************************
396 \subsection{Type @Int@}
398 %*********************************************************
403 zeroInt, oneInt, twoInt, maxInt, minInt :: Int
407 minInt = I# (-2147483648#) -- GHC <= 2.09 had this at -2147483647
408 maxInt = I# 2147483647#
410 instance Eq Int where
411 (==) x y = x `eqInt` y
412 (/=) x y = x `neInt` y
414 instance Ord Int where
415 compare x y = compareInt x y
422 compareInt :: Int -> Int -> Ordering
423 (I# x) `compareInt` (I# y) | x <# y = LT
429 %*********************************************************
431 \subsection{The function type}
433 %*********************************************************
444 -- function composition
446 (.) :: (b -> c) -> (a -> b) -> a -> c
449 -- flip f takes its (first) two arguments in the reverse order of f.
450 flip :: (a -> b -> c) -> b -> a -> c
453 -- right-associating infix application operator (useful in continuation-
455 ($) :: (a -> b) -> a -> b
458 -- until p f yields the result of applying f until p holds.
459 until :: (a -> Bool) -> (a -> a) -> a -> a
460 until p f x | p x = x
461 | otherwise = until p f (f x)
463 -- asTypeOf is a type-restricted version of const. It is usually used
464 -- as an infix operator, and its typing forces its first argument
465 -- (which is usually overloaded) to have the same type as the second.
466 asTypeOf :: a -> a -> a
470 %*********************************************************
472 \subsection{CCallable instances}
474 %*********************************************************
476 Defined here to avoid orphans
479 instance CCallable Char
480 instance CReturnable Char
482 instance CCallable Int
483 instance CReturnable Int
485 -- DsCCall knows how to pass strings...
486 instance CCallable [Char]
488 instance CReturnable () -- Why, exactly?
492 %*********************************************************
494 \subsection{Numeric primops}
496 %*********************************************************
498 Definitions of the boxed PrimOps; these will be
499 used in the case of partial applications, etc.
508 {-# INLINE plusInt #-}
509 {-# INLINE minusInt #-}
510 {-# INLINE timesInt #-}
511 {-# INLINE quotInt #-}
512 {-# INLINE remInt #-}
513 {-# INLINE negateInt #-}
515 plusInt, minusInt, timesInt, quotInt, remInt, gcdInt :: Int -> Int -> Int
516 plusInt (I# x) (I# y) = I# (x +# y)
517 minusInt(I# x) (I# y) = I# (x -# y)
518 timesInt(I# x) (I# y) = I# (x *# y)
519 quotInt (I# x) (I# y) = I# (quotInt# x y)
520 remInt (I# x) (I# y) = I# (remInt# x y)
521 gcdInt (I# a) (I# b) = I# (gcdInt# a b)
523 negateInt :: Int -> Int
524 negateInt (I# x) = I# (negateInt# x)
526 divInt, modInt :: Int -> Int -> Int
528 | x > zeroInt && y < zeroInt = quotInt ((x `minusInt` y) `minusInt` oneInt) y
529 | x < zeroInt && y > zeroInt = quotInt ((x `minusInt` y) `plusInt` oneInt) y
530 | otherwise = quotInt x y
533 | x > zeroInt && y < zeroInt ||
534 x < zeroInt && y > zeroInt = if r/=zeroInt then r `plusInt` y else zeroInt
539 gtInt, geInt, eqInt, neInt, ltInt, leInt :: Int -> Int -> Bool
540 gtInt (I# x) (I# y) = x ># y
541 geInt (I# x) (I# y) = x >=# y
542 eqInt (I# x) (I# y) = x ==# y
543 neInt (I# x) (I# y) = x /=# y
544 ltInt (I# x) (I# y) = x <# y
545 leInt (I# x) (I# y) = x <=# y