{-# LANGUAGE CPP #-}
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeFamilies #-}
module Numeric.Rounded.Hardware.Interval
  ( Interval(..)
  , increasing
  , maxI
  , minI
  , powInt
  , null
  , inf
  , sup
  , width
  , widthUlp
  , hull
  , intersection
  ) where
import           Control.DeepSeq (NFData (..))
import           Control.Monad
import           Control.Monad.ST
import qualified Data.Array.Base as A
import           Data.Coerce
import           Data.Ix
import           Data.Primitive
import qualified Data.Vector.Generic as VG
import qualified Data.Vector.Generic.Mutable as VGM
import qualified Data.Vector.Unboxed as VU
import qualified Data.Vector.Unboxed.Mutable as VUM
import           GHC.Float (expm1, log1mexp, log1p, log1pexp)
import           GHC.Generics (Generic)
import           Numeric.Rounded.Hardware.Internal
import qualified Numeric.Rounded.Hardware.Interval.Class as C
import qualified Numeric.Rounded.Hardware.Interval.NonEmpty as NE
import           Prelude hiding (null)

data Interval a
  = I !(Rounded 'TowardNegInf a) !(Rounded 'TowardInf a)
  | Empty
  deriving (Int -> Interval a -> ShowS
forall a. Show a => Int -> Interval a -> ShowS
forall a. Show a => [Interval a] -> ShowS
forall a. Show a => Interval a -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [Interval a] -> ShowS
$cshowList :: forall a. Show a => [Interval a] -> ShowS
show :: Interval a -> String
$cshow :: forall a. Show a => Interval a -> String
showsPrec :: Int -> Interval a -> ShowS
$cshowsPrec :: forall a. Show a => Int -> Interval a -> ShowS
Show,forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
forall a x. Rep (Interval a) x -> Interval a
forall a x. Interval a -> Rep (Interval a) x
$cto :: forall a x. Rep (Interval a) x -> Interval a
$cfrom :: forall a x. Interval a -> Rep (Interval a) x
Generic)

instance NFData a => NFData (Interval a)

increasing :: (forall r. Rounding r => Rounded r a -> Rounded r a) -> Interval a -> Interval a
increasing :: forall a.
(forall (r :: RoundingMode).
 Rounding r =>
 Rounded r a -> Rounded r a)
-> Interval a -> Interval a
increasing forall (r :: RoundingMode).
Rounding r =>
Rounded r a -> Rounded r a
f (I Rounded 'TowardNegInf a
a Rounded 'TowardInf a
b) = forall a.
Rounded 'TowardNegInf a -> Rounded 'TowardInf a -> Interval a
I (forall (r :: RoundingMode).
Rounding r =>
Rounded r a -> Rounded r a
f Rounded 'TowardNegInf a
a) (forall (r :: RoundingMode).
Rounding r =>
Rounded r a -> Rounded r a
f Rounded 'TowardInf a
b)
increasing forall (r :: RoundingMode).
Rounding r =>
Rounded r a -> Rounded r a
_ Interval a
Empty   = forall a. Interval a
Empty
{-# INLINE increasing #-}

instance (Num a, RoundedRing a) => Num (Interval a) where
  + :: Interval a -> Interval a -> Interval a
(+) = forall a.
(Interval a -> Interval a -> Interval a)
-> Interval a -> Interval a -> Interval a
liftBinaryNE forall a. Num a => a -> a -> a
(+)
  (-) = forall a.
(Interval a -> Interval a -> Interval a)
-> Interval a -> Interval a -> Interval a
liftBinaryNE (-)
  negate :: Interval a -> Interval a
negate = forall a. (Interval a -> Interval a) -> Interval a -> Interval a
liftUnaryNE forall a. Num a => a -> a
negate
  * :: Interval a -> Interval a -> Interval a
(*) = forall a.
(Interval a -> Interval a -> Interval a)
-> Interval a -> Interval a -> Interval a
liftBinaryNE forall a. Num a => a -> a -> a
(*)
  abs :: Interval a -> Interval a
abs = forall a. (Interval a -> Interval a) -> Interval a -> Interval a
liftUnaryNE forall a. Num a => a -> a
abs
  signum :: Interval a -> Interval a
signum = forall a. (Interval a -> Interval a) -> Interval a -> Interval a
liftUnaryNE forall a. Num a => a -> a
signum
  fromInteger :: Integer -> Interval a
fromInteger Integer
x = case forall a.
RoundedRing a =>
Integer -> (Rounded 'TowardNegInf a, Rounded 'TowardInf a)
intervalFromInteger Integer
x of
                    (Rounded 'TowardNegInf a
y, Rounded 'TowardInf a
y') -> forall a.
Rounded 'TowardNegInf a -> Rounded 'TowardInf a -> Interval a
I Rounded 'TowardNegInf a
y Rounded 'TowardInf a
y'
  {-# INLINE (+) #-}
  {-# INLINE (-) #-}
  {-# INLINE negate #-}
  {-# INLINE (*) #-}
  {-# INLINE abs #-}
  {-# INLINE signum #-}
  {-# INLINE fromInteger #-}

instance (Num a, RoundedFractional a) => Fractional (Interval a) where
  recip :: Interval a -> Interval a
recip = forall a. (Interval a -> Interval a) -> Interval a -> Interval a
liftUnaryNE forall a. Fractional a => a -> a
recip
  / :: Interval a -> Interval a -> Interval a
(/) = forall a.
(Interval a -> Interval a -> Interval a)
-> Interval a -> Interval a -> Interval a
liftBinaryNE forall a. Fractional a => a -> a -> a
(/)
  fromRational :: Rational -> Interval a
fromRational Rational
x = case forall a.
RoundedFractional a =>
Rational -> (Rounded 'TowardNegInf a, Rounded 'TowardInf a)
intervalFromRational Rational
x of
                     (Rounded 'TowardNegInf a
y, Rounded 'TowardInf a
y') -> forall a.
Rounded 'TowardNegInf a -> Rounded 'TowardInf a -> Interval a
I Rounded 'TowardNegInf a
y Rounded 'TowardInf a
y'
  {-# INLINE recip #-}
  {-# INLINE (/) #-}
  {-# INLINE fromRational #-}

maxI :: Ord a => Interval a -> Interval a -> Interval a
maxI :: forall a. Ord a => Interval a -> Interval a -> Interval a
maxI (I Rounded 'TowardNegInf a
a Rounded 'TowardInf a
a') (I Rounded 'TowardNegInf a
b Rounded 'TowardInf a
b') = forall a.
Rounded 'TowardNegInf a -> Rounded 'TowardInf a -> Interval a
I (forall a. Ord a => a -> a -> a
max Rounded 'TowardNegInf a
a Rounded 'TowardNegInf a
b) (forall a. Ord a => a -> a -> a
max Rounded 'TowardInf a
a' Rounded 'TowardInf a
b')
maxI Interval a
_ Interval a
_               = forall a. Interval a
Empty
{-# SPECIALIZE maxI :: Interval Float -> Interval Float -> Interval Float #-}
{-# SPECIALIZE maxI :: Interval Double -> Interval Double -> Interval Double #-}

minI :: Ord a => Interval a -> Interval a -> Interval a
minI :: forall a. Ord a => Interval a -> Interval a -> Interval a
minI (I Rounded 'TowardNegInf a
a Rounded 'TowardInf a
a') (I Rounded 'TowardNegInf a
b Rounded 'TowardInf a
b') = forall a.
Rounded 'TowardNegInf a -> Rounded 'TowardInf a -> Interval a
I (forall a. Ord a => a -> a -> a
min Rounded 'TowardNegInf a
a Rounded 'TowardNegInf a
b) (forall a. Ord a => a -> a -> a
min Rounded 'TowardInf a
a' Rounded 'TowardInf a
b')
minI Interval a
_ Interval a
_               = forall a. Interval a
Empty
{-# SPECIALIZE minI :: Interval Float -> Interval Float -> Interval Float #-}
{-# SPECIALIZE minI :: Interval Double -> Interval Double -> Interval Double #-}

powInt :: (Ord a, Num a, RoundedRing a) => Interval a -> Int -> Interval a
powInt :: forall a.
(Ord a, Num a, RoundedRing a) =>
Interval a -> Int -> Interval a
powInt (I Rounded 'TowardNegInf a
a Rounded 'TowardInf a
a') Int
n | forall a. Integral a => a -> Bool
odd Int
n Bool -> Bool -> Bool
|| Rounded 'TowardNegInf a
0 forall a. Ord a => a -> a -> Bool
<= Rounded 'TowardNegInf a
a = forall a.
Rounded 'TowardNegInf a -> Rounded 'TowardInf a -> Interval a
I (Rounded 'TowardNegInf a
aforall a b. (Num a, Integral b) => a -> b -> a
^Int
n) (Rounded 'TowardInf a
a'forall a b. (Num a, Integral b) => a -> b -> a
^Int
n)
                  | Rounded 'TowardInf a
a' forall a. Ord a => a -> a -> Bool
<= Rounded 'TowardInf a
0 = forall a.
Rounded 'TowardNegInf a -> Rounded 'TowardInf a -> Interval a
I ((coerce :: forall a b. Coercible a b => a -> b
coerce (forall a. Num a => a -> a
abs Rounded 'TowardInf a
a'))forall a b. (Num a, Integral b) => a -> b -> a
^Int
n) ((coerce :: forall a b. Coercible a b => a -> b
coerce (forall a. Num a => a -> a
abs Rounded 'TowardNegInf a
a))forall a b. (Num a, Integral b) => a -> b -> a
^Int
n)
                  | Bool
otherwise = forall a.
Rounded 'TowardNegInf a -> Rounded 'TowardInf a -> Interval a
I Rounded 'TowardNegInf a
0 (forall a. Ord a => a -> a -> a
max ((coerce :: forall a b. Coercible a b => a -> b
coerce (forall a. Num a => a -> a
abs Rounded 'TowardNegInf a
a))forall a b. (Num a, Integral b) => a -> b -> a
^Int
n) (Rounded 'TowardInf a
a'forall a b. (Num a, Integral b) => a -> b -> a
^Int
n))
powInt Interval a
Empty Int
_ = forall a. Interval a
Empty
{-# SPECIALIZE powInt :: Interval Float -> Int -> Interval Float #-}
{-# SPECIALIZE powInt :: Interval Double -> Int -> Interval Double #-}

null :: Interval a -> Bool
null :: forall a. Interval a -> Bool
null Interval a
Empty = Bool
True
null Interval a
_     = Bool
False

inf :: Interval a -> Rounded 'TowardNegInf a
inf :: forall a. Interval a -> Rounded 'TowardNegInf a
inf (I Rounded 'TowardNegInf a
x Rounded 'TowardInf a
_) = Rounded 'TowardNegInf a
x
inf Interval a
_       = forall a. HasCallStack => String -> a
error String
"empty interval"

sup :: Interval a -> Rounded 'TowardInf a
sup :: forall a. Interval a -> Rounded 'TowardInf a
sup (I Rounded 'TowardNegInf a
_ Rounded 'TowardInf a
y) = Rounded 'TowardInf a
y
sup Interval a
_       = forall a. HasCallStack => String -> a
error String
"empty interval"

width :: (Num a, RoundedRing a) => Interval a -> Rounded 'TowardInf a
width :: forall a.
(Num a, RoundedRing a) =>
Interval a -> Rounded 'TowardInf a
width (I Rounded 'TowardNegInf a
x Rounded 'TowardInf a
y) = Rounded 'TowardInf a
y forall a. Num a => a -> a -> a
- coerce :: forall a b. Coercible a b => a -> b
coerce Rounded 'TowardNegInf a
x
width Interval a
Empty   = Rounded 'TowardInf a
0

widthUlp :: (RealFloat a) => Interval a -> Maybe Integer
widthUlp :: forall a. RealFloat a => Interval a -> Maybe Integer
widthUlp (I Rounded 'TowardNegInf a
x Rounded 'TowardInf a
y) = forall a. RealFloat a => a -> a -> Maybe Integer
distanceUlp (forall (r :: RoundingMode) a. Rounded r a -> a
getRounded Rounded 'TowardNegInf a
x) (forall (r :: RoundingMode) a. Rounded r a -> a
getRounded Rounded 'TowardInf a
y)
widthUlp Interval a
Empty   = forall a. a -> Maybe a
Just Integer
0

hull :: RoundedRing a => Interval a -> Interval a -> Interval a
hull :: forall a. RoundedRing a => Interval a -> Interval a -> Interval a
hull (I Rounded 'TowardNegInf a
x Rounded 'TowardInf a
y) (I Rounded 'TowardNegInf a
x' Rounded 'TowardInf a
y') = forall a.
Rounded 'TowardNegInf a -> Rounded 'TowardInf a -> Interval a
I (forall a. Ord a => a -> a -> a
min Rounded 'TowardNegInf a
x Rounded 'TowardNegInf a
x') (forall a. Ord a => a -> a -> a
max Rounded 'TowardInf a
y Rounded 'TowardInf a
y')
hull Interval a
Empty Interval a
v           = Interval a
v
hull Interval a
u Interval a
Empty           = Interval a
u

intersection :: RoundedRing a => Interval a -> Interval a -> Interval a
intersection :: forall a. RoundedRing a => Interval a -> Interval a -> Interval a
intersection (I Rounded 'TowardNegInf a
x Rounded 'TowardInf a
y) (I Rounded 'TowardNegInf a
x' Rounded 'TowardInf a
y') | forall (r :: RoundingMode) a. Rounded r a -> a
getRounded Rounded 'TowardNegInf a
x'' forall a. Ord a => a -> a -> Bool
<= forall (r :: RoundingMode) a. Rounded r a -> a
getRounded Rounded 'TowardInf a
y'' = forall a.
Rounded 'TowardNegInf a -> Rounded 'TowardInf a -> Interval a
I Rounded 'TowardNegInf a
x'' Rounded 'TowardInf a
y''
  where x'' :: Rounded 'TowardNegInf a
x'' = forall a. Ord a => a -> a -> a
max Rounded 'TowardNegInf a
x Rounded 'TowardNegInf a
x'
        y'' :: Rounded 'TowardInf a
y'' = forall a. Ord a => a -> a -> a
min Rounded 'TowardInf a
y Rounded 'TowardInf a
y'
intersection Interval a
_ Interval a
_ = forall a. Interval a
Empty

liftUnaryNE :: (NE.Interval a -> NE.Interval a) -> Interval a -> Interval a
liftUnaryNE :: forall a. (Interval a -> Interval a) -> Interval a -> Interval a
liftUnaryNE Interval a -> Interval a
f (I Rounded 'TowardNegInf a
x Rounded 'TowardInf a
x') = case Interval a -> Interval a
f (forall a.
Rounded 'TowardNegInf a -> Rounded 'TowardInf a -> Interval a
NE.I Rounded 'TowardNegInf a
x Rounded 'TowardInf a
x') of
                           NE.I Rounded 'TowardNegInf a
y Rounded 'TowardInf a
y' -> forall a.
Rounded 'TowardNegInf a -> Rounded 'TowardInf a -> Interval a
I Rounded 'TowardNegInf a
y Rounded 'TowardInf a
y'
liftUnaryNE Interval a -> Interval a
_f Interval a
Empty = forall a. Interval a
Empty
{-# INLINE [1] liftUnaryNE #-}

liftBinaryNE :: (NE.Interval a -> NE.Interval a -> NE.Interval a) -> Interval a -> Interval a -> Interval a
liftBinaryNE :: forall a.
(Interval a -> Interval a -> Interval a)
-> Interval a -> Interval a -> Interval a
liftBinaryNE Interval a -> Interval a -> Interval a
f (I Rounded 'TowardNegInf a
x Rounded 'TowardInf a
x') (I Rounded 'TowardNegInf a
y Rounded 'TowardInf a
y') = case Interval a -> Interval a -> Interval a
f (forall a.
Rounded 'TowardNegInf a -> Rounded 'TowardInf a -> Interval a
NE.I Rounded 'TowardNegInf a
x Rounded 'TowardInf a
x') (forall a.
Rounded 'TowardNegInf a -> Rounded 'TowardInf a -> Interval a
NE.I Rounded 'TowardNegInf a
y Rounded 'TowardInf a
y') of
                                     NE.I Rounded 'TowardNegInf a
z Rounded 'TowardInf a
z' -> forall a.
Rounded 'TowardNegInf a -> Rounded 'TowardInf a -> Interval a
I Rounded 'TowardNegInf a
z Rounded 'TowardInf a
z'
liftBinaryNE Interval a -> Interval a -> Interval a
_f Interval a
_ Interval a
_ = forall a. Interval a
Empty
{-# INLINE [1] liftBinaryNE #-}

instance (Num a, RoundedFractional a, RoundedSqrt a, Eq a, RealFloat a, RealFloatConstants a) => Floating (Interval a) where
  pi :: Interval a
pi = forall a.
Rounded 'TowardNegInf a -> Rounded 'TowardInf a -> Interval a
I forall a. RealFloatConstants a => Rounded 'TowardNegInf a
pi_down forall a. RealFloatConstants a => Rounded 'TowardInf a
pi_up
  exp :: Interval a -> Interval a
exp = forall a. (Interval a -> Interval a) -> Interval a -> Interval a
liftUnaryNE forall a. Floating a => a -> a
exp
  log :: Interval a -> Interval a
log = forall a. (Interval a -> Interval a) -> Interval a -> Interval a
liftUnaryNE forall a. Floating a => a -> a
log
  sqrt :: Interval a -> Interval a
sqrt = forall a. (Interval a -> Interval a) -> Interval a -> Interval a
liftUnaryNE forall a. Floating a => a -> a
sqrt
  ** :: Interval a -> Interval a -> Interval a
(**) = forall a.
(Interval a -> Interval a -> Interval a)
-> Interval a -> Interval a -> Interval a
liftBinaryNE forall a. Floating a => a -> a -> a
(**)
  logBase :: Interval a -> Interval a -> Interval a
logBase = forall a.
(Interval a -> Interval a -> Interval a)
-> Interval a -> Interval a -> Interval a
liftBinaryNE forall a. Floating a => a -> a -> a
logBase
  sin :: Interval a -> Interval a
sin = forall a. (Interval a -> Interval a) -> Interval a -> Interval a
liftUnaryNE forall a. Floating a => a -> a
sin
  cos :: Interval a -> Interval a
cos = forall a. (Interval a -> Interval a) -> Interval a -> Interval a
liftUnaryNE forall a. Floating a => a -> a
cos
  tan :: Interval a -> Interval a
tan = forall a. (Interval a -> Interval a) -> Interval a -> Interval a
liftUnaryNE forall a. Floating a => a -> a
tan
  asin :: Interval a -> Interval a
asin = forall a. (Interval a -> Interval a) -> Interval a -> Interval a
liftUnaryNE forall a. Floating a => a -> a
asin
  acos :: Interval a -> Interval a
acos = forall a. (Interval a -> Interval a) -> Interval a -> Interval a
liftUnaryNE forall a. Floating a => a -> a
acos
  atan :: Interval a -> Interval a
atan = forall a. (Interval a -> Interval a) -> Interval a -> Interval a
liftUnaryNE forall a. Floating a => a -> a
atan
  sinh :: Interval a -> Interval a
sinh = forall a. (Interval a -> Interval a) -> Interval a -> Interval a
liftUnaryNE forall a. Floating a => a -> a
sinh
  cosh :: Interval a -> Interval a
cosh = forall a. (Interval a -> Interval a) -> Interval a -> Interval a
liftUnaryNE forall a. Floating a => a -> a
cosh
  tanh :: Interval a -> Interval a
tanh = forall a. (Interval a -> Interval a) -> Interval a -> Interval a
liftUnaryNE forall a. Floating a => a -> a
tanh
  asinh :: Interval a -> Interval a
asinh = forall a. (Interval a -> Interval a) -> Interval a -> Interval a
liftUnaryNE forall a. Floating a => a -> a
asinh
  acosh :: Interval a -> Interval a
acosh = forall a. (Interval a -> Interval a) -> Interval a -> Interval a
liftUnaryNE forall a. Floating a => a -> a
acosh
  atanh :: Interval a -> Interval a
atanh = forall a. (Interval a -> Interval a) -> Interval a -> Interval a
liftUnaryNE forall a. Floating a => a -> a
atanh
  log1p :: Interval a -> Interval a
log1p = forall a. (Interval a -> Interval a) -> Interval a -> Interval a
liftUnaryNE forall a. Floating a => a -> a
log1p
  expm1 :: Interval a -> Interval a
expm1 = forall a. (Interval a -> Interval a) -> Interval a -> Interval a
liftUnaryNE forall a. Floating a => a -> a
expm1
  log1pexp :: Interval a -> Interval a
log1pexp = forall a. (Interval a -> Interval a) -> Interval a -> Interval a
liftUnaryNE forall a. Floating a => a -> a
log1pexp
  log1mexp :: Interval a -> Interval a
log1mexp = forall a. (Interval a -> Interval a) -> Interval a -> Interval a
liftUnaryNE forall a. Floating a => a -> a
log1mexp
  {-# INLINE exp #-}
  {-# INLINE log #-}
  {-# INLINE sqrt #-}
  {-# INLINE (**) #-}
  {-# INLINE logBase #-}
  {-# INLINE sin #-}
  {-# INLINE cos #-}
  {-# INLINE tan #-}
  {-# INLINE asin #-}
  {-# INLINE acos #-}
  {-# INLINE atan #-}
  {-# INLINE sinh #-}
  {-# INLINE cosh #-}
  {-# INLINE tanh #-}
  {-# INLINE asinh #-}
  {-# INLINE acosh #-}
  {-# INLINE atanh #-}
  {-# INLINE log1p #-}
  {-# INLINE expm1 #-}
  {-# INLINE log1pexp #-}
  {-# INLINE log1mexp #-}

instance (Num a, RoundedRing a, RealFloat a) => C.IsInterval (Interval a) where
  type EndPoint (Interval a) = a
  makeInterval :: Rounded 'TowardNegInf (EndPoint (Interval a))
-> Rounded 'TowardInf (EndPoint (Interval a)) -> Interval a
makeInterval = forall a.
Rounded 'TowardNegInf a -> Rounded 'TowardInf a -> Interval a
I
  width :: Interval a -> Rounded 'TowardInf (EndPoint (Interval a))
width = forall a.
(Num a, RoundedRing a) =>
Interval a -> Rounded 'TowardInf a
width
  withEndPoints :: (Rounded 'TowardNegInf (EndPoint (Interval a))
 -> Rounded 'TowardInf (EndPoint (Interval a)) -> Interval a)
-> Interval a -> Interval a
withEndPoints Rounded 'TowardNegInf (EndPoint (Interval a))
-> Rounded 'TowardInf (EndPoint (Interval a)) -> Interval a
f (I Rounded 'TowardNegInf a
x Rounded 'TowardInf a
y) = Rounded 'TowardNegInf (EndPoint (Interval a))
-> Rounded 'TowardInf (EndPoint (Interval a)) -> Interval a
f Rounded 'TowardNegInf a
x Rounded 'TowardInf a
y
  withEndPoints Rounded 'TowardNegInf (EndPoint (Interval a))
-> Rounded 'TowardInf (EndPoint (Interval a)) -> Interval a
_ Interval a
Empty   = forall a. Interval a
Empty
  hull :: Interval a -> Interval a -> Interval a
hull = forall a. RoundedRing a => Interval a -> Interval a -> Interval a
hull
  intersection :: Interval a -> Interval a -> Interval a
intersection = forall a. RoundedRing a => Interval a -> Interval a -> Interval a
intersection
  maybeIntersection :: Interval a -> Interval a -> Maybe (Interval a)
maybeIntersection Interval a
x Interval a
y = case forall a. RoundedRing a => Interval a -> Interval a -> Interval a
intersection Interval a
x Interval a
y of
                            Interval a
Empty -> forall a. Maybe a
Nothing
                            Interval a
z     -> forall a. a -> Maybe a
Just Interval a
z
  equalAsSet :: Interval a -> Interval a -> Bool
equalAsSet (I Rounded 'TowardNegInf a
x Rounded 'TowardInf a
y) (I Rounded 'TowardNegInf a
x' Rounded 'TowardInf a
y') = Rounded 'TowardNegInf a
x forall a. Eq a => a -> a -> Bool
== Rounded 'TowardNegInf a
x' Bool -> Bool -> Bool
&& Rounded 'TowardInf a
y forall a. Eq a => a -> a -> Bool
== Rounded 'TowardInf a
y'
  equalAsSet Interval a
Empty Interval a
Empty       = Bool
True
  equalAsSet Interval a
_ Interval a
_               = Bool
False
  subset :: Interval a -> Interval a -> Bool
subset (I Rounded 'TowardNegInf a
x Rounded 'TowardInf a
y) (I Rounded 'TowardNegInf a
x' Rounded 'TowardInf a
y') = Rounded 'TowardNegInf a
x' forall a. Ord a => a -> a -> Bool
<= Rounded 'TowardNegInf a
x Bool -> Bool -> Bool
&& Rounded 'TowardInf a
y forall a. Ord a => a -> a -> Bool
<= Rounded 'TowardInf a
y'
  subset Interval a
Empty Interval a
_           = Bool
True
  subset I{} Interval a
Empty         = Bool
False
  weaklyLess :: Interval a -> Interval a -> Bool
weaklyLess (I Rounded 'TowardNegInf a
x Rounded 'TowardInf a
y) (I Rounded 'TowardNegInf a
x' Rounded 'TowardInf a
y') = Rounded 'TowardNegInf a
x forall a. Ord a => a -> a -> Bool
<= Rounded 'TowardNegInf a
x' Bool -> Bool -> Bool
&& Rounded 'TowardInf a
y forall a. Ord a => a -> a -> Bool
<= Rounded 'TowardInf a
y'
  weaklyLess Interval a
Empty Interval a
Empty       = Bool
True
  weaklyLess Interval a
_ Interval a
_               = Bool
False
  precedes :: Interval a -> Interval a -> Bool
precedes (I Rounded 'TowardNegInf a
_ Rounded 'TowardInf a
y) (I Rounded 'TowardNegInf a
x' Rounded 'TowardInf a
_) = forall (r :: RoundingMode) a. Rounded r a -> a
getRounded Rounded 'TowardInf a
y forall a. Ord a => a -> a -> Bool
<= forall (r :: RoundingMode) a. Rounded r a -> a
getRounded Rounded 'TowardNegInf a
x'
  precedes Interval a
_ Interval a
_              = Bool
True
  interior :: Interval a -> Interval a -> Bool
interior (I Rounded 'TowardNegInf a
x Rounded 'TowardInf a
y) (I Rounded 'TowardNegInf a
x' Rounded 'TowardInf a
y') = forall (r :: RoundingMode) a. Rounded r a -> a
getRounded Rounded 'TowardNegInf a
x' forall {a}. RealFloat a => a -> a -> Bool
<# forall (r :: RoundingMode) a. Rounded r a -> a
getRounded Rounded 'TowardNegInf a
x Bool -> Bool -> Bool
&& forall (r :: RoundingMode) a. Rounded r a -> a
getRounded Rounded 'TowardInf a
y forall {a}. RealFloat a => a -> a -> Bool
<# forall (r :: RoundingMode) a. Rounded r a -> a
getRounded Rounded 'TowardInf a
y'
    where a
s <# :: a -> a -> Bool
<# a
t = a
s forall a. Ord a => a -> a -> Bool
< a
t Bool -> Bool -> Bool
|| (a
s forall a. Eq a => a -> a -> Bool
== a
t Bool -> Bool -> Bool
&& forall a. RealFloat a => a -> Bool
isInfinite a
s)
  interior Interval a
Empty Interval a
_ = Bool
True
  interior I{} Interval a
Empty = Bool
False
  strictLess :: Interval a -> Interval a -> Bool
strictLess (I Rounded 'TowardNegInf a
x Rounded 'TowardInf a
y) (I Rounded 'TowardNegInf a
x' Rounded 'TowardInf a
y') = forall (r :: RoundingMode) a. Rounded r a -> a
getRounded Rounded 'TowardNegInf a
x forall {a}. RealFloat a => a -> a -> Bool
<# forall (r :: RoundingMode) a. Rounded r a -> a
getRounded Rounded 'TowardNegInf a
x' Bool -> Bool -> Bool
&& forall (r :: RoundingMode) a. Rounded r a -> a
getRounded Rounded 'TowardInf a
y forall {a}. RealFloat a => a -> a -> Bool
<# forall (r :: RoundingMode) a. Rounded r a -> a
getRounded Rounded 'TowardInf a
y'
    where a
s <# :: a -> a -> Bool
<# a
t = a
s forall a. Ord a => a -> a -> Bool
< a
t Bool -> Bool -> Bool
|| (a
s forall a. Eq a => a -> a -> Bool
== a
t Bool -> Bool -> Bool
&& forall a. RealFloat a => a -> Bool
isInfinite a
s)
  strictLess Interval a
Empty Interval a
Empty = Bool
True
  strictLess Interval a
_ Interval a
_ = Bool
False
  strictPrecedes :: Interval a -> Interval a -> Bool
strictPrecedes (I Rounded 'TowardNegInf a
_ Rounded 'TowardInf a
y) (I Rounded 'TowardNegInf a
x' Rounded 'TowardInf a
_) = forall (r :: RoundingMode) a. Rounded r a -> a
getRounded Rounded 'TowardInf a
y forall a. Ord a => a -> a -> Bool
< forall (r :: RoundingMode) a. Rounded r a -> a
getRounded Rounded 'TowardNegInf a
x'
  strictPrecedes Interval a
_ Interval a
_              = Bool
True
  disjoint :: Interval a -> Interval a -> Bool
disjoint (I Rounded 'TowardNegInf a
x Rounded 'TowardInf a
y) (I Rounded 'TowardNegInf a
x' Rounded 'TowardInf a
y') = forall (r :: RoundingMode) a. Rounded r a -> a
getRounded Rounded 'TowardInf a
y forall a. Ord a => a -> a -> Bool
< forall (r :: RoundingMode) a. Rounded r a -> a
getRounded Rounded 'TowardNegInf a
x' Bool -> Bool -> Bool
|| forall (r :: RoundingMode) a. Rounded r a -> a
getRounded Rounded 'TowardInf a
y' forall a. Ord a => a -> a -> Bool
< forall (r :: RoundingMode) a. Rounded r a -> a
getRounded Rounded 'TowardNegInf a
x
  disjoint Interval a
_ Interval a
_ = Bool
True

--
-- Instance for Data.Vector.Unboxed.Unbox
--

newtype instance VUM.MVector s (Interval a) = MV_Interval (VUM.MVector s (a, a))
newtype instance VU.Vector (Interval a) = V_Interval (VU.Vector (a, a))

intervalToPair :: Fractional a => Interval a -> (a, a)
intervalToPair :: forall a. Fractional a => Interval a -> (a, a)
intervalToPair (I (Rounded a
x) (Rounded a
y)) = (a
x, a
y)
intervalToPair Interval a
Empty                       = (a
1forall a. Fractional a => a -> a -> a
/a
0, -a
1forall a. Fractional a => a -> a -> a
/a
0)
{-# INLINE intervalToPair #-}

pairToInterval :: Ord a => (a, a) -> Interval a
pairToInterval :: forall a. Ord a => (a, a) -> Interval a
pairToInterval (a
x, a
y) | a
y forall a. Ord a => a -> a -> Bool
< a
x = forall a. Interval a
Empty
                      | Bool
otherwise = forall a.
Rounded 'TowardNegInf a -> Rounded 'TowardInf a -> Interval a
I (forall (r :: RoundingMode) a. a -> Rounded r a
Rounded a
x) (forall (r :: RoundingMode) a. a -> Rounded r a
Rounded a
y)
{-# INLINE pairToInterval #-}

instance (VU.Unbox a, Ord a, Fractional a) => VGM.MVector VUM.MVector (Interval a) where
  basicLength :: forall s. MVector s (Interval a) -> Int
basicLength (MV_Interval MVector s (a, a)
mv) = forall (v :: * -> * -> *) a s. MVector v a => v s a -> Int
VGM.basicLength MVector s (a, a)
mv
  basicUnsafeSlice :: forall s.
Int -> Int -> MVector s (Interval a) -> MVector s (Interval a)
basicUnsafeSlice Int
i Int
l (MV_Interval MVector s (a, a)
mv) = forall s a. MVector s (a, a) -> MVector s (Interval a)
MV_Interval (forall (v :: * -> * -> *) a s.
MVector v a =>
Int -> Int -> v s a -> v s a
VGM.basicUnsafeSlice Int
i Int
l MVector s (a, a)
mv)
  basicOverlaps :: forall s. MVector s (Interval a) -> MVector s (Interval a) -> Bool
basicOverlaps (MV_Interval MVector s (a, a)
mv) (MV_Interval MVector s (a, a)
mv') = forall (v :: * -> * -> *) a s.
MVector v a =>
v s a -> v s a -> Bool
VGM.basicOverlaps MVector s (a, a)
mv MVector s (a, a)
mv'
  basicUnsafeNew :: forall s. Int -> ST s (MVector s (Interval a))
basicUnsafeNew Int
l = forall s a. MVector s (a, a) -> MVector s (Interval a)
MV_Interval forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall (v :: * -> * -> *) a s. MVector v a => Int -> ST s (v s a)
VGM.basicUnsafeNew Int
l
  basicInitialize :: forall s. MVector s (Interval a) -> ST s ()
basicInitialize (MV_Interval MVector s (a, a)
mv) = forall (v :: * -> * -> *) a s. MVector v a => v s a -> ST s ()
VGM.basicInitialize MVector s (a, a)
mv
  basicUnsafeReplicate :: forall s. Int -> Interval a -> ST s (MVector s (Interval a))
basicUnsafeReplicate Int
i Interval a
x = forall s a. MVector s (a, a) -> MVector s (Interval a)
MV_Interval forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall (v :: * -> * -> *) a s.
MVector v a =>
Int -> a -> ST s (v s a)
VGM.basicUnsafeReplicate Int
i (forall a. Fractional a => Interval a -> (a, a)
intervalToPair Interval a
x)
  basicUnsafeRead :: forall s. MVector s (Interval a) -> Int -> ST s (Interval a)
basicUnsafeRead (MV_Interval MVector s (a, a)
mv) Int
i = forall a. Ord a => (a, a) -> Interval a
pairToInterval forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall (v :: * -> * -> *) a s.
MVector v a =>
v s a -> Int -> ST s a
VGM.basicUnsafeRead MVector s (a, a)
mv Int
i
  basicUnsafeWrite :: forall s. MVector s (Interval a) -> Int -> Interval a -> ST s ()
basicUnsafeWrite (MV_Interval MVector s (a, a)
mv) Int
i Interval a
x = forall (v :: * -> * -> *) a s.
MVector v a =>
v s a -> Int -> a -> ST s ()
VGM.basicUnsafeWrite MVector s (a, a)
mv Int
i (forall a. Fractional a => Interval a -> (a, a)
intervalToPair Interval a
x)
  basicClear :: forall s. MVector s (Interval a) -> ST s ()
basicClear (MV_Interval MVector s (a, a)
mv) = forall (v :: * -> * -> *) a s. MVector v a => v s a -> ST s ()
VGM.basicClear MVector s (a, a)
mv
  basicSet :: forall s. MVector s (Interval a) -> Interval a -> ST s ()
basicSet (MV_Interval MVector s (a, a)
mv) Interval a
x = forall (v :: * -> * -> *) a s. MVector v a => v s a -> a -> ST s ()
VGM.basicSet MVector s (a, a)
mv (forall a. Fractional a => Interval a -> (a, a)
intervalToPair Interval a
x)
  basicUnsafeCopy :: forall s.
MVector s (Interval a) -> MVector s (Interval a) -> ST s ()
basicUnsafeCopy (MV_Interval MVector s (a, a)
mv) (MV_Interval MVector s (a, a)
mv') = forall (v :: * -> * -> *) a s.
MVector v a =>
v s a -> v s a -> ST s ()
VGM.basicUnsafeCopy MVector s (a, a)
mv MVector s (a, a)
mv'
  basicUnsafeMove :: forall s.
MVector s (Interval a) -> MVector s (Interval a) -> ST s ()
basicUnsafeMove (MV_Interval MVector s (a, a)
mv) (MV_Interval MVector s (a, a)
mv') = forall (v :: * -> * -> *) a s.
MVector v a =>
v s a -> v s a -> ST s ()
VGM.basicUnsafeMove MVector s (a, a)
mv MVector s (a, a)
mv'
  basicUnsafeGrow :: forall s.
MVector s (Interval a) -> Int -> ST s (MVector s (Interval a))
basicUnsafeGrow (MV_Interval MVector s (a, a)
mv) Int
n = forall s a. MVector s (a, a) -> MVector s (Interval a)
MV_Interval forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall (v :: * -> * -> *) a s.
MVector v a =>
v s a -> Int -> ST s (v s a)
VGM.basicUnsafeGrow MVector s (a, a)
mv Int
n
  {-# INLINE basicLength #-}
  {-# INLINE basicUnsafeSlice #-}
  {-# INLINE basicOverlaps #-}
  {-# INLINE basicUnsafeNew #-}
  {-# INLINE basicInitialize #-}
  {-# INLINE basicUnsafeReplicate #-}
  {-# INLINE basicUnsafeRead #-}
  {-# INLINE basicUnsafeWrite #-}
  {-# INLINE basicClear #-}
  {-# INLINE basicSet #-}
  {-# INLINE basicUnsafeCopy #-}
  {-# INLINE basicUnsafeMove #-}
  {-# INLINE basicUnsafeGrow #-}

instance (VU.Unbox a, Ord a, Fractional a) => VG.Vector VU.Vector (Interval a) where
  basicUnsafeFreeze :: forall s.
Mutable Vector s (Interval a) -> ST s (Vector (Interval a))
basicUnsafeFreeze (MV_Interval MVector s (a, a)
mv) = forall a. Vector (a, a) -> Vector (Interval a)
V_Interval forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall (v :: * -> *) a s. Vector v a => Mutable v s a -> ST s (v a)
VG.basicUnsafeFreeze MVector s (a, a)
mv
  basicUnsafeThaw :: forall s.
Vector (Interval a) -> ST s (Mutable Vector s (Interval a))
basicUnsafeThaw (V_Interval Vector (a, a)
v) = forall s a. MVector s (a, a) -> MVector s (Interval a)
MV_Interval forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall (v :: * -> *) a s. Vector v a => v a -> ST s (Mutable v s a)
VG.basicUnsafeThaw Vector (a, a)
v
  basicLength :: Vector (Interval a) -> Int
basicLength (V_Interval Vector (a, a)
v) = forall (v :: * -> *) a. Vector v a => v a -> Int
VG.basicLength Vector (a, a)
v
  basicUnsafeSlice :: Int -> Int -> Vector (Interval a) -> Vector (Interval a)
basicUnsafeSlice Int
i Int
l (V_Interval Vector (a, a)
v) = forall a. Vector (a, a) -> Vector (Interval a)
V_Interval (forall (v :: * -> *) a. Vector v a => Int -> Int -> v a -> v a
VG.basicUnsafeSlice Int
i Int
l Vector (a, a)
v)
  basicUnsafeIndexM :: Vector (Interval a) -> Int -> Box (Interval a)
basicUnsafeIndexM (V_Interval Vector (a, a)
v) Int
i = forall a. Ord a => (a, a) -> Interval a
pairToInterval forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall (v :: * -> *) a. Vector v a => v a -> Int -> Box a
VG.basicUnsafeIndexM Vector (a, a)
v Int
i
  basicUnsafeCopy :: forall s.
Mutable Vector s (Interval a) -> Vector (Interval a) -> ST s ()
basicUnsafeCopy (MV_Interval MVector s (a, a)
mv) (V_Interval Vector (a, a)
v) = forall (v :: * -> *) a s.
Vector v a =>
Mutable v s a -> v a -> ST s ()
VG.basicUnsafeCopy MVector s (a, a)
mv Vector (a, a)
v
  elemseq :: forall b. Vector (Interval a) -> Interval a -> b -> b
elemseq (V_Interval Vector (a, a)
_) Interval a
x b
y = Interval a
x seq :: forall a b. a -> b -> b
`seq` b
y
  {-# INLINE basicUnsafeFreeze #-}
  {-# INLINE basicUnsafeThaw #-}
  {-# INLINE basicLength #-}
  {-# INLINE basicUnsafeSlice #-}
  {-# INLINE basicUnsafeIndexM #-}
  {-# INLINE basicUnsafeCopy #-}
  {-# INLINE elemseq #-}

instance (VU.Unbox a, Ord a, Fractional a) => VU.Unbox (Interval a)

--
-- Instances for Data.Array.Unboxed
--

instance (Prim a, Ord a, Fractional a) => A.MArray (A.STUArray s) (Interval a) (ST s) where
  getBounds :: forall i. Ix i => STUArray s i (Interval a) -> ST s (i, i)
getBounds (A.STUArray i
l i
u Int
_ MutableByteArray# s
_) = forall (m :: * -> *) a. Monad m => a -> m a
return (i
l, i
u)
  getNumElements :: forall i. Ix i => STUArray s i (Interval a) -> ST s Int
getNumElements (A.STUArray i
_ i
_ Int
n MutableByteArray# s
_) = forall (m :: * -> *) a. Monad m => a -> m a
return Int
n
  -- newArray: Use default
  unsafeNewArray_ :: forall i. Ix i => (i, i) -> ST s (STUArray s i (Interval a))
unsafeNewArray_ = forall (a :: * -> * -> *) e (m :: * -> *) i.
(MArray a e m, Ix i) =>
(i, i) -> m (a i e)
A.newArray_
  newArray_ :: forall i. Ix i => (i, i) -> ST s (STUArray s i (Interval a))
newArray_ bounds :: (i, i)
bounds@(i
l,i
u) = do
    let n :: Int
n = forall a. Ix a => (a, a) -> Int
rangeSize (i, i)
bounds
    arr :: MutableByteArray s
arr@(MutableByteArray MutableByteArray# s
arr_) <- forall (m :: * -> *).
PrimMonad m =>
Int -> m (MutableByteArray (PrimState m))
newByteArray (Int
2 forall a. Num a => a -> a -> a
* forall a. Prim a => a -> Int
sizeOf (forall a. HasCallStack => a
undefined :: a) forall a. Num a => a -> a -> a
* Int
n)
    forall a (m :: * -> *).
(Prim a, PrimMonad m) =>
MutableByteArray (PrimState m) -> Int -> Int -> a -> m ()
setByteArray MutableByteArray s
arr Int
0 (Int
2 forall a. Num a => a -> a -> a
* Int
n) (a
0 :: a)
    forall (m :: * -> *) a. Monad m => a -> m a
return (forall s i e.
i -> i -> Int -> MutableByteArray# s -> STUArray s i e
A.STUArray i
l i
u Int
n MutableByteArray# s
arr_)
  unsafeRead :: forall i.
Ix i =>
STUArray s i (Interval a) -> Int -> ST s (Interval a)
unsafeRead (A.STUArray i
_ i
_ Int
_ MutableByteArray# s
byteArr) Int
i = do
    a
x <- forall a (m :: * -> *).
(Prim a, PrimMonad m) =>
MutableByteArray (PrimState m) -> Int -> m a
readByteArray (forall s. MutableByteArray# s -> MutableByteArray s
MutableByteArray MutableByteArray# s
byteArr) (Int
2 forall a. Num a => a -> a -> a
* Int
i)
    a
y <- forall a (m :: * -> *).
(Prim a, PrimMonad m) =>
MutableByteArray (PrimState m) -> Int -> m a
readByteArray (forall s. MutableByteArray# s -> MutableByteArray s
MutableByteArray MutableByteArray# s
byteArr) (Int
2 forall a. Num a => a -> a -> a
* Int
i forall a. Num a => a -> a -> a
+ Int
1)
    forall (m :: * -> *) a. Monad m => a -> m a
return (forall a. Ord a => (a, a) -> Interval a
pairToInterval (a
x, a
y))
  unsafeWrite :: forall i.
Ix i =>
STUArray s i (Interval a) -> Int -> Interval a -> ST s ()
unsafeWrite (A.STUArray i
_ i
_ Int
_ MutableByteArray# s
byteArr) Int
i Interval a
e = do
    let (a
x, a
y) = forall a. Fractional a => Interval a -> (a, a)
intervalToPair Interval a
e
    forall a (m :: * -> *).
(Prim a, PrimMonad m) =>
MutableByteArray (PrimState m) -> Int -> a -> m ()
writeByteArray (forall s. MutableByteArray# s -> MutableByteArray s
MutableByteArray MutableByteArray# s
byteArr) (Int
2 forall a. Num a => a -> a -> a
* Int
i) a
x
    forall a (m :: * -> *).
(Prim a, PrimMonad m) =>
MutableByteArray (PrimState m) -> Int -> a -> m ()
writeByteArray (forall s. MutableByteArray# s -> MutableByteArray s
MutableByteArray MutableByteArray# s
byteArr) (Int
2 forall a. Num a => a -> a -> a
* Int
i forall a. Num a => a -> a -> a
+ Int
1) a
y

instance (Prim a, Ord a, Fractional a) => A.IArray A.UArray (Interval a) where
  bounds :: forall i. Ix i => UArray i (Interval a) -> (i, i)
bounds (A.UArray i
l i
u Int
_ ByteArray#
_) = (i
l,i
u)
  numElements :: forall i. Ix i => UArray i (Interval a) -> Int
numElements (A.UArray i
_ i
_ Int
n ByteArray#
_) = Int
n
  unsafeArray :: forall i.
Ix i =>
(i, i) -> [(Int, Interval a)] -> UArray i (Interval a)
unsafeArray (i, i)
bounds [(Int, Interval a)]
el = forall a. (forall s. ST s a) -> a
runST forall a b. (a -> b) -> a -> b
$ do
    STUArray s i (Interval a)
marr <- forall (a :: * -> * -> *) e (m :: * -> *) i.
(MArray a e m, Ix i) =>
(i, i) -> m (a i e)
A.newArray_ (i, i)
bounds
    forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
t a -> (a -> m b) -> m ()
forM_ [(Int, Interval a)]
el forall a b. (a -> b) -> a -> b
$ \(Int
i,Interval a
e) -> forall (a :: * -> * -> *) e (m :: * -> *) i.
(MArray a e m, Ix i) =>
a i e -> Int -> e -> m ()
A.unsafeWrite STUArray s i (Interval a)
marr Int
i Interval a
e
    forall s i e. STUArray s i e -> ST s (UArray i e)
A.unsafeFreezeSTUArray STUArray s i (Interval a)
marr
  unsafeAt :: forall i. Ix i => UArray i (Interval a) -> Int -> Interval a
unsafeAt (A.UArray i
_ i
_ Int
_ ByteArray#
byteArr) Int
i =
    let x :: a
x = forall a. Prim a => ByteArray -> Int -> a
indexByteArray (ByteArray# -> ByteArray
ByteArray ByteArray#
byteArr) (Int
2 forall a. Num a => a -> a -> a
* Int
i)
        y :: a
y = forall a. Prim a => ByteArray -> Int -> a
indexByteArray (ByteArray# -> ByteArray
ByteArray ByteArray#
byteArr) (Int
2 forall a. Num a => a -> a -> a
* Int
i forall a. Num a => a -> a -> a
+ Int
1)
    in forall a. Ord a => (a, a) -> Interval a
pairToInterval (a
x, a
y)
  -- unsafeReplace, unsafeAccum, unsafeAccumArray: Use default

#if !MIN_VERSION_base(4, 16, 0)
{-# RULES
"fromIntegral/a->Interval Float"
  fromIntegral = \x -> case intervalFromIntegral x of (l, u) -> I l u :: Interval Float
"fromIntegral/a->Interval Double"
  fromIntegral = \x -> case intervalFromIntegral x of (l, u) -> I l u :: Interval Double
  #-}
#endif