[project @ 1998-02-02 17:27:26 by simonm]
[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 succ, pred              :: Enum a => a -> a
228 succ                    =  toEnum . (+1) . fromEnum
229 pred                    =  toEnum . (subtract 1) . fromEnum
230
231 chr = (toEnum   :: Int  -> Char)
232 ord = (fromEnum :: Char -> Int)
233
234 ord_0 :: Num a => a
235 ord_0 = fromInt (ord '0')
236
237 {-# GENERATE_SPECS subtract a{Int#,Double#,Int,Double,Complex(Double#),Complex(Double)} #-}
238 subtract        :: (Num a) => a -> a -> a
239 subtract x y    =  y - x
240 \end{code}
241
242
243 %*********************************************************
244 %*                                                      *
245 \subsection{The @Show@ class}
246 %*                                                      *
247 %*********************************************************
248
249 \begin{code}
250 type  ShowS     = String -> String
251
252 class  Show a  where
253     showsPrec :: Int -> a -> ShowS
254     showList  :: [a] -> ShowS
255
256     showList ls = showList__ (showsPrec 0) ls 
257 \end{code}
258
259 %*********************************************************
260 %*                                                      *
261 \subsection{The list type}
262 %*                                                      *
263 %*********************************************************
264
265 \begin{code}
266 data [] a = [] | a : [a]  -- do explicitly: deriving (Eq, Ord)
267                           -- to avoid weird names like con2tag_[]#
268
269 instance (Eq a) => Eq [a]  where
270     []     == []     = True     
271     (x:xs) == (y:ys) = x == y && xs == ys
272     xs     == ys     = False                    
273     xs     /= ys     = if (xs == ys) then False else True
274
275 instance (Ord a) => Ord [a] where
276     a <  b  = case compare a b of { LT -> True;  EQ -> False; GT -> False }
277     a <= b  = case compare a b of { LT -> True;  EQ -> True;  GT -> False }
278     a >= b  = case compare a b of { LT -> False; EQ -> True;  GT -> True  }
279     a >  b  = case compare a b of { LT -> False; EQ -> False; GT -> True  }
280
281     max a b = case compare a b of { LT -> b; EQ -> a;  GT -> a }
282     min a b = case compare a b of { LT -> a; EQ -> a;  GT -> b }
283
284     compare []     []     = EQ
285     compare (x:xs) []     = GT
286     compare []     (y:ys) = LT
287     compare (x:xs) (y:ys) = case compare x y of
288                                  LT -> LT       
289                                  GT -> GT               
290                                  EQ -> compare xs ys
291
292 instance Functor [] where
293     map f []             =  []
294     map f (x:xs)         =  f x : map f xs
295
296 instance  Monad []  where
297     m >>= k             = foldr ((++) . k) [] m
298     m >> k              = foldr ((++) . (\ _ -> k)) [] m
299     return x            = [x]
300
301 instance  MonadZero []  where
302     zero                = []
303
304 instance  MonadPlus []  where
305 #ifdef USE_REPORT_PRELUDE
306     xs ++ ys            =  foldr (:) ys xs
307 #else
308     [] ++ ys            =  ys
309     (x:xs) ++ ys        =  x : (xs ++ ys)
310 #endif
311
312 instance  (Show a) => Show [a]  where
313     showsPrec p         = showList
314     showList  ls        = showList__ (showsPrec 0) ls
315 \end{code}
316
317 \end{code}
318
319 A few list functions that appear here because they are used here.
320 The rest of the prelude list functions are in PrelList.
321
322 \begin{code}
323 foldr                   :: (a -> b -> b) -> b -> [a] -> b
324 foldr f z []            =  z
325 foldr f z (x:xs)        =  f x (foldr f z xs)
326
327 -- takeWhile, applied to a predicate p and a list xs, returns the longest
328 -- prefix (possibly empty) of xs of elements that satisfy p.  dropWhile p xs
329 -- returns the remaining suffix.  Span p xs is equivalent to 
330 -- (takeWhile p xs, dropWhile p xs), while break p uses the negation of p.
331
332 takeWhile               :: (a -> Bool) -> [a] -> [a]
333 takeWhile p []          =  []
334 takeWhile p (x:xs) 
335             | p x       =  x : takeWhile p xs
336             | otherwise =  []
337
338 dropWhile               :: (a -> Bool) -> [a] -> [a]
339 dropWhile p []          =  []
340 dropWhile p xs@(x:xs')
341             | p x       =  dropWhile p xs'
342             | otherwise =  xs
343
344 -- List index (subscript) operator, 0-origin
345 (!!)                    :: [a] -> Int -> a
346 #ifdef USE_REPORT_PRELUDE
347 (x:_)  !! 0             =  x
348 (_:xs) !! n | n > 0     =  xs !! (n-1)
349 (_:_)  !! _             =  error "PreludeList.!!: negative index"
350 []     !! _             =  error "PreludeList.!!: index too large"
351 #else
352 -- HBC version (stolen), then unboxified
353 -- The semantics is not quite the same for error conditions
354 -- in the more efficient version.
355 --
356 _      !! n | n < 0  =  error "(!!){PreludeList}: negative index\n"
357 xs     !! n          =  sub xs (case n of { I# n# -> n# })
358                            where sub :: [a] -> Int# -> a
359                                  sub []      _ = error "(!!){PreludeList}: index too large\n"
360                                  sub (x:xs) n# = if n# ==# 0#
361                                                  then x
362                                                  else sub xs (n# -# 1#)
363 #endif
364 \end{code}
365
366
367 %*********************************************************
368 %*                                                      *
369 \subsection{Type @Void@}
370 %*                                                      *
371 %*********************************************************
372
373 The type @Void@ is built in, but it needs a @Show@ instance.
374
375 \begin{code}
376 void :: Void
377 void = error "You tried to evaluate void"
378
379 instance  Show Void  where
380     showsPrec p f  =  showString "<<void>>"
381     showList ls    = showList__ (showsPrec 0) ls
382 \end{code}
383
384
385 %*********************************************************
386 %*                                                      *
387 \subsection{Type @Bool@}
388 %*                                                      *
389 %*********************************************************
390
391 \begin{code}
392 data  Bool  =  False | True     deriving (Eq, Ord, Enum, Bounded, Show {- Read -})
393
394 -- Boolean functions
395
396 (&&), (||)              :: Bool -> Bool -> Bool
397 True  && x              =  x
398 False && x              =  False
399 True  || x              =  True
400 False || x              =  x
401
402 not                     :: Bool -> Bool
403 not True                =  False
404 not False               =  True
405
406 otherwise               :: Bool
407 otherwise               =  True
408 \end{code}
409
410
411 %*********************************************************
412 %*                                                      *
413 \subsection{The @()@ type}
414 %*                                                      *
415 %*********************************************************
416
417 The Unit type is here because virtually any program needs it (whereas
418 some programs may get away without consulting PrelTup).  Furthermore,
419 the renamer currently *always* asks for () to be in scope, so that
420 ccalls can use () as their default type; so when compiling PrelBase we
421 need ().  (We could arrange suck in () only if -fglasgow-exts, but putting
422 it here seems more direct.
423
424 \begin{code}
425 data  ()  =  ()  --easier to do explicitly: deriving (Eq, Ord, Enum, Show, Bounded)
426                  -- (avoids weird-named functions, e.g., con2tag_()#
427
428 instance Eq () where
429     () == () = True
430     () /= () = False
431
432 instance Ord () where
433     () <= () = True
434     () <  () = False
435     () >= () = True
436     () >  () = False
437     max () () = ()
438     min () () = ()
439     compare () () = EQ
440
441 instance Enum () where
442     toEnum 0    = ()
443     toEnum _    = error "Prelude.Enum.().toEnum: argument not 0"
444     fromEnum () = 0
445     enumFrom ()         = [()]
446     enumFromThen () ()  = [()]
447     enumFromTo () ()    = [()]
448     enumFromThenTo () () () = [()]
449
450 instance  Show ()  where
451     showsPrec p () = showString "()"
452     showList ls    = showList__ (showsPrec 0) ls
453 \end{code}
454
455 %*********************************************************
456 %*                                                      *
457 \subsection{Type @Ordering@}
458 %*                                                      *
459 %*********************************************************
460
461 \begin{code}
462 data Ordering = LT | EQ | GT    deriving (Eq, Ord, Enum, Bounded, Show {- Read -})
463 \end{code}
464
465
466 %*********************************************************
467 %*                                                      *
468 \subsection{Type @Char@ and @String@}
469 %*                                                      *
470 %*********************************************************
471
472 \begin{code}
473 type  String = [Char]
474
475 data Char = C# Char#    deriving (Eq, Ord)
476
477 instance  Enum Char  where
478     toEnum   (I# i) | i >=# 0# && i <=# 255# =  C# (chr# i)
479                     | otherwise = error ("Prelude.Enum.Char.toEnum:out of range: " ++ show (I# i))
480     fromEnum (C# c)              =  I# (ord# c)
481
482     enumFrom   (C# c)          =  efttCh (ord# c)  1#   (># 255#)
483     enumFromTo (C# c1) (C# c2) = efttCh (ord# c1) 1#  (># (ord# c2))
484
485     enumFromThen (C# c1) (C# c2)
486         | c1 `leChar#` c2 = efttCh (ord# c1) (ord# c2 -# ord# c1) (># 255#)
487         | otherwise       = efttCh (ord# c1) (ord# c2 -# ord# c1) (<# 0#)
488
489     enumFromThenTo (C# c1) (C# c2) (C# c3)
490         | c1 `leChar#` c2 = efttCh (ord# c1) (ord# c2 -# ord# c1) (># (ord# c3))
491         | otherwise       = efttCh (ord# c1) (ord# c2 -# ord# c1) (<# (ord# c3))
492
493 efttCh :: Int# -> Int# -> (Int# -> Bool) -> [Char]
494 efttCh now step done 
495   = go now
496   where
497     go now | done now  = []
498            | otherwise = C# (chr# now) : go (now +# step)
499
500 instance  Show Char  where
501     showsPrec p '\'' = showString "'\\''"
502     showsPrec p c    = showChar '\'' . showLitChar c . showChar '\''
503
504     showList cs = showChar '"' . showl cs
505                  where showl ""       = showChar '"'
506                        showl ('"':cs) = showString "\\\"" . showl cs
507                        showl (c:cs)   = showLitChar c . showl cs
508 \end{code}
509
510
511 \begin{code}
512 isAscii, isLatin1, isControl, isPrint, isSpace, isUpper,
513  isLower, isAlpha, isDigit, isOctDigit, isHexDigit, isAlphanum :: Char -> Bool
514 isAscii c               =  fromEnum c < 128
515 isLatin1 c              =  c <= '\xff'
516 isControl c             =  c < ' ' || c >= '\DEL' && c <= '\x9f'
517 isPrint c               =  not (isControl c)
518
519 -- isSpace includes non-breaking space
520 -- Done with explicit equalities both for efficiency, and to avoid a tiresome
521 -- recursion with PrelList elem
522 isSpace c               =  c == ' '     ||
523                            c == '\t'    ||
524                            c == '\n'    ||
525                            c == '\r'    ||
526                            c == '\f'    ||
527                            c == '\v'    ||
528                            c == '\xa0'
529
530 -- The upper case ISO characters have the multiplication sign dumped
531 -- randomly in the middle of the range.  Go figure.
532 isUpper c               =  c >= 'A' && c <= 'Z' || 
533                            c >= '\xC0' && c <= '\xD6' ||
534                            c >= '\xD8' && c <= '\xDE'
535 -- The lower case ISO characters have the division sign dumped
536 -- randomly in the middle of the range.  Go figure.
537 isLower c               =  c >= 'a' && c <= 'z' ||
538                            c >= '\xDF' && c <= '\xF6' ||
539                            c >= '\xF8' && c <= '\xFF'
540 isAlpha c               =  isLower c || isUpper c
541 isDigit c               =  c >= '0' && c <= '9'
542 isOctDigit c            =  c >= '0' && c <= '7'
543 isHexDigit c            =  isDigit c || c >= 'A' && c <= 'F' ||
544                                         c >= 'a' && c <= 'f'
545 isAlphanum c            =  isAlpha c || isDigit c
546
547 -- Case-changing operations
548
549 toUpper, toLower        :: Char -> Char
550 toUpper c | isLower c   && c /= '\xDF' && c /= '\xFF'
551  =  toEnum (fromEnum c - fromEnum 'a' + fromEnum 'A')
552   | otherwise   =  c
553
554 toLower c | isUpper c   =  toEnum (fromEnum c - fromEnum 'A' 
555                                               + fromEnum 'a')
556           | otherwise   =  c
557
558 asciiTab = -- Using an array drags in the array module.  listArray ('\NUL', ' ')
559            ["NUL", "SOH", "STX", "ETX", "EOT", "ENQ", "ACK", "BEL",
560             "BS",  "HT",  "LF",  "VT",  "FF",  "CR",  "SO",  "SI", 
561             "DLE", "DC1", "DC2", "DC3", "DC4", "NAK", "SYN", "ETB",
562             "CAN", "EM",  "SUB", "ESC", "FS",  "GS",  "RS",  "US", 
563             "SP"] 
564 \end{code}
565
566 %*********************************************************
567 %*                                                      *
568 \subsection{Type @Int@}
569 %*                                                      *
570 %*********************************************************
571
572 \begin{code}
573 data Int = I# Int#
574
575 instance Eq Int where
576     (==) x y = x `eqInt` y
577     (/=) x y = x `neInt` y
578
579 instance Ord Int where
580     compare x y = compareInt x y 
581
582     (<)  x y = ltInt x y
583     (<=) x y = leInt x y
584     (>=) x y = geInt x y
585     (>)  x y = gtInt x y
586     max x y = case (compareInt x y) of { LT -> y ; EQ -> x ; GT -> x }
587     min x y = case (compareInt x y) of { LT -> x ; EQ -> x ; GT -> y }
588
589 (I# x) `compareInt` (I# y) | x <# y    = LT
590                            | x ==# y   = EQ
591                            | otherwise = GT
592
593 instance  Enum Int  where
594     toEnum   x = x
595     fromEnum x = x
596
597 #ifndef USE_FOLDR_BUILD
598     enumFrom     (I# c)          = eftInt c  1#
599     enumFromTo   (I# c1) (I# c2) = efttInt c1 1#  (># c2)
600     enumFromThen (I# c1) (I# c2) = eftInt c1 (c2 -# c1)
601
602     enumFromThenTo (I# c1) (I# c2) (I# c3)
603         | c1 <=# c2 = efttInt c1 (c2 -# c1) (># c3)
604         | otherwise = efttInt c1 (c2 -# c1) (<# c3)
605
606 #else
607     {-# INLINE enumFrom #-}
608     {-# INLINE enumFromTo #-}
609     enumFrom x           = build (\ c _ -> 
610         let g x = x `c` g (x `plusInt` 1) in g x)
611     enumFromTo x y       = build (\ c n ->
612         let g x = if x <= y then x `c` g (x `plusInt` 1) else n in g x)
613 #endif
614
615 efttInt :: Int# -> Int# -> (Int# -> Bool) -> [Int]
616 efttInt now step done
617   = go now
618   where
619     go now | done now  = []
620            | otherwise = I# now : go (now +# step)
621
622 eftInt :: Int# -> Int# -> [Int]
623 eftInt now step
624   = go now
625   where
626     go now = I# now : go (now +# step)
627
628
629 instance  Num Int  where
630     (+)    x y =  plusInt x y
631     (-)    x y =  minusInt x y
632     negate x   =  negateInt x
633     (*)    x y =  timesInt x y
634     abs    n   = if n `geInt` 0 then n else (negateInt n)
635
636     signum n | n `ltInt` 0 = negateInt 1
637              | n `eqInt` 0 = 0
638              | otherwise   = 1
639
640     fromInteger (J# a# s# d#)
641       = case (integer2Int# a# s# d#) of { i# -> I# i# }
642
643     fromInt n           = n
644
645 instance  Show Int  where
646     showsPrec p n = showSignedInt p n
647     showList ls   = showList__ (showsPrec 0)  ls
648 \end{code}
649
650
651 %*********************************************************
652 %*                                                      *
653 \subsection{Type @Integer@, @Float@, @Double@}
654 %*                                                      *
655 %*********************************************************
656
657 Just the type declarations.  If we don't actually use any @Integers@ we'd
658 rather not link the @Integer@ module at all; and the default-decl stuff
659 in the renamer tends to slurp in @Double@ regardless.
660
661 \begin{code}
662 data Float      = F# Float#
663 data Double     = D# Double#
664 data Integer    = J# Int# Int# ByteArray#
665 \end{code}
666
667
668 %*********************************************************
669 %*                                                      *
670 \subsection{The function type}
671 %*                                                      *
672 %*********************************************************
673
674 \begin{code}
675 instance Eval (a -> b) 
676
677 instance  Show (a -> b)  where
678     showsPrec p f  =  showString "<<function>>"
679     showList ls    = showList__ (showsPrec 0) ls
680
681
682 -- identity function
683 id                      :: a -> a
684 id x                    =  x
685
686 -- constant function
687 const                   :: a -> b -> a
688 const x _               =  x
689
690 -- function composition
691 {-# INLINE (.) #-}
692 {-# GENERATE_SPECS (.) a b c #-}
693 (.)       :: (b -> c) -> (a -> b) -> a -> c
694 (.) f g x = f (g x)
695
696 -- flip f  takes its (first) two arguments in the reverse order of f.
697 flip                    :: (a -> b -> c) -> b -> a -> c
698 flip f x y              =  f y x
699
700 -- right-associating infix application operator (useful in continuation-
701 -- passing style)
702 ($)                     :: (a -> b) -> a -> b
703 f $ x                   =  f x
704
705 -- until p f  yields the result of applying f until p holds.
706 until                   :: (a -> Bool) -> (a -> a) -> a -> a
707 until p f x | p x       =  x
708             | otherwise =  until p f (f x)
709
710 -- asTypeOf is a type-restricted version of const.  It is usually used
711 -- as an infix operator, and its typing forces its first argument
712 -- (which is usually overloaded) to have the same type as the second.
713 asTypeOf                :: a -> a -> a
714 asTypeOf                =  const
715 \end{code}
716
717
718 %*********************************************************
719 %*                                                      *
720 \subsection{Miscellaneous}
721 %*                                                      *
722 %*********************************************************
723
724
725 \begin{code}
726 data Lift a = Lift a
727 {-# GENERATE_SPECS data a :: Lift a #-}
728 \end{code}
729
730
731
732
733 %*********************************************************
734 %*                                                      *
735 \subsection{Support code for @Show@}
736 %*                                                      *
737 %*********************************************************
738
739 \begin{code}
740 shows           :: (Show a) => a -> ShowS
741 shows           =  showsPrec 0
742
743 show            :: (Show a) => a -> String
744 show x          =  shows x ""
745
746 showChar        :: Char -> ShowS
747 showChar        =  (:)
748
749 showString      :: String -> ShowS
750 showString      =  (++)
751
752 showParen       :: Bool -> ShowS -> ShowS
753 showParen b p   =  if b then showChar '(' . p . showChar ')' else p
754
755 {-# GENERATE_SPECS showList__ a #-}
756 showList__ :: (a -> ShowS) ->  [a] -> ShowS
757
758 showList__ showx []     = showString "[]"
759 showList__ showx (x:xs) = showChar '[' . showx x . showl xs
760   where
761     showl []     = showChar ']'
762     showl (x:xs) = showString ", " . showx x . showl xs
763
764 showSpace :: ShowS
765 showSpace = {-showChar ' '-} \ xs -> ' ' : xs
766 \end{code}
767
768 Code specific for characters
769
770 \begin{code}
771 showLitChar                :: Char -> ShowS
772 showLitChar c | c > '\DEL' =  showChar '\\' . protectEsc isDigit (shows (ord c))
773 showLitChar '\DEL'         =  showString "\\DEL"
774 showLitChar '\\'           =  showString "\\\\"
775 showLitChar c | c >= ' '   =  showChar c
776 showLitChar '\a'           =  showString "\\a"
777 showLitChar '\b'           =  showString "\\b"
778 showLitChar '\f'           =  showString "\\f"
779 showLitChar '\n'           =  showString "\\n"
780 showLitChar '\r'           =  showString "\\r"
781 showLitChar '\t'           =  showString "\\t"
782 showLitChar '\v'           =  showString "\\v"
783 showLitChar '\SO'          =  protectEsc (== 'H') (showString "\\SO")
784 showLitChar c              =  showString ('\\' : asciiTab!!ord c)
785
786 protectEsc p f             = f . cont
787                              where cont s@(c:_) | p c = "\\&" ++ s
788                                    cont s             = s
789
790 intToDigit :: Int -> Char
791 intToDigit i
792  | i >= 0  && i <=  9   =  toEnum (fromEnum '0' + i)
793  | i >= 10 && i <= 15   =  toEnum (fromEnum 'a' + i -10)
794  | otherwise            =  error "Char.intToDigit: not a digit" ++ show i)
795
796 \end{code}
797
798 Code specific for Ints.
799
800 \begin{code}
801 showSignedInt :: Int -> Int -> ShowS
802 showSignedInt p (I# n) r
803   = -- from HBC version; support code follows
804     if n <# 0# && p > 6 then '(':itos n++(')':r) else itos n ++ r
805
806 itos :: Int# -> String
807 itos n =
808     if n <# 0# then
809         if negateInt# n <# 0# then
810             -- n is minInt, a difficult number
811             itos (n `quotInt#` 10#) ++ itos' (negateInt# (n `remInt#` 10#)) []
812         else
813             '-':itos' (negateInt# n) []
814     else 
815         itos' n []
816   where
817     itos' :: Int# -> String -> String
818     itos' n cs = 
819         if n <# 10# then
820             C# (chr# (n +# ord# '0'#)) : cs
821         else 
822             itos' (n `quotInt#` 10#) (C# (chr# (n `remInt#` 10# +# ord# '0'#)) : cs)
823 \end{code}
824
825 %*********************************************************
826 %*                                                      *
827 \subsection{Numeric primops}
828 %*                                                      *
829 %*********************************************************
830
831 Definitions of the boxed PrimOps; these will be
832 used in the case of partial applications, etc.
833
834 \begin{code}
835 {-# INLINE eqInt #-}
836 {-# INLINE neInt #-}
837
838 plusInt (I# x) (I# y) = I# (x +# y)
839 minusInt(I# x) (I# y) = I# (x -# y)
840 timesInt(I# x) (I# y) = I# (x *# y)
841 quotInt (I# x) (I# y) = I# (quotInt# x y)
842 remInt  (I# x) (I# y) = I# (remInt# x y)
843 negateInt (I# x)      = I# (negateInt# x)
844 gtInt   (I# x) (I# y) = x ># y
845 geInt   (I# x) (I# y) = x >=# y
846 eqInt   (I# x) (I# y) = x ==# y
847 neInt   (I# x) (I# y) = x /=# y
848 ltInt   (I# x) (I# y) = x <# y
849 leInt   (I# x) (I# y) = x <=# y
850 \end{code}