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