1 % -----------------------------------------------------------------------------
2 % $Id: PrelBase.lhs,v 1.37 2000/09/07 09:10:07 simonpj 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 unpackCStringUtf8# :: Addr# -> [Char]
131 unpackCString# a = error "urk"
132 unpackFoldrCString# a = error "urk"
133 unpackAppendCString# a = error "urk"
134 unpackCStringUtf8# 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
201 %*********************************************************
203 \subsection{The list type}
205 %*********************************************************
208 data [] a = [] | a : [a] -- do explicitly: deriving (Eq, Ord)
209 -- to avoid weird names like con2tag_[]#
212 instance (Eq a) => Eq [a] where
214 {-# SPECIALISE instance Eq [Char] #-}
217 (x:xs) == (y:ys) = x == y && xs == ys
220 xs /= ys = if (xs == ys) then False else True
222 instance (Ord a) => Ord [a] where
224 {-# SPECIALISE instance Ord [Char] #-}
226 a < b = case compare a b of { LT -> True; EQ -> False; GT -> False }
227 a <= b = case compare a b of { LT -> True; EQ -> True; GT -> False }
228 a >= b = case compare a b of { LT -> False; EQ -> True; GT -> True }
229 a > b = case compare a b of { LT -> False; EQ -> False; GT -> True }
232 compare (_:_) [] = GT
233 compare [] (_:_) = LT
234 compare (x:xs) (y:ys) = case compare x y of
239 instance Functor [] where
242 instance Monad [] where
243 m >>= k = foldr ((++) . k) [] m
244 m >> k = foldr ((++) . (\ _ -> k)) [] m
249 A few list functions that appear here because they are used here.
250 The rest of the prelude list functions are in PrelList.
252 ----------------------------------------------
253 -- foldr/build/augment
254 ----------------------------------------------
257 foldr :: (a -> b -> b) -> b -> [a] -> b
259 -- foldr f z (x:xs) = f x (foldr f z xs)
264 go (x:xs) = x `k` go xs
266 build :: forall a. (forall b. (a -> b -> b) -> b -> b) -> [a]
267 {-# INLINE 2 build #-}
268 -- The INLINE is important, even though build is tiny,
269 -- because it prevents [] getting inlined in the version that
270 -- appears in the interface file. If [] *is* inlined, it
271 -- won't match with [] appearing in rules in an importing module.
273 -- The "2" says to inline in phase 2
277 augment :: forall a. (forall b. (a->b->b) -> b -> b) -> [a] -> [a]
278 {-# INLINE 2 augment #-}
279 augment g xs = g (:) xs
282 "fold/build" forall k z (g::forall b. (a->b->b) -> b -> b) .
283 foldr k z (build g) = g k z
285 "foldr/augment" forall k z xs (g::forall b. (a->b->b) -> b -> b) .
286 foldr k z (augment g xs) = g k (foldr k z xs)
288 "foldr/id" foldr (:) [] = \x->x
289 "foldr/app" forall xs ys. foldr (:) ys xs = append xs ys
291 "foldr/cons" forall k z x xs. foldr k z (x:xs) = k x (foldr k z xs)
292 "foldr/nil" forall k z. foldr k z [] = z
294 "augment/build" forall (g::forall b. (a->b->b) -> b -> b)
295 (h::forall b. (a->b->b) -> b -> b) .
296 augment g (build h) = build (\c n -> g c (h c n))
297 "augment/nil" forall (g::forall b. (a->b->b) -> b -> b) .
298 augment g [] = build g
301 -- This rule is true, but not (I think) useful:
302 -- augment g (augment h t) = augment (\cn -> g c (h c n)) t
306 ----------------------------------------------
308 ----------------------------------------------
311 map :: (a -> b) -> [a] -> [b]
315 mapFB c f x ys = c (f x) ys
317 mapList :: (a -> b) -> [a] -> [b]
319 mapList f (x:xs) = f x : mapList f xs
322 "map" forall f xs. map f xs = build (\c n -> foldr (mapFB c f) n xs)
323 "mapFB" forall c f g. mapFB (mapFB c f) g = mapFB c (f.g)
324 "mapList" forall f. foldr (mapFB (:) f) [] = mapList f
329 ----------------------------------------------
331 ----------------------------------------------
333 (++) :: [a] -> [a] -> [a]
337 "++" forall xs ys. (++) xs ys = augment (\c n -> foldr c n xs) ys
340 append :: [a] -> [a] -> [a]
342 append (x:xs) ys = x : append xs ys
346 %*********************************************************
348 \subsection{Type @Bool@}
350 %*********************************************************
353 data Bool = False | True deriving (Eq, Ord)
354 -- Read in PrelRead, Show in PrelShow
358 (&&), (||) :: Bool -> Bool -> Bool
373 %*********************************************************
375 \subsection{The @()@ type}
377 %*********************************************************
379 The Unit type is here because virtually any program needs it (whereas
380 some programs may get away without consulting PrelTup). Furthermore,
381 the renamer currently *always* asks for () to be in scope, so that
382 ccalls can use () as their default type; so when compiling PrelBase we
383 need (). (We could arrange suck in () only if -fglasgow-exts, but putting
384 it here seems more direct.)
393 instance Ord () where
404 %*********************************************************
406 \subsection{Type @Ordering@}
408 %*********************************************************
411 data Ordering = LT | EQ | GT deriving (Eq, Ord)
412 -- Read in PrelRead, Show in PrelShow
416 %*********************************************************
418 \subsection{Type @Char@ and @String@}
420 %*********************************************************
427 -- We don't use deriving for Eq and Ord, because for Ord the derived
428 -- instance defines only compare, which takes two primops. Then
429 -- '>' uses compare, and therefore takes two primops instead of one.
431 instance Eq Char where
432 (C# c1) == (C# c2) = c1 `eqChar#` c2
433 (C# c1) /= (C# c2) = c1 `neChar#` c2
435 instance Ord Char where
436 (C# c1) > (C# c2) = c1 `gtChar#` c2
437 (C# c1) >= (C# c2) = c1 `geChar#` c2
438 (C# c1) <= (C# c2) = c1 `leChar#` c2
439 (C# c1) < (C# c2) = c1 `ltChar#` c2
442 chr (I# i) | i >=# 0#
443 #if INT_SIZE_IN_BYTES > 4
447 | otherwise = error ("Prelude.chr: bad argument")
449 unsafeChr :: Int -> Char
450 unsafeChr (I# i) = C# (chr# i)
453 ord (C# c) = I# (ord# c)
456 String equality is used when desugaring pattern-matches against strings.
457 It's worth making it fast, and providing a rule to use the fast version
461 eqString :: String -> String -> Bool
462 eqString [] [] = True
463 eqString (C# c1 : cs1) (C# c2 : cs2) = c1 `eqChar#` c2 && cs1 `eqString` cs2
467 "eqString" (==) = eqString
471 %*********************************************************
473 \subsection{Type @Int@}
475 %*********************************************************
480 zeroInt, oneInt, twoInt, maxInt, minInt :: Int
484 minInt = I# (-2147483648#) -- GHC <= 2.09 had this at -2147483647
485 maxInt = I# 2147483647#
487 instance Eq Int where
488 (==) x y = x `eqInt` y
489 (/=) x y = x `neInt` y
491 instance Ord Int where
492 compare x y = compareInt x y
499 compareInt :: Int -> Int -> Ordering
500 (I# x) `compareInt` (I# y) | x <# y = LT
506 %*********************************************************
508 \subsection{The function type}
510 %*********************************************************
521 -- function composition
523 (.) :: (b -> c) -> (a -> b) -> a -> c
526 -- flip f takes its (first) two arguments in the reverse order of f.
527 flip :: (a -> b -> c) -> b -> a -> c
530 -- right-associating infix application operator (useful in continuation-
532 ($) :: (a -> b) -> a -> b
535 -- until p f yields the result of applying f until p holds.
536 until :: (a -> Bool) -> (a -> a) -> a -> a
537 until p f x | p x = x
538 | otherwise = until p f (f x)
540 -- asTypeOf is a type-restricted version of const. It is usually used
541 -- as an infix operator, and its typing forces its first argument
542 -- (which is usually overloaded) to have the same type as the second.
543 asTypeOf :: a -> a -> a
547 %*********************************************************
549 \subsection{CCallable instances}
551 %*********************************************************
553 Defined here to avoid orphans
556 instance CCallable Char
557 instance CReturnable Char
559 instance CCallable Int
560 instance CReturnable Int
562 instance CReturnable () -- Why, exactly?
566 %*********************************************************
568 \subsection{Numeric primops}
570 %*********************************************************
572 Definitions of the boxed PrimOps; these will be
573 used in the case of partial applications, etc.
582 {-# INLINE plusInt #-}
583 {-# INLINE minusInt #-}
584 {-# INLINE timesInt #-}
585 {-# INLINE quotInt #-}
586 {-# INLINE remInt #-}
587 {-# INLINE negateInt #-}
589 plusInt, minusInt, timesInt, quotInt, remInt, gcdInt :: Int -> Int -> Int
590 plusInt (I# x) (I# y) = I# (x +# y)
591 minusInt(I# x) (I# y) = I# (x -# y)
592 timesInt(I# x) (I# y) = I# (x *# y)
593 quotInt (I# x) (I# y) = I# (quotInt# x y)
594 remInt (I# x) (I# y) = I# (remInt# x y)
596 gcdInt (I# a) (I# b) = g a b
597 where g 0# 0# = error "PrelBase.gcdInt: gcd 0 0 is undefined"
600 g _ _ = I# (gcdInt# absA absB)
602 absInt x = if x <# 0# then negateInt# x else x
607 negateInt :: Int -> Int
608 negateInt (I# x) = I# (negateInt# x)
610 divInt, modInt :: Int -> Int -> Int
612 | x > zeroInt && y < zeroInt = quotInt ((x `minusInt` y) `minusInt` oneInt) y
613 | x < zeroInt && y > zeroInt = quotInt ((x `minusInt` y) `plusInt` oneInt) y
614 | otherwise = quotInt x y
617 | x > zeroInt && y < zeroInt ||
618 x < zeroInt && y > zeroInt = if r/=zeroInt then r `plusInt` y else zeroInt
623 gtInt, geInt, eqInt, neInt, ltInt, leInt :: Int -> Int -> Bool
624 gtInt (I# x) (I# y) = x ># y
625 geInt (I# x) (I# y) = x >=# y
626 eqInt (I# x) (I# y) = x ==# y
627 neInt (I# x) (I# y) = x /=# y
628 ltInt (I# x) (I# y) = x <# y
629 leInt (I# x) (I# y) = x <=# y
633 %********************************************************
635 \subsection{Unpacking C strings}
637 %********************************************************
639 This code is needed for virtually all programs, since it's used for
640 unpacking the strings of error messages.
643 unpackCString# :: Addr# -> [Char]
644 unpackCString# a = unpackCStringList# a
646 unpackCStringList# :: Addr# -> [Char]
647 unpackCStringList# addr
651 | ch `eqChar#` '\0'# = []
652 | otherwise = C# ch : unpack (nh +# 1#)
654 ch = indexCharOffAddr# addr nh
656 unpackAppendCString# :: Addr# -> [Char] -> [Char]
657 unpackAppendCString# addr rest
661 | ch `eqChar#` '\0'# = rest
662 | otherwise = C# ch : unpack (nh +# 1#)
664 ch = indexCharOffAddr# addr nh
666 unpackFoldrCString# :: Addr# -> (Char -> a -> a) -> a -> a
667 unpackFoldrCString# addr f z
671 | ch `eqChar#` '\0'# = z
672 | otherwise = C# ch `f` unpack (nh +# 1#)
674 ch = indexCharOffAddr# addr nh
676 unpackCStringUtf8# :: Addr# -> [Char]
677 unpackCStringUtf8# addr
681 | ch `eqChar#` '\0'# = []
682 | ch `leChar#` '\x7F'# = C# ch : unpack (nh +# 1#)
683 | ch `leChar#` '\xDF'# = C# (chr# ((ord# ch `iShiftL#` 6#) +#
684 (ord# (indexCharOffAddr# addr (nh +# 1#))) -# 0x3080#))
686 | ch `leChar#` '\xEF'# = C# (chr# ((ord# ch `iShiftL#` 12#) +#
687 (ord# (indexCharOffAddr# addr (nh +# 1#)) `iShiftL#` 6#) +#
688 (ord# (indexCharOffAddr# addr (nh +# 2#))) -# 0xE2080#))
690 | ch `leChar#` '\xF7'# = C# (chr# ((ord# ch `iShiftL#` 18#) +#
691 (ord# (indexCharOffAddr# addr (nh +# 1#)) `iShiftL#` 12#) +#
692 (ord# (indexCharOffAddr# addr (nh +# 2#)) `iShiftL#` 6#) +#
693 (ord# (indexCharOffAddr# addr (nh +# 3#))) -# 0x3C82080#))
695 | ch `leChar#` '\xFB'# = C# (chr# ((ord# ch -# 0xF8# `iShiftL#` 24#) +#
696 (ord# (indexCharOffAddr# addr (nh +# 1#)) `iShiftL#` 18#) +#
697 (ord# (indexCharOffAddr# addr (nh +# 2#)) `iShiftL#` 12#) +#
698 (ord# (indexCharOffAddr# addr (nh +# 3#)) `iShiftL#` 6#) +#
699 (ord# (indexCharOffAddr# addr (nh +# 4#))) -# 0x2082080#))
701 | otherwise = C# (chr# (((ord# ch -# 0xFC#) `iShiftL#` 30#) +#
702 ((ord# (indexCharOffAddr# addr (nh +# 1#)) -# 0x80#)
704 (ord# (indexCharOffAddr# addr (nh +# 2#)) `iShiftL#` 18#) +#
705 (ord# (indexCharOffAddr# addr (nh +# 3#)) `iShiftL#` 12#) +#
706 (ord# (indexCharOffAddr# addr (nh +# 4#)) `iShiftL#` 6#) +#
707 (ord# (indexCharOffAddr# addr (nh +# 5#))) -# 0x2082080#))
710 ch = indexCharOffAddr# addr nh
712 unpackNBytes# :: Addr# -> Int# -> [Char]
713 unpackNBytes# _addr 0# = []
714 unpackNBytes# addr len# = unpack [] (len# -# 1#)
719 case indexCharOffAddr# addr i# of
720 ch -> unpack (C# ch : acc) (i# -# 1#)
723 "unpack" forall a . unpackCString# a = build (unpackFoldrCString# a)
724 "unpack-list" forall a . unpackFoldrCString# a (:) [] = unpackCStringList# a
725 "unpack-append" forall a n . unpackFoldrCString# a (:) n = unpackAppendCString# a n
727 -- There's a built-in rule (in PrelRules.lhs) for
728 -- unpackFoldr "foo" c (unpackFoldr "baz" c n) = unpackFoldr "foobaz" c n