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