1 % -----------------------------------------------------------------------------
2 % $Id: PrelBase.lhs,v 1.42 2001/02/28 00:01:03 qrczak 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 #-}
83 module PrelGHC, -- Re-export PrelGHC and PrelErr, to avoid lots
84 module PrelErr -- of people having to import it explicitly
89 import {-# SOURCE #-} PrelErr
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 class (Eq a) => Ord a where
153 compare :: a -> a -> Ordering
154 (<), (<=), (>), (>=) :: a -> a -> Bool
155 max, min :: a -> a -> a
157 -- An instance of Ord should define either 'compare' or '<='.
158 -- Using 'compare' can be more efficient for complex types.
162 | x <= y = LT -- NB: must be '<=' not '<' to validate the
163 -- above claim about the minimal things that
164 -- can be defined for an instance of Ord
167 x < y = case compare x y of { LT -> True; _other -> False }
168 x <= y = case compare x y of { GT -> False; _other -> True }
169 x > y = case compare x y of { GT -> True; _other -> False }
170 x >= y = case compare x y of { LT -> False; _other -> True }
172 -- These two default methods use '<=' rather than 'compare'
173 -- because the latter is often more expensive
174 max x y = if x <= y then y else x
175 min x y = if x <= y then x else y
178 %*********************************************************
180 \subsection{Monadic classes @Functor@, @Monad@ }
182 %*********************************************************
185 class Functor f where
186 fmap :: (a -> b) -> f a -> f b
189 (>>=) :: m a -> (a -> m b) -> m b
190 (>>) :: m a -> m b -> m b
192 fail :: String -> m a
194 m >> k = m >>= \_ -> k
199 %*********************************************************
201 \subsection{The list type}
203 %*********************************************************
206 data [] a = [] | a : [a] -- do explicitly: deriving (Eq, Ord)
207 -- to avoid weird names like con2tag_[]#
210 instance (Eq a) => Eq [a] where
212 {-# SPECIALISE instance Eq [Char] #-}
215 (x:xs) == (y:ys) = x == y && xs == ys
218 instance (Ord a) => Ord [a] where
220 {-# SPECIALISE instance Ord [Char] #-}
222 a < b = case compare a b of { LT -> True; EQ -> False; GT -> False }
223 a <= b = case compare a b of { LT -> True; EQ -> True; GT -> False }
224 a >= b = case compare a b of { LT -> False; EQ -> True; GT -> True }
225 a > b = case compare a b of { LT -> False; EQ -> False; GT -> True }
228 compare (_:_) [] = GT
229 compare [] (_:_) = LT
230 compare (x:xs) (y:ys) = case compare x y of
235 instance Functor [] where
238 instance Monad [] where
239 m >>= k = foldr ((++) . k) [] m
240 m >> k = foldr ((++) . (\ _ -> k)) [] m
245 A few list functions that appear here because they are used here.
246 The rest of the prelude list functions are in PrelList.
248 ----------------------------------------------
249 -- foldr/build/augment
250 ----------------------------------------------
253 foldr :: (a -> b -> b) -> b -> [a] -> b
255 -- foldr f z (x:xs) = f x (foldr f z xs)
260 go (y:ys) = y `k` go ys
262 build :: forall a. (forall b. (a -> b -> b) -> b -> b) -> [a]
263 {-# INLINE 2 build #-}
264 -- The INLINE is important, even though build is tiny,
265 -- because it prevents [] getting inlined in the version that
266 -- appears in the interface file. If [] *is* inlined, it
267 -- won't match with [] appearing in rules in an importing module.
269 -- The "2" says to inline in phase 2
273 augment :: forall a. (forall b. (a->b->b) -> b -> b) -> [a] -> [a]
274 {-# INLINE 2 augment #-}
275 augment g xs = g (:) xs
278 "fold/build" forall k z (g::forall b. (a->b->b) -> b -> b) .
279 foldr k z (build g) = g k z
281 "foldr/augment" forall k z xs (g::forall b. (a->b->b) -> b -> b) .
282 foldr k z (augment g xs) = g k (foldr k z xs)
284 "foldr/id" foldr (:) [] = \x->x
285 "foldr/app" forall xs ys. foldr (:) ys xs = append xs ys
287 "foldr/cons" forall k z x xs. foldr k z (x:xs) = k x (foldr k z xs)
288 "foldr/nil" forall k z. foldr k z [] = z
290 "augment/build" forall (g::forall b. (a->b->b) -> b -> b)
291 (h::forall b. (a->b->b) -> b -> b) .
292 augment g (build h) = build (\c n -> g c (h c n))
293 "augment/nil" forall (g::forall b. (a->b->b) -> b -> b) .
294 augment g [] = build g
297 -- This rule is true, but not (I think) useful:
298 -- augment g (augment h t) = augment (\cn -> g c (h c n)) t
302 ----------------------------------------------
304 ----------------------------------------------
307 map :: (a -> b) -> [a] -> [b]
311 mapFB :: (elt -> lst -> lst) -> (a -> elt) -> a -> lst -> lst
312 mapFB c f x ys = c (f x) ys
314 mapList :: (a -> b) -> [a] -> [b]
316 mapList f (x:xs) = f x : mapList f xs
319 "map" forall f xs. map f xs = build (\c n -> foldr (mapFB c f) n xs)
320 "mapFB" forall c f g. mapFB (mapFB c f) g = mapFB c (f.g)
321 "mapList" forall f. foldr (mapFB (:) f) [] = mapList f
326 ----------------------------------------------
328 ----------------------------------------------
330 (++) :: [a] -> [a] -> [a]
334 "++" forall xs ys. (++) xs ys = augment (\c n -> foldr c n xs) ys
337 append :: [a] -> [a] -> [a]
339 append (x:xs) ys = x : append xs ys
343 %*********************************************************
345 \subsection{Type @Bool@}
347 %*********************************************************
350 data Bool = False | True deriving (Eq, Ord)
351 -- Read in PrelRead, Show in PrelShow
355 (&&), (||) :: Bool -> Bool -> Bool
370 %*********************************************************
372 \subsection{The @()@ type}
374 %*********************************************************
376 The Unit type is here because virtually any program needs it (whereas
377 some programs may get away without consulting PrelTup). Furthermore,
378 the renamer currently *always* asks for () to be in scope, so that
379 ccalls can use () as their default type; so when compiling PrelBase we
380 need (). (We could arrange suck in () only if -fglasgow-exts, but putting
381 it here seems more direct.)
390 instance Ord () where
401 %*********************************************************
403 \subsection{Type @Ordering@}
405 %*********************************************************
408 data Ordering = LT | EQ | GT deriving (Eq, Ord)
409 -- Read in PrelRead, Show in PrelShow
413 %*********************************************************
415 \subsection{Type @Char@ and @String@}
417 %*********************************************************
424 -- We don't use deriving for Eq and Ord, because for Ord the derived
425 -- instance defines only compare, which takes two primops. Then
426 -- '>' uses compare, and therefore takes two primops instead of one.
428 instance Eq Char where
429 (C# c1) == (C# c2) = c1 `eqChar#` c2
430 (C# c1) /= (C# c2) = c1 `neChar#` c2
432 instance Ord Char where
433 (C# c1) > (C# c2) = c1 `gtChar#` c2
434 (C# c1) >= (C# c2) = c1 `geChar#` c2
435 (C# c1) <= (C# c2) = c1 `leChar#` c2
436 (C# c1) < (C# c2) = c1 `ltChar#` c2
439 chr (I# i) | i >=# 0# && i <=# 0x10FFFF#
441 | otherwise = error "Prelude.chr: bad argument"
443 unsafeChr :: Int -> Char
444 unsafeChr (I# i) = C# (chr# i)
447 ord (C# c) = I# (ord# c)
450 String equality is used when desugaring pattern-matches against strings.
451 It's worth making it fast, and providing a rule to use the fast version
455 eqString :: String -> String -> Bool
456 eqString [] [] = True
457 eqString (C# c1 : cs1) (C# c2 : cs2) = c1 `eqChar#` c2 && cs1 `eqString` cs2
461 "eqString" (==) = eqString
465 %*********************************************************
467 \subsection{Type @Int@}
469 %*********************************************************
474 zeroInt, oneInt, twoInt, maxInt, minInt :: Int
478 #if WORD_SIZE_IN_BYTES == 4
479 minInt = I# (-0x80000000#)
480 maxInt = I# 0x7FFFFFFF#
482 minInt = I# (-0x8000000000000000#)
483 maxInt = I# 0x7FFFFFFFFFFFFFFF#
486 instance Eq Int where
490 instance Ord Int where
498 compareInt :: Int -> Int -> Ordering
499 (I# x) `compareInt` (I# y) = compareInt# x y
501 compareInt# :: Int# -> Int# -> Ordering
509 %*********************************************************
511 \subsection{The function type}
513 %*********************************************************
524 -- function composition
526 (.) :: (b -> c) -> (a -> b) -> a -> c
529 -- flip f takes its (first) two arguments in the reverse order of f.
530 flip :: (a -> b -> c) -> b -> a -> c
533 -- right-associating infix application operator (useful in continuation-
536 ($) :: (a -> b) -> a -> b
539 -- until p f yields the result of applying f until p holds.
540 until :: (a -> Bool) -> (a -> a) -> a -> a
541 until p f x | p x = x
542 | otherwise = until p f (f x)
544 -- asTypeOf is a type-restricted version of const. It is usually used
545 -- as an infix operator, and its typing forces its first argument
546 -- (which is usually overloaded) to have the same type as the second.
547 asTypeOf :: a -> a -> a
551 %*********************************************************
553 \subsection{CCallable instances}
555 %*********************************************************
557 Defined here to avoid orphans
560 instance CCallable Char
561 instance CReturnable Char
563 instance CCallable Int
564 instance CReturnable Int
566 instance CReturnable () -- Why, exactly?
570 %*********************************************************
572 \subsection{Generics}
574 %*********************************************************
578 data a :+: b = Inl a | Inr b
579 data a :*: b = a :*: b
583 %*********************************************************
585 \subsection{Numeric primops}
587 %*********************************************************
590 divInt#, modInt# :: Int# -> Int# -> Int#
592 | (x# ># 0#) && (y# <# 0#) = ((x# -# y#) -# 1#) `quotInt#` y#
593 | (x# <# 0#) && (y# ># 0#) = ((x# -# y#) +# 1#) `quotInt#` y#
594 | otherwise = x# `quotInt#` y#
596 | (x# ># 0#) && (y# <# 0#) ||
597 (x# <# 0#) && (y# ># 0#) = if r# /=# 0# then r# +# y# else 0#
603 Definitions of the boxed PrimOps; these will be
604 used in the case of partial applications, etc.
613 {-# INLINE plusInt #-}
614 {-# INLINE minusInt #-}
615 {-# INLINE timesInt #-}
616 {-# INLINE quotInt #-}
617 {-# INLINE remInt #-}
618 {-# INLINE negateInt #-}
620 plusInt, minusInt, timesInt, quotInt, remInt, divInt, modInt, gcdInt :: Int -> Int -> Int
621 (I# x) `plusInt` (I# y) = I# (x +# y)
622 (I# x) `minusInt` (I# y) = I# (x -# y)
623 (I# x) `timesInt` (I# y) = I# (x *# y)
624 (I# x) `quotInt` (I# y) = I# (x `quotInt#` y)
625 (I# x) `remInt` (I# y) = I# (x `remInt#` y)
626 (I# x) `divInt` (I# y) = I# (x `divInt#` y)
627 (I# x) `modInt` (I# y) = I# (x `modInt#` y)
629 gcdInt (I# a) (I# b) = g a b
630 where g 0# 0# = error "PrelBase.gcdInt: gcd 0 0 is undefined"
633 g _ _ = I# (gcdInt# absA absB)
635 absInt x = if x <# 0# then negateInt# x else x
640 negateInt :: Int -> Int
641 negateInt (I# x) = I# (negateInt# x)
643 gtInt, geInt, eqInt, neInt, ltInt, leInt :: Int -> Int -> Bool
644 (I# x) `gtInt` (I# y) = x ># y
645 (I# x) `geInt` (I# y) = x >=# y
646 (I# x) `eqInt` (I# y) = x ==# y
647 (I# x) `neInt` (I# y) = x /=# y
648 (I# x) `ltInt` (I# y) = x <# y
649 (I# x) `leInt` (I# y) = x <=# y
652 "int2Word2Int" forall x#. int2Word# (word2Int# x#) = x#
653 "word2Int2Word" forall x#. word2Int# (int2Word# x#) = x#
658 %********************************************************
660 \subsection{Unpacking C strings}
662 %********************************************************
664 This code is needed for virtually all programs, since it's used for
665 unpacking the strings of error messages.
668 unpackCString# :: Addr# -> [Char]
669 unpackCString# a = unpackCStringList# a
671 unpackCStringList# :: Addr# -> [Char]
672 unpackCStringList# addr
676 | ch `eqChar#` '\0'# = []
677 | otherwise = C# ch : unpack (nh +# 1#)
679 ch = indexCharOffAddr# addr nh
681 unpackAppendCString# :: Addr# -> [Char] -> [Char]
682 unpackAppendCString# addr rest
686 | ch `eqChar#` '\0'# = rest
687 | otherwise = C# ch : unpack (nh +# 1#)
689 ch = indexCharOffAddr# addr nh
691 unpackFoldrCString# :: Addr# -> (Char -> a -> a) -> a -> a
692 unpackFoldrCString# addr f z
696 | ch `eqChar#` '\0'# = z
697 | otherwise = C# ch `f` unpack (nh +# 1#)
699 ch = indexCharOffAddr# addr nh
701 unpackCStringUtf8# :: Addr# -> [Char]
702 unpackCStringUtf8# addr
706 | ch `eqChar#` '\0'# = []
707 | ch `leChar#` '\x7F'# = C# ch : unpack (nh +# 1#)
708 | ch `leChar#` '\xDF'# =
709 C# (chr# ((ord# ch -# 0xC0#) `iShiftL#` 6# +#
710 (ord# (indexCharOffAddr# addr (nh +# 1#)) -# 0x80#))) :
712 | ch `leChar#` '\xEF'# =
713 C# (chr# ((ord# ch -# 0xE0#) `iShiftL#` 12# +#
714 (ord# (indexCharOffAddr# addr (nh +# 1#)) -# 0x80#) `iShiftL#` 6# +#
715 (ord# (indexCharOffAddr# addr (nh +# 2#)) -# 0x80#))) :
718 C# (chr# ((ord# ch -# 0xF0#) `iShiftL#` 18# +#
719 (ord# (indexCharOffAddr# addr (nh +# 1#)) -# 0x80#) `iShiftL#` 12# +#
720 (ord# (indexCharOffAddr# addr (nh +# 2#)) -# 0x80#) `iShiftL#` 6# +#
721 (ord# (indexCharOffAddr# addr (nh +# 3#)) -# 0x80#))) :
724 ch = indexCharOffAddr# addr nh
726 unpackNBytes# :: Addr# -> Int# -> [Char]
727 unpackNBytes# _addr 0# = []
728 unpackNBytes# addr len# = unpack [] (len# -# 1#)
733 case indexCharOffAddr# addr i# of
734 ch -> unpack (C# ch : acc) (i# -# 1#)
737 "unpack" forall a . unpackCString# a = build (unpackFoldrCString# a)
738 "unpack-list" forall a . unpackFoldrCString# a (:) [] = unpackCStringList# a
739 "unpack-append" forall a n . unpackFoldrCString# a (:) n = unpackAppendCString# a n
741 -- There's a built-in rule (in PrelRules.lhs) for
742 -- unpackFoldr "foo" c (unpackFoldr "baz" c n) = unpackFoldr "foobaz" c n