{- |
Copyright   :  (c) Henning Thielemann 2007-2010

Maintainer  :  haskell@henning-thielemann.de
Stability   :  stable
Portability :  Haskell 98


Event lists starting with a body and ending with a time difference.

-}
module Data.EventList.Relative.BodyTime
   (T,
    empty, singleton, null,
    fromPairList, toPairList,
    getTimes, getBodies, duration, durationR,
    mapBody, mapTime,
    concatMapMonoid,
    traverse, traverse_, traverseBody, traverseTime,
    mapM, mapM_, mapBodyM, mapTimeM,
    foldr, foldrPair,
    cons, snoc, viewL, viewR, switchL, switchR,
    span,
   ) where

import Data.EventList.Relative.BodyTimePrivate

import qualified Data.AlternatingList.List.Disparate as Disp
-- import qualified Data.AlternatingList.List.Uniform as Uniform

import Control.Monad (Monad, )
import Control.Applicative (Applicative, WrappedMonad(WrapMonad, unwrapMonad), )
import Data.Monoid (Monoid, mempty, )

import Data.Tuple.HT (mapFst, mapSnd, mapPair, )
import qualified Data.List as List

import Data.Function ((.), ($), )
import Data.Functor (fmap, )
import Data.Maybe (Maybe, )
import Prelude (Num, (+), Bool, )


fromPairList :: [(body, time)] -> T time body
fromPairList :: forall body time. [(body, time)] -> T time body
fromPairList = forall time body. T body time -> T time body
Cons forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b. [(a, b)] -> T a b
Disp.fromPairList

toPairList :: T time body -> [(body, time)]
toPairList :: forall time body. T time body -> [(body, time)]
toPairList = forall a b. T a b -> [(a, b)]
Disp.toPairList forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall time body. T time body -> T body time
decons

getBodies :: T time body -> [body]
getBodies :: forall time body. T time body -> [body]
getBodies = forall a b. T a b -> [a]
Disp.getFirsts forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall time body. T time body -> T body time
decons

getTimes :: T time body -> [time]
getTimes :: forall time body. T time body -> [time]
getTimes = forall a b. T a b -> [b]
Disp.getSeconds forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall time body. T time body -> T body time
decons

duration :: Num time => T time body -> time
duration :: forall time body. Num time => T time body -> time
duration = forall (t :: * -> *) a. (Foldable t, Num a) => t a -> a
List.sum forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall time body. T time body -> [time]
getTimes

durationR :: Num time => T time body -> time
durationR :: forall time body. Num time => T time body -> time
durationR = forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
List.foldr forall a. Num a => a -> a -> a
(+) time
0 forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall time body. T time body -> [time]
getTimes


mapBody :: (body0 -> body1) -> T time body0 -> T time body1
mapBody :: forall body0 body1 time.
(body0 -> body1) -> T time body0 -> T time body1
mapBody body0 -> body1
f = forall body0 time0 body1 time1.
(T body0 time0 -> T body1 time1) -> T time0 body0 -> T time1 body1
lift (forall a0 a1 b. (a0 -> a1) -> T a0 b -> T a1 b
Disp.mapFirst body0 -> body1
f)

mapTime :: (time0 -> time1) -> T time0 body -> T time1 body
mapTime :: forall time0 time1 body.
(time0 -> time1) -> T time0 body -> T time1 body
mapTime time0 -> time1
f = forall body0 time0 body1 time1.
(T body0 time0 -> T body1 time1) -> T time0 body0 -> T time1 body1
lift (forall b0 b1 a. (b0 -> b1) -> T a b0 -> T a b1
Disp.mapSecond time0 -> time1
f)


concatMapMonoid :: Monoid m =>
   (time -> m) -> (body -> m) ->
   T time body -> m
concatMapMonoid :: forall m time body.
Monoid m =>
(time -> m) -> (body -> m) -> T time body -> m
concatMapMonoid time -> m
f body -> m
g =
   forall m time body.
Monoid m =>
(time -> m) -> (body -> m) -> T time body -> m
Disp.concatMapMonoid body -> m
g time -> m
f forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall time body. T time body -> T body time
decons

traverse :: Applicative m =>
   (time0 -> m time1) -> (body0 -> m body1) ->
   T time0 body0 -> m (T time1 body1)
traverse :: forall (m :: * -> *) time0 time1 body0 body1.
Applicative m =>
(time0 -> m time1)
-> (body0 -> m body1) -> T time0 body0 -> m (T time1 body1)
traverse time0 -> m time1
timeAction body0 -> m body1
bodyAction =
   forall (m :: * -> *) body0 time0 body1 time1.
Applicative m =>
(T body0 time0 -> m (T body1 time1))
-> T time0 body0 -> m (T time1 body1)
liftA (forall (m :: * -> *) a0 a1 b0 b1.
Applicative m =>
(a0 -> m a1) -> (b0 -> m b1) -> T a0 b0 -> m (T a1 b1)
Disp.traverse body0 -> m body1
bodyAction time0 -> m time1
timeAction)

