{-# OPTIONS_GHC -Wall #-}
{-# LANGUAGE DeriveDataTypeable #-}
module Data.ExtendedReal
( Extended (..)
, inf
, isFinite
, isInfinite
) where
import Prelude hiding (isInfinite)
import Control.DeepSeq
import Data.Data
import Data.Hashable
import Data.Typeable
data Extended r
= NegInf
| Finite !r
| PosInf
deriving (Eq (Extended r)
Eq (Extended r)
-> (Extended r -> Extended r -> Ordering)
-> (Extended r -> Extended r -> Bool)
-> (Extended r -> Extended r -> Bool)
-> (Extended r -> Extended r -> Bool)
-> (Extended r -> Extended r -> Bool)
-> (Extended r -> Extended r -> Extended r)
-> (Extended r -> Extended r -> Extended r)
-> Ord (Extended r)
Extended r -> Extended r -> Bool
Extended r -> Extended r -> Ordering
Extended r -> Extended r -> Extended r
forall a.
Eq a
-> (a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
forall r. Ord r => Eq (Extended r)
forall r. Ord r => Extended r -> Extended r -> Bool
forall r. Ord r => Extended r -> Extended r -> Ordering
forall r. Ord r => Extended r -> Extended r -> Extended r
min :: Extended r -> Extended r -> Extended r
$cmin :: forall r. Ord r => Extended r -> Extended r -> Extended r
max :: Extended r -> Extended r -> Extended r
$cmax :: forall r. Ord r => Extended r -> Extended r -> Extended r
>= :: Extended r -> Extended r -> Bool
$c>= :: forall r. Ord r => Extended r -> Extended r -> Bool
> :: Extended r -> Extended r -> Bool
$c> :: forall r. Ord r => Extended r -> Extended r -> Bool
<= :: Extended r -> Extended r -> Bool
$c<= :: forall r. Ord r => Extended r -> Extended r -> Bool
< :: Extended r -> Extended r -> Bool
$c< :: forall r. Ord r => Extended r -> Extended r -> Bool
compare :: Extended r -> Extended r -> Ordering
$ccompare :: forall r. Ord r => Extended r -> Extended r -> Ordering
$cp1Ord :: forall r. Ord r => Eq (Extended r)
Ord, Extended r -> Extended r -> Bool
(Extended r -> Extended r -> Bool)
-> (Extended r -> Extended r -> Bool) -> Eq (Extended r)
forall r. Eq r => Extended r -> Extended r -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: Extended r -> Extended r -> Bool
$c/= :: forall r. Eq r => Extended r -> Extended r -> Bool
== :: Extended r -> Extended r -> Bool
$c== :: forall r. Eq r => Extended r -> Extended r -> Bool
Eq, Int -> Extended r -> ShowS
[Extended r] -> ShowS
Extended r -> String
(Int -> Extended r -> ShowS)
-> (Extended r -> String)
-> ([Extended r] -> ShowS)
-> Show (Extended r)
forall r. Show r => Int -> Extended r -> ShowS
forall r. Show r => [Extended r] -> ShowS
forall r. Show r => Extended r -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [Extended r] -> ShowS
$cshowList :: forall r. Show r => [Extended r] -> ShowS
show :: Extended r -> String
$cshow :: forall r. Show r => Extended r -> String
showsPrec :: Int -> Extended r -> ShowS
$cshowsPrec :: forall r. Show r => Int -> Extended r -> ShowS
Show, ReadPrec [Extended r]
ReadPrec (Extended r)
Int -> ReadS (Extended r)
ReadS [Extended r]
(Int -> ReadS (Extended r))
-> ReadS [Extended r]
-> ReadPrec (Extended r)
-> ReadPrec [Extended r]
-> Read (Extended r)
forall r. Read r => ReadPrec [Extended r]
forall r. Read r => ReadPrec (Extended r)
forall r. Read r => Int -> ReadS (Extended r)
forall r. Read r => ReadS [Extended r]
forall a.
(Int -> ReadS a)
-> ReadS [a] -> ReadPrec a -> ReadPrec [a] -> Read a
readListPrec :: ReadPrec [Extended r]
$creadListPrec :: forall r. Read r => ReadPrec [Extended r]
readPrec :: ReadPrec (Extended r)
$creadPrec :: forall r. Read r => ReadPrec (Extended r)
readList :: ReadS [Extended r]
$creadList :: forall r. Read r => ReadS [Extended r]
readsPrec :: Int -> ReadS (Extended r)
$creadsPrec :: forall r. Read r => Int -> ReadS (Extended r)
Read, Typeable, Typeable (Extended r)
DataType
Constr
Typeable (Extended r)
-> (forall (c :: * -> *).
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> Extended r -> c (Extended r))
-> (forall (c :: * -> *).
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c (Extended r))
-> (Extended r -> Constr)
-> (Extended r -> DataType)
-> (forall (t :: * -> *) (c :: * -> *).
Typeable t =>
(forall d. Data d => c (t d)) -> Maybe (c (Extended r)))
-> (forall (t :: * -> * -> *) (c :: * -> *).
Typeable t =>
(forall d e. (Data d, Data e) => c (t d e))
-> Maybe (c (Extended r)))
-> ((forall b. Data b => b -> b) -> Extended r -> Extended r)
-> (forall r r'.
(r -> r' -> r)
-> r -> (forall d. Data d => d -> r') -> Extended r -> r)
-> (forall r r'.
(r' -> r -> r)
-> r -> (forall d. Data d => d -> r') -> Extended r -> r)
-> (forall u. (forall d. Data d => d -> u) -> Extended r -> [u])
-> (forall u.
Int -> (forall d. Data d => d -> u) -> Extended r -> u)
-> (forall (m :: * -> *).
Monad m =>
(forall d. Data d => d -> m d) -> Extended r -> m (Extended r))
-> (forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d) -> Extended r -> m (Extended r))
-> (forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d) -> Extended r -> m (Extended r))
-> Data (Extended r)
Extended r -> DataType
Extended r -> Constr
(forall d. Data d => c (t d)) -> Maybe (c (Extended r))
(forall b. Data b => b -> b) -> Extended r -> Extended r
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> Extended r -> c (Extended r)
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c (Extended r)
forall r. Data r => Typeable (Extended r)
forall r. Data r => Extended r -> DataType
forall r. Data r => Extended r -> Constr
forall r.
Data r =>
(forall b. Data b => b -> b) -> Extended r -> Extended r
forall r u.
Data r =>
Int -> (forall d. Data d => d -> u) -> Extended r -> u
forall r u.
Data r =>
(forall d. Data d => d -> u) -> Extended r -> [u]
forall r r r'.
Data r =>
(r -> r' -> r)
-> r -> (forall d. Data d => d -> r') -> Extended r -> r
forall r r r'.
Data r =>
(r' -> r -> r)
-> r -> (forall d. Data d => d -> r') -> Extended r -> r
forall r (m :: * -> *).
(Data r, Monad m) =>
(forall d. Data d => d -> m d) -> Extended r -> m (Extended r)
forall r (m :: * -> *).
(Data r, MonadPlus m) =>
(forall d. Data d => d -> m d) -> Extended r -> m (Extended r)
forall r (c :: * -> *).
Data r =>
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c (Extended r)
forall r (c :: * -> *).
Data r =>
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> Extended r -> c (Extended r)
forall r (t :: * -> *) (c :: * -> *).
(Data r, Typeable t) =>
(forall d. Data d => c (t d)) -> Maybe (c (Extended r))
forall r (t :: * -> * -> *) (c :: * -> *).
(Data r, Typeable t) =>
(forall d e. (Data d, Data e) => c (t d e))
-> Maybe (c (Extended r))
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) -> Extended r -> u
forall u. (forall d. Data d => d -> u) -> Extended r -> [u]
forall r r'.
(r -> r' -> r)
-> r -> (forall d. Data d => d -> r') -> Extended r -> r
forall r r'.
(r' -> r -> r)
-> r -> (forall d. Data d => d -> r') -> Extended r -> r
forall (m :: * -> *).
Monad m =>
(forall d. Data d => d -> m d) -> Extended r -> m (Extended r)
forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d) -> Extended r -> m (Extended r)
forall (c :: * -> *).
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c (Extended r)
forall (c :: * -> *).
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> Extended r -> c (Extended r)
forall (t :: * -> *) (c :: * -> *).
Typeable t =>
(forall d. Data d => c (t d)) -> Maybe (c (Extended r))
forall (t :: * -> * -> *) (c :: * -> *).
Typeable t =>
(forall d e. (Data d, Data e) => c (t d e))
-> Maybe (c (Extended r))
$cPosInf :: Constr
$cFinite :: Constr
$cNegInf :: Constr
$tExtended :: DataType
gmapMo :: (forall d. Data d => d -> m d) -> Extended r -> m (Extended r)
$cgmapMo :: forall r (m :: * -> *).
(Data r, MonadPlus m) =>
(forall d. Data d => d -> m d) -> Extended r -> m (Extended r)
gmapMp :: (forall d. Data d => d -> m d) -> Extended r -> m (Extended r)
$cgmapMp :: forall r (m :: * -> *).
(Data r, MonadPlus m) =>
(forall d. Data d => d -> m d) -> Extended r -> m (Extended r)
gmapM :: (forall d. Data d => d -> m d) -> Extended r -> m (Extended r)
$cgmapM :: forall r (m :: * -> *).
(Data r, Monad m) =>
(forall d. Data d => d -> m d) -> Extended r -> m (Extended r)
gmapQi :: Int -> (forall d. Data d => d -> u) -> Extended r -> u
$cgmapQi :: forall r u.
Data r =>
Int -> (forall d. Data d => d -> u) -> Extended r -> u
gmapQ :: (forall d. Data d => d -> u) -> Extended r -> [u]
$cgmapQ :: forall r u.
Data r =>
(forall d. Data d => d -> u) -> Extended r -> [u]
gmapQr :: (r' -> r -> r)
-> r -> (forall d. Data d => d -> r') -> Extended r -> r
$cgmapQr :: forall r r r'.
Data r =>
(r' -> r -> r)
-> r -> (forall d. Data d => d -> r') -> Extended r -> r
gmapQl :: (r -> r' -> r)
-> r -> (forall d. Data d => d -> r') -> Extended r -> r
$cgmapQl :: forall r r r'.
Data r =>
(r -> r' -> r)
-> r -> (forall d. Data d => d -> r') -> Extended r -> r
gmapT :: (forall b. Data b => b -> b) -> Extended r -> Extended r
$cgmapT :: forall r.
Data r =>
(forall b. Data b => b -> b) -> Extended r -> Extended r
dataCast2 :: (forall d e. (Data d, Data e) => c (t d e))
-> Maybe (c (Extended r))
$cdataCast2 :: forall r (t :: * -> * -> *) (c :: * -> *).
(Data r, Typeable t) =>
(forall d e. (Data d, Data e) => c (t d e))
-> Maybe (c (Extended r))
dataCast1 :: (forall d. Data d => c (t d)) -> Maybe (c (Extended r))
$cdataCast1 :: forall r (t :: * -> *) (c :: * -> *).
(Data r, Typeable t) =>
(forall d. Data d => c (t d)) -> Maybe (c (Extended r))
dataTypeOf :: Extended r -> DataType
$cdataTypeOf :: forall r. Data r => Extended r -> DataType
toConstr :: Extended r -> Constr
$ctoConstr :: forall r. Data r => Extended r -> Constr
gunfold :: (forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c (Extended r)
$cgunfold :: forall r (c :: * -> *).
Data r =>
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c (Extended r)
gfoldl :: (forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> Extended r -> c (Extended r)
$cgfoldl :: forall r (c :: * -> *).
Data r =>
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> Extended r -> c (Extended r)
$cp1Data :: forall r. Data r => Typeable (Extended r)
Data)
instance Bounded (Extended r) where
minBound :: Extended r
minBound = Extended r
forall r. Extended r
NegInf
maxBound :: Extended r
maxBound = Extended r
forall r. Extended r
PosInf
instance Functor Extended where
fmap :: (a -> b) -> Extended a -> Extended b
fmap a -> b
_ Extended a
NegInf = Extended b
forall r. Extended r
NegInf
fmap a -> b
f (Finite a
x) = b -> Extended b
forall r. r -> Extended r
Finite (a -> b
f a
x)
fmap a -> b
_ Extended a
PosInf = Extended b
forall r. Extended r
PosInf
instance NFData r => NFData (Extended r) where
rnf :: Extended r -> ()
rnf (Finite r
x) = r -> ()
forall a. NFData a => a -> ()
rnf r
x
rnf Extended r
_ = ()
instance Hashable r => Hashable (Extended r) where
hashWithSalt :: Int -> Extended r -> Int
hashWithSalt Int
s Extended r
NegInf = Int
s Int -> Int -> Int
forall a. Hashable a => Int -> a -> Int
`hashWithSalt` (Int
0::Int)
hashWithSalt Int
s (Finite r
x) = Int
s Int -> Int -> Int
forall a. Hashable a => Int -> a -> Int
`hashWithSalt` (Int
1::Int) Int -> r -> Int
forall a. Hashable a => Int -> a -> Int
`hashWithSalt` r
x
hashWithSalt Int
s Extended r
PosInf = Int
s Int -> Int -> Int
forall a. Hashable a => Int -> a -> Int
`hashWithSalt` (Int
2::Int)
inf :: Extended r
inf :: Extended r
inf = Extended r
forall r. Extended r
PosInf
isFinite :: Extended r -> Bool
isFinite :: Extended r -> Bool
isFinite (Finite r
_) = Bool
True
isFinite Extended r
_ = Bool
False
isInfinite :: Extended r -> Bool
isInfinite :: Extended r -> Bool
isInfinite = Bool -> Bool
not (Bool -> Bool) -> (Extended r -> Bool) -> Extended r -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Extended r -> Bool
forall r. Extended r -> Bool
isFinite
instance (Num r, Ord r) => Num (Extended r) where
Finite r
a + :: Extended r -> Extended r -> Extended r
+ Finite r
b = r -> Extended r
forall r. r -> Extended r
Finite (r
ar -> r -> r
forall a. Num a => a -> a -> a
+r
b)
Extended r
PosInf + Extended r
NegInf = String -> Extended r
forall a. HasCallStack => String -> a
error String
"PosInf + NegInf is undefined"
Extended r
NegInf + Extended r
PosInf = String -> Extended r
forall a. HasCallStack => String -> a
error String
"NegInf + PosInf is undefined"
Extended r
PosInf + Extended r
_ = Extended r
forall r. Extended r
PosInf
Extended r
_ + Extended r
PosInf = Extended r
forall r. Extended r
PosInf
Extended r
NegInf + Extended r
_ = Extended r
forall r. Extended r
NegInf
Extended r
_ + Extended r
NegInf = Extended r
forall r. Extended r
NegInf
Finite r
x1 * :: Extended r -> Extended r -> Extended r
* Extended r
e = r -> Extended r -> Extended r
forall r. (Num r, Ord r) => r -> Extended r -> Extended r
scale r
x1 Extended r
e
Extended r
e * Finite r
x2 = r -> Extended r -> Extended r
forall r. (Num r, Ord r) => r -> Extended r -> Extended r
scale r
x2 Extended r
e
Extended r
PosInf * Extended r
PosInf = Extended r
forall r. Extended r
PosInf
Extended r
PosInf * Extended r
NegInf = Extended r
forall r. Extended r
NegInf
Extended r
NegInf * Extended r
PosInf = Extended r
forall r. Extended r
NegInf
Extended r
NegInf * Extended r
NegInf = Extended r
forall r. Extended r
PosInf
negate :: Extended r -> Extended r
negate Extended r
NegInf = Extended r
forall r. Extended r
PosInf
negate (Finite r
x) = r -> Extended r
forall r. r -> Extended r
Finite (r -> r
forall a. Num a => a -> a
negate r
x)
negate Extended r
PosInf = Extended r
forall r. Extended r
NegInf
abs :: Extended r -> Extended r
abs Extended r
NegInf = Extended r
forall r. Extended r
PosInf
abs (Finite r
x) = r -> Extended r
forall r. r -> Extended r
Finite (r -> r
forall a. Num a => a -> a
abs r
x)
abs Extended r
PosInf = Extended r
forall r. Extended r
PosInf
signum :: Extended r -> Extended r
signum Extended r
NegInf = r -> Extended r
forall r. r -> Extended r
Finite (-r
1)
signum (Finite r
x) = r -> Extended r
forall r. r -> Extended r
Finite (r -> r
forall a. Num a => a -> a
signum r
x)
signum Extended r
PosInf = r -> Extended r
forall r. r -> Extended r
Finite r
1
fromInteger :: Integer -> Extended r
fromInteger = r -> Extended r
forall r. r -> Extended r
Finite (r -> Extended r) -> (Integer -> r) -> Integer -> Extended r
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Integer -> r
forall a. Num a => Integer -> a
fromInteger
instance (Fractional r, Ord r) => Fractional (Extended r) where
recip :: Extended r -> Extended r
recip (Finite r
x) = r -> Extended r
forall r. r -> Extended r
Finite (r
1r -> r -> r
forall a. Fractional a => a -> a -> a
/r
x)
recip Extended r
_ = r -> Extended r
forall r. r -> Extended r
Finite r
0
fromRational :: Rational -> Extended r
fromRational = r -> Extended r
forall r. r -> Extended r
Finite (r -> Extended r) -> (Rational -> r) -> Rational -> Extended r
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Rational -> r
forall a. Fractional a => Rational -> a
fromRational
scale :: (Num r, Ord r) => r -> Extended r -> Extended r
scale :: r -> Extended r -> Extended r
scale r
a Extended r
e = Extended r -> Extended r -> Extended r
seq Extended r
e (Extended r -> Extended r) -> Extended r -> Extended r
forall a b. (a -> b) -> a -> b
$
case r
a r -> r -> Ordering
forall a. Ord a => a -> a -> Ordering
`compare` r
0 of
Ordering
EQ -> r -> Extended r
forall r. r -> Extended r
Finite r
0
Ordering
GT ->
case Extended r
e of
Extended r
NegInf -> Extended r
forall r. Extended r
NegInf
Finite r
b -> r -> Extended r
forall r. r -> Extended r
Finite (r
ar -> r -> r
forall a. Num a => a -> a -> a
*r
b)
Extended r
PosInf -> Extended r
forall r. Extended r
PosInf
Ordering
LT ->
case Extended r
e of
Extended r
NegInf -> Extended r
forall r. Extended r
PosInf
Finite r
b -> r -> Extended r
forall r. r -> Extended r
Finite (r
ar -> r -> r
forall a. Num a => a -> a -> a
*r
b)
Extended r
PosInf -> Extended r
forall r. Extended r
NegInf