interval-functor-0.0.0.0: Intervals of functors.
Safe HaskellNone
LanguageHaskell2010

Data.Functor.Interval

Description

Closed intervals in a space given by an arbitrary functor.

Synopsis

Documentation

data Interval f a Source #

f-dimensional intervals with coordinates in a.

Constructors

Interval 

Fields

  • inf :: !(f a)

    The infimum, or lower bound.

  • sup :: !(f a)

    The supremum, or upper bound.

Instances

Instances details
MonadTrans Interval Source # 
Instance details

Defined in Data.Functor.Interval

Methods

lift :: Monad m => m a -> Interval m a #

Monad f => Monad (Interval f) Source # 
Instance details

Defined in Data.Functor.Interval

Methods

(>>=) :: Interval f a -> (a -> Interval f b) -> Interval f b #

(>>) :: Interval f a -> Interval f b -> Interval f b #

return :: a -> Interval f a #

Functor f => Functor (Interval f) Source #

Maps over each coordinate of the endpoints. See mapInterval for mapping over the endpoints themselves.

Instance details

Defined in Data.Functor.Interval

Methods

fmap :: (a -> b) -> Interval f a -> Interval f b #

(<$) :: a -> Interval f b -> Interval f a #

Applicative f => Applicative (Interval f) Source # 
Instance details

Defined in Data.Functor.Interval

Methods

pure :: a -> Interval f a #

(<*>) :: Interval f (a -> b) -> Interval f a -> Interval f b #

liftA2 :: (a -> b -> c) -> Interval f a -> Interval f b -> Interval f c #

(*>) :: Interval f a -> Interval f b -> Interval f b #

(<*) :: Interval f a -> Interval f b -> Interval f a #

Foldable f => Foldable (Interval f) Source #

Folds over each coordinate of the endpoints. See foldMapInterval for folding over the endpoints themselves.

Instance details

Defined in Data.Functor.Interval

Methods

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 #

sum :: Num a => Interval f a -> a #

product :: Num a => Interval f a -> a #

Traversable f => Traversable (Interval f) Source #

Traverses over each coordinate of the endpoints. See traverseInterval for traversing over the endpoints themselves.

Instance details

Defined in Data.Functor.Interval

Methods

traverse :: Applicative f0 => (a -> f0 b) -> Interval f a -> f0 (Interval f b) #

sequenceA :: Applicative f0 => Interval f (f0 a) -> f0 (Interval f a) #

mapM :: Monad m => (a -> m b) -> Interval f a -> m (Interval f b) #

sequence :: Monad m => Interval f (m a) -> m (Interval f a) #

Generic1 (Interval f :: Type -> Type) Source # 
Instance details

Defined in Data.Functor.Interval

Associated Types

type Rep1 (Interval f) :: k -> Type #

Methods

from1 :: forall (a :: k). Interval f a -> Rep1 (Interval f) a #

to1 :: forall (a :: k). Rep1 (Interval f) a -> Interval f a #

Eq (f a) => Eq (Interval f a) Source # 
Instance details

Defined in Data.Functor.Interval

Methods

(==) :: Interval f a -> Interval f a -> Bool #

(/=) :: Interval f a -> Interval f a -> Bool #

(Applicative f, Floating a) => Floating (Interval f a) Source # 
Instance details

Defined in Data.Functor.Interval

Methods

pi :: Interval f a #

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 #

log1pexp :: Interval f a -> Interval f a #

log1mexp :: Interval f a -> Interval f a #

(Applicative f, Fractional a) => Fractional (Interval f a) Source # 
Instance details

Defined in Data.Functor.Interval

Methods

(/) :: Interval f a -> Interval f a -> Interval f a #

recip :: Interval f a -> Interval f a #

fromRational :: Rational -> Interval f a #

(Applicative f, Num a) => Num (Interval f a) Source # 
Instance details

Defined in Data.Functor.Interval

Methods

(+) :: 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 f, with the infima taking precedence over the suprema.

Instance details

Defined in Data.Functor.Interval

Methods

compare :: Interval f a -> Interval f a -> Ordering #

(<) :: Interval f a -> Interval f a -> Bool #

(<=) :: Interval f a -> Interval f a -> Bool #

(>) :: Interval f a -> Interval f a -> Bool #

(>=) :: Interval f a -> Interval f a -> Bool #

max :: Interval f a -> Interval f a -> Interval f a #

min :: Interval f a -> Interval f a -> Interval f a #

Show (f a) => Show (Interval f a) Source # 
Instance details

Defined in Data.Functor.Interval

Methods

showsPrec :: Int -> Interval f a -> ShowS #

show :: Interval f a -> String #

showList :: [Interval f a] -> ShowS #

Generic (Interval f a) Source # 
Instance details

Defined in Data.Functor.Interval

Associated Types

type Rep (Interval f a) :: Type -> Type #

Methods

from :: Interval f a -> Rep (Interval f a) x #

to :: Rep (Interval f a) x -> Interval f a #

(Applicative f, Ord a) => Semigroup (Interval f a) Source #

<> is a synonym for union.

Instance details

Defined in Data.Functor.Interval

Methods

(<>) :: Interval f a -> Interval f a -> Interval f a #

sconcat :: NonEmpty (Interval f a) -> Interval f a #

stimes :: Integral b => b -> Interval f a -> Interval f a #

type Rep1 (Interval f :: Type -> Type) Source # 
Instance details

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 # 
Instance details

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

