{-# LANGUAGE FlexibleContexts, GADTs, TupleSections, RankNTypes,
             ScopedTypeVariables #-}
-- | Routing for splitting and merging processing pipelines.
module Data.Machine.Concurrent.Scatter (
  scatter, mergeSum, splitSum, splitProd
  ) where
import Control.Arrow ((***))
import Control.Concurrent.Async (Async, waitAny)
import Control.Concurrent.Async.Lifted (wait, waitEither, waitBoth)
import Control.Monad ((>=>))
import Control.Monad.Base (liftBase)
import Control.Monad.Trans.Control (MonadBaseControl, restoreM, StM)
import Data.Machine
import Data.Machine.Concurrent.AsyncStep

holes :: [a] -> [[a]]
holes :: [a] -> [[a]]
holes = ([a] -> [a]) -> [a] -> [[a]]
forall a a. ([a] -> a) -> [a] -> [a]
go [a] -> [a]
forall a. a -> a
id
  where go :: ([a] -> a) -> [a] -> [a]
go [a] -> a
_ [] = []
        go [a] -> a
x (a
y:[a]
ys) = [a] -> a
x [a]
ys a -> [a] -> [a]
forall a. a -> [a] -> [a]
: ([a] -> a) -> [a] -> [a]
go ([a] -> a
x ([a] -> a) -> ([a] -> [a]) -> [a] -> a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (a
ya -> [a] -> [a]
forall a. a -> [a] -> [a]
:)) [a]
ys

diff :: [a] -> [(a,[a])]
diff :: [a] -> [(a, [a])]
diff [a]
xs = [a] -> [[a]] -> [(a, [a])]
forall a b. [a] -> [b] -> [(a, b)]
zip [a]
xs ([a] -> [[a]]
forall a. [a] -> [[a]]
holes [a]
xs)

waitAnyHole :: MonadBaseControl IO m => [(Async (StM m a), [b])] -> m (a, [b])
waitAnyHole :: [(Async (StM m a), [b])] -> m (a, [b])
waitAnyHole [(Async (StM m a), [b])]
xs = do (Async (StM m a, [b])
_,(StM m a
s,[b]
b)) <- IO (Async (StM m a, [b]), (StM m a, [b]))
-> m (Async (StM m a, [b]), (StM m a, [b]))
forall (b :: * -> *) (m :: * -> *) α. MonadBase b m => b α -> m α
liftBase (IO (Async (StM m a, [b]), (StM m a, [b]))
 -> m (Async (StM m a, [b]), (StM m a, [b])))
-> IO (Async (StM m a, [b]), (StM m a, [b]))
-> m (Async (StM m a, [b]), (StM m a, [b]))
forall a b. (a -> b) -> a -> b
$ [Async (StM m a, [b])] -> IO (Async (StM m a, [b]), (StM m a, [b]))
forall a. [Async a] -> IO (Async a, a)
waitAny [Async (StM m a, [b])]
xs'
                    (a -> (a, [b])) -> m a -> m (a, [b])
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (,[b]
b) (StM m a -> m a
forall (b :: * -> *) (m :: * -> *) a.
MonadBaseControl b m =>
StM m a -> m a
restoreM StM m a
s)
  where xs' :: [Async (StM m a, [b])]
xs' = ((Async (StM m a), [b]) -> Async (StM m a, [b]))
-> [(Async (StM m a), [b])] -> [Async (StM m a, [b])]
forall a b. (a -> b) -> [a] -> [b]
map (\(Async (StM m a)
a,[b]
b) -> (StM m a -> (StM m a, [b]))
-> Async (StM m a) -> Async (StM m a, [b])
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (,[b]
b) Async (StM m a)
a) [(Async (StM m a), [b])]
xs

-- | Produces values from whichever source 'MachineT' yields
-- first. This operation may also be viewed as a /gather/ operation in
-- that all values produced by the given machines are interleaved when
-- fed downstream. Note that inputs are /not/ shared. The composite
-- machine will await an input when any constituent machine awaits an
-- input. That input will be supplied to the awaiting constituent and
-- no other.
--
-- Some examples of more specific useful types @scatter@ may be used
-- at,
-- 
-- @
-- scatter :: [ProcessT m a b] -> ProcessT m a b
-- scatter :: [SourceT m a] -> SourceT m a
-- @
--
-- The former may be used to stream data through a collection of
-- worker 'Process'es, the latter may be used to intersperse values
-- from a collection of sources.
scatter :: MonadBaseControl IO m => [MachineT m k o] -> MachineT m k o
scatter :: [MachineT m k o] -> MachineT m k o
scatter [] = MachineT m k o
forall (k :: * -> *) b. Machine k b
stopped
scatter [MachineT m k o]
sinks = m (Step k o (MachineT m k o)) -> MachineT m k o
forall (m :: * -> *) (k :: * -> *) o.
m (Step k o (MachineT m k o)) -> MachineT m k o
MachineT (m (Step k o (MachineT m k o)) -> MachineT m k o)
-> m (Step k o (MachineT m k o)) -> MachineT m k o
forall a b. (a -> b) -> a -> b
$ (MachineT m k o -> m (Async (StM m (Step k o (MachineT m k o)))))
-> [MachineT m k o]
-> m [Async (StM m (Step k o (MachineT m k o)))]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
mapM MachineT m k o -> m (Async (StM m (Step k o (MachineT m k o))))
forall (m :: * -> *) (k :: * -> *) o.
MonadBaseControl IO m =>
MachineT m k o -> m (AsyncStep m k o)
asyncRun [MachineT m k o]
sinks
                 m [Async (StM m (Step k o (MachineT m k o)))]
-> ([Async (StM m (Step k o (MachineT m k o)))]
    -> m (Step k o (MachineT m k o),
          [Async (StM m (Step k o (MachineT m k o)))]))
-> m (Step k o (MachineT m k o),
      [Async (StM m (Step k o (MachineT m k o)))])
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= [(Async (StM m (Step k o (MachineT m k o))),
  [Async (StM m (Step k o (MachineT m k o)))])]
-> m (Step k o (MachineT m k o),
      [Async (StM m (Step k o (MachineT m k o)))])
forall (m :: * -> *) a b.
MonadBaseControl IO m =>
[(Async (StM m a), [b])] -> m (a, [b])
waitAnyHole ([(Async (StM m (Step k o (MachineT m k o))),
   [Async (StM m (Step k o (MachineT m k o)))])]
 -> m (Step k o (MachineT m k o),
       [Async (StM m (Step k o (MachineT m k o)))]))
-> ([Async (StM m (Step k o (MachineT m k o)))]
    -> [(Async (StM m (Step k o (MachineT m k o))),
         [Async (StM m (Step k o (MachineT m k o)))])])
-> [Async (StM m (Step k o (MachineT m k o)))]
-> m (Step k o (MachineT m k o),
      [Async (StM m (Step k o (MachineT m k o)))])
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Async (StM m (Step k o (MachineT m k o)))]
-> [(Async (StM m (Step k o (MachineT m k o))),
     [Async (StM m (Step k o (MachineT m k o)))])]
forall a. [a] -> [(a, [a])]
diff
                 m (Step k o (MachineT m k o),
   [Async (StM m (Step k o (MachineT m k o)))])
-> ((Step k o (MachineT m k o),
     [Async (StM m (Step k o (MachineT m k o)))])
    -> m (Step k o (MachineT m k o)))
-> m (Step k o (MachineT m k o))
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= (Step k o (MachineT m k o)
 -> [Async (StM m (Step k o (MachineT m k o)))]
 -> m (Step k o (MachineT m k o)))
-> (Step k o (MachineT m k o),
    [Async (StM m (Step k o (MachineT m k o)))])
-> m (Step k o (MachineT m k o))
forall a b c. (a -> b -> c) -> (a, b) -> c
uncurry Step k o (MachineT m k o)
-> [Async (StM m (Step k o (MachineT m k o)))]
-> m (Step k o (MachineT m k o))
forall (m :: * -> *) (k :: * -> *) o.
MonadBaseControl IO m =>
MachineStep m k o -> [AsyncStep m k o] -> m (MachineStep m k o)
go
  where go :: MonadBaseControl IO m
           => MachineStep m k o
           -> [AsyncStep m k o]
           -> m (MachineStep m k o)
        go :: MachineStep m k o -> [AsyncStep m k o] -> m (MachineStep m k o)
go MachineStep m k o
Stop [] = MachineStep m k o -> m (MachineStep m k o)
forall (m :: * -> *) a. Monad m => a -> m a
return MachineStep m k o
forall (k :: * -> *) o r. Step k o r
Stop
        go MachineStep m k o
