{-# LANGUAGE NoImplicitPrelude #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE FlexibleContexts #-}
{-|
Module      : Interval Algebra Utilities
Description : Functions for operating on containers of Intervals.
Copyright   : (c) NoviSci, Inc 2020
License     : BSD3
Maintainer  : bsaul@novisci.com
Stability   : experimental
-}

module IntervalAlgebra.IntervalUtilities (
      combineIntervals
    , combineIntervals'
    , gaps
    , gaps'
    , durations
    , clip
    , relations
    , relations'
    , gapsWithin
    , nothingIf
    , nothingIfNone
    , nothingIfAny
    , nothingIfAll

    -- * Filtering functions
    , compareIntervals
    , filterBefore
    , filterMeets
    , filterOverlaps
    , filterFinishedBy
    , filterContains
    , filterStarts
    , filterEquals
    , filterStartedBy
    , filterDuring
    , filterFinishes
    , filterOverlappedBy
    , filterMetBy
    , filterAfter
    , filterDisjoint
    , filterNotDisjoint
    , filterConcur
    , filterWithin
    , filterEnclose
    , filterEnclosedBy

) where

import GHC.Base
    ( otherwise, ($), (.), (<*>), seq, not
    , Semigroup((<>))
    , Functor(fmap)
    , Applicative(pure)
    , Int, Bool, Ord)
import GHC.Show ( Show )
import GHC.Num ()
import Data.Tuple ( fst )
import Data.Foldable ( Foldable(null, foldl', toList), all, any )
import Data.Monoid ( (<>), Monoid(mempty) )
import IntervalAlgebra
    ( Interval, Intervallic(..), IntervalAlgebraic(..)
    , IntervalCombinable(..), IntervalSizeable(..)
    , IntervalRelation(..)
    , ComparativePredicateOf
    , unsafeInterval
    , beginerval
    , enderval
    )
import Data.Maybe (mapMaybe, catMaybes, fromMaybe, Maybe(..))
import Data.List ( (++), map, head, init, last, tail )
import Witherable ( Filterable(filter) )

-------------------------------------------------
-- Unexported utilties used in functions below --
-------------------------------------------------

intInt :: Int -> Int -> Interval Int
intInt :: Int -> Int -> Interval Int
intInt = Int -> Int -> Interval Int
forall a. a -> a -> Interval a
unsafeInterval

-- Fold over consecutive pairs of foldable structure and collect the results in 
-- a monoidal structure.
foldlAccume :: (Foldable f, Applicative m, Monoid (m a))=>
      (b -> b -> a) -- ^ @f@: a function to apply to consecutive elements of @f b@
    -> f b
    -> m a
foldlAccume :: (b -> b -> a) -> f b -> m a
foldlAccume b -> b -> a
f f b
x = (m a, Maybe b) -> m a
forall a b. (a, b) -> a
fst ((m a, Maybe b) -> m a) -> (m a, Maybe b) -> m a
forall a b. (a -> b) -> a -> b
$ ((m a, Maybe b) -> b -> (m a, Maybe b))
-> (m a, Maybe b) -> f b -> (m a, Maybe b)
forall (t :: * -> *) b a.
Foldable t =>
(b -> a -> b) -> b -> t a -> b
foldl' ((b -> b -> a) -> (m a, Maybe b) -> b -> (m a, Maybe b)
forall (f :: * -> *) a b.
(Monoid (f a), Applicative f) =>
(b -> b -> a) -> (f a, Maybe b) -> b -> (f a, Maybe b)
applyAccume b -> b -> a
f) (m a
forall a. Monoid a => a
mempty, Maybe b
forall a. Maybe a
Nothing) f b
x

-- Apply a function and accumulate the results in a monoidal structure.
applyAccume :: (Monoid (f a), Applicative f) =>
       (b -> b -> a)  -- ^ @f@: a function combining two @b@s to get an @a@
    -> (f a, Maybe b) -- ^ a pair (accumulating monoid for @b@s, optional @a@)
    -> b              -- ^ this will be the second argument to @f@
    -> (f a, Maybe b)
applyAccume :: (b -> b -> a) -> (f a, Maybe b) -> b -> (f a, Maybe b)
applyAccume b -> b -> a
f (f a
fs, Maybe b
Nothing) b
x = (f a
fs, b -> Maybe b
forall a. a -> Maybe a
Just b
x)
applyAccume b -> b -> a
f (f a
fs, Just b
x)  b
y  = (f a
fs f a -> f a -> f a
forall a. Semigroup a => a -> a -> a
<> a -> f a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (b -> b -> a
f b
x b
y), b -> Maybe b
forall a. a -> Maybe a
Just b
y)

-- Lifts a list to a foldable, applicative monoid 
liftListToFoldable :: (Applicative f
                      , Monoid (f a)
                      , Foldable f) =>
    [a] -> f a
liftListToFoldable :: [a] -> f a
liftListToFoldable = (f a -> a -> f a) -> f a -> [a] -> f a
forall (t :: * -> *) b a.
Foldable t =>
(b -> a -> b) -> b -> t a -> b
foldl' (\f a
x a
y -> f a
x f a -> f a -> f a
forall a. Semigroup a => a -> a -> a
<> a -> f a
forall (f :: * -> *) a. Applicative f => a -> f a
pure a
y) f a
forall a. Monoid a => a
mempty

-- Box to avoid overlapping instances
newtype Box a = Box { Box a -> [a]
unBox :: [a] }

-- Defines how a Box of Intervals are combined. Specifically, the last element of
-- x and first element of y are combined by '<+>'.
instance (IntervalCombinable a) => Semigroup (Box (Interval a)) where
    Box [Interval a]
x <> :: Box (Interval a) -> Box (Interval a) -> Box (Interval a)
<> Box [Interval a]
y
       | [Interval a] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null [Interval a]
x         = [Interval a] -> Box (Interval a)
forall a. [a] -> Box a
Box [Interval a]
y
       | [Interval a] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null [Interval a]
y         = [Interval a] -> Box (Interval a)
forall a. [a] -> Box a
Box [Interval a]
x
       | Bool
otherwise      = [Interval a] -> Box (Interval a)
forall a. [a] -> Box a
Box ([Interval a] -> Box (Interval a))
-> [Interval a] -> Box (Interval a)
forall a b. (a -> b) -> a -> b
$ [Interval a] -> [Interval a]
forall a. [a] -> [a]
init [Interval a]
x [Interval a] -> [Interval a] -> [Interval a]
forall a. [a] -> [a] -> [a]
++ (Interval a
lx Interval a -> Interval a -> [Interval a]
forall a (f :: * -> *).
(IntervalCombinable a, Semigroup (f (Interval a)),
 Applicative f) =>
Interval a -> Interval a -> f (Interval a)
<+> Interval a
fy) [Interval a] -> [Interval a] -> [Interval a]
forall a. [a] -> [a] -> [a]
++ [Interval a] -> [Interval a]
forall a. [a] -> [a]
tail [Interval a]
y
       where lx :: Interval a
lx = [Interval a] -> Interval a
forall a. [a] -> a
last [Interval a]
x
             fy :: Interval a
fy = [Interval a] -> Interval a
forall a. [a] -> a
head [Interval a]
y

-------------------------------------------------

-- | Returns a container of intervals where any intervals that meet or share support
--   are combined into one interval. *To work properly, the input should 
--   be sorted*. See 'combineIntervals'' for a version that works only on lists.
--
-- >>> combineIntervals [intInt 0 10, intInt 2 7, intInt 10 12, intInt 13 15]
-- [(0, 12),(13, 15)]
combineIntervals :: (IntervalCombinable a
         , Applicative f
         , Monoid (f (Interval a))
         , Foldable f) =>
      f (Interval a) ->
      f (Interval a)
combineIntervals :: f (Interval a) -> f (Interval a)
combineIntervals f (Interval a)
x = [Interval a] -> f (Interval a)
forall (f :: * -> *) a.
(Applicative f, Monoid (f a), Foldable f) =>
[a] -> f a
liftListToFoldable ([Interval a] -> [Interval a]
forall a. IntervalCombinable a => [Interval a] -> [Interval a]
combineIntervals' ([Interval a] -> [Interval a]) -> [Interval a] -> [Interval a]
forall a b. (a -> b) -> a -> b
$ f (Interval a) -> [Interval a]
forall (t :: * -> *) a. Foldable t => t a -> [a]
toList f (Interval a)
x)

-- | Returns a list of intervals where any intervals that meet or share support
--   are combined into one interval. *To work properly, the input list should 
--   be sorted*. 
--
-- >>> combineIntervals' [intInt 0 10, intInt 2 7, intInt 10 12, intInt 13 15]
-- [(0, 12),(13, 15)]
combineIntervals' :: (IntervalCombinable a) => [Interval a] -> [Interval a]
combineIntervals' :: [Interval a] -> [Interval a]
combineIntervals' [Interval a]
l = Box (Interval a) -> [Interval a]
forall a. Box a -> [a]
unBox (Box (Interval a) -> [Interval a])
-> Box (Interval a) -> [Interval a]
forall a b. (a -> b) -> a -> b
$ (Box (Interval a) -> Box (Interval a) -> Box (Interval a))
-> Box (Interval a) -> [Box (Interval a)] -> Box (Interval a)
forall (t :: * -> *) b a.
Foldable t =>
(b -> a -> b) -> b -> t a -> b
foldl' Box (Interval a) -> Box (Interval a) -> Box (Interval a)
forall a. Semigroup a => a -> a -> a
(<>) ([Interval a] -> Box (Interval a)
forall a. [a] -> Box a
Box []) ((Interval a -> Box (Interval a))
-> [Interval a] -> [Box (Interval a)]
forall a b. (a -> b) -> [a] -> [b]
map (\Interval a
z -> [Interval a] -> Box (Interval a)
forall a. [a] -> Box a
Box [Interval a
z]) [Interval a]
l)

-- | Returns a (possibly empty) container of intervals consisting of the gaps 
--   between intervals in the input. *To work properly, the input should be
--   sorted*. See 'gaps'' for a version that returns a list.
--
-- >>> gaps [intInt 1 5, intInt 8 12, intInt 11 14]
-- [(5, 8)]
gaps :: (IntervalCombinable a
         , Applicative f
         , Monoid (f (Interval a))
         , Foldable f) =>
      f (Interval a) ->
      f (Interval a)
gaps :: f (Interval a) -> f (Interval a)
gaps f (Interval a)
x = [Interval a] -> f (Interval a)
forall (f :: * -> *) a.
(Applicative f, Monoid (f a), Foldable f) =>
[a] -> f a
liftListToFoldable (f (Interval a) -> [Interval a]
forall a (f :: * -> *).
(IntervalCombinable a, Applicative f, Monoid (f (Interval a)),
 Foldable f) =>
f (Interval a) -> [Interval a]
gaps' f (Interval a)
x)

-- | Returns a (possibly empty) list of intervals consisting of the gaps between
--   intervals in the input container. *To work properly, the input should be 
--   sorted*. This version outputs a list. See 'gaps' for a version that lifts
--   the result to same input structure @f@.
gaps' :: (IntervalCombinable a
         , Applicative f
         , Monoid (f (Interval a))
         , Foldable f) =>
      f (Interval a) ->
      [Interval a]
