1 {-# OPTIONS -fno-implicit-prelude #-}
2 -----------------------------------------------------------------------------
5 -- Copyright : (c) The University of Glasgow 2002
6 -- License : BSD-style (see the file libraries/base/LICENSE)
8 -- Maintainer : libraries@haskell.org
9 -- Stability : provisional
10 -- Portability : portable
12 -- Odds and ends, mostly functions for reading and showing
13 -- 'RealFloat'-like kind of values.
15 -----------------------------------------------------------------------------
21 showSigned, -- :: (Real a) => (a -> ShowS) -> Int -> a -> ShowS
23 showIntAtBase, -- :: Integral a => a -> (a -> Char) -> a -> ShowS
24 showInt, -- :: Integral a => a -> ShowS
25 showHex, -- :: Integral a => a -> ShowS
26 showOct, -- :: Integral a => a -> ShowS
28 showEFloat, -- :: (RealFloat a) => Maybe Int -> a -> ShowS
29 showFFloat, -- :: (RealFloat a) => Maybe Int -> a -> ShowS
30 showGFloat, -- :: (RealFloat a) => Maybe Int -> a -> ShowS
31 showFloat, -- :: (RealFloat a) => a -> ShowS
33 floatToDigits, -- :: (RealFloat a) => Integer -> a -> ([Int], Int)
37 -- | /NB:/ 'readInt' is the \'dual\' of 'showIntAtBase',
38 -- and 'readDec' is the \`dual\' of 'showInt'.
39 -- The inconsistent naming is a historical accident.
41 readSigned, -- :: (Real a) => ReadS a -> ReadS a
43 readInt, -- :: (Integral a) => a -> (Char -> Bool)
44 -- -> (Char -> Int) -> ReadS a
45 readDec, -- :: (Integral a) => ReadS a
46 readOct, -- :: (Integral a) => ReadS a
47 readHex, -- :: (Integral a) => ReadS a
49 readFloat, -- :: (RealFloat a) => ReadS a
51 lexDigits, -- :: ReadS String
55 fromRat, -- :: (RealFloat a) => Rational -> a
59 #ifdef __GLASGOW_HASKELL__
67 import Text.ParserCombinators.ReadP( ReadP, readP_to_S, pfail )
68 import qualified Text.Read.Lex as L
78 #ifdef __GLASGOW_HASKELL__
79 -- -----------------------------------------------------------------------------
82 -- | Reads an /unsigned/ 'Integral' value in an arbitrary base.
85 -> (Char -> Bool) -- ^ a predicate distinguishing valid digits in this base
86 -> (Char -> Int) -- ^ a function converting a valid digit character to an 'Int'
88 readInt base isDigit valDigit = readP_to_S (L.readIntP base isDigit valDigit)
90 -- | Read an unsigned number in octal notation.
91 readOct :: Num a => ReadS a
92 readOct = readP_to_S L.readOctP
94 -- | Read an unsigned number in decimal notation.
95 readDec :: Num a => ReadS a
96 readDec = readP_to_S L.readDecP
98 -- | Read an unsigned number in hexadecimal notation.
99 -- Both upper or lower case letters are allowed.
100 readHex :: Num a => ReadS a
101 readHex = readP_to_S L.readHexP
103 -- | Reads an /unsigned/ 'RealFrac' value,
104 -- expressed in decimal scientific notation.
105 readFloat :: RealFrac a => ReadS a
106 readFloat = readP_to_S readFloatP
108 readFloatP :: RealFrac a => ReadP a
112 L.Rat y -> return (fromRational y)
113 L.Int i -> return (fromInteger i)
116 -- It's turgid to have readSigned work using list comprehensions,
117 -- but it's specified as a ReadS to ReadS transformer
118 -- With a bit of luck no one will use it.
120 -- | Reads a /signed/ 'Real' value, given a reader for an unsigned value.
121 readSigned :: (Real a) => ReadS a -> ReadS a
122 readSigned readPos = readParen False read'
123 where read' r = read'' r ++
130 (n,"") <- readPos str
133 -- -----------------------------------------------------------------------------
136 -- | Show /non-negative/ 'Integral' numbers in base 10.
137 showInt :: Integral a => a -> ShowS
139 | n < 0 = error "Numeric.showInt: can't show negative numbers"
140 | otherwise = go n cs
143 | n < 10 = case unsafeChr (ord '0' + fromIntegral n) of
145 | otherwise = case unsafeChr (ord '0' + fromIntegral r) of
146 c@(C# _) -> go q (c:cs)
148 (q,r) = n `quotRem` 10
150 -- Controlling the format and precision of floats. The code that
151 -- implements the formatting itself is in @PrelNum@ to avoid
152 -- mutual module deps.
154 {-# SPECIALIZE showEFloat ::
155 Maybe Int -> Float -> ShowS,
156 Maybe Int -> Double -> ShowS #-}
157 {-# SPECIALIZE showFFloat ::
158 Maybe Int -> Float -> ShowS,
159 Maybe Int -> Double -> ShowS #-}
160 {-# SPECIALIZE showGFloat ::
161 Maybe Int -> Float -> ShowS,
162 Maybe Int -> Double -> ShowS #-}
164 -- | Show a signed 'RealFloat' value
165 -- using scientific (exponential) notation (e.g. @2.45e2@, @1.5e-3@).
167 -- In the call @'showEFloat' digs val@, if @digs@ is 'Nothing',
168 -- the value is shown to full precision; if @digs@ is @'Just' d@,
169 -- then at most @d@ digits after the decimal point are shown.
170 showEFloat :: (RealFloat a) => Maybe Int -> a -> ShowS
172 -- | Show a signed 'RealFloat' value
173 -- using standard decimal notation (e.g. @245000@, @0.0015@).
175 -- In the call @'showFFloat' digs val@, if @digs@ is 'Nothing',
176 -- the value is shown to full precision; if @digs@ is @'Just' d@,
177 -- then at most @d@ digits after the decimal point are shown.
178 showFFloat :: (RealFloat a) => Maybe Int -> a -> ShowS
180 -- | Show a signed 'RealFloat' value
181 -- using standard decimal notation for arguments whose absolute value lies
182 -- between @0.1@ and @9,999,999@, and scientific notation otherwise.
184 -- In the call @'showGFloat' digs val@, if @digs@ is 'Nothing',
185 -- the value is shown to full precision; if @digs@ is @'Just' d@,
186 -- then at most @d@ digits after the decimal point are shown.
187 showGFloat :: (RealFloat a) => Maybe Int -> a -> ShowS
189 showEFloat d x = showString (formatRealFloat FFExponent d x)
190 showFFloat d x = showString (formatRealFloat FFFixed d x)
191 showGFloat d x = showString (formatRealFloat FFGeneric d x)
192 #endif /* __GLASGOW_HASKELL__ */
194 -- ---------------------------------------------------------------------------
195 -- Integer printing functions
197 -- | Shows a /non-negative/ 'Integral' number using the base specified by the
198 -- first argument, and the character representation specified by the second.
199 showIntAtBase :: Integral a => a -> (Int -> Char) -> a -> ShowS
200 showIntAtBase base toChr n r
201 | base <= 1 = error ("Numeric.showIntAtBase: applied to unsupported base " ++ show base)
202 | n < 0 = error ("Numeric.showIntAtBase: applied to negative number " ++ show n)
203 | otherwise = showIt (quotRem n base) r
205 showIt (n,d) r = seq c $ -- stricter than necessary
208 _ -> showIt (quotRem n base) r'
210 c = toChr (fromIntegral d)
213 -- | Show /non-negative/ 'Integral' numbers in base 16.
214 showHex :: Integral a => a -> ShowS
215 showHex = showIntAtBase 16 intToDigit
217 -- | Show /non-negative/ 'Integral' numbers in base 8.
218 showOct :: Integral a => a -> ShowS
219 showOct = showIntAtBase 8 intToDigit