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