{-# LANGUAGE DeriveDataTypeable #-}
-- |
-- Module      :  Data.Attoparsec.Number
-- Copyright   :  Bryan O'Sullivan 2007-2015
-- License     :  BSD3
--
-- Maintainer  :  bos@serpentine.com
-- Stability   :  experimental
-- Portability :  unknown
--
-- This module is deprecated, and both the module and 'Number' type
-- will be removed in the next major release.  Use the
-- <http://hackage.haskell.org/package/scientific scientific> package
-- and the 'Data.Scientific.Scientific' type instead.
--
-- A simple number type, useful for parsing both exact and inexact
-- quantities without losing much precision.
module Data.Attoparsec.Number
    {-# DEPRECATED "This module will be removed in the next major release." #-}
    (
      Number(..)
    ) where

import Control.DeepSeq (NFData(rnf))
import Data.Data (Data)
import Data.Function (on)
import Data.Typeable (Typeable)

-- | A numeric type that can represent integers accurately, and
-- floating point numbers to the precision of a 'Double'.
--
-- /Note/: this type is deprecated, and will be removed in the next
-- major release.  Use the 'Data.Scientific.Scientific' type instead.
data Number = I !Integer
            | D {-# UNPACK #-} !Double
              deriving (Typeable, Typeable Number
DataType
Constr
Typeable Number =>
(forall (c :: * -> *).
 (forall d b. Data d => c (d -> b) -> d -> c b)
 -> (forall g. g -> c g) -> Number -> c Number)
-> (forall (c :: * -> *).
    (forall b r. Data b => c (b -> r) -> c r)
    -> (forall r. r -> c r) -> Constr -> c Number)
-> (Number -> Constr)
-> (Number -> DataType)
-> (forall (t :: * -> *) (c :: * -> *).
    Typeable t =>
    (forall d. Data d => c (t d)) -> Maybe (c Number))
-> (forall (t :: * -> * -> *) (c :: * -> *).
    Typeable t =>
    (forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c Number))
-> ((forall b. Data b => b -> b) -> Number -> Number)
-> (forall r r'.
    (r -> r' -> r)
    -> r -> (forall d. Data d => d -> r') -> Number -> r)
-> (forall r r'.
    (r' -> r -> r)
    -> r -> (forall d. Data d => d -> r') -> Number -> r)
-> (forall u. (forall d. Data d => d -> u) -> Number -> [u])
-> (forall u. Int -> (forall d. Data d => d -> u) -> Number -> u)
-> (forall (m :: * -> *).
    Monad m =>
    (forall d. Data d => d -> m d) -> Number -> m Number)
-> (forall (m :: * -> *).
    MonadPlus m =>
    (forall d. Data d => d -> m d) -> Number -> m Number)
-> (forall (m :: * -> *).
    MonadPlus m =>
    (forall d. Data d => d -> m d) -> Number -> m Number)
-> Data Number
Number -> DataType
Number -> Constr
(forall b. Data b => b -> b) -> Number -> Number
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> Number -> c Number
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c Number
forall a.
Typeable a =>
(forall (c :: * -> *).
 (forall d b. Data d => c (d -> b) -> d -> c b)
 -> (forall g. g -> c g) -> a -> c a)
-> (forall (c :: * -> *).
    (forall b r. Data b => c (b -> r) -> c r)
    -> (forall r. r -> c r) -> Constr -> c a)
-> (a -> Constr)
-> (a -> DataType)
-> (forall (t :: * -> *) (c :: * -> *).
    Typeable t =>
    (forall d. Data d => c (t d)) -> Maybe (c a))
-> (forall (t :: * -> * -> *) (c :: * -> *).
    Typeable t =>
    (forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c a))
-> ((forall b. Data b => b -> b) -> a -> a)
-> (forall r r'.
    (r -> r' -> r) -> r -> (forall d. Data d => d -> r') -> a -> r)
-> (forall r r'.
    (r' -> r -> r) -> r -> (forall d. Data d => d -> r') -> a -> r)
-> (forall u. (forall d. Data d => d -> u) -> a -> [u])
-> (forall u. Int -> (forall d. Data d => d -> u) -> a -> u)
-> (forall (m :: * -> *).
    Monad m =>
    (forall d. Data d => d -> m d) -> a -> m a)
-> (forall (m :: * -> *).
    MonadPlus m =>
    (forall d. Data d => d -> m d) -> a -> m a)
-> (forall (m :: * -> *).
    MonadPlus m =>
    (forall d. Data d => d -> m d) -> a -> m a)
-> Data a
forall u. Int -> (forall d. Data d => d -> u) -> Number -> u
forall u. (forall d. Data d => d -> u) -> Number -> [u]
forall r r'.
(r -> r' -> r) -> r -> (forall d. Data d => d -> r') -> Number -> r
forall r r'.
(r' -> r -> r) -> r -> (forall d. Data d => d -> r') -> Number -> r
forall (m :: * -> *).
Monad m =>
(forall d. Data d => d -> m d) -> Number -> m Number
forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d) -> Number -> m Number
forall (c :: * -> *).
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c Number
forall (c :: * -> *).
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> Number -> c Number
forall (t :: * -> *) (c :: * -> *).
Typeable t =>
(forall d. Data d => c (t d)) -> Maybe (c Number)
forall (t :: * -> * -> *) (c :: * -> *).
Typeable t =>
(forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c Number)
$cD :: Constr
$cI :: Constr
$tNumber :: DataType
gmapMo :: (forall d. Data d => d -> m d) -> Number -> m Number
$cgmapMo :: forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d) -> Number -> m Number
gmapMp :: (forall d. Data d => d -> m d) -> Number -> m Number
$cgmapMp :: forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d) -> Number -> m Number
gmapM :: (forall d. Data d => d -> m d) -> Number -> m Number
$cgmapM :: forall (m :: * -> *).
Monad m =>
(forall d. Data d => d -> m d) -> Number -> m Number
gmapQi :: Int -> (forall d. Data d => d -> u) -> Number -> u
$cgmapQi :: forall u. Int -> (forall d. Data d => d -> u) -> Number -> u
gmapQ :: (forall d. Data d => d -> u) -> Number -> [u]
$cgmapQ :: forall u. (forall d. Data d => d -> u) -> Number -> [u]
gmapQr :: (r' -> r -> r) -> r -> (forall d. Data d => d -> r') -> Number -> r
$cgmapQr :: forall r r'.
(r' -> r -> r) -> r -> (forall d. Data d => d -> r') -> Number -> r
gmapQl :: (r -> r' -> r) -> r -> (forall d. Data d => d -> r') -> Number -> r
$cgmapQl :: forall r r'.
(r -> r' -> r) -> r -> (forall d. Data d => d -> r') -> Number -> r
gmapT :: (forall b. Data b => b -> b) -> Number -> Number
$cgmapT :: (forall b. Data b => b -> b) -> Number -> Number
dataCast2 :: (forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c Number)
$cdataCast2 :: forall (t :: * -> * -> *) (c :: * -> *).
Typeable t =>
(forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c Number)
dataCast1 :: (forall d. Data d => c (t d)) -> Maybe (c Number)
$cdataCast1 :: forall (t :: * -> *) (c :: * -> *).
Typeable t =>
(forall d. Data d => c (t d)) -> Maybe (c Number)
dataTypeOf :: Number -> DataType
$cdataTypeOf :: Number -> DataType
toConstr :: Number -> Constr
$ctoConstr :: Number -> Constr
gunfold :: (forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c Number
$cgunfold :: forall (c :: * -> *).
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c Number
gfoldl :: (forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> Number -> c Number
$cgfoldl :: forall (c :: * -> *).
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> Number -> c Number
$cp1Data :: Typeable Number
Data)
{-# DEPRECATED Number "Use Scientific instead." #-}

instance Show Number where
    show :: Number -> String
show (I a :: Integer
a) = Integer -> String
forall a. Show a => a -> String
show Integer
a
    show (D a :: Double
a) = Double -> String
forall a. Show a => a -> String
show Double
a

instance NFData Number where
    rnf :: Number -> ()
rnf (I _) = ()
    rnf (D _) = ()
    {-# INLINE rnf #-}

binop :: (Integer -> Integer -> a) -> (Double -> Double -> a)
      -> Number -> Number -> a
binop :: (Integer -> Integer -> a)
-> (Double -> Double -> a) -> Number -> Number -> a
binop _ d :: Double -> Double -> a
d (D a :: Double
a) (D b :: Double
b) = Double -> Double -> a
d Double
a Double
b
binop i :: Integer -> Integer -> a
i _ (I a :: Integer
a) (I b :: Integer
b) = Integer -> Integer -> a
i Integer
a Integer
b
binop _ d :: Double -> Double -> a
d (D a :: Double
a) (I b :: Integer
b) = Double -> Double -> a
d Double
a (Integer -> Double
forall a b. (Integral a, Num b) => a -> b
fromIntegral Integer
b)
binop _ d :: Double -> Double -> a
d (I a :: Integer
a) (D b :: Double
b) = Double -> Double -> a
d (Integer -> Double
forall a b. (Integral a, Num b) => a -> b
fromIntegral Integer
a) Double
b
{-# INLINE binop #-}

instance Eq Number where
    == :: Number -> Number -> Bool
(==) = (Integer -> Integer -> Bool)
-> (Double -> Double -> Bool) -> Number -> Number -> Bool
forall a.
(Integer -> Integer -> a)
-> (Double -> Double -> a) -> Number -> Number -> a
binop Integer -> Integer -> Bool
forall a. Eq a => a -> a -> Bool
(==) Double -> Double -> Bool
forall a. Eq a => a -> a -> Bool
(==)
    {-# INLINE (==) #-}

    /= :: Number -> Number -> Bool
(/=) = (Integer -> Integer -> Bool)
-> (Double -> Double -> Bool) -> Number -> Number -> Bool
forall a.
(Integer -> Integer -> a)
-> (Double -> Double -> a) -> Number -> Number -> a
binop Integer -> Integer -> Bool
forall a. Eq a => a -> a -> Bool
(/=) Double -> Double -> Bool
forall a. Eq a => a -> a -> Bool
(/=)
    {-# INLINE (/=) #-}

instance Ord Number where
    < :: Number -> Number -> Bool
(<) = (Integer -> Integer -> Bool)
-> (Double -> Double -> Bool) -> Number -> Number -> Bool
forall a.
(Integer -> Integer -> a)
-> (Double -> Double -> a) -> Number -> Number -> a
binop Integer -> Integer -> Bool
forall a. Ord a => a -> a -> Bool
(<) Double -> Double -> Bool
forall a. Ord a => a -> a -> Bool
(<)
    {-# INLINE (<) #-}

    <= :: Number -> Number -> Bool
(<=) = (Integer -> Integer -> Bool)
-> (Double -> Double -> Bool) -> Number -> Number -> Bool
forall a.
(Integer -> Integer -> a)
-> (Double -> Double -> a) -> Number -> Number -> a
binop Integer -> Integer -> Bool
forall a. Ord a => a -> a -> Bool
(<=) Double -> Double -> Bool
forall a. Ord a => a -> a -> Bool
(<=)
    {-# INLINE (<=) #-}

    > :: Number -> Number -> Bool
(>) = (Integer -> Integer -> Bool)
-> (Double -> Double -> Bool) -> Number -> Number -> Bool
forall a.
(Integer -> Integer -> a)
-> (Double -> Double -> a) -> Number -> Number -> a
binop Integer -> Integer -> Bool
forall a. Ord a => a -> a -> Bool
(>) Double -> Double -> Bool
forall a. Ord a => a -> a -> Bool
(>)
    {-# INLINE (>) #-}

    >= :: Number -> Number -> Bool
(>=) = (Integer -> Integer -> Bool)
-> (Double -> Double -> Bool) -> Number -> Number -> Bool
forall a.
(Integer -> Integer -> a)
-> (Double -> Double -> a) -> Number -> Number -> a
binop Integer -> Integer -> Bool
forall a. Ord a => a -> a -> Bool
(>=) Double -> Double -> Bool
forall a. Ord a => a -> a -> Bool
(>=)
    {-# INLINE (>=) #-}

    compare :: Number -> Number -> Ordering
compare = (Integer -> Integer -> Ordering)
-> (Double -> Double -> Ordering) -> Number -> Number -> Ordering
forall a.
(Integer -> Integer -> a)
-> (Double -> Double -> a) -> Number -> Number -> a
binop Integer -> Integer -> Ordering
forall a. Ord a => a -> a -> Ordering
compare Double -> Double -> Ordering
forall a. Ord a => a -> a -> Ordering
compare
    {-# INLINE compare #-}

instance Num Number where
    + :: Number -> Number -> Number
(+) = (Integer -> Integer -> Number)
-> (Double -> Double -> Number) -> Number -> Number -> Number
forall a.
(Integer -> Integer -> a)
-> (Double -> Double -> a) -> Number -> Number -> a
binop (((Integer -> Number
I(Integer -> Number) -> Integer -> Number
forall a b. (a -> b) -> a -> b
$!)(Integer -> Number) -> (Integer -> Integer) -> Integer -> Number
forall b c a. (b -> c) -> (a -> b) -> a -> c
.) ((Integer -> Integer) -> Integer -> Number)
-> (Integer -> Integer -> Integer) -> Integer -> Integer -> Number
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
(+)) (((Double -> Number
D(Double -> Number) -> Double -> Number
forall a b. (a -> b) -> a -> b
$!)(Double -> Number) -> (Double -> Double) -> Double -> Number
forall b c a. (b -> c) -> (a -> b) -> a -> c
.) ((Double -> Double) -> Double -> Number)
-> (Double -> Double -> Double) -> Double -> Double -> Number
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Double -> Double -> Double
forall a. Num a => a -> a -> a
(+))
    {-# INLINE (+) #-}

    (-) = (Integer -> Integer -> Number)
-> (Double -> Double -> Number) -> Number -> Number -> Number
forall a.
(Integer -> Integer -> a)
-> (Double -> Double -> a) -> Number -> Number -> a
binop (((Integer -> Number
I(Integer -> Number) -> Integer -> Number
forall a b. (a -> b) -> a -> b
$!)(Integer -> Number) -> (Integer -> Integer) -> Integer -> Number
forall b c a. (b -> c) -> (a -> b) -> a -> c
.) ((Integer -> Integer) -> Integer -> Number)
-> (Integer -> Integer -> Integer) -> Integer -> Integer -> Number
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (-)) (((Double -> Number
D(Double -> Number) -> Double -> Number
forall a b. (a -> b) -> a -> b
$!)(Double -> Number) -> (Double -> Double) -> Double -> Number
forall b c a. (b -> c) -> (a -> b) -> a -> c
.) ((Double -> Double) -> Double -> Number)
-> (Double -> Double -> Double) -> Double -> Double -> Number
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (-))
    {-# INLINE (-) #-}

    * :: Number -> Number -> Number
(*) = (Integer -> Integer -> Number)
-> (Double -> Double -> Number) -> Number -> Number -> Number
forall a.
(Integer -> Integer -> a)
-> (Double -> Double -> a) -> Number -> Number -> a
binop (((Integer -> Number
I(Integer -> Number) -> Integer -> Number
forall a b. (a -> b) -> a -> b
$!)(Integer -> Number) -> (Integer -> Integer) -> Integer -> Number
forall b c a. (b -> c) -> (a -> b) -> a -> c
.) ((Integer -> Integer) -> Integer -> Number)
-> (Integer -> Integer -> Integer) -> Integer -> Integer -> Number
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
(*)) (((Double -> Number
D(Double -> Number) -> Double -> Number
forall a b. (a -> b) -> a -> b
$!)(Double -> Number) -> (Double -> Double) -> Double -> Number
forall b c a. (b -> c) -> (a -> b) -> a -> c
.) ((Double -> Double) -> Double -> Number)
-> (Double -> Double -> Double) -> Double -> Double -> Number
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Double -> Double -> Double
forall a. Num a => a -> a -> a
(*))
    {-# INLINE (*) #-}

    abs :: Number -> Number
abs (I a :: Integer
a) = Integer -> Number
I (Integer -> Number) -> Integer -> Number
forall a b. (a -> b) -> a -> b
$! Integer -> Integer
forall a. Num a => a -> a
abs Integer
a
    abs (D a :: Double
a) = Double -> Number
D (Double -> Number) -> Double -> Number
forall a b. (a -> b) -> a -> b
$! Double -> Double
forall a. Num a => a -> a
abs Double
a
    {-# INLINE abs #-}

    negate :: Number -> Number
negate (I a :: Integer
a) = Integer -> Number
I (Integer -> Number) -> Integer -> Number
forall a b. (a -> b) -> a -> b
$! Integer -> Integer
forall a. Num a => a -> a
negate Integer
a
    negate (D a :: Double
a) = Double -> Number
D (Double -> Number) -> Double -> Number
forall a b. (a -> b) -> a -> b
$! Double -> Double
forall a. Num a => a -> a
negate Double
a
    {-# INLINE negate #-}

    signum :: Number -> Number
signum (I a :: Integer
a) = Integer -> Number
I (Integer -> Number) -> Integer -> Number
forall a b. (a -> b) -> a -> b
$! Integer -> Integer
forall a. Num a => a -> a
signum Integer
a
    signum (D a :: Double
a) = Double -> Number
D (Double -> Number) -> Double -> Number
forall a b. (a -> b) -> a -> b
$! Double -> Double
forall a. Num a => a -> a
signum Double
a
    {-# INLINE signum #-}

    fromInteger :: Integer -> Number
fromInteger = (Integer -> Number
I(Integer -> Number) -> Integer -> Number
forall a b. (a -> b) -> a -> b
$!) (Integer -> Number) -> (Integer -> Integer) -> Integer -> Number
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Integer -> Integer
forall a. Num a => Integer -> a
fromInteger
    {-# INLINE fromInteger #-}

instance Real Number where
    toRational :: Number -> Rational
toRational (I a :: Integer
a) = Integer -> Rational
forall a b. (Integral a, Num b) => a -> b
fromIntegral Integer
a
    toRational (D a :: Double
a) = Double -> Rational
forall a. Real a => a -> Rational
toRational Double
a
    {-# INLINE toRational #-}

instance Fractional Number where
    fromRational :: Rational -> Number
fromRational = (Double -> Number
D(Double -> Number) -> Double -> Number
forall a b. (a -> b) -> a -> b
$!) (Double -> Number) -> (Rational -> Double) -> Rational -> Number
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Rational -> Double
forall a. Fractional a => Rational -> a
fromRational
    {-# INLINE fromRational #-}

    / :: Number -> Number -> Number
(/) = (Integer -> Integer -> Number)
-> (Double -> Double -> Number) -> Number -> Number -> Number
forall a.
(Integer -> Integer -> a)
-> (Double -> Double -> a) -> Number -> Number -> a
binop (((Double -> Number
D(Double -> Number) -> Double -> Number
forall a b. (a -> b) -> a -> b
$!)(Double -> Number) -> (Double -> Double) -> Double -> Number
forall b c a. (b -> c) -> (a -> b) -> a -> c
.) ((Double -> Double) -> Double -> Number)
-> (Double -> Double -> Double) -> Double -> Double -> Number
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Double -> Double -> Double
forall a. Fractional a => a -> a -> a
(/) (Double -> Double -> Number)
-> (Integer -> Double) -> Integer -> Integer -> Number
forall b c a. (b -> b -> c) -> (a -> b) -> a -> a -> c
`on` Integer -> Double
forall a b. (Integral a, Num b) => a -> b
fromIntegral)
                (((Double -> Number
D(Double -> Number) -> Double -> Number
forall a b. (a -> b) -> a -> b
$!)(Double -> Number) -> (Double -> Double) -> Double -> Number
forall b c a. (b -> c) -> (a -> b) -> a -> c
.) ((Double -> Double) -> Double -> Number)
-> (Double -> Double -> Double) -> Double -> Double -> Number
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Double -> Double -> Double
forall a. Fractional a => a -> a -> a
(/))
    {-# INLINE (/) #-}

    recip :: Number -> Number
recip (I a :: Integer
a) = Double -> Number
D (Double -> Number) -> Double -> Number
forall a b. (a -> b) -> a -> b
$! Double -> Double
forall a. Fractional a => a -> a
recip (Integer -> Double
forall a b. (Integral a, Num b) => a -> b
fromIntegral Integer
a)
    recip (D a :: Double
a) = Double -> Number
D (Double -> Number) -> Double -> Number
forall a b. (a -> b) -> a -> b
$! Double -> Double
forall a. Fractional a => a -> a
recip Double
a
    {-# INLINE recip #-}

instance RealFrac Number where
    properFraction :: Number -> (b, Number)
properFraction (I a :: Integer
a) = (Integer -> b
forall a b. (Integral a, Num b) => a -> b
fromIntegral Integer
a,0)
    properFraction (D a :: Double
a) = case Double -> (b, Double)
forall a b. (RealFrac a, Integral b) => a -> (b, a)
properFraction Double
a of
                             (i :: b
i,d :: Double
d) -> (b
i,Double -> Number
D Double
d)
    {-# INLINE properFraction #-}
    truncate :: Number -> b
truncate (I a :: Integer
a) = Integer -> b
forall a b. (Integral a, Num b) => a -> b
fromIntegral Integer
a
    truncate (D a :: Double
a) = Double -> b
forall a b. (RealFrac a, Integral b) => a -> b
truncate Double
a
    {-# INLINE truncate #-}
    round :: Number -> b
round (I a :: Integer
a) = Integer -> b
forall a b. (Integral a, Num b) => a -> b
fromIntegral Integer
a
    round (D a :: Double
a) = Double -> b
forall a b. (RealFrac a, Integral b) => a -> b
round Double
a
    {-# INLINE round #-}
    ceiling :: Number -> b
ceiling (I a :: Integer
a) = Integer -> b
forall a b. (Integral a, Num b) => a -> b
fromIntegral Integer
a
    ceiling (D a :: Double
a) = Double -> b
forall a b. (RealFrac a, Integral b) => a -> b
ceiling Double
a
    {-# INLINE ceiling #-}
    floor :: Number -> b
floor (I a :: Integer
a) = Integer -> b
forall a b. (Integral a, Num b) => a -> b
fromIntegral Integer
a
    floor (D a :: Double
a) = Double -> b
forall a b. (RealFrac a, Integral b) => a -> b
floor Double
a
    {-# INLINE floor #-}