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