gaps' :: f (Interval a) -> [Interval a]
gaps' f (Interval a)
x = [Maybe (Interval a)] -> [Interval a]
forall a. [Maybe a] -> [a]
catMaybes ((Interval a -> Interval a -> Maybe (Interval a))
-> f (Interval a) -> [Maybe (Interval a)]
forall (f :: * -> *) (m :: * -> *) a b.
(Foldable f, Applicative m, Monoid (m a)) =>
(b -> b -> a) -> f b -> m a
foldlAccume Interval a -> Interval a -> Maybe (Interval a)
forall a.
IntervalCombinable a =>
Interval a -> Interval a -> Maybe (Interval a)
(><) f (Interval a)
x)

-- | Returns the 'duration' of each 'Intervallic i a' in the 'Functor' @f@.
--
-- >>> durations [intInt 1 10, intInt 2 12, intInt 5 6]
-- [9,10,1]
durations :: (Functor f, Intervallic i a, IntervalSizeable a b)=>
       f (i a)
    -> f b
durations :: f (i a) -> f b
durations = (i a -> b) -> f (i a) -> f b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap i a -> b
forall a b (i :: * -> *).
(IntervalSizeable a b, Intervallic i a) =>
i a -> b
duration

-- | In the case that x y are not disjoint, clips y to the extent of x.
-- 
-- >>> clip (intInt 0 5) (intInt 3 6)
-- Just (3, 5)
--
-- >>> clip (intInt 0 3) (intInt 4 6)
-- Nothing
clip :: (IntervalAlgebraic Interval a, IntervalSizeable a b)=>
       Interval a
    -> Interval a
    -> Maybe (Interval a)