Stop [AsyncStep m k o]
sinks' = [(AsyncStep m k o, [AsyncStep m k o])]
-> m (MachineStep m k o, [AsyncStep m k o])
forall (m :: * -> *) a b.
MonadBaseControl IO m =>
[(Async (StM m a), [b])] -> m (a, [b])
waitAnyHole ([AsyncStep m k o] -> [(AsyncStep m k o, [AsyncStep m k o])]
forall a. [a] -> [(a, [a])]
diff [AsyncStep m k o]
sinks') m (MachineStep m k o, [AsyncStep m k o])
-> ((MachineStep m k o, [AsyncStep m k o])
    -> m (MachineStep m k o))
-> m (MachineStep m k o)
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= (MachineStep m k o -> [AsyncStep m k o] -> m (MachineStep m k o))
-> (MachineStep m k o, [AsyncStep m k o]) -> m (MachineStep m k o)
forall a b c. (a -> b -> c) -> (a, b) -> c
uncurry MachineStep m k o -> [AsyncStep m k o] -> m (MachineStep m k o)
forall (m :: * -> *) (k :: * -> *) o.
MonadBaseControl IO m =>
MachineStep m k o -> [AsyncStep m k o] -> m (MachineStep m k o)
go
        go (Yield o
o MachineT m k o
k) [AsyncStep m k o]
sinks' = 
          MachineT m k o -> m (AsyncStep m k o)
forall (m :: * -> *) (k :: * -> *) o.
MonadBaseControl IO m =>
MachineT m k o -> m (AsyncStep m k o)
asyncRun MachineT m k o
k m (AsyncStep m k o)
-> (AsyncStep m k o -> m (MachineStep m k o))
-> m (MachineStep m k o)
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= MachineStep m k o -> m (MachineStep m k o)
forall (m :: * -> *) a. Monad m => a -> m a
return (MachineStep m k o -> m (MachineStep m k o))
-> (AsyncStep m k o -> MachineStep m k o)
-> AsyncStep m k o
-> m (MachineStep m k o)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. o -> MachineT m k o -> MachineStep m k o
forall (k :: * -> *) o r. o -> r -> Step k o r
Yield o
o (MachineT m k o -> MachineStep m k o)
-> (AsyncStep m k o -> MachineT m k o)
-> AsyncStep m k o
-> MachineStep m k o
forall b c a. (b -> c) -> (a -> b) -> a -> c
. m (MachineStep m k o) -> MachineT m k o
forall (m :: * -> *) (k :: * -> *) o.
m (Step k o (MachineT m k o)) -> MachineT m k o
MachineT (m (MachineStep m k o) -> MachineT m k o)
-> (AsyncStep m k o -> m (MachineStep m k o))
-> AsyncStep m k o
-> MachineT m k o
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [AsyncStep m k o] -> m (MachineStep m k o)
forall (m :: * -> *) (k :: * -> *) o.
MonadBaseControl IO m =>
[AsyncStep m k o] -> m (MachineStep m k o)
goWait ([AsyncStep m k o] -> m (MachineStep m k o))
-> (AsyncStep m k o -> [AsyncStep m k o])
-> AsyncStep m k o
-> m (MachineStep m k o)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (AsyncStep m k o -> [AsyncStep m k o] -> [AsyncStep m k o]
forall a. a -> [a] -> [a]
:[AsyncStep m k o]
sinks')
        go (Await t -> MachineT m k o
f k t
fk MachineT m k o
ff) [AsyncStep m k o]
sinks' =
          (t -> MachineT m k o)
-> k t
-> MachineT m k o
-> (AsyncStep m k o -> MachineT m k o)
-> m (MachineStep m k o)
forall (m :: * -> *) a (k :: * -> *) o (k' :: * -> *)
       (k1 :: * -> *) o1 b.
MonadBaseControl IO m =>
(a -> MachineT m k o)
-> k' a
-> MachineT m k o
-> (AsyncStep m k o -> MachineT m k1 o1)
-> m (Step k' b (MachineT m k1 o1))
asyncAwait t -> MachineT m k o
f k t
fk MachineT m k o
ff (m (MachineStep m k o) -> MachineT m k o
forall (m :: * -> *) (k :: * -> *) o.
m (Step k o (MachineT m k o)) -> MachineT m k o
MachineT (m (MachineStep m k o) -> MachineT m k o)
-> (AsyncStep m k o -> m (MachineStep m k o))
-> AsyncStep m k o
-> MachineT m k o
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [AsyncStep m k o] -> m (MachineStep m k o)
forall (m :: * -> *) (k :: * -> *) o.
MonadBaseControl IO m =>
[AsyncStep m k o] -> m (MachineStep m k o)
goWait ([AsyncStep m k o] -> m (MachineStep m k o))
-> (AsyncStep m k o -> [AsyncStep m k o])
-> AsyncStep m k o
-> m (MachineStep m k o)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (AsyncStep m k o -> [AsyncStep m k o] -> [AsyncStep m k o]
forall a. a -> [a] -> [a]
:[AsyncStep m k o]
sinks'))
        goWait :: MonadBaseControl IO m
               => [AsyncStep m k o]
               -> m (MachineStep m k o)
        goWait :: [AsyncStep m k o] -> m (MachineStep m k o)
goWait = [(AsyncStep m k o, [AsyncStep m k o])]
-> m (MachineStep m k o, [AsyncStep m k o])
forall (m :: * -> *) a b.
MonadBaseControl IO m =>
[(Async (StM m a), [b])] -> m (a, [b])
waitAnyHole ([(AsyncStep m k o, [AsyncStep m k o])]
 -> m (MachineStep m k o, [AsyncStep m k o]))
-> ([AsyncStep m k o] -> [(AsyncStep m k o, [AsyncStep m k o])])
-> [AsyncStep m k o]
-> m (MachineStep m k o, [AsyncStep m k o])
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [AsyncStep m k o] -> [(AsyncStep m k o, [AsyncStep m k o])]
forall a. [a] -> [(a, [a])]
diff ([AsyncStep m k o] -> m (MachineStep m k o, [AsyncStep m k o]))
-> ((MachineStep m k o, [AsyncStep m k o])
    -> m (MachineStep m k o))
-> [AsyncStep m k o]
-> m (MachineStep m k o)
forall (m :: * -> *) a b c.
Monad m =>
(a -> m b) -> (b -> m c) -> a -> m c
>=> (MachineStep m k o -> [AsyncStep m k o] -> m (MachineStep m k o))
-> (MachineStep m k o, [AsyncStep m k o]) -> m (MachineStep m k o)
forall a b c. (a -> b -> c) -> (a, b) -> c
uncurry MachineStep m k o -> [AsyncStep m k o] -> m (MachineStep m k o)
forall (m :: * -> *) (k :: * -> *) o.
MonadBaseControl IO m =>
MachineStep m k o -> [AsyncStep m k o] -> m (MachineStep m k o)
go

-- | Similar to 'Control.Arrow.|||': split the input between two
-- processes and merge their outputs.
--
-- Connect two processes to the downstream tails of a 'Machine' that
-- produces 'Either's. The two downstream consumers are run
-- concurrently when possible. When one downstream consumer stops, the
-- other is allowed to run until it stops or the upstream source
-- yields a value the remaining consumer can not handle.
--
-- @mergeSum sinkL sinkR@ produces a topology like this,
--
-- @
--                                 sinkL
--                                /      \\
--                              a          \\
--                             /            \\
--    source -- Either a b -->                -- r -->
--                             \\            /
--                              b          /
--                               \\       /
--                                 sinkR 
-- @
mergeSum :: forall m a b r. MonadBaseControl IO m
         => ProcessT m a r -> ProcessT m b r -> ProcessT m (Either a b) r
mergeSum :: ProcessT m a r -> ProcessT m b r -> ProcessT m (Either a b) r
mergeSum ProcessT m a r
snkL ProcessT m b r
snkR = m (Step (Is (Either a b)) r (ProcessT m (Either a b) r))
-> ProcessT m (Either a b) r
forall (m :: * -> *) (k :: * -> *) o.
m (Step k o (MachineT m k o)) -> MachineT m k o
MachineT (m (Step (Is (Either a b)) r (ProcessT m (Either a b) r))
 -> ProcessT m (Either a b) r)
-> m (Step (Is (Either a b)) r (ProcessT m (Either a b) r))
-> ProcessT m (Either a b) r
forall a b. (a -> b) -> a -> b
$ do Async (StM m (MachineStep m (Is a) r))
sl <- ProcessT m a r -> m (Async (StM m (MachineStep m (Is a) r)))
forall (m :: * -> *) (k :: * -> *) o.
MonadBaseControl IO m =>
MachineT m k o -> m (AsyncStep m k o)
asyncRun ProcessT m a r
snkL
                                   Async (StM m (MachineStep m (Is b) r))
sr <- ProcessT m b r -> m (Async (StM m (MachineStep m (Is b) r)))
forall (m :: * -> *) (k :: * -> *) o.
MonadBaseControl IO m =>
MachineT m k o -> m (AsyncStep m k o)
asyncRun ProcessT m b r
snkR
                                   Async (StM m (MachineStep m (Is a) r))
-> Async (StM m (MachineStep m (Is b) r))
-> m (Step (Is (Either a b)) r (ProcessT m (Either a b) r))
go Async (StM m (MachineStep m (Is a) r))
sl Async (StM m (MachineStep m (Is b) r))
sr
  where go :: AsyncStep m (Is a) r
           -> AsyncStep m (Is b) r
           -> m (MachineStep m (Is (Either a b)) r)
        go :: Async (StM m (MachineStep m (Is a) r))
-> Async (StM m (MachineStep m (Is b) r))
-> m (Step (Is (Either a b)) r (ProcessT m (Either a b) r))
go Async (StM m (MachineStep m (Is a) r))
sl Async (StM m (MachineStep m (Is b) r))
sr = Async (StM m (MachineStep m (Is a) r))
-> Async (StM m (MachineStep m (Is b) r))
-> m (Either (MachineStep m (Is a) r) (MachineStep m (Is b) r))
forall (m :: * -> *) a b.
MonadBaseControl IO m =>
Async (StM m a) -> Async (StM m b) -> m (Either a b)
waitEither Async (StM m (MachineStep m (Is a) r))
sl Async (StM m (MachineStep m (Is b) r))
sr m (Either (MachineStep m (Is a) r) (MachineStep m (Is b) r))
-> (Either (MachineStep m (Is a) r) (MachineStep m (Is b) r)
    -> m (Step (Is (Either a b)) r (ProcessT m (Either a b) r)))
-> m (Step (Is (Either a b)) r (ProcessT m (Either a b) r))
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= 
                   \(Either (MachineStep m (Is a) r) (MachineStep m (Is b) r)
s :: Either (MachineStep m (Is a) r)
                                 (MachineStep m (Is b) r)) -> case Either (MachineStep m (Is a) r) (MachineStep m (Is b) r)
s of
          Left MachineStep m (Is a) r
Stop -> Async (StM m (MachineStep m (Is b) r))
-> m (MachineStep m (Is b) r)
forall (m :: * -> *) a.
MonadBaseControl IO m =>
Async (StM m a) -> m a
wait Async (StM m (MachineStep m (Is b) r))
sr m (MachineStep m (Is b) r)
-> (MachineStep m (Is b) r
    -> m (Step (Is (Either a b)) r (ProcessT m (Either a b) r)))
-> m (Step (Is (Either a b)) r (ProcessT m (Either a b) r))
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= ProcessT m (Either a b) r
-> m (Step (Is (Either a b)) r (ProcessT m (Either a b) r))
forall (m :: * -> *) (k :: * -> *) o.
MachineT m k o -> m (Step k o (MachineT m k o))
runMachineT (ProcessT m (Either a b) r
 -> m (Step (Is (Either a b)) r (ProcessT m (Either a b) r)))
-> (MachineStep m (Is b) r -> ProcessT m (Either a b) r)
-> MachineStep m (Is b) r
-> m (Step (Is (Either a b)) r (ProcessT m (Either a b) r))
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ProcessT m b r -> ProcessT m (Either a b) r
forall (m :: * -> *) b r a.
Monad m =>
ProcessT m b r -> ProcessT m (Either a b) r
rightOnly (ProcessT m b r -> ProcessT m (Either a b) r)
-> (MachineStep m (Is b) r -> ProcessT m b r)
-> MachineStep m (Is b) r
-> ProcessT m (Either a b) r
forall b c a. (b -> c) -> (a -> b) -> a -> c
. MachineStep m (Is b) r -> ProcessT m b r
forall (m :: * -> *) (k :: * -> *) o.
Monad m =>
Step k o (MachineT m k o) -> MachineT m k o
encased
          Right MachineStep m (Is b) r
Stop -> Async (StM m (MachineStep m (Is a) r))
-> m (MachineStep m (Is a) r)
forall (m :: * -> *) a.
MonadBaseControl IO m =>
Async (StM m a) -> m a
wait Async (StM m (MachineStep m (Is a) r))
sl m (MachineStep m (Is a) r)
-> (MachineStep m (Is a) r
    -> m (Step (Is (Either a b)) r (ProcessT m (Either a b) r)))
-> m (Step (Is (Either a b)) r (ProcessT m (Either a b) r))
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= ProcessT m (Either a b) r
-> m (Step (Is (Either a b)) r (ProcessT m (Either a b) r))
forall (m :: * -> *) (k :: * -> *) o.
MachineT m k o -> m (Step k o (MachineT m k o))
runMachineT (ProcessT m (Either a b) r
 -> m (Step (Is (Either a b)) r (ProcessT m (Either a b) r)))
-> (MachineStep m (Is a) r -> ProcessT m (Either a b) r)
-> MachineStep m (Is a) r
-> m (Step (Is (Either a b)) r (ProcessT m (Either a b) r))
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ProcessT m a r -> ProcessT m (Either a b) r
forall (m :: * -> *) a r b.
Monad m =>
ProcessT m a r -> ProcessT m (Either a b) r
leftOnly (ProcessT m a r -> ProcessT m (Either a b) r)
-> (MachineStep m (Is a) r -> ProcessT m a r)
-> MachineStep m (Is a) r
-> ProcessT m (Either a b) r
forall b c a. (b -> c) -> (a -> b) -> a -> c
. MachineStep m (Is a) r -> ProcessT m a r
forall (m :: * -> *) (k :: * -> *) o.
Monad m =>
Step k o (MachineT m k o) -> MachineT m k o
encased

          Left (Yield r
o ProcessT m a r
k) -> 
            Step (Is (Either a b)) r (ProcessT m (Either a b) r)
-> m (Step (Is (Either a b)) r (ProcessT m (Either a b) r))
forall (m :: * -> *) a. Monad m => a -> m a
return (Step (Is (Either a b)) r (ProcessT m (Either a b) r)
 -> m (Step (Is (Either a b)) r (ProcessT m (Either a b) r)))
-> (m (Step (Is (Either a b)) r (ProcessT m (Either a b) r))
    -> Step (Is (Either a b)) r (ProcessT m (Either a b) r))
-> m (Step (Is (Either a b)) r (ProcessT m (Either a b) r))
-> m (Step (Is (Either a b)) r (ProcessT m (Either a b) r))
forall b c a. (b -> c) -> (a -> b) -> a -> c
. r
-> ProcessT m (Either a b) r
-> Step (Is (Either a b)) r (ProcessT m (Either a b) r)
forall (k :: * -> *) o r. o -> r -> Step k o r
Yield r
o (ProcessT m (Either a b) r
 -> Step (Is (Either a b)) r (ProcessT m (Either a b) r))
-> (m (Step (Is (Either a b)) r (ProcessT m (Either a b) r))
    -> ProcessT m (Either a b) r)
-> m (Step (Is (Either a b)) r (ProcessT m (Either a b) r))
-> Step (Is (Either a b)) r (ProcessT m (Either a b) r)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. m (Step (Is (Either a b)) r (ProcessT m (Either a b) r))
-> ProcessT m (Either a b) r
forall (m :: * -> *) (k :: * -> *) o.
m (Step k o (MachineT m k o)) -> MachineT m k o
MachineT (m (Step (Is (Either a b)) r (ProcessT m (Either a b) r))
 -> m (Step (Is (Either a b)) r (ProcessT m (Either a b) r)))
-> m (Step (Is (Either a b)) r (ProcessT m (Either a b) r))
-> m (Step (Is (Either a b)) r (ProcessT m (Either a b) r))
forall a b. (a -> b) -> a -> b
$ ProcessT m a r -> m (Async (StM m (MachineStep m (Is a) r)))
forall (m :: * -> *) (k :: * -> *) o.
MonadBaseControl IO m =>
MachineT m k o -> m (AsyncStep m k o)
asyncRun ProcessT m a r
k m (Async (StM m (MachineStep m (Is a) r)))
-> (Async (StM m (MachineStep m (Is a) r))
    -> m (Step (Is (Either a b)) r (ProcessT m (Either a b) r)))
-> m (Step (Is (Either a b)) r (ProcessT m (Either a b) r))
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= (Async (StM m (MachineStep m (Is a) r))
 -> Async (StM m (MachineStep m (Is b) r))
 -> m (Step (Is (Either a b)) r (ProcessT m (Either a b) r)))
-> Async (StM m (MachineStep m (Is b) r))
-> Async (StM m (MachineStep m (Is a) r))
-> m (Step (Is (Either a b)) r (ProcessT m (Either a b) r))
forall a b c. (a -> b -> c) -> b -> a -> c
flip Async (StM m (MachineStep m (Is a) r))
-> Async (StM m (MachineStep m (Is b) r))
-> m (Step (Is (Either a b)) r (ProcessT m (Either a b) r))
go Async (StM m (MachineStep m (Is b) r))
sr
          Right (Yield r
o ProcessT m b r
k) -> 
            Step (Is (Either a b)) r (ProcessT m (Either a b) r)
-> m (Step (Is (Either a b)) r (ProcessT m (Either a b) r))
forall (m :: * -> *) a. Monad m => a -> m a
return (Step (Is (Either a b)) r (ProcessT m (Either a b) r)
 -> m (Step (Is (Either a b)) r (ProcessT m (Either a b) r)))
-> (m (Step (Is (Either a b)) r (ProcessT m (Either a b) r))
    -> Step (Is (Either a b)) r (ProcessT m (Either a b) r))
-> m (Step (Is (Either a b)) r (ProcessT m (Either a b) r))
-> m (Step (Is (Either a b)) r (ProcessT m (Either a b) r))
forall b c a. (b -> c) -> (a -> b) -> a -> c
. r
-> ProcessT m (Either a b) r
-> Step (Is (Either a b)) r (ProcessT m (Either a b) r)
forall (k :: * -> *) o r. o -> r -> Step k o r
Yield r
o (ProcessT m (Either a b) r
 -> Step (Is (Either a b)) r (ProcessT m (Either a b) r))
-> (m (Step (Is (Either a b)) r (ProcessT m (Either a b) r))
    -> ProcessT m (Either a b) r)
-> m (Step (Is (Either a b)) r (ProcessT m (Either a b) r))
-> Step (Is (Either a b)) r (ProcessT m (Either a b) r)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. m (Step (Is (Either a b)) r (ProcessT m (Either a b) r))
-> ProcessT m (Either a b) r
forall (m :: * -> *) (k :: * -> *) o.
m (Step k o (MachineT m k o)) -> MachineT m k o
MachineT (m (Step (Is (Either a b)) r (ProcessT m (Either a b) r))
 -> m (Step (Is (Either a b)) r (ProcessT m (Either a b) r)))
-> m (Step (Is (Either a b)) r (ProcessT m (Either a b) r))
-> m (Step (Is (Either a b)) r (ProcessT m (Either a b) r))
forall a b. (a -> b) -> a -> b
$ ProcessT m b r -> m (Async (StM m (MachineStep m (Is b) r)))
forall (m :: * -> *) (k :: * -> *) o.
MonadBaseControl IO m =>
MachineT m k o -> m (AsyncStep m k o)
asyncRun ProcessT m b r
k m (Async (StM m (MachineStep m (Is b) r)))
-> (Async (StM m (MachineStep m (Is b) r))
    -> m (Step (Is (Either a b)) r (ProcessT m (Either a b) r)))
-> m (Step (Is (Either a b)) r (ProcessT m (Either a b) r))
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= Async (StM m (MachineStep m (Is a) r))
-> Async (StM m (MachineStep m (Is b) r))
-> m (Step (Is (Either a b)) r (ProcessT m (Either a b) r))
go Async (StM m (MachineStep m (Is a) r))
sl
                               
          Left (Await t -> ProcessT m a r
f Is a t
Refl ProcessT m a r
ff) ->
            Step (Is (Either t b)) r (ProcessT m (Either a b) r)
-> m (Step (Is (Either t b)) r (ProcessT m (Either a b) r))
forall (m :: * -> *) a. Monad m => a -> m a
return (Step (Is (Either t b)) r (ProcessT m (Either a b) r)
 -> m (Step (Is (Either t b)) r (ProcessT m (Either a b) r)))
-> Step (Is (Either t b)) r (ProcessT m (Either a b) r)
-> m (Step (Is (Either t b)) r (ProcessT m (Either a b) r))
forall a b. (a -> b) -> a -> b
$ 
            (Either t b -> ProcessT m (Either a b) r)
-> Is (Either t b) (Either t b)
-> ProcessT m (Either a b) r
-> Step (Is (Either t b)) r (ProcessT m (Either a b) r)
forall (k :: * -> *) o r t. (t -> r) -> k t -> r -> Step k o r
Await (\Either t b
u -> case Either t b
u of
                           Left t
a -> m (Step (Is (Either a b)) r (ProcessT m (Either a b) r))
-> ProcessT m (Either a b) r
forall (m :: * -> *) (k :: * -> *) o.
m (Step k o (MachineT m k o)) -> MachineT m k o
MachineT (m (Step (Is (Either a b)) r (ProcessT m (Either a b) r))
 -> ProcessT m (Either a b) r)
-> m (Step (Is (Either a b)) r (ProcessT m (Either a b) r))
-> ProcessT m (Either a b) r
forall a b. (a -> b) -> a -> b
$ ProcessT m a r -> m (Async (StM m (MachineStep m (Is a) r)))
forall (m :: * -> *) (k :: * -> *) o.
MonadBaseControl IO m =>
MachineT m k o -> m (AsyncStep m k o)
asyncRun (t -> ProcessT m a r
f t
a) m (Async (StM m (MachineStep m (Is a) r)))
-> (Async (StM m (MachineStep m (Is a) r))
    -> m (Step (Is (Either a b)) r (ProcessT m (Either a b) r)))
-> m (Step (Is (Either a b)) r (ProcessT m (Either a b) r))
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= (Async (StM m (MachineStep m (Is a) r))
 -> Async (StM m (MachineStep m (Is b) r))
 -> m (Step (Is (Either a b)) r (ProcessT m (Either a b) r)))
-> Async (StM m (MachineStep m (Is b) r))
-> Async (StM m (MachineStep m (Is a) r))
-> m (Step (Is (Either a b)) r (ProcessT m (Either a b) r))
forall a b c. (a -> b -> c) -> b -> a -> c
flip Async (StM m (MachineStep m (Is a) r))
-> Async (StM m (MachineStep m (Is b) r))
-> m (Step (Is (Either a b)) r (ProcessT m (Either a b) r))
go Async (StM m (MachineStep m (Is b) r))
sr
                           Right b
b -> m (Step (Is (Either a b)) r (ProcessT m (Either a b) r))
-> ProcessT m (Either a b) r
forall (m :: * -> *) (k :: * -> *) o.
m (Step k o (MachineT m k o)) -> MachineT m k o
MachineT (m (Step (Is (Either a b)) r (ProcessT m (Either a b) r))
 -> ProcessT m (Either a b) r)
-> m (Step (Is (Either a b)) r (ProcessT m (Either a b) r))
-> ProcessT m (Either a b) r
forall a b. (a -> b) -> a -> b
$ 
                                      Async (StM m (MachineStep m (Is b) r))
-> m (MachineStep m (Is b) r)
forall (m :: * -> *) a.
MonadBaseControl IO m =>
Async (StM m a) -> m a
wait Async (StM m (MachineStep m (Is b) r))
sr m (MachineStep m (Is b) r)
-> (MachineStep m (Is b) r
    -> m (Step (Is (Either a b)) r (ProcessT m (Either a b) r)))
-> m (Step (Is (Either a b)) r (ProcessT m (Either a b) r))
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= (Async (StM m (MachineStep m (Is b) r))
 -> m (Step (Is (Either a b)) r (ProcessT m (Either a b) r)))
-> b
-> ProcessT m b r
-> m (Step (Is (Either a b)) r (ProcessT m (Either a b) r))
forall (m :: * -> *) a (k :: * -> *) b.
MonadBaseControl IO m =>
(AsyncStep m (Is a) b -> m (MachineStep m k b))
-> a -> ProcessT m a b -> m (MachineStep m k b)
forceFeed (Async (StM m (MachineStep m (Is a) r))
-> Async (StM m (MachineStep m (Is b) r))
-> m (Step (Is (Either a b)) r (ProcessT m (Either a b) r))
go Async (StM m (MachineStep m (Is a) r))
sl) b
b (ProcessT m b r
 -> m (Step (Is (Either a b)) r (ProcessT m (Either a b) r)))
-> (MachineStep m (Is b) r -> ProcessT m b r)
-> MachineStep m (Is b) r
-> m (Step (Is (Either a b)) r (ProcessT m (Either a b) r))
forall b c a. (b -> c) -> (a -> b) -> a -> c
. MachineStep m (Is b) r -> ProcessT m b r
forall (m :: * -> *) (k :: * -> *) o.
Monad m =>
Step k o (MachineT m k o) -> MachineT m k o
encased)
                  Is (Either t b) (Either t b)
forall a. Is a a
Refl
                  (m (Step (Is (Either a b)) r (ProcessT m (Either a b) r))
-> ProcessT m (Either a b) r
forall (m :: * -> *) (k :: * -> *) o.
m (Step k o (MachineT m k o)) -> MachineT m k o
MachineT (m (Step (Is (Either a b)) r (ProcessT m (Either a b) r))
 -> ProcessT m (Either a b) r)
-> m (Step (Is (Either a b)) r (ProcessT m (Either a b) r))
-> ProcessT m (Either a b) r
forall a b. (a -> b) -> a -> b
$ ProcessT m a r -> m (Async (StM m (MachineStep m (Is a) r)))
forall (m :: * -> *) (k :: * -> *) o.
MonadBaseControl IO m =>
MachineT m k o -> m (AsyncStep m k o)
asyncRun ProcessT m a r
ff m (Async (StM m (MachineStep m (Is a) r)))
-> (Async (StM m (MachineStep m (Is a) r))
    -> m (Step (Is (Either a b)) r (ProcessT m (Either a b) r)))
-> m (Step (Is (Either a b)) r (ProcessT m (Either a b) r))
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= (Async (StM m (MachineStep m (Is a) r))
 -> Async (StM m (MachineStep m (Is b) r))
 -> m (Step (Is (Either a b)) r (ProcessT m (Either a b) r)))
-> Async (StM m (MachineStep m (Is b) r))
-> Async (StM m (MachineStep m (Is a) r))
-> m (Step (Is (Either a b)) r (ProcessT m (Either a b) r))
forall a b c. (a -> b -> c) -> b -> a -> c
flip Async (StM m (MachineStep m (Is a) r))
-> Async (StM m (MachineStep m (Is b) r))
-> m (Step (Is (Either a b)) r (ProcessT m (Either a b) r))
go Async (StM m (MachineStep m (Is b) r))
sr)
          Right (Await t -> ProcessT m b r
g Is b t
Refl ProcessT m b r
gg) -> Step (Is (Either a t)) r (ProcessT m (Either a b) r)
-> m (Step (Is (Either a t)) r (ProcessT m (Either a b) r))
forall (m :: * -> *) a. Monad m => a -> m a
return (Step (Is (Either a t)) r (ProcessT m (Either a b) r)
 -> m (Step (Is (Either a t)) r (ProcessT m (Either a b) r)))
-> Step (Is (Either a t)) r (ProcessT m (Either a b) r)
-> m (Step (Is (Either a t)) r (ProcessT m (Either a b) r))
forall a b. (a -> b) -> a -> b
$
            (Either a t -> ProcessT m (Either a b) r)
-> Is (Either a t) (Either a t)
-> ProcessT m (Either a b) r
-> Step (Is (Either a t)) r (ProcessT m (Either a b) r)
forall (k :: * -> *) o r t. (t -> r) -> k t -> r -> Step k o r
Await (\Either a t
u -> case Either a t
u of
                           Left a
a -> 
                             m (Step (Is (Either a b)) r (ProcessT m (Either a b) r))
-> ProcessT m (Either a b) r
forall (m :: * -> *) (k :: * -> *) o.
m (Step k o (MachineT m k o)) -> MachineT m k o
MachineT (m (Step (Is (Either a b)) r (ProcessT m (Either a b) r))
 -> ProcessT m (Either a b) r)
-> m (Step (Is (Either a b)) r (ProcessT m (Either a b) r))
-> ProcessT m (Either a b) r
forall a b. (a -> b) -> a -> b
$
                             Async (StM m (MachineStep m (Is a) r))
-> m (MachineStep m (Is a) r)
forall (m :: * -> *) a.
MonadBaseControl IO m =>
Async (StM m a) -> m a
wait Async (StM m (MachineStep m (Is a) r))
sl m (MachineStep m (Is a) r)
-> (MachineStep m (Is a) r
    -> m (Step (Is (Either a b)) r (ProcessT m (Either a b) r)))
-> m (Step (Is (Either a b)) r (ProcessT m (Either a b) r))
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= (Async (StM m (MachineStep m (Is a) r))
 -> m (Step (Is (Either a b)) r (ProcessT m (Either a b) r)))
-> a
-> ProcessT m a r
-> m (Step (Is (Either a b)) r (ProcessT m (Either a b) r))
forall (m :: * -> *) a (k :: * -> *) b.
MonadBaseControl IO m =>
(AsyncStep m (Is a) b -> m (MachineStep m k b))
-> a -> ProcessT m a b -> m (MachineStep m k b)
forceFeed ((Async (StM m (MachineStep m (Is a) r))
 -> Async (StM m (MachineStep m (Is b) r))
 -> m (Step (Is (Either a b)) r (ProcessT m (Either a b) r)))
-> Async (StM m (MachineStep m (Is b) r))
-> Async (StM m (MachineStep m (Is a) r))
-> m (Step (Is (Either a b)) r (ProcessT m (Either a b) r))
forall a b c. (a -> b -> c) -> b -> a -> c
flip Async (StM m (MachineStep m (Is a) r))
-> Async (StM m (MachineStep m (Is b) r))
-> m (Step (Is (Either a b)) r (ProcessT m (Either a b) r))
go Async (StM m (MachineStep m (Is b) r))
sr) a
a (ProcessT m a r
 -> m (Step (Is (Either a b)) r (ProcessT m (Either a b) r)))
-> (MachineStep m (Is a) r -> ProcessT m a r)
-> MachineStep m (Is a) r
-> m (Step (Is (Either a b)) r (ProcessT m (Either a b) r))
forall b c a. (b -> c) -> (a -> b) -> a -> c
. MachineStep m (Is a) r -> ProcessT m a r
forall (m :: * -> *) (k :: * -> *) o.
Monad m =>
Step k o (MachineT m k o) -> MachineT m k o
encased
                           Right t
b -> m (Step (Is (Either a b)) r (ProcessT m (Either a b) r))
-> ProcessT m (Either a b) r
forall (m :: * -> *) (k :: * -> *) o.
m (Step k o (MachineT m k o)) -> MachineT m k o
MachineT (m (Step (Is (Either a b)) r (ProcessT m (Either a b) r))
 -> ProcessT m (Either a b) r)
-> m (Step (Is (Either a b)) r (ProcessT m (Either a b) r))
-> ProcessT m (Either a b) r
forall a b. (a -> b) -> a -> b
$ ProcessT m b r -> m (Async (StM m (MachineStep m (Is b) r)))
forall (m :: * -> *) (k :: * -> *) o.
MonadBaseControl IO m =>
MachineT m k o -> m (AsyncStep m k o)
asyncRun (t -> ProcessT m b r
g t
b) m (Async (StM m (MachineStep m (Is b) r)))
-> (Async (StM m (MachineStep m (Is b) r))
    -> m (Step (Is (Either a b)) r (ProcessT m (Either a b) r)))
-> m (Step (Is (Either a b)) r (ProcessT m (Either a b) r))
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= Async (StM m (MachineStep m (Is a) r))
-> Async (StM m (MachineStep m (Is b) r))
-> m (Step (Is (Either a b)) r (ProcessT m (Either a b) r))
go Async (StM m (MachineStep m (Is a) r))
sl)
                  Is (Either a t) (Either a t)
forall a. Is a a
Refl
                  (m (Step (Is (Either a b)) r (ProcessT m (Either a b) r))
-> ProcessT m (Either a b) r
forall (m :: * -> *) (k :: * -> *) o.
m (Step k o (MachineT m k o)) -> MachineT m k o
MachineT (m (Step (Is (Either a b)) r (ProcessT m (Either a b) r))
 -> ProcessT m (Either a b) r)
-> m (Step (Is (Either a b)) r (ProcessT m (Either a b) r))
-> ProcessT m (Either a b) r
forall a b. (a -> b) -> a -> b
$ ProcessT m b r -> m (Async (StM m (MachineStep m (Is b) r)))
forall (m :: * -> *) (k :: * -> *) o.
MonadBaseControl IO m =>
MachineT m k o -> m (AsyncStep m k o)
asyncRun ProcessT m b r
gg m (Async (StM m (MachineStep m (Is b) r)))
-> (Async (StM m (MachineStep m (Is b) r))
    -> m (Step (Is (Either a b)) r (ProcessT m (Either a b) r)))
-> m (Step (Is (Either a b)) r (ProcessT m (Either a b) r))
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= Async (StM m (MachineStep m (Is a) r))
-> Async (StM m (MachineStep m (Is b) r))
-> m (Step (Is (Either a b)) r (ProcessT m (Either a b) r))
go Async (StM m (MachineStep m (Is a) r))
sl)

-- | Similar to 'Control.Arrow.+++': split the input between two
-- processes, retagging and merging their outputs.
--
-- The two processes are run concurrently whenever possible.
splitSum :: forall m a b c d. MonadBaseControl IO m
         => ProcessT m a b -> ProcessT m c d -> ProcessT m (Either a c) (Either b d)
splitSum :: ProcessT m a b
-> ProcessT m c d -> ProcessT m (Either a c) (Either b d)
splitSum ProcessT m a b
snkL ProcessT m c d
snkR = m (Step
     (Is (Either a c))
     (Either b d)
     (ProcessT m (Either a c) (Either b d)))
-> ProcessT m (Either a c) (Either b d)
forall (m :: * -> *) (k :: * -> *) o.
m (Step k o (MachineT m k o)) -> MachineT m k o
MachineT (m (Step
      (Is (Either a c))
      (Either b d)
      (ProcessT m (Either a c) (Either b d)))
 -> ProcessT m (Either a c) (Either b d))
-> m (Step
        (Is (Either a c))
        (Either b d)
        (ProcessT m (Either a c) (Either b d)))
-> ProcessT m (Either a c) (Either b d)
forall a b. (a -> b) -> a -> b
$ do Async (StM m (MachineStep m (Is a) (Either b d)))
sl <- MachineT m (Is a) (Either b d)
-> m (Async (StM m (MachineStep m (Is a) (Either b d))))
forall (m :: * -> *) (k :: * -> *) o.
MonadBaseControl IO m =>
MachineT m k o -> m (AsyncStep m k o)
asyncRun ((b -> Either b d)
-> ProcessT m a b -> MachineT m (Is a) (Either b d)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap b -> Either b d
lft ProcessT m a b
snkL)
                                   Async (StM m (MachineStep m (Is c) (Either b d)))
sr <- MachineT m (Is c) (Either b d)
-> m (Async (StM m (MachineStep m (Is c) (Either b d))))
forall (m :: * -> *) (k :: * -> *) o.
MonadBaseControl IO m =>
MachineT m k o -> m (AsyncStep m k o)
asyncRun ((d -> Either b d)
-> ProcessT m c d -> MachineT m (Is c) (Either b d)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap d -> Either b d
rgt ProcessT m c d
snkR)
                                   Async (StM m (MachineStep m (Is a) (Either b d)))
-> Async (StM m (MachineStep m (Is c) (Either b d)))
-> m (Step
        (Is (Either a c))
        (Either b d)
        (ProcessT m (Either a c) (Either b d)))
go Async (StM m (MachineStep m (Is a) (Either b d)))
sl Async (StM m (MachineStep m (Is c) (Either b d)))
sr
  where lft :: b -> Either b d
        lft :: b -> Either b d
lft = b -> Either b d
forall a b. a -> Either a b
Left
        rgt :: d -> Either b d
        rgt :: d -> Either b d
rgt = d -> Either b d
forall a b. b -> Either a b
Right
        go :: AsyncStep m (Is a) (Either b d)
           -> AsyncStep m (Is c) (Either b d)
           -> m (MachineStep m (Is (Either a c)) (Either b d))
        go :: Async (StM m (MachineStep m (Is a) (Either b d)))
-> Async (StM m (MachineStep m (Is c) (Either b d)))
-> m (Step
        (Is (Either a c))
        (Either b d)
        (ProcessT m (Either a c) (Either b d)))
go Async (StM m (MachineStep m (Is a) (Either b d)))
sl Async (StM m (MachineStep m (Is c) (Either b d)))
sr = Async (StM m (MachineStep m (Is a) (Either b d)))
-> Async (StM m (MachineStep m (Is c) (Either b d)))
-> m (Either
        (MachineStep m (Is a) (Either b d))
        (MachineStep m (Is c) (Either b d)))
forall (m :: * -> *) a b.
MonadBaseControl IO m =>
Async (StM m a) -> Async (StM m b) -> m (Either a b)
waitEither Async (StM m (MachineStep m (Is a) (Either b d)))
sl Async (StM m (MachineStep m (Is c) (Either b d)))
sr m (Either
     (MachineStep m (Is a) (Either b d))
     (MachineStep m (Is c) (Either b d)))
-> (Either
      (MachineStep m (Is a) (Either b d))
      (MachineStep m (Is c) (Either b d))
    -> m (Step
            (Is (Either a c))
            (Either b d)
            (ProcessT m (Either a c) (Either b d))))
-> m (Step
        (Is (Either a c))
        (Either b d)
        (ProcessT m (Either a c) (Either b d)))
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>=
                   \(Either
  (MachineStep m (Is a) (Either b d))
  (MachineStep m (Is c) (Either b d))
s :: Either (MachineStep m (Is a) (Either b d))
                                 (MachineStep m (Is c) (Either b d))) -> case Either
  (MachineStep m (Is a) (Either b d))
  (MachineStep m (Is c) (Either b d))
s of
          Left MachineStep m (Is a) (Either b d)
Stop -> Async (StM m (MachineStep m (Is c) (Either b d)))
-> m (MachineStep m (Is c) (Either b d))
forall (m :: * -> *) a.
MonadBaseControl IO m =>
Async (StM m a) -> m a
wait Async (StM m (MachineStep m (Is c) (Either b d)))
sr m (MachineStep m (Is c) (Either b d))
-> (MachineStep m (Is c) (Either b d)
    -> m (Step
            (Is (Either a c))
            (Either b d)
            (ProcessT m (Either a c) (Either b d))))
-> m (Step
        (Is (Either a c))
        (Either b d)
        (ProcessT m (Either a c) (Either b d)))
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= ProcessT m (Either a c) (Either b d)
-> m (Step
        (Is (Either a c))
        (Either b d)
        (ProcessT m (Either a c) (Either b d)))
forall (m :: * -> *) (k :: * -> *) o.
MachineT m k o -> m (Step k o (MachineT m k o))
runMachineT (ProcessT m (Either a c) (Either b d)
 -> m (Step
         (Is (Either a c))
         (Either b d)
         (ProcessT m (Either a c) (Either b d))))
-> (MachineStep m (Is c) (Either b d)
    -> ProcessT m (Either a c) (Either b d))
-> MachineStep m (Is c) (Either b d)
-> m (Step
        (Is (Either a c))
        (Either b d)
        (ProcessT m (Either a c) (Either b d)))
forall b c a. (b -> c) -> (a -> b) -> a -> c
. MachineT m (Is c) (Either b d)
-> ProcessT m (Either a c) (Either b d)
forall (m :: * -> *) b r a.
Monad m =>
ProcessT m b r -> ProcessT m (Either a b) r
rightOnly (MachineT m (Is c) (Either b d)
 -> ProcessT m (Either a c) (Either b d))
-> (MachineStep m (Is c) (Either b d)
    -> MachineT m (Is c) (Either b d))
-> MachineStep m (Is c) (Either b d)
-> ProcessT m (Either a c) (Either b d)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. MachineStep m (Is c) (Either b d) -> MachineT m (Is c) (Either b d)
forall (m :: * -> *) (k :: * -> *) o.
Monad m =>
Step k o (MachineT m k o) -> MachineT m k o
encased
          Right MachineStep m (Is c) (Either b d)
Stop -> Async (StM m (MachineStep m (Is a) (Either b d)))
-> m (MachineStep m (Is a) (Either b d))
forall (m :: * -> *) a.
MonadBaseControl IO m =>
Async (StM m a) -> m a
wait Async (StM m (MachineStep m (Is a) (Either b d)))
sl m (MachineStep m (Is a) (Either b d))
-> (MachineStep m (Is a) (Either b d)
    -> m (Step
            (Is (Either a c))
            (Either b d)
            (ProcessT m (Either a c) (Either b d))))
-> m (Step
        (Is (Either a c))
        (Either b d)
        (ProcessT m (Either a c) (Either b d)))
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= ProcessT m (Either a c) (Either b d)
-> m (Step
        (Is (Either a c))
        (Either b d)
        (ProcessT m (Either a c) (Either b d)))
forall (m :: * -> *) (k :: * -> *) o.
MachineT m k o -> m (Step k o (MachineT m k o))
runMachineT (ProcessT m (Either a c) (Either b d)
 -> m (Step
         (Is (Either a c))
         (Either b d)
         (ProcessT m (Either a c) (Either b d))))
-> (MachineStep m (Is a) (Either b d)
    -> ProcessT m (Either a c) (Either b d))
-> MachineStep m (Is a) (Either b d)
-> m (Step
        (Is (Either a c))
        (Either b d)
        (ProcessT m (Either a c) (Either b d)))
forall b c a. (b -> c) -> (a -> b) -> a -> c
. MachineT m (Is a) (Either b d)
-> ProcessT m (Either a c) (Either b d)
forall (m :: * -> *) a r b.
Monad m =>
ProcessT m a r -> ProcessT m (Either a b) r
leftOnly (MachineT m (Is a) (Either b d)
 -> ProcessT m (Either a c) (Either b d))
-> (MachineStep m (Is a) (Either b d)
    -> MachineT m (Is a) (Either b d))
-> MachineStep m (Is a) (Either b d)
-> ProcessT m (Either a c) (Either b d)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. MachineStep m (Is a) (Either b d) -> MachineT m (Is a) (Either b d)
forall (m :: * -> *) (k :: * -> *) o.
Monad m =>
Step k o (MachineT m k o) -> MachineT m k o
encased

          Left (Yield Either b d
o MachineT m (Is a) (Either b d)
k) -> 
            Step
  (Is (Either a c))
  (Either b d)
  (ProcessT m (Either a c) (Either b d))
-> m (Step
        (Is (Either a c))
        (Either b d)
        (ProcessT m (Either a c) (Either b d)))
forall (m :: * -> *) a. Monad m => a -> m a
return (Step
   (Is (Either a c))
   (Either b d)
   (ProcessT m (Either a c) (Either b d))
 -> m (Step
         (Is (Either a c))
         (Either b d)
         (ProcessT m (Either a c) (Either b d))))
-> (m (Step
         (Is (Either a c))
         (Either b d)
         (ProcessT m (Either a c) (Either b d)))
    -> Step
         (Is (Either a c))
         (Either b d)
         (ProcessT m (Either a c) (Either b d)))
-> m (Step
        (Is (Either a c))
        (Either b d)
        (ProcessT m (Either a c) (Either b d)))
-> m (Step
        (Is (Either a c))
        (Either b d)
        (ProcessT m (Either a c) (Either b d)))
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Either b d
-> ProcessT m (Either a c) (Either b d)
-> Step
     (Is (Either a c))
     (Either b d)
     (ProcessT m (Either a c) (Either b d))
forall (k :: * -> *) o r. o -> r -> Step k o r
Yield Either b d
o (ProcessT m (Either a c) (Either b d)
 -> Step
      (Is (Either a c))
      (Either b d)
      (ProcessT m (Either a c) (Either b d)))
-> (m (Step
         (Is (Either a c))
         (Either b d)
         (ProcessT m (Either a c) (Either b d)))
    -> ProcessT m (Either a c) (Either b d))
-> m (Step
        (Is (Either a c))
        (Either b d)
        (ProcessT m (Either a c) (Either b d)))
-> Step
     (Is (Either a c))
     (Either b d)
     (ProcessT m (Either a c) (Either b d))
forall b c a. (b -> c) -> (a -> b) -> a -> c
. m (Step
     (Is (Either a c))
     (Either b d)
     (ProcessT m (Either a c) (Either b d)))
-> ProcessT m (Either a c) (Either b d)
forall (m :: * -> *) (k :: * -> *) o.
m (Step k o (MachineT m k o)) -> MachineT m k o
MachineT (m (Step
      (Is (Either a c))
      (Either b d)
      (ProcessT m (Either a c) (Either b d)))
 -> m (Step
         (Is (Either a c))
         (Either b d)
         (ProcessT m (Either a c) (Either b d))))
-> m (Step
        (Is (Either a c))
        (Either b d)
        (ProcessT m (Either a c) (Either b d)))
-> m (Step
        (Is (Either a c))
        (Either b d)
        (ProcessT m (Either a c) (Either b d)))
forall a b. (a -> b) -> a -> b
$ MachineT m (Is a) (Either b d)
-> m (Async (StM m (MachineStep m (Is a) (Either b d))))
forall (m :: * -> *) (k :: * -> *) o.
MonadBaseControl IO m =>
MachineT m k o -> m (AsyncStep m k o)
asyncRun MachineT m (Is a) (Either b d)
k m (Async (StM m (MachineStep m (Is a) (Either b d))))
-> (Async (StM m (MachineStep m (Is a) (Either b d)))
    -> m (Step
            (Is (Either a c))
            (Either b d)
            (ProcessT m (Either a c) (Either b d))))
-> m (Step
        (Is (Either a c))
        (Either b d)
        (ProcessT m (Either a c) (Either b d)))
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= (Async (StM m (MachineStep m (Is a) (Either b d)))
 -> Async (StM m (MachineStep m (Is c) (Either b d)))
 -> m (Step
         (Is (Either a c))
         (Either b d)
         (ProcessT m (Either a c) (Either b d))))
-> Async (StM m (MachineStep m (Is c) (Either b d)))
-> Async (StM m (MachineStep m (Is a) (Either b d)))
-> m (Step
        (Is (Either a c))
        (Either b d)
        (ProcessT m (Either a c) (Either b d)))
forall a b c. (a -> b -> c) -> b -> a -> c
flip Async (StM m (MachineStep m (Is a) (Either b d)))
-> Async (StM m (MachineStep m (Is c) (Either b d)))
-> m (Step
        (Is (Either a c))
        (Either b d)
        (ProcessT m (Either a c) (Either b d)))
go Async (StM m (MachineStep m (Is c) (Either b d)))
sr
          Right (Yield Either b d
o MachineT m (Is c) (Either b d)
k) -> 
            Step
  (Is (Either a c))
  (Either b d)
  (ProcessT m (Either a c) (Either b d))
-> m (Step
        (Is (Either a c))
        (Either b d)
        (ProcessT m (Either a c) (Either b d)))
forall (m :: * -> *) a. Monad m => a -> m a
return (Step
   (Is (Either a c))
   (Either b d)
   (ProcessT m (Either a c) (Either b d))
 -> m (Step
         (Is (Either a c))
         (Either b d)
         (ProcessT m (Either a c) (Either b d))))
-> (m (Step
         (Is (Either a c))
         (Either b d)
         (ProcessT m (Either a c) (Either b d)))
    -> Step
         (Is (Either a c))
         (Either b d)
         (ProcessT m (Either a c) (Either b d)))
-> m (Step
        (Is (Either a c))
        (Either b d)
        (ProcessT m (Either a c) (Either b d)))
-> m (Step
        (Is (Either a c))
        (Either b d)
        (ProcessT m (Either a c) (Either b d)))
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Either b d
-> ProcessT m (Either a c) (Either b d)
-> Step
     (Is (Either a c))
     (Either b d)
     (ProcessT m (Either a c) (Either b d))
forall (k :: * -> *) o r. o -> r -> Step k o r
Yield Either b d
o (ProcessT m (Either a c) (Either b d)
 -> Step
      (Is (Either a c))
      (Either b d)
      (ProcessT m (Either a c) (Either b d)))
-> (m (Step
         (Is (Either a c))
         (Either b d)
         (ProcessT m (Either a c) (Either b d)))
    -> ProcessT m (Either a c) (Either b d))
-> m (Step
        (Is (Either a c))
        (Either b d)
        (ProcessT m (Either a c) (Either b d)))
-> Step
     (Is (Either a c))
     (Either b d)
     (ProcessT m (Either a c) (Either b d))
forall b c a. (b -> c) -> (a -> b) -> a -> c
. m (Step
     (Is (Either a c))
     (Either b d)
     (ProcessT m (Either a c) (Either b d)))
-> ProcessT m (Either a c) (Either b d)
forall (m :: * -> *) (k :: * -> *) o.
m (Step k o (MachineT m k o)) -> MachineT m k o
MachineT (m (Step
      (Is (Either a c))
      (Either b d)
      (ProcessT m (Either a c) (Either b d)))
 -> m (Step
         (Is (Either a c))
         (Either b d)
         (ProcessT m (Either a c) (Either b d))))
-> m (Step
        (Is (Either a c))
        (Either b d)
        (ProcessT m (Either a c) (Either b d)))
-> m (Step
        (Is (Either a c))
        (Either b d)
        (ProcessT m (Either a c) (Either b d)))
forall a b. (a -> b) -> a -> b
$ MachineT m (Is c) (Either b d)
-> m (Async (StM m (MachineStep m (Is c) (Either b d))))
forall (m :: * -> *) (k :: * -> *) o.
MonadBaseControl IO m =>
MachineT m k o -> m (AsyncStep m k o)
asyncRun MachineT m (Is c) (Either b d)
k m (Async (StM m (MachineStep m (Is c) (Either b d))))
-> (Async (StM m (MachineStep m (Is c) (Either b d)))
    -> m (Step
            (Is (Either a c))
            (Either b d)
            (ProcessT m (Either a c) (Either b d))))
-> m (Step
        (Is (Either a c))
        (Either b d)
        (ProcessT m (Either a c) (Either b d)))
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= Async (StM m (MachineStep m (Is a) (Either b d)))
-> Async (StM m (MachineStep m (Is c) (Either b d)))
-> m (Step
        (Is (Either a c))
        (Either b d)
        (ProcessT m (Either a c) (Either b d)))
go Async (StM m (MachineStep m (Is a) (Either b d)))
sl
                               
          Left (Await t -> MachineT m (Is a) (Either b d)
f Is a t
Refl MachineT m (Is a) (Either b d)
ff) ->
            Step
  (Is (Either t c))
  (Either b d)
  (ProcessT m (Either a c) (Either b d))
-> m (Step
        (Is (Either t c))
        (Either b d)
        (ProcessT m (Either a c) (Either b d)))
forall (m :: * -> *) a. Monad m => a -> m a
return (Step
   (Is (Either t c))
   (Either b d)
   (ProcessT m (Either a c) (Either b d))
 -> m (Step
         (Is (Either t c))
         (Either b d)
         (ProcessT m (Either a c) (Either b d))))
-> Step
     (Is (Either t c))
     (Either b d)
     (ProcessT m (Either a c) (Either b d))
-> m (Step
        (Is (Either t c))
        (Either b d)
        (ProcessT m (Either a c) (Either b d)))
forall a b. (a -> b) -> a -> b
$ 
            (Either t c -> ProcessT m (Either a c) (Either b d))
-> Is (Either t c) (Either t c)
-> ProcessT m (Either a c) (Either b d)
-> Step
     (Is (Either t c))
     (Either b d)
     (ProcessT m (Either a c) (Either b d))
forall (k :: * -> *) o r t. (t -> r) -> k t -> r -> Step k o r
Await (\Either t c
u -> case Either t c
u of
                           Left t
a -> m (Step
     (Is (Either a c))
     (Either b d)
     (ProcessT m (Either a c) (Either b d)))
-> ProcessT m (Either a c) (Either b d)
forall (m :: * -> *) (k :: * -> *) o.
m (Step k o (MachineT m k o)) -> MachineT m k o
MachineT (m (Step
      (Is (Either a c))
      (Either b d)
      (ProcessT m (Either a c) (Either b d)))
 -> ProcessT m (Either a c) (Either b d))
-> m (Step
        (Is (Either a c))
        (Either b d)
        (ProcessT m (Either a c) (Either b d)))
-> ProcessT m (Either a c) (Either b d)
forall a b. (a -> b) -> a -> b
$ MachineT m (Is a) (Either b d)
-> m (Async (StM m (MachineStep m (Is a) (Either b d))))
forall (m :: * -> *) (k :: * -> *) o.
MonadBaseControl IO m =>
MachineT m k o -> m (AsyncStep m k o)
asyncRun (t -> MachineT m (Is a) (Either b d)
f t
a) m (Async (StM m (MachineStep m (Is a) (Either b d))))
-> (Async (StM m (MachineStep m (Is a) (Either b d)))
    -> m (Step
            (Is (Either a c))
            (Either b d)
            (ProcessT m (Either a c) (Either b d))))
-> m (Step
        (Is (Either a c))
        (Either b d)
        (ProcessT m (Either a c) (Either b d)))
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= (Async (StM m (MachineStep m (Is a) (Either b d)))
 -> Async (StM m (MachineStep m (Is c) (Either b d)))
 -> m (Step
         (Is (Either a c))
         (Either b d)
         (ProcessT m (Either a c) (Either b d))))
-> Async (StM m (MachineStep m (Is c) (Either b d)))
-> Async (StM m (MachineStep m (Is a) (Either b d)))
-> m (Step
        (Is (Either a c))
        (Either b d)
        (ProcessT m (Either a c) (Either b d)))
forall a b c. (a -> b -> c) -> b -> a -> c
flip Async (StM m (MachineStep m (Is a) (Either b d)))
-> Async (StM m (MachineStep m (Is c) (Either b d)))
-> m (Step
        (Is (Either a c))
        (Either b d)
        (ProcessT m (Either a c) (Either b d)))
go Async (StM m (MachineStep m (Is c) (Either b d)))
sr
                           Right c
b -> m (Step
     (Is (Either a c))
     (Either b d)
     (ProcessT m (Either a c) (Either b d)))
-> ProcessT m (Either a c) (Either b d)
forall (m :: * -> *) (k :: * -> *) o.
m (Step k o (MachineT m k o)) -> MachineT m k o
MachineT (m (Step
      (Is (Either a c))
      (Either b d)
      (ProcessT m (Either a c) (Either b d)))
 -> ProcessT m (Either a c) (Either b d))
-> m (Step
        (Is (Either a c))
        (Either b d)
        (ProcessT m (Either a c) (Either b d)))
-> ProcessT m (Either a c) (Either b d)
forall a b. (a -> b) -> a -> b
$ 
                                      Async (StM m (MachineStep m (Is c) (Either b d)))
-> m (MachineStep m (Is c) (Either b d))
forall (m :: * -> *) a.
MonadBaseControl IO m =>
Async (StM m a) -> m a
wait Async (StM m (MachineStep m (Is c) (Either b d)))
sr m (MachineStep m (Is c) (Either b d))
-> (MachineStep m (Is c) (Either b d)
    -> m (Step
            (Is (Either a c))
            (Either b d)
            (ProcessT m (Either a c) (Either b d))))
-> m (Step
        (Is (Either a c))
        (Either b d)
        (ProcessT m (Either a c) (Either b d)))
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= (Async (StM m (MachineStep m (Is c) (Either b d)))
 -> m (Step
         (Is (Either a c))
         (Either b d)
         (ProcessT m (Either a c) (Either b d))))
-> c
-> MachineT m (Is c) (Either b d)
-> m (Step
        (Is (Either a c))
        (Either b d)
        (ProcessT m (Either a c) (Either b d)))
forall (m :: * -> *) a (k :: * -> *) b.
MonadBaseControl IO m =>
(AsyncStep m (Is a) b -> m (MachineStep m k b))
-> a -> ProcessT m a b -> m (MachineStep m k b)
forceFeed (Async (StM m (MachineStep m (Is a) (Either b d)))
-> Async (StM m (MachineStep m (Is c) (Either b d)))
-> m (Step
        (Is (Either a c))
        (Either b d)
        (ProcessT m (Either a c) (Either b d)))
go Async (StM m (MachineStep m (Is a) (Either b d)))
sl) c
b (MachineT m (Is c) (Either b d)
 -> m (Step
         (Is (Either a c))
         (Either b d)
         (ProcessT m (Either a c) (Either b d))))
-> (MachineStep m (Is c) (Either b d)
    -> MachineT m (Is c) (Either b d))
-> MachineStep m (Is c) (Either b d)
-> m (Step
        (Is (Either a c))
        (Either b d)
        (ProcessT m (Either a c) (Either b d)))
forall b c a. (b -> c) -> (a -> b) -> a -> c
. MachineStep m (Is c) (Either b d) -> MachineT m (Is c) (Either b d)
forall (m :: * -> *) (k :: * -> *) o.
Monad m =>
Step k o (MachineT m k o) -> MachineT m k o
encased)
                  Is (Either t c) (Either t c)
forall a. Is a a
Refl
                  (m (Step
     (Is (Either a c))
     (Either b d)
     (ProcessT m (Either a c) (Either b d)))
-> ProcessT m (Either a c) (Either b d)
forall (m :: * -> *) (k :: * -> *) o.
m (Step k o (MachineT m k o)) -> MachineT m k o
MachineT (m (Step
      (Is (Either a c))
      (Either b d)
      (ProcessT m (Either a c) (Either b d)))
 -> ProcessT m (Either a c) (Either b d))
-> m (Step
        (Is (Either a c))
        (Either b d)
        (ProcessT m (Either a c) (Either b d)))
-> ProcessT m (Either a c) (Either b d)
forall a b. (a -> b) -> a -> b
$ MachineT m (Is a) (Either b d)
-> m (Async (StM m (MachineStep m (Is a) (Either b d))))
forall (m :: * -> *) (k :: * -> *) o.
MonadBaseControl IO m =>
MachineT m k o -> m (AsyncStep m k o)
asyncRun MachineT m (Is a) (Either b d)
ff m (Async (StM m (MachineStep m (Is a) (Either b d))))
-> (Async (StM m (MachineStep m (Is a) (Either b d)))
    -> m (Step
            (Is (Either a c))
            (Either b d)
            (ProcessT m (Either a c) (Either b d))))
-> m (Step
        (Is (Either a c))
        (Either b d)
        (ProcessT m (Either a c) (Either b d)))
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= (Async (StM m (MachineStep m (Is a) (Either b d)))
 -> Async (StM m (MachineStep m (Is c) (Either b d)))
 -> m (Step
         (Is (Either a c))
         (Either b d)
         (ProcessT m (Either a c) (Either b d))))
-> Async (StM m (MachineStep m (Is c) (Either b d)))
-> Async (StM m (MachineStep m (Is a) (Either b d)))
-> m (Step
        (Is (Either a c))
        (Either b d)
        (ProcessT m (Either a c) (Either b d)))
forall a b c. (a -> b -> c) -> b -> a -> c
flip Async (StM m (MachineStep m (Is a) (Either b d)))
-> Async (StM m (MachineStep m (Is c) (Either b d)))
-> m (Step
        (Is (Either a c))
        (Either b d)
        (ProcessT m (Either a c) (Either b d)))
go Async (StM m (MachineStep m (Is c) (Either b d)))
sr)
          Right (Await t -> MachineT m (Is c) (Either b d)
g Is c t
Refl MachineT m (Is c) (Either b d)
gg) -> Step
  (Is (Either a t))
  (Either b d)
  (ProcessT m (Either a c) (Either b d))
-> m (Step
        (Is (Either a t))
        (Either b d)
        (ProcessT m (Either a c) (Either b d)))
forall (m :: * -> *) a. Monad m => a -> m a
return (Step
   (Is (Either a t))
   (Either b d)
   (ProcessT m (Either a c) (Either b d))
 -> m (Step
         (Is (Either a t))
         (Either b d)
         (ProcessT m (Either a c) (Either b d))))
-> Step
     (Is (Either a t))
     (Either b d)
     (ProcessT m (Either a c) (Either b d))
-> m (Step
        (Is (Either a t))
        (Either b d)
        (ProcessT m (Either a c) (Either b d)))
forall a b. (a -> b) -> a -> b
$
            (Either a t -> ProcessT m (Either a c) (Either b d))
-> Is (Either a t) (Either a t)
-> ProcessT m (Either a c) (Either b d)
-> Step
     (Is (Either a t))
     (Either b d)
     (ProcessT m (Either a c) (Either b d))
forall (k :: * -> *) o r t. (t -> r) -> k t -> r -> Step k o r
Await (\Either a t
u -> case Either a t
u of
                           Left a
a -> 
                             m (Step
     (Is (Either a c))
     (Either b d)
     (ProcessT m (Either a c) (Either b d)))
-> ProcessT m (Either a c) (Either b d)
forall (m :: * -> *) (k :: * -> *) o.
m (Step k o (MachineT m k o)) -> MachineT m k o
MachineT (m (Step
      (Is (Either a c))
      (Either b d)
      (ProcessT m (Either a c) (Either b d)))
 -> ProcessT m (Either a c) (Either b d))
-> m (Step
        (Is (Either a c))
        (Either b d)
        (ProcessT m (Either a c) (Either b d)))
-> ProcessT m (Either a c) (Either b d)
forall a b. (a -> b) -> a -> b
$
                             Async (StM m (MachineStep m (Is a) (Either b d)))
-> m (MachineStep m (Is a) (Either b d))
forall (m :: * -> *) a.
MonadBaseControl IO m =>
Async (StM m a) -> m a
wait Async (StM m (MachineStep m (Is a) (Either b d)))
sl m (MachineStep m (Is a) (Either b d))
-> (MachineStep m (Is a) (Either b d)
    -> m (Step
            (Is (Either a c))
            (Either b d)
            (ProcessT m (Either a c) (Either b d))))
-> m (Step
        (Is (Either a c))
        (Either b d)
        (ProcessT m (Either a c) (Either b d)))
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= (Async (StM m (MachineStep m (Is a) (Either b d)))
 -> m (Step
         (Is (Either a c))
         (Either b d)
         (ProcessT m (Either a c) (Either b d))))
-> a
-> MachineT m (Is a) (Either b d)
-> m (Step
        (Is (Either a c))
        (Either b d)
        (ProcessT m (Either a c) (Either b d)))
forall (m :: * -> *) a (k :: * -> *) b.
MonadBaseControl IO m =>
(AsyncStep m (Is a) b -> m (MachineStep m k b))
-> a -> ProcessT m a b -> m (MachineStep m k b)
forceFeed ((Async (StM m (MachineStep m (Is a) (Either b d)))
 -> Async (StM m (MachineStep m (Is c) (Either b d)))
 -> m (Step
         (Is (Either a c))
         (Either b d)
         (ProcessT m (Either a c) (Either b d))))
-> Async (StM m (MachineStep m (Is c) (Either b d)))
-> Async (StM m (MachineStep m (Is a) (Either b d)))
-> m (Step
        (Is (Either a c))
        (Either b d)
        (ProcessT m (Either a c) (Either b d)))
forall a b c. (a -> b -> c) -> b -> a -> c
flip Async (StM m (MachineStep m (Is a) (Either b d)))
-> Async (StM m (MachineStep m (Is c) (Either b d)))
-> m (Step
        (Is (Either a c))
        (Either b d)
        (ProcessT m (Either a c) (Either b d)))
go Async (StM m (MachineStep m (Is c) (Either b d)))
sr) a
a (MachineT m (Is a) (Either b d)
 -> m (Step
         (Is (Either a c))
         (Either b d)
         (ProcessT m (Either a c) (Either b d))))
-> (MachineStep m (Is a) (Either b d)
    -> MachineT m (Is a) (Either b d))
-> MachineStep m (Is a) (Either b d)
-> m (Step
        (Is (Either a c))
        (Either b d)
        (ProcessT m (Either a c) (Either b d)))
forall b c a. (b -> c) -> (a -> b) -> a -> c
. MachineStep m (Is a) (Either b d) -> MachineT m (Is a) (Either b d)
forall (m :: * -> *) (k :: * -> *) o.
Monad m =>
Step k o (MachineT m k o) -> MachineT m k o
encased
                           Right t
b -> m (Step
     (Is (Either a c))
     (Either b d)
     (ProcessT m (Either a c) (Either b d)))
-> ProcessT m (Either a c) (Either b d)
forall (m :: * -> *) (k :: * -> *) o.
m (Step k o (MachineT m k o)) -> MachineT m k o
MachineT (m (Step
      (Is (Either a c))
      (Either b d)
      (ProcessT m (Either a c) (Either b d)))
 -> ProcessT m (Either a c) (Either b d))
-> m (Step
        (Is (Either a c))
        (Either b d)
        (ProcessT m (Either a c) (Either b d)))
-> ProcessT m (Either a c) (Either b d)
forall a b. (a -> b) -> a -> b
$ MachineT m (Is c) (Either b d)
-> m (Async (StM m (MachineStep m (Is c) (Either b d))))
forall (m :: * -> *) (k :: * -> *) o.
MonadBaseControl IO m =>
MachineT m k o -> m (AsyncStep m k o)
asyncRun (t -> MachineT m (Is c) (Either b d)
g t
b) m (Async (StM m (MachineStep m (Is c) (Either b d))))
-> (Async (StM m (MachineStep m (Is c) (Either b d)))
    -> m (Step
            (Is (Either a c))
            (Either b d)
            (ProcessT m (Either a c) (Either b d))))
-> m (Step
        (Is (Either a c))
        (Either b d)
        (ProcessT m (Either a c) (Either b d)))
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= Async (StM m (MachineStep m (Is a) (Either b d)))
-> Async (StM m (MachineStep m (Is c) (Either b d)))
-> m (Step
        (Is (Either a c))
        (Either b d)
        (ProcessT m (Either a c) (Either b d)))
go Async (StM m (MachineStep m (Is a) (Either b d)))
sl)
                  Is (Either a t) (Either a t)
forall a. Is a a
Refl
                  (m (Step
     (Is (Either a c))
     (Either b d)
     (ProcessT m (Either a c) (Either b d)))
-> ProcessT m (Either a c) (Either b d)
forall (m :: * -> *) (k :: * -> *) o.
m (Step k o (MachineT m k o)) -> MachineT m k o
MachineT (m (Step
      (Is (Either a c))
      (Either b d)
      (ProcessT m (Either a c) (Either b d)))
 -> ProcessT m (Either a c) (Either b d))
-> m (Step
        (Is (Either a c))
        (Either b d)
        (ProcessT m (Either a c) (Either b d)))
-> ProcessT m (Either a c) (Either b d)
forall a b. (a -> b) -> a -> b
$ MachineT m (Is c) (Either b d)
-> m (Async (StM m (MachineStep m (Is c) (Either b d))))
forall (m :: * -> *) (k :: * -> *) o.
MonadBaseControl IO m =>
MachineT m k o -> m (AsyncStep m k o)
asyncRun MachineT m (Is c) (Either b d)
gg m (Async (StM m (MachineStep m (Is c) (Either b d))))
-> (Async (StM m (MachineStep m (Is c) (Either b d)))
    -> m (Step
            (Is (Either a c))
            (Either b d)
            (ProcessT m (Either a c) (Either b d))))
-> m (Step
        (Is (Either a c))
        (Either b d)
        (ProcessT m (Either a c) (Either b d)))
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= Async (StM m (MachineStep m (Is a) (Either b d)))
-> Async (StM m (MachineStep m (Is c) (Either b d)))
-> m (Step
        (Is (Either a c))
        (Either b d)
        (ProcessT m (Either a c) (Either b d)))
go Async (StM m (MachineStep m (Is a) (Either b d)))
sl)

-- | @forceFeed k x p@ runs machine @p@ until it awaits, at which
-- point it is fed @x@. The result of that feeding is asynchronously
-- run, and supplied to the continuation @k@.
forceFeed :: forall m a k b. MonadBaseControl IO m
          => (AsyncStep m (Is a) b -> m (MachineStep m k b))
          -> a
          -> ProcessT m a b
          -> m (MachineStep m k b)
forceFeed :: (AsyncStep m (Is a) b -> m (MachineStep m k b))
-> a -> ProcessT m a b -> m (MachineStep m k b)
forceFeed AsyncStep m (Is a) b -> m (MachineStep m k b)
go a
x = ProcessT m a b -> m (MachineStep m k b)
aux
  where aux :: ProcessT m a b -> m (MachineStep m k b)
aux ProcessT m a b
p = ProcessT m a b -> m (Step (Is a) b (ProcessT m a b))
forall (m :: * -> *) (k :: * -> *) o.
MachineT m k o -> m (Step k o (MachineT m k o))
runMachineT ProcessT m a b
p m (Step (Is a) b (ProcessT m a b))
-> (Step (Is a) b (ProcessT m a b) -> m (MachineStep m k b))
-> m (MachineStep m k b)
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \Step (Is a) b (ProcessT m a b)
v -> case Step (Is a) b (ProcessT m a b)
v of
          -- Stop -> asyncRun stopped >>= go
          Step (Is a) b (ProcessT m a b)
Stop -> MachineStep m k b -> m (MachineStep m k b)
forall (m :: * -> *) a. Monad m => a -> m a
return MachineStep m k b
forall (k :: * -> *) o r. Step k o r
Stop
          Yield b
o ProcessT m a b
k -> MachineStep m k b -> m (MachineStep m k b)
forall (m :: * -> *) a. Monad m => a -> m a
return (MachineStep m k b -> m (MachineStep m k b))
-> (m (MachineStep m k b) -> MachineStep m k b)
-> m (MachineStep m k b)
-> m (MachineStep m k b)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. b -> MachineT m k b -> MachineStep m k b
forall (k :: * -> *) o r. o -> r -> Step k o r
Yield b
o (MachineT m k b -> MachineStep m k b)
-> (m (MachineStep m k b) -> MachineT m k b)
-> m (MachineStep m k b)
-> MachineStep m k b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. m (MachineStep m k b) -> MachineT m k b
forall (m :: * -> *) (k :: * -> *) o.
m (Step k o (MachineT m k o)) -> MachineT m k o
MachineT (m (MachineStep m k b) -> m (MachineStep m k b))
-> m (MachineStep m k b) -> m (MachineStep m k b)
forall a b. (a -> b) -> a -> b
$ ProcessT m a b -> m (MachineStep m k b)
aux ProcessT m a b
k
          Await t -> ProcessT m a b
f Is a t
Refl ProcessT m a b
_ -> ProcessT m a b -> m (AsyncStep m (Is a) b)
forall (m :: * -> *) (k :: * -> *) o.
MonadBaseControl IO m =>
MachineT m k o -> m (AsyncStep m k o)
asyncRun (t -> ProcessT m a b
f a
t
x) m (AsyncStep m (Is a) b)
-> (AsyncStep m (Is a) b -> m (MachineStep m k b))
-> m (MachineStep m k b)
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= AsyncStep m (Is a) b -> m (MachineStep m k b)
go

-- | We have a sink for the Right output of a source, so we want to
-- keep running it as long as upstream does not yield a 'Left' which
-- we can not handle. When upstream yields a 'Left', we 'stop'.
rightOnly :: Monad m => ProcessT m b r -> ProcessT m (Either a b) r
rightOnly :: ProcessT m b r -> ProcessT m (Either a b) r
rightOnly ProcessT m b r
snk = PlanT (Is (Either a b)) b m () -> MachineT m (Is (Either a b)) b
forall (m :: * -> *) (k :: * -> *) o a.
Monad m =>
PlanT k o m a -> MachineT m k o
repeatedly (PlanT (Is (Either a b)) b m (Either a b)
forall (k :: * -> * -> *) i o. Category k => Plan (k i) o i
await PlanT (Is (Either a b)) b m (Either a b)
-> (Either a b -> PlanT (Is (Either a b)) b m ())
-> PlanT (Is (Either a b)) b m ()
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= (a -> PlanT (Is (Either a b)) b m ())
-> (b -> PlanT (Is (Either a b)) b m ())
-> Either a b
-> PlanT (Is (Either a b)) b m ()
forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either (PlanT (Is (Either a b)) b m ()
-> a -> PlanT (Is (Either a b)) b m ()
forall a b. a -> b -> a
const PlanT (Is (Either a b)) b m ()
forall (k :: * -> *) o a. Plan k o a
stop) (\b
x -> b -> Plan (Is (Either a b)) b ()
forall o (k :: * -> *). o -> Plan k o ()
yield b
x)) MachineT m (Is (Either a b)) b
-> ProcessT m b r -> ProcessT m (Either a b) r
forall (m :: * -> *) (k :: * -> *) b c.
Monad m =>
MachineT m k b -> ProcessT m b c -> MachineT m k c
~> ProcessT m b r
snk

-- | We have a sink for the Left output of a source, so we want to
-- keep running it as long as upstream does not yield a 'Right' which
-- we can not handle. When upstream yields a 'Right', we 'stop'.
leftOnly :: Monad m => ProcessT m a r -> ProcessT m (Either a b) r
leftOnly :: ProcessT m a r -> ProcessT m (Either a b) r
leftOnly ProcessT m a r
snk = PlanT (Is (Either a b)) a m () -> MachineT m (Is (Either a b)) a
forall (m :: * -> *) (k :: * -> *) o a.
Monad m =>
PlanT k o m a -> MachineT m k o
repeatedly (PlanT (Is (Either a b)) a m (Either a b)
forall (k :: * -> * -> *) i o. Category k => Plan (k i) o i
await PlanT (Is (Either a b)) a m (Either a b)
-> (Either a b -> PlanT (Is (Either a b)) a m ())
-> PlanT (Is (Either a b)) a m ()
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= (a -> PlanT (Is (Either a b)) a m ())
-> (b -> PlanT (Is (Either a b)) a m ())
-> Either a b
-> PlanT (Is (Either a b)) a m ()
forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either (\a
x -> a -> Plan (Is (Either a b)) a ()
forall o (k :: * -> *). o -> Plan k o ()
yield a
x) (PlanT (Is (Either a b)) a m ()
-> b -> PlanT (Is (Either a b)) a m ()
forall a b. a -> b -> a
const PlanT (Is (Either a b)) a m ()
forall (k :: * -> *) o a. Plan k o a
stop)) MachineT m (Is (Either a b)) a
-> ProcessT m a r -> ProcessT m (Either a b) r
forall (m :: * -> *) (k :: * -> *) b c.
Monad m =>
MachineT m k b -> ProcessT m b c -> MachineT m k c
~> ProcessT m a r
snk

-- | Connect two processes to the downstream tails of a 'Machine' that
-- produces tuples. The two downstream consumers are run
-- concurrently. When one downstream consumer stops, the entire
-- pipeline is stopped.
--
-- @splitProd sink1 sink2@ produces a topology like this,
--
-- @
--                            sink1
--                           /      \\
--                         a          \\
--                        /            \\
--    source -- (a,b) -->               -- r -->
--                        \\            /
--                         b         /
--                           \\     /
--                            sink2 
-- @
splitProd :: forall m a b r. MonadBaseControl IO m
          => ProcessT m a r -> ProcessT m b r -> ProcessT m (a,b) r
splitProd :: ProcessT m a r -> ProcessT m b r -> ProcessT m (a, b) r
splitProd ProcessT m a r
snk1 ProcessT m b r
snk2 = m (Step (Is (a, b)) r (ProcessT m (a, b) r)) -> ProcessT m (a, b) r
forall (m :: * -> *) (k :: * -> *) o.
m (Step k o (MachineT m k o)) -> MachineT m k o
MachineT (m (Step (Is (a, b)) r (ProcessT m (a, b) r))
 -> ProcessT m (a, b) r)
-> m (Step (Is (a, b)) r (ProcessT m (a, b) r))
-> ProcessT m (a, b) r
forall a b. (a -> b) -> a -> b
$ do Async (StM m (MachineStep m (Is a) r))
s1 <- ProcessT m a r -> m (Async (StM m (MachineStep m (Is a) r)))
forall (m :: * -> *) (k :: * -> *) o.
MonadBaseControl IO m =>
MachineT m k o -> m (AsyncStep m k o)
asyncRun ProcessT m a r
snk1
                                    Async (StM m (MachineStep m (Is b) r))
s2 <- ProcessT m b r -> m (Async (StM m (MachineStep m (Is b) r)))
forall (m :: * -> *) (k :: * -> *) o.
MonadBaseControl IO m =>
MachineT m k o -> m (AsyncStep m k o)
asyncRun ProcessT m b r
snk2
                                    Async (StM m (MachineStep m (Is a) r))
-> Async (StM m (MachineStep m (Is b) r))
-> m (Step (Is (a, b)) r (ProcessT m (a, b) r))
go Async (StM m (MachineStep m (Is a) r))
s1 Async (StM m (MachineStep m (Is b) r))
s2
  where go :: AsyncStep m (Is a) r
           -> AsyncStep m (Is b) r
           -> m (MachineStep m (Is (a,b)) r)
        go :: Async (StM m (MachineStep m (Is a) r))
-> Async (StM m (MachineStep m (Is b) r))
-> m (Step (Is (a, b)) r (ProcessT m (a, b) r))
go Async (StM m (MachineStep m (Is a) r))
s1 Async (StM m (MachineStep m (Is b) r))
s2 = Async (StM m (MachineStep m (Is a) r))
-> Async (StM m (MachineStep m (Is b) r))
-> m (MachineStep m (Is a) r, MachineStep m (Is b) r)
forall (m :: * -> *) a b.
MonadBaseControl IO m =>
Async (StM m a) -> Async (StM m b) -> m (a, b)
waitBoth Async (StM m (MachineStep m (Is a) r))
s1 Async (StM m (MachineStep m (Is b) r))
s2 m (MachineStep m (Is a) r, MachineStep m (Is b) r)
-> ((MachineStep m (Is a) r, MachineStep m (Is b) r)
    -> m (Step (Is (a, b)) r (ProcessT m (a, b) r)))
-> m (Step (Is (a, b)) r (ProcessT m (a, b) r))
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= 
                   \((MachineStep m (Is a) r, MachineStep m (Is b) r)
ss :: (MachineStep m (Is a) r, MachineStep m (Is b) r)) -> case (MachineStep m (Is a) r, MachineStep m (Is b) r)
ss of
          (MachineStep m (Is a) r
Stop, MachineStep m (Is b) r
_) -> Step (Is (a, b)) r (ProcessT m (a, b) r)
-> m (Step (Is (a, b)) r (ProcessT m (a, b) r))
forall (m :: * -> *) a. Monad m => a -> m a
return Step (Is (a, b)) r (ProcessT m (a, b) r)
forall (k :: * -> *) o r. Step k o r
Stop
          (MachineStep m (Is a) r
_, MachineStep m (Is b) r
Stop) -> Step (Is (a, b)) r (ProcessT m (a, b) r)
-> m (Step (Is (a, b)) r (ProcessT m (a, b) r))
forall (m :: * -> *) a. Monad m => a -> m a
return Step (Is (a, b)) r (ProcessT m (a, b) r)
forall (k :: * -> *) o r. Step k o r
Stop
          (Yield r
o1 ProcessT m a r
k1, Yield r
o2 ProcessT m b r
k2) -> 
            Step (Is (a, b)) r (ProcessT m (a, b) r)
-> m (Step (Is (a, b)) r (ProcessT m (a, b) r))
forall (m :: * -> *) a. Monad m => a -> m a
return (Step (Is (a, b)) r (ProcessT m (a, b) r)
 -> m (Step (Is (a, b)) r (ProcessT m (a, b) r)))
-> (Step (Is (a, b)) r (ProcessT m (a, b) r)
    -> Step (Is (a, b)) r (ProcessT m (a, b) r))
-> Step (Is (a, b)) r (ProcessT m (a, b) r)
-> m (Step (Is (a, b)) r (ProcessT m (a, b) r))
forall b c a. (b -> c) -> (a -> b) -> a -> c
. r
-> ProcessT m (a, b) r -> Step (Is (a, b)) r (ProcessT m (a, b) r)
forall (k :: * -> *) o r. o -> r -> Step k o r
Yield r
o1 (ProcessT m (a, b) r -> Step (Is (a, b)) r (ProcessT m (a, b) r))
-> (Step (Is (a, b)) r (ProcessT m (a, b) r)
    -> ProcessT m (a, b) r)
-> Step (Is (a, b)) r (ProcessT m (a, b) r)
-> Step (Is (a, b)) r (ProcessT m (a, b) r)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Step (Is (a, b)) r (ProcessT m (a, b) r) -> ProcessT m (a, b) r
forall (m :: * -> *) (k :: * -> *) o.
Monad m =>
Step k o (MachineT m k o) -> MachineT m k o
encased (Step (Is (a, b)) r (ProcessT m (a, b) r)
 -> m (Step (Is (a, b)) r (ProcessT m (a, b) r)))
-> Step (Is (a, b)) r (ProcessT m (a, b) r)
-> m (Step (Is (a, b)) r (ProcessT m (a, b) r))
forall a b. (a -> b) -> a -> b
$ r
-> ProcessT m (a, b) r -> Step (Is (a, b)) r (ProcessT m (a, b) r)
forall (k :: * -> *) o r. o -> r -> Step k o r
Yield r
o2 (ProcessT m (a, b) r -> Step (Is (a, b)) r (ProcessT m (a, b) r))
-> ProcessT m (a, b) r -> Step (Is (a, b)) r (ProcessT m (a, b) r)
forall a b. (a -> b) -> a -> b
$ m (Step (Is (a, b)) r (ProcessT m (a, b) r)) -> ProcessT m (a, b) r
forall (m :: * -> *) (k :: * -> *) o.
m (Step k o (MachineT m k o)) -> MachineT m k o
MachineT (m (Step (Is (a, b)) r (ProcessT m (a, b) r))
 -> ProcessT m (a, b) r)
-> m (Step (Is (a, b)) r (ProcessT m (a, b) r))
-> ProcessT m (a, b) r
forall a b. (a -> b) -> a -> b
$
            do Async (StM m (MachineStep m (Is a) r))
k1' <- ProcessT m a r -> m (Async (StM m (MachineStep m (Is a) r)))
forall (m :: * -> *) (k :: * -> *) o.
MonadBaseControl IO m =>
MachineT m k o -> m (AsyncStep m k o)
asyncRun ProcessT m a r
k1
               Async (StM m (MachineStep m (Is b) r))
k2' <- ProcessT m b r -> m (Async (StM m (MachineStep m (Is b) r)))
forall (m :: * -> *) (k :: * -> *) o.
MonadBaseControl IO m =>
MachineT m k o -> m (AsyncStep m k o)
asyncRun ProcessT m b r
k2
               Async (StM m (MachineStep m (Is a) r))
-> Async (StM m (MachineStep m (Is b) r))
-> m (Step (Is (a, b)) r (ProcessT m (a, b) r))
go Async (StM m (MachineStep m (Is a) r))
k1' Async (StM m (MachineStep m (Is b) r))
k2'
          (Yield r
o ProcessT m a r
k, MachineStep m (Is b) r
_) ->
            Step (Is (a, b)) r (ProcessT m (a, b) r)
-> m (Step (Is (a, b)) r (ProcessT m (a, b) r))
forall (m :: * -> *) a. Monad m => a -> m a
return (Step (Is (a, b)) r (ProcessT m (a, b) r)
 -> m (Step (Is (a, b)) r (ProcessT m (a, b) r)))
-> (m (Step (Is (a, b)) r (ProcessT m (a, b) r))
    -> Step (Is (a, b)) r (ProcessT m (a, b) r))
-> m (Step (Is (a, b)) r (ProcessT m (a, b) r))
-> m (Step (Is (a, b)) r (ProcessT m (a, b) r))
forall b c a. (b -> c) -> (a -> b) -> a -> c
. r
-> ProcessT m (a, b) r -> Step (Is (a, b)) r (ProcessT m (a, b) r)
forall (k :: * -> *) o r. o -> r -> Step k o r
Yield r
o (ProcessT m (a, b) r -> Step (Is (a, b)) r (ProcessT m (a, b) r))
-> (m (Step (Is (a, b)) r (ProcessT m (a, b) r))
    -> ProcessT m (a, b) r)
-> m (Step (Is (a, b)) r (ProcessT m (a, b) r))
-> Step (Is (a, b)) r (ProcessT m (a, b) r)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. m (Step (Is (a, b)) r (ProcessT m (a, b) r)) -> ProcessT m (a, b) r
forall (m :: * -> *) (k :: * -> *) o.
m (Step k o (MachineT m k o)) -> MachineT m k o
MachineT (m (Step (Is (a, b)) r (ProcessT m (a, b) r))
 -> m (Step (Is (a, b)) r (ProcessT m (a, b) r)))
-> m (Step (Is (a, b)) r (ProcessT m (a, b) r))
-> m (Step (Is (a, b)) r (ProcessT m (a, b) r))
forall a b. (a -> b) -> a -> b
$ ProcessT m a r -> m (Async (StM m (MachineStep m (Is a) r)))
forall (m :: * -> *) (k :: * -> *) o.
MonadBaseControl IO m =>
MachineT m k o -> m (AsyncStep m k o)
asyncRun ProcessT m a r
k m (Async (StM m (MachineStep m (Is a) r)))
-> (Async (StM m (MachineStep m (Is a) r))
    -> m (Step (Is (a, b)) r (ProcessT m (a, b) r)))
-> m (Step (Is (a, b)) r (ProcessT m (a, b) r))
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= (Async (StM m (MachineStep m (Is a) r))
 -> Async (StM m (MachineStep m (Is b) r))
 -> m (Step (Is (a, b)) r (ProcessT m (a, b) r)))
-> Async (StM m (MachineStep m (Is b) r))
-> Async (StM m (MachineStep m (Is a) r))
-> m (Step (Is (a, b)) r (ProcessT m (a, b) r))
forall a b c. (a -> b -> c) -> b -> a -> c
flip Async (StM m (MachineStep m (Is a) r))
-> Async (StM m (MachineStep m (Is b) r))
-> m (Step (Is (a, b)) r (ProcessT m (a, b) r))
go Async (StM m (MachineStep m (Is b) r))
s2
          (MachineStep m (Is a) r
_, Yield r
o ProcessT m b r
k) ->
            Step (Is (a, b)) r (ProcessT m (a, b) r)
-> m (Step (Is (a, b)) r (ProcessT m (a, b) r))
forall (m :: * -> *) a. Monad m => a -> m a
return (Step (Is (a, b)) r (ProcessT m (a, b) r)
 -> m (Step (Is (a, b)) r (ProcessT m (a, b) r)))
-> (m (Step (Is (a, b)) r (ProcessT m (a, b) r))
    -> Step (Is (a, b)) r (ProcessT m (a, b) r))
-> m (Step (Is (a, b)) r (ProcessT m (a, b) r))
-> m (Step (Is (a, b)) r (ProcessT m (a, b) r))
forall b c a. (b -> c) -> (a -> b) -> a -> c
. r
-> ProcessT m (a, b) r -> Step (Is (a, b)) r (ProcessT m (a, b) r)
forall (k :: * -> *) o r. o -> r -> Step k o r
Yield r
o (ProcessT m (a, b) r -> Step (Is (a, b)) r (ProcessT m (a, b) r))
-> (m (Step (Is (a, b)) r (ProcessT m (a, b) r))
    -> ProcessT m (a, b) r)
-> m (Step (Is (a, b)) r (ProcessT m (a, b) r))
-> Step (Is (a, b)) r (ProcessT m (a, b) r)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. m (Step (Is (a, b)) r (ProcessT m (a, b) r)) -> ProcessT m (a, b) r
forall (m :: * -> *) (k :: * -> *) o.
m (Step k o (MachineT m k o)) -> MachineT m k o
MachineT (m (Step (Is (a, b)) r (ProcessT m (a, b) r))
 -> m (Step (Is (a, b)) r (ProcessT m (a, b) r)))
-> m (Step (Is (a, b)) r (ProcessT m (a, b) r))
-> m (Step (Is (a, b)) r (ProcessT m (a, b) r))
forall a b. (a -> b) -> a -> b
$ ProcessT m b r -> m (Async (StM m (MachineStep m (Is b) r)))
forall (m :: * -> *) (k :: * -> *) o.
MonadBaseControl IO m =>
MachineT m k o -> m (AsyncStep m k o)
asyncRun ProcessT m b r
k m (Async (StM m (MachineStep m (Is b) r)))
-> (Async (StM m (MachineStep m (Is b) r))
    -> m (Step (Is (a, b)) r (ProcessT m (a, b) r)))
-> m (Step (Is (a, b)) r (ProcessT m (a, b) r))
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= Async (StM m (MachineStep m (Is a) r))
-> Async (StM m (MachineStep m (Is b) r))
-> m (Step (Is (a, b)) r (ProcessT m (a, b) r))
go Async (StM m (MachineStep m (Is a) r))
s1
          (Await t -> ProcessT m a r
f Is a t
Refl ProcessT m a r
ff, Await t -> ProcessT m b r
g Is b t
Refl ProcessT m b r
gg) ->
            Step (Is (a, b)) r (ProcessT m (a, b) r)
-> m (Step (Is (a, b)) r (ProcessT m (a, b) r))
forall (m :: * -> *) a. Monad m => a -> m a
return (Step (Is (a, b)) r (ProcessT m (a, b) r)
 -> m (Step (Is (a, b)) r (ProcessT m (a, b) r)))
-> Step (Is (a, b)) r (ProcessT m (a, b) r)
-> m (Step (Is (a, b)) r (ProcessT m (a, b) r))
forall a b. (a -> b) -> a -> b
$ ((a, b) -> ProcessT m (a, b) r)
-> Is (a, b) (a, b)
-> ProcessT m (a, b) r
-> Step (Is (a, b)) r (ProcessT m (a, b) r)
forall (k :: * -> *) o r t. (t -> r) -> k t -> r -> Step k o r
Await ((ProcessT m a r -> ProcessT m b r -> ProcessT m (a, b) r)
-> (ProcessT m a r, ProcessT m b r) -> ProcessT m (a, b) r
forall a b c. (a -> b -> c) -> (a, b) -> c
uncurry ProcessT m a r -> ProcessT m b r -> ProcessT m (a, b) r
forall (m :: * -> *) a b r.
MonadBaseControl IO m =>
ProcessT m a r -> ProcessT m b r -> ProcessT m (a, b) r
splitProd ((ProcessT m a r, ProcessT m b r) -> ProcessT m (a, b) r)
-> ((a, b) -> (ProcessT m a r, ProcessT m b r))
-> (a, b)
-> ProcessT m (a, b) r
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (a -> ProcessT m a r
t -> ProcessT m a r
f(a -> ProcessT m a r)
-> (b -> ProcessT m b r)
-> (a, b)
-> (ProcessT m a r, ProcessT m b r)
forall (a :: * -> * -> *) b c b' c'.
Arrow a =>
a b c -> a b' c' -> a (b, b') (c, c')
***b -> ProcessT m b r
t -> ProcessT m b r
g)) Is (a, b) (a, b)
forall a. Is a a
Refl (ProcessT m a r -> ProcessT m b r -> ProcessT m (a, b) r
forall (m :: * -> *) a b r.
MonadBaseControl IO m =>
ProcessT m a r -> ProcessT m b r -> ProcessT m (a, b) r
splitProd ProcessT m a r
ff ProcessT m b r
gg)