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#
388 %*********************************************************
390 \subsection{The function type}
392 %*********************************************************
403 -- function composition
405 (.) :: (b -> c) -> (a -> b) -> a -> c
408 -- flip f takes its (first) two arguments in the reverse order of f.
409 flip :: (a -> b -> c) -> b -> a -> c
412 -- right-associating infix application operator (useful in continuation-
414 ($) :: (a -> b) -> a -> b
417 -- until p f yields the result of applying f until p holds.
418 until :: (a -> Bool) -> (a -> a) -> a -> a
419 until p f x | p x = x
420 | otherwise = until p f (f x)
422 -- asTypeOf is a type-restricted version of const. It is usually used
423 -- as an infix operator, and its typing forces its first argument
424 -- (which is usually overloaded) to have the same type as the second.
425 asTypeOf :: a -> a -> a
429 %*********************************************************
431 \subsection{Numeric primops}
433 %*********************************************************
435 Definitions of the boxed PrimOps; these will be
436 used in the case of partial applications, etc.
445 {-# INLINE plusInt #-}
446 {-# INLINE minusInt #-}
447 {-# INLINE timesInt #-}
448 {-# INLINE quotInt #-}
449 {-# INLINE remInt #-}
450 {-# INLINE negateInt #-}
452 plusInt, minusInt, timesInt, quotInt, remInt :: Int -> Int -> Int
453 plusInt (I# x) (I# y) = I# (x +# y)
454 minusInt(I# x) (I# y) = I# (x -# y)
455 timesInt(I# x) (I# y) = I# (x *# y)
456 quotInt (I# x) (I# y) = I# (quotInt# x y)
457 remInt (I# x) (I# y) = I# (remInt# x y)
459 negateInt :: Int -> Int
460 negateInt (I# x) = I# (negateInt# x)
462 gtInt, geInt, eqInt, neInt, ltInt, leInt :: Int -> Int -> Bool
463 gtInt (I# x) (I# y) = x ># y
464 geInt (I# x) (I# y) = x >=# y
465 eqInt (I# x) (I# y) = x ==# y
466 neInt (I# x) (I# y) = x /=# y
467 ltInt (I# x) (I# y) = x <# y
468 leInt (I# x) (I# y) = x <=# y
471 Convenient boxed Integer PrimOps. These are 'thin-air' Ids, so
472 it's nice to have them in PrelBase.
475 {-# INLINE int2Integer #-}
476 {-# INLINE addr2Integer #-}
477 int2Integer :: Int# -> Integer
479 addr2Integer :: Addr# -> Integer
480 addr2Integer x = case addr2Integer# x of (# s, d #) -> J# s d