clip :: Interval a -> Interval a -> Maybe (Interval a)
clip Interval a
x Interval a
y
   | ComparativePredicateOf (Interval a)
forall (i :: * -> *) a.
IntervalAlgebraic i a =>
ComparativePredicateOf (i a)
overlaps Interval a
x Interval a
y     = Interval a -> Maybe (Interval a)
forall a. a -> Maybe a
Just (Interval a -> Maybe (Interval a))
-> Interval a -> Maybe (Interval a)
forall a b. (a -> b) -> a -> b
$ b -> a -> Interval a
forall a b. IntervalSizeable a b => b -> a -> Interval a
enderval   (a -> a -> b
forall a b. IntervalSizeable a b => a -> a -> b
diff (Interval a -> a
forall (i :: * -> *) a. Intervallic i a => i a -> a
end Interval a
x) (Interval a -> a
forall (i :: * -> *) a. Intervallic i a => i a -> a
begin Interval a
y)) (Interval a -> a
forall (i :: * -> *) a. Intervallic i a => i a -> a
end Interval a
x)
   | ComparativePredicateOf (Interval a)
forall (i :: * -> *) a.
IntervalAlgebraic i a =>
ComparativePredicateOf (i a)
overlappedBy Interval a
x Interval a
y = Interval a -> Maybe (Interval a)
forall a. a -> Maybe a
Just (Interval a -> Maybe (Interval a))
-> Interval a -> Maybe (Interval a)
forall a b. (a -> b) -> a -> b
$ b -> a -> Interval a
forall a b. IntervalSizeable a b => b -> a -> Interval a
beginerval (a -> a -> b
forall a b. IntervalSizeable a b => a -> a -> b
diff (Interval a -> a
forall (i :: * -> *) a. Intervallic i a => i a -> a
end Interval a
y) (Interval a -> a
forall (i :: * -> *) a. Intervallic i a => i a -> a
begin Interval a
x)) (Interval a -> a
forall (i :: * -> *) a. Intervallic i a => i a -> a
begin Interval a
x)
   | ComparativePredicateOf (Interval a)
jx Interval a
x Interval a
y           = Interval a -> Maybe (Interval a)
forall a. a -> Maybe a
Just Interval a
x
   | ComparativePredicateOf (Interval a)
jy Interval a
x Interval a
y           = Interval a -> Maybe (Interval a)
forall a. a -> Maybe a
Just Interval a
y
   | ComparativePredicateOf (Interval a)
forall (i :: * -> *) a.
IntervalAlgebraic i a =>
ComparativePredicateOf (i a)
disjoint Interval a
x Interval a
y     = Maybe (Interval a)
forall a. Maybe a
Nothing
   where jy :: ComparativePredicateOf (Interval a)
jy = ComparativePredicateOf (Interval a)
forall (i :: * -> *) a.
IntervalAlgebraic i a =>
ComparativePredicateOf (i a)
equals ComparativePredicateOf (Interval a)
-> ComparativePredicateOf (Interval a)
-> ComparativePredicateOf (Interval a)
forall (i :: * -> *) a.
IntervalAlgebraic i a =>
ComparativePredicateOf (i a)
-> ComparativePredicateOf (i a) -> ComparativePredicateOf (i a)
<|> ComparativePredicateOf (Interval a)
forall (i :: * -> *) a.
IntervalAlgebraic i a =>
ComparativePredicateOf (i a)
startedBy ComparativePredicateOf (Interval a)
-> ComparativePredicateOf (Interval a)
-> ComparativePredicateOf (Interval a)
forall (i :: * -> *) a.
IntervalAlgebraic i a =>
ComparativePredicateOf (i a)
-> ComparativePredicateOf (i a) -> ComparativePredicateOf (i a)
<|> ComparativePredicateOf (Interval a)
forall (i :: * -> *) a.
IntervalAlgebraic i a =>
ComparativePredicateOf (i a)
contains ComparativePredicateOf (Interval a)
-> ComparativePredicateOf (Interval a)
-> ComparativePredicateOf (Interval a)
forall (i :: * -> *) a.
IntervalAlgebraic i a =>
ComparativePredicateOf (i a)
-> ComparativePredicateOf (i a) -> ComparativePredicateOf (i a)
<|> ComparativePredicateOf (Interval a)
forall (i :: * -> *) a.
IntervalAlgebraic i a =>
ComparativePredicateOf (i a)
finishedBy
         jx :: ComparativePredicateOf (Interval a)
jx = ComparativePredicateOf (Interval a)
forall (i :: * -> *) a.
IntervalAlgebraic i a =>
ComparativePredicateOf (i a)
starts ComparativePredicateOf (Interval a)
-> ComparativePredicateOf (Interval a)
-> ComparativePredicateOf (Interval a)
forall (i :: * -> *) a.
IntervalAlgebraic i a =>
ComparativePredicateOf (i a)
-> ComparativePredicateOf (i a) -> ComparativePredicateOf (i a)
<|> ComparativePredicateOf (Interval a)
forall (i :: * -> *) a.
IntervalAlgebraic i a =>
ComparativePredicateOf (i a)
during ComparativePredicateOf (Interval a)
-> ComparativePredicateOf (Interval a)
-> ComparativePredicateOf (Interval a)
forall (i :: * -> *) a.
IntervalAlgebraic i a =>
ComparativePredicateOf (i a)
-> ComparativePredicateOf (i a) -> ComparativePredicateOf (i a)
<|> ComparativePredicateOf (Interval a)
forall (i :: * -> *) a.
IntervalAlgebraic i a =>
ComparativePredicateOf (i a)
finishes