traverse_ :: Applicative m =>
   (time -> m ()) -> (body -> m ()) ->
   T time body -> m ()
traverse_ :: forall (m :: * -> *) time body.
Applicative m =>
(time -> m ()) -> (body -> m ()) -> T time body -> m ()
traverse_ time -> m ()
f body -> m ()
g = forall (m :: * -> *) d a b.
(Applicative m, Monoid d) =>
(a -> m d) -> (b -> m d) -> T a b -> m d
Disp.traverse_ body -> m ()
g time -> m ()
f forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall time body. T time body -> T body time
decons

traverseBody :: Applicative m =>
   (body0 -> m body1) -> T time body0 -> m (T time body1)
traverseBody :: forall (m :: * -> *) body0 body1 time.
Applicative m =>
(body0 -> m body1) -> T time body0 -> m (T time body1)
traverseBody body0 -> m body1
f = forall (m :: * -> *) body0 time0 body1 time1.
Applicative m =>
(T body0 time0 -> m (T body1 time1))
-> T time0 body0 -> m (T time1 body1)
liftA (forall (m :: * -> *) a0 a1 b.
Applicative m =>
(a0 -> m a1) -> T a0 b -> m (T a1 b)
Disp.traverseFirst body0 -> m body1
f)

traverseTime :: Applicative m =>
   (time0 -> m time1) -> T time0 body -> m (T time1 body)
traverseTime :: forall (m :: * -> *) time0 time1 body.
Applicative m =>
(time0 -> m time1) -> T time0 body -> m (T time1 body)
traverseTime time0 -> m time1
f = forall (m :: * -> *) body0 time0 body1 time1.
Applicative m =>
(T body0 time0 -> m (T body1 time1))
-> T time0 body0 -> m (T time1 body1)
liftA (forall (m :: * -> *) b0 b1 a.
Applicative m =>
(b0 -> m b1) -> T a b0 -> m (T a b1)
Disp.traverseSecond time0 -> m time1
f)


mapM :: Monad m =>
   (time0 -> m time1) -> (body0 -> m body1) ->
   T time0 body0 -> m (T time1 body1)
mapM :: forall (m :: * -> *) time0 time1 body0 body1.
Monad m =>
(time0 -> m time1)
-> (body0 -> m body1) -> T time0 body0 -> m (T time1 body1)
mapM time0 -> m time1
timeAction body0 -> m body1
bodyAction =
   forall (m :: * -> *) a. WrappedMonad m a -> m a
unwrapMonad forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (m :: * -> *) time0 time1 body0 body1.
Applicative m =>
(time0 -> m time1)
-> (body0 -> m body1) -> T time0 body0 -> m (T time1 body1)
traverse (forall (m :: * -> *) a. m a -> WrappedMonad m a
WrapMonad forall b c a. (b -> c) -> (a -> b) -> a -> c
. time0 -> m time1
timeAction) (forall (m :: * -> *) a. m a -> WrappedMonad m a
WrapMonad forall b c a. (b -> c) -> (a -> b) -> a -> c
. body0 -> m body1
bodyAction)

mapM_ :: Monad m =>
   (time -> m ()) -> (body -> m ()) ->
   T time body -> m ()
mapM_ :: forall (m :: * -> *) time body.
Monad m =>
(time -> m ()) -> (body -> m ()) -> T time body -> m ()
mapM_ time -> m ()
f body -> m ()
g =
   forall (m :: * -> *) a. WrappedMonad m a -> m a
unwrapMonad forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (m :: * -> *) time body.
Applicative m =>
(time -> m ()) -> (body -> m ()) -> T time body -> m ()
traverse_ (forall (m :: * -> *) a. m a -> WrappedMonad m a
WrapMonad forall b c a. (b -> c) -> (a -> b) -> a -> c
. time -> m ()
f) (forall (m :: * -> *) a. m a -> WrappedMonad m a
WrapMonad forall b c a. (b -> c) -> (a -> b) -> a -> c
. body -> m ()
g)


mapBodyM :: Monad m =>
   (body0 -> m body1) -> T time body0 -> m (T time body1)
mapBodyM :: forall (m :: * -> *) body0 body1 time.
Monad m =>
(body0 -> m body1) -> T time body0 -> m (T time body1)
mapBodyM body0 -> m body1
f = forall (m :: * -> *) a. WrappedMonad m a -> m a
unwrapMonad forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (m :: * -> *) body0 body1 time.
Applicative m =>
(body0 -> m body1) -> T time body0 -> m (T time body1)
traverseBody (forall (m :: * -> *) a. m a -> WrappedMonad m a
WrapMonad forall b c a. (b -> c) -> (a -> b) -> a -> c
. body0 -> m body1
f)

mapTimeM :: Monad m =>
   (time0 -> m time1) -> T time0 body -> m (T time1 body)
