{-# LANGUAGE NoImplicitPrelude #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE ExistentialQuantification #-}
{-# LANGUAGE RankNTypes #-}
{- |
ToDo:
Better name for the module is certainly
  Synthesizer.Generator.Signal
-}
module Synthesizer.State.Signal where

import qualified Synthesizer.Plain.Modifier as Modifier
import qualified Data.List as List

import qualified Algebra.Module   as Module
import qualified Algebra.Additive as Additive
import Algebra.Module ((*>))
import Algebra.Additive (zero)

import qualified Synthesizer.Format as Format

import qualified Data.EventList.Relative.BodyTime as EventList
import qualified Numeric.NonNegative.Class as NonNeg98
import Numeric.NonNegative.Class ((-|), )

import Control.Monad.Trans.State
          (runState, StateT(StateT), runStateT, )
import Control.Monad (Monad, mplus, msum,
           (>>), (>>=), fail, return, (=<<),
           liftM2,
           Functor, fmap, )

import qualified Control.Applicative as App

import Data.Foldable (Foldable, foldr, )
import Data.Monoid (Monoid, mappend, mempty, )
import Data.Semigroup (Semigroup, (<>), )

import qualified Synthesizer.Storable.Signal as SigSt
import qualified Data.StorableVector.Lazy.Pattern as SVL
import qualified Data.StorableVector.Lazy.Pointer as PtrSt
import qualified Data.StorableVector as V
import Foreign.Storable (Storable)

import qualified Data.List.HT as ListHT
import Data.Tuple.HT (mapFst, mapSnd, mapPair, fst3, snd3, thd3, )
import Data.Function.HT (nest, )
import Data.Maybe.HT (toMaybe, )
import Data.Bool.HT (if', )
import NumericPrelude.Numeric (Float, Double, fromInteger, )

import Text.Show (Show(showsPrec), show, showParen, showString, )
import Data.Maybe (Maybe(Just, Nothing), maybe, fromMaybe, )
import qualified Prelude as P
import Prelude
   ((.), ($), id, const, flip, curry, uncurry, fst, snd, error,
    (>), (>=), max, Ord, (==), Eq,
    succ, pred, Bool(True,False), (&&), not, Int,
--    fromInteger,
    (++),
    seq,
    )


-- | Cf. StreamFusion  Data.Stream
data T a =
   forall s. -- Seq s =>
      Cons !(StateT s Maybe a)  -- compute next value
           !s                   -- initial state


instance (Show y) => Show (T y) where
   showsPrec :: Int -> T y -> ShowS
showsPrec Int
p T y
x =
      Bool -> ShowS -> ShowS
showParen (Int
p forall a. Ord a => a -> a -> Bool
>= Int
10)
         (String -> ShowS
showString String
"StateSignal.fromList " forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Show a => Int -> a -> ShowS
showsPrec Int
11 (forall y. T y -> [y]
toList T y
x))

instance (Eq y) => Eq (T y) where
   == :: T y -> T y -> Bool
(==) = forall y. Eq y => T y -> T y -> Bool
equal

instance Format.C T where
   format :: forall y. Show y => Int -> T y -> ShowS
format = forall a. Show a => Int -> a -> ShowS
showsPrec

instance Functor T where
   fmap :: forall a b. (a -> b) -> T a -> T b
fmap a -> b
g (Cons StateT s Maybe a
f s
s) = forall a s. StateT s Maybe a -> s -> T a
Cons (forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap a -> b
g StateT s Maybe a
f) s
s

instance Foldable T where
   foldr :: forall a b. (a -> b -> b) -> b -> T a -> b
foldr = forall a b. (a -> b -> b) -> b -> T a -> b
foldR

instance App.Applicative T where
   pure :: forall a. a -> T a
pure = forall a. a -> T a
singleton
   T (a -> b)
x <*> :: forall a b. T (a -> b) -> T a -> T b
<*> T a
y = forall a b c. (a -> b -> c) -> T a -> T b -> T c
liftA2 forall a b. (a -> b) -> a -> b
($) T (a -> b)
x T a
y

instance Monad T where
   return :: forall a. a -> T a
return = forall a. a -> T a
singleton
   T a
x >>= :: forall a b. T a -> (a -> T b) -> T b
>>= a -> T b
k =
      forall y x. T y -> (forall s. (s -> Maybe (y, s)) -> s -> x) -> x
runViewL T a
x forall a b. (a -> b) -> a -> b
$ \s -> Maybe (a, s)
f s
s0 ->
      forall a b c. (a -> b -> c) -> b -> a -> c
flip forall acc y. (acc -> Maybe (y, acc)) -> acc -> T y
generate (forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (forall a c b. (a -> c) -> (a, b) -> (c, b)
mapFst a -> T b
k) forall a b. (a -> b) -> a -> b
$ s -> Maybe (a, s)
f s
s0) forall a b. (a -> b) -> a -> b
$ \Maybe (T b, s)
m ->
      Maybe (T b, s)
m forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>=
      let go :: (T b, s) -> Maybe (b, Maybe (T b, s))
go (T b
y,s
s) =
             forall (m :: * -> *) a. MonadPlus m => m a -> m a -> m a
mplus
                (forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (\(b
y1,T b
ys) -> (b
y1, forall a. a -> Maybe a
Just (T b
ys,s
s))) (forall a. T a -> Maybe (a, T a)
viewL T b
y))
                (forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (forall a c b. (a -> c) -> (a, b) -> (c, b)
mapFst a -> T b
k) (s -> Maybe (a, s)
f s
s) forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= (T b, s) -> Maybe (b, Maybe (T b, s))
go)
      in  (T b, s) -> Maybe (b, Maybe (T b, s))
go


{- |
It is a common pattern to use @switchL@ or @viewL@ in a loop
in order to traverse a signal.
However this needs repeated packing and unpacking
of the 'viewL' function and the state.
It seems that GHC is not clever enough to detect,
that the 'view' function does not change.
With 'runViewL' you can unpack a stream once
and use an efficient 'viewL' in the loop.
-}
{-# INLINE runViewL #-}
runViewL ::
   T y ->
   (forall s. (s -> Maybe (y, s)) -> s -> x) ->
   x
runViewL :: forall y x. T y -> (forall s. (s -> Maybe (y, s)) -> s -> x) -> x
runViewL (Cons StateT s Maybe y
f s
s) forall s. (s -> Maybe (y, s)) -> s -> x
cont =
   forall s. (s -> Maybe (y, s)) -> s -> x
cont (forall s (m :: * -> *) a. StateT s m a -> s -> m (a, s)
runStateT StateT s Maybe y
f) s
s

{-# INLINE runSwitchL #-}
runSwitchL ::
   T y ->
   (forall s. (forall z. z -> (y -> s -> z) -> s -> z) -> s -> x) ->
   x
runSwitchL :: forall y x.
T y
-> (forall s. (forall z. z -> (y -> s -> z) -> s -> z) -> s -> x)
-> x
runSwitchL T y
sig forall s. (forall z. z -> (y -> s -> z) -> s -> z) -> s -> x
cont =
   forall y x. T y -> (forall s. (s -> Maybe (y, s)) -> s -> x) -> x
runViewL T y
sig (\s -> Maybe (y, s)
next ->
      forall s. (forall z. z -> (y -> s -> z) -> s -> z) -> s -> x
cont (\z
n y -> s -> z
j -> forall b a. b -> (a -> b) -> Maybe a -> b
maybe z
n (forall a b c. (a -> b -> c) -> (a, b) -> c
uncurry y -> s -> z
j) forall b c a. (b -> c) -> (a -> b) -> a -> c
. s -> Maybe (y, s)
next))

{-# INLINE generate #-}
generate :: (acc -> Maybe (y, acc)) -> acc -> T y
generate :: forall acc y. (acc -> Maybe (y, acc)) -> acc -> T y
generate acc -> Maybe (y, acc)
f = forall a s. StateT s Maybe a -> s -> T a
Cons (forall s (m :: * -> *) a. (s -> m (a, s)) -> StateT s m a
StateT acc -> Maybe (y, acc)
f)

{-# INLINE unfoldR #-}
unfoldR :: (acc -> Maybe (y, acc)) -> acc -> T y
unfoldR :: forall acc y. (acc -> Maybe (y, acc)) -> acc -> T y
unfoldR = forall acc y. (acc -> Maybe (y, acc)) -> acc -> T y
generate

{-# INLINE generateInfinite #-}
generateInfinite :: (acc -> (y, acc)) -> acc -> T y
generateInfinite :: forall acc y. (acc -> (y, acc)) -> acc -> T y
generateInfinite acc -> (y, acc)
f = forall acc y. (acc -> Maybe (y, acc)) -> acc -> T y
generate (forall a. a -> Maybe a
Just forall b c a. (b -> c) -> (a -> b) -> a -> c
. acc -> (y, acc)
f)

{-# INLINE fromList #-}
fromList :: [y] -> T y
fromList :: forall y. [y] -> T y
fromList = forall acc y. (acc -> Maybe (y, acc)) -> acc -> T y
generate forall a. [a] -> Maybe (a, [a])
ListHT.viewL

{-# INLINE toList #-}
toList :: T y -> [y]
toList :: forall y. T y -> [y]
toList (Cons StateT s Maybe y
f s
x0) =
   forall b a. (b -> Maybe (a, b)) -> b -> [a]
List.unfoldr (forall s (m :: * -> *) a. StateT s m a -> s -> m (a, s)
runStateT StateT s Maybe y
f) s
x0


{-# INLINE fromStorableSignal #-}
fromStorableSignal ::
   (Storable a) =>
   SigSt.T a -> T a
fromStorableSignal :: forall a. Storable a => T a -> T a
fromStorableSignal =
   forall acc y. (acc -> Maybe (y, acc)) -> acc -> T y
generate forall a. Storable a => Pointer a -> Maybe (a, Pointer a)
PtrSt.viewL forall b c a. (b -> c) -> (a -> b) -> a -> c
.
   forall a. Storable a => Vector a -> Pointer a
PtrSt.cons

{-# INLINE fromStrictStorableSignal #-}
fromStrictStorableSignal ::
   (Storable a) =>
   V.Vector a -> T a
fromStrictStorableSignal :: forall a. Storable a => Vector a -> T a
fromStrictStorableSignal Vector a
xs =
   forall a b. (a -> b) -> T a -> T b
map (forall a. Storable a => Vector a -> Int -> a
V.index Vector a
xs) forall a b. (a -> b) -> a -> b
$ forall a. Int -> T a -> T a
take (forall a. Vector a -> Int
V.length Vector a
xs) forall a b. (a -> b) -> a -> b
$ forall a. (a -> a) -> a -> T a
iterate forall a. Enum a => a -> a
succ forall a. C a => a
zero

{-# INLINE toStorableSignal #-}
toStorableSignal ::
   (Storable a) =>
   SigSt.ChunkSize -> T a -> SigSt.T a
toStorableSignal :: forall a. Storable a => ChunkSize -> T a -> T a
toStorableSignal ChunkSize
size (Cons StateT s Maybe a
f s
a) =
   forall b a.
Storable b =>
ChunkSize -> (a -> Maybe (b, a)) -> a -> Vector b
SigSt.unfoldr ChunkSize
size (forall s (m :: * -> *) a. StateT s m a -> s -> m (a, s)
runStateT StateT s Maybe a
f) s
a

{-# INLINE toStrictStorableSignal #-}
toStrictStorableSignal ::
   (Storable a) =>
   Int -> T a -> V.Vector a
toStrictStorableSignal :: forall a. Storable a => Int -> T a -> Vector a
toStrictStorableSignal Int
size (Cons StateT s Maybe a
f s
a) =
   forall a b. (a, b) -> a
fst forall a b. (a -> b) -> a -> b
$ forall b a.
Storable b =>
Int -> (a -> Maybe (b, a)) -> a -> (Vector b, Maybe a)
V.unfoldrN Int
size (forall s (m :: * -> *) a. StateT s m a -> s -> m (a, s)
runStateT StateT s Maybe a
f) s
a

-- needed in synthesizer-alsa
{-# INLINE toStorableSignalVary #-}
toStorableSignalVary ::
   (Storable a) =>
   SVL.LazySize -> T a -> SigSt.T a
toStorableSignalVary :: forall a. Storable a => LazySize -> T a -> T a
toStorableSignalVary LazySize
size (Cons StateT s Maybe a
f s
a) =
   forall a b. (a, b) -> a
fst forall a b. (a -> b) -> a -> b
$ forall b a.
Storable b =>
LazySize -> (a -> Maybe (b, a)) -> a -> (Vector b, Maybe a)
SVL.unfoldrN LazySize
size (forall s (m :: * -> *) a. StateT s m a -> s -> m (a, s)
runStateT StateT s Maybe a
f) s
a

fromPiecewiseConstant ::
   (NonNeg98.C time, P.Integral time) =>
   EventList.T time a -> T a
fromPiecewiseConstant :: forall time a. (C time, Integral time) => T time a -> T a
fromPiecewiseConstant T time a
xs0 =
   forall acc y. (acc -> Maybe (y, acc)) -> acc -> T y
generate
      (let go :: ((body, time), T time body)
-> Maybe (body, ((body, time), T time body))
go ((body
x,time
n),T time body
xs) =
             forall a. Bool -> a -> a -> a
if' (time
n forall a. Eq a => a -> a -> Bool
== forall a. Num a => Integer -> a
P.fromInteger Integer
0)
               (((body, time), T time body)
-> Maybe (body, ((body, time), T time body))
go forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< forall time body. T time body -> Maybe ((body, time), T time body)
EventList.viewL T time body
xs)
               (forall a. a -> Maybe a
Just (body
x, ((body
x, time
n forall a. C a => a -> a -> a
-| forall a. Num a => Integer -> a
P.fromInteger Integer
1), T time body
xs)))
       in  forall {time} {body}.
(Num time, C time) =>
((body, time), T time body)
-> Maybe (body, ((body, time), T time body))
go)
      ((forall a. HasCallStack => String -> a
error String
"if counter is zero, the sample value is invalid", forall a. Num a => Integer -> a
P.fromInteger Integer
0), T time a
xs0)


{-# INLINE iterate #-}
iterate :: (a -> a) -> a -> T a
iterate :: forall a. (a -> a) -> a -> T a
iterate a -> a
f = forall acc y. (acc -> (y, acc)) -> acc -> T y
generateInfinite (\a
x -> (a
x, a -> a
f a
x))

{-# INLINE iterateAssociative #-}
iterateAssociative :: (a -> a -> a) -> a -> T a
iterateAssociative :: forall a. (a -> a -> a) -> a -> T a
iterateAssociative a -> a -> a
op a
x = forall a. (a -> a) -> a -> T a
iterate (a -> a -> a
op a
x) a
x -- should be optimized

{-# INLINE repeat #-}
repeat :: a -> T a
repeat :: forall a. a -> T a
repeat = forall a. (a -> a) -> a -> T a
iterate forall a. a -> a
id




{-# INLINE crochetL #-}
crochetL :: (x -> acc -> Maybe (y, acc)) -> acc -> T x -> T y
crochetL :: forall x acc y. (x -> acc -> Maybe (y, acc)) -> acc -> T x -> T y
crochetL x -> acc -> Maybe (y, acc)
g acc
b (Cons StateT s Maybe x
f s
a) =
   forall a s. StateT s Maybe a -> s -> T a
Cons
      (forall s (m :: * -> *) a. (s -> m (a, s)) -> StateT s m a
StateT (\(s
a0,acc
b0) ->
          do (x
x0,s
a1) <- forall s (m :: * -> *) a. StateT s m a -> s -> m (a, s)
runStateT StateT s Maybe x
f s
a0
             (y
y0,acc
b1) <- x -> acc -> Maybe (y, acc)
g x
x0 acc
b0
             forall a. a -> Maybe a
Just (y
y0, (s
a1,acc
b1))))
      (s
a,acc
b)


{-# INLINE scanL #-}
scanL :: (acc -> x -> acc) -> acc -> T x -> T acc
scanL :: forall acc x. (acc -> x -> acc) -> acc -> T x -> T acc
scanL acc -> x -> acc
f acc
start =
   forall a. a -> T a -> T a
cons acc
start forall b c a. (b -> c) -> (a -> b) -> a -> c
.
   forall x acc y. (x -> acc -> Maybe (y, acc)) -> acc -> T x -> T y
crochetL (\x
x acc
acc -> let y :: acc
y = acc -> x -> acc
f acc
acc x
x in forall a. a -> Maybe a
Just (acc
y, acc
y)) acc
start


{-# INLINE scanLClip #-}
-- | input and output have equal length, that's better for fusion
scanLClip :: (acc -> x -> acc) -> acc -> T x -> T acc
scanLClip :: forall acc x. (acc -> x -> acc) -> acc -> T x -> T acc
scanLClip acc -> x -> acc
f acc
start =
   forall x acc y. (x -> acc -> Maybe (y, acc)) -> acc -> T x -> T y
crochetL (\x
x acc
acc -> forall a. a -> Maybe a
Just (acc
acc, acc -> x -> acc
f acc
acc x
x)) acc
start

{-# INLINE map #-}
map :: (a -> b) -> (T a -> T b)
map :: forall a b. (a -> b) -> T a -> T b
map = forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap
-- map f = crochetL (\x _ -> Just (f x, ())) ()


{- |
This function will recompute the input lists
and is thus probably not what you want.
If you want to avoid recomputation please consider Causal.Process.
-}
{-# INLINE unzip #-}
unzip :: T (a,b) -> (T a, T b)
unzip :: forall a b. T (a, b) -> (T a, T b)
unzip T (a, b)
x = (forall a b. (a -> b) -> T a -> T b
map forall a b. (a, b) -> a
fst T (a, b)
x, forall a b. (a -> b) -> T a -> T b
map forall a b. (a, b) -> b
snd T (a, b)
x)

{-# INLINE unzip3 #-}
unzip3 :: T (a,b,c) -> (T a, T b, T c)
unzip3 :: forall a b c. T (a, b, c) -> (T a, T b, T c)
unzip3 T (a, b, c)
xs = (forall a b. (a -> b) -> T a -> T b
map forall a b c. (a, b, c) -> a
fst3 T (a, b, c)
xs, forall a b. (a -> b) -> T a -> T b
map forall a b c. (a, b, c) -> b
snd3 T (a, b, c)
xs, forall a b. (a -> b) -> T a -> T b
map forall a b c. (a, b, c) -> c
thd3 T (a, b, c)
xs)


{-# INLINE delay1 #-}
{- |
This is a fusion friendly implementation of delay.
However, in order to be a 'crochetL'
the output has the same length as the input,
that is, the last element is removed - at least for finite input.
-}
delay1 :: a -> T a -> T a
delay1 :: forall a. a -> T a -> T a
delay1 = forall x acc y. (x -> acc -> Maybe (y, acc)) -> acc -> T x -> T y
crochetL (forall a b c. (a -> b -> c) -> b -> a -> c
flip (forall a b c. ((a, b) -> c) -> a -> b -> c
curry forall a. a -> Maybe a
Just))

{-# INLINE delay #-}
delay :: y -> Int -> T y -> T y
delay :: forall y. y -> Int -> T y -> T y
delay y
z Int
n = forall a. T a -> T a -> T a
append (forall a. Int -> a -> T a
replicate Int
n y
z)

{-# INLINE take #-}
take :: Int -> T a -> T a
take :: forall a. Int -> T a -> T a
take Int
n =
   forall a b. (a -> b) -> T a -> T b
map forall a b. (a, b) -> b
snd forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. (a -> Bool) -> T a -> T a
takeWhile ((forall a. Ord a => a -> a -> Bool
>Int
0) forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b. (a, b) -> a
fst) forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b. T a -> T b -> T (a, b)
zip (forall a. (a -> a) -> a -> T a
iterate forall a. Enum a => a -> a
pred Int
n)
   -- crochetL (\x n -> toMaybe (n>zero) (x, pred n))

{-# INLINE takeWhile #-}
takeWhile :: (a -> Bool) -> T a -> T a
takeWhile :: forall a. (a -> Bool) -> T a -> T a
takeWhile a -> Bool
p = forall x acc y. (x -> acc -> Maybe (y, acc)) -> acc -> T x -> T y
crochetL (\a
x ()
_ -> forall a. Bool -> a -> Maybe a
toMaybe (a -> Bool
p a
x) (a
x, ())) ()

{-# INLINE replicate #-}
replicate :: Int -> a -> T a
replicate :: forall a. Int -> a -> T a
replicate Int
n = forall a. Int -> T a -> T a
take Int
n forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. a -> T a
repeat


{- * functions consuming multiple lists -}

{-# INLINE zipWith #-}
zipWith :: (a -> b -> c) -> (T a -> T b -> T c)
zipWith :: forall a b c. (a -> b -> c) -> T a -> T b -> T c
zipWith a -> b -> c
h (Cons StateT s Maybe a
f s
a) =
   forall x acc y. (x -> acc -> Maybe (y, acc)) -> acc -> T x -> T y
crochetL
      (\b
x0 s
a0 ->
          do (a
y0,s
a1) <- forall s (m :: * -> *) a. StateT s m a -> s -> m (a, s)
runStateT StateT s Maybe a
f s
a0
             forall a. a -> Maybe a
Just (a -> b -> c
h a
y0 b
x0, s
a1))
      s
a

{-# INLINE zipWithStorable #-}
zipWithStorable :: (Storable b, Storable c) =>
   (a -> b -> c) -> (T a -> SigSt.T b -> SigSt.T c)
zipWithStorable :: forall b c a.
(Storable b, Storable c) =>
(a -> b -> c) -> T a -> T b -> T c
zipWithStorable a -> b -> c
h (Cons StateT s Maybe a
f s
a) =
   forall x y acc.
(Storable x, Storable y) =>
(x -> acc -> Maybe (y, acc)) -> acc -> Vector x -> Vector y
SigSt.crochetL
      (\b
x0 s
a0 ->
          do (a
y0,s
a1) <- forall s (m :: * -> *) a. StateT s m a -> s -> m (a, s)
runStateT StateT s Maybe a
f s
a0
             forall a. a -> Maybe a
Just (a -> b -> c
h a
y0 b
x0, s
a1))
      s
a

{-# INLINE zipWith3 #-}
zipWith3 :: (a -> b -> c -> d) -> (T a -> T b -> T c -> T d)
zipWith3 :: forall a b c d. (a -> b -> c -> d) -> T a -> T b -> T c -> T d
zipWith3 a -> b -> c -> d
f T a
s0 T b
s1 =
   forall a b c. (a -> b -> c) -> T a -> T b -> T c
zipWith (forall a b c. (a -> b -> c) -> (a, b) -> c
uncurry a -> b -> c -> d
f) (forall a b. T a -> T b -> T (a, b)
zip T a
s0 T b
s1)

{-# INLINE zipWith4 #-}
zipWith4 :: (a -> b -> c -> d -> e) -> (T a -> T b -> T c -> T d -> T e)
zipWith4 :: forall a b c d e.
(a -> b -> c -> d -> e) -> T a -> T b -> T c -> T d -> T e
zipWith4 a -> b -> c -> d -> e
f T a
s0 T b
s1 =
   forall a b c d. (a -> b -> c -> d) -> T a -> T b -> T c -> T d
zipWith3 (forall a b c. (a -> b -> c) -> (a, b) -> c
uncurry a -> b -> c -> d -> e
f) (forall a b. T a -> T b -> T (a, b)
zip T a
s0 T b
s1)


{-# INLINE zip #-}
zip :: T a -> T b -> T (a,b)
zip :: forall a b. T a -> T b -> T (a, b)
zip = forall a b c. (a -> b -> c) -> T a -> T b -> T c
zipWith (,)

{-# INLINE zip3 #-}
zip3 :: T a -> T b -> T c -> T (a,b,c)
zip3 :: forall a b c. T a -> T b -> T c -> T (a, b, c)
zip3 = forall a b c d. (a -> b -> c -> d) -> T a -> T b -> T c -> T d
zipWith3 (,,)

{-# INLINE zip4 #-}
zip4 :: T a -> T b -> T c -> T d -> T (a,b,c,d)
zip4 :: forall a b c d. T a -> T b -> T c -> T d -> T (a, b, c, d)
zip4 = forall a b c d e.
(a -> b -> c -> d -> e) -> T a -> T b -> T c -> T d -> T e
zipWith4 (,,,)


{- * functions based on 'foldL' -}

{-# INLINE foldL' #-}
foldL' :: (x -> acc -> acc) -> acc -> T x -> acc
foldL' :: forall a b. (a -> b -> b) -> b -> T a -> b
foldL' x -> acc -> acc
g acc
b0 T x
sig =
   forall y x.
T y
-> (forall s. (forall z. z -> (y -> s -> z) -> s -> z) -> s -> x)
-> x
runSwitchL T x
sig (\forall z. z -> (x -> s -> z) -> s -> z
next s
s0 ->
      let recurse :: acc -> s -> acc
recurse acc
b s
s =
             seq :: forall a b. a -> b -> b
seq acc
b (forall z. z -> (x -> s -> z) -> s -> z
next acc
b (\x
x -> acc -> s -> acc
recurse (x -> acc -> acc
g x
x acc
b)) s
s)
      in  acc -> s -> acc
recurse acc
b0 s
s0)
{-
foldL' g b =
   seq b . switchL b (\ x xs -> foldL' g (g x b) xs)
-}

{-# INLINE foldL #-}
foldL :: (acc -> x -> acc) -> acc -> T x -> acc
foldL :: forall b a. (b -> a -> b) -> b -> T a -> b
foldL acc -> x -> acc
f = forall a b. (a -> b -> b) -> b -> T a -> b
foldL' (forall a b c. (a -> b -> c) -> b -> a -> c
flip acc -> x -> acc
f)

{-# INLINE foldL1 #-}
foldL1 :: (x -> x -> x) -> T x -> x
foldL1 :: forall a. (a -> a -> a) -> T a -> a
foldL1 x -> x -> x
f =
   forall b a. b -> (a -> T a -> b) -> T a -> b
switchL
      (forall a. HasCallStack => String -> a
error String
"State.Signal.foldL1: empty signal")
      (forall b a. (b -> a -> b) -> b -> T a -> b
foldL x -> x -> x
f)

{-# INLINE length #-}
length :: T a -> Int
length :: forall a. T a -> Int
length = forall a b. (a -> b -> b) -> b -> T a -> b
foldL' (forall a b. a -> b -> a
const forall a. Enum a => a -> a
succ) forall a. C a => a
zero

{-# INLINE equal #-}
equal :: (Eq a) => T a -> T a -> Bool
equal :: forall y. Eq y => T y -> T y -> Bool
equal T a
xs T a
ys =
   forall y x. T y -> (forall s. (s -> Maybe (y, s)) -> s -> x) -> x
runViewL T a
xs (\s -> Maybe (a, s)
nextX s
sx ->
   forall y x. T y -> (forall s. (s -> Maybe (y, s)) -> s -> x) -> x
runViewL T a
ys (\s -> Maybe (a, s)
nextY s
sy ->
      let go :: s -> s -> Bool
go s
px s
py =
             case (s -> Maybe (a, s)
nextX s
px, s -> Maybe (a, s)
nextY s
py) of
                (Maybe (a, s)
Nothing, Maybe (a, s)
Nothing) -> Bool
True
                (Just (a
x,s
xr), Just (a
y,s
yr)) ->
                   a
xforall a. Eq a => a -> a -> Bool
==a
y Bool -> Bool -> Bool
&& s -> s -> Bool
go s
xr s
yr
                (Maybe (a, s), Maybe (a, s))
_ -> Bool
False
      in  s -> s -> Bool
go s
sx s
sy
   ))


{- * functions based on 'foldR' -}

foldR :: (x -> acc -> acc) -> acc -> T x -> acc
foldR :: forall a b. (a -> b -> b) -> b -> T a -> b
foldR x -> acc -> acc
g acc
b T x
sig =
   forall y x.
T y
-> (forall s. (forall z. z -> (y -> s -> z) -> s -> z) -> s -> x)
-> x
runSwitchL T x
sig (\forall z. z -> (x -> s -> z) -> s -> z
next s
s0 ->
      let recurse :: s -> acc
recurse =
             forall z. z -> (x -> s -> z) -> s -> z
next acc
b (\ x
x s
xs -> x -> acc -> acc
g x
x (s -> acc
recurse s
xs))
      in  s -> acc
recurse s
s0)
{-
foldR g b =
   switchL b (\ x xs -> g x (foldR g b xs))
-}


{- * Other functions -}

{-# INLINE null #-}
null :: T a -> Bool
null :: forall a. T a -> Bool
null =
   forall b a. b -> (a -> T a -> b) -> T a -> b
switchL Bool
True (forall a b. a -> b -> a
const (forall a b. a -> b -> a
const Bool
False))
   -- foldR (const (const False)) True

{-# INLINE empty #-}
empty :: T a
empty :: forall a. T a
empty = forall acc y. (acc -> Maybe (y, acc)) -> acc -> T y
generate (forall a b. a -> b -> a
const forall a. Maybe a
Nothing) ()

{-# INLINE singleton #-}
singleton :: a -> T a
singleton :: forall a. a -> T a
singleton =
   forall acc y. (acc -> Maybe (y, acc)) -> acc -> T y
generate (forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (\a
x -> (a
x, forall a. Maybe a
Nothing))) forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. a -> Maybe a
Just

{-# INLINE cons #-}
{- |
This is expensive and should not be used to construct lists iteratively!
-}
cons :: a -> T a -> T a
cons :: forall a. a -> T a -> T a
cons a
x T a
xs =
   forall acc y. (acc -> Maybe (y, acc)) -> acc -> T y
generate
      (\(Maybe a
mx0,T a
xs0) ->
          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 a. Maybe a
Nothing)) forall a b. (a -> b) -> a -> b
$
          forall b a. b -> (a -> b) -> Maybe a -> b
maybe
             (forall a. T a -> Maybe (a, T a)
viewL T a
xs0)
             (\a
x0 -> forall a. a -> Maybe a
Just (a
x0, T a
xs0))
             Maybe a
mx0) forall a b. (a -> b) -> a -> b
$
   (forall a. a -> Maybe a
Just a
x, T a
xs)

{-# INLINE viewL #-}
viewL :: T a -> Maybe (a, T a)
viewL :: forall a. T a -> Maybe (a, T a)
viewL (Cons StateT s Maybe a
f s
a0) =
   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 a s. StateT s Maybe a -> s -> T a
Cons StateT s Maybe a
f))
      (forall s (m :: * -> *) a. StateT s m a -> s -> m (a, s)
runStateT StateT s Maybe a
f s
a0)

{- iterated 'cons' is very inefficient
viewR :: T a -> Maybe (T a, a)
viewR =
   foldR (\x mxs -> Just (maybe (empty,x) (mapFst (cons x)) mxs)) Nothing
-}

{-# INLINE viewR #-}
viewR :: Storable a => T a -> Maybe (T a, a)
viewR :: forall a. Storable a => T a -> Maybe (T a, a)
viewR = forall a. Storable a => ChunkSize -> T a -> Maybe (T a, a)
viewRSize ChunkSize
SigSt.defaultChunkSize

{-# INLINE viewRSize #-}
viewRSize :: Storable a => SigSt.ChunkSize -> T a -> Maybe (T a, a)
viewRSize :: forall a. Storable a => ChunkSize -> T a -> Maybe (T a, a)
viewRSize ChunkSize
size =
   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 a. Storable a => T a -> T a
fromStorableSignal) forall b c a. (b -> c) -> (a -> b) -> a -> c
.
   forall a. Storable a => Vector a -> Maybe (Vector a, a)
SigSt.viewR forall b c a. (b -> c) -> (a -> b) -> a -> c
.
   forall a. Storable a => ChunkSize -> T a -> T a
toStorableSignal ChunkSize
size


{-# INLINE switchL #-}
switchL :: b -> (a -> T a -> b) -> T a -> b
switchL :: forall b a. b -> (a -> T a -> b) -> T a -> b
switchL b
n a -> T a -> b
j =
   forall b a. b -> (a -> b) -> Maybe a -> b
maybe b
n (forall a b c. (a -> b -> c) -> (a, b) -> c
uncurry a -> T a -> b
j) forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. T a -> Maybe (a, T a)
viewL

{-# INLINE switchR #-}
switchR :: Storable a => b -> (T a -> a -> b) -> T a -> b
switchR :: forall a b. Storable a => b -> (T a -> a -> b) -> T a -> b
switchR b
n T a -> a -> b
j =
   forall b a. b -> (a -> b) -> Maybe a -> b
maybe b
n (forall a b c. (a -> b -> c) -> (a, b) -> c
uncurry T a -> a -> b
j) forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Storable a => T a -> Maybe (T a, a)
viewR


{- |
This implementation requires
that the input generator has to check repeatedly whether it is finished.
-}
{-# INLINE extendConstant #-}
extendConstant :: T a -> T a
extendConstant :: forall a. T a -> T a
extendConstant T a
sig =
   forall y x.
T y
-> (forall s. (forall z. z -> (y -> s -> z) -> s -> z) -> s -> x)
-> x
runSwitchL T a
sig (\forall z. z -> (a -> s -> z) -> s -> z
switch s
s0 ->
   forall z. z -> (a -> s -> z) -> s -> z
switch
      forall a. T a
empty
      (\ a
x0 s
_ ->
          forall acc y. (acc -> Maybe (y, acc)) -> acc -> T y
generate
             (\xt1 :: (a, s)
xt1@(a
x1,s
s1) ->
                 forall a. a -> Maybe a
Just forall a b. (a -> b) -> a -> b
$ forall z. z -> (a -> s -> z) -> s -> z
switch
                    (a
x1,(a, s)
xt1)
                    (\a
x s
s2 -> (a
x, (a
x,s
s2)))
                    s
s1)
             (a
x0,s
s0)) forall a b. (a -> b) -> a -> b
$
      s
s0)


{-
{-# INLINE tail #-}
tail :: T a -> T a
tail = Cons . List.tail . decons

{-# INLINE head #-}
head :: T a -> a
head = List.head . decons
-}

{-# INLINE drop #-}
drop :: Int -> T a -> T a
drop :: forall a. Int -> T a -> T a
drop Int
n =
   forall a. a -> Maybe a -> a
fromMaybe forall a. T a
empty forall b c a. (b -> c) -> (a -> b) -> a -> c
.
   forall a. Int -> (a -> a) -> a -> a
nest Int
n (forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap forall a b. (a, b) -> b
snd forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. T a -> Maybe (a, T a)
viewL forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<<) forall b c a. (b -> c) -> (a -> b) -> a -> c
.
   forall a. a -> Maybe a
Just

{-# INLINE dropMarginRem #-}
{- |
This implementation expects that looking ahead is cheap.
-}
dropMarginRem :: Int -> Int -> T a -> (Int, T a)
dropMarginRem :: forall a. Int -> Int -> T a -> (Int, T a)
dropMarginRem Int
n Int
m =
   forall b a. b -> (a -> T a -> b) -> T a -> b
switchL (forall a. HasCallStack => String -> a
error forall a b. (a -> b) -> a -> b
$ String
"StateSignal.dropMaringRem: length xs < " forall a. [a] -> [a] -> [a]
++ forall a. Show a => a -> String
show Int
n) forall a b. a -> b -> a
const forall b c a. (b -> c) -> (a -> b) -> a -> c
.
   forall a. Int -> Int -> T a -> T a
dropMargin (forall a. Enum a => a -> a
succ Int
n) Int
m forall b c a. (b -> c) -> (a -> b) -> a -> c
.
   forall y0 y1 y2. (y0 -> T y1 -> y2) -> T y0 -> T y1 -> T y2
zipWithTails1 (,) (forall a. (a -> a) -> a -> T a
iterate (forall a. Ord a => a -> a -> a
max Int
0 forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Enum a => a -> a
pred) Int
m)

{-# INLINE dropMargin #-}
dropMargin :: Int -> Int -> T a -> T a
dropMargin :: forall a. Int -> Int -> T a -> T a
dropMargin Int
n Int
m T a
xs =
   forall a b. T a -> T b -> T b
dropMatch (forall a. Int -> T a -> T a
take Int
m (forall a. Int -> T a -> T a
drop Int
n T a
xs)) T a
xs


dropMatch :: T b -> T a -> T a
dropMatch :: forall a b. T a -> T b -> T b
dropMatch T b
xs T a
ys =
   forall a. a -> Maybe a -> a
fromMaybe T a
ys forall a b. (a -> b) -> a -> b
$
   forall (m :: * -> *) a1 a2 r.
Monad m =>
(a1 -> a2 -> r) -> m a1 -> m a2 -> m r
liftM2 forall a b. T a -> T b -> T b
dropMatch
      (forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap forall a b. (a, b) -> b
snd forall a b. (a -> b) -> a -> b
$ forall a. T a -> Maybe (a, T a)
viewL T b
xs)
      (forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap forall a b. (a, b) -> b
snd forall a b. (a -> b) -> a -> b
$ forall a. T a -> Maybe (a, T a)
viewL T a
ys)


index :: Int -> T a -> a
index :: forall a. Int -> T a -> a
index Int
n =
   forall b a. b -> (a -> T a -> b) -> T a -> b
switchL (forall a. HasCallStack => String -> a
error forall a b. (a -> b) -> a -> b
$ String
"State.Signal: index " forall a. [a] -> [a] -> [a]
++ forall a. Show a => a -> String
show Int
n forall a. [a] -> [a] -> [a]
++ String
" too large") forall a b. a -> b -> a
const forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Int -> T a -> T a
drop Int
n


{-
splitAt :: Int -> T a -> (T a, T a)
splitAt n = mapPair (Cons, Cons) . List.splitAt n . decons
-}

{-# INLINE splitAt #-}
splitAt :: Storable a =>
   Int -> T a -> (T a, T a)
splitAt :: forall a. Storable a => Int -> T a -> (T a, T a)
splitAt = forall a. Storable a => ChunkSize -> Int -> T a -> (T a, T a)
splitAtSize ChunkSize
SigSt.defaultChunkSize

{-# INLINE splitAtSize #-}
splitAtSize :: Storable a =>
   SigSt.ChunkSize -> Int -> T a -> (T a, T a)
splitAtSize :: forall a. Storable a => ChunkSize -> Int -> T a -> (T a, T a)
splitAtSize ChunkSize
size Int
n =
   forall a c b d. (a -> c, b -> d) -> (a, b) -> (c, d)
mapPair (forall a. Storable a => T a -> T a
fromStorableSignal, forall a. Storable a => T a -> T a
fromStorableSignal) forall b c a. (b -> c) -> (a -> b) -> a -> c
.
   forall a. Storable a => Int -> Vector a -> (Vector a, Vector a)
SigSt.splitAt Int
n forall b c a. (b -> c) -> (a -> b) -> a -> c
.
   forall a. Storable a => ChunkSize -> T a -> T a
toStorableSignal ChunkSize
size


{-# INLINE dropWhile #-}
dropWhile :: (a -> Bool) -> T a -> T a
dropWhile :: forall a. (a -> Bool) -> T a -> T a
dropWhile a -> Bool
p (Cons StateT s Maybe a
f s
s0) =
   let recurse :: s -> T a
recurse s
s =
          forall b a. b -> (a -> b) -> Maybe a -> b
maybe forall a. T a
empty (\(a
x,s
s1) -> forall a. Bool -> a -> a -> a
if' (a -> Bool
p a
x) (s -> T a
recurse s
s1) (forall a s. StateT s Maybe a -> s -> T a
Cons StateT s Maybe a
f s
s)) forall a b. (a -> b) -> a -> b
$
          forall s (m :: * -> *) a. StateT s m a -> s -> m (a, s)
runStateT StateT s Maybe a
f s
s
   in  s -> T a
recurse s
s0
{-
dropWhile p xt =
   switchL empty (\ x xs -> if p x then dropWhile p xs else xt) xt
-}

{-
span :: (a -> Bool) -> T a -> (T a, T a)
span p = mapPair (Cons, Cons) . List.span p . decons
-}

{-# INLINE span #-}
span :: Storable a =>
   (a -> Bool) -> T a -> (T a, T a)
span :: forall a. Storable a => (a -> Bool) -> T a -> (T a, T a)
span = forall a.
Storable a =>
ChunkSize -> (a -> Bool) -> T a -> (T a, T a)
spanSize ChunkSize
SigSt.defaultChunkSize

{-# INLINE spanSize #-}
spanSize :: Storable a =>
   SigSt.ChunkSize -> (a -> Bool) -> T a -> (T a, T a)
spanSize :: forall a.
Storable a =>
ChunkSize -> (a -> Bool) -> T a -> (T a, T a)
spanSize ChunkSize
size a -> Bool
p =
   forall a c b d. (a -> c, b -> d) -> (a, b) -> (c, d)
mapPair (forall a. Storable a => T a -> T a
fromStorableSignal, forall a. Storable a => T a -> T a
fromStorableSignal) forall b c a. (b -> c) -> (a -> b) -> a -> c
.
   forall a.
Storable a =>
(a -> Bool) -> Vector a -> (Vector a, Vector a)
SigSt.span a -> Bool
p forall b c a. (b -> c) -> (a -> b) -> a -> c
.
   forall a. Storable a => ChunkSize -> T a -> T a
toStorableSignal ChunkSize
size


{-# INLINE cycle #-}
cycle :: T a -> T a
cycle :: forall a. T a -> T a
cycle T a
sig =
   forall y x. T y -> (forall s. (s -> Maybe (y, s)) -> s -> x) -> x
runViewL T a
sig
      (\s -> Maybe (a, s)
next s
s ->
          forall b a. b -> (a -> b) -> Maybe a -> b
maybe
             (forall a. HasCallStack => String -> a
error String
"StateSignal.cycle: empty input")
             (\(a, s)
yt -> forall acc y. (acc -> Maybe (y, acc)) -> acc -> T y
generate (forall a. a -> Maybe a
Just forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. a -> Maybe a -> a
fromMaybe (a, s)
yt forall b c a. (b -> c) -> (a -> b) -> a -> c
. s -> Maybe (a, s)
next) s
s) forall a b. (a -> b) -> a -> b
$
             s -> Maybe (a, s)
next s
s)
{-
cycle xs =
   maybe
      (error "StateSignal.cycle: empty input")
      (\yt -> generate (Just . fromMaybe yt . viewL) xs) $
      viewL xs
-}


{-# SPECIALISE INLINE mix :: T Float -> T Float -> T Float #-}
{-# SPECIALISE INLINE mix :: T Double -> T Double -> T Double #-}
{-# INLINE mix #-}
mix :: Additive.C a => T a -> T a -> T a
mix :: forall a. C a => T a -> T a -> T a
mix = forall y. (y -> y -> y) -> T y -> T y -> T y
zipWithAppend forall a. C a => a -> a -> a
(Additive.+)


{-# INLINE sub #-}
sub :: Additive.C a => T a -> T a -> T a
sub :: forall a. C a => T a -> T a -> T a
sub T a
xs T a
ys =  forall a. C a => T a -> T a -> T a
mix T a
xs (forall a. C a => T a -> T a
neg T a
ys)

{-# INLINE neg #-}
neg :: Additive.C a => T a -> T a
neg :: forall a. C a => T a -> T a
neg = forall a b. (a -> b) -> T a -> T b
map forall a. C a => a -> a
Additive.negate

instance Additive.C y => Additive.C (T y) where
   zero :: T y
zero = forall a. T a
empty
   + :: T y -> T y -> T y
(+) = forall a. C a => T a -> T a -> T a
mix
   (-) = forall a. C a => T a -> T a -> T a
sub
   negate :: T y -> T y
negate = forall a. C a => T a -> T a
neg

instance Module.C y yv => Module.C y (T yv) where
   *> :: y -> T yv -> T yv
(*>) y
x T yv
y = forall a b. (a -> b) -> T a -> T b
map (y
xforall a v. C a v => a -> v -> v
*>) T yv
y


infixr 5 `append`

{-# INLINE append #-}
append :: T a -> T a -> T a
append :: forall a. T a -> T a -> T a
append T a
xs T a
ys =
   forall acc y. (acc -> Maybe (y, acc)) -> acc -> T y
generate
      (\(Bool
b,T a
xys) ->
          forall (m :: * -> *) a. MonadPlus m => m a -> m a -> m a
mplus
             (forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (forall b c a. (b -> c) -> (a, b) -> (a, c)
mapSnd ((,) Bool
b)) forall a b. (a -> b) -> a -> b
$ forall a. T a -> Maybe (a, T a)
viewL T a
xys)
             (forall a. Bool -> a -> a -> a
if' Bool
b forall a. Maybe a
Nothing
                (forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (forall b c a. (b -> c) -> (a, b) -> (a, c)
mapSnd ((,) Bool
True)) forall a b. (a -> b) -> a -> b
$ forall a. T a -> Maybe (a, T a)
viewL T a
ys)))
      (Bool
False,T a
xs)

{-# INLINE appendStored #-}
appendStored :: Storable a =>
   T a -> T a -> T a
appendStored :: forall a. Storable a => T a -> T a -> T a
appendStored = forall a. Storable a => ChunkSize -> T a -> T a -> T a
appendStoredSize ChunkSize
SigSt.defaultChunkSize

{-# INLINE appendStoredSize #-}
appendStoredSize :: Storable a =>
   SigSt.ChunkSize -> T a -> T a -> T a
appendStoredSize :: forall a. Storable a => ChunkSize -> T a -> T a -> T a
appendStoredSize ChunkSize
size T a
xs T a
ys =
   forall a. Storable a => T a -> T a
fromStorableSignal forall a b. (a -> b) -> a -> b
$
   forall a. Storable a => Vector a -> Vector a -> Vector a
SigSt.append
      (forall a. Storable a => ChunkSize -> T a -> T a
toStorableSignal ChunkSize
size T a
xs)
      (forall a. Storable a => ChunkSize -> T a -> T a
toStorableSignal ChunkSize
size T a
ys)

{-# INLINE concat #-}
-- | certainly inefficient because of frequent list deconstruction
concat :: [T a] -> T a
concat :: forall a. [T a] -> T a
concat =
   forall acc y. (acc -> Maybe (y, acc)) -> acc -> T y
generate
      (forall (t :: * -> *) (m :: * -> *) a.
(Foldable t, MonadPlus m) =>
t (m a) -> m a
msum forall b c a. (b -> c) -> (a -> b) -> a -> c
.
       forall a b. (a -> b) -> [a] -> [b]
List.map
          (\ [T a]
x -> forall a. [a] -> Maybe (a, [a])
ListHT.viewL [T a]
x forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>=
           \(T a
y,[T a]
ys) -> forall a. T a -> Maybe (a, T a)
viewL T a
y forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>=
           \(a
z,T a
zs) -> forall a. a -> Maybe a
Just (a
z,T a
zsforall a. a -> [a] -> [a]
:[T a]
ys)) forall b c a. (b -> c) -> (a -> b) -> a -> c
.
       forall a. [a] -> [a]
List.init forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. [a] -> [[a]]
List.tails)


{-# INLINE concatStored #-}
concatStored :: Storable a =>
   [T a] -> T a
concatStored :: forall a. Storable a => [T a] -> T a
concatStored = forall a. Storable a => ChunkSize -> [T a] -> T a
concatStoredSize ChunkSize
SigSt.defaultChunkSize

{-# INLINE concatStoredSize #-}
concatStoredSize :: Storable a =>
   SigSt.ChunkSize -> [T a] -> T a
concatStoredSize :: forall a. Storable a => ChunkSize -> [T a] -> T a
concatStoredSize ChunkSize
size =
   forall a. Storable a => T a -> T a
fromStorableSignal forall b c a. (b -> c) -> (a -> b) -> a -> c
.
   forall a. Storable a => [Vector a] -> Vector a
SigSt.concat forall b c a. (b -> c) -> (a -> b) -> a -> c
.
   forall a b. (a -> b) -> [a] -> [b]
List.map (forall a. Storable a => ChunkSize -> T a -> T a
toStorableSignal ChunkSize
size)

{-
This should be faster than Monad.ap
if an empty signal as second operand is detected.
In this case an empty signal is returned without running a loop.
-}
liftA2 :: (a -> b -> c) -> (T a -> T b -> T c)
liftA2 :: forall a b c. (a -> b -> c) -> T a -> T b -> T c
liftA2 a -> b -> c
p T a
x T b
y =
   forall y x. T y -> (forall s. (s -> Maybe (y, s)) -> s -> x) -> x
runViewL T a
x forall a b. (a -> b) -> a -> b
$ \s -> Maybe (a, s)
f s
s0 ->
   forall y x. T y -> (forall s. (s -> Maybe (y, s)) -> s -> x) -> x
runViewL T b
y forall a b. (a -> b) -> a -> b
$ \s -> Maybe (b, s)
g s
t0 ->
   forall a b c. (a -> b -> c) -> b -> a -> c
flip forall acc y. (acc -> Maybe (y, acc)) -> acc -> T y
generate (forall (f :: * -> *) a b c.
Applicative f =>
(a -> b -> c) -> f a -> f b -> f c
App.liftA2 (,) (s -> Maybe (a, s)
f s
s0) (s -> Maybe (b, s)
g s
t0)) forall a b. (a -> b) -> a -> b
$ \Maybe ((a, s), (b, s))
m ->
   forall a b c. (a -> b -> c) -> b -> a -> c
flip forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Maybe ((a, s), (b, s))
m forall a b. (a -> b) -> a -> b
$ \(as :: (a, s)
as@(a
a,s
s), (b
b,s
t)) ->
   (a -> b -> c
p a
a b
b,
    forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap ((,) (a, s)
as) (s -> Maybe (b, s)
g s
t) forall (m :: * -> *) a. MonadPlus m => m a -> m a -> m a
`mplus`
    forall (f :: * -> *) a b c.
Applicative f =>
(a -> b -> c) -> f a -> f b -> f c
App.liftA2 (,) (s -> Maybe (a, s)
f s
s) (s -> Maybe (b, s)
g s
t0))

{-# INLINE reverse #-}
reverse ::
   T a -> T a
reverse :: forall a. T a -> T a
reverse =
   forall y. [y] -> T y
fromList forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. [a] -> [a]
List.reverse forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall y. T y -> [y]
toList

{-# INLINE reverseStored #-}
reverseStored :: Storable a =>
   T a -> T a
reverseStored :: forall a. Storable a => T a -> T a
reverseStored = forall a. Storable a => ChunkSize -> T a -> T a
reverseStoredSize ChunkSize
SigSt.defaultChunkSize

{-# INLINE reverseStoredSize #-}
reverseStoredSize :: Storable a =>
   SigSt.ChunkSize -> T a -> T a
reverseStoredSize :: forall a. Storable a => ChunkSize -> T a -> T a
reverseStoredSize ChunkSize
size =
   forall a. Storable a => T a -> T a
fromStorableSignal forall b c a. (b -> c) -> (a -> b) -> a -> c
.
   forall a. Storable a => Vector a -> Vector a
SigSt.reverse forall b c a. (b -> c) -> (a -> b) -> a -> c
.
   forall a. Storable a => ChunkSize -> T a -> T a
toStorableSignal ChunkSize
size


{-# INLINE sum #-}
sum :: (Additive.C a) => T a -> a
sum :: forall a. C a => T a -> a
sum = forall a b. (a -> b -> b) -> b -> T a -> b
foldL' forall a. C a => a -> a -> a
(Additive.+) forall a. C a => a
Additive.zero

{-# INLINE maximum #-}
maximum :: (Ord a) => T a -> a
maximum :: forall a. Ord a => T a -> a
maximum =
   forall b a. b -> (a -> T a -> b) -> T a -> b
switchL
      (forall a. HasCallStack => String -> a
error String
"StateSignal.maximum: empty list")
      (forall a b. (a -> b -> b) -> b -> T a -> b
foldL' forall a. Ord a => a -> a -> a
max)

{-
{-# INLINE tails #-}
tails :: T y -> [T y]
tails = List.map Cons . List.tails . decons
-}

{-# INLINE init #-}
init :: T y -> T y
init :: forall a. T a -> T a
init =
   forall b a. b -> (a -> T a -> b) -> T a -> b
switchL
      (forall a. HasCallStack => String -> a
error String
"StateSignal.init: empty list")
      (forall x acc y. (x -> acc -> Maybe (y, acc)) -> acc -> T x -> T y
crochetL (\y
x y
acc -> forall a. a -> Maybe a
Just (y
acc,y
x)))

{-# INLINE sliceVert #-}
-- inefficient since it computes some things twice
sliceVert :: Int -> T y -> [T y]
sliceVert :: forall y. Int -> T y -> [T y]
sliceVert Int
n =
--   map fromList . Sig.sliceVert n . toList
   forall a b. (a -> b) -> [a] -> [b]
List.map (forall a. Int -> T a -> T a
take Int
n) forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. (a -> Bool) -> [a] -> [a]
List.takeWhile (Bool -> Bool
not forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. T a -> Bool
null) forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. (a -> a) -> a -> [a]
List.iterate (forall a. Int -> T a -> T a
drop Int
n)

{-# DEPRECATED zapWith, zapWithAlt "use mapAdjacent" #-}
{-# INLINE zapWith #-}
zapWith :: (a -> a -> b) -> T a -> T b
zapWith :: forall a b. (a -> a -> b) -> T a -> T b
zapWith = forall a b. (a -> a -> b) -> T a -> T b
mapAdjacent

zapWithAlt :: (a -> a -> b) -> T a -> T b
zapWithAlt :: forall a b. (a -> a -> b) -> T a -> T b
zapWithAlt a -> a -> b
f T a
xs =
   forall a b c. (a -> b -> c) -> T a -> T b -> T c
zipWith a -> a -> b
f T a
xs (forall b a. b -> (a -> T a -> b) -> T a -> b
switchL forall a. T a
empty (forall a b c. ((a, b) -> c) -> a -> b -> c
curry forall a b. (a, b) -> b
snd) T a
xs)

{-# INLINE mapAdjacent #-}
mapAdjacent :: (a -> a -> b) -> T a -> T b
mapAdjacent :: forall a b. (a -> a -> b) -> T a -> T b
mapAdjacent a -> a -> b
f =
   forall b a. b -> (a -> T a -> b) -> T a -> b
switchL forall a. T a
empty
      (forall x acc y. (x -> acc -> Maybe (y, acc)) -> acc -> T x -> T y
crochetL (\a
y a
x -> forall a. a -> Maybe a
Just (a -> a -> b
f a
x a
y, a
y)))

{-# INLINE modifyStatic #-}
modifyStatic :: Modifier.Simple s ctrl a b -> ctrl -> T a -> T b
modifyStatic :: forall s ctrl a b. Simple s ctrl a b -> ctrl -> T a -> T b
modifyStatic Simple s ctrl a b
modif ctrl
control T a
x =
   forall x acc y. (x -> acc -> Maybe (y, acc)) -> acc -> T x -> T y
crochetL
      (\a
a s
acc ->
         forall a. a -> Maybe a
Just (forall s a. State s a -> s -> (a, s)
runState (forall s ctrl a b. Simple s ctrl a b -> ctrl -> a -> State s b
Modifier.step Simple s ctrl a b
modif ctrl
control a
a) s
acc))
      (forall s ctrl a b. Simple s ctrl a b -> s
Modifier.init Simple s ctrl a b
modif) T a
x

{-| Here the control may vary over the time. -}
{-# INLINE modifyModulated #-}
modifyModulated :: Modifier.Simple s ctrl a b -> T ctrl -> T a -> T b
modifyModulated :: forall s ctrl a b. Simple s ctrl a b -> T ctrl -> T a -> T b
modifyModulated Simple s ctrl a b
modif T ctrl
control T a
x =
   forall x acc y. (x -> acc -> Maybe (y, acc)) -> acc -> T x -> T y
crochetL
      (\(ctrl, a)
ca s
acc ->
         forall a. a -> Maybe a
Just (forall s a. State s a -> s -> (a, s)
runState (forall a b c. (a -> b -> c) -> (a, b) -> c
uncurry (forall s ctrl a b. Simple s ctrl a b -> ctrl -> a -> State s b
Modifier.step Simple s ctrl a b
modif) (ctrl, a)
ca) s
acc))
      (forall s ctrl a b. Simple s ctrl a b -> s
Modifier.init Simple s ctrl a b
modif)
      (forall a b. T a -> T b -> T (a, b)
zip T ctrl
control T a
x)


-- cf. Module.linearComb
{-# INLINE linearComb #-}
linearComb ::
   (Module.C t y) =>
   T t -> T y -> y
linearComb :: forall t y. C t y => T t -> T y -> y
linearComb T t
ts T y
ys =
   forall a. C a => T a -> a
sum forall a b. (a -> b) -> a -> b
$ forall a b c. (a -> b -> c) -> T a -> T b -> T c
zipWith forall a v. C a v => a -> v -> v
(*>) T t
ts T y
ys


-- comonadic 'bind'
-- only non-empty suffixes are processed
{-# INLINE mapTails #-}
mapTails ::
   (T y0 -> y1) -> T y0 -> T y1
mapTails :: forall y0 y1. (T y0 -> y1) -> T y0 -> T y1
mapTails T y0 -> y1
f =
   forall acc y. (acc -> Maybe (y, acc)) -> acc -> T y
generate (\T y0
xs ->
      do (y0
_,T y0
ys) <- forall a. T a -> Maybe (a, T a)
viewL T y0
xs
         forall (m :: * -> *) a. Monad m => a -> m a
return (T y0 -> y1
f T y0
xs, T y0
ys))
{-
mapTails f xs0 =
   runViewL xs0 (\next ->
      generate (\xs ->
         do (_,ys) <- next xs
            return (f xs, ys)))
-}

-- | only non-empty suffixes are processed
{-# INLINE zipWithTails #-}
zipWithTails ::
   (y0 -> T y1 -> y2) -> T y0 -> T y1 -> T y2
zipWithTails :: forall y0 y1 y2. (y0 -> T y1 -> y2) -> T y0 -> T y1 -> T y2
zipWithTails y0 -> T y1 -> y2
f =
   forall a b c. ((a, b) -> c) -> a -> b -> c
curry forall a b. (a -> b) -> a -> b
$ forall acc y. (acc -> Maybe (y, acc)) -> acc -> T y
generate (\(T y0
xs0,T y1
ys0) ->
      do (y0
x,T y0
xs) <- forall a. T a -> Maybe (a, T a)
viewL T y0
xs0
         (y1
_,T y1
ys) <- forall a. T a -> Maybe (a, T a)
viewL T y1
ys0
         forall (m :: * -> *) a. Monad m => a -> m a
return (y0 -> T y1 -> y2
f y0
x T y1
ys0, (T y0
xs,T y1
ys)))
{-
zipWithTails f xs1 ys1 =
   runViewL xs1 (\nextX xs2 ->
   runViewL ys1 (\nextY ys2 ->
      generate (\(xs0,ys0) ->
         do (x,xs) <- nextX xs0
            (_,ys) <- nextY ys0
            return (f x ys0, (xs,ys)))
         (xs2,ys2)))
-}

-- | in contrast to 'zipWithTails' it also generates the empty suffix (once)
{-# INLINE zipWithTails1 #-}
zipWithTails1 ::
   (y0 -> T y1 -> y2) -> T y0 -> T y1 -> T y2
zipWithTails1 :: forall y0 y1 y2. (y0 -> T y1 -> y2) -> T y0 -> T y1 -> T y2
zipWithTails1 y0 -> T y1 -> y2
f T y0
xs T y1
ys =
   forall acc y. (acc -> Maybe (y, acc)) -> acc -> T y
generate (\(T y0
xs0,Maybe (T y1)
ys0) ->
      do (y0
x,T y0
xs1) <- forall a. T a -> Maybe (a, T a)
viewL T y0
xs0
         T y1
ys1 <- Maybe (T y1)
ys0
         forall (m :: * -> *) a. Monad m => a -> m a
return (y0 -> T y1 -> y2
f y0
x T y1
ys1, (T y0
xs1, forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap forall a b. (a, b) -> b
snd forall a b. (a -> b) -> a -> b
$ forall a. T a -> Maybe (a, T a)
viewL T y1
ys1)))
      (T y0
xs, forall a. a -> Maybe a
Just T y1
ys)

-- | in contrast to 'zipWithTails' it appends infinitely many empty suffixes
{-# INLINE zipWithTailsInf #-}
zipWithTailsInf ::
   (y0 -> T y1 -> y2) -> T y0 -> T y1 -> T y2
zipWithTailsInf :: forall y0 y1 y2. (y0 -> T y1 -> y2) -> T y0 -> T y1 -> T y2
zipWithTailsInf y0 -> T y1 -> y2
f =
   forall a b c. ((a, b) -> c) -> a -> b -> c
curry forall a b. (a -> b) -> a -> b
$ forall acc y. (acc -> Maybe (y, acc)) -> acc -> T y
generate (\(T y0
xs0,T y1
ys0) ->
      do (y0
x,T y0
xs) <- forall a. T a -> Maybe (a, T a)
viewL T y0
xs0
         forall (m :: * -> *) a. Monad m => a -> m a
return (y0 -> T y1 -> y2
f y0
x T y1
ys0, (T y0
xs, forall b a. b -> (a -> T a -> b) -> T a -> b
switchL forall a. T a
empty (forall a b c. (a -> b -> c) -> b -> a -> c
flip forall a b. a -> b -> a
const) T y1
ys0)))

{-
This can hardly be implemented in an efficient way.
But this means, we cannot implement the Generic.Transform class.

zipWithRest ::
   (y0 -> y0 -> y1) ->
   T y0 -> T y0 ->
   (T y1, (Bool, T y0))
zipWithRest f =
   curry $ generate (\(xs0,ys0) ->
      do (x,xs) <- viewL xs0
         (y,ys) <- viewL ys0
         return (f x y, (xs,ys)))
-}


{-# INLINE zipWithAppend #-}
zipWithAppend ::
   (y -> y -> y) ->
   T y -> T y -> T y
zipWithAppend :: forall y. (y -> y -> y) -> T y -> T y -> T y
zipWithAppend y -> y -> y
f T y
xs T y
ys =
   forall y x. T y -> (forall s. (s -> Maybe (y, s)) -> s -> x) -> x
runViewL T y
xs (\s -> Maybe (y, s)
nextX s
sx ->
   forall y x. T y -> (forall s. (s -> Maybe (y, s)) -> s -> x) -> x
runViewL T y
ys (\s -> Maybe (y, s)
nextY s
sy ->
      forall acc y. (acc -> Maybe (y, acc)) -> acc -> T y
unfoldR (forall s a t.
(s -> Maybe (a, s))
-> (t -> Maybe (a, t))
-> (a -> a -> a)
-> (s, t)
-> Maybe (a, (s, t))
zipStep s -> Maybe (y, s)
nextX s -> Maybe (y, s)
nextY y -> y -> y
f) (s
sx,s
sy)
   ))

{-# INLINE zipStep #-}
zipStep ::
   (s -> Maybe (a,s)) ->
   (t -> Maybe (a,t)) ->
   (a -> a -> a) -> (s, t) -> Maybe (a, (s, t))
zipStep :: forall s a t.
(s -> Maybe (a, s))
-> (t -> Maybe (a, t))
-> (a -> a -> a)
-> (s, t)
-> Maybe (a, (s, t))
zipStep s -> Maybe (a, s)
nextX t -> Maybe (a, t)
nextY a -> a -> a
f (s
xt,t
yt) =
   case (s -> Maybe (a, s)
nextX s
xt, t -> Maybe (a, t)
nextY t
yt) of
      (Just (a
x,s
xs), Just (a
y,t
ys)) -> forall a. a -> Maybe a
Just (a -> a -> a
f a
x a
y, (s
xs,t
ys))
      (Maybe (a, s)
Nothing,     Just (a
y,t
ys)) -> forall a. a -> Maybe a
Just (a
y,     (s
xt,t
ys))
      (Just (a
x,s
xs), Maybe (a, t)
Nothing)     -> forall a. a -> Maybe a
Just (a
x,     (s
xs,t
yt))
      (Maybe (a, s)
Nothing,     Maybe (a, t)
Nothing)     -> forall a. Maybe a
Nothing



delayLoop ::
      (T y -> T y)
            -- ^ processor that shall be run in a feedback loop
   -> T y   -- ^ prefix of the output, its length determines the delay
   -> T y
delayLoop :: forall y. (T y -> T y) -> T y -> T y
delayLoop T y -> T y
proc T y
prefix =
   -- the temporary list is need for sharing the output
   let ys :: T y
ys = forall y. [y] -> T y
fromList (forall y. T y -> [y]
toList T y
prefix forall a. [a] -> [a] -> [a]
List.++ forall y. T y -> [y]
toList (T y -> T y
proc T y
ys))
   in  T y
ys

delayLoopOverlap ::
   (Additive.C y) =>
      Int
   -> (T y -> T y)
            -- ^ processor that shall be run in a feedback loop
   -> T y   -- ^ input
   -> T y   -- ^ output has the same length as the input
delayLoopOverlap :: forall y. C y => Int -> (T y -> T y) -> T y -> T y
delayLoopOverlap Int
time T y -> T y
proc T y
xs =
   -- the temporary list is need for sharing the output
   let ys :: T y
ys = forall a b c. (a -> b -> c) -> T a -> T b -> T c
zipWith forall a. C a => a -> a -> a
(Additive.+) T y
xs (forall y. y -> Int -> T y -> T y
delay forall a. C a => a
zero Int
time (T y -> T y
proc (forall y. [y] -> T y
fromList (forall y. T y -> [y]
toList T y
ys))))
   in  T y
ys


{-
A traversable instance is hardly useful,
because 'cons' is so expensive.

instance Traversable T where
-}
{-# INLINE sequence_ #-}
sequence_ :: Monad m => T (m a) -> m ()
sequence_ :: forall (m :: * -> *) a. Monad m => T (m a) -> m ()
sequence_ =
   forall b a. b -> (a -> T a -> b) -> T a -> b
switchL (forall (m :: * -> *) a. Monad m => a -> m a
return ()) (\m a
x T (m a)
xs -> m a
x forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> forall (m :: * -> *) a. Monad m => T (m a) -> m ()
sequence_ T (m a)
xs)

{-# INLINE mapM_ #-}
mapM_ :: Monad m => (a -> m ()) -> T a -> m ()
mapM_ :: forall (m :: * -> *) a. Monad m => (a -> m ()) -> T a -> m ()
mapM_ a -> m ()
f = forall (m :: * -> *) a. Monad m => T (m a) -> m ()
sequence_ forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b. (a -> b) -> T a -> T b
map a -> m ()
f


{- |
Counterpart to 'Data.Monoid.mconcat'.
-}
fold :: Monoid m => T m -> m
fold :: forall m. Monoid m => T m -> m
fold = forall a b. (a -> b -> b) -> b -> T a -> b
foldR forall a. Monoid a => a -> a -> a
mappend forall a. Monoid a => a
mempty

{-# DEPRECATED monoidConcat "Use foldMap instead." #-}
monoidConcat :: Monoid m => T m -> m
monoidConcat :: forall m. Monoid m => T m -> m
monoidConcat = forall m. Monoid m => T m -> m
fold


foldMap :: Monoid m => (a -> m) -> T a -> m
foldMap :: forall m a. Monoid m => (a -> m) -> T a -> m
foldMap a -> m
f = forall m. Monoid m => T m -> m
monoidConcat forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b. (a -> b) -> T a -> T b
map a -> m
f

{-# DEPRECATED monoidConcatMap "Use foldMap instead." #-}
monoidConcatMap :: Monoid m => (a -> m) -> T a -> m
monoidConcatMap :: forall m a. Monoid m => (a -> m) -> T a -> m
monoidConcatMap = forall m a. Monoid m => (a -> m) -> T a -> m
foldMap


instance Semigroup (T y) where
   <> :: T y -> T y -> T y
(<>) = forall a. T a -> T a -> T a
append

instance Monoid (T y) where
   mempty :: T y
mempty = forall a. T a
empty
   mappend :: T y -> T y -> T y
mappend = forall a. Semigroup a => a -> a -> a
(<>)


catMaybes :: T (Maybe a) -> T a
catMaybes :: forall a. T (Maybe a) -> T a
catMaybes T (Maybe a)
sig =
   forall y x. T y -> (forall s. (s -> Maybe (y, s)) -> s -> x) -> x
runViewL T (Maybe a)
sig (\s -> Maybe (Maybe a, s)
next ->
   forall acc y. (acc -> Maybe (y, acc)) -> acc -> T y
generate (
      let go :: s -> Maybe (a, s)
go s
s0 =
             s -> Maybe (Maybe a, s)
next s
s0 forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \(Maybe a
ma,s
s1) ->
             forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (forall a b c. (a -> b -> c) -> b -> a -> c
flip (,) s
s1) Maybe a
ma forall (m :: * -> *) a. MonadPlus m => m a -> m a -> m a
`mplus`
             s -> Maybe (a, s)
go s
s1
      in  s -> Maybe (a, s)
go))

flattenPairs :: T (a,a) -> T a
flattenPairs :: forall a. T (a, a) -> T a
flattenPairs T (a, a)
sig =
   forall y x. T y -> (forall s. (s -> Maybe (y, s)) -> s -> x) -> x
runViewL T (a, a)
sig (\s -> Maybe ((a, a), s)
next s
t ->
   forall acc y. (acc -> Maybe (y, acc)) -> acc -> T y
generate
      (\(Maybe a
carry,s
s0) ->
         forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (\a
b -> (a
b, (forall a. Maybe a
Nothing, s
s0))) Maybe a
carry forall (m :: * -> *) a. MonadPlus m => m a -> m a -> m a
`mplus`
         forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (\((a
a,a
b),s
s1) -> (a
a, (forall a. a -> Maybe a
Just a
b, s
s1))) (s -> Maybe ((a, a), s)
next s
s0))
      (forall a. Maybe a
Nothing,s
t))

interleave, interleaveAlt ::
   T y -> T y -> T y
interleave :: forall a. T a -> T a -> T a
interleave T y
xs T y
ys =
   forall y x. T y -> (forall s. (s -> Maybe (y, s)) -> s -> x) -> x
runViewL T y
xs (\s -> Maybe (y, s)
nextX s
sx ->
   forall y x. T y -> (forall s. (s -> Maybe (y, s)) -> s -> x) -> x
runViewL T y
ys (\s -> Maybe (y, s)
nextY s
sy ->
   forall acc y. (acc -> Maybe (y, acc)) -> acc -> T y
unfoldR
      (\(Bool
select,(s
sx0,s
sy0)) ->
         case Bool
select of
            Bool
False -> forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (forall b c a. (b -> c) -> (a, b) -> (a, c)
mapSnd (\s
sx1 -> (Bool
True,  (s
sx1,s
sy0)))) forall a b. (a -> b) -> a -> b
$ s -> Maybe (y, s)
nextX s
sx0
            Bool
True  -> forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (forall b c a. (b -> c) -> (a, b) -> (a, c)
mapSnd (\s
sy1 -> (Bool
False, (s
sx0,s
sy1)))) forall a b. (a -> b) -> a -> b
$ s -> Maybe (y, s)
nextY s
sy0)
      (Bool
False, (s
sx,s
sy))))

interleaveAlt :: forall a. T a -> T a -> T a
interleaveAlt T y
xs T y
ys = forall a. T (a, a) -> T a
flattenPairs forall a b. (a -> b) -> a -> b
$ forall a b. T a -> T b -> T (a, b)
zip T y
xs T y
ys