-- | Returns a list of the 'IntervalRelation' between each consecutive pair 
--   of intervals. This the specialized form of 'relations'' which can return
--   any 'Applicative', 'Monoid' structure.
--
-- >>> relations [intInt 0 1, intInt 1 2] 
-- [Meets]
relations :: (IntervalAlgebraic i a, Foldable f)=>
       f (i a)
    -> [IntervalRelation (i a)]
relations :: f (i a) -> [IntervalRelation (i a)]
relations = f (i a) -> [IntervalRelation (i a)]
forall (i :: * -> *) a (f :: * -> *) (m :: * -> *).
(IntervalAlgebraic i a, Foldable f, Applicative m,
 Monoid (m (IntervalRelation (i a)))) =>
f (i a) -> m (IntervalRelation (i a))
relations'

-- | A generic form of 'relations' which can output any 'Applicative' and 
--   'Monoid' structure. 
-- >>> (relations' [intInt 0 1, intInt 1 2]) :: [IntervalRelation Int]
-- [Meets]
relations' :: ( IntervalAlgebraic i a
              , Foldable f
              , Applicative m
              , Monoid (m (IntervalRelation (i a))) )=>
        f (i a)
     -> m (IntervalRelation (i a))
relations' :: f (i a) -> m (IntervalRelation (i a))
relations' = (i a -> i a -> IntervalRelation (i a))
-> f (i a) -> m (IntervalRelation (i a))
forall (f :: * -> *) (m :: * -> *) a b.
(Foldable f, Applicative m, Monoid (m a)) =>
(b -> b -> a) -> f b -> m a
foldlAccume i a -> i a -> IntervalRelation (i a)
forall (i :: * -> *) a.
IntervalAlgebraic i a =>
i a -> i a -> IntervalRelation (i a)
relate

-- | Applies 'gaps' to all the non-disjoint intervals in @x@ that are *not* disjoint
-- from @i@. Intervals that 'overlaps' or are 'overlappedBy' @i@ are 'clip'ped 
-- to @i@, so that all the intervals are 'within' @i@. If there are no gaps, then
-- 'Nothing' is returned.
--
-- >>> gapsWithin (intInt 1 10) [intInt 0 5, intInt 7 9, intInt 12 15]
-- Just [(5, 7),(9, 10)]
--
gapsWithin :: ( Applicative f
               , Foldable f
               , Monoid (f (Interval a))
               , IntervalSizeable a b
               , IntervalCombinable a
               , Filterable f
               , IntervalAlgebraic Interval a)=>
     Interval a     -- ^ i
  -> f (Interval a) -- ^ x
  -> Maybe (f (Interval a))
gapsWithin :: Interval a -> f (Interval a) -> Maybe (f (Interval a))
gapsWithin Interval a
i f (Interval a)
x 
  | f (Interval a) -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null f (Interval a)
ivs  = Maybe (f (Interval a))
forall a. Maybe a
Nothing  
  | Bool
otherwise = f (Interval a) -> Maybe (f (Interval a))
forall a. a -> Maybe a
Just (f (Interval a) -> Maybe (f (Interval a)))
-> f (Interval a) -> Maybe (f (Interval a))
forall a b. (a -> b) -> a -> b
$ f (Interval a) -> f (Interval a)
forall a (f :: * -> *).
(IntervalCombinable a, Applicative f, Monoid (f (Interval a)),
 Foldable f) =>
f (Interval a) -> f (Interval a)
gaps (f (Interval a) -> f (Interval a))
-> f (Interval a) -> f (Interval a)
forall a b. (a -> b) -> a -> b
$ Interval a -> f (Interval a)
forall (f :: * -> *) a. Applicative f => a -> f a
pure Interval a
s f (Interval a) -> f (Interval a) -> f (Interval a)
forall a. Semigroup a => a -> a -> a
<> f (Interval a)
ivs f (Interval a) -> f (Interval a) -> f (Interval a)
forall a. Semigroup a => a -> a -> a
<> Interval a -> f (Interval a)
forall (f :: * -> *) a. Applicative f => a -> f a
pure Interval a
e
        where s :: Interval a
s   = b -> a -> Interval a
forall a b. IntervalSizeable a b => b -> a -> Interval a
enderval   b
0 (Interval a -> a
forall (i :: * -> *) a. Intervallic i a => i a -> a
begin Interval a
i)
              e :: Interval a
e   = b -> a -> Interval a
forall a b. IntervalSizeable a b => b -> a -> Interval a
beginerval b
0 (Interval a -> a
forall (i :: * -> *) a. Intervallic i a => i a -> a
end Interval a
i)
              nd :: [Interval a]
nd  = f (Interval a) -> [Interval a]
forall (t :: * -> *) a. Foldable t => t a -> [a]
toList (Interval a -> f (Interval a) -> f (Interval a)
forall (f :: * -> *) a (i0 :: * -> *) (i1 :: * -> *).
(Filterable f, IntervalAlgebraic Interval a,
 IntervalAlgebraic i0 a, IntervalAlgebraic i1 a) =>
i0 a -> f (i1 a) -> f (i1 a)
filterNotDisjoint Interval a
i f (Interval a)
x)
              ivs :: f (Interval a)
ivs = [Interval a] -> f (Interval a)
forall (f :: * -> *) a.
(Applicative f, Monoid (f a), Foldable f) =>
[a] -> f a
liftListToFoldable ((Interval a -> Maybe (Interval a)) -> [Interval a] -> [Interval a]
forall a b. (a -> Maybe b) -> [a] -> [b]
mapMaybe (Interval a -> Interval a -> Maybe (Interval a)
forall a b.
(IntervalAlgebraic Interval a, IntervalSizeable a b) =>
Interval a -> Interval a -> Maybe (Interval a)
clip Interval a
i) [Interval a]
nd) 

-- | Given a predicate combinator, a predicate, and list of intervals, returns 
--   the input unchanged if the predicate combinator is @True@. Otherwise, returns
--   an empty list. See 'nothingIfAny' and 'nothingIfNone' for examples.
nothingIf :: (Monoid (f (i a)), Filterable f, IntervalAlgebraic i a)=>
     ((i a -> Bool) -> f (i a) -> Bool) -- ^ e.g. 'any' or 'all'
  -> (i a -> Bool) -- ^ predicate to apply to each element of input list
  -> f (i a)
  -> Maybe (f (i a))
