{-# LANGUAGE CPP #-}
module Streamly.Internal.Data.Unfold.Type
(
Step(..)
, Unfold (..)
, mkUnfoldM
, mkUnfoldrM
, unfoldrM
, unfoldr
, functionM
, function
, identity
, fromEffect
, fromPure
, fromList
, lmap
, lmapM
, map
, map2
, mapM
, mapM2
, both
, first
, second
, takeWhileMWithInput
, takeWhileM
, takeWhile
, ConcatState (..)
, many
, many2
, manyInterleave
, crossApplySnd
, crossApplyFst
, crossWithM
, crossWith
, cross
, crossApply
, concatMapM
, concatMap
, bind
, zipWithM
, zipWith
)
where
#include "inline.hs"
import Control.Monad ((>=>))
import Data.Void (Void)
import Fusion.Plugin.Types (Fuse(..))
import Streamly.Internal.Data.Tuple.Strict (Tuple'(..))
import Streamly.Internal.Data.Stream.Step (Step(..))
import Prelude hiding (map, mapM, concatMap, zipWith, takeWhile)
#include "DocTestDataUnfold.hs"
data Unfold m a b =
forall s. Unfold (s -> m (Step s b)) (a -> m s)
{-# INLINE mkUnfoldM #-}
mkUnfoldM :: (s -> m (Step s b)) -> (a -> m s) -> Unfold m a b
mkUnfoldM :: forall s (m :: * -> *) b a.
(s -> m (Step s b)) -> (a -> m s) -> Unfold m a b
mkUnfoldM = (s -> m (Step s b)) -> (a -> m s) -> Unfold m a b
forall (m :: * -> *) a b s.
(s -> m (Step s b)) -> (a -> m s) -> Unfold m a b
Unfold
{-# INLINE mkUnfoldrM #-}
mkUnfoldrM :: Applicative m => (a -> m (Step a b)) -> Unfold m a b
mkUnfoldrM :: forall (m :: * -> *) a b.
Applicative m =>
(a -> m (Step a b)) -> Unfold m a b
mkUnfoldrM a -> m (Step a b)
step = (a -> m (Step a b)) -> (a -> m a) -> Unfold m a b
forall (m :: * -> *) a b s.
(s -> m (Step s b)) -> (a -> m s) -> Unfold m a b
Unfold a -> m (Step a b)
step a -> m a
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure
{-# INLINE unfoldrM #-}
unfoldrM :: Applicative m => (a -> m (Maybe (b, a))) -> Unfold m a b
unfoldrM :: forall (m :: * -> *) a b.
Applicative m =>
(a -> m (Maybe (b, a))) -> Unfold m a b
unfoldrM a -> m (Maybe (b, a))
next = (a -> m (Step a b)) -> (a -> m a) -> Unfold m a b
forall (m :: * -> *) a b s.
(s -> m (Step s b)) -> (a -> m s) -> Unfold m a b
Unfold a -> m (Step a b)
step a -> m a
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure
where
{-# INLINE_LATE step #-}
step :: a -> m (Step a b)
step a
st =
(\case
Just (b
x, a
s) -> b -> a -> Step a b
forall s a. a -> s -> Step s a
Yield b
x a
s
Maybe (b, a)
Nothing -> Step a b
forall s a. Step s a
Stop) (Maybe (b, a) -> Step a b) -> m (Maybe (b, a)) -> m (Step a b)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> a -> m (Maybe (b, a))
next a
st
{-# INLINE unfoldr #-}
unfoldr :: Applicative m => (a -> Maybe (b, a)) -> Unfold m a b
unfoldr :: forall (m :: * -> *) a b.
Applicative m =>
(a -> Maybe (b, a)) -> Unfold m a b
unfoldr a -> Maybe (b, a)
step = (a -> m (Maybe (b, a))) -> Unfold m a b
forall (m :: * -> *) a b.
Applicative m =>
(a -> m (Maybe (b, a))) -> Unfold m a b
unfoldrM (Maybe (b, a) -> m (Maybe (b, a))
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Maybe (b, a) -> m (Maybe (b, a)))
-> (a -> Maybe (b, a)) -> a -> m (Maybe (b, a))
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> Maybe (b, a)
step)
{-# INLINE_NORMAL lmap #-}
lmap :: (a -> c) -> Unfold m c b -> Unfold m a b
lmap :: forall a c (m :: * -> *) b.
(a -> c) -> Unfold m c b -> Unfold m a b
lmap a -> c
f (Unfold s -> m (Step s b)
ustep c -> m s
uinject) = (s -> m (Step s b)) -> (a -> m s) -> Unfold m a b
forall (m :: * -> *) a b s.
(s -> m (Step s b)) -> (a -> m s) -> Unfold m a b
Unfold s -> m (Step s b)
ustep (c -> m s
uinject (c -> m s) -> (a -> c) -> a -> m s
forall b c a. (b -> c) -> (a -> b) -> a -> c
Prelude.. a -> c
f)
{-# INLINE_NORMAL lmapM #-}
lmapM :: Monad m => (a -> m c) -> Unfold m c b -> Unfold m a b
lmapM :: forall (m :: * -> *) a c b.
Monad m =>
(a -> m c) -> Unfold m c b -> Unfold m a b
lmapM a -> m c
f (Unfold s -> m (Step s b)
ustep c -> m s
uinject) = (s -> m (Step s b)) -> (a -> m s) -> Unfold m a b
forall (m :: * -> *) a b s.
(s -> m (Step s b)) -> (a -> m s) -> Unfold m a b
Unfold s -> m (Step s b)
ustep (a -> m c
f (a -> m c) -> (c -> m s) -> a -> m s
forall (m :: * -> *) a b c.
Monad m =>
(a -> m b) -> (b -> m c) -> a -> m c
>=> c -> m s
uinject)
{-# INLINE_NORMAL both #-}
both :: a -> Unfold m a b -> Unfold m Void b
both :: forall a (m :: * -> *) b. a -> Unfold m a b -> Unfold m Void b
both a
a = (Void -> a) -> Unfold m a b -> Unfold m Void b
forall a c (m :: * -> *) b.
(a -> c) -> Unfold m c b -> Unfold m a b
lmap (a -> Void -> a
forall a b. a -> b -> a
Prelude.const a
a)
{-# INLINE_NORMAL first #-}
first :: a -> Unfold m (a, b) c -> Unfold m b c
first :: forall a (m :: * -> *) b c. a -> Unfold m (a, b) c -> Unfold m b c
first a
a = (b -> (a, b)) -> Unfold m (a, b) c -> Unfold m b c
forall a c (m :: * -> *) b.
(a -> c) -> Unfold m c b -> Unfold m a b
lmap (a
a, )
{-# INLINE_NORMAL second #-}
second :: b -> Unfold m (a, b) c -> Unfold m a c
second :: forall b (m :: * -> *) a c. b -> Unfold m (a, b) c -> Unfold m a c
second b
b = (a -> (a, b)) -> Unfold m (a, b) c -> Unfold m a c
forall a c (m :: * -> *) b.
(a -> c) -> Unfold m c b -> Unfold m a b
lmap (, b
b)
{-# INLINE_NORMAL takeWhileMWithInput #-}
takeWhileMWithInput :: Monad m =>
(a -> b -> m Bool) -> Unfold m a b -> Unfold m a b
takeWhileMWithInput :: forall (m :: * -> *) a b.
Monad m =>
(a -> b -> m Bool) -> Unfold m a b -> Unfold m a b
takeWhileMWithInput a -> b -> m Bool
f (Unfold s -> m (Step s b)
step1 a -> m s
inject1) = (Tuple' a s -> m (Step (Tuple' a s) b))
-> (a -> m (Tuple' a s)) -> Unfold m a b
forall (m :: * -> *) a b s.
(s -> m (Step s b)) -> (a -> m s) -> Unfold m a b
Unfold Tuple' a s -> m (Step (Tuple' a s) b)
step a -> m (Tuple' a s)
inject
where
inject :: a -> m (Tuple' a s)
inject a
a = do
s
s <- a -> m s
inject1 a
a
Tuple' a s -> m (Tuple' a s)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Tuple' a s -> m (Tuple' a s)) -> Tuple' a s -> m (Tuple' a s)
forall a b. (a -> b) -> a -> b
$ a -> s -> Tuple' a s
forall a b. a -> b -> Tuple' a b
Tuple' a
a s
s
{-# INLINE_LATE step #-}
step :: Tuple' a s -> m (Step (Tuple' a s) b)
step (Tuple' a
a s
st) = do
Step s b
r <- s -> m (Step s b)
step1 s
st
case Step s b
r of
Yield b
x s
s -> do
Bool
b <- a -> b -> m Bool
f a
a b
x
Step (Tuple' a s) b -> m (Step (Tuple' a s) b)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Step (Tuple' a s) b -> m (Step (Tuple' a s) b))
-> Step (Tuple' a s) b -> m (Step (Tuple' a s) b)
forall a b. (a -> b) -> a -> b
$ if Bool
b then b -> Tuple' a s -> Step (Tuple' a s) b
forall s a. a -> s -> Step s a
Yield b
x (a -> s -> Tuple' a s
forall a b. a -> b -> Tuple' a b
Tuple' a
a s
s) else Step (Tuple' a s) b
forall s a. Step s a
Stop
Skip s
s -> Step (Tuple' a s) b -> m (Step (Tuple' a s) b)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Step (Tuple' a s) b -> m (Step (Tuple' a s) b))
-> Step (Tuple' a s) b -> m (Step (Tuple' a s) b)
forall a b. (a -> b) -> a -> b
$ Tuple' a s -> Step (Tuple' a s) b
forall s a. s -> Step s a
Skip (a -> s -> Tuple' a s
forall a b. a -> b -> Tuple' a b
Tuple' a
a s
s)
Step s b
Stop -> Step (Tuple' a s) b -> m (Step (Tuple' a s) b)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return Step (Tuple' a s) b
forall s a. Step s a
Stop
{-# INLINE_NORMAL takeWhileM #-}
takeWhileM :: Monad m => (b -> m Bool) -> Unfold m a b -> Unfold m a b
takeWhileM :: forall (m :: * -> *) b a.
Monad m =>
(b -> m Bool) -> Unfold m a b -> Unfold m a b
takeWhileM b -> m Bool
f (Unfold s -> m (Step s b)
step1 a -> m s
inject1) = (s -> m (Step s b)) -> (a -> m s) -> Unfold m a b
forall (m :: * -> *) a b s.
(s -> m (Step s b)) -> (a -> m s) -> Unfold m a b
Unfold s -> m (Step s b)
step a -> m s
inject1
where
{-# INLINE_LATE step #-}
step :: s -> m (Step s b)
step s
st = do
Step s b
r <- s -> m (Step s b)
step1 s
st
case Step s b
r of
Yield b
x s
s -> do
Bool
b <- b -> m Bool
f b
x
Step s b -> m (Step s b)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Step s b -> m (Step s b)) -> Step s b -> m (Step s b)
forall a b. (a -> b) -> a -> b
$ if Bool
b then b -> s -> Step s b
forall s a. a -> s -> Step s a
Yield b
x s
s else Step s b
forall s a. Step s a
Stop
Skip s
s -> Step s b -> m (Step s b)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Step s b -> m (Step s b)) -> Step s b -> m (Step s b)
forall a b. (a -> b) -> a -> b
$ s -> Step s b
forall s a. s -> Step s a
Skip s
s
Step s b
Stop -> Step s b -> m (Step s b)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return Step s b
forall s a. Step s a
Stop
{-# INLINE takeWhile #-}
takeWhile :: Monad m => (b -> Bool) -> Unfold m a b -> Unfold m a b
takeWhile :: forall (m :: * -> *) b a.
Monad m =>
(b -> Bool) -> Unfold m a b -> Unfold m a b
takeWhile b -> Bool
f = (b -> m Bool) -> Unfold m a b -> Unfold m a b
forall (m :: * -> *) b a.
Monad m =>
(b -> m Bool) -> Unfold m a b -> Unfold m a b
takeWhileM (Bool -> m Bool
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Bool -> m Bool) -> (b -> Bool) -> b -> m Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. b -> Bool
f)
{-# INLINE_NORMAL mapM2 #-}
mapM2 :: Monad m => (a -> b -> m c) -> Unfold m a b -> Unfold m a c
mapM2 :: forall (m :: * -> *) a b c.
Monad m =>
(a -> b -> m c) -> Unfold m a b -> Unfold m a c
mapM2 a -> b -> m c
f (Unfold s -> m (Step s b)
ustep a -> m s
uinject) = ((a, s) -> m (Step (a, s) c)) -> (a -> m (a, s)) -> Unfold m a c
forall (m :: * -> *) a b s.
(s -> m (Step s b)) -> (a -> m s) -> Unfold m a b
Unfold (a, s) -> m (Step (a, s) c)
step a -> m (a, s)
inject
where
inject :: a -> m (a, s)
inject a
a = do
s
r <- a -> m s
uinject a
a
(a, s) -> m (a, s)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (a
a, s
r)
{-# INLINE_LATE step #-}
step :: (a, s) -> m (Step (a, s) c)
step (a
inp, s
st) = do
Step s b
r <- s -> m (Step s b)
ustep s
st
case Step s b
r of
Yield b
x s
s -> a -> b -> m c
f a
inp b
x m c -> (c -> m (Step (a, s) c)) -> m (Step (a, s) c)
forall a b. m a -> (a -> m b) -> m b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \c
a -> Step (a, s) c -> m (Step (a, s) c)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Step (a, s) c -> m (Step (a, s) c))
-> Step (a, s) c -> m (Step (a, s) c)
forall a b. (a -> b) -> a -> b
$ c -> (a, s) -> Step (a, s) c
forall s a. a -> s -> Step s a
Yield c
a (a
inp, s
s)
Skip s
s -> Step (a, s) c -> m (Step (a, s) c)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Step (a, s) c -> m (Step (a, s) c))
-> Step (a, s) c -> m (Step (a, s) c)
forall a b. (a -> b) -> a -> b
$ (a, s) -> Step (a, s) c
forall s a. s -> Step s a
Skip (a
inp, s
s)
Step s b
Stop -> Step (a, s) c -> m (Step (a, s) c)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return Step (a, s) c
forall s a. Step s a
Stop
{-# INLINE_NORMAL mapM #-}
mapM :: Monad m => (b -> m c) -> Unfold m a b -> Unfold m a c
mapM :: forall (m :: * -> *) b c a.
Monad m =>
(b -> m c) -> Unfold m a b -> Unfold m a c
mapM b -> m c
f (Unfold s -> m (Step s b)
ustep a -> m s
uinject) = (s -> m (Step s c)) -> (a -> m s) -> Unfold m a c
forall (m :: * -> *) a b s.
(s -> m (Step s b)) -> (a -> m s) -> Unfold m a b
Unfold s -> m (Step s c)
step a -> m s
uinject
where
{-# INLINE_LATE step #-}
step :: s -> m (Step s c)
step s
st = do
Step s b
r <- s -> m (Step s b)
ustep s
st
case Step s b
r of
Yield b
x s
s -> b -> m c
f b
x m c -> (c -> m (Step s c)) -> m (Step s c)
forall a b. m a -> (a -> m b) -> m b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \c
a -> Step s c -> m (Step s c)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Step s c -> m (Step s c)) -> Step s c -> m (Step s c)
forall a b. (a -> b) -> a -> b
$ c -> s -> Step s c
forall s a. a -> s -> Step s a
Yield c
a s
s
Skip s
s -> Step s c -> m (Step s c)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Step s c -> m (Step s c)) -> Step s c -> m (Step s c)
forall a b. (a -> b) -> a -> b
$ s -> Step s c
forall s a. s -> Step s a
Skip s
s
Step s b
Stop -> Step s c -> m (Step s c)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return Step s c
forall s a. Step s a
Stop
{-# INLINE_NORMAL map2 #-}
map2 :: Functor m => (a -> b -> c) -> Unfold m a b -> Unfold m a c
map2 :: forall (m :: * -> *) a b c.
Functor m =>
(a -> b -> c) -> Unfold m a b -> Unfold m a c
map2 a -> b -> c
f (Unfold s -> m (Step s b)
ustep a -> m s
uinject) = ((a, s) -> m (Step (a, s) c)) -> (a -> m (a, s)) -> Unfold m a c
forall (m :: * -> *) a b s.
(s -> m (Step s b)) -> (a -> m s) -> Unfold m a b
Unfold (a, s) -> m (Step (a, s) c)
step (\a
a -> (a
a,) (s -> (a, s)) -> m s -> m (a, s)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> a -> m s
uinject a
a)
where
func :: a -> Step b b -> Step (a, b) c
func a
a Step b b
r =
case Step b b
r of
Yield b
x b
s -> c -> (a, b) -> Step (a, b) c
forall s a. a -> s -> Step s a
Yield (a -> b -> c
f a
a b
x) (a
a, b
s)
Skip b
s -> (a, b) -> Step (a, b) c
forall s a. s -> Step s a
Skip (a
a, b
s)
Step b b
Stop -> Step (a, b) c
forall s a. Step s a
Stop
{-# INLINE_LATE step #-}
step :: (a, s) -> m (Step (a, s) c)
step (a
a, s
st) = (Step s b -> Step (a, s) c) -> m (Step s b) -> m (Step (a, s) c)
forall a b. (a -> b) -> m a -> m b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (a -> Step s b -> Step (a, s) c
forall {b}. a -> Step b b -> Step (a, b) c
func a
a) (s -> m (Step s b)
ustep s
st)
{-# INLINE_NORMAL map #-}
map :: Functor m => (b -> c) -> Unfold m a b -> Unfold m a c
map :: forall (m :: * -> *) b c a.
Functor m =>
(b -> c) -> Unfold m a b -> Unfold m a c
map b -> c
f (Unfold s -> m (Step s b)
ustep a -> m s
uinject) = (s -> m (Step s c)) -> (a -> m s) -> Unfold m a c
forall (m :: * -> *) a b s.
(s -> m (Step s b)) -> (a -> m s) -> Unfold m a b
Unfold s -> m (Step s c)
step a -> m s
uinject
where
{-# INLINE_LATE step #-}
step :: s -> m (Step s c)
step s
st = (Step s b -> Step s c) -> m (Step s b) -> m (Step s c)
forall a b. (a -> b) -> m a -> m b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap ((b -> c) -> Step s b -> Step s c
forall a b. (a -> b) -> Step s a -> Step s b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap b -> c
f) (s -> m (Step s b)
ustep s
st)
instance Functor m => Functor (Unfold m a) where
{-# INLINE fmap #-}
fmap :: forall a b. (a -> b) -> Unfold m a a -> Unfold m a b
fmap = (a -> b) -> Unfold m a a -> Unfold m a b
forall (m :: * -> *) b c a.
Functor m =>
(b -> c) -> Unfold m a b -> Unfold m a c
map
{-# INLINE fromEffect #-}
fromEffect :: Applicative m => m b -> Unfold m a b
fromEffect :: forall (m :: * -> *) b a. Applicative m => m b -> Unfold m a b
fromEffect m b
m = (Bool -> m (Step Bool b)) -> (a -> m Bool) -> Unfold m a b
forall (m :: * -> *) a b s.
(s -> m (Step s b)) -> (a -> m s) -> Unfold m a b
Unfold Bool -> m (Step Bool b)
step a -> m Bool
forall {f :: * -> *} {p}. Applicative f => p -> f Bool
inject
where
inject :: p -> f Bool
inject p
_ = Bool -> f Bool
forall a. a -> f a
forall (f :: * -> *) a. Applicative f => a -> f a
pure Bool
False
step :: Bool -> m (Step Bool b)
step Bool
False = (b -> Bool -> Step Bool b
forall s a. a -> s -> Step s a
`Yield` Bool
True) (b -> Step Bool b) -> m b -> m (Step Bool b)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> m b
m
step Bool
True = Step Bool b -> m (Step Bool b)
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure Step Bool b
forall s a. Step s a
Stop
fromPure :: Applicative m => b -> Unfold m a b
fromPure :: forall (m :: * -> *) b a. Applicative m => b -> Unfold m a b
fromPure = m b -> Unfold m a b
forall (m :: * -> *) b a. Applicative m => m b -> Unfold m a b
fromEffect (m b -> Unfold m a b) -> (b -> m b) -> b -> Unfold m a b
forall b c a. (b -> c) -> (a -> b) -> a -> c
Prelude.. b -> m b
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure
{-# INLINE_LATE fromList #-}
fromList :: Applicative m => Unfold m [a] a
fromList :: forall (m :: * -> *) a. Applicative m => Unfold m [a] a
fromList = ([a] -> m (Step [a] a)) -> ([a] -> m [a]) -> Unfold m [a] a
forall (m :: * -> *) a b s.
(s -> m (Step s b)) -> (a -> m s) -> Unfold m a b
Unfold [a] -> m (Step [a] a)
forall {f :: * -> *} {a}. Applicative f => [a] -> f (Step [a] a)
step [a] -> m [a]
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure
where
{-# INLINE_LATE step #-}
step :: [a] -> f (Step [a] a)
step (a
x:[a]
xs) = Step [a] a -> f (Step [a] a)
forall a. a -> f a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Step [a] a -> f (Step [a] a)) -> Step [a] a -> f (Step [a] a)
forall a b. (a -> b) -> a -> b
$ a -> [a] -> Step [a] a
forall s a. a -> s -> Step s a
Yield a
x [a]
xs
step [] = Step [a] a -> f (Step [a] a)
forall a. a -> f a
forall (f :: * -> *) a. Applicative f => a -> f a
pure Step [a] a
forall s a. Step s a
Stop
{-# INLINE_NORMAL crossApplySnd #-}
crossApplySnd ::
Unfold m a b -> Unfold m a c -> Unfold m a c
crossApplySnd :: forall (m :: * -> *) a b c.
Unfold m a b -> Unfold m a c -> Unfold m a c
crossApplySnd (Unfold s -> m (Step s b)
_step1 a -> m s
_inject1) (Unfold s -> m (Step s c)
_step2 a -> m s
_inject2) = Unfold m a c
forall a. HasCallStack => a
undefined
{-# INLINE_NORMAL crossApplyFst #-}
crossApplyFst ::
Unfold m a b -> Unfold m a c -> Unfold m a b
crossApplyFst :: forall (m :: * -> *) a b c.
Unfold m a b -> Unfold m a c -> Unfold m a b
crossApplyFst (Unfold s -> m (Step s b)
_step1 a -> m s
_inject1) (Unfold s -> m (Step s c)
_step2 a -> m s
_inject2) = Unfold m a b
forall a. HasCallStack => a
undefined
{-# ANN type Many2State Fuse #-}
data Many2State x s1 s2 = Many2Outer x s1 | Many2Inner x s1 s2
{-# INLINE_NORMAL many2 #-}
many2 :: Monad m => Unfold m (a, b) c -> Unfold m a b -> Unfold m a c
many2 :: forall (m :: * -> *) a b c.
Monad m =>
Unfold m (a, b) c -> Unfold m a b -> Unfold m a c
many2 (Unfold s -> m (Step s c)
step2 (a, b) -> m s
inject2) (Unfold s -> m (Step s b)
step1 a -> m s
inject1) = (Many2State a s s -> m (Step (Many2State a s s) c))
-> (a -> m (Many2State a s s)) -> Unfold m a c
forall (m :: * -> *) a b s.
(s -> m (Step s b)) -> (a -> m s) -> Unfold m a b
Unfold Many2State a s s -> m (Step (Many2State a s s) c)
step a -> m (Many2State a s s)
forall {s2}. a -> m (Many2State a s s2)
inject
where
inject :: a -> m (Many2State a s s2)
inject a
a = do
s
s <- a -> m s
inject1 a
a
Many2State a s s2 -> m (Many2State a s s2)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Many2State a s s2 -> m (Many2State a s s2))
-> Many2State a s s2 -> m (Many2State a s s2)
forall a b. (a -> b) -> a -> b
$ a -> s -> Many2State a s s2
forall x s1 s2. x -> s1 -> Many2State x s1 s2
Many2Outer a
a s
s
{-# INLINE_LATE step #-}
step :: Many2State a s s -> m (Step (Many2State a s s) c)
step (Many2Outer a
a s
st) = do
Step s b
r <- s -> m (Step s b)
step1 s
st
case Step s b
r of
Yield b
b s
s -> do
s
innerSt <- (a, b) -> m s
inject2 (a
a, b
b)
Step (Many2State a s s) c -> m (Step (Many2State a s s) c)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Step (Many2State a s s) c -> m (Step (Many2State a s s) c))
-> Step (Many2State a s s) c -> m (Step (Many2State a s s) c)
forall a b. (a -> b) -> a -> b
$ Many2State a s s -> Step (Many2State a s s) c
forall s a. s -> Step s a
Skip (a -> s -> s -> Many2State a s s
forall x s1 s2. x -> s1 -> s2 -> Many2State x s1 s2
Many2Inner a
a s
s s
innerSt)
Skip s
s -> Step (Many2State a s s) c -> m (Step (Many2State a s s) c)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Step (Many2State a s s) c -> m (Step (Many2State a s s) c))
-> Step (Many2State a s s) c -> m (Step (Many2State a s s) c)
forall a b. (a -> b) -> a -> b
$ Many2State a s s -> Step (Many2State a s s) c
forall s a. s -> Step s a
Skip (a -> s -> Many2State a s s
forall x s1 s2. x -> s1 -> Many2State x s1 s2
Many2Outer a
a s
s)
Step s b
Stop -> Step (Many2State a s s) c -> m (Step (Many2State a s s) c)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return Step (Many2State a s s) c
forall s a. Step s a
Stop
step (Many2Inner a
a s
ost s
ist) = do
Step s c
r <- s -> m (Step s c)
step2 s
ist
Step (Many2State a s s) c -> m (Step (Many2State a s s) c)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Step (Many2State a s s) c -> m (Step (Many2State a s s) c))
-> Step (Many2State a s s) c -> m (Step (Many2State a s s) c)
forall a b. (a -> b) -> a -> b
$ case Step s c
r of
Yield c
x s
s -> c -> Many2State a s s -> Step (Many2State a s s) c
forall s a. a -> s -> Step s a
Yield c
x (a -> s -> s -> Many2State a s s
forall x s1 s2. x -> s1 -> s2 -> Many2State x s1 s2
Many2Inner a
a s
ost s
s)
Skip s
s -> Many2State a s s -> Step (Many2State a s s) c
forall s a. s -> Step s a
Skip (a -> s -> s -> Many2State a s s
forall x s1 s2. x -> s1 -> s2 -> Many2State x s1 s2
Many2Inner a
a s
ost s
s)
Step s c
Stop -> Many2State a s s -> Step (Many2State a s s) c
forall s a. s -> Step s a
Skip (a -> s -> Many2State a s s
forall x s1 s2. x -> s1 -> Many2State x s1 s2
Many2Outer a
a s
ost)
data Cross a s1 b s2 = CrossOuter a s1 | CrossInner a s1 b s2
{-# INLINE_NORMAL crossWithM #-}
crossWithM :: Monad m =>
(b -> c -> m d) -> Unfold m a b -> Unfold m a c -> Unfold m a d
crossWithM :: forall (m :: * -> *) b c d a.
Monad m =>
(b -> c -> m d) -> Unfold m a b -> Unfold m a c -> Unfold m a d
crossWithM b -> c -> m d
f (Unfold s -> m (Step s b)
step1 a -> m s
inject1) (Unfold s -> m (Step s c)
step2 a -> m s
inject2) = (Cross a s b s -> m (Step (Cross a s b s) d))
-> (a -> m (Cross a s b s)) -> Unfold m a d
forall (m :: * -> *) a b s.
(s -> m (Step s b)) -> (a -> m s) -> Unfold m a b
Unfold Cross a s b s -> m (Step (Cross a s b s) d)
step a -> m (Cross a s b s)
forall {b} {s2}. a -> m (Cross a s b s2)
inject
where
inject :: a -> m (Cross a s b s2)
inject a
a = do
s
s1 <- a -> m s
inject1 a
a
Cross a s b s2 -> m (Cross a s b s2)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Cross a s b s2 -> m (Cross a s b s2))
-> Cross a s b s2 -> m (Cross a s b s2)
forall a b. (a -> b) -> a -> b
$ a -> s -> Cross a s b s2
forall a s1 b s2. a -> s1 -> Cross a s1 b s2
CrossOuter a
a s
s1
{-# INLINE_LATE step #-}
step :: Cross a s b s -> m (Step (Cross a s b s) d)
step (CrossOuter a
a s
s1) = do
Step s b
r <- s -> m (Step s b)
step1 s
s1
case Step s b
r of
Yield b
b s
s -> do
s
s2 <- a -> m s
inject2 a
a
Step (Cross a s b s) d -> m (Step (Cross a s b s) d)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Step (Cross a s b s) d -> m (Step (Cross a s b s) d))
-> Step (Cross a s b s) d -> m (Step (Cross a s b s) d)
forall a b. (a -> b) -> a -> b
$ Cross a s b s -> Step (Cross a s b s) d
forall s a. s -> Step s a
Skip (a -> s -> b -> s -> Cross a s b s
forall a s1 b s2. a -> s1 -> b -> s2 -> Cross a s1 b s2
CrossInner a
a s
s b
b s
s2)
Skip s
s -> Step (Cross a s b s) d -> m (Step (Cross a s b s) d)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Step (Cross a s b s) d -> m (Step (Cross a s b s) d))
-> Step (Cross a s b s) d -> m (Step (Cross a s b s) d)
forall a b. (a -> b) -> a -> b
$ Cross a s b s -> Step (Cross a s b s) d
forall s a. s -> Step s a
Skip (a -> s -> Cross a s b s
forall a s1 b s2. a -> s1 -> Cross a s1 b s2
CrossOuter a
a s
s)
Step s b
Stop -> Step (Cross a s b s) d -> m (Step (Cross a s b s) d)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return Step (Cross a s b s) d
forall s a. Step s a
Stop
step (CrossInner a
a s
s1 b
b s
s2) = do
Step s c
r <- s -> m (Step s c)
step2 s
s2
case Step s c
r of
Yield c
c s
s -> b -> c -> m d
f b
b c
c m d
-> (d -> m (Step (Cross a s b s) d)) -> m (Step (Cross a s b s) d)
forall a b. m a -> (a -> m b) -> m b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \d
d -> Step (Cross a s b s) d -> m (Step (Cross a s b s) d)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Step (Cross a s b s) d -> m (Step (Cross a s b s) d))
-> Step (Cross a s b s) d -> m (Step (Cross a s b s) d)
forall a b. (a -> b) -> a -> b
$ d -> Cross a s b s -> Step (Cross a s b s) d
forall s a. a -> s -> Step s a
Yield d
d (a -> s -> b -> s -> Cross a s b s
forall a s1 b s2. a -> s1 -> b -> s2 -> Cross a s1 b s2
CrossInner a
a s
s1 b
b s
s)
Skip s
s -> Step (Cross a s b s) d -> m (Step (Cross a s b s) d)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Step (Cross a s b s) d -> m (Step (Cross a s b s) d))
-> Step (Cross a s b s) d -> m (Step (Cross a s b s) d)
forall a b. (a -> b) -> a -> b
$ Cross a s b s -> Step (Cross a s b s) d
forall s a. s -> Step s a
Skip (a -> s -> b -> s -> Cross a s b s
forall a s1 b s2. a -> s1 -> b -> s2 -> Cross a s1 b s2
CrossInner a
a s
s1 b
b s
s)
Step s c
Stop -> Step (Cross a s b s) d -> m (Step (Cross a s b s) d)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Step (Cross a s b s) d -> m (Step (Cross a s b s) d))
-> Step (Cross a s b s) d -> m (Step (Cross a s b s) d)
forall a b. (a -> b) -> a -> b
$ Cross a s b s -> Step (Cross a s b s) d
forall s a. s -> Step s a
Skip (a -> s -> Cross a s b s
forall a s1 b s2. a -> s1 -> Cross a s1 b s2
CrossOuter a
a s
s1)
{-# INLINE crossWith #-}
crossWith :: Monad m =>
(b -> c -> d) -> Unfold m a b -> Unfold m a c -> Unfold m a d
crossWith :: forall (m :: * -> *) b c d a.
Monad m =>
(b -> c -> d) -> Unfold m a b -> Unfold m a c -> Unfold m a d
crossWith b -> c -> d
f = (b -> c -> m d) -> Unfold m a b -> Unfold m a c -> Unfold m a d
forall (m :: * -> *) b c d a.
Monad m =>
(b -> c -> m d) -> Unfold m a b -> Unfold m a c -> Unfold m a d
crossWithM (\b
b c
c -> d -> m d
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (d -> m d) -> d -> m d
forall a b. (a -> b) -> a -> b
$ b -> c -> d
f b
b c
c)
{-# INLINE_NORMAL cross #-}
cross :: Monad m => Unfold m a b -> Unfold m a c -> Unfold m a (b, c)
cross :: forall (m :: * -> *) a b c.
Monad m =>
Unfold m a b -> Unfold m a c -> Unfold m a (b, c)
cross = (b -> c -> (b, c))
-> Unfold m a b -> Unfold m a c -> Unfold m a (b, c)
forall (m :: * -> *) b c d a.
Monad m =>
(b -> c -> d) -> Unfold m a b -> Unfold m a c -> Unfold m a d
crossWith (,)
crossApply :: Monad m => Unfold m a (b -> c) -> Unfold m a b -> Unfold m a c
crossApply :: forall (m :: * -> *) a b c.
Monad m =>
Unfold m a (b -> c) -> Unfold m a b -> Unfold m a c
crossApply Unfold m a (b -> c)
u1 Unfold m a b
u2 = ((b -> c, b) -> c) -> Unfold m a (b -> c, b) -> Unfold m a c
forall a b. (a -> b) -> Unfold m a a -> Unfold m a b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (\(b -> c
a, b
b) -> b -> c
a b
b) (Unfold m a (b -> c) -> Unfold m a b -> Unfold m a (b -> c, b)
forall (m :: * -> *) a b c.
Monad m =>
Unfold m a b -> Unfold m a c -> Unfold m a (b, c)
cross Unfold m a (b -> c)
u1 Unfold m a b
u2)
data ConcatMapState m b s1 x =
ConcatMapOuter x s1
| forall s2. ConcatMapInner x s1 s2 (s2 -> m (Step s2 b))
{-# INLINE_NORMAL concatMapM #-}
concatMapM :: Monad m
=> (b -> m (Unfold m a c)) -> Unfold m a b -> Unfold m a c
concatMapM :: forall (m :: * -> *) b a c.
Monad m =>
(b -> m (Unfold m a c)) -> Unfold m a b -> Unfold m a c
concatMapM b -> m (Unfold m a c)
f (Unfold s -> m (Step s b)
step1 a -> m s
inject1) = (ConcatMapState m c s a -> m (Step (ConcatMapState m c s a) c))
-> (a -> m (ConcatMapState m c s a)) -> Unfold m a c
forall (m :: * -> *) a b s.
(s -> m (Step s b)) -> (a -> m s) -> Unfold m a b
Unfold ConcatMapState m c s a -> m (Step (ConcatMapState m c s a) c)
step a -> m (ConcatMapState m c s a)
forall {m :: * -> *} {b}. a -> m (ConcatMapState m b s a)
inject
where
inject :: a -> m (ConcatMapState m b s a)
inject a
x = do
s
s <- a -> m s
inject1 a
x
ConcatMapState m b s a -> m (ConcatMapState m b s a)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (ConcatMapState m b s a -> m (ConcatMapState m b s a))
-> ConcatMapState m b s a -> m (ConcatMapState m b s a)
forall a b. (a -> b) -> a -> b
$ a -> s -> ConcatMapState m b s a
forall (m :: * -> *) b s1 x. x -> s1 -> ConcatMapState m b s1 x
ConcatMapOuter a
x s
s
{-# INLINE_LATE step #-}
step :: ConcatMapState m c s a -> m (Step (ConcatMapState m c s a) c)
step (ConcatMapOuter a
seed s
st) = do
Step s b
r <- s -> m (Step s b)
step1 s
st
case Step s b
r of
Yield b
x s
s -> do
Unfold s -> m (Step s c)
step2 a -> m s
inject2 <- b -> m (Unfold m a c)
f b
x
s
innerSt <- a -> m s
inject2 a
seed
Step (ConcatMapState m c s a) c
-> m (Step (ConcatMapState m c s a) c)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Step (ConcatMapState m c s a) c
-> m (Step (ConcatMapState m c s a) c))
-> Step (ConcatMapState m c s a) c
-> m (Step (ConcatMapState m c s a) c)
forall a b. (a -> b) -> a -> b
$ ConcatMapState m c s a -> Step (ConcatMapState m c s a) c
forall s a. s -> Step s a
Skip (a -> s -> s -> (s -> m (Step s c)) -> ConcatMapState m c s a
forall (m :: * -> *) b s1 x s2.
x -> s1 -> s2 -> (s2 -> m (Step s2 b)) -> ConcatMapState m b s1 x
ConcatMapInner a
seed s
s s
innerSt s -> m (Step s c)
step2)
Skip s
s -> Step (ConcatMapState m c s a) c
-> m (Step (ConcatMapState m c s a) c)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Step (ConcatMapState m c s a) c
-> m (Step (ConcatMapState m c s a) c))
-> Step (ConcatMapState m c s a) c
-> m (Step (ConcatMapState m c s a) c)
forall a b. (a -> b) -> a -> b
$ ConcatMapState m c s a -> Step (ConcatMapState m c s a) c
forall s a. s -> Step s a
Skip (a -> s -> ConcatMapState m c s a
forall (m :: * -> *) b s1 x. x -> s1 -> ConcatMapState m b s1 x
ConcatMapOuter a
seed s
s)
Step s b
Stop -> Step (ConcatMapState m c s a) c
-> m (Step (ConcatMapState m c s a) c)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return Step (ConcatMapState m c s a) c
forall s a. Step s a
Stop
step (ConcatMapInner a
seed s
ost s2
ist s2 -> m (Step s2 c)
istep) = do
Step s2 c
r <- s2 -> m (Step s2 c)
istep s2
ist
Step (ConcatMapState m c s a) c
-> m (Step (ConcatMapState m c s a) c)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Step (ConcatMapState m c s a) c
-> m (Step (ConcatMapState m c s a) c))
-> Step (ConcatMapState m c s a) c
-> m (Step (ConcatMapState m c s a) c)
forall a b. (a -> b) -> a -> b
$ case Step s2 c
r of
Yield c
x s2
s -> c -> ConcatMapState m c s a -> Step (ConcatMapState m c s a) c
forall s a. a -> s -> Step s a
Yield c
x (a -> s -> s2 -> (s2 -> m (Step s2 c)) -> ConcatMapState m c s a
forall (m :: * -> *) b s1 x s2.
x -> s1 -> s2 -> (s2 -> m (Step s2 b)) -> ConcatMapState m b s1 x
ConcatMapInner a
seed s
ost s2
s s2 -> m (Step s2 c)
istep)
Skip s2
s -> ConcatMapState m c s a -> Step (ConcatMapState m c s a) c
forall s a. s -> Step s a
Skip (a -> s -> s2 -> (s2 -> m (Step s2 c)) -> ConcatMapState m c s a
forall (m :: * -> *) b s1 x s2.
x -> s1 -> s2 -> (s2 -> m (Step s2 b)) -> ConcatMapState m b s1 x
ConcatMapInner a
seed s
ost s2
s s2 -> m (Step s2 c)
istep)
Step s2 c
Stop -> ConcatMapState m c s a -> Step (ConcatMapState m c s a) c
forall s a. s -> Step s a
Skip (a -> s -> ConcatMapState m c s a
forall (m :: * -> *) b s1 x. x -> s1 -> ConcatMapState m b s1 x
ConcatMapOuter a
seed s
ost)
{-# INLINE concatMap #-}
concatMap :: Monad m => (b -> Unfold m a c) -> Unfold m a b -> Unfold m a c
concatMap :: forall (m :: * -> *) b a c.
Monad m =>
(b -> Unfold m a c) -> Unfold m a b -> Unfold m a c
concatMap b -> Unfold m a c
f = (b -> m (Unfold m a c)) -> Unfold m a b -> Unfold m a c
forall (m :: * -> *) b a c.
Monad m =>
(b -> m (Unfold m a c)) -> Unfold m a b -> Unfold m a c
concatMapM (Unfold m a c -> m (Unfold m a c)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Unfold m a c -> m (Unfold m a c))
-> (b -> Unfold m a c) -> b -> m (Unfold m a c)
forall b c a. (b -> c) -> (a -> b) -> a -> c
Prelude.. b -> Unfold m a c
f)
infixl 1 `bind`
{-# INLINE bind #-}
bind :: Monad m => Unfold m a b -> (b -> Unfold m a c) -> Unfold m a c
bind :: forall (m :: * -> *) a b c.
Monad m =>
Unfold m a b -> (b -> Unfold m a c) -> Unfold m a c
bind = ((b -> Unfold m a c) -> Unfold m a b -> Unfold m a c)
-> Unfold m a b -> (b -> Unfold m a c) -> Unfold m a c
forall a b c. (a -> b -> c) -> b -> a -> c
flip (b -> Unfold m a c) -> Unfold m a b -> Unfold m a c
forall (m :: * -> *) b a c.
Monad m =>
(b -> Unfold m a c) -> Unfold m a b -> Unfold m a c
concatMap
{-# INLINE functionM #-}
functionM :: Applicative m => (a -> m b) -> Unfold m a b
functionM :: forall (m :: * -> *) a b.
Applicative m =>
(a -> m b) -> Unfold m a b
functionM a -> m b
f = (Maybe a -> m (Step (Maybe a) b))
-> (a -> m (Maybe a)) -> Unfold m a b
forall (m :: * -> *) a b s.
(s -> m (Step s b)) -> (a -> m s) -> Unfold m a b
Unfold Maybe a -> m (Step (Maybe a) b)
forall {a}. Maybe a -> m (Step (Maybe a) b)
step a -> m (Maybe a)
forall {f :: * -> *} {a}. Applicative f => a -> f (Maybe a)
inject
where
inject :: a -> f (Maybe a)
inject a
x = Maybe a -> f (Maybe a)
forall a. a -> f a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Maybe a -> f (Maybe a)) -> Maybe a -> f (Maybe a)
forall a b. (a -> b) -> a -> b
$ a -> Maybe a
forall a. a -> Maybe a
Just a
x
{-# INLINE_LATE step #-}
step :: Maybe a -> m (Step (Maybe a) b)
step (Just a
x) = (b -> Maybe a -> Step (Maybe a) b
forall s a. a -> s -> Step s a
`Yield` Maybe a
forall a. Maybe a
Nothing) (b -> Step (Maybe a) b) -> m b -> m (Step (Maybe a) b)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> a -> m b
f a
x
step Maybe a
Nothing = Step (Maybe a) b -> m (Step (Maybe a) b)
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure Step (Maybe a) b
forall s a. Step s a
Stop
{-# INLINE function #-}
function :: Applicative m => (a -> b) -> Unfold m a b
function :: forall (m :: * -> *) a b. Applicative m => (a -> b) -> Unfold m a b
function a -> b
f = (a -> m b) -> Unfold m a b
forall (m :: * -> *) a b.
Applicative m =>
(a -> m b) -> Unfold m a b
functionM ((a -> m b) -> Unfold m a b) -> (a -> m b) -> Unfold m a b
forall a b. (a -> b) -> a -> b
$ b -> m b
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (b -> m b) -> (a -> b) -> a -> m b
forall b c a. (b -> c) -> (a -> b) -> a -> c
Prelude.. a -> b
f
{-# INLINE identity #-}
identity :: Applicative m => Unfold m a a
identity :: forall (m :: * -> *) a. Applicative m => Unfold m a a
identity = (a -> a) -> Unfold m a a
forall (m :: * -> *) a b. Applicative m => (a -> b) -> Unfold m a b
function a -> a
forall a. a -> a
Prelude.id
{-# ANN type ConcatState Fuse #-}
data ConcatState s1 s2 = ConcatOuter s1 | ConcatInner s1 s2
{-# INLINE_NORMAL many #-}
many :: Monad m => Unfold m b c -> Unfold m a b -> Unfold m a c
many :: forall (m :: * -> *) b c a.
Monad m =>
Unfold m b c -> Unfold m a b -> Unfold m a c
many (Unfold s -> m (Step s c)
step2 b -> m s
inject2) (Unfold s -> m (Step s b)
step1 a -> m s
inject1) = (ConcatState s s -> m (Step (ConcatState s s) c))
-> (a -> m (ConcatState s s)) -> Unfold m a c
forall (m :: * -> *) a b s.
(s -> m (Step s b)) -> (a -> m s) -> Unfold m a b
Unfold ConcatState s s -> m (Step (ConcatState s s) c)
step a -> m (ConcatState s s)
forall {s2}. a -> m (ConcatState s s2)
inject
where
inject :: a -> m (ConcatState s s2)
inject a
x = do
s
s <- a -> m s
inject1 a
x
ConcatState s s2 -> m (ConcatState s s2)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (ConcatState s s2 -> m (ConcatState s s2))
-> ConcatState s s2 -> m (ConcatState s s2)
forall a b. (a -> b) -> a -> b
$ s -> ConcatState s s2
forall s1 s2. s1 -> ConcatState s1 s2
ConcatOuter s
s
{-# INLINE_LATE step #-}
step :: ConcatState s s -> m (Step (ConcatState s s) c)
step (ConcatOuter s
st) = do
Step s b
r <- s -> m (Step s b)
step1 s
st
case Step s b
r of
Yield b
x s
s -> do
s
innerSt <- b -> m s
inject2 b
x
Step (ConcatState s s) c -> m (Step (ConcatState s s) c)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Step (ConcatState s s) c -> m (Step (ConcatState s s) c))
-> Step (ConcatState s s) c -> m (Step (ConcatState s s) c)
forall a b. (a -> b) -> a -> b
$ ConcatState s s -> Step (ConcatState s s) c
forall s a. s -> Step s a
Skip (s -> s -> ConcatState s s
forall s1 s2. s1 -> s2 -> ConcatState s1 s2
ConcatInner s
s s
innerSt)
Skip s
s -> Step (ConcatState s s) c -> m (Step (ConcatState s s) c)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Step (ConcatState s s) c -> m (Step (ConcatState s s) c))
-> Step (ConcatState s s) c -> m (Step (ConcatState s s) c)
forall a b. (a -> b) -> a -> b
$ ConcatState s s -> Step (ConcatState s s) c
forall s a. s -> Step s a
Skip (s -> ConcatState s s
forall s1 s2. s1 -> ConcatState s1 s2
ConcatOuter s
s)
Step s b
Stop -> Step (ConcatState s s) c -> m (Step (ConcatState s s) c)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return Step (ConcatState s s) c
forall s a. Step s a
Stop
step (ConcatInner s
ost s
ist) = do
Step s c
r <- s -> m (Step s c)
step2 s
ist
Step (ConcatState s s) c -> m (Step (ConcatState s s) c)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Step (ConcatState s s) c -> m (Step (ConcatState s s) c))
-> Step (ConcatState s s) c -> m (Step (ConcatState s s) c)
forall a b. (a -> b) -> a -> b
$ case Step s c
r of
Yield c
x s
s -> c -> ConcatState s s -> Step (ConcatState s s) c
forall s a. a -> s -> Step s a
Yield c
x (s -> s -> ConcatState s s
forall s1 s2. s1 -> s2 -> ConcatState s1 s2
ConcatInner s
ost s
s)
Skip s
s -> ConcatState s s -> Step (ConcatState s s) c
forall s a. s -> Step s a
Skip (s -> s -> ConcatState s s
forall s1 s2. s1 -> s2 -> ConcatState s1 s2
ConcatInner s
ost s
s)
Step s c
Stop -> ConcatState s s -> Step (ConcatState s s) c
forall s a. s -> Step s a
Skip (s -> ConcatState s s
forall s1 s2. s1 -> ConcatState s1 s2
ConcatOuter s
ost)
{-# INLINE_NORMAL zipWithM #-}
zipWithM :: Monad m
=> (b -> c -> m d) -> Unfold m a b -> Unfold m a c -> Unfold m a d
zipWithM :: forall (m :: * -> *) b c d a.
Monad m =>
(b -> c -> m d) -> Unfold m a b -> Unfold m a c -> Unfold m a d
zipWithM b -> c -> m d
f (Unfold s -> m (Step s b)
step1 a -> m s
inject1) (Unfold s -> m (Step s c)
step2 a -> m s
inject2) = ((s, s, Maybe b) -> m (Step (s, s, Maybe b) d))
-> (a -> m (s, s, Maybe b)) -> Unfold m a d
forall (m :: * -> *) a b s.
(s -> m (Step s b)) -> (a -> m s) -> Unfold m a b
Unfold (s, s, Maybe b) -> m (Step (s, s, Maybe b) d)
step a -> m (s, s, Maybe b)
forall {a}. a -> m (s, s, Maybe a)
inject
where
inject :: a -> m (s, s, Maybe a)
inject a
x = do
s
s1 <- a -> m s
inject1 a
x
s
s2 <- a -> m s
inject2 a
x
(s, s, Maybe a) -> m (s, s, Maybe a)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (s
s1, s
s2, Maybe a
forall a. Maybe a
Nothing)
{-# INLINE_LATE step #-}
step :: (s, s, Maybe b) -> m (Step (s, s, Maybe b) d)
step (s
s1, s
s2, Maybe b
Nothing) = do
Step s b
r <- s -> m (Step s b)
step1 s
s1
Step (s, s, Maybe b) d -> m (Step (s, s, Maybe b) d)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Step (s, s, Maybe b) d -> m (Step (s, s, Maybe b) d))
-> Step (s, s, Maybe b) d -> m (Step (s, s, Maybe b) d)
forall a b. (a -> b) -> a -> b
$
case Step s b
r of
Yield b
x s
s -> (s, s, Maybe b) -> Step (s, s, Maybe b) d
forall s a. s -> Step s a
Skip (s
s, s
s2, b -> Maybe b
forall a. a -> Maybe a
Just b
x)
Skip s
s -> (s, s, Maybe b) -> Step (s, s, Maybe b) d
forall s a. s -> Step s a
Skip (s
s, s
s2, Maybe b
forall a. Maybe a
Nothing)
Step s b
Stop -> Step (s, s, Maybe b) d
forall s a. Step s a
Stop
step (s
s1, s
s2, Just b
x) = do
Step s c
r <- s -> m (Step s c)
step2 s
s2
case Step s c
r of
Yield c
y s
s -> do
d
z <- b -> c -> m d
f b
x c
y
Step (s, s, Maybe b) d -> m (Step (s, s, Maybe b) d)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Step (s, s, Maybe b) d -> m (Step (s, s, Maybe b) d))
-> Step (s, s, Maybe b) d -> m (Step (s, s, Maybe b) d)
forall a b. (a -> b) -> a -> b
$ d -> (s, s, Maybe b) -> Step (s, s, Maybe b) d
forall s a. a -> s -> Step s a
Yield d
z (s
s1, s
s, Maybe b
forall a. Maybe a
Nothing)
Skip s
s -> Step (s, s, Maybe b) d -> m (Step (s, s, Maybe b) d)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Step (s, s, Maybe b) d -> m (Step (s, s, Maybe b) d))
-> Step (s, s, Maybe b) d -> m (Step (s, s, Maybe b) d)
forall a b. (a -> b) -> a -> b
$ (s, s, Maybe b) -> Step (s, s, Maybe b) d
forall s a. s -> Step s a
Skip (s
s1, s
s, b -> Maybe b
forall a. a -> Maybe a
Just b
x)
Step s c
Stop -> Step (s, s, Maybe b) d -> m (Step (s, s, Maybe b) d)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return Step (s, s, Maybe b) d
forall s a. Step s a
Stop
{-# INLINE zipWith #-}
zipWith :: Monad m
=> (b -> c -> d) -> Unfold m a b -> Unfold m a c -> Unfold m a d
zipWith :: forall (m :: * -> *) b c d a.
Monad m =>
(b -> c -> d) -> Unfold m a b -> Unfold m a c -> Unfold m a d
zipWith b -> c -> d
f = (b -> c -> m d) -> Unfold m a b -> Unfold m a c -> Unfold m a d
forall (m :: * -> *) b c d a.
Monad m =>
(b -> c -> m d) -> Unfold m a b -> Unfold m a c -> Unfold m a d
zipWithM (\b
a c
b -> d -> m d
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (b -> c -> d
f b
a c
b))
data ManyInterleaveState o i =
ManyInterleaveOuter o [i]
| ManyInterleaveInner o [i]
| ManyInterleaveInnerL [i] [i]
| ManyInterleaveInnerR [i] [i]
{-# INLINE_NORMAL manyInterleave #-}
manyInterleave :: Monad m => Unfold m a b -> Unfold m c a -> Unfold m c b
manyInterleave :: forall (m :: * -> *) b c a.
Monad m =>
Unfold m b c -> Unfold m a b -> Unfold m a c
manyInterleave (Unfold s -> m (Step s b)
istep a -> m s
iinject) (Unfold s -> m (Step s a)
ostep c -> m s
oinject) =
(ManyInterleaveState s s -> m (Step (ManyInterleaveState s s) b))
-> (c -> m (ManyInterleaveState s s)) -> Unfold m c b
forall (m :: * -> *) a b s.
(s -> m (Step s b)) -> (a -> m s) -> Unfold m a b
Unfold ManyInterleaveState s s -> m (Step (ManyInterleaveState s s) b)
step c -> m (ManyInterleaveState s s)
forall {i}. c -> m (ManyInterleaveState s i)
inject
where
inject :: c -> m (ManyInterleaveState s i)
inject c
x = do
s
ost <- c -> m s
oinject c
x
ManyInterleaveState s i -> m (ManyInterleaveState s i)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (s -> [i] -> ManyInterleaveState s i
forall o i. o -> [i] -> ManyInterleaveState o i
ManyInterleaveOuter s
ost [])
{-# INLINE_LATE step #-}
step :: ManyInterleaveState s s -> m (Step (ManyInterleaveState s s) b)
step (ManyInterleaveOuter s
o [s]
ls) = do
Step s a
r <- s -> m (Step s a)
ostep s
o
case Step s a
r of
Yield a
a s
o' -> do
s
i <- a -> m s
iinject a
a
s
i s
-> m (Step (ManyInterleaveState s s) b)
-> m (Step (ManyInterleaveState s s) b)
forall a b. a -> b -> b
`seq` Step (ManyInterleaveState s s) b
-> m (Step (ManyInterleaveState s s) b)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (ManyInterleaveState s s -> Step (ManyInterleaveState s s) b
forall s a. s -> Step s a
Skip (s -> [s] -> ManyInterleaveState s s
forall o i. o -> [i] -> ManyInterleaveState o i
ManyInterleaveInner s
o' (s
i s -> [s] -> [s]
forall a. a -> [a] -> [a]
: [s]
ls)))
Skip s
o' -> Step (ManyInterleaveState s s) b
-> m (Step (ManyInterleaveState s s) b)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Step (ManyInterleaveState s s) b
-> m (Step (ManyInterleaveState s s) b))
-> Step (ManyInterleaveState s s) b
-> m (Step (ManyInterleaveState s s) b)
forall a b. (a -> b) -> a -> b
$ ManyInterleaveState s s -> Step (ManyInterleaveState s s) b
forall s a. s -> Step s a
Skip (s -> [s] -> ManyInterleaveState s s
forall o i. o -> [i] -> ManyInterleaveState o i
ManyInterleaveOuter s
o' [s]
ls)
Step s a
Stop -> Step (ManyInterleaveState s s) b
-> m (Step (ManyInterleaveState s s) b)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Step (ManyInterleaveState s s) b
-> m (Step (ManyInterleaveState s s) b))
-> Step (ManyInterleaveState s s) b
-> m (Step (ManyInterleaveState s s) b)
forall a b. (a -> b) -> a -> b
$ ManyInterleaveState s s -> Step (ManyInterleaveState s s) b
forall s a. s -> Step s a
Skip ([s] -> [s] -> ManyInterleaveState s s
forall o i. [i] -> [i] -> ManyInterleaveState o i
ManyInterleaveInnerL [s]
ls [])
step (ManyInterleaveInner s
_ []) = m (Step (ManyInterleaveState s s) b)
forall a. HasCallStack => a
undefined
step (ManyInterleaveInner s
o (s
st:[s]
ls)) = do
Step s b
r <- s -> m (Step s b)
istep s
st
Step (ManyInterleaveState s s) b
-> m (Step (ManyInterleaveState s s) b)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Step (ManyInterleaveState s s) b
-> m (Step (ManyInterleaveState s s) b))
-> Step (ManyInterleaveState s s) b
-> m (Step (ManyInterleaveState s s) b)
forall a b. (a -> b) -> a -> b
$ case Step s b
r of
Yield b
x s
s -> b -> ManyInterleaveState s s -> Step (ManyInterleaveState s s) b
forall s a. a -> s -> Step s a
Yield b
x (s -> [s] -> ManyInterleaveState s s
forall o i. o -> [i] -> ManyInterleaveState o i
ManyInterleaveOuter s
o (s
ss -> [s] -> [s]
forall a. a -> [a] -> [a]
:[s]
ls))
Skip s
s -> ManyInterleaveState s s -> Step (ManyInterleaveState s s) b
forall s a. s -> Step s a
Skip (s -> [s] -> ManyInterleaveState s s
forall o i. o -> [i] -> ManyInterleaveState o i
ManyInterleaveInner s
o (s
ss -> [s] -> [s]
forall a. a -> [a] -> [a]
:[s]
ls))
Step s b
Stop -> ManyInterleaveState s s -> Step (ManyInterleaveState s s) b
forall s a. s -> Step s a
Skip (s -> [s] -> ManyInterleaveState s s
forall o i. o -> [i] -> ManyInterleaveState o i
ManyInterleaveOuter s
o [s]
ls)
step (ManyInterleaveInnerL [] []) = Step (ManyInterleaveState s s) b
-> m (Step (ManyInterleaveState s s) b)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return Step (ManyInterleaveState s s) b
forall s a. Step s a
Stop
step (ManyInterleaveInnerL [] [s]
rs) =
Step (ManyInterleaveState s s) b
-> m (Step (ManyInterleaveState s s) b)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Step (ManyInterleaveState s s) b
-> m (Step (ManyInterleaveState s s) b))
-> Step (ManyInterleaveState s s) b
-> m (Step (ManyInterleaveState s s) b)
forall a b. (a -> b) -> a -> b
$ ManyInterleaveState s s -> Step (ManyInterleaveState s s) b
forall s a. s -> Step s a
Skip ([s] -> [s] -> ManyInterleaveState s s
forall o i. [i] -> [i] -> ManyInterleaveState o i
ManyInterleaveInnerR [] [s]
rs)
step (ManyInterleaveInnerL (s
st:[s]
ls) [s]
rs) = do
Step s b
r <- s -> m (Step s b)
istep s
st
Step (ManyInterleaveState s s) b
-> m (Step (ManyInterleaveState s s) b)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Step (ManyInterleaveState s s) b
-> m (Step (ManyInterleaveState s s) b))
-> Step (ManyInterleaveState s s) b
-> m (Step (ManyInterleaveState s s) b)
forall a b. (a -> b) -> a -> b
$ case Step s b
r of
Yield b
x s
s -> b -> ManyInterleaveState s s -> Step (ManyInterleaveState s s) b
forall s a. a -> s -> Step s a
Yield b
x ([s] -> [s] -> ManyInterleaveState s s
forall o i. [i] -> [i] -> ManyInterleaveState o i
ManyInterleaveInnerL [s]
ls (s
ss -> [s] -> [s]
forall a. a -> [a] -> [a]
:[s]
rs))
Skip s
s -> ManyInterleaveState s s -> Step (ManyInterleaveState s s) b
forall s a. s -> Step s a
Skip ([s] -> [s] -> ManyInterleaveState s s
forall o i. [i] -> [i] -> ManyInterleaveState o i
ManyInterleaveInnerL (s
ss -> [s] -> [s]
forall a. a -> [a] -> [a]
:[s]
ls) [s]
rs)
Step s b
Stop -> ManyInterleaveState s s -> Step (ManyInterleaveState s s) b
forall s a. s -> Step s a
Skip ([s] -> [s] -> ManyInterleaveState s s
forall o i. [i] -> [i] -> ManyInterleaveState o i
ManyInterleaveInnerL [s]
ls [s]
rs)
step (ManyInterleaveInnerR [] []) = Step (ManyInterleaveState s s) b
-> m (Step (ManyInterleaveState s s) b)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return Step (ManyInterleaveState s s) b
forall s a. Step s a
Stop
step (ManyInterleaveInnerR [s]
ls []) =
Step (ManyInterleaveState s s) b
-> m (Step (ManyInterleaveState s s) b)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Step (ManyInterleaveState s s) b
-> m (Step (ManyInterleaveState s s) b))
-> Step (ManyInterleaveState s s) b
-> m (Step (ManyInterleaveState s s) b)
forall a b. (a -> b) -> a -> b
$ ManyInterleaveState s s -> Step (ManyInterleaveState s s) b
forall s a. s -> Step s a
Skip ([s] -> [s] -> ManyInterleaveState s s
forall o i. [i] -> [i] -> ManyInterleaveState o i
ManyInterleaveInnerL [s]
ls [])
step (ManyInterleaveInnerR [s]
ls (s
st:[s]
rs)) = do
Step s b
r <- s -> m (Step s b)
istep s
st
Step (ManyInterleaveState s s) b
-> m (Step (ManyInterleaveState s s) b)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Step (ManyInterleaveState s s) b
-> m (Step (ManyInterleaveState s s) b))
-> Step (ManyInterleaveState s s) b
-> m (Step (ManyInterleaveState s s) b)
forall a b. (a -> b) -> a -> b
$ case Step s b
r of
Yield b
x s
s -> b -> ManyInterleaveState s s -> Step (ManyInterleaveState s s) b
forall s a. a -> s -> Step s a
Yield b
x ([s] -> [s] -> ManyInterleaveState s s
forall o i. [i] -> [i] -> ManyInterleaveState o i
ManyInterleaveInnerR (s
ss -> [s] -> [s]
forall a. a -> [a] -> [a]
:[s]
ls) [s]
rs)
Skip s
s -> ManyInterleaveState s s -> Step (ManyInterleaveState s s) b
forall s a. s -> Step s a
Skip ([s] -> [s] -> ManyInterleaveState s s
forall o i. [i] -> [i] -> ManyInterleaveState o i
ManyInterleaveInnerR [s]
ls (s
ss -> [s] -> [s]
forall a. a -> [a] -> [a]
:[s]
rs))
Step s b
Stop -> ManyInterleaveState s s -> Step (ManyInterleaveState s s) b
forall s a. s -> Step s a
Skip ([s] -> [s] -> ManyInterleaveState s s
forall o i. [i] -> [i] -> ManyInterleaveState o i
ManyInterleaveInnerR [s]
ls [s]
rs)