[project @ 1999-02-17 15:57:20 by simonm]
[ghc-hetmet.git] / ghc / lib / std / PrelBase.lhs
1 %
2 % (c) The GRAP/AQUA Project, Glasgow University, 1992-1996
3 %
4 \section[PrelBase]{Module @PrelBase@}
5
6
7 \begin{code}
8 {-# OPTIONS -fno-implicit-prelude #-}
9
10 module PrelBase
11         (
12         module PrelBase,
13         module PrelGHC          -- Re-export PrelGHC, to avoid lots of people 
14                                 -- having to import it explicitly
15   ) 
16         where
17
18 import {-# SOURCE #-} PrelErr ( error )
19 import PrelGHC
20
21 infixr 9  .
22 infixl 9  !!
23 infixl 7  *
24 infixl 6  +, -
25 infixr 5  ++, :
26 infix  4  ==, /=, <, <=, >=, >
27 infixr 3  &&
28 infixr 2  ||
29 infixl 1  >>, >>=
30 infixr 0  $
31 \end{code}
32
33
34 %*********************************************************
35 %*                                                      *
36 \subsection{Standard classes @Eq@, @Ord@, @Bounded@
37 %*                                                      *
38 %*********************************************************
39
40 \begin{code}
41 class  Eq a  where
42     (==), (/=)          :: a -> a -> Bool
43
44     x /= y              =  not (x == y)
45     x == y              = not  (x /= y)
46
47 class  (Eq a) => Ord a  where
48     compare             :: a -> a -> Ordering
49     (<), (<=), (>=), (>):: a -> a -> Bool
50     max, min            :: a -> a -> a
51
52 -- An instance of Ord should define either compare or <=
53 -- Using compare can be more efficient for complex types.
54     compare x y
55             | x == y    = EQ
56             | x <= y    = LT
57             | otherwise = GT
58
59     x <= y  = compare x y /= GT
60     x <  y  = compare x y == LT
61     x >= y  = compare x y /= LT
62     x >  y  = compare x y == GT
63     max x y = case (compare x y) of { LT -> y ; EQ -> x ; GT -> x }
64     min x y = case (compare x y) of { LT -> x ; EQ -> x ; GT -> y }
65
66 class  Bounded a  where
67     minBound, maxBound :: a
68 \end{code}
69
70 %*********************************************************
71 %*                                                      *
72 \subsection{Monadic classes @Functor@, @Monad@ }
73 %*                                                      *
74 %*********************************************************
75
76 \begin{code}
77 class  Functor f  where
78     fmap         :: (a -> b) -> f a -> f b
79
80 class  Monad m  where
81     (>>=)       :: m a -> (a -> m b) -> m b
82     (>>)        :: m a -> m b -> m b
83     return      :: a -> m a
84     fail        :: String -> m a
85
86     m >> k      =  m >>= \_ -> k
87     fail s      = error s
88
89 \end{code}
90
91
92 %*********************************************************
93 %*                                                      *
94 \subsection{Classes @Num@ and @Enum@}
95 %*                                                      *
96 %*********************************************************
97
98 \begin{code}
99 class  Enum a   where
100     succ, pred          :: a -> a
101     toEnum              :: Int -> a
102     fromEnum            :: a -> Int
103     enumFrom            :: a -> [a]             -- [n..]
104     enumFromThen        :: a -> a -> [a]        -- [n,n'..]
105     enumFromTo          :: a -> a -> [a]        -- [n..m]
106     enumFromThenTo      :: a -> a -> a -> [a]   -- [n,n'..m]
107
108     succ                = toEnum . (+1) . fromEnum
109     pred                = toEnum . (+(-1)) . fromEnum
110     enumFromTo n m      =  map toEnum [fromEnum n .. fromEnum m]
111     enumFromThenTo n n' m
112                         =  map toEnum [fromEnum n, fromEnum n' .. fromEnum m]
113
114 class  (Eq a, Show a) => Num a  where
115     (+), (-), (*)       :: a -> a -> a
116     negate              :: a -> a
117     abs, signum         :: a -> a
118     fromInteger         :: Integer -> a
119     fromInt             :: Int -> a -- partain: Glasgow extension
120
121     x - y               = x + negate y
122     negate x            = 0 - x
123     fromInt (I# i#)     = fromInteger (S# i#)
124                                         -- Go via the standard class-op if the
125                                         -- non-standard one ain't provided
126 \end{code}
127
128 \begin{code}
129 chr :: Int -> Char
130 chr = toEnum
131 ord :: Char -> Int
132 ord = fromEnum
133
134 ord_0 :: Num a => a
135 ord_0 = fromInt (ord '0')
136
137 {-# SPECIALISE subtract :: Int -> Int -> Int #-}
138 subtract        :: (Num a) => a -> a -> a
139 subtract x y    =  y - x
140 \end{code}
141
142
143 %*********************************************************
144 %*                                                      *
145 \subsection{The @Show@ class}
146 %*                                                      *
147 %*********************************************************
148
149 \begin{code}
150 type  ShowS     = String -> String
151
152 class  Show a  where
153     showsPrec :: Int -> a -> ShowS
154     show      :: a   -> String
155     showList  :: [a] -> ShowS
156
157     showList ls     = showList__ (showsPrec 0) ls 
158     showsPrec _ x s = show x ++ s
159     show x          = showsPrec 0 x ""
160 \end{code}
161
162 %*********************************************************
163 %*                                                      *
164 \subsection{The list type}
165 %*                                                      *
166 %*********************************************************
167
168 \begin{code}
169 data [] a = [] | a : [a]  -- do explicitly: deriving (Eq, Ord)
170                           -- to avoid weird names like con2tag_[]#
171
172
173
174 instance (Eq a) => Eq [a]  where
175     []     == []     = True     
176     (x:xs) == (y:ys) = x == y && xs == ys
177     _xs    == _ys    = False                    
178
179     xs     /= ys     = if (xs == ys) then False else True
180
181 instance (Ord a) => Ord [a] where
182     a <  b  = case compare a b of { LT -> True;  EQ -> False; GT -> False }
183     a <= b  = case compare a b of { LT -> True;  EQ -> True;  GT -> False }
184     a >= b  = case compare a b of { LT -> False; EQ -> True;  GT -> True  }
185     a >  b  = case compare a b of { LT -> False; EQ -> False; GT -> True  }
186
187     max a b = case compare a b of { LT -> b; EQ -> a;  GT -> a }
188     min a b = case compare a b of { LT -> a; EQ -> a;  GT -> b }
189
190     compare []     []     = EQ
191     compare (_:_)  []     = GT
192     compare []     (_:_)  = LT
193     compare (x:xs) (y:ys) = case compare x y of
194                                  LT -> LT       
195                                  GT -> GT               
196                                  EQ -> compare xs ys
197
198 map :: (a -> b) -> [a] -> [b]
199 map _ []     = []
200 map f (x:xs) = f x : map f xs
201
202 (++) :: [a] -> [a] -> [a]
203 []     ++ ys = ys
204 (x:xs) ++ ys = x : (xs ++ ys)
205
206 instance Functor [] where
207     fmap = map
208
209 instance  Monad []  where
210     m >>= k             = foldr ((++) . k) [] m
211     m >> k              = foldr ((++) . (\ _ -> k)) [] m
212     return x            = [x]
213     fail _              = []
214
215 instance  (Show a) => Show [a]  where
216     showsPrec _         = showList
217     showList  ls        = showList__ (showsPrec 0) ls
218 \end{code}
219
220 \end{code}
221
222 A few list functions that appear here because they are used here.
223 The rest of the prelude list functions are in PrelList.
224
225 \begin{code}
226 foldr                   :: (a -> b -> b) -> b -> [a] -> b
227 foldr _ z []            =  z
228 foldr f z (x:xs)        =  f x (foldr f z xs)
229
230 -- takeWhile, applied to a predicate p and a list xs, returns the longest
231 -- prefix (possibly empty) of xs of elements that satisfy p.  dropWhile p xs
232 -- returns the remaining suffix.  Span p xs is equivalent to 
233 -- (takeWhile p xs, dropWhile p xs), while break p uses the negation of p.
234
235 takeWhile               :: (a -> Bool) -> [a] -> [a]
236 takeWhile _ []          =  []
237 takeWhile p (x:xs) 
238             | p x       =  x : takeWhile p xs
239             | otherwise =  []
240
241 dropWhile               :: (a -> Bool) -> [a] -> [a]
242 dropWhile _ []          =  []
243 dropWhile p xs@(x:xs')
244             | p x       =  dropWhile p xs'
245             | otherwise =  xs
246
247 -- List index (subscript) operator, 0-origin
248 (!!)                    :: [a] -> Int -> a
249 #ifdef USE_REPORT_PRELUDE
250 (x:_)  !! 0             =  x
251 (_:xs) !! n | n > 0     =  xs !! (n-1)
252 (_:_)  !! _             =  error "Prelude.(!!): negative index"
253 []     !! _             =  error "Prelude.(!!): index too large"
254 #else
255 -- HBC version (stolen), then unboxified
256 -- The semantics is not quite the same for error conditions
257 -- in the more efficient version.
258 --
259 _      !! n | n < 0  =  error "Prelude.(!!): negative index\n"
260 xs     !! n          =  sub xs (case n of { I# n# -> n# })
261                            where sub :: [a] -> Int# -> a
262                                  sub []      _ = error "Prelude.(!!): index too large\n"
263                                  sub (y:ys) n# = if n# ==# 0#
264                                                  then y
265                                                  else sub ys (n# -# 1#)
266 #endif
267 \end{code}
268
269
270 %*********************************************************
271 %*                                                      *
272 \subsection{Type @Bool@}
273 %*                                                      *
274 %*********************************************************
275
276 \begin{code}
277 data  Bool  =  False | True     deriving (Eq, Ord, Enum, Bounded, Show {- Read -})
278
279 -- Boolean functions
280
281 (&&), (||)              :: Bool -> Bool -> Bool
282 True  && x              =  x
283 False && _              =  False
284 True  || _              =  True
285 False || x              =  x
286
287 not                     :: Bool -> Bool
288 not True                =  False
289 not False               =  True
290
291 otherwise               :: Bool
292 otherwise               =  True
293 \end{code}
294
295
296 %*********************************************************
297 %*                                                      *
298 \subsection{The @()@ type}
299 %*                                                      *
300 %*********************************************************
301
302 The Unit type is here because virtually any program needs it (whereas
303 some programs may get away without consulting PrelTup).  Furthermore,
304 the renamer currently *always* asks for () to be in scope, so that
305 ccalls can use () as their default type; so when compiling PrelBase we
306 need ().  (We could arrange suck in () only if -fglasgow-exts, but putting
307 it here seems more direct.
308
309 \begin{code}
310 data  ()  =  ()  --easier to do explicitly: deriving (Eq, Ord, Enum, Show, Bounded)
311                  -- (avoids weird-named functions, e.g., con2tag_()#
312
313 instance Eq () where
314     () == () = True
315     () /= () = False
316
317 instance Ord () where
318     () <= () = True
319     () <  () = False
320     () >= () = True
321     () >  () = False
322     max () () = ()
323     min () () = ()
324     compare () () = EQ
325
326 instance Enum () where
327     succ x      = error "Prelude.Enum.succ{()}: not possible"
328     pred x      = error "Prelude.Enum.pred{()}: not possible"
329     toEnum 0    = ()
330     toEnum _    = error "Prelude.Enum.toEnum{()}: argument not 0"
331     fromEnum () = 0
332     enumFrom ()         = [()]
333     enumFromThen () ()  = [()]
334     enumFromTo () ()    = [()]
335     enumFromThenTo () () () = [()]
336
337 instance  Show ()  where
338     showsPrec _ () = showString "()"
339     showList ls    = showList__ (showsPrec 0) ls
340 \end{code}
341
342 %*********************************************************
343 %*                                                      *
344 \subsection{Type @Ordering@}
345 %*                                                      *
346 %*********************************************************
347
348 \begin{code}
349 data Ordering = LT | EQ | GT    deriving (Eq, Ord, Enum, Bounded, Show {- in PrelRead: Read -})
350 \end{code}
351
352
353 %*********************************************************
354 %*                                                      *
355 \subsection{Type @Char@ and @String@}
356 %*                                                      *
357 %*********************************************************
358
359 \begin{code}
360 type  String = [Char]
361
362 data Char = C# Char#    deriving (Eq, Ord)
363
364 instance  Enum Char  where
365     succ     c@(C# c#)
366        | not (ord# c# ==# 255#) = C# (chr# (ord# c# +# 1#))
367        | otherwise              = error ("Prelude.Enum.succ{Char}: tried to take `succ' of maxBound")
368     pred     c@(C# c#)
369        | not (ord# c# ==# 0#)   = C# (chr# (ord# c# -# 1#))
370        | otherwise              = error ("Prelude.Enum.pred{Char}: tried to to take `pred' of minBound")
371
372     toEnum   (I# i) | i >=# 0# && i <=# 255# =  C# (chr# i)
373                     | otherwise = error ("Prelude.Enum.toEnum{Char}: out of range: " ++ show (I# i))
374     fromEnum (C# c) =  I# (ord# c)
375
376     enumFrom   (C# c)          = efttCh (ord# c)  1#   (># 255#)
377     enumFromTo (C# c1) (C# c2) 
378         | c1 `leChar#` c2 = efttCh (ord# c1) 1#               (># (ord# c2))
379         | otherwise       = []
380
381     enumFromThen (C# c1) (C# c2)
382         | c1 `leChar#` c2 = efttCh (ord# c1) (ord# c2 -# ord# c1) (># 255#)
383         | otherwise       = efttCh (ord# c1) (ord# c2 -# ord# c1) (<# 0#)
384
385     enumFromThenTo (C# c1) (C# c2) (C# c3)
386         | c1 `leChar#` c2 = efttCh (ord# c1) (ord# c2 -# ord# c1) (># (ord# c3))
387         | otherwise       = efttCh (ord# c1) (ord# c2 -# ord# c1) (<# (ord# c3))
388
389 efttCh :: Int# -> Int# -> (Int# -> Bool) -> [Char]
390 efttCh init step done 
391   = go init
392   where
393     go now | done now  = []
394            | otherwise = C# (chr# now) : go (now +# step)
395
396 instance  Show Char  where
397     showsPrec _ '\'' = showString "'\\''"
398     showsPrec _ c    = showChar '\'' . showLitChar c . showChar '\''
399
400     showList cs = showChar '"' . showl cs
401                  where showl ""       = showChar '"'
402                        showl ('"':xs) = showString "\\\"" . showl xs
403                        showl (x:xs)   = showLitChar x . showl xs
404 \end{code}
405
406
407 \begin{code}
408 isAscii, isLatin1, isControl, isPrint, isSpace, isUpper,
409  isLower, isAlpha, isDigit, isOctDigit, isHexDigit, isAlphaNum :: Char -> Bool
410 isAscii c               =  c <  '\x80'
411 isLatin1 c              =  c <= '\xff'
412 isControl c             =  c < ' ' || c >= '\DEL' && c <= '\x9f'
413 isPrint c               =  not (isControl c)
414
415 -- isSpace includes non-breaking space
416 -- Done with explicit equalities both for efficiency, and to avoid a tiresome
417 -- recursion with PrelList elem
418 isSpace c               =  c == ' '     ||
419                            c == '\t'    ||
420                            c == '\n'    ||
421                            c == '\r'    ||
422                            c == '\f'    ||
423                            c == '\v'    ||
424                            c == '\xa0'
425
426 -- The upper case ISO characters have the multiplication sign dumped
427 -- randomly in the middle of the range.  Go figure.
428 isUpper c               =  c >= 'A' && c <= 'Z' || 
429                            c >= '\xC0' && c <= '\xD6' ||
430                            c >= '\xD8' && c <= '\xDE'
431 -- The lower case ISO characters have the division sign dumped
432 -- randomly in the middle of the range.  Go figure.
433 isLower c               =  c >= 'a' && c <= 'z' ||
434                            c >= '\xDF' && c <= '\xF6' ||
435                            c >= '\xF8' && c <= '\xFF'
436 isAsciiLower c          =  c >= 'a' && c <= 'z'
437 isAsciiUpper c          =  c >= 'A' && c <= 'Z'
438
439 isAlpha c               =  isLower c || isUpper c
440 isDigit c               =  c >= '0' && c <= '9'
441 isOctDigit c            =  c >= '0' && c <= '7'
442 isHexDigit c            =  isDigit c || c >= 'A' && c <= 'F' ||
443                                         c >= 'a' && c <= 'f'
444 isAlphaNum c            =  isAlpha c || isDigit c
445
446 -- Case-changing operations
447
448 toUpper, toLower        :: Char -> Char
449 toUpper c@(C# c#)
450   | isAsciiLower c    = C# (chr# (ord# c# -# 32#))
451   | isAscii c         = c
452     -- fall-through to the slower stuff.
453   | isLower c   && c /= '\xDF' && c /= '\xFF'
454   = toEnum (fromEnum c - fromEnum 'a' + fromEnum 'A')
455   | otherwise
456   = c
457
458
459
460 toLower c@(C# c#)
461   | isAsciiUpper c = C# (chr# (ord# c# +# 32#))
462   | isAscii c      = c
463   | isUpper c      =  toEnum (fromEnum c - fromEnum 'A' 
464                                               + fromEnum 'a')
465   | otherwise      =  c
466
467 asciiTab :: [String]
468 asciiTab = -- Using an array drags in the array module.  listArray ('\NUL', ' ')
469            ["NUL", "SOH", "STX", "ETX", "EOT", "ENQ", "ACK", "BEL",
470             "BS",  "HT",  "LF",  "VT",  "FF",  "CR",  "SO",  "SI", 
471             "DLE", "DC1", "DC2", "DC3", "DC4", "NAK", "SYN", "ETB",
472             "CAN", "EM",  "SUB", "ESC", "FS",  "GS",  "RS",  "US", 
473             "SP"] 
474 \end{code}
475
476 %*********************************************************
477 %*                                                      *
478 \subsection{Type @Int@}
479 %*                                                      *
480 %*********************************************************
481
482 \begin{code}
483 data Int = I# Int#
484
485 instance Eq Int where
486     (==) x y = x `eqInt` y
487     (/=) x y = x `neInt` y
488
489 instance Ord Int where
490     compare x y = compareInt x y 
491
492     (<)  x y = ltInt x y
493     (<=) x y = leInt x y
494     (>=) x y = geInt x y
495     (>)  x y = gtInt x y
496     max x y = case (compareInt x y) of { LT -> y ; EQ -> x ; GT -> x }
497     min x y = case (compareInt x y) of { LT -> x ; EQ -> x ; GT -> y }
498
499 compareInt :: Int -> Int -> Ordering
500 (I# x) `compareInt` (I# y) | x <# y    = LT
501                            | x ==# y   = EQ
502                            | otherwise = GT
503
504 instance  Bounded Int where
505     minBound =  -2147483648             -- GHC <= 2.09 had this at -2147483647
506     maxBound =   2147483647
507
508 instance  Enum Int  where
509     succ x  
510        | x == maxBound  = error "Prelude.Enum.succ{Int}: tried to take `succ' of maxBound"
511        | otherwise      = x+1
512     pred x
513        | x == minBound  = error "Prelude.Enum.pred{Int}: tried to take `pred' of minBound"
514        | otherwise      = x-1
515
516     toEnum   x = x
517     fromEnum x = x
518
519 #ifndef USE_FOLDR_BUILD
520     enumFrom     (I# c) = efttInt True c 1# (\ _ -> False)
521
522     enumFromTo   (I# c1) (I# c2) 
523         | c1 <=# c2 = efttInt True  c1 1#              (># c2)
524         | otherwise = []
525
526     enumFromThen (I# c1) (I# c2) 
527         | c1 <# c2  = efttInt True  c1 (c2 -# c1) (\ _ -> False)
528         | otherwise = efttInt False c1 (c2 -# c1) (\ _ -> False)
529
530     enumFromThenTo (I# c1) (I# c2) (I# c3)
531         | c1 <=# c2 = efttInt True  c1 (c2 -# c1) (># c3)
532         | otherwise = efttInt False c1 (c2 -# c1) (<# c3)
533
534 #else
535     {-# INLINE enumFrom #-}
536     {-# INLINE enumFromTo #-}
537     enumFrom x           = build (\ c _ -> 
538         let g x = x `c` g (x `plusInt` 1) in g x)
539     enumFromTo x y       = build (\ c n ->
540         let g x = if x <= y then x `c` g (x `plusInt` 1) else n in g x)
541 #endif
542
543 efttInt :: Bool -> Int# -> Int# -> (Int# -> Bool) -> [Int]
544 efttInt increasing init step done = go init
545   where
546     go now 
547      | done now                     = []    
548      | increasing     && now ># nxt = [I# now] -- overflowed
549      | not increasing && now <# nxt = [I# now] -- underflowed
550      | otherwise                    = I# now : go nxt
551      where
552       nxt = now +# step
553
554 instance  Num Int  where
555     (+)    x y =  plusInt x y
556     (-)    x y =  minusInt x y
557     negate x   =  negateInt x
558     (*)    x y =  timesInt x y
559     abs    n   = if n `geInt` 0 then n else (negateInt n)
560
561     signum n | n `ltInt` 0 = negateInt 1
562              | n `eqInt` 0 = 0
563              | otherwise   = 1
564
565     fromInteger (S# i#) = I# i#
566     fromInteger (J# s# d#)
567       = case (integer2Int# s# d#) of { i# -> I# i# }
568
569     fromInt n           = n
570
571 instance  Show Int  where
572     showsPrec p n = showSignedInt p n
573     showList ls   = showList__ (showsPrec 0)  ls
574 \end{code}
575
576
577 %*********************************************************
578 %*                                                      *
579 \subsection{Type @Integer@, @Float@, @Double@}
580 %*                                                      *
581 %*********************************************************
582
583 \begin{code}
584 data Float      = F# Float#
585 data Double     = D# Double#
586
587 data Integer    
588    = S# Int#                            -- small integers
589    | J# Int# ByteArray#                 -- large integers
590
591 instance  Eq Integer  where
592     (S# i)     ==  (S# j)     = i ==# j
593     (S# i)     ==  (J# s d)   = cmpIntegerInt# s d i ==# 0#
594     (J# s d)   ==  (S# i)     = cmpIntegerInt# s d i ==# 0#
595     (J# s1 d1) ==  (J# s2 d2) = (cmpInteger# s1 d1 s2 d2) ==# 0#
596
597     (S# i)     /=  (S# j)     = i /=# j
598     (S# i)     /=  (J# s d)   = cmpIntegerInt# s d i /=# 0#
599     (J# s d)   /=  (S# i)     = cmpIntegerInt# s d i /=# 0#
600     (J# s1 d1) /=  (J# s2 d2) = (cmpInteger# s1 d1 s2 d2) /=# 0#
601 \end{code}
602
603 %*********************************************************
604 %*                                                      *
605 \subsection{The function type}
606 %*                                                      *
607 %*********************************************************
608
609 \begin{code}
610 instance  Show (a -> b)  where
611     showsPrec _ _  =  showString "<<function>>"
612     showList ls    = showList__ (showsPrec 0) ls
613
614
615 -- identity function
616 id                      :: a -> a
617 id x                    =  x
618
619 -- constant function
620 const                   :: a -> b -> a
621 const x _               =  x
622
623 -- function composition
624 {-# INLINE (.) #-}
625 (.)       :: (b -> c) -> (a -> b) -> a -> c
626 (.) f g x = f (g x)
627
628 -- flip f  takes its (first) two arguments in the reverse order of f.
629 flip                    :: (a -> b -> c) -> b -> a -> c
630 flip f x y              =  f y x
631
632 -- right-associating infix application operator (useful in continuation-
633 -- passing style)
634 ($)                     :: (a -> b) -> a -> b
635 f $ x                   =  f x
636
637 -- until p f  yields the result of applying f until p holds.
638 until                   :: (a -> Bool) -> (a -> a) -> a -> a
639 until p f x | p x       =  x
640             | otherwise =  until p f (f x)
641
642 -- asTypeOf is a type-restricted version of const.  It is usually used
643 -- as an infix operator, and its typing forces its first argument
644 -- (which is usually overloaded) to have the same type as the second.
645 asTypeOf                :: a -> a -> a
646 asTypeOf                =  const
647 \end{code}
648
649 %*********************************************************
650 %*                                                      *
651 \subsection{Support code for @Show@}
652 %*                                                      *
653 %*********************************************************
654
655 \begin{code}
656 shows           :: (Show a) => a -> ShowS
657 shows           =  showsPrec 0
658
659 showChar        :: Char -> ShowS
660 showChar        =  (:)
661
662 showString      :: String -> ShowS
663 showString      =  (++)
664
665 showParen       :: Bool -> ShowS -> ShowS
666 showParen b p   =  if b then showChar '(' . p . showChar ')' else p
667
668 showList__ :: (a -> ShowS) ->  [a] -> ShowS
669
670 showList__ _ []         = showString "[]"
671 showList__ showx (x:xs) = showChar '[' . showx x . showl xs
672   where
673     showl []     = showChar ']'
674     showl (y:ys) = showChar ',' . showx y . showl ys
675
676 showSpace :: ShowS
677 showSpace = {-showChar ' '-} \ xs -> ' ' : xs
678 \end{code}
679
680 Code specific for characters
681
682 \begin{code}
683 showLitChar                :: Char -> ShowS
684 showLitChar c | c > '\DEL' =  showChar '\\' . protectEsc isDigit (shows (ord c))
685 showLitChar '\DEL'         =  showString "\\DEL"
686 showLitChar '\\'           =  showString "\\\\"
687 showLitChar c | c >= ' '   =  showChar c
688 showLitChar '\a'           =  showString "\\a"
689 showLitChar '\b'           =  showString "\\b"
690 showLitChar '\f'           =  showString "\\f"
691 showLitChar '\n'           =  showString "\\n"
692 showLitChar '\r'           =  showString "\\r"
693 showLitChar '\t'           =  showString "\\t"
694 showLitChar '\v'           =  showString "\\v"
695 showLitChar '\SO'          =  protectEsc (== 'H') (showString "\\SO")
696 showLitChar c              =  showString ('\\' : asciiTab!!ord c)
697
698 protectEsc :: (Char -> Bool) -> ShowS -> ShowS
699 protectEsc p f             = f . cont
700                              where cont s@(c:_) | p c = "\\&" ++ s
701                                    cont s             = s
702
703 intToDigit :: Int -> Char
704 intToDigit i
705  | i >= 0  && i <=  9   =  toEnum (fromEnum '0' + i)
706  | i >= 10 && i <= 15   =  toEnum (fromEnum 'a' + i - 10)
707  | otherwise            =  error ("Char.intToDigit: not a digit " ++ show i)
708
709 \end{code}
710
711 Code specific for Ints.
712
713 \begin{code}
714 showSignedInt :: Int -> Int -> ShowS
715 showSignedInt p (I# n) r
716   | n <# 0# && p > 6 = '(':itos n (')':r)
717   | otherwise        = itos n r
718
719 itos :: Int# -> String -> String
720 itos n r
721   | n >=# 0#            = itos' n r
722   | negateInt# n <# 0#  = -- n is minInt, a difficult number
723             itos (n `quotInt#` 10#) (itos' (negateInt# (n `remInt#` 10#)) r)
724   | otherwise = '-':itos' (negateInt# n) r
725  where
726    itos' :: Int# -> String -> String
727    itos' x cs 
728      | x <# 10#  = C# (chr# (x +# ord# '0'#)) : cs
729      | otherwise = itos' (x `quotInt#` 10#) 
730                          (C# (chr# (x `remInt#` 10# +# ord# '0'#)) : cs)
731 \end{code}
732
733 %*********************************************************
734 %*                                                      *
735 \subsection{Numeric primops}
736 %*                                                      *
737 %*********************************************************
738
739 Definitions of the boxed PrimOps; these will be
740 used in the case of partial applications, etc.
741
742 \begin{code}
743 {-# INLINE eqInt #-}
744 {-# INLINE neInt #-}
745
746 plusInt, minusInt, timesInt, quotInt, remInt :: Int -> Int -> Int
747 plusInt (I# x) (I# y) = I# (x +# y)
748 minusInt(I# x) (I# y) = I# (x -# y)
749 timesInt(I# x) (I# y) = I# (x *# y)
750 quotInt (I# x) (I# y) = I# (quotInt# x y)
751 remInt  (I# x) (I# y) = I# (remInt# x y)
752
753 negateInt :: Int -> Int
754 negateInt (I# x)      = I# (negateInt# x)
755
756 gtInt, geInt, eqInt, neInt, ltInt, leInt :: Int -> Int -> Bool
757 gtInt   (I# x) (I# y) = x ># y
758 geInt   (I# x) (I# y) = x >=# y
759 eqInt   (I# x) (I# y) = x ==# y
760 neInt   (I# x) (I# y) = x /=# y
761 ltInt   (I# x) (I# y) = x <# y
762 leInt   (I# x) (I# y) = x <=# y
763 \end{code}
764
765 Convenient boxed Integer PrimOps.  These are 'thin-air' Ids, so
766 it's nice to have them in PrelBase.
767
768 \begin{code}
769 {-# INLINE int2Integer #-}
770 {-# INLINE addr2Integer #-}
771 int2Integer :: Int# -> Integer
772 int2Integer  i = S# i
773 addr2Integer :: Addr# -> Integer
774 addr2Integer x = case addr2Integer# x of (# s, d #) -> J# s d
775 \end{code}