nothingIf :: ((i a -> Bool) -> f (i a) -> Bool)
-> (i a -> Bool) -> f (i a) -> Maybe (f (i a))
nothingIf (i a -> Bool) -> f (i a) -> Bool
quantifier i a -> Bool
predicate f (i a)
x = if (i a -> Bool) -> f (i a) -> Bool
quantifier i a -> Bool
predicate f (i a)
x then Maybe (f (i a))
forall a. Maybe a
Nothing else f (i a) -> Maybe (f (i a))
forall a. a -> Maybe a
Just f (i a)
x

-- | Returns the 'Nothing' if *none* of the element of input satisfy
--   the predicate condition.
-- 
-- For example, the following returns 'Nothing' because none of the intervals
-- in the input list 'starts' (3, 5).
--
-- >>> nothingIfNone (starts (intInt 3 5)) [intInt 3 4, intInt 5 6]
-- Nothing
--
-- In the following, (3, 5) 'starts' (3, 6), so 'Just' the input is returned.
--
-- >>> nothingIfNone (starts (intInt 3 5)) [intInt 3 6, intInt 5 6]
-- Just [(3, 6),(5, 6)]
--
nothingIfNone :: (Monoid (f (i a)), Foldable f, Filterable f, IntervalAlgebraic i a)=>
    (i a -> Bool) -- ^ predicate to apply to each element of input list
  -> f (i a)
  -> Maybe (f (i a))
nothingIfNone :: (i a -> Bool) -> f (i a) -> Maybe (f (i a))
nothingIfNone = ((i a -> Bool) -> f (i a) -> Bool)
-> (i a -> Bool) -> f (i a) -> Maybe (f (i a))
forall (f :: * -> *) (i :: * -> *) a.
(Monoid (f (i a)), Filterable f, IntervalAlgebraic i a) =>
((i a -> Bool) -> f (i a) -> Bool)
-> (i a -> Bool) -> f (i a) -> Maybe (f (i a))
nothingIf (\i a -> Bool
f f (i a)
x -> (Bool -> Bool
not(Bool -> Bool) -> (f (i a) -> Bool) -> f (i a) -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
.(i a -> Bool) -> f (i a) -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
any i a -> Bool
f) f (i a)
x)

-- | Returns 'Nothing' if *any* of the element of input satisfy the predicate condition.
nothingIfAny :: (Monoid (f (i a)), Foldable f, Filterable f, IntervalAlgebraic i a)=>
    (i a -> Bool) -- ^ predicate to apply to each element of input list
  -> f (i a)
  -> Maybe (f (i a))
nothingIfAny :: (i a -> Bool) -> f (i a) -> Maybe (f (i a))
nothingIfAny = ((i a -> Bool) -> f (i a) -> Bool)
-> (i a -> Bool) -> f (i a) -> Maybe (f (i a))
forall (f :: * -> *) (i :: * -> *) a.
(Monoid (f (i a)), Filterable f, IntervalAlgebraic i a) =>
((i a -> Bool) -> f (i a) -> Bool)
-> (i a -> Bool) -> f (i a) -> Maybe (f (i a))
nothingIf (i a -> Bool) -> f (i a) -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
any

-- | Returns 'Nothing' if *all* of the element of input satisfy the predicate condition
nothingIfAll :: (Monoid (f (i a)), Foldable f, Filterable f, IntervalAlgebraic i a)=>
    (i a -> Bool) -- ^ predicate to apply to each element of input list
  -> f (i a)
  -> Maybe (f (i a))
nothingIfAll :: (i a -> Bool) -> f (i a) -> Maybe (f (i a))
nothingIfAll = ((i a -> Bool) -> f (i a) -> Bool)
-> (i a -> Bool) -> f (i a) -> Maybe (f (i a))
forall (f :: * -> *) (i :: * -> *) a.
(Monoid (f (i a)), Filterable f, IntervalAlgebraic i a) =>
((i a -> Bool) -> f (i a) -> Bool)
-> (i a -> Bool) -> f (i a) -> Maybe (f (i a))
nothingIf (i a -> Bool) -> f (i a) -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
all

{- | 
Filter functions provides means for filtering 'Filterable' containers of 
@'Intervallic i a'@s based on @'IntervalAlgebraic'@ relations.
-}

-- | Lifts a predicate to be able to compare two different 'IntervalAlgebraic' 
--   structure by comparing the intervals contain within each. 
compareIntervals :: (IntervalAlgebraic i0 a, IntervalAlgebraic i1 a) =>
   ComparativePredicateOf (Interval a) 
    -> i0 a
    -> i1 a
    -> Bool
compareIntervals :: ComparativePredicateOf (Interval a) -> i0 a -> i1 a -> Bool
compareIntervals ComparativePredicateOf (Interval a)
pf i0 a
x i1 a
y = ComparativePredicateOf (Interval a)
pf (i0 a -> Interval a
forall (i :: * -> *) a. Intervallic i a => i a -> Interval a
getInterval i0 a
x) (i1 a -> Interval a
forall (i :: * -> *) a. Intervallic i a => i a -> Interval a
getInterval i1 a
y)

-- | Creates a function for filtering a 'Witherable.Filterable' of @i1 a@s 
--   by comparing the @Interval a@s that of an @i0 a@. 
filterMaker :: (Filterable f
                , IntervalAlgebraic Interval a
                , IntervalAlgebraic i0 a
                , IntervalAlgebraic i1 a) =>
        ComparativePredicateOf (Interval a)
      -> i0 a
      -> (f (i1 a) -> f (i1 a))
filterMaker :: ComparativePredicateOf (Interval a) -> i0 a -> f (i1 a) -> f (i1 a)
filterMaker ComparativePredicateOf (Interval a)
f i0 a
p = (i1 a -> Bool) -> f (i1 a) -> f (i1 a)
forall (f :: * -> *) a. Filterable f => (a -> Bool) -> f a -> f a
Witherable.filter (ComparativePredicateOf (Interval a) -> i0 a -> i1 a -> Bool
forall (i0 :: * -> *) a (i1 :: * -> *).
(IntervalAlgebraic i0 a, IntervalAlgebraic i1 a) =>
ComparativePredicateOf (Interval a) -> i0 a -> i1 a -> Bool
compareIntervals ComparativePredicateOf (Interval a)
f i0 a
p)

