2 % (c) The GRAP/AQUA Project, Glasgow University, 1992-1996
4 \section[PrelBase]{Module @PrelBase@}
8 {-# OPTIONS -fno-implicit-prelude #-}
13 module PrelGHC -- Re-export PrelGHC, to avoid lots of people
14 -- having to import it explicitly
18 import {-# SOURCE #-} PrelErr ( error )
23 infix 4 ==, /=, <, <=, >=, >
31 %*********************************************************
33 \subsection{Standard classes @Eq@, @Ord@}
35 %*********************************************************
39 (==), (/=) :: a -> a -> Bool
44 class (Eq a) => Ord a where
45 compare :: a -> a -> Ordering
46 (<), (<=), (>=), (>):: a -> a -> Bool
47 max, min :: a -> a -> a
49 -- An instance of Ord should define either compare or <=
50 -- Using compare can be more efficient for complex types.
53 | x <= y = LT -- NB: must be '<=' not '<' to validate the
54 -- above claim about the minimal things that can
55 -- be defined for an instance of Ord
58 x <= y = case compare x y of { GT -> False; _other -> True }
59 x < y = case compare x y of { LT -> True; _other -> False }
60 x >= y = case compare x y of { LT -> False; _other -> True }
61 x > y = case compare x y of { GT -> True; _other -> False }
63 -- These two default methods use '>' rather than compare
64 -- because the latter is often more expensive
65 max x y = if x > y then x else y
66 min x y = if x > y then y else x
69 %*********************************************************
71 \subsection{Monadic classes @Functor@, @Monad@ }
73 %*********************************************************
77 fmap :: (a -> b) -> f a -> f b
80 (>>=) :: m a -> (a -> m b) -> m b
81 (>>) :: m a -> m b -> m b
85 m >> k = m >>= \_ -> k
91 %*********************************************************
93 \subsection{The list type}
95 %*********************************************************
98 data [] a = [] | a : [a] -- do explicitly: deriving (Eq, Ord)
99 -- to avoid weird names like con2tag_[]#
101 instance (Eq a) => Eq [a] where
102 {-# SPECIALISE instance Eq [Char] #-}
104 (x:xs) == (y:ys) = x == y && xs == ys
107 xs /= ys = if (xs == ys) then False else True
109 instance (Ord a) => Ord [a] where
110 {-# SPECIALISE instance Ord [Char] #-}
111 a < b = case compare a b of { LT -> True; EQ -> False; GT -> False }
112 a <= b = case compare a b of { LT -> True; EQ -> True; GT -> False }
113 a >= b = case compare a b of { LT -> False; EQ -> True; GT -> True }
114 a > b = case compare a b of { LT -> False; EQ -> False; GT -> True }
117 compare (_:_) [] = GT
118 compare [] (_:_) = LT
119 compare (x:xs) (y:ys) = case compare x y of
124 instance Functor [] where
127 instance Monad [] where
128 m >>= k = foldr ((++) . k) [] m
129 m >> k = foldr ((++) . (\ _ -> k)) [] m
134 A few list functions that appear here because they are used here.
135 The rest of the prelude list functions are in PrelList.
137 ----------------------------------------------
138 -- foldr/build/augment
139 ----------------------------------------------
142 foldr :: (a -> b -> b) -> b -> [a] -> b
144 -- foldr f z (x:xs) = f x (foldr f z xs)
149 go (x:xs) = x `k` go xs
151 build :: forall a. (forall b. (a -> b -> b) -> b -> b) -> [a]
152 {-# INLINE 2 build #-}
153 -- The INLINE is important, even though build is tiny,
154 -- because it prevents [] getting inlined in the version that
155 -- appears in the interface file. If [] *is* inlined, it
156 -- won't match with [] appearing in rules in an importing module.
158 -- The "2" says to inline in phase 2
162 augment :: forall a. (forall b. (a->b->b) -> b -> b) -> [a] -> [a]
163 {-# INLINE 2 augment #-}
164 augment g xs = g (:) xs
167 "fold/build" forall k z (g::forall b. (a->b->b) -> b -> b) .
168 foldr k z (build g) = g k z
170 "foldr/augment" forall k z xs (g::forall b. (a->b->b) -> b -> b) .
171 foldr k z (augment g xs) = g k (foldr k z xs)
173 "foldr/id" foldr (:) [] = \x->x
174 "foldr/app" forall xs ys. foldr (:) ys xs = append xs ys
176 "foldr/cons" forall k z x xs. foldr k z (x:xs) = k x (foldr k z xs)
177 "foldr/nil" forall k z. foldr k z [] = z
182 ----------------------------------------------
184 ----------------------------------------------
187 map :: (a -> b) -> [a] -> [b]
189 map f xs = build (\c n -> foldr (mapFB c f) n xs)
192 mapFB c f x ys = c (f x) ys
194 mapList :: (a -> b) -> [a] -> [b]
196 mapList f (x:xs) = f x : mapList f xs
199 "mapFB" forall c f g. mapFB (mapFB c f) g = mapFB c (f.g)
200 "mapList" forall f. foldr (mapFB (:) f) [] = mapList f
205 ----------------------------------------------
207 ----------------------------------------------
209 (++) :: [a] -> [a] -> [a]
211 xs ++ ys = augment (\c n -> foldr c n xs) ys
213 append :: [a] -> [a] -> [a]
215 append (x:xs) ys = x : append xs ys
219 %*********************************************************
221 \subsection{Type @Bool@}
223 %*********************************************************
226 data Bool = False | True deriving (Eq, Ord)
227 -- Read in PrelRead, Show in PrelShow
231 (&&), (||) :: Bool -> Bool -> Bool
246 %*********************************************************
248 \subsection{The @()@ type}
250 %*********************************************************
252 The Unit type is here because virtually any program needs it (whereas
253 some programs may get away without consulting PrelTup). Furthermore,
254 the renamer currently *always* asks for () to be in scope, so that
255 ccalls can use () as their default type; so when compiling PrelBase we
256 need (). (We could arrange suck in () only if -fglasgow-exts, but putting
257 it here seems more direct.)
266 instance Ord () where
277 %*********************************************************
279 \subsection{Type @Ordering@}
281 %*********************************************************
284 data Ordering = LT | EQ | GT deriving (Eq, Ord)
285 -- Read in PrelRead, Show in PrelShow
289 %*********************************************************
291 \subsection{Type @Char@ and @String@}
293 %*********************************************************
300 -- We don't use deriving for Eq and Ord, because for Ord the derived
301 -- instance defines only compare, which takes two primops. Then
302 -- '>' uses compare, and therefore takes two primops instead of one.
304 instance Eq Char where
305 (C# c1) == (C# c2) = c1 `eqChar#` c2
306 (C# c1) /= (C# c2) = c1 `neChar#` c2
308 instance Ord Char where
309 (C# c1) > (C# c2) = c1 `gtChar#` c2
310 (C# c1) >= (C# c2) = c1 `geChar#` c2
311 (C# c1) <= (C# c2) = c1 `leChar#` c2
312 (C# c1) < (C# c2) = c1 `ltChar#` c2
315 chr (I# i) | i >=# 0# && i <=# 255# = C# (chr# i)
316 | otherwise = error ("Prelude.chr: bad argument")
318 unsafeChr :: Int -> Char
319 unsafeChr (I# i) = C# (chr# i)
322 ord (C# c) = I# (ord# c)
326 %*********************************************************
328 \subsection{Type @Int@}
330 %*********************************************************
335 zeroInt, oneInt, twoInt, maxInt, minInt :: Int
339 minInt = I# (-2147483648#) -- GHC <= 2.09 had this at -2147483647
340 maxInt = I# 2147483647#
342 instance Eq Int where
343 (==) x y = x `eqInt` y
344 (/=) x y = x `neInt` y
346 instance Ord Int where
347 compare x y = compareInt x y
354 compareInt :: Int -> Int -> Ordering
355 (I# x) `compareInt` (I# y) | x <# y = LT
361 %*********************************************************
363 \subsection{Type @Integer@, @Float@, @Double@}
365 %*********************************************************
368 data Float = F# Float#
369 data Double = D# Double#
372 = S# Int# -- small integers
373 | J# Int# ByteArray# -- large integers
375 instance Eq Integer where
376 (S# i) == (S# j) = i ==# j
377 (S# i) == (J# s d) = cmpIntegerInt# s d i ==# 0#
378 (J# s d) == (S# i) = cmpIntegerInt# s d i ==# 0#
379 (J# s1 d1) == (J# s2 d2) = (cmpInteger# s1 d1 s2 d2) ==# 0#
381 (S# i) /= (S# j) = i /=# j
382 (S# i) /= (J# s d) = cmpIntegerInt# s d i /=# 0#
383 (J# s d) /= (S# i) = cmpIntegerInt# s d i /=# 0#
384 (J# s1 d1) /= (J# s2 d2) = (cmpInteger# s1 d1 s2 d2) /=# 0#
386 instance Ord Integer where
387 (S# i) <= (S# j) = i <=# j
388 (J# s d) <= (S# i) = cmpIntegerInt# s d i <=# 0#
389 (S# i) <= (J# s d) = cmpIntegerInt# s d i >=# 0#
390 (J# s1 d1) <= (J# s2 d2) = (cmpInteger# s1 d1 s2 d2) <=# 0#
392 (S# i) > (S# j) = i ># j
393 (J# s d) > (S# i) = cmpIntegerInt# s d i ># 0#
394 (S# i) > (J# s d) = cmpIntegerInt# s d i <# 0#
395 (J# s1 d1) > (J# s2 d2) = (cmpInteger# s1 d1 s2 d2) ># 0#
397 (S# i) < (S# j) = i <# j
398 (J# s d) < (S# i) = cmpIntegerInt# s d i <# 0#
399 (S# i) < (J# s d) = cmpIntegerInt# s d i ># 0#
400 (J# s1 d1) < (J# s2 d2) = (cmpInteger# s1 d1 s2 d2) <# 0#
402 (S# i) >= (S# j) = i >=# j
403 (J# s d) >= (S# i) = cmpIntegerInt# s d i >=# 0#
404 (S# i) >= (J# s d) = cmpIntegerInt# s d i <=# 0#
405 (J# s1 d1) >= (J# s2 d2) = (cmpInteger# s1 d1 s2 d2) >=# 0#
407 compare (S# i) (S# j)
411 compare (J# s d) (S# i)
412 = case cmpIntegerInt# s d i of { res# ->
413 if res# <# 0# then LT else
414 if res# ># 0# then GT else EQ
416 compare (S# i) (J# s d)
417 = case cmpIntegerInt# s d i of { res# ->
418 if res# ># 0# then LT else
419 if res# <# 0# then GT else EQ
421 compare (J# s1 d1) (J# s2 d2)
422 = case cmpInteger# s1 d1 s2 d2 of { res# ->
423 if res# <# 0# then LT else
424 if res# ># 0# then GT else EQ
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{Numeric primops}
474 %*********************************************************
476 Definitions of the boxed PrimOps; these will be
477 used in the case of partial applications, etc.
486 {-# INLINE plusInt #-}
487 {-# INLINE minusInt #-}
488 {-# INLINE timesInt #-}
489 {-# INLINE quotInt #-}
490 {-# INLINE remInt #-}
491 {-# INLINE negateInt #-}
493 plusInt, minusInt, timesInt, quotInt, remInt :: Int -> Int -> Int
494 plusInt (I# x) (I# y) = I# (x +# y)
495 minusInt(I# x) (I# y) = I# (x -# y)
496 timesInt(I# x) (I# y) = I# (x *# y)
497 quotInt (I# x) (I# y) = I# (quotInt# x y)
498 remInt (I# x) (I# y) = I# (remInt# x y)
500 negateInt :: Int -> Int
501 negateInt (I# x) = I# (negateInt# x)
503 gtInt, geInt, eqInt, neInt, ltInt, leInt :: Int -> Int -> Bool
504 gtInt (I# x) (I# y) = x ># y
505 geInt (I# x) (I# y) = x >=# y
506 eqInt (I# x) (I# y) = x ==# y
507 neInt (I# x) (I# y) = x /=# y
508 ltInt (I# x) (I# y) = x <# y
509 leInt (I# x) (I# y) = x <=# y
512 Convenient boxed Integer PrimOps. These are 'thin-air' Ids, so
513 it's nice to have them in PrelBase.
516 {-# INLINE int2Integer #-}
517 {-# INLINE addr2Integer #-}
518 int2Integer :: Int# -> Integer
520 addr2Integer :: Addr# -> Integer
521 addr2Integer x = case addr2Integer# x of (# s, d #) -> J# s d