6371651372868dab0f74013b6d9ecc0a1fabebc2
[ghc-hetmet.git] / ghc / lib / exts / NumExts.lhs
1 %
2 % (c) The AQUA Project, Glasgow University, 1998
3 %
4
5 \section[NumExts]{Misc numeric bits}
6
7 \begin{code}
8 module NumExts
9
10        (
11          doubleToFloat   -- :: Double -> Float
12        , floatToDouble   -- :: Double -> Float
13
14        , showHex         -- :: Integral a => a -> ShowS
15        , showOct         -- :: Integral a => a -> ShowS
16        , showBin         -- :: Integral a => a -> ShowS
17
18          -- general purpose number->string converter.
19        , showIntAtBase   -- :: Integral a 
20                          -- => a                -- base
21                          -- -> (a -> Char)      -- digit to char
22                          -- -> a                -- number to show.
23                          -- -> ShowS
24        , showListWith    -- :: (a -> ShowS)
25                          -- -> [a]
26                          -- -> ShowS
27        ) where
28
29 import Char (ord, chr)
30 #ifdef __HUGS__
31 import PreludeBuiltin
32 ord_0 = ord '0'
33 #else
34 import PrelNum ( ord_0 )
35 import PrelShow( showList__ )
36 import GlaExts
37 #endif
38 \end{code}
39
40 \begin{code}
41 doubleToFloat :: Double -> Float
42 floatToDouble :: Float -> Double
43
44 #ifdef __HUGS__
45 doubleToFloat = primDoubleToFloat
46 floatToDouble = primFloatToDouble
47 #else
48 doubleToFloat (D# d#) = F# (double2Float# d#)
49 floatToDouble (F# f#) = D# (float2Double# f#)
50 #endif
51
52 #ifdef __HUGS__
53 showIntAtBase :: Integral a => a -> (a -> Char) -> a -> ShowS
54 showIntAtBase base toChr n r
55   | n < 0  = error ("NumExts.showIntAtBase: applied to negative number " ++ show n)
56   | otherwise = 
57     case quotRem n base of { (n', d) ->
58     let c = toChr d in
59     seq c $ -- stricter than necessary
60     let
61         r' = c : r
62     in
63     if n' == 0 then r' else showIntAtBase base toChr n' r'
64     }
65 #else
66 showIntAtBase :: Integral a => a -> (a -> Char) -> a -> ShowS
67 showIntAtBase base toChr n r
68   | n < 0  = error ("NumExts.showIntAtBase: applied to negative number " ++ show n)
69   | otherwise = 
70     case quotRem n base of { (n', d) ->
71     case toChr d        of { C# c# -> -- stricter than necessary
72     let
73         r' = C# c# : r
74     in
75     if n' == 0 then r' else showIntAtBase base toChr n' r'
76     }}
77 #endif
78
79 showHex :: Integral a => a -> ShowS
80 showHex n r = 
81  showString "0x" $
82  showIntAtBase 16 (toChrHex) n r
83  where  
84   toChrHex d
85     | d < 10    = chr (ord_0   + fromIntegral d)
86     | otherwise = chr (ord 'a' + fromIntegral (d - 10))
87
88 showOct :: Integral a => a -> ShowS
89 showOct n r = 
90  showString "0o" $
91  showIntAtBase 8 (toChrOct) n r
92  where toChrOct d = chr (ord_0   + fromIntegral d)
93
94 showBin :: Integral a => a -> ShowS
95 showBin n r = 
96  showString "0b" $
97  showIntAtBase 2 (toChrOct) n r
98  where toChrOct d = chr (ord_0 + fromIntegral d)
99 \end{code}
100
101 Easy enough to define by the user, but since it's
102 occasionally useful (when, say, printing out a 
103 list of hex values), we define and export it
104 from @NumExts@.
105
106 \begin{code}
107 showListWith :: (a -> ShowS) -> [a] -> ShowS 
108 showListWith = showList__
109 \end{code}