-- | Filter by 'overlaps'.
filterOverlaps :: (Filterable f
                  , IntervalAlgebraic Interval a
                  , IntervalAlgebraic i0 a
                  , IntervalAlgebraic i1 a) => 
                  i0 a -> f (i1 a) -> f (i1 a)
filterOverlaps :: i0 a -> f (i1 a) -> f (i1 a)
filterOverlaps = ComparativePredicateOf (Interval a) -> i0 a -> f (i1 a) -> f (i1 a)
forall (f :: * -> *) a (i0 :: * -> *) (i1 :: * -> *).
(Filterable f, IntervalAlgebraic Interval a,
 IntervalAlgebraic i0 a, IntervalAlgebraic i1 a) =>
ComparativePredicateOf (Interval a) -> i0 a -> f (i1 a) -> f (i1 a)
filterMaker ComparativePredicateOf (Interval a)
forall (i :: * -> *) a.
IntervalAlgebraic i a =>
ComparativePredicateOf (i a)
overlaps

-- | Filter by 'overlappedBy'.
filterOverlappedBy :: (Filterable f
                  , IntervalAlgebraic Interval a
                  , IntervalAlgebraic i0 a
                  , IntervalAlgebraic i1 a) => 
                  i0 a -> f (i1 a) -> f (i1 a)
filterOverlappedBy :: i0 a -> f (i1 a) -> f (i1 a)
filterOverlappedBy = ComparativePredicateOf (Interval a) -> i0 a -> f (i1 a) -> f (i1 a)
forall (f :: * -> *) a (i0 :: * -> *) (i1 :: * -> *).
(Filterable f, IntervalAlgebraic Interval a,
 IntervalAlgebraic i0 a, IntervalAlgebraic i1 a) =>
ComparativePredicateOf (Interval a) -> i0 a -> f (i1 a) -> f (i1 a)
filterMaker ComparativePredicateOf (Interval a)
forall (i :: * -> *) a.
IntervalAlgebraic i a =>
ComparativePredicateOf (i a)
overlappedBy

-- | Filter by 'before'.
filterBefore :: (Filterable f
                  , IntervalAlgebraic Interval a
                  , IntervalAlgebraic i0 a
                  , IntervalAlgebraic i1 a) => 
                  i0 a -> f (i1 a) -> f (i1 a)
filterBefore :: i0 a -> f (i1 a) -> f (i1 a)
filterBefore = ComparativePredicateOf (Interval a) -> i0 a -> f (i1 a) -> f (i1 a)
forall (f :: * -> *) a (i0 :: * -> *) (i1 :: * -> *).
(Filterable f, IntervalAlgebraic Interval a,
 IntervalAlgebraic i0 a, IntervalAlgebraic i1 a) =>
ComparativePredicateOf (Interval a) -> i0 a -> f (i1 a) -> f (i1 a)
filterMaker ComparativePredicateOf (Interval a)
forall (i :: * -> *) a.
IntervalAlgebraic i a =>
ComparativePredicateOf (i a)
before

-- | Filter by 'after'.
filterAfter :: (Filterable f
                  , IntervalAlgebraic Interval a
                  , IntervalAlgebraic i0 a
                  , IntervalAlgebraic i1 a) => 
                  i0 a -> f (i1 a) -> f (i1 a) 
filterAfter :: i0 a -> f (i1 a) -> f (i1 a)
filterAfter = ComparativePredicateOf (Interval a) -> i0 a -> f (i1 a) -> f (i1 a)
forall (f :: * -> *) a (i0 :: * -> *) (i1 :: * -> *).
(Filterable f, IntervalAlgebraic Interval a,
 IntervalAlgebraic i0 a, IntervalAlgebraic i1 a) =>
ComparativePredicateOf (Interval a) -> i0 a -> f (i1 a) -> f (i1 a)
filterMaker ComparativePredicateOf (Interval a)
forall (i :: * -> *) a.
IntervalAlgebraic i a =>
ComparativePredicateOf (i a)
after

-- | Filter by 'starts'.
filterStarts :: (Filterable f
                  , IntervalAlgebraic Interval a
                  , IntervalAlgebraic i0 a
                  , IntervalAlgebraic i1 a) => 
                  i0 a -> f (i1 a) -> f (i1 a)
filterStarts :: i0 a -> f (i1 a) -> f (i1 a)
filterStarts = ComparativePredicateOf (Interval a) -> i0 a -> f (i1 a) -> f (i1 a)
forall (f :: * -> *) a (i0 :: * -> *) (i1 :: * -> *).
(Filterable f, IntervalAlgebraic Interval a,
 IntervalAlgebraic i0 a, IntervalAlgebraic i1 a) =>
ComparativePredicateOf (Interval a) -> i0 a -> f (i1 a) -> f (i1 a)
filterMaker ComparativePredicateOf (Interval a)
forall (i :: * -> *) a.
IntervalAlgebraic i a =>
ComparativePredicateOf (i a)
starts 

-- | Filter by 'startedBy'.
filterStartedBy :: (Filterable f
                  , IntervalAlgebraic Interval a
                  , IntervalAlgebraic i0 a
                  , IntervalAlgebraic i1 a) => 
                  i0 a -> f (i1 a) -> f (i1 a)
filterStartedBy :: i0 a -> f (i1 a) -> f (i1 a)
filterStartedBy = ComparativePredicateOf (Interval a) -> i0 a -> f (i1 a) -> f (i1 a)
forall (f :: * -> *) a (i0 :: * -> *) (i1 :: * -> *).
(Filterable f, IntervalAlgebraic Interval a,
 IntervalAlgebraic i0 a, IntervalAlgebraic i1 a) =>
ComparativePredicateOf (Interval a) -> i0 a -> f (i1 a) -> f (i1 a)
filterMaker ComparativePredicateOf (Interval a)
forall (i :: * -> *) a.
IntervalAlgebraic i a =>
ComparativePredicateOf (i a)
startedBy 

-- | Filter by 'finishes'.
filterFinishes :: (Filterable f
                  , IntervalAlgebraic Interval a
                  , IntervalAlgebraic i0 a
                  , IntervalAlgebraic i1 a) => 
                  i0 a -> f (i1 a) -> f (i1 a)
