1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
|
%
% (c) The GRAP/AQUA Project, Glasgow University, 1992-1996
%
\section[PrelBase]{Module @PrelBase@}
The overall structure of the GHC Prelude is a bit tricky.
a) We want to avoid "orphan modules", i.e. ones with instance
decls that don't belong either to a tycon or a class
defined in the same module
b) We want to avoid giant modules
So the rough structure is as follows, in (linearised) dependency order
PrelGHC Has no implementation. It defines built-in things, and
by importing it you bring them into scope.
The source file is PrelGHC.hi-boot, which is just
copied to make PrelGHC.hi
Classes: CCallable, CReturnable
PrelBase Classes: Eq, Ord, Functor, Monad
Types: list, (), Int, Bool, Ordering, Char, String
PrelTup Types: tuples, plus instances for PrelBase classes
PrelShow Class: Show, plus instances for PrelBase/PrelTup types
PrelEnum Class: Enum, plus instances for PrelBase/PrelTup types
PrelMaybe Type: Maybe, plus instances for PrelBase classes
PrelNum Class: Num, plus instances for Int
Type: Integer, plus instances for all classes so far (Eq, Ord, Num, Show)
Integer is needed here because it is mentioned in the signature
of 'fromInteger' in class Num
PrelReal Classes: Real, Integral, Fractional, RealFrac
plus instances for Int, Integer
Types: Ratio, Rational
plus intances for classes so far
Rational is needed here because it is mentioned in the signature
of 'toRational' in class Real
Ix Classes: Ix, plus instances for Int, Bool, Char, Integer, Ordering, tuples
PrelArr Types: Array, MutableArray, MutableVar
Does *not* contain any ByteArray stuff (see PrelByteArr)
Arrays are used by a function in PrelFloat
PrelFloat Classes: Floating, RealFloat
Types: Float, Double, plus instances of all classes so far
This module contains everything to do with floating point.
It is a big module (900 lines)
With a bit of luck, many modules can be compiled without ever reading PrelFloat.hi
PrelByteArr Types: ByteArray, MutableByteArray
We want this one to be after PrelFloat, because it defines arrays
of unboxed floats.
Other Prelude modules are much easier with fewer complex dependencies.
\begin{code}
{-# OPTIONS -fno-implicit-prelude #-}
module PrelBase
(
module PrelBase,
module PrelGHC -- Re-export PrelGHC, to avoid lots of people
-- having to import it explicitly
)
where
import {-# SOURCE #-} PrelErr ( error )
import PrelGHC
infixr 9 .
infixr 5 ++, :
infix 4 ==, /=, <, <=, >=, >
infixr 3 &&
infixr 2 ||
infixl 1 >>, >>=
infixr 0 $
default () -- Double isn't available yet
\end{code}
%*********************************************************
%* *
\subsection{Standard classes @Eq@, @Ord@}
%* *
%*********************************************************
\begin{code}
class Eq a where
(==), (/=) :: a -> a -> Bool
x /= y = not (x == y)
x == y = not (x /= y)
class (Eq a) => Ord a where
compare :: a -> a -> Ordering
(<), (<=), (>=), (>):: a -> a -> Bool
max, min :: a -> a -> a
-- An instance of Ord should define either compare or <=
-- Using compare can be more efficient for complex types.
compare x y
| x == y = EQ
| x <= y = LT -- NB: must be '<=' not '<' to validate the
-- above claim about the minimal things that can
-- be defined for an instance of Ord
| otherwise = GT
x <= y = case compare x y of { GT -> False; _other -> True }
x < y = case compare x y of { LT -> True; _other -> False }
x >= y = case compare x y of { LT -> False; _other -> True }
x > y = case compare x y of { GT -> True; _other -> False }
-- These two default methods use '>' rather than compare
-- because the latter is often more expensive
max x y = if x > y then x else y
min x y = if x > y then y else x
\end{code}
%*********************************************************
%* *
\subsection{Monadic classes @Functor@, @Monad@ }
%* *
%*********************************************************
\begin{code}
class Functor f where
fmap :: (a -> b) -> f a -> f b
class Monad m where
(>>=) :: m a -> (a -> m b) -> m b
(>>) :: m a -> m b -> m b
return :: a -> m a
fail :: String -> m a
m >> k = m >>= \_ -> k
fail s = error s
\end{code}
%*********************************************************
%* *
\subsection{The list type}
%* *
%*********************************************************
\begin{code}
data [] a = [] | a : [a] -- do explicitly: deriving (Eq, Ord)
-- to avoid weird names like con2tag_[]#
instance (Eq a) => Eq [a] where
{-# SPECIALISE instance Eq [Char] #-}
[] == [] = True
(x:xs) == (y:ys) = x == y && xs == ys
_xs == _ys = False
xs /= ys = if (xs == ys) then False else True
instance (Ord a) => Ord [a] where
{-# SPECIALISE instance Ord [Char] #-}
a < b = case compare a b of { LT -> True; EQ -> False; GT -> False }
a <= b = case compare a b of { LT -> True; EQ -> True; GT -> False }
a >= b = case compare a b of { LT -> False; EQ -> True; GT -> True }
a > b = case compare a b of { LT -> False; EQ -> False; GT -> True }
compare [] [] = EQ
compare (_:_) [] = GT
compare [] (_:_) = LT
compare (x:xs) (y:ys) = case compare x y of
LT -> LT
GT -> GT
EQ -> compare xs ys
instance Functor [] where
fmap = map
instance Monad [] where
m >>= k = foldr ((++) . k) [] m
m >> k = foldr ((++) . (\ _ -> k)) [] m
return x = [x]
fail _ = []
\end{code}
A few list functions that appear here because they are used here.
The rest of the prelude list functions are in PrelList.
----------------------------------------------
-- foldr/build/augment
----------------------------------------------
\begin{code}
foldr :: (a -> b -> b) -> b -> [a] -> b
-- foldr _ z [] = z
-- foldr f z (x:xs) = f x (foldr f z xs)
{-# INLINE foldr #-}
foldr k z xs = go xs
where
go [] = z
go (x:xs) = x `k` go xs
build :: forall a. (forall b. (a -> b -> b) -> b -> b) -> [a]
{-# INLINE 2 build #-}
-- The INLINE is important, even though build is tiny,
-- because it prevents [] getting inlined in the version that
-- appears in the interface file. If [] *is* inlined, it
-- won't match with [] appearing in rules in an importing module.
--
-- The "2" says to inline in phase 2
build g = g (:) []
augment :: forall a. (forall b. (a->b->b) -> b -> b) -> [a] -> [a]
{-# INLINE 2 augment #-}
augment g xs = g (:) xs
{-# RULES
"fold/build" forall k z (g::forall b. (a->b->b) -> b -> b) .
foldr k z (build g) = g k z
"foldr/augment" forall k z xs (g::forall b. (a->b->b) -> b -> b) .
foldr k z (augment g xs) = g k (foldr k z xs)
"foldr/id" foldr (:) [] = \x->x
"foldr/app" forall xs ys. foldr (:) ys xs = append xs ys
"foldr/cons" forall k z x xs. foldr k z (x:xs) = k x (foldr k z xs)
"foldr/nil" forall k z. foldr k z [] = z
"augment/build" forall (g::forall b. (a->b->b) -> b -> b)
(h::forall b. (a->b->b) -> b -> b) .
augment g (build h) = build (\c n -> g c (h c n))
"augment/nil" forall (g::forall b. (a->b->b) -> b -> b) .
augment g [] = build g
#-}
-- This rule is true, but not (I think) useful:
-- augment g (augment h t) = augment (\cn -> g c (h c n)) t
\end{code}
----------------------------------------------
-- map
----------------------------------------------
\begin{code}
map :: (a -> b) -> [a] -> [b]
{-# INLINE map #-}
map f xs = build (\c n -> foldr (mapFB c f) n xs)
-- Note eta expanded
mapFB c f x ys = c (f x) ys
mapList :: (a -> b) -> [a] -> [b]
mapList _ [] = []
mapList f (x:xs) = f x : mapList f xs
{-# RULES
"mapFB" forall c f g. mapFB (mapFB c f) g = mapFB c (f.g)
"mapList" forall f. foldr (mapFB (:) f) [] = mapList f
#-}
\end{code}
----------------------------------------------
-- append
----------------------------------------------
\begin{code}
(++) :: [a] -> [a] -> [a]
{-# INLINE (++) #-}
xs ++ ys = augment (\c n -> foldr c n xs) ys
append :: [a] -> [a] -> [a]
append [] ys = ys
append (x:xs) ys = x : append xs ys
\end{code}
%*********************************************************
%* *
\subsection{Type @Bool@}
%* *
%*********************************************************
\begin{code}
data Bool = False | True deriving (Eq, Ord)
-- Read in PrelRead, Show in PrelShow
-- Boolean functions
(&&), (||) :: Bool -> Bool -> Bool
True && x = x
False && _ = False
True || _ = True
False || x = x
not :: Bool -> Bool
not True = False
not False = True
otherwise :: Bool
otherwise = True
\end{code}
%*********************************************************
%* *
\subsection{The @()@ type}
%* *
%*********************************************************
The Unit type is here because virtually any program needs it (whereas
some programs may get away without consulting PrelTup). Furthermore,
the renamer currently *always* asks for () to be in scope, so that
ccalls can use () as their default type; so when compiling PrelBase we
need (). (We could arrange suck in () only if -fglasgow-exts, but putting
it here seems more direct.)
\begin{code}
data () = ()
instance Eq () where
() == () = True
() /= () = False
instance Ord () where
() <= () = True
() < () = False
() >= () = True
() > () = False
max () () = ()
min () () = ()
compare () () = EQ
\end{code}
%*********************************************************
%* *
\subsection{Type @Ordering@}
%* *
%*********************************************************
\begin{code}
data Ordering = LT | EQ | GT deriving (Eq, Ord)
-- Read in PrelRead, Show in PrelShow
\end{code}
%*********************************************************
%* *
\subsection{Type @Char@ and @String@}
%* *
%*********************************************************
\begin{code}
type String = [Char]
data Char = C# Char#
-- We don't use deriving for Eq and Ord, because for Ord the derived
-- instance defines only compare, which takes two primops. Then
-- '>' uses compare, and therefore takes two primops instead of one.
instance Eq Char where
(C# c1) == (C# c2) = c1 `eqChar#` c2
(C# c1) /= (C# c2) = c1 `neChar#` c2
instance Ord Char where
(C# c1) > (C# c2) = c1 `gtChar#` c2
(C# c1) >= (C# c2) = c1 `geChar#` c2
(C# c1) <= (C# c2) = c1 `leChar#` c2
(C# c1) < (C# c2) = c1 `ltChar#` c2
chr :: Int -> Char
chr (I# i) | i >=# 0# && i <=# 255# = C# (chr# i)
| otherwise = error ("Prelude.chr: bad argument")
unsafeChr :: Int -> Char
unsafeChr (I# i) = C# (chr# i)
ord :: Char -> Int
ord (C# c) = I# (ord# c)
\end{code}
%*********************************************************
%* *
\subsection{Type @Int@}
%* *
%*********************************************************
\begin{code}
data Int = I# Int#
zeroInt, oneInt, twoInt, maxInt, minInt :: Int
zeroInt = I# 0#
oneInt = I# 1#
twoInt = I# 2#
minInt = I# (-2147483648#) -- GHC <= 2.09 had this at -2147483647
maxInt = I# 2147483647#
instance Eq Int where
(==) x y = x `eqInt` y
(/=) x y = x `neInt` y
instance Ord Int where
compare x y = compareInt x y
(<) x y = ltInt x y
(<=) x y = leInt x y
(>=) x y = geInt x y
(>) x y = gtInt x y
compareInt :: Int -> Int -> Ordering
(I# x) `compareInt` (I# y) | x <# y = LT
| x ==# y = EQ
| otherwise = GT
\end{code}
%*********************************************************
%* *
\subsection{The function type}
%* *
%*********************************************************
\begin{code}
-- identity function
id :: a -> a
id x = x
-- constant function
const :: a -> b -> a
const x _ = x
-- function composition
{-# INLINE (.) #-}
(.) :: (b -> c) -> (a -> b) -> a -> c
(.) f g x = f (g x)
-- flip f takes its (first) two arguments in the reverse order of f.
flip :: (a -> b -> c) -> b -> a -> c
flip f x y = f y x
-- right-associating infix application operator (useful in continuation-
-- passing style)
($) :: (a -> b) -> a -> b
f $ x = f x
-- until p f yields the result of applying f until p holds.
until :: (a -> Bool) -> (a -> a) -> a -> a
until p f x | p x = x
| otherwise = until p f (f x)
-- asTypeOf is a type-restricted version of const. It is usually used
-- as an infix operator, and its typing forces its first argument
-- (which is usually overloaded) to have the same type as the second.
asTypeOf :: a -> a -> a
asTypeOf = const
\end{code}
%*********************************************************
%* *
\subsection{CCallable instances}
%* *
%*********************************************************
Defined here to avoid orphans
\begin{code}
instance CCallable Char
instance CReturnable Char
instance CCallable Int
instance CReturnable Int
-- DsCCall knows how to pass strings...
instance CCallable [Char]
instance CReturnable () -- Why, exactly?
\end{code}
%*********************************************************
%* *
\subsection{Numeric primops}
%* *
%*********************************************************
Definitions of the boxed PrimOps; these will be
used in the case of partial applications, etc.
\begin{code}
{-# INLINE eqInt #-}
{-# INLINE neInt #-}
{-# INLINE gtInt #-}
{-# INLINE geInt #-}
{-# INLINE ltInt #-}
{-# INLINE leInt #-}
{-# INLINE plusInt #-}
{-# INLINE minusInt #-}
{-# INLINE timesInt #-}
{-# INLINE quotInt #-}
{-# INLINE remInt #-}
{-# INLINE negateInt #-}
plusInt, minusInt, timesInt, quotInt, remInt, gcdInt :: Int -> Int -> Int
plusInt (I# x) (I# y) = I# (x +# y)
minusInt(I# x) (I# y) = I# (x -# y)
timesInt(I# x) (I# y) = I# (x *# y)
quotInt (I# x) (I# y) = I# (quotInt# x y)
remInt (I# x) (I# y) = I# (remInt# x y)
gcdInt (I# a) (I# b) = I# (gcdInt# a b)
negateInt :: Int -> Int
negateInt (I# x) = I# (negateInt# x)
divInt, modInt :: Int -> Int -> Int
x `divInt` y
| x > zeroInt && y < zeroInt = quotInt ((x `minusInt` y) `minusInt` oneInt) y
| x < zeroInt && y > zeroInt = quotInt ((x `minusInt` y) `plusInt` oneInt) y
| otherwise = quotInt x y
x `modInt` y
| x > zeroInt && y < zeroInt ||
x < zeroInt && y > zeroInt = if r/=zeroInt then r `plusInt` y else zeroInt
| otherwise = r
where
r = remInt x y
gtInt, geInt, eqInt, neInt, ltInt, leInt :: Int -> Int -> Bool
gtInt (I# x) (I# y) = x ># y
geInt (I# x) (I# y) = x >=# y
eqInt (I# x) (I# y) = x ==# y
neInt (I# x) (I# y) = x /=# y
ltInt (I# x) (I# y) = x <# y
leInt (I# x) (I# y) = x <=# y
\end{code}
%********************************************************
%* *
\subsection{Unpacking C strings}
%* *
%********************************************************
This code is needed for virtually all programs, since it's used for
unpacking the strings of error messages.
\begin{code}
unpackCString# :: Addr# -> [Char]
{-# INLINE unpackCString# #-}
unpackCString# a = build (unpackFoldrCString# a)
unpackCStringList# :: Addr# -> [Char]
unpackCStringList# addr
= unpack 0#
where
unpack nh
| ch `eqChar#` '\0'# = []
| otherwise = C# ch : unpack (nh +# 1#)
where
ch = indexCharOffAddr# addr nh
unpackAppendCString# :: Addr# -> [Char] -> [Char]
unpackAppendCString# addr rest
= unpack 0#
where
unpack nh
| ch `eqChar#` '\0'# = rest
| otherwise = C# ch : unpack (nh +# 1#)
where
ch = indexCharOffAddr# addr nh
unpackFoldrCString# :: Addr# -> (Char -> a -> a) -> a -> a
unpackFoldrCString# addr f z
= unpack 0#
where
unpack nh
| ch `eqChar#` '\0'# = z
| otherwise = C# ch `f` unpack (nh +# 1#)
where
ch = indexCharOffAddr# addr nh
unpackNBytes# :: Addr# -> Int# -> [Char]
-- This one is called by the compiler to unpack literal
-- strings with NULs in them; rare. It's strict!
-- We don't try to do list deforestation for this one
unpackNBytes# _addr 0# = []
unpackNBytes# addr len# = unpack [] (len# -# 1#)
where
unpack acc i#
| i# <# 0# = acc
| otherwise =
case indexCharOffAddr# addr i# of
ch -> unpack (C# ch : acc) (i# -# 1#)
{-# RULES
"unpack-list" forall a . unpackFoldrCString# a (:) [] = unpackCStringList# a
"unpack-append" forall a n . unpackFoldrCString# a (:) n = unpackAppendCString# a n
-- There's a built-in rule (in PrelRules.lhs) for
-- unpackFoldr "foo" c (unpackFoldr "baz" c n) = unpackFoldr "foobaz" c n
#-}
\end{code}
|