Safe Haskell | None |
---|---|
Language | Haskell2010 |
Closed intervals in a space given by an arbitrary functor.
Synopsis
- data Interval f a = Interval {}
- inf_ :: Lens' (Interval f a) (f a)
- sup_ :: Lens' (Interval f a) (f a)
- (...) :: Applicative f => a -> a -> Interval f a
- point :: f a -> Interval f a
- diameter :: (Applicative f, Num a) => Interval f a -> f a
- midpoint :: (Applicative f, Fractional a) => Interval f a -> f a
- uncurryI :: (f a -> f a -> b) -> Interval f a -> b
- liftI :: Applicative f => (a -> a -> b) -> Interval f a -> f b
- enum :: Enum (f a) => Interval f a -> [f a]
- liftEnum :: (Applicative f, Enum a) => Interval f a -> f [a]
- toUnit :: (Applicative f, Fractional a) => Interval f a -> f a -> f a
- fromUnit :: (Applicative f, Fractional a) => Interval f a -> f a -> f a
- transform :: (Applicative f, Fractional a) => Interval f a -> Interval f a -> f a -> f a
- lerp :: (Applicative f, Num a) => a -> Interval f a -> f a
- wrap :: (Applicative f, Real a) => Interval f a -> f a -> f a
- foldMapInterval :: Semigroup s => (f a -> s) -> Interval f a -> s
- mapInterval :: (f a -> g b) -> Interval f a -> Interval g b
- traverseInterval :: Applicative m => (f a -> m (g b)) -> Interval f a -> m (Interval g b)
- member :: (Applicative f, Foldable f, Ord a) => f a -> Interval f a -> Bool
- isValid :: (Applicative f, Foldable f, Ord a) => Interval f a -> Bool
- isEmpty :: (Applicative f, Foldable f, Ord a) => Interval f a -> Bool
- isPoint :: (Applicative f, Foldable f, Eq a) => Interval f a -> Bool
- isSubintervalOf :: (Applicative f, Foldable f, Ord a) => Interval f a -> Interval f a -> Bool
- isSuperintervalOf :: (Applicative f, Foldable f, Ord a) => Interval f a -> Interval f a -> Bool
- isProperSubintervalOf :: (Applicative f, Foldable f, Ord a) => Interval f a -> Interval f a -> Bool
- isProperSuperintervalOf :: (Applicative f, Foldable f, Ord a) => Interval f a -> Interval f a -> Bool
- intersects :: (Applicative f, Foldable f, Ord a) => Interval f a -> Interval f a -> Bool
- newtype Union f a = Union {}
- union :: forall f a. (Applicative f, Ord a) => Interval f a -> Interval f a -> Interval f a
- newtype Intersection f a = Intersection {
- getIntersection :: Interval f a
- intersection :: forall f a. (Applicative f, Ord a) => Interval f a -> Interval f a -> Interval f a
Documentation
f
-dimensional intervals with coordinates in a
.
Instances
MonadTrans Interval Source # | |
Defined in Data.Functor.Interval | |
Monad f => Monad (Interval f) Source # | |
Functor f => Functor (Interval f) Source # | Maps over each coordinate of the endpoints. See |
Applicative f => Applicative (Interval f) Source # | |
Defined in Data.Functor.Interval | |
Foldable f => Foldable (Interval f) Source # | Folds over each coordinate of the endpoints. See |
Defined in Data.Functor.Interval fold :: Monoid m => Interval f m -> m # foldMap :: Monoid m => (a -> m) -> Interval f a -> m # foldMap' :: Monoid m => (a -> m) -> Interval f a -> m # foldr :: (a -> b -> b) -> b -> Interval f a -> b # foldr' :: (a -> b -> b) -> b -> Interval f a -> b # foldl :: (b -> a -> b) -> b -> Interval f a -> b # foldl' :: (b -> a -> b) -> b -> Interval f a -> b # foldr1 :: (a -> a -> a) -> Interval f a -> a # foldl1 :: (a -> a -> a) -> Interval f a -> a # toList :: Interval f a -> [a] # null :: Interval f a -> Bool # length :: Interval f a -> Int # elem :: Eq a => a -> Interval f a -> Bool # maximum :: Ord a => Interval f a -> a # minimum :: Ord a => Interval f a -> a # | |
Traversable f => Traversable (Interval f) Source # | Traverses over each coordinate of the endpoints. See |
Defined in Data.Functor.Interval | |
Generic1 (Interval f :: Type -> Type) Source # | |
Eq (f a) => Eq (Interval f a) Source # | |
(Applicative f, Floating a) => Floating (Interval f a) Source # | |
Defined in Data.Functor.Interval exp :: Interval f a -> Interval f a # log :: Interval f a -> Interval f a # sqrt :: Interval f a -> Interval f a # (**) :: Interval f a -> Interval f a -> Interval f a # logBase :: Interval f a -> Interval f a -> Interval f a # sin :: Interval f a -> Interval f a # cos :: Interval f a -> Interval f a # tan :: Interval f a -> Interval f a # asin :: Interval f a -> Interval f a # acos :: Interval f a -> Interval f a # atan :: Interval f a -> Interval f a # sinh :: Interval f a -> Interval f a # cosh :: Interval f a -> Interval f a # tanh :: Interval f a -> Interval f a # asinh :: Interval f a -> Interval f a # acosh :: Interval f a -> Interval f a # atanh :: Interval f a -> Interval f a # log1p :: Interval f a -> Interval f a # expm1 :: Interval f a -> Interval f a # | |
(Applicative f, Fractional a) => Fractional (Interval f a) Source # | |
(Applicative f, Num a) => Num (Interval f a) Source # | |
Defined in Data.Functor.Interval (+) :: Interval f a -> Interval f a -> Interval f a # (-) :: Interval f a -> Interval f a -> Interval f a # (*) :: Interval f a -> Interval f a -> Interval f a # negate :: Interval f a -> Interval f a # abs :: Interval f a -> Interval f a # signum :: Interval f a -> Interval f a # fromInteger :: Integer -> Interval f a # | |
Ord (f a) => Ord (Interval f a) Source # | The ordering is defined by |
Defined in Data.Functor.Interval | |
Show (f a) => Show (Interval f a) Source # | |
Generic (Interval f a) Source # | |
(Applicative f, Ord a) => Semigroup (Interval f a) Source # | |
type Rep1 (Interval f :: Type -> Type) Source # | |
Defined in Data.Functor.Interval type Rep1 (Interval f :: Type -> Type) = D1 ('MetaData "Interval" "Data.Functor.Interval" "interval-functor-0.0.0.0-inplace" 'False) (C1 ('MetaCons "Interval" 'PrefixI 'True) (S1 ('MetaSel ('Just "inf") 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) (Rec1 f) :*: S1 ('MetaSel ('Just "sup") 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) (Rec1 f))) | |
type Rep (Interval f a) Source # | |
Defined in Data.Functor.Interval type Rep (Interval f a) = D1 ('MetaData "Interval" "Data.Functor.Interval" "interval-functor-0.0.0.0-inplace" 'False) (C1 ('MetaCons "Interval" 'PrefixI 'True) (S1 ('MetaSel ('Just "inf") 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) (Rec0 (f a)) :*: S1 ('MetaSel ('Just "sup") 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) (Rec0 (f a)))) |
Lenses
Constructors
(...) :: Applicative f => a -> a -> Interval f a infix 3 Source #
Construct a square interval in f
dimensions from the given coordinates.
>>>
0...1 :: Interval Identity Int
Identity 0...Identity 1
>>>
0...1 :: Interval V2 Int
V2 0 0...V2 1 1
point :: f a -> Interval f a Source #
Construct a point (or degenerate) interval from the given endpoint.
>>>
point (V2 0 1)
V2 0 1...V2 0 1
Eliminators
diameter :: (Applicative f, Num a) => Interval f a -> f a Source #
Compute the diameter of an interval, in f
dimensions, defined as the absolute difference between the endpoints.
Note that the diameter of closed point intervals is zero, so this is not the interval’s cardinality.
midpoint :: (Applicative f, Fractional a) => Interval f a -> f a Source #
Compute the midpoint of an interval, halfway between the endpoints.
midpoint (point x) = x
uncurryI :: (f a -> f a -> b) -> Interval f a -> b Source #
Apply a function to the endpoints of an interval.
>>>
uncurryI (,) (Interval a b)
(a, b)
liftI :: Applicative f => (a -> a -> b) -> Interval f a -> f b Source #
Lift a function over the coordinates in each dimension of f
.
>>>
liftI (+) (Interval (V2 1 2) (V2 3 4))
V2 4 6
Enumerations
enum :: Enum (f a) => Interval f a -> [f a] Source #
Enumerate the points in f
between the interval’s endpoints.
>>>
enum (0...1 :: Interval Identity Int)
[Identity 0, Identity 1]
liftEnum :: (Applicative f, Enum a) => Interval f a -> f [a] Source #
Enumerate the coordinates in a
between the interval’s endpoints along each dimension of f
.
>>>
liftEnum (Interval (V2 1 2) (V2 1 3))
V2 [1] [2, 3]
Transformations
toUnit :: (Applicative f, Fractional a) => Interval f a -> f a -> f a Source #
Linearly transform a point in f
from a non-point interval of f
to the unit interval.
toUnit = (`transform` 0...1)
toUnit i . fromUnit i = id
fromUnit :: (Applicative f, Fractional a) => Interval f a -> f a -> f a Source #
Linearly transform a point in f
from the unit interval to an interval of f
.
fromUnit = transform (0...1)
fromUnit i . toUnit i = id
transform :: (Applicative f, Fractional a) => Interval f a -> Interval f a -> f a -> f a Source #
Transform a point linearly between the subspaces described by non-point intervals.
transform i j (inf i) = inf j
transform i j (midpoint i) = midpoint j
transform i j (sup i) = sup j
transform i i = id
transform i j . transform j i = id
transform (0...1) = fromUnit
(`transform` 0...1) = toUnit
lerp :: (Applicative f, Num a) => a -> Interval f a -> f a Source #
Linearly interpolate between the endpoints of an interval.
lerp 0 = inf
lerp 0.5 = midpoint
lerp 1 = sup
lerp t i = fromUnit i (pure t)
wrap :: (Applicative f, Real a) => Interval f a -> f a -> f a Source #
Clamp a point in f
to the given interval, wrapping out-of-bounds values around.
e.g. to wrap angles in radians to the interval [-pi, pi]:
>>>
wrap (-pi...pi) (pi + x)
Identity (-pi + x)
wrap i (lerp t i) = lerp (snd (properFraction t)) i
Traversals
foldMapInterval :: Semigroup s => (f a -> s) -> Interval f a -> s Source #
Map and fold over an interval’s endpoints.
Where foldMap
only folds over the individual coordinates, foldMapInterval
can interpret the structure of the space as well.
>>>
foldMapInterval (\ p -> [p]) (Interval a b)
[a, b]
foldMap f = foldMapInterval (foldMap f)
mapInterval :: (f a -> g b) -> Interval f a -> Interval g b Source #
Map over an interval’s endpoints.
Where fmap
only maps over the individual coordinates, mapInterval
can change the space as well.
>>>
mapInterval (\ (V2 x y) -> V3 x y 0) (Interval (V2 1 2) (V2 3 4))
V3 1 2 0...V3 3 4 0
fmap f = mapInterval (fmap f)
traverseInterval :: Applicative m => (f a -> m (g b)) -> Interval f a -> m (Interval g b) Source #
Traverse over an interval’s endpoints.
Where traverse
only traverses over the individual coordinates, traverseInterval
can change the space as well.
>>>
:t traverseInterval (\ (V2 x y) -> V3 x y)
traverseInterval (\ (V2 x y) -> V3 x y) :: Interval V2 a -> a -> Interval V3 a
>>>
traverseInterval (\ (V2 x y) -> V3 x y) (Interval (V2 1 2) (V2 3 4)) 0
V3 1 2 0...V3 3 4 0
traverse f = traverseInterval (traverse f)
foldMapInterval f ≅ getConst . traverseInterval (Const . f)
mapInterval f = runIdentity . traverseInterval (Identity . f)
Predicates
member :: (Applicative f, Foldable f, Ord a) => f a -> Interval f a -> Bool Source #
Test a point for inclusion within an interval.
isValid :: (Applicative f, Foldable f, Ord a) => Interval f a -> Bool Source #
Test an interval for validity, i.e. non-emptiness.
isEmpty :: (Applicative f, Foldable f, Ord a) => Interval f a -> Bool Source #
Test an interval for validity, i.e. non-emptiness.
isPoint :: (Applicative f, Foldable f, Eq a) => Interval f a -> Bool Source #
Test whether an interval is a singleton.
Relations
isSubintervalOf :: (Applicative f, Foldable f, Ord a) => Interval f a -> Interval f a -> Bool Source #
Test whether one interval is a subinterval of another.
i `isSubintervalOf` i = True
i `isSubintervalOf` j && j `isSubintervalOf` k => i `isSubintervalOf` k
isSuperintervalOf :: (Applicative f, Foldable f, Ord a) => Interval f a -> Interval f a -> Bool Source #
Test whether one interval is a superinterval of another.
i `isSuperintervalOf` i = True
i `isSuperintervalOf` j && j `isSuperintervalOf` k => i `isSuperintervalOf` k
isProperSubintervalOf :: (Applicative f, Foldable f, Ord a) => Interval f a -> Interval f a -> Bool Source #
Test whether one interval is a proper subinterval of another (i.e. a subinterval, but not equal).
i `isProperSubintervalOf` i = False
i `isProperSubintervalOf` j && j `isProperSubintervalOf` k => i `isProperSubintervalOf` k
isProperSuperintervalOf :: (Applicative f, Foldable f, Ord a) => Interval f a -> Interval f a -> Bool Source #
Test whether one interval is a proper superinterval of another (i.e. a superinterval, but not equal).
i `isProperSuperintervalOf` i = False
i `isProperSuperintervalOf` j && j `isProperSuperintervalOf` k => i `isProperSuperintervalOf` k
intersects :: (Applicative f, Foldable f, Ord a) => Interval f a -> Interval f a -> Bool Source #
Test whether two intervals intersect.
i `intersects` i = True
i `intersects` j = j `intersects` i
Semigroups
Instances
Monad f => Monad (Union f) Source # | |
Functor f => Functor (Union f) Source # | |
Applicative f => Applicative (Union f) Source # | |
Foldable f => Foldable (Union f) Source # | |
Defined in Data.Functor.Interval fold :: Monoid m => Union f m -> m # foldMap :: Monoid m => (a -> m) -> Union f a -> m # foldMap' :: Monoid m => (a -> m) -> Union f a -> m # foldr :: (a -> b -> b) -> b -> Union f a -> b # foldr' :: (a -> b -> b) -> b -> Union f a -> b # foldl :: (b -> a -> b) -> b -> Union f a -> b # foldl' :: (b -> a -> b) -> b -> Union f a -> b # foldr1 :: (a -> a -> a) -> Union f a -> a # foldl1 :: (a -> a -> a) -> Union f a -> a # elem :: Eq a => a -> Union f a -> Bool # maximum :: Ord a => Union f a -> a # minimum :: Ord a => Union f a -> a # | |
Traversable f => Traversable (Union f) Source # | |
Eq (f a) => Eq (Union f a) Source # | |
Ord (f a) => Ord (Union f a) Source # | |
Defined in Data.Functor.Interval | |
Show (f a) => Show (Union f a) Source # | |
(Applicative f, Ord a) => Semigroup (Union f a) Source # | |
union :: forall f a. (Applicative f, Ord a) => Interval f a -> Interval f a -> Interval f a Source #
newtype Intersection f a Source #
Intersection | |
|
Instances
intersection :: forall f a. (Applicative f, Ord a) => Interval f a -> Interval f a -> Interval f a Source #
Take the intersection of two intervals.
This is equivalent to the Semigroup
instance for Intersection
, and is provided for clarity and convenience.