filterFinishes :: i0 a -> f (i1 a) -> f (i1 a)
filterFinishes = ComparativePredicateOf (Interval a) -> i0 a -> f (i1 a) -> f (i1 a)
forall (f :: * -> *) a (i0 :: * -> *) (i1 :: * -> *).
(Filterable f, IntervalAlgebraic Interval a,
 IntervalAlgebraic i0 a, IntervalAlgebraic i1 a) =>
ComparativePredicateOf (Interval a) -> i0 a -> f (i1 a) -> f (i1 a)
filterMaker ComparativePredicateOf (Interval a)
forall (i :: * -> *) a.
IntervalAlgebraic i a =>
ComparativePredicateOf (i a)
finishes

-- | Filter by'finishedBy'.
filterFinishedBy :: (Filterable f
                  , IntervalAlgebraic Interval a
                  , IntervalAlgebraic i0 a
                  , IntervalAlgebraic i1 a) => 
                  i0 a -> f (i1 a) -> f (i1 a)
filterFinishedBy :: i0 a -> f (i1 a) -> f (i1 a)
filterFinishedBy = ComparativePredicateOf (Interval a) -> i0 a -> f (i1 a) -> f (i1 a)
forall (f :: * -> *) a (i0 :: * -> *) (i1 :: * -> *).
(Filterable f, IntervalAlgebraic Interval a,
 IntervalAlgebraic i0 a, IntervalAlgebraic i1 a) =>
ComparativePredicateOf (Interval a) -> i0 a -> f (i1 a) -> f (i1 a)
filterMaker ComparativePredicateOf (Interval a)
forall (i :: * -> *) a.
IntervalAlgebraic i a =>
ComparativePredicateOf (i a)
finishedBy 

-- | Filter by 'meets'.
filterMeets :: (Filterable f
                  , IntervalAlgebraic Interval a
                  , IntervalAlgebraic i0 a
                  , IntervalAlgebraic i1 a) => 
                  i0 a -> f (i1 a) -> f (i1 a)
filterMeets :: i0 a -> f (i1 a) -> f (i1 a)
filterMeets = ComparativePredicateOf (Interval a) -> i0 a -> f (i1 a) -> f (i1 a)
forall (f :: * -> *) a (i0 :: * -> *) (i1 :: * -> *).
(Filterable f, IntervalAlgebraic Interval a,
 IntervalAlgebraic i0 a, IntervalAlgebraic i1 a) =>
ComparativePredicateOf (Interval a) -> i0 a -> f (i1 a) -> f (i1 a)
filterMaker ComparativePredicateOf (Interval a)
forall (i :: * -> *) a.
IntervalAlgebraic i a =>
ComparativePredicateOf (i a)
meets

-- | Filter by 'metBy'.
filterMetBy :: (Filterable f
                  , IntervalAlgebraic Interval a
                  , IntervalAlgebraic i0 a
                  , IntervalAlgebraic i1 a) => 
                  i0 a -> f (i1 a) -> f (i1 a)
filterMetBy :: i0 a -> f (i1 a) -> f (i1 a)
filterMetBy = ComparativePredicateOf (Interval a) -> i0 a -> f (i1 a) -> f (i1 a)
forall (f :: * -> *) a (i0 :: * -> *) (i1 :: * -> *).
(Filterable f, IntervalAlgebraic Interval a,
 IntervalAlgebraic i0 a, IntervalAlgebraic i1 a) =>
ComparativePredicateOf (Interval a) -> i0 a -> f (i1 a) -> f (i1 a)
filterMaker ComparativePredicateOf (Interval a)
forall (i :: * -> *) a.
IntervalAlgebraic i a =>
ComparativePredicateOf (i a)
metBy

-- | Filter by 'during'.
filterDuring :: (Filterable f
                  , IntervalAlgebraic Interval a
                  , IntervalAlgebraic i0 a
                  , IntervalAlgebraic i1 a) => 
                  i0 a -> f (i1 a) -> f (i1 a)
filterDuring :: i0 a -> f (i1 a) -> f (i1 a)
filterDuring = ComparativePredicateOf (Interval a) -> i0 a -> f (i1 a) -> f (i1 a)
forall (f :: * -> *) a (i0 :: * -> *) (i1 :: * -> *).
(Filterable f, IntervalAlgebraic Interval a,
 IntervalAlgebraic i0 a, IntervalAlgebraic i1 a) =>
ComparativePredicateOf (Interval a) -> i0 a -> f (i1 a) -> f (i1 a)
filterMaker ComparativePredicateOf (Interval a)
forall (i :: * -> *) a.
IntervalAlgebraic i a =>
ComparativePredicateOf (i a)
during

-- | Filter by 'contains'.
filterContains :: (Filterable f
                  , IntervalAlgebraic Interval a
                  , IntervalAlgebraic i0 a
                  , IntervalAlgebraic i1 a) => 
                  i0 a -> f (i1 a) -> f (i1 a)
filterContains :: i0 a -> f (i1 a) -> f (i1 a)
filterContains = ComparativePredicateOf (Interval a) -> i0 a -> f (i1 a) -> f (i1 a)
forall (f :: * -> *) a (i0 :: * -> *) (i1 :: * -> *).
(Filterable f, IntervalAlgebraic Interval a,
 IntervalAlgebraic i0 a, IntervalAlgebraic i1 a) =>
ComparativePredicateOf (Interval a) -> i0 a -> f (i1 a) -> f (i1 a)
filterMaker ComparativePredicateOf (Interval a)
forall (i :: * -> *) a.
IntervalAlgebraic i a =>
ComparativePredicateOf (i a)
contains

-- | Filter by 'equals'.
filterEquals :: (Filterable f
                  , IntervalAlgebraic Interval a
                  , IntervalAlgebraic i0 a
                  , IntervalAlgebraic i1 a) => 
                  i0 a -> f (i1 a) -> f (i1 a)
filterEquals :: i0 a -> f (i1 a) -> f (i1 a)
filterEquals = ComparativePredicateOf (Interval a) -> i0 a -> f (i1 a) -> f (i1 a)
forall (f :: * -> *) a (i0 :: * -> *) (i1 :: * -> *).
(Filterable f, IntervalAlgebraic Interval a,
 IntervalAlgebraic i0 a, IntervalAlgebraic i1 a) =>
