@@ -939,3 +939,46 @@ instance Monad m => Monad (Stream m) where
939939 return = pure
940940 {-# INLINE (>>=) #-}
941941 (>>=) = flip concatMap
942+
943+{-
944+-- ConcatMap recursively on itself using a merge strategy.
945+concatLoopBy :: IsStream t
946+ => (forall c. t m c -> t m c -> t m c)
947+ -> (a -> t m a) -> t m a
948+concatLoopBy = undefined
949+
950+-- This is mfix. Put another way, concatMap recursively on the output of a
951+-- stream. Compare with iterate.
952+concatLoop :: IsStream t => (a -> t m a) -> t m a
953+concatLoop = concatLoopBy serial
954+
955+instance MonadFix (Stream m) where
956+ mfix = concatLoop
957+
958+-- The SVar implementation is something similar to concatFeedBack, so maybe
959+-- there is an opportunity to share the implementation here. In an SVar we run
960+-- a part of an action (a stream), it yields an output and the rest of the
961+-- stream, we yield the output and queue back the rest of the stream for
962+-- further evaluation. Also see unfoldrA.
963+--
964+-- There could be multiple variants of this combinator, for example use a
965+-- specific way of concating i.e. concatLoopBy. Which also includes combinators
966+-- with different stop behaviors. For example if the Left values are errors we
967+-- can stop the whole composition on errors or on specific errors.
968+--
969+-- We can also flip the serial/ahead append behavior e.g. instead of processing
970+-- the Left output after the original stream we can reverse the order.
971+--
972+-- | Concat map on the 'Left' output of a stream and merge it back into the
973+-- stream. The right output is yielded in the output stream.
974+concatFeedBack :: IsStream t
975+ => (b -> t m (Either a b)) -> t m (Either a b) -> t m a
976+concatFeedBack = undefined
977+
978+-- Compare this with unfoldr. Start with a seed stream and generate a stream
979+-- with a value and new seeds. The new seeds are fed back to generate a seed
980+-- stream and so on. This is a stream level unfoldr.
981+concatUnfoldr :: IsStream t
982+ => (b -> t m (Maybe (a, b))) -> t m (Maybe (a, b)) -> t m a
983+concatUnfoldr = undefined
984+-}