module Numeric.Data.Interval
(
IntervalBound (..),
Interval (MkInterval),
Internal.mkInterval,
mkIntervalTH,
Internal.unsafeInterval,
reallyUnsafeInterval,
unInterval,
O,
C,
N,
_MkInterval,
rmatching,
_Open,
_Closed,
_None,
)
where
import Data.Bifunctor (Bifunctor (first))
import Data.Singletons (SingI)
import GHC.TypeNats (Nat)
import Language.Haskell.TH (Code, Q)
import Language.Haskell.TH.Syntax (Lift)
import Numeric.Convert.Integer (FromInteger)
import Numeric.Data.Internal.Utils (rmatching)
import Numeric.Data.Internal.Utils qualified as Utils
import Numeric.Data.Interval.Internal
( Interval (MkInterval, UnsafeInterval),
IntervalBound (Closed, None, Open),
)
import Numeric.Data.Interval.Internal qualified as Internal
import Optics.Core (Prism', ReversedPrism', ReversibleOptic (re), prism)
unInterval :: Interval l r a -> a
unInterval :: forall (l :: IntervalBound) (r :: IntervalBound) a.
Interval l r a -> a
unInterval (UnsafeInterval a
x) = a
x
mkIntervalTH ::
forall l r a.
( FromInteger a,
Lift a,
Ord a,
SingI l,
SingI r,
Show a
) =>
a ->
Code Q (Interval l r a)
mkIntervalTH :: forall (l :: IntervalBound) (r :: IntervalBound) a.
(FromInteger a, Lift a, Ord a, SingI l, SingI r, Show a) =>
a -> Code Q (Interval l r a)
mkIntervalTH = Either String (Interval l r a) -> Code Q (Interval l r a)
forall a. Lift a => Either String a -> Code Q a
Utils.liftErrorTH (Either String (Interval l r a) -> Code Q (Interval l r a))
-> (a -> Either String (Interval l r a))
-> a
-> Code Q (Interval l r a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> Either String (Interval l r a)
forall (l :: IntervalBound) (r :: IntervalBound) a.
(FromInteger a, Ord a, Show a, SingI l, SingI r) =>
a -> Either String (Interval l r a)
Internal.mkInterval
{-# INLINEABLE mkIntervalTH #-}
reallyUnsafeInterval :: a -> Interval l r a
reallyUnsafeInterval :: forall a (l :: IntervalBound) (r :: IntervalBound).
a -> Interval l r a
reallyUnsafeInterval = a -> Interval l r a
forall (l :: IntervalBound) (r :: IntervalBound) a.
a -> Interval l r a
UnsafeInterval
{-# INLINEABLE reallyUnsafeInterval #-}
type O :: Nat -> IntervalBound
type O n = Open n
type C :: Nat -> IntervalBound
type C n = Closed n
type N :: IntervalBound
type N = None
_MkInterval ::
forall l r a.
( FromInteger a,
Ord a,
Show a,
SingI l,
SingI r
) =>
ReversedPrism' (Interval l r a) a
_MkInterval :: forall (l :: IntervalBound) (r :: IntervalBound) a.
(FromInteger a, Ord a, Show a, SingI l, SingI r) =>
ReversedPrism' (Interval l r a) a
_MkInterval = Optic A_Prism NoIx a a (Interval l r a) (Interval l r a)
-> Optic
(ReversedOptic A_Prism) NoIx (Interval l r a) (Interval l r a) a a
forall (is :: IxList) s t a b.
AcceptsEmptyIndices "re" is =>
Optic A_Prism is s t a b
-> Optic (ReversedOptic A_Prism) is b a t s
forall k (is :: IxList) s t a b.
(ReversibleOptic k, AcceptsEmptyIndices "re" is) =>
Optic k is s t a b -> Optic (ReversedOptic k) is b a t s
re ((Interval l r a -> a)
-> (a -> Either a (Interval l r a))
-> Optic A_Prism NoIx a a (Interval l r a) (Interval l r a)
forall b t s a. (b -> t) -> (s -> Either t a) -> Prism s t a b
prism Interval l r a -> a
forall (l :: IntervalBound) (r :: IntervalBound) a.
Interval l r a -> a
unInterval a -> Either a (Interval l r a)
forall {b} {l :: IntervalBound} {r :: IntervalBound}.
(FromInteger b, Ord b, Show b, SingI l, SingI r) =>
b -> Either b (Interval l r b)
g)
where
g :: b -> Either b (Interval l r b)
g b
x = (String -> b)
-> Either String (Interval l r b) -> Either b (Interval l r b)
forall a b c. (a -> b) -> Either a c -> Either b c
forall (p :: Type -> Type -> Type) a b c.
Bifunctor p =>
(a -> b) -> p a c -> p b c
first (b -> String -> b
forall a b. a -> b -> a
const b
x) (Either String (Interval l r b) -> Either b (Interval l r b))
-> (b -> Either String (Interval l r b))
-> b
-> Either b (Interval l r b)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. b -> Either String (Interval l r b)
forall (l :: IntervalBound) (r :: IntervalBound) a.
(FromInteger a, Ord a, Show a, SingI l, SingI r) =>
a -> Either String (Interval l r a)
Internal.mkInterval (b -> Either b (Interval l r b)) -> b -> Either b (Interval l r b)
forall a b. (a -> b) -> a -> b
$ b
x
{-# INLINEABLE _MkInterval #-}
_Open :: Prism' IntervalBound Nat
_Open :: Prism' IntervalBound Nat
_Open =
(Nat -> IntervalBound)
-> (IntervalBound -> Either IntervalBound Nat)
-> Prism' IntervalBound Nat
forall b t s a. (b -> t) -> (s -> Either t a) -> Prism s t a b
prism
Nat -> IntervalBound
Open
( \case
Open Nat
x -> Nat -> Either IntervalBound Nat
forall a b. b -> Either a b
Right Nat
x
IntervalBound
other -> IntervalBound -> Either IntervalBound Nat
forall a b. a -> Either a b
Left IntervalBound
other
)
{-# INLINEABLE _Open #-}
_Closed :: Prism' IntervalBound Nat
_Closed :: Prism' IntervalBound Nat
_Closed =
(Nat -> IntervalBound)
-> (IntervalBound -> Either IntervalBound Nat)
-> Prism' IntervalBound Nat
forall b t s a. (b -> t) -> (s -> Either t a) -> Prism s t a b
prism
Nat -> IntervalBound
Closed
( \case
Closed Nat
x -> Nat -> Either IntervalBound Nat
forall a b. b -> Either a b
Right Nat
x
IntervalBound
other -> IntervalBound -> Either IntervalBound Nat
forall a b. a -> Either a b
Left IntervalBound
other
)
{-# INLINEABLE _Closed #-}
_None :: Prism' IntervalBound ()
_None :: Prism' IntervalBound ()
_None =
(() -> IntervalBound)
-> (IntervalBound -> Either IntervalBound ())
-> Prism' IntervalBound ()
forall b t s a. (b -> t) -> (s -> Either t a) -> Prism s t a b
prism
(IntervalBound -> () -> IntervalBound
forall a b. a -> b -> a
const IntervalBound
None)
( \case
IntervalBound
None -> () -> Either IntervalBound ()
forall a b. b -> Either a b
Right ()
IntervalBound
other -> IntervalBound -> Either IntervalBound ()
forall a b. a -> Either a b
Left IntervalBound
other
)
{-# INLINEABLE _None #-}