{-# LANGUAGE DeriveDataTypeable #-}
-- |
-- Module      :  Data.Attoparsec.Number
-- Copyright   :  Bryan O'Sullivan 2007-2015
-- License     :  BSD3
--
-- Maintainer  :  [email protected]
-- 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
Number -> DataType
Number -> Constr
(forall b. Data b => b -> b) -> Number -> 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)
gmapMo :: forall (m :: * -> *).
MonadPlus m =>
(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 (m :: * -> *).
MonadPlus m =>
(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 (m :: * -> *).
Monad m =>
(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 :: forall u. Int -> (forall d. Data d => d -> u) -> Number -> u
$cgmapQi :: forall u. Int -> (forall d. Data d => d -> u) -> Number -> u
gmapQ :: forall u. (forall d. Data d => d -> u) -> Number -> [u]
$cgmapQ :: forall u. (forall d. Data d => d -> u) -> Number -> [u]
gmapQr :: forall r r'.
(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 :: forall r r'.
(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 (t :: * -> * -> *) (c :: * -> *).
Typeable t =>
(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 (t :: * -> *) (c :: * -> *).
Typeable t =>
(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 (c :: * -> *).
(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 (c :: * -> *).
(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
Data)
{-# DEPRECATED Number "Use Scientific instead." #-}

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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