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@, @Bounded@
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.
56 x <= y = case compare x y of { GT -> False; other -> True }
57 x < y = case compare x y of { LT -> True; other -> False }
58 x >= y = case compare x y of { LT -> False; other -> True }
59 x > y = case compare x y of { GT -> True; other -> False }
61 -- These two default methods use '>' rather than compare
62 -- because the latter is often more expensive
63 max x y = if x > y then x else y
64 min x y = if x > y then y else x
67 %*********************************************************
69 \subsection{Monadic classes @Functor@, @Monad@ }
71 %*********************************************************
75 fmap :: (a -> b) -> f a -> f b
78 (>>=) :: m a -> (a -> m b) -> m b
79 (>>) :: m a -> m b -> m b
83 m >> k = m >>= \_ -> k
89 %*********************************************************
91 \subsection{The list type}
93 %*********************************************************
96 data [] a = [] | a : [a] -- do explicitly: deriving (Eq, Ord)
97 -- to avoid weird names like con2tag_[]#
99 instance (Eq a) => Eq [a] where
101 (x:xs) == (y:ys) = x == y && xs == ys
104 xs /= ys = if (xs == ys) then False else True
106 instance (Ord a) => Ord [a] where
107 a < b = case compare a b of { LT -> True; EQ -> False; GT -> False }
108 a <= b = case compare a b of { LT -> True; EQ -> True; GT -> False }
109 a >= b = case compare a b of { LT -> False; EQ -> True; GT -> True }
110 a > b = case compare a b of { LT -> False; EQ -> False; GT -> True }
113 compare (_:_) [] = GT
114 compare [] (_:_) = LT
115 compare (x:xs) (y:ys) = case compare x y of
120 instance Functor [] where
123 instance Monad [] where
124 m >>= k = foldr ((++) . k) [] m
125 m >> k = foldr ((++) . (\ _ -> k)) [] m
130 A few list functions that appear here because they are used here.
131 The rest of the prelude list functions are in PrelList.
133 ----------------------------------------------
134 -- foldr/build/augment
135 ----------------------------------------------
138 foldr :: (a -> b -> b) -> b -> [a] -> b
140 foldr f z (x:xs) = f x (foldr f z xs)
142 build :: forall a. (forall b. (a -> b -> b) -> b -> b) -> [a]
144 -- The INLINE is important, even though build is tiny,
145 -- because it prevents [] getting inlined in the version that
146 -- appears in the interface file. If [] *is* inlined, it
147 -- won't match with [] appearing in rules in an importing module.
150 augment :: forall a. (forall b. (a->b->b) -> b -> b) -> [a] -> [a]
151 {-# INLINE augment #-}
152 augment g xs = g (:) xs
155 "fold/build" forall k,z,g::forall b. (a->b->b) -> b -> b .
156 foldr k z (build g) = g k z
158 "foldr/augment" forall k,z,xs,g::forall b. (a->b->b) -> b -> b .
159 foldr k z (augment g xs) = g k (foldr k z xs)
161 "foldr/id" foldr (:) [] = \x->x
162 "foldr/app" forall xs, ys. foldr (:) ys xs = append xs ys
164 "foldr/cons" forall k,z,x,xs. foldr k z (x:xs) = k x (foldr k z xs)
165 "foldr/nil" forall k,z. foldr k z [] = z
170 ----------------------------------------------
172 ----------------------------------------------
175 map :: (a -> b) -> [a] -> [b]
177 map f xs = build (\c n -> foldr (mapFB c f) n xs)
179 mapFB c f xs = c (f xs)
181 mapList :: (a -> b) -> [a] -> [b]
183 mapList f (x:xs) = f x : mapList f xs
186 "mapFB" forall c,f,g. mapFB (mapFB c f) g = mapFB c (f.g)
187 "mapList" forall f. foldr (mapFB (:) f) [] = mapList f
192 ----------------------------------------------
194 ----------------------------------------------
196 (++) :: [a] -> [a] -> [a]
198 xs ++ ys = augment (\c n -> foldr c n xs) ys
200 append :: [a] -> [a] -> [a]
202 append (x:xs) ys = x : append xs ys
206 %*********************************************************
208 \subsection{Type @Bool@}
210 %*********************************************************
213 data Bool = False | True deriving (Eq, Ord)
214 -- Read in PrelRead, Show in PrelShow
218 (&&), (||) :: Bool -> Bool -> Bool
233 %*********************************************************
235 \subsection{The @()@ type}
237 %*********************************************************
239 The Unit type is here because virtually any program needs it (whereas
240 some programs may get away without consulting PrelTup). Furthermore,
241 the renamer currently *always* asks for () to be in scope, so that
242 ccalls can use () as their default type; so when compiling PrelBase we
243 need (). (We could arrange suck in () only if -fglasgow-exts, but putting
244 it here seems more direct.
253 instance Ord () where
264 %*********************************************************
266 \subsection{Type @Ordering@}
268 %*********************************************************
271 data Ordering = LT | EQ | GT deriving (Eq, Ord)
272 -- Read in PrelRead, Show in PrelShow
276 %*********************************************************
278 \subsection{Type @Char@ and @String@}
280 %*********************************************************
285 data Char = C# Char# deriving (Eq, Ord)
288 chr (I# i) | i >=# 0# && i <=# 255# = C# (chr# i)
289 | otherwise = error ("Prelude.chr: bad argument")
291 unsafeChr :: Int -> Char
292 unsafeChr (I# i) = C# (chr# i)
295 ord (C# c) = I# (ord# c)
299 %*********************************************************
301 \subsection{Type @Int@}
303 %*********************************************************
308 zeroInt, oneInt, twoInt, maxInt, minInt :: Int
312 minInt = I# (-2147483648#) -- GHC <= 2.09 had this at -2147483647
313 maxInt = I# 2147483647#
315 instance Eq Int where
316 (==) x y = x `eqInt` y
317 (/=) x y = x `neInt` y
319 instance Ord Int where
320 compare x y = compareInt x y
327 compareInt :: Int -> Int -> Ordering
328 (I# x) `compareInt` (I# y) | x <# y = LT
334 %*********************************************************
336 \subsection{Type @Integer@, @Float@, @Double@}
338 %*********************************************************
341 data Float = F# Float#
342 data Double = D# Double#
345 = S# Int# -- small integers
346 | J# Int# ByteArray# -- large integers
348 instance Eq Integer where
349 (S# i) == (S# j) = i ==# j
350 (S# i) == (J# s d) = cmpIntegerInt# s d i ==# 0#
351 (J# s d) == (S# i) = cmpIntegerInt# s d i ==# 0#
352 (J# s1 d1) == (J# s2 d2) = (cmpInteger# s1 d1 s2 d2) ==# 0#
354 (S# i) /= (S# j) = i /=# j
355 (S# i) /= (J# s d) = cmpIntegerInt# s d i /=# 0#
356 (J# s d) /= (S# i) = cmpIntegerInt# s d i /=# 0#
357 (J# s1 d1) /= (J# s2 d2) = (cmpInteger# s1 d1 s2 d2) /=# 0#
361 %*********************************************************
363 \subsection{The function type}
365 %*********************************************************
376 -- function composition
378 (.) :: (b -> c) -> (a -> b) -> a -> c
381 -- flip f takes its (first) two arguments in the reverse order of f.
382 flip :: (a -> b -> c) -> b -> a -> c
385 -- right-associating infix application operator (useful in continuation-
387 ($) :: (a -> b) -> a -> b
390 -- until p f yields the result of applying f until p holds.
391 until :: (a -> Bool) -> (a -> a) -> a -> a
392 until p f x | p x = x
393 | otherwise = until p f (f x)
395 -- asTypeOf is a type-restricted version of const. It is usually used
396 -- as an infix operator, and its typing forces its first argument
397 -- (which is usually overloaded) to have the same type as the second.
398 asTypeOf :: a -> a -> a
402 %*********************************************************
404 \subsection{Numeric primops}
406 %*********************************************************
408 Definitions of the boxed PrimOps; these will be
409 used in the case of partial applications, etc.
418 {-# INLINE plusInt #-}
419 {-# INLINE minusInt #-}
420 {-# INLINE timesInt #-}
421 {-# INLINE quotInt #-}
422 {-# INLINE remInt #-}
423 {-# INLINE negateInt #-}
425 plusInt, minusInt, timesInt, quotInt, remInt :: Int -> Int -> Int
426 plusInt (I# x) (I# y) = I# (x +# y)
427 minusInt(I# x) (I# y) = I# (x -# y)
428 timesInt(I# x) (I# y) = I# (x *# y)
429 quotInt (I# x) (I# y) = I# (quotInt# x y)
430 remInt (I# x) (I# y) = I# (remInt# x y)
432 negateInt :: Int -> Int
433 negateInt (I# x) = I# (negateInt# x)
435 gtInt, geInt, eqInt, neInt, ltInt, leInt :: Int -> Int -> Bool
436 gtInt (I# x) (I# y) = x ># y
437 geInt (I# x) (I# y) = x >=# y
438 eqInt (I# x) (I# y) = x ==# y
439 neInt (I# x) (I# y) = x /=# y
440 ltInt (I# x) (I# y) = x <# y
441 leInt (I# x) (I# y) = x <=# y
444 Convenient boxed Integer PrimOps. These are 'thin-air' Ids, so
445 it's nice to have them in PrelBase.
448 {-# INLINE int2Integer #-}
449 {-# INLINE addr2Integer #-}
450 int2Integer :: Int# -> Integer
452 addr2Integer :: Addr# -> Integer
453 addr2Integer x = case addr2Integer# x of (# s, d #) -> J# s d