1 % -----------------------------------------------------------------------------
2 % $Id: PrelBase.lhs,v 1.35 2000/08/07 23:37:23 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 #-}
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
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#
444 #if INT_SIZE_IN_BYTES > 4
448 | otherwise = error ("Prelude.chr: bad argument")
450 unsafeChr :: Int -> Char
451 unsafeChr (I# i) = C# (chr# i)
454 ord (C# c) = I# (ord# c)
458 %*********************************************************
460 \subsection{Type @Int@}
462 %*********************************************************
467 zeroInt, oneInt, twoInt, maxInt, minInt :: Int
471 minInt = I# (-2147483648#) -- GHC <= 2.09 had this at -2147483647
472 maxInt = I# 2147483647#
474 instance Eq Int where
475 (==) x y = x `eqInt` y
476 (/=) x y = x `neInt` y
478 instance Ord Int where
479 compare x y = compareInt x y
486 compareInt :: Int -> Int -> Ordering
487 (I# x) `compareInt` (I# y) | x <# y = LT
493 %*********************************************************
495 \subsection{The function type}
497 %*********************************************************
508 -- function composition
510 (.) :: (b -> c) -> (a -> b) -> a -> c
513 -- flip f takes its (first) two arguments in the reverse order of f.
514 flip :: (a -> b -> c) -> b -> a -> c
517 -- right-associating infix application operator (useful in continuation-
519 ($) :: (a -> b) -> a -> b
522 -- until p f yields the result of applying f until p holds.
523 until :: (a -> Bool) -> (a -> a) -> a -> a
524 until p f x | p x = x
525 | otherwise = until p f (f x)
527 -- asTypeOf is a type-restricted version of const. It is usually used
528 -- as an infix operator, and its typing forces its first argument
529 -- (which is usually overloaded) to have the same type as the second.
530 asTypeOf :: a -> a -> a
534 %*********************************************************
536 \subsection{CCallable instances}
538 %*********************************************************
540 Defined here to avoid orphans
543 instance CCallable Char
544 instance CReturnable Char
546 instance CCallable Int
547 instance CReturnable Int
549 instance CReturnable () -- Why, exactly?
553 %*********************************************************
555 \subsection{Numeric primops}
557 %*********************************************************
559 Definitions of the boxed PrimOps; these will be
560 used in the case of partial applications, etc.
569 {-# INLINE plusInt #-}
570 {-# INLINE minusInt #-}
571 {-# INLINE timesInt #-}
572 {-# INLINE quotInt #-}
573 {-# INLINE remInt #-}
574 {-# INLINE negateInt #-}
576 plusInt, minusInt, timesInt, quotInt, remInt, gcdInt :: Int -> Int -> Int
577 plusInt (I# x) (I# y) = I# (x +# y)
578 minusInt(I# x) (I# y) = I# (x -# y)
579 timesInt(I# x) (I# y) = I# (x *# y)
580 quotInt (I# x) (I# y) = I# (quotInt# x y)
581 remInt (I# x) (I# y) = I# (remInt# x y)
583 gcdInt (I# a) (I# b) = g a b
584 where g 0# 0# = error "PrelBase.gcdInt: gcd 0 0 is undefined"
587 g _ _ = I# (gcdInt# absA absB)
589 absInt x = if x <# 0# then negateInt# x else x
594 negateInt :: Int -> Int
595 negateInt (I# x) = I# (negateInt# x)
597 divInt, modInt :: Int -> Int -> Int
599 | x > zeroInt && y < zeroInt = quotInt ((x `minusInt` y) `minusInt` oneInt) y
600 | x < zeroInt && y > zeroInt = quotInt ((x `minusInt` y) `plusInt` oneInt) y
601 | otherwise = quotInt x y
604 | x > zeroInt && y < zeroInt ||
605 x < zeroInt && y > zeroInt = if r/=zeroInt then r `plusInt` y else zeroInt
610 gtInt, geInt, eqInt, neInt, ltInt, leInt :: Int -> Int -> Bool
611 gtInt (I# x) (I# y) = x ># y
612 geInt (I# x) (I# y) = x >=# y
613 eqInt (I# x) (I# y) = x ==# y
614 neInt (I# x) (I# y) = x /=# y
615 ltInt (I# x) (I# y) = x <# y
616 leInt (I# x) (I# y) = x <=# y
620 %********************************************************
622 \subsection{Unpacking C strings}
624 %********************************************************
626 This code is needed for virtually all programs, since it's used for
627 unpacking the strings of error messages.
630 unpackCString# :: Addr# -> [Char]
631 unpackCString# a = unpackCStringList# a
633 unpackCStringList# :: Addr# -> [Char]
634 unpackCStringList# addr
638 | ch `eqChar#` '\0'# = []
639 | otherwise = C# ch : unpack (nh +# 1#)
641 ch = indexCharOffAddr# addr nh
643 unpackAppendCString# :: Addr# -> [Char] -> [Char]
644 unpackAppendCString# addr rest
648 | ch `eqChar#` '\0'# = rest
649 | otherwise = C# ch : unpack (nh +# 1#)
651 ch = indexCharOffAddr# addr nh
653 unpackFoldrCString# :: Addr# -> (Char -> a -> a) -> a -> a
654 unpackFoldrCString# addr f z
658 | ch `eqChar#` '\0'# = z
659 | otherwise = C# ch `f` unpack (nh +# 1#)
661 ch = indexCharOffAddr# addr nh
663 unpackCStringUtf8# :: Addr# -> [Char]
664 unpackCStringUtf8# addr
668 | ch `eqChar#` '\0'# = []
669 | ch `leChar#` '\x7F'# = C# ch : unpack (nh +# 1#)
670 | ch `leChar#` '\xDF'# = C# (chr# ((ord# ch `iShiftL#` 6#) +#
671 (ord# (indexCharOffAddr# addr (nh +# 1#))) -# 0x3080#))
673 | ch `leChar#` '\xEF'# = C# (chr# ((ord# ch `iShiftL#` 12#) +#
674 (ord# (indexCharOffAddr# addr (nh +# 1#)) `iShiftL#` 6#) +#
675 (ord# (indexCharOffAddr# addr (nh +# 2#))) -# 0xE2080#))
677 | ch `leChar#` '\xF7'# = C# (chr# ((ord# ch `iShiftL#` 18#) +#
678 (ord# (indexCharOffAddr# addr (nh +# 1#)) `iShiftL#` 12#) +#
679 (ord# (indexCharOffAddr# addr (nh +# 2#)) `iShiftL#` 6#) +#
680 (ord# (indexCharOffAddr# addr (nh +# 3#))) -# 0x3C82080#))
682 | ch `leChar#` '\xFB'# = C# (chr# ((ord# ch `iShiftL#` 24#) +#
683 (ord# (indexCharOffAddr# addr (nh +# 1#)) `iShiftL#` 18#) +#
684 (ord# (indexCharOffAddr# addr (nh +# 2#)) `iShiftL#` 12#) +#
685 (ord# (indexCharOffAddr# addr (nh +# 3#)) `iShiftL#` 6#) +#
686 (ord# (indexCharOffAddr# addr (nh +# 4#))) -# 0xFA082080#))
688 | otherwise = C# (chr# (((ord# ch -# 0xFC#) `iShiftL#` 30#) +#
689 (ord# (indexCharOffAddr# addr (nh +# 1#)) `iShiftL#` 24#) +#
690 (ord# (indexCharOffAddr# addr (nh +# 2#)) `iShiftL#` 18#) +#
691 (ord# (indexCharOffAddr# addr (nh +# 3#)) `iShiftL#` 12#) +#
692 (ord# (indexCharOffAddr# addr (nh +# 4#)) `iShiftL#` 6#) +#
693 (ord# (indexCharOffAddr# addr (nh +# 5#))) -# 0x82082080#))
696 ch = indexCharOffAddr# addr nh
698 unpackNBytes# :: Addr# -> Int# -> [Char]
699 unpackNBytes# _addr 0# = []
700 unpackNBytes# addr len# = unpack [] (len# -# 1#)
705 case indexCharOffAddr# addr i# of
706 ch -> unpack (C# ch : acc) (i# -# 1#)
709 "unpack" forall a . unpackCString# a = build (unpackFoldrCString# a)
710 "unpack-list" forall a . unpackFoldrCString# a (:) [] = unpackCStringList# a
711 "unpack-append" forall a n . unpackFoldrCString# a (:) n = unpackAppendCString# a n
713 -- There's a built-in rule (in PrelRules.lhs) for
714 -- unpackFoldr "foo" c (unpackFoldr "baz" c n) = unpackFoldr "foobaz" c n