1ec3111ebd98aba5922344d5be799dae1a5a3836
[ghc-hetmet.git] / ghc / lib / std / PrelBase.lhs
1 % -----------------------------------------------------------------------------
2 % $Id: PrelBase.lhs,v 1.42 2001/02/28 00:01:03 qrczak 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 #include "MachDeps.h"
79
80 module PrelBase
81         (
82         module PrelBase,
83         module PrelGHC,         -- Re-export PrelGHC and PrelErr, to avoid lots
84         module PrelErr          -- of people having to import it explicitly
85   ) 
86         where
87
88 import PrelGHC
89 import {-# SOURCE #-} PrelErr
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
152 class  (Eq a) => Ord a  where
153     compare              :: a -> a -> Ordering
154     (<), (<=), (>), (>=) :: a -> a -> Bool
155     max, min             :: a -> a -> a
156
157     -- An instance of Ord should define either 'compare' or '<='.
158     -- Using 'compare' can be more efficient for complex types.
159
160     compare x y
161         | x == y    = EQ
162         | x <= y    = LT        -- NB: must be '<=' not '<' to validate the
163                                 -- above claim about the minimal things that
164                                 -- can be defined for an instance of Ord
165         | otherwise = GT
166
167     x <  y = case compare x y of { LT -> True;  _other -> False }
168     x <= y = case compare x y of { GT -> False; _other -> True }
169     x >  y = case compare x y of { GT -> True;  _other -> False }
170     x >= y = case compare x y of { LT -> False; _other -> True }
171
172         -- These two default methods use '<=' rather than 'compare'
173         -- because the latter is often more expensive
174     max x y = if x <= y then y else x
175     min x y = if x <= y then x else y
176 \end{code}
177
178 %*********************************************************
179 %*                                                      *
180 \subsection{Monadic classes @Functor@, @Monad@ }
181 %*                                                      *
182 %*********************************************************
183
184 \begin{code}
185 class  Functor f  where
186     fmap        :: (a -> b) -> f a -> f b
187
188 class  Monad m  where
189     (>>=)       :: m a -> (a -> m b) -> m b
190     (>>)        :: m a -> m b -> m b
191     return      :: a -> m a
192     fail        :: String -> m a
193
194     m >> k      =  m >>= \_ -> k
195     fail s      = error s
196 \end{code}
197
198
199 %*********************************************************
200 %*                                                      *
201 \subsection{The list type}
202 %*                                                      *
203 %*********************************************************
204
205 \begin{code}
206 data [] a = [] | a : [a]  -- do explicitly: deriving (Eq, Ord)
207                           -- to avoid weird names like con2tag_[]#
208
209
210 instance (Eq a) => Eq [a]  where
211 {-
212     {-# SPECIALISE instance Eq [Char] #-}
213 -}
214     []     == []     = True
215     (x:xs) == (y:ys) = x == y && xs == ys
216     _xs    == _ys    = False
217
218 instance (Ord a) => Ord [a] where
219 {-
220     {-# SPECIALISE instance Ord [Char] #-}
221 -}
222     a <  b  = case compare a b of { LT -> True;  EQ -> False; GT -> False }
223     a <= b  = case compare a b of { LT -> True;  EQ -> True;  GT -> False }
224     a >= b  = case compare a b of { LT -> False; EQ -> True;  GT -> True  }
225     a >  b  = case compare a b of { LT -> False; EQ -> False; GT -> True  }
226
227     compare []     []     = EQ
228     compare (_:_)  []     = GT
229     compare []     (_:_)  = LT
230     compare (x:xs) (y:ys) = case compare x y of
231                                 LT -> LT
232                                 GT -> GT
233                                 EQ -> compare xs ys
234
235 instance Functor [] where
236     fmap = map
237
238 instance  Monad []  where
239     m >>= k             = foldr ((++) . k) [] m
240     m >> k              = foldr ((++) . (\ _ -> k)) [] m
241     return x            = [x]
242     fail _              = []
243 \end{code}
244
245 A few list functions that appear here because they are used here.
246 The rest of the prelude list functions are in PrelList.
247
248 ----------------------------------------------
249 --      foldr/build/augment
250 ----------------------------------------------
251   
252 \begin{code}
253 foldr            :: (a -> b -> b) -> b -> [a] -> b
254 -- foldr _ z []     =  z
255 -- foldr f z (x:xs) =  f x (foldr f z xs)
256 {-# INLINE foldr #-}
257 foldr k z xs = go xs
258              where
259                go []     = z
260                go (y:ys) = y `k` go ys
261
262 build   :: forall a. (forall b. (a -> b -> b) -> b -> b) -> [a]
263 {-# INLINE 2 build #-}
264         -- The INLINE is important, even though build is tiny,
265         -- because it prevents [] getting inlined in the version that
266         -- appears in the interface file.  If [] *is* inlined, it
267         -- won't match with [] appearing in rules in an importing module.
268         --
269         -- The "2" says to inline in phase 2
270
271 build g = g (:) []
272
273 augment :: forall a. (forall b. (a->b->b) -> b -> b) -> [a] -> [a]
274 {-# INLINE 2 augment #-}
275 augment g xs = g (:) xs
276
277 {-# RULES
278 "fold/build"    forall k z (g::forall b. (a->b->b) -> b -> b) . 
279                 foldr k z (build g) = g k z
280
281 "foldr/augment" forall k z xs (g::forall b. (a->b->b) -> b -> b) . 
282                 foldr k z (augment g xs) = g k (foldr k z xs)
283
284 "foldr/id"      foldr (:) [] = \x->x
285 "foldr/app"     forall xs ys. foldr (:) ys xs = append xs ys
286
287 "foldr/cons"    forall k z x xs. foldr k z (x:xs) = k x (foldr k z xs)
288 "foldr/nil"     forall k z.      foldr k z []     = z 
289
290 "augment/build" forall (g::forall b. (a->b->b) -> b -> b)
291                        (h::forall b. (a->b->b) -> b -> b) .
292                        augment g (build h) = build (\c n -> g c (h c n))
293 "augment/nil"   forall (g::forall b. (a->b->b) -> b -> b) .
294                         augment g [] = build g
295  #-}
296
297 -- This rule is true, but not (I think) useful:
298 --      augment g (augment h t) = augment (\cn -> g c (h c n)) t
299 \end{code}
300
301
302 ----------------------------------------------
303 --              map     
304 ----------------------------------------------
305
306 \begin{code}
307 map :: (a -> b) -> [a] -> [b]
308 map = mapList
309
310 -- Note eta expanded
311 mapFB ::  (elt -> lst -> lst) -> (a -> elt) -> a -> lst -> lst
312 mapFB c f x ys = c (f x) ys
313
314 mapList :: (a -> b) -> [a] -> [b]
315 mapList _ []     = []
316 mapList f (x:xs) = f x : mapList f xs
317
318 {-# RULES
319 "map"       forall f xs.        map f xs                = build (\c n -> foldr (mapFB c f) n xs)
320 "mapFB"     forall c f g.       mapFB (mapFB c f) g     = mapFB c (f.g) 
321 "mapList"   forall f.           foldr (mapFB (:) f) []  = mapList f
322  #-}
323 \end{code}
324
325
326 ----------------------------------------------
327 --              append  
328 ----------------------------------------------
329 \begin{code}
330 (++) :: [a] -> [a] -> [a]
331 (++) = append
332
333 {-# RULES
334   "++"  forall xs ys. (++) xs ys = augment (\c n -> foldr c n xs) ys
335  #-}
336
337 append :: [a] -> [a] -> [a]
338 append []     ys = ys
339 append (x:xs) ys = x : append xs ys
340 \end{code}
341
342
343 %*********************************************************
344 %*                                                      *
345 \subsection{Type @Bool@}
346 %*                                                      *
347 %*********************************************************
348
349 \begin{code}
350 data  Bool  =  False | True  deriving (Eq, Ord)
351         -- Read in PrelRead, Show in PrelShow
352
353 -- Boolean functions
354
355 (&&), (||)              :: Bool -> Bool -> Bool
356 True  && x              =  x
357 False && _              =  False
358 True  || _              =  True
359 False || x              =  x
360
361 not                     :: Bool -> Bool
362 not True                =  False
363 not False               =  True
364
365 otherwise               :: Bool
366 otherwise               =  True
367 \end{code}
368
369
370 %*********************************************************
371 %*                                                      *
372 \subsection{The @()@ type}
373 %*                                                      *
374 %*********************************************************
375
376 The Unit type is here because virtually any program needs it (whereas
377 some programs may get away without consulting PrelTup).  Furthermore,
378 the renamer currently *always* asks for () to be in scope, so that
379 ccalls can use () as their default type; so when compiling PrelBase we
380 need ().  (We could arrange suck in () only if -fglasgow-exts, but putting
381 it here seems more direct.)
382
383 \begin{code}
384 data  ()  =  ()
385
386 instance Eq () where
387     () == () = True
388     () /= () = False
389
390 instance Ord () where
391     () <= () = True
392     () <  () = False
393     () >= () = True
394     () >  () = False
395     max () () = ()
396     min () () = ()
397     compare () () = EQ
398 \end{code}
399
400
401 %*********************************************************
402 %*                                                      *
403 \subsection{Type @Ordering@}
404 %*                                                      *
405 %*********************************************************
406
407 \begin{code}
408 data Ordering = LT | EQ | GT deriving (Eq, Ord)
409         -- Read in PrelRead, Show in PrelShow
410 \end{code}
411
412
413 %*********************************************************
414 %*                                                      *
415 \subsection{Type @Char@ and @String@}
416 %*                                                      *
417 %*********************************************************
418
419 \begin{code}
420 type  String = [Char]
421
422 data Char = C# Char#
423
424 -- We don't use deriving for Eq and Ord, because for Ord the derived
425 -- instance defines only compare, which takes two primops.  Then
426 -- '>' uses compare, and therefore takes two primops instead of one.
427
428 instance Eq Char where
429   (C# c1) == (C# c2) = c1 `eqChar#` c2
430   (C# c1) /= (C# c2) = c1 `neChar#` c2
431
432 instance Ord Char where
433   (C# c1) >  (C# c2) = c1 `gtChar#` c2
434   (C# c1) >= (C# c2) = c1 `geChar#` c2
435   (C# c1) <= (C# c2) = c1 `leChar#` c2
436   (C# c1) <  (C# c2) = c1 `ltChar#` c2
437
438 chr :: Int -> Char
439 chr (I# i) | i >=# 0# && i <=# 0x10FFFF#
440              = C# (chr# i)
441            | otherwise = error "Prelude.chr: bad argument"
442
443 unsafeChr :: Int -> Char
444 unsafeChr (I# i) =  C# (chr# i)
445
446 ord :: Char -> Int
447 ord (C# c) =  I# (ord# c)
448 \end{code}
449
450 String equality is used when desugaring pattern-matches against strings.
451 It's worth making it fast, and providing a rule to use the fast version
452 where possible.
453
454 \begin{code}
455 eqString :: String -> String -> Bool
456 eqString []            []            = True
457 eqString (C# c1 : cs1) (C# c2 : cs2) = c1 `eqChar#` c2 && cs1 `eqString` cs2
458 eqString _             _             = False
459
460 {-# RULES
461 "eqString"  (==) = eqString
462   #-}  
463 \end{code}
464
465 %*********************************************************
466 %*                                                      *
467 \subsection{Type @Int@}
468 %*                                                      *
469 %*********************************************************
470
471 \begin{code}
472 data Int = I# Int#
473
474 zeroInt, oneInt, twoInt, maxInt, minInt :: Int
475 zeroInt = I# 0#
476 oneInt  = I# 1#
477 twoInt  = I# 2#
478 #if WORD_SIZE_IN_BYTES == 4
479 minInt  = I# (-0x80000000#)
480 maxInt  = I# 0x7FFFFFFF#
481 #else
482 minInt  = I# (-0x8000000000000000#)
483 maxInt  = I# 0x7FFFFFFFFFFFFFFF#
484 #endif
485
486 instance Eq Int where
487     (==) = eqInt
488     (/=) = neInt
489
490 instance Ord Int where
491     compare = compareInt
492
493     (<)  = ltInt
494     (<=) = leInt
495     (>=) = geInt
496     (>)  = gtInt
497
498 compareInt :: Int -> Int -> Ordering
499 (I# x) `compareInt` (I# y) = compareInt# x y
500
501 compareInt# :: Int# -> Int# -> Ordering
502 compareInt# x# y#
503  | x# <#  y# = LT
504  | x# ==# y# = EQ
505  | otherwise = GT
506 \end{code}
507
508
509 %*********************************************************
510 %*                                                      *
511 \subsection{The function type}
512 %*                                                      *
513 %*********************************************************
514
515 \begin{code}
516 -- identity function
517 id                      :: a -> a
518 id x                    =  x
519
520 -- constant function
521 const                   :: a -> b -> a
522 const x _               =  x
523
524 -- function composition
525 {-# INLINE (.) #-}
526 (.)       :: (b -> c) -> (a -> b) -> a -> c
527 (.) f g x = f (g x)
528
529 -- flip f  takes its (first) two arguments in the reverse order of f.
530 flip                    :: (a -> b -> c) -> b -> a -> c
531 flip f x y              =  f y x
532
533 -- right-associating infix application operator (useful in continuation-
534 -- passing style)
535 {-# INLINE ($) #-}
536 ($)                     :: (a -> b) -> a -> b
537 f $ x                   =  f x
538
539 -- until p f  yields the result of applying f until p holds.
540 until                   :: (a -> Bool) -> (a -> a) -> a -> a
541 until p f x | p x       =  x
542             | otherwise =  until p f (f x)
543
544 -- asTypeOf is a type-restricted version of const.  It is usually used
545 -- as an infix operator, and its typing forces its first argument
546 -- (which is usually overloaded) to have the same type as the second.
547 asTypeOf                :: a -> a -> a
548 asTypeOf                =  const
549 \end{code}
550
551 %*********************************************************
552 %*                                                      *
553 \subsection{CCallable instances}
554 %*                                                      *
555 %*********************************************************
556
557 Defined here to avoid orphans
558
559 \begin{code}
560 instance CCallable Char
561 instance CReturnable Char
562
563 instance CCallable   Int
564 instance CReturnable Int
565
566 instance CReturnable () -- Why, exactly?
567 \end{code}
568
569
570 %*********************************************************
571 %*                                                      *
572 \subsection{Generics}
573 %*                                                      *
574 %*********************************************************
575
576 \begin{code}
577 data Unit = Unit
578 data a :+: b = Inl a | Inr b
579 data a :*: b = a :*: b
580 \end{code}
581
582
583 %*********************************************************
584 %*                                                      *
585 \subsection{Numeric primops}
586 %*                                                      *
587 %*********************************************************
588
589 \begin{code}
590 divInt#, modInt# :: Int# -> Int# -> Int#
591 x# `divInt#` y#
592     | (x# ># 0#) && (y# <# 0#) = ((x# -# y#) -# 1#) `quotInt#` y#
593     | (x# <# 0#) && (y# ># 0#) = ((x# -# y#) +# 1#) `quotInt#` y#
594     | otherwise                = x# `quotInt#` y#
595 x# `modInt#` y#
596     | (x# ># 0#) && (y# <# 0#) ||
597       (x# <# 0#) && (y# ># 0#)    = if r# /=# 0# then r# +# y# else 0#
598     | otherwise                   = r#
599     where
600     r# = x# `remInt#` y#
601 \end{code}
602
603 Definitions of the boxed PrimOps; these will be
604 used in the case of partial applications, etc.
605
606 \begin{code}
607 {-# INLINE eqInt #-}
608 {-# INLINE neInt #-}
609 {-# INLINE gtInt #-}
610 {-# INLINE geInt #-}
611 {-# INLINE ltInt #-}
612 {-# INLINE leInt #-}
613 {-# INLINE plusInt #-}
614 {-# INLINE minusInt #-}
615 {-# INLINE timesInt #-}
616 {-# INLINE quotInt #-}
617 {-# INLINE remInt #-}
618 {-# INLINE negateInt #-}
619
620 plusInt, minusInt, timesInt, quotInt, remInt, divInt, modInt, gcdInt :: Int -> Int -> Int
621 (I# x) `plusInt`  (I# y) = I# (x +# y)
622 (I# x) `minusInt` (I# y) = I# (x -# y)
623 (I# x) `timesInt` (I# y) = I# (x *# y)
624 (I# x) `quotInt`  (I# y) = I# (x `quotInt#` y)
625 (I# x) `remInt`   (I# y) = I# (x `remInt#`  y)
626 (I# x) `divInt`   (I# y) = I# (x `divInt#`  y)
627 (I# x) `modInt`   (I# y) = I# (x `modInt#`  y)
628
629 gcdInt (I# a) (I# b) = g a b
630    where g 0# 0# = error "PrelBase.gcdInt: gcd 0 0 is undefined"
631          g 0# _  = I# absB
632          g _  0# = I# absA
633          g _  _  = I# (gcdInt# absA absB)
634
635          absInt x = if x <# 0# then negateInt# x else x
636
637          absA     = absInt a
638          absB     = absInt b
639
640 negateInt :: Int -> Int
641 negateInt (I# x) = I# (negateInt# x)
642
643 gtInt, geInt, eqInt, neInt, ltInt, leInt :: Int -> Int -> Bool
644 (I# x) `gtInt` (I# y) = x >#  y
645 (I# x) `geInt` (I# y) = x >=# y
646 (I# x) `eqInt` (I# y) = x ==# y
647 (I# x) `neInt` (I# y) = x /=# y
648 (I# x) `ltInt` (I# y) = x <#  y
649 (I# x) `leInt` (I# y) = x <=# y
650
651 {-# RULES
652 "int2Word2Int"  forall x#. int2Word# (word2Int# x#) = x#
653 "word2Int2Word" forall x#. word2Int# (int2Word# x#) = x#
654     #-}
655 \end{code}
656
657
658 %********************************************************
659 %*                                                      *
660 \subsection{Unpacking C strings}
661 %*                                                      *
662 %********************************************************
663
664 This code is needed for virtually all programs, since it's used for
665 unpacking the strings of error messages.
666
667 \begin{code}
668 unpackCString# :: Addr# -> [Char]
669 unpackCString# a = unpackCStringList# a
670
671 unpackCStringList# :: Addr# -> [Char]
672 unpackCStringList# addr 
673   = unpack 0#
674   where
675     unpack nh
676       | ch `eqChar#` '\0'# = []
677       | otherwise          = C# ch : unpack (nh +# 1#)
678       where
679         ch = indexCharOffAddr# addr nh
680
681 unpackAppendCString# :: Addr# -> [Char] -> [Char]
682 unpackAppendCString# addr rest
683   = unpack 0#
684   where
685     unpack nh
686       | ch `eqChar#` '\0'# = rest
687       | otherwise          = C# ch : unpack (nh +# 1#)
688       where
689         ch = indexCharOffAddr# addr nh
690
691 unpackFoldrCString# :: Addr# -> (Char  -> a -> a) -> a -> a 
692 unpackFoldrCString# addr f z 
693   = unpack 0#
694   where
695     unpack nh
696       | ch `eqChar#` '\0'# = z
697       | otherwise          = C# ch `f` unpack (nh +# 1#)
698       where
699         ch = indexCharOffAddr# addr nh
700
701 unpackCStringUtf8# :: Addr# -> [Char]
702 unpackCStringUtf8# addr 
703   = unpack 0#
704   where
705     unpack nh
706       | ch `eqChar#` '\0'#   = []
707       | ch `leChar#` '\x7F'# = C# ch : unpack (nh +# 1#)
708       | ch `leChar#` '\xDF'# =
709           C# (chr# ((ord# ch                                  -# 0xC0#) `iShiftL#`  6# +#
710                     (ord# (indexCharOffAddr# addr (nh +# 1#)) -# 0x80#))) :
711           unpack (nh +# 2#)
712       | ch `leChar#` '\xEF'# =
713           C# (chr# ((ord# ch                                  -# 0xE0#) `iShiftL#` 12# +#
714                     (ord# (indexCharOffAddr# addr (nh +# 1#)) -# 0x80#) `iShiftL#`  6# +#
715                     (ord# (indexCharOffAddr# addr (nh +# 2#)) -# 0x80#))) :
716           unpack (nh +# 3#)
717       | otherwise            =
718           C# (chr# ((ord# ch                                  -# 0xF0#) `iShiftL#` 18# +#
719                     (ord# (indexCharOffAddr# addr (nh +# 1#)) -# 0x80#) `iShiftL#` 12# +#
720                     (ord# (indexCharOffAddr# addr (nh +# 2#)) -# 0x80#) `iShiftL#`  6# +#
721                     (ord# (indexCharOffAddr# addr (nh +# 3#)) -# 0x80#))) :
722           unpack (nh +# 4#)
723       where
724         ch = indexCharOffAddr# addr nh
725
726 unpackNBytes# :: Addr# -> Int# -> [Char]
727 unpackNBytes# _addr 0#   = []
728 unpackNBytes#  addr len# = unpack [] (len# -# 1#)
729     where
730      unpack acc i#
731       | i# <# 0#  = acc
732       | otherwise = 
733          case indexCharOffAddr# addr i# of
734             ch -> unpack (C# ch : acc) (i# -# 1#)
735
736 {-# RULES
737 "unpack"         forall a   . unpackCString# a             = build (unpackFoldrCString# a)
738 "unpack-list"    forall a   . unpackFoldrCString# a (:) [] = unpackCStringList# a
739 "unpack-append"  forall a n . unpackFoldrCString# a (:) n  = unpackAppendCString# a n
740
741 -- There's a built-in rule (in PrelRules.lhs) for
742 --      unpackFoldr "foo" c (unpackFoldr "baz" c n)  =  unpackFoldr "foobaz" c n
743
744   #-}
745 \end{code}