inf_ :: Lens' (Interval f a) (f a) Source #

Access the infimum of an interval.

sup_ :: Lens' (Interval f a) (f a) Source #

Access the supremum of an interval.

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

newtype Union f a Source #

Intervals form a Semigroup under the union.

Constructors

Union 

Fields

Instances

Instances details
Monad f => Monad (Union f) Source # 
Instance details

Defined in Data.Functor.Interval

Methods

(>>=) :: Union f a -> (a -> Union f b) -> Union f b #

(>>) :: Union f a -> Union f b -> Union f b #

return :: a -> Union f a #

Functor f => Functor (Union f) Source # 
Instance details

Defined in Data.Functor.Interval

Methods

fmap :: (a -> b) -> Union f a -> Union f b #

(<$) :: a -> Union f b -> Union f a #

Applicative f => Applicative (Union f) Source # 
Instance details

Defined in Data.Functor.Interval

Methods

pure :: a -> Union f a #

(<*>) :: Union f (a -> b) -> Union f a -> Union f b #

liftA2 :: (a -> b -> c) -> Union f a -> Union f b -> Union f c #

(*>) :: Union f a -> Union f b -> Union f b #

(<*) :: Union f a -> Union f b -> Union f a #

Foldable f => Foldable (Union f) Source # 
Instance details

Defined in Data.Functor.Interval

Methods

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 #

toList :: Union f a -> [a] #

null :: Union f a -> Bool #

length :: Union f a -> Int #

elem :: Eq a => a -> Union f a -> Bool #

maximum :: Ord a => Union f a -> a #

minimum :: Ord a => Union f a -> a #

sum :: Num a => Union f a -> a #

product :: Num a => Union f a -> a #

Traversable f => Traversable (Union f) Source # 
Instance details

Defined in Data.Functor.Interval

Methods

traverse :: Applicative f0 => (a -> f0 b) -> Union f a -> f0 (Union f b) #

sequenceA :: Applicative f0 => Union f (f0 a) -> f0 (Union f a) #

mapM :: Monad m => (a -> m b) -> Union f a -> m (Union f b) #

sequence :: Monad m => Union f (m a) -> m (Union f a) #

Eq (f a) => Eq (Union f a) Source # 
Instance details

Defined in Data.Functor.Interval

Methods

(==) :: Union f a -> Union f a -> Bool #

(/=) :: Union f a -> Union f a -> Bool #

Ord (f a) => Ord (Union f a) Source # 
Instance details

Defined in Data.Functor.Interval

Methods

compare :: Union f a -> Union f a -> Ordering #

(<) :: Union f a -> Union f a -> Bool #

(<=) :: Union f a -> Union f a -> Bool #

(>) :: Union f a -> Union f a -> Bool #

(>=) :: Union f a -> Union f a -> Bool #

max :: Union f a -> Union f a -> Union f a #

min :: Union f a -> Union f a -> Union f a #

Show (f a) => Show (Union f a) Source # 
Instance details

Defined in Data.Functor.Interval

Methods

showsPrec :: Int -> Union f a -> ShowS #

show :: Union f a -> String #

showList :: [Union f a] -> ShowS #

(Applicative f, Ord a) => Semigroup (Union f a) Source # 
Instance details

Defined in Data.Functor.Interval

Methods

(<>) :: Union f a -> Union f a -> Union f a #

sconcat :: NonEmpty (Union f a) -> Union f a #

stimes :: Integral b => b -> Union f a -> Union f a #

union :: forall f a. (Applicative f, Ord a) => Interval f a -> Interval f a -> Interval f a Source #

Take the union of two intervals.

This is equivalent to the Semigroup instance for Interval (and for Union), and is provided for clarity and convenience.

newtype Intersection f a Source #

Intervals form a Semigroup under intersection.

Constructors

Intersection 

Fields

Instances

Instances details
Monad f => Monad (Intersection f) Source # 
Instance details

Defined in Data.Functor.Interval

Methods

(>>=) :: Intersection f a -> (a -> Intersection f b) -> Intersection f b #

(>>) :: Intersection f a -> Intersection f b -> Intersection f b #

return :: a -> Intersection f a #

Functor f => Functor (Intersection f) Source # 
Instance details

Defined in Data.Functor.Interval

Methods

fmap :: (a -> b) -> Intersection f a -> Intersection f b #

(<$) :: a -> Intersection f b -> Intersection f a #

Applicative f => Applicative (Intersection f) Source # 
Instance details

Defined in Data.Functor.Interval

Methods

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 #

Foldable f => Foldable (Intersection f) Source # 
Instance details

Defined in Data.Functor.Interval

Methods

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 # 
Instance details

Defined in Data.Functor.Interval

Methods

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) #

Eq (f a) => Eq (Intersection f a) Source # 
Instance details

Defined in Data.Functor.Interval

Methods

(==) :: Intersection f a -> Intersection f a -> Bool #

(/=) :: Intersection f a -> Intersection f a -> Bool #

Ord (f a) => Ord (Intersection f a) Source # 
Instance details

Defined in Data.Functor.Interval

Show (f a) => Show (Intersection f a) Source # 
Instance details

Defined in Data.Functor.Interval

(Applicative f, Ord a) => Semigroup (Intersection f a) Source # 
Instance details

Defined in Data.Functor.Interval

Methods

(<>) :: 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 #

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.