ComparativePredicateOf (Interval a) -> i0 a -> f (i1 a) -> f (i1 a)
filterMaker ComparativePredicateOf (Interval a)
forall (i :: * -> *) a.
IntervalAlgebraic i a =>
ComparativePredicateOf (i a)
equals

-- | Filter by 'disjoint'.
filterDisjoint :: (Filterable f
                  , IntervalAlgebraic Interval a
                  , IntervalAlgebraic i0 a
                  , IntervalAlgebraic i1 a) => 
                  i0 a -> f (i1 a) -> f (i1 a)
filterDisjoint :: i0 a -> f (i1 a) -> f (i1 a)
filterDisjoint = ComparativePredicateOf (Interval a) -> i0 a -> f (i1 a) -> f (i1 a)
forall (f :: * -> *) a (i0 :: * -> *) (i1 :: * -> *).
(Filterable f, IntervalAlgebraic Interval a,
 IntervalAlgebraic i0 a, IntervalAlgebraic i1 a) =>
ComparativePredicateOf (Interval a) -> i0 a -> f (i1 a) -> f (i1 a)
filterMaker ComparativePredicateOf (Interval a)
forall (i :: * -> *) a.
IntervalAlgebraic i a =>
ComparativePredicateOf (i a)
disjoint

-- | Filter by 'notDisjoint'.
filterNotDisjoint :: (Filterable f
                  , IntervalAlgebraic Interval a
                  , IntervalAlgebraic i0 a
                  , IntervalAlgebraic i1 a) => 
                  i0 a -> f (i1 a) -> f (i1 a)
filterNotDisjoint :: i0 a -> f (i1 a) -> f (i1 a)
filterNotDisjoint = ComparativePredicateOf (Interval a) -> i0 a -> f (i1 a) -> f (i1 a)
forall (f :: * -> *) a (i0 :: * -> *) (i1 :: * -> *).
(Filterable f, IntervalAlgebraic Interval a,
 IntervalAlgebraic i0 a, IntervalAlgebraic i1 a) =>
ComparativePredicateOf (Interval a) -> i0 a -> f (i1 a) -> f (i1 a)
filterMaker ComparativePredicateOf (Interval a)
forall (i :: * -> *) a.
IntervalAlgebraic i a =>
ComparativePredicateOf (i a)
notDisjoint

-- | Filter by 'concur'.
filterConcur ::  (Filterable f
                  , IntervalAlgebraic Interval a
                  , IntervalAlgebraic i0 a
                  , IntervalAlgebraic i1 a) => 
                  i0 a -> f (i1 a) -> f (i1 a)
filterConcur :: i0 a -> f (i1 a) -> f (i1 a)
filterConcur = ComparativePredicateOf (Interval a) -> i0 a -> f (i1 a) -> f (i1 a)
forall (f :: * -> *) a (i0 :: * -> *) (i1 :: * -> *).
(Filterable f, IntervalAlgebraic Interval a,
 IntervalAlgebraic i0 a, IntervalAlgebraic i1 a) =>
ComparativePredicateOf (Interval a) -> i0 a -> f (i1 a) -> f (i1 a)
filterMaker ComparativePredicateOf (Interval a)
forall (i :: * -> *) a.
IntervalAlgebraic i a =>
ComparativePredicateOf (i a)
concur

-- | Filter by 'within'.
filterWithin :: (Filterable f
                  , IntervalAlgebraic Interval a
                  , IntervalAlgebraic i0 a
                  , IntervalAlgebraic i1 a) => 
                  i0 a -> f (i1 a) -> f (i1 a)
filterWithin :: i0 a -> f (i1 a) -> f (i1 a)
filterWithin = ComparativePredicateOf (Interval a) -> i0 a -> f (i1 a) -> f (i1 a)
forall (f :: * -> *) a (i0 :: * -> *) (i1 :: * -> *).
(Filterable f, IntervalAlgebraic Interval a,
 IntervalAlgebraic i0 a, IntervalAlgebraic i1 a) =>
ComparativePredicateOf (Interval a) -> i0 a -> f (i1 a) -> f (i1 a)
filterMaker ComparativePredicateOf (Interval a)
forall (i :: * -> *) a.
IntervalAlgebraic i a =>
ComparativePredicateOf (i a)
within

-- | Filter by 'enclose'.
filterEnclose :: (Filterable f
                  , IntervalAlgebraic Interval a
                  , IntervalAlgebraic i0 a
                  , IntervalAlgebraic i1 a) => 
                  i0 a -> f (i1 a) -> f (i1 a)
filterEnclose :: i0 a -> f (i1 a) -> f (i1 a)
filterEnclose = ComparativePredicateOf (Interval a) -> i0 a -> f (i1 a) -> f (i1 a)
forall (f :: * -> *) a (i0 :: * -> *) (i1 :: * -> *).
(Filterable f, IntervalAlgebraic Interval a,
 IntervalAlgebraic i0 a, IntervalAlgebraic i1 a) =>
ComparativePredicateOf (Interval a) -> i0 a -> f (i1 a) -> f (i1 a)
filterMaker ComparativePredicateOf (Interval a)
forall (i :: * -> *) a.
IntervalAlgebraic i a =>
ComparativePredicateOf (i a)
enclose

-- | Filter by 'enclosedBy'.
filterEnclosedBy :: (Filterable f
                  , IntervalAlgebraic Interval a
                  , IntervalAlgebraic i0 a
                  , IntervalAlgebraic i1 a) => 
                  i0 a -> f (i1 a) -> f (i1 a)
filterEnclosedBy :: i0 a -> f (i1 a) -> f (i1 a)
filterEnclosedBy = ComparativePredicateOf (Interval a) -> i0 a -> f (i1 a) -> f (i1 a)
forall (f :: * -> *) a (i0 :: * -> *) (i1 :: * -> *).
(Filterable f, IntervalAlgebraic Interval a,
 IntervalAlgebraic i0 a, IntervalAlgebraic i1 a) =>
ComparativePredicateOf (Interval a) -> i0 a -> f (i1 a) -> f (i1 a)
filterMaker ComparativePredicateOf (Interval a)
forall (i :: * -> *) a.
IntervalAlgebraic i a =>
ComparativePredicateOf (i a)
enclosedBy