[project @ 2002-06-06 16:01:37 by simonpj]
[ghc-base.git] / Numeric.hs
index 66a4f21..9f84fdb 100644 (file)
@@ -1,16 +1,14 @@
 {-# OPTIONS -fno-implicit-prelude #-}
 -----------------------------------------------------------------------------
--- 
+-- |
 -- Module      :  Numeric
 -- Copyright   :  (c) The University of Glasgow 2002
--- License     :  BSD-style (see the file libraries/core/LICENSE)
+-- License     :  BSD-style (see the file libraries/base/LICENSE)
 -- 
 -- Maintainer  :  libraries@haskell.org
 -- Stability   :  provisional
 -- Portability :  portable
 --
--- $Id: Numeric.hs,v 1.5 2002/02/12 10:52:18 simonmar Exp $
---
 -- Odds and ends, mostly functions for reading and showing
 -- RealFloat-like kind of values.
 --
@@ -55,12 +53,56 @@ import GHC.Float
 import GHC.Num
 import GHC.Show
 import Data.Maybe
+import Text.ParserCombinators.ReadP( ReadP, readP_to_S, pfail )
+import qualified Text.Read.Lex as L
 #endif
 
 #ifdef __HUGS__
 import Array
 #endif
 
+
+-- -----------------------------------------------------------------------------
+-- Reading
+
+readInt :: Num a => a -> (Char -> Bool) -> (Char -> Int) -> ReadS a
+readInt base isDigit valDigit = readP_to_S (L.readIntP base isDigit valDigit)
+
+readOct, readDec, readHex :: Num a => ReadS a
+readOct = readP_to_S L.readOctP
+readDec = readP_to_S L.readDecP
+readHex = readP_to_S L.readHexP 
+
+readFloat :: RealFrac a => ReadS a
+readFloat = readP_to_S readFloatP
+
+readFloatP :: RealFrac a => ReadP a
+readFloatP =
+  do tok <- L.lex
+     case tok of
+       L.Rat y  -> return (fromRational y)
+       L.Int i  -> return (fromInteger i)
+       other    -> pfail
+
+-- It's turgid to have readSigned work using list comprehensions,
+-- but it's specified as a ReadS to ReadS transformer
+-- With a bit of luck no one will use it.
+readSigned :: (Real a) => ReadS a -> ReadS a
+readSigned readPos = readParen False read'
+                    where read' r  = read'' r ++
+                                     (do
+                                       ("-",s) <- lex r
+                                       (x,t)   <- read'' s
+                                       return (-x,t))
+                          read'' r = do
+                              (str,s) <- lex r
+                              (n,"")  <- readPos str
+                              return (n,s)
+
+
+-- -----------------------------------------------------------------------------
+-- Showing
+
 #ifdef __GLASGOW_HASKELL__
 showInt :: Integral a => a -> ShowS
 showInt n cs