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