Safe Haskell | None |
---|---|
Language | Haskell2010 |
Closed intervals in a space given by an arbitrary functor.
Synopsis
- data Interval (f :: Type -> Type) a = Interval {}
- inf_ :: forall f1 a f2. Functor f2 => (f1 a -> f2 (f1 a)) -> Interval f1 a -> f2 (Interval f1 a)
- sup_ :: forall f1 a f2. Functor f2 => (f1 a -> f2 (f1 a)) -> Interval f1 a -> f2 (Interval f1 a)
- (...) :: forall (f :: Type -> Type) 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 :: forall (f :: Type -> Type) a. (Applicative f, Foldable f, Ord a) => Interval f a -> Bool
- isEmpty :: forall (f :: Type -> Type) a. (Applicative f, Foldable f, Ord a) => Interval f a -> Bool
- isPoint :: forall (f :: Type -> Type) a. (Applicative f, Foldable f, Eq a) => Interval f a -> Bool
- isSubintervalOf :: forall (f :: Type -> Type) a. (Applicative f, Foldable f, Ord a) => Interval f a -> Interval f a -> Bool
- isSuperintervalOf :: forall (f :: Type -> Type) a. (Applicative f, Foldable f, Ord a) => Interval f a -> Interval f a -> Bool
- isProperSubintervalOf :: forall (f :: Type -> Type) a. (Applicative f, Foldable f, Ord a) => Interval f a -> Interval f a -> Bool
- isProperSuperintervalOf :: forall (f :: Type -> Type) a. (Applicative f, Foldable f, Ord a) => Interval f a -> Interval f a -> Bool
- intersects :: forall (f :: Type -> Type) a. (Applicative f, Foldable f, Ord a) => Interval f a -> Interval f a -> Bool
- newtype Union (f :: Type -> Type) a = Union {}
- union :: forall (f :: Type -> Type) a. (Applicative f, Ord a) => Interval f a -> Interval f a -> Interval f a
- newtype Intersection (f :: Type -> Type) a = Intersection {
- getIntersection :: Interval f a
- intersection :: forall (f :: Type -> Type) a. (Applicative f, Ord a) => Interval f a -> Interval f a -> Interval f a
Documentation
data Interval (f :: Type -> Type) a Source #
f
-dimensional intervals with coordinates in a
.
Instances
Lenses
inf_ :: forall f1 a f2. Functor f2 => (f1 a -> f2 (f1 a)) -> Interval f1 a -> f2 (Interval f1 a) Source #
Access the infimum of an interval.
sup_ :: forall f1 a f2. Functor f2 => (f1 a -> f2 (f1 a)) -> Interval f1 a -> f2 (Interval f1 a) Source #
Access the supremum of an interval.
Constructors
(...) :: forall (f :: Type -> Type) a. 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 :: forall (f :: Type -> Type) a. (Applicative f, Foldable f, Ord a) => Interval f a -> Bool Source #
Test an interval for validity, i.e. non-emptiness.
isEmpty :: forall (f :: Type -> Type) a. (Applicative f, Foldable f, Ord a) => Interval f a -> Bool Source #
Test an interval for validity, i.e. non-emptiness.
isPoint :: forall (f :: Type -> Type) a. (Applicative f, Foldable f, Eq a) => Interval f a -> Bool Source #
Test whether an interval is a singleton.
Relations
isSubintervalOf :: forall (f :: Type -> Type) a. (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 :: forall (f :: Type -> Type) a. (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 :: forall (f :: Type -> Type) a. (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 :: forall (f :: Type -> Type) a. (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 :: forall (f :: Type -> Type) a. (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
newtype Union (f :: Type -> Type) a Source #
Instances
Applicative f => Applicative (Union f) Source # | |
Functor f => Functor (Union f) Source # | |
Monad f => Monad (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 # | |
(Applicative f, Ord a) => Semigroup (Union f a) Source # | |
Show (f a) => Show (Union f a) Source # | |
Eq (f a) => Eq (Union f a) Source # | |
Ord (f a) => Ord (Union f a) Source # | |
Defined in Data.Functor.Interval |
union :: forall (f :: Type -> Type) a. (Applicative f, Ord a) => Interval f a -> Interval f a -> Interval f a Source #
newtype Intersection (f :: Type -> Type) a Source #
Intersection | |
|
Instances
Applicative f => Applicative (Intersection f) Source # | |
Defined in Data.Functor.Interval pure :: a -> Intersection f a # (<*>) :: Intersection f (a -> b) -> Intersection f a -> Intersection f b # liftA2 :: (a -> b -> c) -> Intersection f a -> Intersection f b -> Intersection f c # (*>) :: Intersection f a -> Intersection f b -> Intersection f b # (<*) :: Intersection f a -> Intersection f b -> Intersection f a # | |
Functor f => Functor (Intersection f) Source # | |
Defined in Data.Functor.Interval fmap :: (a -> b) -> Intersection f a -> Intersection f b # (<$) :: a -> Intersection f b -> Intersection f a # | |
Monad f => Monad (Intersection f) Source # | |
Defined in Data.Functor.Interval (>>=) :: Intersection f a -> (a -> Intersection f b) -> Intersection f b # (>>) :: Intersection f a -> Intersection f b -> Intersection f b # return :: a -> Intersection f a # | |
Foldable f => Foldable (Intersection f) Source # | |
Defined in Data.Functor.Interval fold :: Monoid m => Intersection f m -> m # foldMap :: Monoid m => (a -> m) -> Intersection f a -> m # foldMap' :: Monoid m => (a -> m) -> Intersection f a -> m # foldr :: (a -> b -> b) -> b -> Intersection f a -> b # foldr' :: (a -> b -> b) -> b -> Intersection f a -> b # foldl :: (b -> a -> b) -> b -> Intersection f a -> b # foldl' :: (b -> a -> b) -> b -> Intersection f a -> b # foldr1 :: (a -> a -> a) -> Intersection f a -> a # foldl1 :: (a -> a -> a) -> Intersection f a -> a # toList :: Intersection f a -> [a] # null :: Intersection f a -> Bool # length :: Intersection f a -> Int # elem :: Eq a => a -> Intersection f a -> Bool # maximum :: Ord a => Intersection f a -> a # minimum :: Ord a => Intersection f a -> a # sum :: Num a => Intersection f a -> a # product :: Num a => Intersection f a -> a # | |
Traversable f => Traversable (Intersection f) Source # | |
Defined in Data.Functor.Interval traverse :: Applicative f0 => (a -> f0 b) -> Intersection f a -> f0 (Intersection f b) # sequenceA :: Applicative f0 => Intersection f (f0 a) -> f0 (Intersection f a) # mapM :: Monad m => (a -> m b) -> Intersection f a -> m (Intersection f b) # sequence :: Monad m => Intersection f (m a) -> m (Intersection f a) # | |
(Applicative f, Ord a) => Semigroup (Intersection f a) Source # | |
Defined in Data.Functor.Interval (<>) :: Intersection f a -> Intersection f a -> Intersection f a # sconcat :: NonEmpty (Intersection f a) -> Intersection f a # stimes :: Integral b => b -> Intersection f a -> Intersection f a # | |
Show (f a) => Show (Intersection f a) Source # | |
Defined in Data.Functor.Interval showsPrec :: Int -> Intersection f a -> ShowS # show :: Intersection f a -> String # showList :: [Intersection f a] -> ShowS # | |
Eq (f a) => Eq (Intersection f a) Source # | |
Defined in Data.Functor.Interval (==) :: Intersection f a -> Intersection f a -> Bool # (/=) :: Intersection f a -> Intersection f a -> Bool # | |
Ord (f a) => Ord (Intersection f a) Source # | |
Defined in Data.Functor.Interval compare :: Intersection f a -> Intersection f a -> Ordering # (<) :: Intersection f a -> Intersection f a -> Bool # (<=) :: Intersection f a -> Intersection f a -> Bool # (>) :: Intersection f a -> Intersection f a -> Bool # (>=) :: Intersection f a -> Intersection f a -> Bool # max :: Intersection f a -> Intersection f a -> Intersection f a # min :: Intersection f a -> Intersection f a -> Intersection f a # |
intersection :: forall (f :: Type -> Type) 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.