mapTimeM :: forall (m :: * -> *) time0 time1 body.
Monad m =>
(time0 -> m time1) -> T time0 body -> m (T time1 body)
mapTimeM time0 -> m time1
f = forall (m :: * -> *) a. WrappedMonad m a -> m a
unwrapMonad forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (m :: * -> *) time0 time1 body.
Applicative m =>
(time0 -> m time1) -> T time0 body -> m (T time1 body)
traverseTime (forall (m :: * -> *) a. m a -> WrappedMonad m a
WrapMonad forall b c a. (b -> c) -> (a -> b) -> a -> c
. time0 -> m time1
f)


foldr :: (body -> a -> b) -> (time -> b -> a) -> b -> T time body -> b
foldr :: forall body a b time.
(body -> a -> b) -> (time -> b -> a) -> b -> T time body -> b
foldr body -> a -> b
f time -> b -> a
g b
x = forall a c d b. (a -> c -> d) -> (b -> d -> c) -> d -> T a b -> d
Disp.foldr body -> a -> b
f time -> b -> a
g b
x forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall time body. T time body -> T body time
decons

foldrPair :: (body -> time -> a -> a) -> a -> T time body -> a
foldrPair :: forall body time a.
(body -> time -> a -> a) -> a -> T time body -> a
foldrPair body -> time -> a -> a
f a
x = forall a b c. (a -> b -> c -> c) -> c -> T a b -> c
Disp.foldrPair body -> time -> a -> a
f a
x forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall time body. T time body -> T body time
decons


empty :: T time body
empty :: forall time body. T time body
empty = forall a. Monoid a => a
mempty

null :: T time body -> Bool
null :: forall time body. T time body -> Bool
null = forall a b. T a b -> Bool
Disp.null forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall time body. T time body -> T body time
decons

singleton :: body -> time -> T time body
singleton :: forall body time. body -> time -> T time body
singleton body
body time
time = forall time body. T body time -> T time body
Cons forall a b. (a -> b) -> a -> b
$ forall a b. a -> b -> T a b
Disp.singleton body
body time
time


cons :: body -> time -> T time body -> T time body
cons :: forall body time. body -> time -> T time body -> T time body
cons body
body time
time = forall body0 time0 body1 time1.
(T body0 time0 -> T body1 time1) -> T time0 body0 -> T time1 body1
lift (forall a b. a -> b -> T a b -> T a b
Disp.cons body
body time
time)

snoc :: T time body -> body -> time -> T time body
snoc :: forall time body. T time body -> body -> time -> T time body
snoc T time body
xs body
body time
time =
   forall time body. T body time -> T time body
Cons forall a b. (a -> b) -> a -> b
$ (forall a b. T a b -> a -> b -> T a b
Disp.snoc forall body time a. (T body time -> a) -> T time body -> a
$*~ T time body
xs) body
body time
time


viewL :: T time body -> Maybe ((body, time), T time body)
viewL :: forall time body. T time body -> Maybe ((body, time), T time body)
viewL = forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (forall b c a. (b -> c) -> (a, b) -> (a, c)
mapSnd forall time body. T body time -> T time body
Cons) forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b. T a b -> Maybe ((a, b), T a b)
Disp.viewL forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall time body. T time body -> T body time
decons

viewR :: T time body -> Maybe (T time body, (body, time))
viewR :: forall time body. T time body -> Maybe (T time body, (body, time))
viewR = forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (forall a c b. (a -> c) -> (a, b) -> (c, b)
mapFst forall time body. T body time -> T time body
Cons) forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b. T a b -> Maybe (T a b, (a, b))
Disp.viewR forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall time body. T time body -> T body time
decons


{-# INLINE switchL #-}
switchL :: c -> (body -> time -> T time body -> c) -> T time body -> c
switchL :: forall c body time.
c -> (body -> time -> T time body -> c) -> T time body -> c
switchL c
f body -> time -> T time body -> c
g = forall c a b. c -> (a -> b -> T a b -> c) -> T a b -> c
Disp.switchL c
f (\ body
b time
t  -> body -> time -> T time body -> c
g body
b time
t forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall time body. T body time -> T time body
Cons) forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall time body. T time body -> T body time
decons

{-# INLINE switchR #-}
switchR :: c -> (T time body -> body -> time -> c) -> T time body -> c
switchR :: forall c time body.
c -> (T time body -> body -> time -> c) -> T time body -> c
switchR c
f T time body -> body -> time -> c
g = forall c a b. c -> (T a b -> a -> b -> c) -> T a b -> c
Disp.switchR c
f (\T body time
xs body
b time
t -> T time body -> body -> time -> c
g (forall time body. T body time -> T time body
Cons T body time
xs) body
b time
t) forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall time body. T time body -> T body time
decons


span :: (body -> Bool) -> T time body -> (T time body, T time body)
span :: forall body time.
(body -> Bool) -> T time body -> (T time body, T time body)
span body -> Bool
p = forall a c b d. (a -> c, b -> d) -> (a, b) -> (c, d)
mapPair (forall time body. T body time -> T time body
Cons, forall time body. T body time -> T time body
Cons) forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b. (a -> Bool) -> T a b -> (T a b, T a b)
Disp.spanFirst body -> Bool
p forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall time body. T time body -> T body time
decons