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
103 (x:xs) == (y:ys) = x == y && xs == ys
106 xs /= ys = if (xs == ys) then False else True
108 instance (Ord a) => Ord [a] where
109 a < b = case compare a b of { LT -> True; EQ -> False; GT -> False }
110 a <= b = case compare a b of { LT -> True; EQ -> True; GT -> False }
111 a >= b = case compare a b of { LT -> False; EQ -> True; GT -> True }
112 a > b = case compare a b of { LT -> False; EQ -> False; GT -> True }
115 compare (_:_) [] = GT
116 compare [] (_:_) = LT
117 compare (x:xs) (y:ys) = case compare x y of
122 instance Functor [] where
125 instance Monad [] where
126 m >>= k = foldr ((++) . k) [] m
127 m >> k = foldr ((++) . (\ _ -> k)) [] m
132 A few list functions that appear here because they are used here.
133 The rest of the prelude list functions are in PrelList.
135 ----------------------------------------------
136 -- foldr/build/augment
137 ----------------------------------------------
140 foldr :: (a -> b -> b) -> b -> [a] -> b
142 -- foldr f z (x:xs) = f x (foldr f z xs)
147 go (x:xs) = x `k` go xs
149 build :: forall a. (forall b. (a -> b -> b) -> b -> b) -> [a]
151 -- The INLINE is important, even though build is tiny,
152 -- because it prevents [] getting inlined in the version that
153 -- appears in the interface file. If [] *is* inlined, it
154 -- won't match with [] appearing in rules in an importing module.
157 augment :: forall a. (forall b. (a->b->b) -> b -> b) -> [a] -> [a]
158 {-# INLINE augment #-}
159 augment g xs = g (:) xs
162 "fold/build" forall k,z,g::forall b. (a->b->b) -> b -> b .
163 foldr k z (build g) = g k z
165 "foldr/augment" forall k,z,xs,g::forall b. (a->b->b) -> b -> b .
166 foldr k z (augment g xs) = g k (foldr k z xs)
168 "foldr/id" foldr (:) [] = \x->x
169 "foldr/app" forall xs, ys. foldr (:) ys xs = append xs ys
171 "foldr/cons" forall k,z,x,xs. foldr k z (x:xs) = k x (foldr k z xs)
172 "foldr/nil" forall k,z. foldr k z [] = z
177 ----------------------------------------------
179 ----------------------------------------------
182 map :: (a -> b) -> [a] -> [b]
184 map f xs = build (\c n -> foldr (mapFB c f) n xs)
187 mapFB c f x ys = c (f x) ys
189 mapList :: (a -> b) -> [a] -> [b]
191 mapList f (x:xs) = f x : mapList f xs
194 "mapFB" forall c,f,g. mapFB (mapFB c f) g = mapFB c (f.g)
195 "mapList" forall f. foldr (mapFB (:) f) [] = mapList f
200 ----------------------------------------------
202 ----------------------------------------------
204 (++) :: [a] -> [a] -> [a]
206 xs ++ ys = augment (\c n -> foldr c n xs) ys
208 append :: [a] -> [a] -> [a]
210 append (x:xs) ys = x : append xs ys
214 %*********************************************************
216 \subsection{Type @Bool@}
218 %*********************************************************
221 data Bool = False | True deriving (Eq, Ord)
222 -- Read in PrelRead, Show in PrelShow
226 (&&), (||) :: Bool -> Bool -> Bool
241 %*********************************************************
243 \subsection{The @()@ type}
245 %*********************************************************
247 The Unit type is here because virtually any program needs it (whereas
248 some programs may get away without consulting PrelTup). Furthermore,
249 the renamer currently *always* asks for () to be in scope, so that
250 ccalls can use () as their default type; so when compiling PrelBase we
251 need (). (We could arrange suck in () only if -fglasgow-exts, but putting
252 it here seems more direct.)
261 instance Ord () where
272 %*********************************************************
274 \subsection{Type @Ordering@}
276 %*********************************************************
279 data Ordering = LT | EQ | GT deriving (Eq, Ord)
280 -- Read in PrelRead, Show in PrelShow
284 %*********************************************************
286 \subsection{Type @Char@ and @String@}
288 %*********************************************************
295 -- We don't use deriving for Eq and Ord, because for Ord the derived
296 -- instance defines only compare, which takes two primops. Then
297 -- '>' uses compare, and therefore takes two primops instead of one.
299 instance Eq Char where
300 (C# c1) == (C# c2) = c1 `eqChar#` c2
301 (C# c1) /= (C# c2) = c1 `neChar#` c2
303 instance Ord Char where
304 (C# c1) > (C# c2) = c1 `gtChar#` c2
305 (C# c1) >= (C# c2) = c1 `geChar#` c2
306 (C# c1) <= (C# c2) = c1 `leChar#` c2
307 (C# c1) < (C# c2) = c1 `ltChar#` c2
310 chr (I# i) | i >=# 0# && i <=# 255# = C# (chr# i)
311 | otherwise = error ("Prelude.chr: bad argument")
313 unsafeChr :: Int -> Char
314 unsafeChr (I# i) = C# (chr# i)
317 ord (C# c) = I# (ord# c)
321 %*********************************************************
323 \subsection{Type @Int@}
325 %*********************************************************
330 zeroInt, oneInt, twoInt, maxInt, minInt :: Int
334 minInt = I# (-2147483648#) -- GHC <= 2.09 had this at -2147483647
335 maxInt = I# 2147483647#
337 instance Eq Int where
338 (==) x y = x `eqInt` y
339 (/=) x y = x `neInt` y
341 instance Ord Int where
342 compare x y = compareInt x y
349 compareInt :: Int -> Int -> Ordering
350 (I# x) `compareInt` (I# y) | x <# y = LT
356 %*********************************************************
358 \subsection{Type @Integer@, @Float@, @Double@}
360 %*********************************************************
363 data Float = F# Float#
364 data Double = D# Double#
367 = S# Int# -- small integers
368 | J# Int# ByteArray# -- large integers
370 instance Eq Integer where
371 (S# i) == (S# j) = i ==# j
372 (S# i) == (J# s d) = cmpIntegerInt# s d i ==# 0#
373 (J# s d) == (S# i) = cmpIntegerInt# s d i ==# 0#
374 (J# s1 d1) == (J# s2 d2) = (cmpInteger# s1 d1 s2 d2) ==# 0#
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#
383 %*********************************************************
385 \subsection{The function type}
387 %*********************************************************
398 -- function composition
400 (.) :: (b -> c) -> (a -> b) -> a -> c
403 -- flip f takes its (first) two arguments in the reverse order of f.
404 flip :: (a -> b -> c) -> b -> a -> c
407 -- right-associating infix application operator (useful in continuation-
409 ($) :: (a -> b) -> a -> b
412 -- until p f yields the result of applying f until p holds.
413 until :: (a -> Bool) -> (a -> a) -> a -> a
414 until p f x | p x = x
415 | otherwise = until p f (f x)
417 -- asTypeOf is a type-restricted version of const. It is usually used
418 -- as an infix operator, and its typing forces its first argument
419 -- (which is usually overloaded) to have the same type as the second.
420 asTypeOf :: a -> a -> a
424 %*********************************************************
426 \subsection{Numeric primops}
428 %*********************************************************
430 Definitions of the boxed PrimOps; these will be
431 used in the case of partial applications, etc.
440 {-# INLINE plusInt #-}
441 {-# INLINE minusInt #-}
442 {-# INLINE timesInt #-}
443 {-# INLINE quotInt #-}
444 {-# INLINE remInt #-}
445 {-# INLINE negateInt #-}
447 plusInt, minusInt, timesInt, quotInt, remInt :: Int -> Int -> Int
448 plusInt (I# x) (I# y) = I# (x +# y)
449 minusInt(I# x) (I# y) = I# (x -# y)
450 timesInt(I# x) (I# y) = I# (x *# y)
451 quotInt (I# x) (I# y) = I# (quotInt# x y)
452 remInt (I# x) (I# y) = I# (remInt# x y)
454 negateInt :: Int -> Int
455 negateInt (I# x) = I# (negateInt# x)
457 gtInt, geInt, eqInt, neInt, ltInt, leInt :: Int -> Int -> Bool
458 gtInt (I# x) (I# y) = x ># y
459 geInt (I# x) (I# y) = x >=# y
460 eqInt (I# x) (I# y) = x ==# y
461 neInt (I# x) (I# y) = x /=# y
462 ltInt (I# x) (I# y) = x <# y
463 leInt (I# x) (I# y) = x <=# y
466 Convenient boxed Integer PrimOps. These are 'thin-air' Ids, so
467 it's nice to have them in PrelBase.
470 {-# INLINE int2Integer #-}
471 {-# INLINE addr2Integer #-}
472 int2Integer :: Int# -> Integer
474 addr2Integer :: Addr# -> Integer
475 addr2Integer x = case addr2Integer# x of (# s, d #) -> J# s d