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