{-# LANGUAGE FlexibleInstances,FlexibleContexts,FunctionalDependencies,MultiParamTypeClasses,CPP #-}
module Data.Interval (
Interval(..),
IntersectionQuery(..),
Adjust(..),
TimeDifference(..),
intersects,properlyIntersects,contains,properlyContains,
covered,coveredBy,overlapTime,prevailing,fractionCovered,
overlap,
intervalDuration,
maybeUnion,maybeIntersection,
hull,
hullSeq,
without,
contiguous,components,componentsSeq,
sortByLeft,
fromEndPoints,
ITree,
itree,
emptyITree,
insert,
hullOfTree,
intersecting,getIntersectsIT,getProperIntersectsIT,
someIntersectsIT,someProperlyIntersectsIT,
leftmostInterval,
findSeq, existsSeq, hullSeqNonOverlap,
invariant, toTree,
intersectingProperly,
filterM,
joinSeq,
splitSeq,
) where
import Data.Tree (Tree)
import qualified Data.Tree as Tree
import qualified Data.Sequence as Seq
import qualified Data.Monoid ((<>))
import Data.Traversable (Traversable)
import Data.Foldable (toList, maximumBy, foldl', foldr')
import Data.Sequence (Seq, ViewL(EmptyL,(:<)), ViewR(EmptyR,(:>)), (><), (<|))
import Data.Function (on)
import Data.Functor.Identity (Identity(Identity, runIdentity))
import Data.Maybe (catMaybes)
import Data.Time (UTCTime, addUTCTime, diffUTCTime, utc, NominalDiffTime)
#if MIN_VERSION_time(1,9,0)
import Data.Time (LocalTime, utcToLocalTime, zonedTimeToLocalTime, diffLocalTime, addLocalTime)
#else
import Data.Time (LocalTime, utcToLocalTime, zonedTimeToLocalTime)
#endif
import Data.Time (ZonedTime, localTimeToUTC, zonedTimeToUTC)
import Control.Arrow ((***))
import Control.Applicative (Alternative, empty, (<|>))
class (Ord e) => Interval e i | i -> e where
lb :: i -> e
lb = (e, e) -> e
forall a b. (a, b) -> a
fst((e, e) -> e) -> (i -> (e, e)) -> i -> e
forall b c a. (b -> c) -> (a -> b) -> a -> c
.i -> (e, e)
forall e i. Interval e i => i -> (e, e)
endPoints
ub :: i -> e
ub = (e, e) -> e
forall a b. (a, b) -> b
snd((e, e) -> e) -> (i -> (e, e)) -> i -> e
forall b c a. (b -> c) -> (a -> b) -> a -> c
.i -> (e, e)
forall e i. Interval e i => i -> (e, e)
endPoints
endPoints :: i -> (e,e)
endPoints i
i = (i -> e
forall e i. Interval e i => i -> e
lb i
i,i -> e
forall e i. Interval e i => i -> e
ub i
i)
{-# MINIMAL (lb,ub) | endPoints #-}
instance (Ord e) => Interval e (e,e) where
endPoints :: (e, e) -> (e, e)
endPoints = (e, e) -> (e, e)
forall a. a -> a
id
instance (Ord e) => Interval e (Identity e) where
lb :: Identity e -> e
lb = Identity e -> e
forall a. Identity a -> a
runIdentity
ub :: Identity e -> e
ub = Identity e -> e
forall a. Identity a -> a
runIdentity
class Foldable f => IntersectionQuery t e f | t -> f where
getIntersects :: (Interval e i, Interval e j) => i -> t j -> f j
getProperIntersects :: (Interval e i, Interval e j) => i -> t j -> f j
someIntersects :: (Interval e i, Interval e j) => i -> t j -> Bool
someProperlyIntersects :: (Interval e i, Interval e j) => i -> t j -> Bool
instance Ord e => IntersectionQuery (ITree e) e Seq where
getIntersects :: i -> ITree e j -> Seq j
getIntersects = i -> ITree e j -> Seq j
forall e i j.
(Interval e i, Interval e j) =>
i -> ITree e j -> Seq j
getIntersectsIT
getProperIntersects :: i -> ITree e j -> Seq j
getProperIntersects = i -> ITree e j -> Seq j
forall e i j.
(Interval e i, Interval e j) =>
i -> ITree e j -> Seq j
getProperIntersectsIT
someIntersects :: i -> ITree e j -> Bool
someIntersects = i -> ITree e j -> Bool
forall e i j.
(Interval e i, Interval e j) =>
i -> ITree e j -> Bool
someIntersectsIT
someProperlyIntersects :: i -> ITree e j -> Bool
someProperlyIntersects = i -> ITree e j -> Bool
forall e i j.
(Interval e i, Interval e j) =>
i -> ITree e j -> Bool
someProperlyIntersectsIT
instance Ord e => IntersectionQuery Seq e Seq where
getIntersects :: i -> Seq j -> Seq j
getIntersects = (i -> (e, e) -> Bool) -> i -> Seq j -> Seq j
forall e i j.
(Interval e i, Interval e j) =>
(i -> (e, e) -> Bool) -> i -> Seq j -> Seq j
findSeq i -> (e, e) -> Bool
forall e i j. (Interval e i, Interval e j) => i -> j -> Bool
intersects
getProperIntersects :: i -> Seq j -> Seq j
getProperIntersects = (i -> (e, e) -> Bool) -> i -> Seq j -> Seq j
forall e i j.
(Interval e i, Interval e j) =>
(i -> (e, e) -> Bool) -> i -> Seq j -> Seq j
findSeq i -> (e, e) -> Bool
forall e i j. (Interval e i, Interval e j) => i -> j -> Bool
properlyIntersects
someIntersects :: i -> Seq j -> Bool
someIntersects = (i -> (e, e) -> Bool) -> i -> Seq j -> Bool
forall e i j.
(Interval e i, Interval e j) =>
(i -> (e, e) -> Bool) -> i -> Seq j -> Bool
existsSeq i -> (e, e) -> Bool
forall e i j. (Interval e i, Interval e j) => i -> j -> Bool
intersects
someProperlyIntersects :: i -> Seq j -> Bool
someProperlyIntersects = (i -> (e, e) -> Bool) -> i -> Seq j -> Bool
forall e i j.
(Interval e i, Interval e j) =>
(i -> (e, e) -> Bool) -> i -> Seq j -> Bool
existsSeq i -> (e, e) -> Bool
forall e i j. (Interval e i, Interval e j) => i -> j -> Bool
properlyIntersects
class TimeDifference t where
diffTime :: t -> t -> NominalDiffTime
addTime :: NominalDiffTime -> t -> t
instance TimeDifference UTCTime where
diffTime :: UTCTime -> UTCTime -> NominalDiffTime
diffTime = UTCTime -> UTCTime -> NominalDiffTime
diffUTCTime
addTime :: NominalDiffTime -> UTCTime -> UTCTime
addTime = NominalDiffTime -> UTCTime -> UTCTime
addUTCTime
#if MIN_VERSION_time(1,9,0)
instance TimeDifference LocalTime where
diffTime :: LocalTime -> LocalTime -> NominalDiffTime
diffTime = LocalTime -> LocalTime -> NominalDiffTime
diffLocalTime
addTime :: NominalDiffTime -> LocalTime -> LocalTime
addTime = NominalDiffTime -> LocalTime -> LocalTime
addLocalTime
#else
instance TimeDifference LocalTime where
diffTime x y = diffUTCTime (localTimeToUTC utc x) (localTimeToUTC utc y)
addTime x = utcToLocalTime utc . addUTCTime x . localTimeToUTC utc
#endif
instance TimeDifference ZonedTime where
diffTime :: ZonedTime -> ZonedTime -> NominalDiffTime
diffTime ZonedTime
x ZonedTime
y = UTCTime -> UTCTime -> NominalDiffTime
diffUTCTime (ZonedTime -> UTCTime
zonedTimeToUTC ZonedTime
x) (ZonedTime -> UTCTime
zonedTimeToUTC ZonedTime
y)
addTime :: NominalDiffTime -> ZonedTime -> ZonedTime
addTime NominalDiffTime
x ZonedTime
z = ZonedTime
z {zonedTimeToLocalTime :: LocalTime
zonedTimeToLocalTime = NominalDiffTime -> LocalTime -> LocalTime
forall t. TimeDifference t => NominalDiffTime -> t -> t
addTime NominalDiffTime
x (ZonedTime -> LocalTime
zonedTimeToLocalTime ZonedTime
z)}
intervalDuration :: (TimeDifference t, Interval t i) => i -> NominalDiffTime
intervalDuration :: i -> NominalDiffTime
intervalDuration i
i = t -> t -> NominalDiffTime
forall t. TimeDifference t => t -> t -> NominalDiffTime
diffTime (i -> t
forall e i. Interval e i => i -> e
ub i
i) (i -> t
forall e i. Interval e i => i -> e
lb i
i)
overlapTime :: (TimeDifference t, Interval t i, Interval t j) =>
i -> j -> NominalDiffTime
overlapTime :: i -> j -> NominalDiffTime
overlapTime i
i j
j = let
x :: t
x = t -> t -> t
forall a. Ord a => a -> a -> a
max (i -> t
forall e i. Interval e i => i -> e
lb i
i) (j -> t
forall e i. Interval e i => i -> e
lb j
j)
y :: t
y = t -> t -> t
forall a. Ord a => a -> a -> a
min (i -> t
forall e i. Interval e i => i -> e
ub i
i) (j -> t
forall e i. Interval e i => i -> e
ub j
j)
in if t
x t -> t -> Bool
forall a. Ord a => a -> a -> Bool
< t
y then t -> t -> NominalDiffTime
forall t. TimeDifference t => t -> t -> NominalDiffTime
diffTime t
y t
x else NominalDiffTime
0
prevailing :: (Interval t i, Interval t j, TimeDifference t) =>
i -> Seq (a,j) -> Maybe a
prevailing :: i -> Seq (a, j) -> Maybe a
prevailing i
i Seq (a, j)
js =
let ks :: Seq (a, j)
ks = ((a, j) -> Bool) -> Seq (a, j) -> Seq (a, j)
forall a. (a -> Bool) -> Seq a -> Seq a
Seq.filter (i -> j -> Bool
forall e i j. (Interval e i, Interval e j) => i -> j -> Bool
intersects i
i (j -> Bool) -> ((a, j) -> j) -> (a, j) -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (a, j) -> j
forall a b. (a, b) -> b
snd) Seq (a, j)
js
in if Seq (a, j) -> Bool
forall a. Seq a -> Bool
Seq.null Seq (a, j)
ks
then Maybe a
forall a. Maybe a
Nothing
else a -> Maybe a
forall a. a -> Maybe a
Just (a -> Maybe a) -> a -> Maybe a
forall a b. (a -> b) -> a -> b
$ (a, j) -> a
forall a b. (a, b) -> a
fst ((a, j) -> a) -> (a, j) -> a
forall a b. (a -> b) -> a -> b
$ ((a, j) -> (a, j) -> Ordering) -> Seq (a, j) -> (a, j)
forall (t :: * -> *) a.
Foldable t =>
(a -> a -> Ordering) -> t a -> a
maximumBy (NominalDiffTime -> NominalDiffTime -> Ordering
forall a. Ord a => a -> a -> Ordering
compare (NominalDiffTime -> NominalDiffTime -> Ordering)
-> ((a, j) -> NominalDiffTime) -> (a, j) -> (a, j) -> Ordering
forall b c a. (b -> b -> c) -> (a -> b) -> a -> a -> c
`on` (i -> j -> NominalDiffTime
forall t i j.
(TimeDifference t, Interval t i, Interval t j) =>
i -> j -> NominalDiffTime
overlapTime i
i (j -> NominalDiffTime)
-> ((a, j) -> j) -> (a, j) -> NominalDiffTime
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (a, j) -> j
forall a b. (a, b) -> b
snd)) Seq (a, j)
ks
class Interval e i => Adjust e i | i -> e where
adjustBounds :: (e -> e) -> (e -> e) -> i -> i
shift :: (e -> e) -> i -> i
shift e -> e
f = (e -> e) -> (e -> e) -> i -> i
forall e i. Adjust e i => (e -> e) -> (e -> e) -> i -> i
adjustBounds e -> e
f e -> e
f
{-# MINIMAL (adjustBounds) #-}
instance Ord e => Adjust e (e,e) where
adjustBounds :: (e -> e) -> (e -> e) -> (e, e) -> (e, e)
adjustBounds e -> e
f e -> e
g (e
x,e
y) = (e -> e
f e
x,e -> e
g e
y)
maybeUnion :: (Interval e j, Interval e i, Adjust e i) => j -> i -> Maybe i
maybeUnion :: j -> i -> Maybe i
maybeUnion j
j i
i = if j
j j -> i -> Bool
forall e i j. (Interval e i, Interval e j) => i -> j -> Bool
`intersects` i
i
then i -> Maybe i
forall a. a -> Maybe a
Just ((e -> e) -> (e -> e) -> i -> i
forall e i. Adjust e i => (e -> e) -> (e -> e) -> i -> i
adjustBounds (e -> e -> e
forall a. Ord a => a -> a -> a
min (j -> e
forall e i. Interval e i => i -> e
lb j
j)) (e -> e -> e
forall a. Ord a => a -> a -> a
max (j -> e
forall e i. Interval e i => i -> e
ub j
j)) i
i)
else Maybe i
forall a. Maybe a
Nothing
maybeIntersection :: (Interval e j, Interval e i, Adjust e i) => j -> i -> Maybe i
maybeIntersection :: j -> i -> Maybe i
maybeIntersection j
j i
i = if j
j j -> i -> Bool
forall e i j. (Interval e i, Interval e j) => i -> j -> Bool
`intersects` i
i
then i -> Maybe i
forall a. a -> Maybe a
Just ((e -> e) -> (e -> e) -> i -> i
forall e i. Adjust e i => (e -> e) -> (e -> e) -> i -> i
adjustBounds (e -> e -> e
forall a. Ord a => a -> a -> a
max (j -> e
forall e i. Interval e i => i -> e
lb j
j)) (e -> e -> e
forall a. Ord a => a -> a -> a
min (j -> e
forall e i. Interval e i => i -> e
ub j
j)) i
i)
else Maybe i
forall a. Maybe a
Nothing
hull :: (Interval e i,Foldable f,Functor f) => f i -> Maybe (e,e)
hull :: f i -> Maybe (e, e)
hull f i
xs = if f i -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null f i
xs
then Maybe (e, e)
forall a. Maybe a
Nothing
else (e, e) -> Maybe (e, e)
forall a. a -> Maybe a
Just (f e -> e
forall (t :: * -> *) a. (Foldable t, Ord a) => t a -> a
minimum ((i -> e) -> f i -> f e
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap i -> e
forall e i. Interval e i => i -> e
lb f i
xs), f e -> e
forall (t :: * -> *) a. (Foldable t, Ord a) => t a -> a
maximum ((i -> e) -> f i -> f e
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap i -> e
forall e i. Interval e i => i -> e
ub f i
xs))
without :: (Adjust e i,Interval e j) => i -> j -> [i]
without :: i -> j -> [i]
without i
i j
j = if j
j j -> i -> Bool
forall e i j. (Interval e i, Interval e j) => i -> j -> Bool
`contains` i
i then [] else
if j -> e
forall e i. Interval e i => i -> e
ub j
j e -> e -> Bool
forall a. Ord a => a -> a -> Bool
<= i -> e
forall e i. Interval e i => i -> e
lb i
i Bool -> Bool -> Bool
|| j -> e
forall e i. Interval e i => i -> e
lb j
j e -> e -> Bool
forall a. Ord a => a -> a -> Bool
>= i -> e
forall e i. Interval e i => i -> e
ub i
i
then [i
i]
else if i
i i -> j -> Bool
forall e i j. (Interval e i, Interval e j) => i -> j -> Bool
`properlyContains` j
j
then [(e -> e) -> (e -> e) -> i -> i
forall e i. Adjust e i => (e -> e) -> (e -> e) -> i -> i
adjustBounds e -> e
forall a. a -> a
id (e -> e -> e
forall a b. a -> b -> a
const (j -> e
forall e i. Interval e i => i -> e
lb j
j)) i
i,(e -> e) -> (e -> e) -> i -> i
forall e i. Adjust e i => (e -> e) -> (e -> e) -> i -> i
adjustBounds (e -> e -> e
forall a b. a -> b -> a
const (j -> e
forall e i. Interval e i => i -> e
ub j
j)) e -> e
forall a. a -> a
id i
i]
else if j -> e
forall e i. Interval e i => i -> e
lb j
j e -> e -> Bool
forall a. Ord a => a -> a -> Bool
<= i -> e
forall e i. Interval e i => i -> e
lb i
i
then [(e -> e) -> (e -> e) -> i -> i
forall e i. Adjust e i => (e -> e) -> (e -> e) -> i -> i
adjustBounds (e -> e -> e
forall a b. a -> b -> a
const (j -> e
forall e i. Interval e i => i -> e
ub j
j)) e -> e
forall a. a -> a
id i
i]
else [(e -> e) -> (e -> e) -> i -> i
forall e i. Adjust e i => (e -> e) -> (e -> e) -> i -> i
adjustBounds e -> e
forall a. a -> a
id (e -> e -> e
forall a b. a -> b -> a
const (j -> e
forall e i. Interval e i => i -> e
lb j
j)) i
i]
contiguous :: Interval e i => [i] -> [[i]]
contiguous :: [i] -> [[i]]
contiguous [] = []
contiguous (i
i:[i]
is) = (i
ii -> [i] -> [i]
forall a. a -> [a] -> [a]
:[i]
js) [i] -> [[i]] -> [[i]]
forall a. a -> [a] -> [a]
: [i] -> [[i]]
forall e i. Interval e i => [i] -> [[i]]
contiguous [i]
ks where
([i]
js,[i]
ks) = (e, e) -> [i] -> ([i], [i])
forall e i. Interval e i => (e, e) -> [i] -> ([i], [i])
go (i -> (e, e)
forall e i. Interval e i => i -> (e, e)
endPoints i
i) [i]
is
go :: Interval e i => (e,e) -> [i] -> ([i],[i])
go :: (e, e) -> [i] -> ([i], [i])
go j :: (e, e)
j@(e
x,e
_y) ls :: [i]
ls@(i
l:[i]
ls') = if (e, e)
j (e, e) -> i -> Bool
forall e i j. (Interval e i, Interval e j) => i -> j -> Bool
`intersects` i
l
then let ([i]
foo,[i]
bar) = (e, e) -> [i] -> ([i], [i])
forall e i. Interval e i => (e, e) -> [i] -> ([i], [i])
go (e
x,i -> e
forall e i. Interval e i => i -> e
ub i
l) [i]
ls' in (i
li -> [i] -> [i]
forall a. a -> [a] -> [a]
:[i]
foo,[i]
bar)
else ([],[i]
ls)
go (e, e)
_ [] = ([],[])
components :: (Interval e i, Adjust e i) => [i] -> [i]
components :: [i] -> [i]
components [] = []
components (i
i:[i]
is) = i -> [i] -> [i]
forall e t. Adjust e t => t -> [t] -> [t]
c i
i [i]
is where
c :: t -> [t] -> [t]
c t
x [] = [t
x]
c t
x (t
y:[t]
ys) = case t -> t -> Maybe t
forall e j i.
(Interval e j, Interval e i, Adjust e i) =>
j -> i -> Maybe i
maybeUnion t
x t
y of
Maybe t
Nothing -> t
x t -> [t] -> [t]
forall a. a -> [a] -> [a]
: t -> [t] -> [t]
c t
y [t]
ys
Just t
z -> t -> [t] -> [t]
c t
z [t]
ys
componentsSeq :: (Interval e i, Adjust e i) => Seq i -> Seq i
componentsSeq :: Seq i -> Seq i
componentsSeq Seq i
ys = case Seq i -> ViewL i
forall a. Seq a -> ViewL a
Seq.viewl Seq i
ys of
ViewL i
EmptyL -> Seq i
forall (f :: * -> *) a. Alternative f => f a
empty
i
x :< Seq i
xs -> i -> Seq i -> Seq i
forall e t. Adjust e t => t -> Seq t -> Seq t
c i
x Seq i
xs where
c :: t -> Seq t -> Seq t
c t
a Seq t
bs = case Seq t -> ViewL t
forall a. Seq a -> ViewL a
Seq.viewl Seq t
bs of
ViewL t
EmptyL -> t -> Seq t
forall a. a -> Seq a
Seq.singleton t
a
t
b :< Seq t
bs' -> case t -> t -> Maybe t
forall e j i.
(Interval e j, Interval e i, Adjust e i) =>
j -> i -> Maybe i
maybeUnion t
a t
b of
Maybe t
Nothing -> t
a t -> Seq t -> Seq t
forall a. a -> Seq a -> Seq a
<| t -> Seq t -> Seq t
c t
b Seq t
bs'
Just t
ab -> t -> Seq t -> Seq t
c t
ab Seq t
bs'
covered :: (Interval e i,Interval e j,Adjust e j) => i -> Seq j -> Seq j
covered :: i -> Seq j -> Seq j
covered i
i =
let mapMaybe :: (a -> t a) -> t a -> Seq a
mapMaybe a -> t a
f = (a -> Seq a) -> t a -> Seq a
forall (t :: * -> *) m a.
(Foldable t, Monoid m) =>
(a -> m) -> t a -> m
foldMap ((a -> Seq a) -> t a -> Seq a
forall (t :: * -> *) m a.
(Foldable t, Monoid m) =>
(a -> m) -> t a -> m
foldMap a -> Seq a
forall a. a -> Seq a
Seq.singleton (t a -> Seq a) -> (a -> t a) -> a -> Seq a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> t a
f)
in Seq j -> Seq j
forall e i. (Interval e i, Adjust e i) => Seq i -> Seq i
componentsSeq (Seq j -> Seq j) -> (Seq j -> Seq j) -> Seq j -> Seq j
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Seq j -> Seq j
forall e i. Interval e i => Seq i -> Seq i
sortByLeft (Seq j -> Seq j) -> (Seq j -> Seq j) -> Seq j -> Seq j
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (j -> Maybe j) -> Seq j -> Seq j
forall (t :: * -> *) (t :: * -> *) a a.
(Foldable t, Foldable t) =>
(a -> t a) -> t a -> Seq a
mapMaybe (i -> j -> Maybe j
forall e j i.
(Interval e j, Interval e i, Adjust e i) =>
j -> i -> Maybe i
maybeIntersection i
i)
coveredBy :: (Interval e i, Interval e j, Foldable f) => i -> f j -> Bool
i
i coveredBy :: i -> f j -> Bool
`coveredBy` f j
js = [(e, e)] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null ([(e, e)] -> Bool) -> [(e, e)] -> Bool
forall a b. (a -> b) -> a -> b
$ ([(e, e)] -> j -> [(e, e)]) -> [(e, e)] -> f j -> [(e, e)]
forall (t :: * -> *) b a.
Foldable t =>
(b -> a -> b) -> b -> t a -> b
foldl (\[(e, e)]
remains j
j -> ((e, e) -> j -> [(e, e)]) -> j -> (e, e) -> [(e, e)]
forall a b c. (a -> b -> c) -> b -> a -> c
flip (e, e) -> j -> [(e, e)]
forall e i j. (Adjust e i, Interval e j) => i -> j -> [i]
without j
j ((e, e) -> [(e, e)]) -> [(e, e)] -> [(e, e)]
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< [(e, e)]
remains) [i -> (e, e)
forall e i. Interval e i => i -> (e, e)
endPoints i
i] f j
js
fractionCovered :: (TimeDifference t, Interval t i, Interval t j, Fractional a) =>
j -> Seq i -> a
fractionCovered :: j -> Seq i -> a
fractionCovered j
i Seq i
xs = let
totalTime :: NominalDiffTime
totalTime = j -> NominalDiffTime
forall t i.
(TimeDifference t, Interval t i) =>
i -> NominalDiffTime
intervalDuration j
i
coveredTime :: NominalDiffTime
coveredTime = (NominalDiffTime -> (t, t) -> NominalDiffTime)
-> NominalDiffTime -> Seq (t, t) -> NominalDiffTime
forall (t :: * -> *) b a.
Foldable t =>
(b -> a -> b) -> b -> t a -> b
foldl' (\NominalDiffTime
s (t, t)
j -> NominalDiffTime
s NominalDiffTime -> NominalDiffTime -> NominalDiffTime
forall a. Num a => a -> a -> a
+ (t, t) -> NominalDiffTime
forall t i.
(TimeDifference t, Interval t i) =>
i -> NominalDiffTime
intervalDuration (t, t)
j) NominalDiffTime
0 (Seq (t, t) -> NominalDiffTime) -> Seq (t, t) -> NominalDiffTime
forall a b. (a -> b) -> a -> b
$ j -> Seq (t, t) -> Seq (t, t)
forall e i j.
(Interval e i, Interval e j, Adjust e j) =>
i -> Seq j -> Seq j
covered j
i (Seq (t, t) -> Seq (t, t)) -> Seq (t, t) -> Seq (t, t)
forall a b. (a -> b) -> a -> b
$ (i -> (t, t)) -> Seq i -> Seq (t, t)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap i -> (t, t)
forall e i. Interval e i => i -> (e, e)
endPoints Seq i
xs
in if NominalDiffTime
totalTimeNominalDiffTime -> NominalDiffTime -> Bool
forall a. Eq a => a -> a -> Bool
==NominalDiffTime
0 then a
1 else (Rational -> a
forall a. Fractional a => Rational -> a
fromRational(Rational -> a)
-> (NominalDiffTime -> Rational) -> NominalDiffTime -> a
forall b c a. (b -> c) -> (a -> b) -> a -> c
.NominalDiffTime -> Rational
forall a. Real a => a -> Rational
toRational) (NominalDiffTime
coveredTimeNominalDiffTime -> NominalDiffTime -> NominalDiffTime
forall a. Fractional a => a -> a -> a
/NominalDiffTime
totalTime)
overlap :: Interval e i => i -> i -> Ordering
overlap :: i -> i -> Ordering
overlap i
i i
j = case (e -> e -> Ordering
forall a. Ord a => a -> a -> Ordering
compare (i -> e
forall e i. Interval e i => i -> e
ub i
i) (i -> e
forall e i. Interval e i => i -> e
lb i
j),e -> e -> Ordering
forall a. Ord a => a -> a -> Ordering
compare (i -> e
forall e i. Interval e i => i -> e
ub i
j) (i -> e
forall e i. Interval e i => i -> e
lb i
i)) of
(Ordering
LT,Ordering
_) -> Ordering
LT
(Ordering
_,Ordering
LT) -> Ordering
GT
(Ordering, Ordering)
_ -> Ordering
EQ
intersects :: (Interval e i,Interval e j) => i -> j -> Bool
i
i intersects :: i -> j -> Bool
`intersects` j
j = Bool -> Bool
not (i -> e
forall e i. Interval e i => i -> e
ub i
i e -> e -> Bool
forall a. Ord a => a -> a -> Bool
< j -> e
forall e i. Interval e i => i -> e
lb j
j Bool -> Bool -> Bool
|| j -> e
forall e i. Interval e i => i -> e
ub j
j e -> e -> Bool
forall a. Ord a => a -> a -> Bool
< i -> e
forall e i. Interval e i => i -> e
lb i
i)
properlyIntersects :: (Interval e i,Interval e j) => i -> j -> Bool
i
i properlyIntersects :: i -> j -> Bool
`properlyIntersects` j
j = Bool -> Bool
not (i -> e
forall e i. Interval e i => i -> e
ub i
i e -> e -> Bool
forall a. Ord a => a -> a -> Bool
<= j -> e
forall e i. Interval e i => i -> e
lb j
j Bool -> Bool -> Bool
|| j -> e
forall e i. Interval e i => i -> e
ub j
j e -> e -> Bool
forall a. Ord a => a -> a -> Bool
<= i -> e
forall e i. Interval e i => i -> e
lb i
i)
contains :: (Interval e i,Interval e j) => i -> j -> Bool
i
i contains :: i -> j -> Bool
`contains` j
j = i -> e
forall e i. Interval e i => i -> e
lb i
i e -> e -> Bool
forall a. Ord a => a -> a -> Bool
<= j -> e
forall e i. Interval e i => i -> e
lb j
j Bool -> Bool -> Bool
&& j -> e
forall e i. Interval e i => i -> e
ub j
j e -> e -> Bool
forall a. Ord a => a -> a -> Bool
<= i -> e
forall e i. Interval e i => i -> e
ub i
i
properlyContains :: (Interval e i,Interval e j) => i -> j -> Bool
i
i properlyContains :: i -> j -> Bool
`properlyContains` j
j = i -> e
forall e i. Interval e i => i -> e
lb i
i e -> e -> Bool
forall a. Ord a => a -> a -> Bool
< j -> e
forall e i. Interval e i => i -> e
lb j
j Bool -> Bool -> Bool
&& i -> e
forall e i. Interval e i => i -> e
ub i
i e -> e -> Bool
forall a. Ord a => a -> a -> Bool
> j -> e
forall e i. Interval e i => i -> e
ub j
j
fromEndPoints :: (Ord e) => [e] -> Seq (e,e)
fromEndPoints :: [e] -> Seq (e, e)
fromEndPoints [] = Seq (e, e)
forall (f :: * -> *) a. Alternative f => f a
empty
fromEndPoints [e
_] = Seq (e, e)
forall (f :: * -> *) a. Alternative f => f a
empty
fromEndPoints [e
x,e
y] = if e
x e -> e -> Bool
forall a. Ord a => a -> a -> Bool
<= e
y then (e, e) -> Seq (e, e)
forall a. a -> Seq a
Seq.singleton (e
x,e
y) else [Char] -> Seq (e, e)
forall a. HasCallStack => [Char] -> a
error [Char]
"unsorted list"
fromEndPoints (e
x:[e]
xs) = let s :: Seq (e, e)
s = [e] -> Seq (e, e)
forall e. Ord e => [e] -> Seq (e, e)
fromEndPoints [e]
xs in case Seq (e, e) -> ViewL (e, e)
forall a. Seq a -> ViewL a
Seq.viewl Seq (e, e)
s of
(e
y,e
_) :< Seq (e, e)
_ -> (e
x,e
y) (e, e) -> Seq (e, e) -> Seq (e, e)
forall a. a -> Seq a -> Seq a
<| Seq (e, e)
s
ViewL (e, e)
EmptyL -> [Char] -> Seq (e, e)
forall a. HasCallStack => [Char] -> a
error [Char]
"Intervals.fromEndPoints: this should never happen"
sortByLeft :: (Interval e i) => Seq i -> Seq i
sortByLeft :: Seq i -> Seq i
sortByLeft = (i -> i -> Ordering) -> Seq i -> Seq i
forall a. (a -> a -> Ordering) -> Seq a -> Seq a
Seq.sortBy (\i
i i
j -> e -> e -> Ordering
forall a. Ord a => a -> a -> Ordering
compare (i -> e
forall e i. Interval e i => i -> e
lb i
i) (i -> e
forall e i. Interval e i => i -> e
lb i
j) Ordering -> Ordering -> Ordering
forall a. Semigroup a => a -> a -> a
<> e -> e -> Ordering
forall a. Ord a => a -> a -> Ordering
compare (i -> e
forall e i. Interval e i => i -> e
ub i
j) (i -> e
forall e i. Interval e i => i -> e
ub i
i))
intersecting :: (Interval e i,Interval e j) => j -> Seq i -> Seq i
intersecting :: j -> Seq i -> Seq i
intersecting j
j = (i -> Bool) -> Seq i -> Seq i
forall a. (a -> Bool) -> Seq a -> Seq a
Seq.filter (j -> i -> Bool
forall e i j. (Interval e i, Interval e j) => i -> j -> Bool
intersects j
j)
intersectingProperly :: (Interval e i,Interval e j) => j -> Seq i -> Seq i
intersectingProperly :: j -> Seq i -> Seq i
intersectingProperly j
j = (i -> Bool) -> Seq i -> Seq i
forall a. (a -> Bool) -> Seq a -> Seq a
Seq.filter (j -> i -> Bool
forall e i j. (Interval e i, Interval e j) => i -> j -> Bool
properlyIntersects j
j)
hullSeq :: Interval e i => Seq i -> Maybe (e,e)
hullSeq :: Seq i -> Maybe (e, e)
hullSeq Seq i
xs = case Seq i -> ViewL i
forall a. Seq a -> ViewL a
Seq.viewl Seq i
xs of
ViewL i
EmptyL -> Maybe (e, e)
forall a. Maybe a
Nothing
i
leftmost :< Seq i
_others -> (e, e) -> Maybe (e, e)
forall a. a -> Maybe a
Just (i -> e
forall e i. Interval e i => i -> e
lb i
leftmost, Seq e -> e
forall (t :: * -> *) a. (Foldable t, Ord a) => t a -> a
maximum ((i -> e) -> Seq i -> Seq e
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap i -> e
forall e i. Interval e i => i -> e
ub Seq i
xs))
data ITree e i = Bin (Seq i) | Split (Seq i) e e e (ITree e i) (ITree e i)
instance Functor (ITree e) where
fmap :: (a -> b) -> ITree e a -> ITree e b
fmap a -> b
f (Bin Seq a
xs) = Seq b -> ITree e b
forall e i. Seq i -> ITree e i
Bin ((a -> b) -> Seq a -> Seq b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap a -> b
f Seq a
xs)
fmap a -> b
f (Split Seq a
up e
x e
y e
z ITree e a
left ITree e a
right) = Seq b -> e -> e -> e -> ITree e b -> ITree e b -> ITree e b
forall e i.
Seq i -> e -> e -> e -> ITree e i -> ITree e i -> ITree e i
Split ((a -> b) -> Seq a -> Seq b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap a -> b
f Seq a
up) e
x e
y e
z ((a -> b) -> ITree e a -> ITree e b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap a -> b
f ITree e a
left) ((a -> b) -> ITree e a -> ITree e b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap a -> b
f ITree e a
right)
instance Foldable (ITree e) where
foldMap :: (a -> m) -> ITree e a -> m
foldMap a -> m
f (Bin Seq a
xs) = (a -> m) -> Seq a -> m
forall (t :: * -> *) m a.
(Foldable t, Monoid m) =>
(a -> m) -> t a -> m
foldMap a -> m
f Seq a
xs
foldMap a -> m
f (Split Seq a
up e
_ e
_ e
_ ITree e a
left ITree e a
right) = (a -> m) -> ITree e a -> m
forall (t :: * -> *) m a.
(Foldable t, Monoid m) =>
(a -> m) -> t a -> m
foldMap a -> m
f ITree e a
left m -> m -> m
forall a. Semigroup a => a -> a -> a
<> (a -> m) -> Seq a -> m
forall (t :: * -> *) m a.
(Foldable t, Monoid m) =>
(a -> m) -> t a -> m
foldMap a -> m
f Seq a
up m -> m -> m
forall a. Semigroup a => a -> a -> a
<> (a -> m) -> ITree e a -> m
forall (t :: * -> *) m a.
(Foldable t, Monoid m) =>
(a -> m) -> t a -> m
foldMap a -> m
f ITree e a
right
emptyITree :: ITree e i
emptyITree :: ITree e i
emptyITree = Seq i -> ITree e i
forall e i. Seq i -> ITree e i
Bin Seq i
forall (f :: * -> *) a. Alternative f => f a
empty
hullOfTree :: (Interval e i) => ITree e i -> Maybe (e,e)
hullOfTree :: ITree e i -> Maybe (e, e)
hullOfTree (Bin Seq i
xs) = Seq i -> Maybe (e, e)
forall e i. Interval e i => Seq i -> Maybe (e, e)
hullSeq Seq i
xs
hullOfTree (Split Seq i
_ e
x e
_ e
y ITree e i
_ ITree e i
_) = (e, e) -> Maybe (e, e)
forall a. a -> Maybe a
Just (e
x,e
y)
invariant :: Interval e i => ITree e i -> Bool
invariant :: ITree e i -> Bool
invariant (Bin Seq i
_) = Bool
True
invariant (Split Seq i
up e
x e
y e
z ITree e i
left ITree e i
right) = e
x e -> e -> Bool
forall a. Ord a => a -> a -> Bool
<= e
y Bool -> Bool -> Bool
&& e
y e -> e -> Bool
forall a. Ord a => a -> a -> Bool
<= e
z Bool -> Bool -> Bool
&& Bool
invUp Bool -> Bool -> Bool
&& Bool
invLeft Bool -> Bool -> Bool
&& Bool
invRight where
invUp :: Bool
invUp = (i -> Bool) -> Seq i -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
all (Identity e -> i -> Bool
forall e i j. (Interval e i, Interval e j) => i -> j -> Bool
intersects (e -> Identity e
forall a. a -> Identity a
Identity e
y)) Seq i
up Bool -> Bool -> Bool
&& (i -> Bool) -> Seq i -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
all ((e, e) -> i -> Bool
forall e i j. (Interval e i, Interval e j) => i -> j -> Bool
contains (e
x,e
z)) Seq i
up
invLeft :: Bool
invLeft = (i -> Bool) -> ITree e i -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
all ((e, e) -> i -> Bool
forall e i j. (Interval e i, Interval e j) => i -> j -> Bool
contains (e
x,e
y)) ITree e i
left Bool -> Bool -> Bool
&& ITree e i -> Bool
forall e i. Interval e i => ITree e i -> Bool
invariant ITree e i
left
invRight :: Bool
invRight = (i -> Bool) -> ITree e i -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
all ((e, e) -> i -> Bool
forall e i j. (Interval e i, Interval e j) => i -> j -> Bool
contains (e
y,e
z)) ITree e i
right Bool -> Bool -> Bool
&& ITree e i -> Bool
forall e i. Interval e i => ITree e i -> Bool
invariant ITree e i
right
getIntersectsIT :: (Interval e i, Interval e j) => i -> ITree e j -> Seq j
getIntersectsIT :: i -> ITree e j -> Seq j
getIntersectsIT i
i (Bin Seq j
bin) = i
i i -> Seq j -> Seq j
forall e i j. (Interval e i, Interval e j) => j -> Seq i -> Seq i
`intersecting` Seq j
bin
getIntersectsIT i
i (Split Seq j
up e
x e
y e
z ITree e j
left ITree e j
right) = let
m :: Seq j
m = i
i i -> Seq j -> Seq j
forall e i j. (Interval e i, Interval e j) => j -> Seq i -> Seq i
`intersecting` Seq j
up
l :: Seq j
l = if i
i i -> (e, e) -> Bool
forall e i j. (Interval e i, Interval e j) => i -> j -> Bool
`intersects` (e
x,e
y) then i -> ITree e j -> Seq j
forall e i j.
(Interval e i, Interval e j) =>
i -> ITree e j -> Seq j
getIntersectsIT i
i ITree e j
left else Seq j
forall (f :: * -> *) a. Alternative f => f a
empty
r :: Seq j
r = if i
i i -> (e, e) -> Bool
forall e i j. (Interval e i, Interval e j) => i -> j -> Bool
`intersects` (e
y,e
z) then i -> ITree e j -> Seq j
forall e i j.
(Interval e i, Interval e j) =>
i -> ITree e j -> Seq j
getIntersectsIT i
i ITree e j
right else Seq j
forall (f :: * -> *) a. Alternative f => f a
empty
in if i
i i -> (e, e) -> Bool
forall e i j. (Interval e i, Interval e j) => i -> j -> Bool
`intersects` (e
x,e
z) then Seq j
m Seq j -> Seq j -> Seq j
forall a. Seq a -> Seq a -> Seq a
>< Seq j
l Seq j -> Seq j -> Seq j
forall a. Seq a -> Seq a -> Seq a
>< Seq j
r else Seq j
forall (f :: * -> *) a. Alternative f => f a
empty
getProperIntersectsIT :: (Interval e i, Interval e j) => i -> ITree e j -> Seq j
getProperIntersectsIT :: i -> ITree e j -> Seq j
getProperIntersectsIT i
i (Bin Seq j
bin) = i
i i -> Seq j -> Seq j
forall e i j. (Interval e i, Interval e j) => j -> Seq i -> Seq i
`intersectingProperly` Seq j
bin
getProperIntersectsIT i
i (Split Seq j
up e
x e
y e
z ITree e j
left ITree e j
right) = let
m :: Seq j
m = i
i i -> Seq j -> Seq j
forall e i j. (Interval e i, Interval e j) => j -> Seq i -> Seq i
`intersectingProperly` Seq j
up
l :: Seq j
l = if i
i i -> (e, e) -> Bool
forall e i j. (Interval e i, Interval e j) => i -> j -> Bool
`properlyIntersects` (e
x,e
y) then i -> ITree e j -> Seq j
forall e i j.
(Interval e i, Interval e j) =>
i -> ITree e j -> Seq j
getProperIntersectsIT i
i ITree e j
left else Seq j
forall (f :: * -> *) a. Alternative f => f a
empty
r :: Seq j
r = if i
i i -> (e, e) -> Bool
forall e i j. (Interval e i, Interval e j) => i -> j -> Bool
`properlyIntersects` (e
y,e
z) then i -> ITree e j -> Seq j
forall e i j.
(Interval e i, Interval e j) =>
i -> ITree e j -> Seq j
getProperIntersectsIT i
i ITree e j
right else Seq j
forall (f :: * -> *) a. Alternative f => f a
empty
in if i
i i -> (e, e) -> Bool
forall e i j. (Interval e i, Interval e j) => i -> j -> Bool
`properlyIntersects` (e
x,e
z) then Seq j
m Seq j -> Seq j -> Seq j
forall a. Seq a -> Seq a -> Seq a
>< Seq j
l Seq j -> Seq j -> Seq j
forall a. Seq a -> Seq a -> Seq a
>< Seq j
r else Seq j
forall (f :: * -> *) a. Alternative f => f a
empty
someIntersectsIT :: (Interval e i, Interval e j) => i -> ITree e j -> Bool
someIntersectsIT :: i -> ITree e j -> Bool
someIntersectsIT i
i = Bool -> Bool
not (Bool -> Bool) -> (ITree e j -> Bool) -> ITree e j -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Seq j -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null (Seq j -> Bool) -> (ITree e j -> Seq j) -> ITree e j -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. i -> ITree e j -> Seq j
forall e i j.
(Interval e i, Interval e j) =>
i -> ITree e j -> Seq j
getIntersectsIT i
i
someProperlyIntersectsIT :: (Interval e i, Interval e j) => i -> ITree e j -> Bool
someProperlyIntersectsIT :: i -> ITree e j -> Bool
someProperlyIntersectsIT i
i = Bool -> Bool
not (Bool -> Bool) -> (ITree e j -> Bool) -> ITree e j -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Seq j -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null (Seq j -> Bool) -> (ITree e j -> Seq j) -> ITree e j -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. i -> ITree e j -> Seq j
forall e i j.
(Interval e i, Interval e j) =>
i -> ITree e j -> Seq j
getProperIntersectsIT i
i
leftmostInterval :: (Interval e i) => ITree e i -> Maybe i
leftmostInterval :: ITree e i -> Maybe i
leftmostInterval (Bin Seq i
bin) = case Seq i -> ViewL i
forall a. Seq a -> ViewL a
Seq.viewl Seq i
bin of
ViewL i
EmptyL -> Maybe i
forall a. Maybe a
Nothing
i
i :< Seq i
_ -> i -> Maybe i
forall a. a -> Maybe a
Just i
i
leftmostInterval (Split Seq i
up e
_ e
_ e
_ ITree e i
left ITree e i
right) = let
headl :: Seq a -> Maybe a
headl Seq a
xs = case Seq a -> ViewL a
forall a. Seq a -> ViewL a
Seq.viewl Seq a
xs of
ViewL a
EmptyL -> Maybe a
forall a. Maybe a
Nothing
a
i :< Seq a
_ -> a -> Maybe a
forall a. a -> Maybe a
Just a
i
in (Seq i -> Maybe i
forall a. Seq a -> Maybe a
headl (Seq i -> Maybe i) -> ([Maybe i] -> Seq i) -> [Maybe i] -> Maybe i
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Seq i -> Seq i
forall e i. Interval e i => Seq i -> Seq i
sortByLeft (Seq i -> Seq i) -> ([Maybe i] -> Seq i) -> [Maybe i] -> Seq i
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [i] -> Seq i
forall a. [a] -> Seq a
Seq.fromList ([i] -> Seq i) -> ([Maybe i] -> [i]) -> [Maybe i] -> Seq i
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Maybe i] -> [i]
forall a. [Maybe a] -> [a]
catMaybes) [ITree e i -> Maybe i
forall e i. Interval e i => ITree e i -> Maybe i
leftmostInterval ITree e i
left,Seq i -> Maybe i
forall a. Seq a -> Maybe a
headl Seq i
up,ITree e i -> Maybe i
forall e i. Interval e i => ITree e i -> Maybe i
leftmostInterval ITree e i
right]
toTree :: Interval e i => ITree e i -> Tree (e,e)
toTree :: ITree e i -> Tree (e, e)
toTree (Bin Seq i
_) = [Char] -> Tree (e, e)
forall a. HasCallStack => [Char] -> a
error [Char]
"Interval.toTree: just a bin"
toTree (Split Seq i
_ e
x e
y e
z ITree e i
left ITree e i
right) = Node :: forall a. a -> Forest a -> Tree a
Tree.Node {rootLabel :: (e, e)
Tree.rootLabel = (e
x,e
z), subForest :: Forest (e, e)
Tree.subForest = [Tree (e, e)
l,Tree (e, e)
r]} where
l :: Tree (e, e)
l = case ITree e i
left of
(Bin Seq i
_) -> Node :: forall a. a -> Forest a -> Tree a
Tree.Node {rootLabel :: (e, e)
Tree.rootLabel = (e
x,e
y), subForest :: Forest (e, e)
Tree.subForest = []}
ITree e i
_ -> ITree e i -> Tree (e, e)
forall e i. Interval e i => ITree e i -> Tree (e, e)
toTree ITree e i
left
r :: Tree (e, e)
r = case ITree e i
right of
(Bin Seq i
_) -> Node :: forall a. a -> Forest a -> Tree a
Tree.Node {rootLabel :: (e, e)
Tree.rootLabel = (e
y,e
z), subForest :: Forest (e, e)
Tree.subForest = []}
ITree e i
_ -> ITree e i -> Tree (e, e)
forall e i. Interval e i => ITree e i -> Tree (e, e)
toTree ITree e i
right
newtype Block e i = Block (Seq i)
blockstart :: Interval e i => Block e i -> e
blockstart :: Block e i -> e
blockstart (Block Seq i
xs) = case Seq i -> ViewL i
forall a. Seq a -> ViewL a
Seq.viewl Seq i
xs of
ViewL i
EmptyL -> [Char] -> e
forall a. HasCallStack => [Char] -> a
error [Char]
"empty Block"
i
x :< Seq i
_ -> i -> e
forall e i. Interval e i => i -> e
lb i
x
blocknull :: Block e i -> Bool
blocknull :: Block e i -> Bool
blocknull (Block Seq i
xs) = Seq i -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null Seq i
xs
instance Interval e i => Eq (Block e i) where
== :: Block e i -> Block e i -> Bool
(==) = e -> e -> Bool
forall a. Eq a => a -> a -> Bool
(==) (e -> e -> Bool)
-> (Block e i -> e) -> Block e i -> Block e i -> Bool
forall b c a. (b -> b -> c) -> (a -> b) -> a -> a -> c
`on` Block e i -> e
forall e i. Interval e i => Block e i -> e
blockstart
instance Interval e i => Ord (Block e i) where
compare :: Block e i -> Block e i -> Ordering
compare = e -> e -> Ordering
forall a. Ord a => a -> a -> Ordering
compare (e -> e -> Ordering)
-> (Block e i -> e) -> Block e i -> Block e i -> Ordering
forall b c a. (b -> b -> c) -> (a -> b) -> a -> a -> c
`on` (Block e i -> e
forall e i. Interval e i => Block e i -> e
blockstart)
instance Functor (Block e) where
fmap :: (a -> b) -> Block e a -> Block e b
fmap a -> b
f (Block Seq a
xs) = Seq b -> Block e b
forall e i. Seq i -> Block e i
Block ((a -> b) -> Seq a -> Seq b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap a -> b
f Seq a
xs)
instance Foldable (Block e) where
foldMap :: (a -> m) -> Block e a -> m
foldMap a -> m
f (Block Seq a
xs) = (a -> m) -> Seq a -> m
forall (t :: * -> *) m a.
(Foldable t, Monoid m) =>
(a -> m) -> t a -> m
foldMap a -> m
f Seq a
xs
instance Semigroup (Block e i) where
(Block Seq i
xs) <> :: Block e i -> Block e i -> Block e i
<> (Block Seq i
ys) = Seq i -> Block e i
forall e i. Seq i -> Block e i
Block (Seq i
xs Seq i -> Seq i -> Seq i
forall a. Seq a -> Seq a -> Seq a
>< Seq i
ys)
instance Monoid (Block e i) where
mempty :: Block e i
mempty = Seq i -> Block e i
forall e i. Seq i -> Block e i
Block Seq i
forall (f :: * -> *) a. Alternative f => f a
empty
mappend :: Block e i -> Block e i -> Block e i
mappend = Block e i -> Block e i -> Block e i
forall a. Semigroup a => a -> a -> a
(<>)
instance Show i => Show (Block e i) where
show :: Block e i -> [Char]
show (Block Seq i
xs) = [Char]
"Block "[Char] -> ShowS
forall a. [a] -> [a] -> [a]
++([i] -> [Char]
forall a. Show a => a -> [Char]
show (Seq i -> [i]
forall (t :: * -> *) a. Foldable t => t a -> [a]
toList Seq i
xs))
filterM :: (Applicative f, Traversable t, Alternative m) => (a -> f Bool) -> t a -> f (m a)
filterM :: (a -> f Bool) -> t a -> f (m a)
filterM a -> f Bool
f = ((t (m a) -> m a) -> f (t (m a)) -> f (m a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap ((m a -> m a -> m a) -> m a -> t (m a) -> m a
forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
foldr m a -> m a -> m a
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
(<|>) m a
forall (f :: * -> *) a. Alternative f => f a
empty)) (f (t (m a)) -> f (m a)) -> (t a -> f (t (m a))) -> t a -> f (m a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (a -> f (m a)) -> t a -> f (t (m a))
forall (t :: * -> *) (f :: * -> *) a b.
(Traversable t, Applicative f) =>
(a -> f b) -> t a -> f (t b)
traverse (\a
a -> (Bool -> m a) -> f Bool -> f (m a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (\Bool
b -> if Bool
b then a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure a
a else m a
forall (f :: * -> *) a. Alternative f => f a
empty) (a -> f Bool
f a
a))
crossesAny :: (Interval e i, Foldable f) => i -> f (Block e i) -> Bool
crossesAny :: i -> f (Block e i) -> Bool
crossesAny i
i = (Block e i -> Bool) -> f (Block e i) -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
any (((i -> e
forall e i. Interval e i => i -> e
ub i
i) e -> e -> Bool
forall a. Ord a => a -> a -> Bool
>)(e -> Bool) -> (Block e i -> e) -> Block e i -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
.Block e i -> e
forall e i. Interval e i => Block e i -> e
blockstart)
removeCrossers :: Interval e i => Block e i -> Seq (Block e i) -> (Seq i,Block e i)
removeCrossers :: Block e i -> Seq (Block e i) -> (Seq i, Block e i)
removeCrossers (Block Seq i
xs) Seq (Block e i)
blocks = let (Seq i
crossers,Seq i
xs') = (i -> (Seq i, Bool)) -> Seq i -> (Seq i, Seq i)
forall (f :: * -> *) (t :: * -> *) (m :: * -> *) a.
(Applicative f, Traversable t, Alternative m) =>
(a -> f Bool) -> t a -> f (m a)
filterM i -> (Seq i, Bool)
f Seq i
xs in (Seq i
crossers,Seq i -> Block e i
forall e i. Seq i -> Block e i
Block Seq i
xs') where
f :: i -> (Seq i, Bool)
f i
i = if i
i i -> Seq (Block e i) -> Bool
forall e i (f :: * -> *).
(Interval e i, Foldable f) =>
i -> f (Block e i) -> Bool
`crossesAny` Seq (Block e i)
blocks
then (i -> Seq i
forall a. a -> Seq a
Seq.singleton i
i,Bool
False)
else Bool -> (Seq i, Bool)
forall (m :: * -> *) a. Monad m => a -> m a
return Bool
True
gatherCrossers :: Interval e i => Seq (Block e i) -> (Seq i,Seq (Block e i))
gatherCrossers :: Seq (Block e i) -> (Seq i, Seq (Block e i))
gatherCrossers Seq (Block e i)
blks = case Seq (Block e i) -> ViewL (Block e i)
forall a. Seq a -> ViewL a
Seq.viewl Seq (Block e i)
blks of
(Block e i
block :< Seq (Block e i)
blocks) -> let
(Seq i
crossers,Seq (Block e i)
blocks') = Seq (Block e i) -> (Seq i, Seq (Block e i))
forall e i.
Interval e i =>
Seq (Block e i) -> (Seq i, Seq (Block e i))
gatherCrossers Seq (Block e i)
blocks
(Seq i
crossers',Block e i
block') = Block e i -> Seq (Block e i) -> (Seq i, Block e i)
forall e i.
Interval e i =>
Block e i -> Seq (Block e i) -> (Seq i, Block e i)
removeCrossers Block e i
block Seq (Block e i)
blocks'
cons :: Seq (Block e i) -> Seq (Block e i)
cons = if Block e i -> Bool
forall e a. Block e a -> Bool
blocknull Block e i
block' then Seq (Block e i) -> Seq (Block e i)
forall a. a -> a
id else (Block e i -> Seq (Block e i) -> Seq (Block e i)
forall a. a -> Seq a -> Seq a
(<|) Block e i
block')
in (Seq i
crossers' Seq i -> Seq i -> Seq i
forall a. Seq a -> Seq a -> Seq a
>< Seq i
crossers,Seq (Block e i) -> Seq (Block e i)
cons Seq (Block e i)
blocks')
ViewL (Block e i)
EmptyL -> (Seq i
forall (f :: * -> *) a. Alternative f => f a
empty,Seq (Block e i)
forall (f :: * -> *) a. Alternative f => f a
empty)
blocksOf :: Int -> Seq i -> Seq (Block e i)
blocksOf :: Int -> Seq i -> Seq (Block e i)
blocksOf Int
n = (Seq i -> Block e i) -> Seq (Seq i) -> Seq (Block e i)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Seq i -> Block e i
forall e i. Seq i -> Block e i
Block (Seq (Seq i) -> Seq (Block e i))
-> (Seq i -> Seq (Seq i)) -> Seq i -> Seq (Block e i)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> Seq i -> Seq (Seq i)
forall a. Int -> Seq a -> Seq (Seq a)
Seq.chunksOf Int
n
data SplitSeq a = EmptySeq | SingletonSeq a | TwoSeqs (Seq a) (Seq a) deriving (Int -> SplitSeq a -> ShowS
[SplitSeq a] -> ShowS
SplitSeq a -> [Char]
(Int -> SplitSeq a -> ShowS)
-> (SplitSeq a -> [Char])
-> ([SplitSeq a] -> ShowS)
-> Show (SplitSeq a)
forall a. Show a => Int -> SplitSeq a -> ShowS
forall a. Show a => [SplitSeq a] -> ShowS
forall a. Show a => SplitSeq a -> [Char]
forall a.
(Int -> a -> ShowS) -> (a -> [Char]) -> ([a] -> ShowS) -> Show a
showList :: [SplitSeq a] -> ShowS
$cshowList :: forall a. Show a => [SplitSeq a] -> ShowS
show :: SplitSeq a -> [Char]
$cshow :: forall a. Show a => SplitSeq a -> [Char]
showsPrec :: Int -> SplitSeq a -> ShowS
$cshowsPrec :: forall a. Show a => Int -> SplitSeq a -> ShowS
Show)
joinSeq :: SplitSeq a -> Seq a
joinSeq :: SplitSeq a -> Seq a
joinSeq SplitSeq a
EmptySeq = Seq a
forall (f :: * -> *) a. Alternative f => f a
empty
joinSeq (SingletonSeq a
a) = a -> Seq a
forall (f :: * -> *) a. Applicative f => a -> f a
pure a
a
joinSeq (TwoSeqs Seq a
xs Seq a
ys) = Seq a
xs Seq a -> Seq a -> Seq a
forall a. Semigroup a => a -> a -> a
<> Seq a
ys
splitSeq :: Seq a -> SplitSeq a
splitSeq :: Seq a -> SplitSeq a
splitSeq Seq a
xs = let (Seq a
l,Seq a
r) = Int -> Seq a -> (Seq a, Seq a)
forall a. Int -> Seq a -> (Seq a, Seq a)
Seq.splitAt (Seq a -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length Seq a
xs Int -> Int -> Int
forall a. Integral a => a -> a -> a
`div` Int
2) Seq a
xs in case (Seq a -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null Seq a
l,Seq a -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null Seq a
r) of
(Bool
_,Bool
True) -> SplitSeq a
forall a. SplitSeq a
EmptySeq
(Bool
True,Bool
False) -> let (a
x :< Seq a
_) = Seq a -> ViewL a
forall a. Seq a -> ViewL a
Seq.viewl Seq a
r in a -> SplitSeq a
forall a. a -> SplitSeq a
SingletonSeq a
x
(Bool
False,Bool
False) -> Seq a -> Seq a -> SplitSeq a
forall a. Seq a -> Seq a -> SplitSeq a
TwoSeqs Seq a
l Seq a
r
buildFromSeq :: Interval e i => Seq (Block e i) -> ITree e i
buildFromSeq :: Seq (Block e i) -> ITree e i
buildFromSeq Seq (Block e i)
blocks = case Seq (Block e i) -> SplitSeq (Block e i)
forall a. Seq a -> SplitSeq a
splitSeq Seq (Block e i)
blocks of
SplitSeq (Block e i)
EmptySeq -> ITree e i
forall e i. ITree e i
emptyITree
SingletonSeq (Block Seq i
bin) -> Seq i -> ITree e i
forall e i. Seq i -> ITree e i
Bin Seq i
bin
TwoSeqs Seq (Block e i)
lblocks Seq (Block e i)
rblocks -> let
y :: e
y = let Block e i
b :< Seq (Block e i)
_ = Seq (Block e i) -> ViewL (Block e i)
forall a. Seq a -> ViewL a
Seq.viewl Seq (Block e i)
rblocks in Block e i -> e
forall e i. Interval e i => Block e i -> e
blockstart Block e i
b
left :: ITree e i
left = Seq (Block e i) -> ITree e i
forall e i. Interval e i => Seq (Block e i) -> ITree e i
buildFromSeq Seq (Block e i)
lblocks
right :: ITree e i
right = Seq (Block e i) -> ITree e i
forall e i. Interval e i => Seq (Block e i) -> ITree e i
buildFromSeq Seq (Block e i)
rblocks
x :: e
x = e -> ((e, e) -> e) -> Maybe (e, e) -> e
forall b a. b -> (a -> b) -> Maybe a -> b
maybe e
y (e, e) -> e
forall a b. (a, b) -> a
fst (ITree e i -> Maybe (e, e)
forall e i. Interval e i => ITree e i -> Maybe (e, e)
hullOfTree ITree e i
left)
z :: e
z = e -> ((e, e) -> e) -> Maybe (e, e) -> e
forall b a. b -> (a -> b) -> Maybe a -> b
maybe e
y (e, e) -> e
forall a b. (a, b) -> b
snd (ITree e i -> Maybe (e, e)
forall e i. Interval e i => ITree e i -> Maybe (e, e)
hullOfTree ITree e i
right)
in Seq i -> e -> e -> e -> ITree e i -> ITree e i -> ITree e i
forall e i.
Seq i -> e -> e -> e -> ITree e i -> ITree e i -> ITree e i
Split Seq i
forall (f :: * -> *) a. Alternative f => f a
empty e
x e
y e
z ITree e i
left ITree e i
right
insert :: Interval e i => i -> ITree e i -> ITree e i
insert :: i -> ITree e i -> ITree e i
insert i
i (Bin Seq i
xs) = Seq i -> ITree e i
forall e i. Seq i -> ITree e i
Bin (i
i i -> Seq i -> Seq i
forall a. a -> Seq a -> Seq a
<| Seq i
xs)
insert i
i (Split Seq i
up e
x e
y e
z ITree e i
left ITree e i
right) = if i -> e
forall e i. Interval e i => i -> e
ub i
i e -> e -> Bool
forall a. Ord a => a -> a -> Bool
<= e
y
then let
left' :: ITree e i
left' = (i -> ITree e i -> ITree e i
forall e i. Interval e i => i -> ITree e i -> ITree e i
insert i
i ITree e i
left)
x' :: e
x' = e -> ((e, e) -> e) -> Maybe (e, e) -> e
forall b a. b -> (a -> b) -> Maybe a -> b
maybe e
x (e -> e -> e
forall a. Ord a => a -> a -> a
min e
x(e -> e) -> ((e, e) -> e) -> (e, e) -> e
forall b c a. (b -> c) -> (a -> b) -> a -> c
.(e, e) -> e
forall a b. (a, b) -> a
fst) (ITree e i -> Maybe (e, e)
forall e i. Interval e i => ITree e i -> Maybe (e, e)
hullOfTree ITree e i
left')
in Seq i -> e -> e -> e -> ITree e i -> ITree e i -> ITree e i
forall e i.
Seq i -> e -> e -> e -> ITree e i -> ITree e i -> ITree e i
Split Seq i
up e
x' e
y e
z ITree e i
left' ITree e i
right
else if i -> e
forall e i. Interval e i => i -> e
lb i
i e -> e -> Bool
forall a. Ord a => a -> a -> Bool
< e
y
then Seq i -> e -> e -> e -> ITree e i -> ITree e i -> ITree e i
forall e i.
Seq i -> e -> e -> e -> ITree e i -> ITree e i -> ITree e i
Split (i
i i -> Seq i -> Seq i
forall a. a -> Seq a -> Seq a
<| Seq i
up) (e -> e -> e
forall a. Ord a => a -> a -> a
min e
x (i -> e
forall e i. Interval e i => i -> e
lb i
i)) e
y (e -> e -> e
forall a. Ord a => a -> a -> a
max e
z (i -> e
forall e i. Interval e i => i -> e
ub i
i)) ITree e i
left ITree e i
right
else let
right' :: ITree e i
right' = i -> ITree e i -> ITree e i
forall e i. Interval e i => i -> ITree e i -> ITree e i
insert i
i ITree e i
right
z' :: e
z' = e -> ((e, e) -> e) -> Maybe (e, e) -> e
forall b a. b -> (a -> b) -> Maybe a -> b
maybe e
z (e -> e -> e
forall a. Ord a => a -> a -> a
max e
z(e -> e) -> ((e, e) -> e) -> (e, e) -> e
forall b c a. (b -> c) -> (a -> b) -> a -> c
.(e, e) -> e
forall a b. (a, b) -> b
snd) (ITree e i -> Maybe (e, e)
forall e i. Interval e i => ITree e i -> Maybe (e, e)
hullOfTree ITree e i
right')
in Seq i -> e -> e -> e -> ITree e i -> ITree e i -> ITree e i
forall e i.
Seq i -> e -> e -> e -> ITree e i -> ITree e i -> ITree e i
Split Seq i
up e
x e
y e
z' ITree e i
left ITree e i
right'
itree :: Interval e i => Int -> Seq i -> ITree e i
itree :: Int -> Seq i -> ITree e i
itree Int
n = ((ITree e i -> ITree e i) -> ITree e i -> ITree e i)
-> (ITree e i -> ITree e i, ITree e i) -> ITree e i
forall a b c. (a -> b -> c) -> (a, b) -> c
uncurry (ITree e i -> ITree e i) -> ITree e i -> ITree e i
forall a b. (a -> b) -> a -> b
($)((ITree e i -> ITree e i, ITree e i) -> ITree e i)
-> (Seq i -> (ITree e i -> ITree e i, ITree e i))
-> Seq i
-> ITree e i
forall b c a. (b -> c) -> (a -> b) -> a -> c
.(Seq i -> ITree e i -> ITree e i
f (Seq i -> ITree e i -> ITree e i)
-> (Seq (Block e i) -> ITree e i)
-> (Seq i, Seq (Block e i))
-> (ITree e i -> ITree e i, ITree e i)
forall (a :: * -> * -> *) b c b' c'.
Arrow a =>
a b c -> a b' c' -> a (b, b') (c, c')
*** Seq (Block e i) -> ITree e i
forall e i. Interval e i => Seq (Block e i) -> ITree e i
buildFromSeq)((Seq i, Seq (Block e i)) -> (ITree e i -> ITree e i, ITree e i))
-> (Seq i -> (Seq i, Seq (Block e i)))
-> Seq i
-> (ITree e i -> ITree e i, ITree e i)
forall b c a. (b -> c) -> (a -> b) -> a -> c
.Seq (Block e i) -> (Seq i, Seq (Block e i))
forall e i.
Interval e i =>
Seq (Block e i) -> (Seq i, Seq (Block e i))
gatherCrossers(Seq (Block e i) -> (Seq i, Seq (Block e i)))
-> (Seq i -> Seq (Block e i)) -> Seq i -> (Seq i, Seq (Block e i))
forall b c a. (b -> c) -> (a -> b) -> a -> c
.Int -> Seq i -> Seq (Block e i)
forall i e. Int -> Seq i -> Seq (Block e i)
blocksOf Int
n(Seq i -> Seq (Block e i))
-> (Seq i -> Seq i) -> Seq i -> Seq (Block e i)
forall b c a. (b -> c) -> (a -> b) -> a -> c
.Seq i -> Seq i
srt where
srt :: Seq i -> Seq i
srt = (i -> i -> Ordering) -> Seq i -> Seq i
forall a. (a -> a -> Ordering) -> Seq a -> Seq a
Seq.unstableSortBy ((e, e) -> (e, e) -> Ordering
forall a. Ord a => a -> a -> Ordering
compare ((e, e) -> (e, e) -> Ordering)
-> (i -> (e, e)) -> i -> i -> Ordering
forall b c a. (b -> b -> c) -> (a -> b) -> a -> a -> c
`on` i -> (e, e)
forall e i. Interval e i => i -> (e, e)
endPoints)
f :: Seq i -> ITree e i -> ITree e i
f = (ITree e i -> Seq i -> ITree e i)
-> Seq i -> ITree e i -> ITree e i
forall a b c. (a -> b -> c) -> b -> a -> c
flip ((i -> ITree e i -> ITree e i) -> ITree e i -> Seq i -> ITree e i
forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
foldr' i -> ITree e i -> ITree e i
forall e i. Interval e i => i -> ITree e i -> ITree e i
insert)
hullSeqNonOverlap :: Interval e i => Seq i -> Maybe (e,e)
hullSeqNonOverlap :: Seq i -> Maybe (e, e)
hullSeqNonOverlap Seq i
xs = case Seq i -> ViewL i
forall a. Seq a -> ViewL a
Seq.viewl Seq i
xs of
ViewL i
EmptyL -> Maybe (e, e)
forall a. Maybe a
Nothing
i
leftmost :< Seq i
others -> (e, e) -> Maybe (e, e)
forall a. a -> Maybe a
Just (i -> e
forall e i. Interval e i => i -> e
lb i
leftmost, case Seq i -> ViewR i
forall a. Seq a -> ViewR a
Seq.viewr Seq i
others of
Seq i
_ :> i
rightmost -> i -> e
forall e i. Interval e i => i -> e
ub i
rightmost
ViewR i
EmptyR -> i -> e
forall e i. Interval e i => i -> e
ub i
leftmost)
findSeq :: (Interval e i, Interval e j) => (i -> (e,e) -> Bool) -> i -> Seq j -> Seq j
findSeq :: (i -> (e, e) -> Bool) -> i -> Seq j -> Seq j
findSeq i -> (e, e) -> Bool
p i
i Seq j
js = case Seq j -> Maybe (e, e)
forall e i. Interval e i => Seq i -> Maybe (e, e)
hullSeqNonOverlap Seq j
js of
Maybe (e, e)
Nothing -> Seq j
forall (f :: * -> *) a. Alternative f => f a
empty
Just (e, e)
h -> if i -> (e, e) -> Bool
p i
i (e, e)
h
then case Seq j -> SplitSeq j
forall a. Seq a -> SplitSeq a
splitSeq Seq j
js of
SingletonSeq j
_j -> Seq j
js
TwoSeqs Seq j
l Seq j
r -> (i -> (e, e) -> Bool) -> i -> Seq j -> Seq j
forall e i j.
(Interval e i, Interval e j) =>
(i -> (e, e) -> Bool) -> i -> Seq j -> Seq j
findSeq i -> (e, e) -> Bool
p i
i Seq j
l Seq j -> Seq j -> Seq j
forall a. Seq a -> Seq a -> Seq a
>< (i -> (e, e) -> Bool) -> i -> Seq j -> Seq j
forall e i j.
(Interval e i, Interval e j) =>
(i -> (e, e) -> Bool) -> i -> Seq j -> Seq j
findSeq i -> (e, e) -> Bool
p i
i Seq j
r
SplitSeq j
EmptySeq -> Seq j
forall (f :: * -> *) a. Alternative f => f a
empty
else Seq j
forall (f :: * -> *) a. Alternative f => f a
empty
existsSeq :: (Interval e i, Interval e j) => (i -> (e,e) -> Bool) -> i -> Seq j -> Bool
existsSeq :: (i -> (e, e) -> Bool) -> i -> Seq j -> Bool
existsSeq i -> (e, e) -> Bool
p i
i Seq j
js = case Seq j -> Maybe (e, e)
forall e i. Interval e i => Seq i -> Maybe (e, e)
hullSeqNonOverlap Seq j
js of
Maybe (e, e)
Nothing -> Bool
False
Just (e, e)
h -> if i -> (e, e) -> Bool
p i
i (e, e)
h
then case Seq j -> SplitSeq j
forall a. Seq a -> SplitSeq a
splitSeq Seq j
js of
SingletonSeq j
_j -> Bool
True
TwoSeqs Seq j
l Seq j
r -> (i -> (e, e) -> Bool) -> i -> Seq j -> Bool
forall e i j.
(Interval e i, Interval e j) =>
(i -> (e, e) -> Bool) -> i -> Seq j -> Bool
existsSeq i -> (e, e) -> Bool
p i
i Seq j
l Bool -> Bool -> Bool
|| (i -> (e, e) -> Bool) -> i -> Seq j -> Bool
forall e i j.
(Interval e i, Interval e j) =>
(i -> (e, e) -> Bool) -> i -> Seq j -> Bool
existsSeq i -> (e, e) -> Bool
p i
i Seq j
r
SplitSeq j
EmptySeq -> Bool
False
else Bool
False