{-# LANGUAGE GADTs #-}
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE LinearTypes #-}
{-# LANGUAGE QualifiedDo #-}
{-# LANGUAGE RecordWildCards #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# OPTIONS_GHC -Wno-name-shadowing #-}
{-# OPTIONS_HADDOCK hide #-}
module Streaming.Linear.Internal.Many
(
unzip,
ZipResidual,
ZipResidual3,
zip,
zipR,
zipWith,
zipWithR,
zip3,
zip3R,
zipWith3,
zipWith3R,
Either3 (..),
merge,
mergeOn,
mergeBy,
)
where
import qualified Control.Functor.Linear as Control
import Prelude.Linear (($), (&))
import Streaming.Linear.Internal.Consume
import Streaming.Linear.Internal.Type
import Prelude (Either (..), Ord (..), Ordering (..))
unzip ::
(Control.Monad m) =>
Stream (Of (a, b)) m r %1 ->
Stream (Of a) (Stream (Of b) m) r
unzip :: forall (m :: * -> *) a b r.
Monad m =>
Stream (Of (a, b)) m r %1 -> Stream (Of a) (Stream (Of b) m) r
unzip = forall (m :: * -> *) a b r.
Monad m =>
Stream (Of (a, b)) m r %1 -> Stream (Of a) (Stream (Of b) m) r
loop
where
loop ::
(Control.Monad m) =>
Stream (Of (a, b)) m r %1 ->
Stream (Of a) (Stream (Of b) m) r
loop :: forall (m :: * -> *) a b r.
Monad m =>
Stream (Of (a, b)) m r %1 -> Stream (Of a) (Stream (Of b) m) r
loop Stream (Of (a, b)) m r
stream =
Stream (Of (a, b)) m r
stream forall a b (p :: Multiplicity) (q :: Multiplicity).
a %p -> (a %p -> b) %q -> b
& \case
Return r
r -> forall r (f :: * -> *) (m :: * -> *). r -> Stream f m r
Return r
r
Effect m (Stream (Of (a, b)) m r)
m -> forall (m :: * -> *) (f :: * -> *) r.
m (Stream f m r) -> Stream f m r
Effect forall a b (p :: Multiplicity) (q :: Multiplicity).
(a %p -> b) %q -> a %p -> b
$ forall (f :: * -> *) a b.
Functor f =>
(a %1 -> b) %1 -> f a %1 -> f b
Control.fmap forall (m :: * -> *) a b r.
Monad m =>
Stream (Of (a, b)) m r %1 -> Stream (Of a) (Stream (Of b) m) r
loop forall a b (p :: Multiplicity) (q :: Multiplicity).
(a %p -> b) %q -> a %p -> b
$ forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a %1 -> t m a
Control.lift m (Stream (Of (a, b)) m r)
m
Step ((a
a, b
b) :> Stream (Of (a, b)) m r
rest) -> forall (f :: * -> *) (m :: * -> *) r.
f (Stream f m r) -> Stream f m r
Step (a
a forall a b. a -> b -> Of a b
:> forall (m :: * -> *) (f :: * -> *) r.
m (Stream f m r) -> Stream f m r
Effect (forall (f :: * -> *) (m :: * -> *) r.
f (Stream f m r) -> Stream f m r
Step (b
b forall a b. a -> b -> Of a b
:> forall r (f :: * -> *) (m :: * -> *). r -> Stream f m r
Return (forall (m :: * -> *) a b r.
Monad m =>
Stream (Of (a, b)) m r %1 -> Stream (Of a) (Stream (Of b) m) r
loop Stream (Of (a, b)) m r
rest))))
{-# INLINEABLE unzip #-}
data Either3 a b c where
Left3 :: a %1 -> Either3 a b c
Middle3 :: b %1 -> Either3 a b c
Right3 :: c %1 -> Either3 a b c
type ZipResidual a b m r1 r2 =
Either3
(r1, r2)
(r1, Stream (Of b) m r2)
(Stream (Of a) m r1, r2)
zipWithR ::
(Control.Monad m) =>
(a -> b -> c) ->
Stream (Of a) m r1 %1 ->
Stream (Of b) m r2 %1 ->
Stream (Of c) m (ZipResidual a b m r1 r2)
zipWithR :: forall (m :: * -> *) a b c r1 r2.
Monad m =>
(a -> b -> c)
-> Stream (Of a) m r1
%1 -> Stream (Of b) m r2
%1 -> Stream (Of c) m (ZipResidual a b m r1 r2)
zipWithR = forall (m :: * -> *) a b c r1 r2.
Monad m =>
(a -> b -> c)
-> Stream (Of a) m r1
%1 -> Stream (Of b) m r2
%1 -> Stream (Of c) m (ZipResidual a b m r1 r2)
loop
where
loop ::
(Control.Monad m) =>
(a -> b -> c) ->
Stream (Of a) m r1 %1 ->
Stream (Of b) m r2 %1 ->
Stream (Of c) m (ZipResidual a b m r1 r2)
loop :: forall (m :: * -> *) a b c r1 r2.
Monad m =>
(a -> b -> c)
-> Stream (Of a) m r1
%1 -> Stream (Of b) m r2
%1 -> Stream (Of c) m (ZipResidual a b m r1 r2)
loop a -> b -> c
f Stream (Of a) m r1
st1 Stream (Of b) m r2
st2 =
Stream (Of a) m r1
st1 forall a b (p :: Multiplicity) (q :: Multiplicity).
a %p -> (a %p -> b) %q -> b
& \case
Effect m (Stream (Of a) m r1)
ms -> forall (m :: * -> *) (f :: * -> *) r.
m (Stream f m r) -> Stream f m r
Effect forall a b (p :: Multiplicity) (q :: Multiplicity).
(a %p -> b) %q -> a %p -> b
$ forall (f :: * -> *) a b.
Functor f =>
(a %1 -> b) %1 -> f a %1 -> f b
Control.fmap (\Stream (Of a) m r1
s -> forall (m :: * -> *) a b c r1 r2.
Monad m =>
(a -> b -> c)
-> Stream (Of a) m r1
%1 -> Stream (Of b) m r2
%1 -> Stream (Of c) m (ZipResidual a b m r1 r2)
loop a -> b -> c
f Stream (Of a) m r1
s Stream (Of b) m r2
st2) m (Stream (Of a) m r1)
ms
Return r1
r1 ->
Stream (Of b) m r2
st2 forall a b (p :: Multiplicity) (q :: Multiplicity).
a %p -> (a %p -> b) %q -> b
& \case
Return r2
r2 -> forall r (f :: * -> *) (m :: * -> *). r -> Stream f m r
Return forall a b (p :: Multiplicity) (q :: Multiplicity).
(a %p -> b) %q -> a %p -> b
$ forall a b c. a -> Either3 a b c
Left3 (r1
r1, r2
r2)
Stream (Of b) m r2
st2' -> forall r (f :: * -> *) (m :: * -> *). r -> Stream f m r
Return forall a b (p :: Multiplicity) (q :: Multiplicity).
(a %p -> b) %q -> a %p -> b
$ forall b a c. b -> Either3 a b c
Middle3 (r1
r1, Stream (Of b) m r2
st2')
Step (a
a :> Stream (Of a) m r1
as) ->
Stream (Of b) m r2
st2 forall a b (p :: Multiplicity) (q :: Multiplicity).
a %p -> (a %p -> b) %q -> b
& \case
Effect m (Stream (Of b) m r2)
ms ->
forall (m :: * -> *) (f :: * -> *) r.
m (Stream f m r) -> Stream f m r
Effect forall a b (p :: Multiplicity) (q :: Multiplicity).
(a %p -> b) %q -> a %p -> b
$ forall (f :: * -> *) a b.
Functor f =>
(a %1 -> b) %1 -> f a %1 -> f b
Control.fmap (\Stream (Of b) m r2
s -> forall (m :: * -> *) a b c r1 r2.
Monad m =>
(a -> b -> c)
-> Stream (Of a) m r1
%1 -> Stream (Of b) m r2
%1 -> Stream (Of c) m (ZipResidual a b m r1 r2)
loop a -> b -> c
f (forall (f :: * -> *) (m :: * -> *) r.
f (Stream f m r) -> Stream f m r
Step (a
a forall a b. a -> b -> Of a b
:> Stream (Of a) m r1
as)) Stream (Of b) m r2
s) m (Stream (Of b) m r2)
ms
Return r2
r2 -> forall r (f :: * -> *) (m :: * -> *). r -> Stream f m r
Return forall a b (p :: Multiplicity) (q :: Multiplicity).
(a %p -> b) %q -> a %p -> b
$ forall c a b. c -> Either3 a b c
Right3 (forall (f :: * -> *) (m :: * -> *) r.
f (Stream f m r) -> Stream f m r
Step (a
a forall a b. a -> b -> Of a b
:> Stream (Of a) m r1
as), r2
r2)
Step (b
b :> Stream (Of b) m r2
bs) -> forall (f :: * -> *) (m :: * -> *) r.
f (Stream f m r) -> Stream f m r
Step forall a b (p :: Multiplicity) (q :: Multiplicity).
(a %p -> b) %q -> a %p -> b
$ (a -> b -> c
f a
a b
b) forall a b. a -> b -> Of a b
:> forall (m :: * -> *) a b c r1 r2.
Monad m =>
(a -> b -> c)
-> Stream (Of a) m r1
%1 -> Stream (Of b) m r2
%1 -> Stream (Of c) m (ZipResidual a b m r1 r2)
loop a -> b -> c
f Stream (Of a) m r1
as Stream (Of b) m r2
bs
{-# INLINEABLE zipWithR #-}
zipWith ::
(Control.Monad m) =>
(a -> b -> c) ->
Stream (Of a) m r1 %1 ->
Stream (Of b) m r2 %1 ->
Stream (Of c) m (r1, r2)
zipWith :: forall (m :: * -> *) a b c r1 r2.
Monad m =>
(a -> b -> c)
-> Stream (Of a) m r1
%1 -> Stream (Of b) m r2
%1 -> Stream (Of c) m (r1, r2)
zipWith a -> b -> c
f Stream (Of a) m r1
s1 Stream (Of b) m r2
s2 = Control.do
ZipResidual a b m r1 r2
result <- forall (m :: * -> *) a b c r1 r2.
Monad m =>
(a -> b -> c)
-> Stream (Of a) m r1
%1 -> Stream (Of b) m r2
%1 -> Stream (Of c) m (ZipResidual a b m r1 r2)
zipWithR a -> b -> c
f Stream (Of a) m r1
s1 Stream (Of b) m r2
s2
ZipResidual a b m r1 r2
result forall a b (p :: Multiplicity) (q :: Multiplicity).
a %p -> (a %p -> b) %q -> b
& \case
Left3 (r1, r2)
rets -> forall (m :: * -> *) a. Monad m => a %1 -> m a
Control.return (r1, r2)
rets
Middle3 (r1
r1, Stream (Of b) m r2
s2') -> Control.do
r2
r2 <- forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a %1 -> t m a
Control.lift forall a b (p :: Multiplicity) (q :: Multiplicity).
(a %p -> b) %q -> a %p -> b
$ forall a (m :: * -> *) r. Monad m => Stream (Of a) m r %1 -> m r
effects Stream (Of b) m r2
s2'
forall (m :: * -> *) a. Monad m => a %1 -> m a
Control.return (r1
r1, r2
r2)
Right3 (Stream (Of a) m r1
s1', r2
r2) -> Control.do
r1
r1 <- forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a %1 -> t m a
Control.lift forall a b (p :: Multiplicity) (q :: Multiplicity).
(a %p -> b) %q -> a %p -> b
$ forall a (m :: * -> *) r. Monad m => Stream (Of a) m r %1 -> m r
effects Stream (Of a) m r1
s1'
forall (m :: * -> *) a. Monad m => a %1 -> m a
Control.return (r1
r1, r2
r2)
{-# INLINEABLE zipWith #-}
zip ::
(Control.Monad m) =>
Stream (Of a) m r1 %1 ->
Stream (Of b) m r2 %1 ->
Stream (Of (a, b)) m (r1, r2)
zip :: forall (m :: * -> *) a r1 b r2.
Monad m =>
Stream (Of a) m r1
%1 -> Stream (Of b) m r2 %1 -> Stream (Of (a, b)) m (r1, r2)
zip = forall (m :: * -> *) a b c r1 r2.
Monad m =>
(a -> b -> c)
-> Stream (Of a) m r1
%1 -> Stream (Of b) m r2
%1 -> Stream (Of c) m (r1, r2)
zipWith (,)
{-# INLINE zip #-}
zipR ::
(Control.Monad m) =>
Stream (Of a) m r1 %1 ->
Stream (Of b) m r2 %1 ->
Stream (Of (a, b)) m (ZipResidual a b m r1 r2)
zipR :: forall (m :: * -> *) a r1 b r2.
Monad m =>
Stream (Of a) m r1
%1 -> Stream (Of b) m r2
%1 -> Stream (Of (a, b)) m (ZipResidual a b m r1 r2)
zipR = forall (m :: * -> *) a b c r1 r2.
Monad m =>
(a -> b -> c)
-> Stream (Of a) m r1
%1 -> Stream (Of b) m r2
%1 -> Stream (Of c) m (ZipResidual a b m r1 r2)
zipWithR (,)
{-# INLINE zipR #-}
type ZipResidual3 a b c m r1 r2 r3 =
( Either r1 (Stream (Of a) m r1),
Either r2 (Stream (Of b) m r2),
Either r3 (Stream (Of c) m r3)
)
zipWith3R ::
(Control.Monad m) =>
(a -> b -> c -> d) ->
Stream (Of a) m r1 %1 ->
Stream (Of b) m r2 %1 ->
Stream (Of c) m r3 %1 ->
Stream (Of d) m (ZipResidual3 a b c m r1 r2 r3)
zipWith3R :: forall (m :: * -> *) a b c d r1 r2 r3.
Monad m =>
(a -> b -> c -> d)
-> Stream (Of a) m r1
%1 -> Stream (Of b) m r2
%1 -> Stream (Of c) m r3
%1 -> Stream (Of d) m (ZipResidual3 a b c m r1 r2 r3)
zipWith3R = forall (m :: * -> *) a b c d r1 r2 r3.
Monad m =>
(a -> b -> c -> d)
-> Stream (Of a) m r1
%1 -> Stream (Of b) m r2
%1 -> Stream (Of c) m r3
%1 -> Stream (Of d) m (ZipResidual3 a b c m r1 r2 r3)
loop
where
loop ::
(Control.Monad m) =>
(a -> b -> c -> d) ->
Stream (Of a) m r1 %1 ->
Stream (Of b) m r2 %1 ->
Stream (Of c) m r3 %1 ->
Stream (Of d) m (ZipResidual3 a b c m r1 r2 r3)
loop :: forall (m :: * -> *) a b c d r1 r2 r3.
Monad m =>
(a -> b -> c -> d)
-> Stream (Of a) m r1
%1 -> Stream (Of b) m r2
%1 -> Stream (Of c) m r3
%1 -> Stream (Of d) m (ZipResidual3 a b c m r1 r2 r3)
loop a -> b -> c -> d
f Stream (Of a) m r1
s1 Stream (Of b) m r2
s2 Stream (Of c) m r3
s3 =
Stream (Of a) m r1
s1 forall a b (p :: Multiplicity) (q :: Multiplicity).
a %p -> (a %p -> b) %q -> b
& \case
Effect m (Stream (Of a) m r1)
ms -> forall (m :: * -> *) (f :: * -> *) r.
m (Stream f m r) -> Stream f m r
Effect forall a b (p :: Multiplicity) (q :: Multiplicity).
(a %p -> b) %q -> a %p -> b
$ forall (f :: * -> *) a b.
Functor f =>
(a %1 -> b) %1 -> f a %1 -> f b
Control.fmap (\Stream (Of a) m r1
s -> forall (m :: * -> *) a b c d r1 r2 r3.
Monad m =>
(a -> b -> c -> d)
-> Stream (Of a) m r1
%1 -> Stream (Of b) m r2
%1 -> Stream (Of c) m r3
%1 -> Stream (Of d) m (ZipResidual3 a b c m r1 r2 r3)
loop a -> b -> c -> d
f Stream (Of a) m r1
s Stream (Of b) m r2
s2 Stream (Of c) m r3
s3) m (Stream (Of a) m r1)
ms
Return r1
r1 ->
(Stream (Of b) m r2
s2, Stream (Of c) m r3
s3) forall a b (p :: Multiplicity) (q :: Multiplicity).
a %p -> (a %p -> b) %q -> b
& \case
(Return r2
r2, Return r3
r3) -> forall r (f :: * -> *) (m :: * -> *). r -> Stream f m r
Return (forall a b. a -> Either a b
Left r1
r1, forall a b. a -> Either a b
Left r2
r2, forall a b. a -> Either a b
Left r3
r3)
(Stream (Of b) m r2
s2', Stream (Of c) m r3
s3') -> forall r (f :: * -> *) (m :: * -> *). r -> Stream f m r
Return (forall a b. a -> Either a b
Left r1
r1, forall a b. b -> Either a b
Right Stream (Of b) m r2
s2', forall a b. b -> Either a b
Right Stream (Of c) m r3
s3')
Step (a
a :> Stream (Of a) m r1
as) ->
Stream (Of b) m r2
s2 forall a b (p :: Multiplicity) (q :: Multiplicity).
a %p -> (a %p -> b) %q -> b
& \case
Effect m (Stream (Of b) m r2)
ms ->
forall (m :: * -> *) (f :: * -> *) r.
m (Stream f m r) -> Stream f m r
Effect forall a b (p :: Multiplicity) (q :: Multiplicity).
(a %p -> b) %q -> a %p -> b
$
forall (f :: * -> *) a b.
Functor f =>
(a %1 -> b) %1 -> f a %1 -> f b
Control.fmap (\Stream (Of b) m r2
s -> forall (m :: * -> *) a b c d r1 r2 r3.
Monad m =>
(a -> b -> c -> d)
-> Stream (Of a) m r1
%1 -> Stream (Of b) m r2
%1 -> Stream (Of c) m r3
%1 -> Stream (Of d) m (ZipResidual3 a b c m r1 r2 r3)
loop a -> b -> c -> d
f (forall (f :: * -> *) (m :: * -> *) r.
f (Stream f m r) -> Stream f m r
Step forall a b (p :: Multiplicity) (q :: Multiplicity).
(a %p -> b) %q -> a %p -> b
$ a
a forall a b. a -> b -> Of a b
:> Stream (Of a) m r1
as) Stream (Of b) m r2
s Stream (Of c) m r3
s3) m (Stream (Of b) m r2)
ms
Return r2
r2 -> forall r (f :: * -> *) (m :: * -> *). r -> Stream f m r
Return (forall a b. b -> Either a b
Right (forall (f :: * -> *) (m :: * -> *) r.
f (Stream f m r) -> Stream f m r
Step forall a b (p :: Multiplicity) (q :: Multiplicity).
(a %p -> b) %q -> a %p -> b
$ a
a forall a b. a -> b -> Of a b
:> Stream (Of a) m r1
as), forall a b. a -> Either a b
Left r2
r2, forall a b. b -> Either a b
Right Stream (Of c) m r3
s3)
Step (b
b :> Stream (Of b) m r2
bs) ->
Stream (Of c) m r3
s3 forall a b (p :: Multiplicity) (q :: Multiplicity).
a %p -> (a %p -> b) %q -> b
& \case
Effect m (Stream (Of c) m r3)
ms ->
forall (m :: * -> *) (f :: * -> *) r.
m (Stream f m r) -> Stream f m r
Effect forall a b (p :: Multiplicity) (q :: Multiplicity).
(a %p -> b) %q -> a %p -> b
$
forall (f :: * -> *) a b.
Functor f =>
(a %1 -> b) %1 -> f a %1 -> f b
Control.fmap (\Stream (Of c) m r3
s -> forall (m :: * -> *) a b c d r1 r2 r3.
Monad m =>
(a -> b -> c -> d)
-> Stream (Of a) m r1
%1 -> Stream (Of b) m r2
%1 -> Stream (Of c) m r3
%1 -> Stream (Of d) m (ZipResidual3 a b c m r1 r2 r3)
loop a -> b -> c -> d
f (forall (f :: * -> *) (m :: * -> *) r.
f (Stream f m r) -> Stream f m r
Step forall a b (p :: Multiplicity) (q :: Multiplicity).
(a %p -> b) %q -> a %p -> b
$ a
a forall a b. a -> b -> Of a b
:> Stream (Of a) m r1
as) (forall (f :: * -> *) (m :: * -> *) r.
f (Stream f m r) -> Stream f m r
Step forall a b (p :: Multiplicity) (q :: Multiplicity).
(a %p -> b) %q -> a %p -> b
$ b
b forall a b. a -> b -> Of a b
:> Stream (Of b) m r2
bs) Stream (Of c) m r3
s) m (Stream (Of c) m r3)
ms
Return r3
r3 ->
forall r (f :: * -> *) (m :: * -> *). r -> Stream f m r
Return (forall a b. b -> Either a b
Right (forall (f :: * -> *) (m :: * -> *) r.
f (Stream f m r) -> Stream f m r
Step forall a b (p :: Multiplicity) (q :: Multiplicity).
(a %p -> b) %q -> a %p -> b
$ a
a forall a b. a -> b -> Of a b
:> Stream (Of a) m r1
as), forall a b. b -> Either a b
Right (forall (f :: * -> *) (m :: * -> *) r.
f (Stream f m r) -> Stream f m r
Step forall a b (p :: Multiplicity) (q :: Multiplicity).
(a %p -> b) %q -> a %p -> b
$ b
b forall a b. a -> b -> Of a b
:> Stream (Of b) m r2
bs), forall a b. a -> Either a b
Left r3
r3)
Step (c
c :> Stream (Of c) m r3
cs) -> forall (f :: * -> *) (m :: * -> *) r.
f (Stream f m r) -> Stream f m r
Step forall a b (p :: Multiplicity) (q :: Multiplicity).
(a %p -> b) %q -> a %p -> b
$ (a -> b -> c -> d
f a
a b
b c
c) forall a b. a -> b -> Of a b
:> forall (m :: * -> *) a b c d r1 r2 r3.
Monad m =>
(a -> b -> c -> d)
-> Stream (Of a) m r1
%1 -> Stream (Of b) m r2
%1 -> Stream (Of c) m r3
%1 -> Stream (Of d) m (ZipResidual3 a b c m r1 r2 r3)
loop a -> b -> c -> d
f Stream (Of a) m r1
as Stream (Of b) m r2
bs Stream (Of c) m r3
cs
{-# INLINEABLE zipWith3R #-}
zipWith3 ::
(Control.Monad m) =>
(a -> b -> c -> d) ->
Stream (Of a) m r1 %1 ->
Stream (Of b) m r2 %1 ->
Stream (Of c) m r3 %1 ->
Stream (Of d) m (r1, r2, r3)
zipWith3 :: forall (m :: * -> *) a b c d r1 r2 r3.
Monad m =>
(a -> b -> c -> d)
-> Stream (Of a) m r1
%1 -> Stream (Of b) m r2
%1 -> Stream (Of c) m r3
%1 -> Stream (Of d) m (r1, r2, r3)
zipWith3 a -> b -> c -> d
f Stream (Of a) m r1
s1 Stream (Of b) m r2
s2 Stream (Of c) m r3
s3 = Control.do
ZipResidual3 a b c m r1 r2 r3
result <- forall (m :: * -> *) a b c d r1 r2 r3.
Monad m =>
(a -> b -> c -> d)
-> Stream (Of a) m r1
%1 -> Stream (Of b) m r2
%1 -> Stream (Of c) m r3
%1 -> Stream (Of d) m (ZipResidual3 a b c m r1 r2 r3)
zipWith3R a -> b -> c -> d
f Stream (Of a) m r1
s1 Stream (Of b) m r2
s2 Stream (Of c) m r3
s3
ZipResidual3 a b c m r1 r2 r3
result forall a b (p :: Multiplicity) (q :: Multiplicity).
a %p -> (a %p -> b) %q -> b
& \case
(Either r1 (Stream (Of a) m r1)
res1, Either r2 (Stream (Of b) m r2)
res2, Either r3 (Stream (Of c) m r3)
res3) -> Control.do
r1
r1 <- forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a %1 -> t m a
Control.lift forall a b (p :: Multiplicity) (q :: Multiplicity).
(a %p -> b) %q -> a %p -> b
$ forall (m :: * -> *) r a.
Monad m =>
Either r (Stream (Of a) m r) %1 -> m r
extractResult Either r1 (Stream (Of a) m r1)
res1
r2
r2 <- forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a %1 -> t m a
Control.lift forall a b (p :: Multiplicity) (q :: Multiplicity).
(a %p -> b) %q -> a %p -> b
$ forall (m :: * -> *) r a.
Monad m =>
Either r (Stream (Of a) m r) %1 -> m r
extractResult Either r2 (Stream (Of b) m r2)
res2
r3
r3 <- forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a %1 -> t m a
Control.lift forall a b (p :: Multiplicity) (q :: Multiplicity).
(a %p -> b) %q -> a %p -> b
$ forall (m :: * -> *) r a.
Monad m =>
Either r (Stream (Of a) m r) %1 -> m r
extractResult Either r3 (Stream (Of c) m r3)
res3
forall (m :: * -> *) a. Monad m => a %1 -> m a
Control.return (r1
r1, r2
r2, r3
r3)
{-# INLINEABLE zipWith3 #-}
zip3 ::
(Control.Monad m) =>
Stream (Of a) m r1 %1 ->
Stream (Of b) m r2 %1 ->
Stream (Of c) m r3 %1 ->
Stream (Of (a, b, c)) m (r1, r2, r3)
zip3 :: forall (m :: * -> *) a r1 b r2 c r3.
Monad m =>
Stream (Of a) m r1
%1 -> Stream (Of b) m r2
%1 -> Stream (Of c) m r3
%1 -> Stream (Of (a, b, c)) m (r1, r2, r3)
zip3 = forall (m :: * -> *) a b c d r1 r2 r3.
Monad m =>
(a -> b -> c -> d)
-> Stream (Of a) m r1
%1 -> Stream (Of b) m r2
%1 -> Stream (Of c) m r3
%1 -> Stream (Of d) m (r1, r2, r3)
zipWith3 (,,)
{-# INLINEABLE zip3 #-}
zip3R ::
(Control.Monad m) =>
Stream (Of a) m r1 %1 ->
Stream (Of b) m r2 %1 ->
Stream (Of c) m r3 %1 ->
Stream (Of (a, b, c)) m (ZipResidual3 a b c m r1 r2 r3)
zip3R :: forall (m :: * -> *) a r1 b r2 c r3.
Monad m =>
Stream (Of a) m r1
%1 -> Stream (Of b) m r2
%1 -> Stream (Of c) m r3
%1 -> Stream (Of (a, b, c)) m (ZipResidual3 a b c m r1 r2 r3)
zip3R = forall (m :: * -> *) a b c d r1 r2 r3.
Monad m =>
(a -> b -> c -> d)
-> Stream (Of a) m r1
%1 -> Stream (Of b) m r2
%1 -> Stream (Of c) m r3
%1 -> Stream (Of d) m (ZipResidual3 a b c m r1 r2 r3)
zipWith3R (,,)
{-# INLINEABLE zip3R #-}
extractResult :: (Control.Monad m) => Either r (Stream (Of a) m r) %1 -> m r
(Left r
r) = forall (m :: * -> *) a. Monad m => a %1 -> m a
Control.return r
r
extractResult (Right Stream (Of a) m r
s) = forall a (m :: * -> *) r. Monad m => Stream (Of a) m r %1 -> m r
effects Stream (Of a) m r
s
merge ::
(Control.Monad m, Ord a) =>
Stream (Of a) m r %1 ->
Stream (Of a) m s %1 ->
Stream (Of a) m (r, s)
merge :: forall (m :: * -> *) a r s.
(Monad m, Ord a) =>
Stream (Of a) m r
%1 -> Stream (Of a) m s %1 -> Stream (Of a) m (r, s)
merge = forall (m :: * -> *) a r s.
Monad m =>
(a -> a -> Ordering)
-> Stream (Of a) m r
%1 -> Stream (Of a) m s
%1 -> Stream (Of a) m (r, s)
mergeBy forall a. Ord a => a -> a -> Ordering
compare
{-# INLINE merge #-}
mergeOn ::
(Control.Monad m, Ord b) =>
(a -> b) ->
Stream (Of a) m r %1 ->
Stream (Of a) m s %1 ->
Stream (Of a) m (r, s)
mergeOn :: forall (m :: * -> *) b a r s.
(Monad m, Ord b) =>
(a -> b)
-> Stream (Of a) m r
%1 -> Stream (Of a) m s
%1 -> Stream (Of a) m (r, s)
mergeOn a -> b
f = forall (m :: * -> *) a r s.
Monad m =>
(a -> a -> Ordering)
-> Stream (Of a) m r
%1 -> Stream (Of a) m s
%1 -> Stream (Of a) m (r, s)
mergeBy (\a
x a
y -> forall a. Ord a => a -> a -> Ordering
compare (a -> b
f a
x) (a -> b
f a
y))
{-# INLINE mergeOn #-}
mergeBy ::
forall m a r s.
(Control.Monad m) =>
(a -> a -> Ordering) ->
Stream (Of a) m r %1 ->
Stream (Of a) m s %1 ->
Stream (Of a) m (r, s)
mergeBy :: forall (m :: * -> *) a r s.
Monad m =>
(a -> a -> Ordering)
-> Stream (Of a) m r
%1 -> Stream (Of a) m s
%1 -> Stream (Of a) m (r, s)
mergeBy a -> a -> Ordering
comp Stream (Of a) m r
s1 Stream (Of a) m s
s2 = Stream (Of a) m r
%1 -> Stream (Of a) m s %1 -> Stream (Of a) m (r, s)
loop Stream (Of a) m r
s1 Stream (Of a) m s
s2
where
loop :: Stream (Of a) m r %1 -> Stream (Of a) m s %1 -> Stream (Of a) m (r, s)
loop :: Stream (Of a) m r
%1 -> Stream (Of a) m s %1 -> Stream (Of a) m (r, s)
loop Stream (Of a) m r
s1 Stream (Of a) m s
s2 =
Stream (Of a) m r
s1 forall a b (p :: Multiplicity) (q :: Multiplicity).
a %p -> (a %p -> b) %q -> b
& \case
Return r
r ->
forall (m :: * -> *) (f :: * -> *) r.
m (Stream f m r) -> Stream f m r
Effect forall a b (p :: Multiplicity) (q :: Multiplicity).
(a %p -> b) %q -> a %p -> b
$ forall a (m :: * -> *) r. Monad m => Stream (Of a) m r %1 -> m r
effects Stream (Of a) m s
s2 forall (m :: * -> *) a b.
Monad m =>
m a %1 -> (a %1 -> m b) %1 -> m b
Control.>>= \s
s -> forall (m :: * -> *) a. Monad m => a %1 -> m a
Control.return forall a b (p :: Multiplicity) (q :: Multiplicity).
(a %p -> b) %q -> a %p -> b
$ forall r (f :: * -> *) (m :: * -> *). r -> Stream f m r
Return (r
r, s
s)
Effect m (Stream (Of a) m r)
ms ->
forall (m :: * -> *) (f :: * -> *) r.
m (Stream f m r) -> Stream f m r
Effect forall a b (p :: Multiplicity) (q :: Multiplicity).
(a %p -> b) %q -> a %p -> b
$
m (Stream (Of a) m r)
ms forall (m :: * -> *) a b.
Monad m =>
m a %1 -> (a %1 -> m b) %1 -> m b
Control.>>= \Stream (Of a) m r
s1' -> forall (m :: * -> *) a. Monad m => a %1 -> m a
Control.return forall a b (p :: Multiplicity) (q :: Multiplicity).
(a %p -> b) %q -> a %p -> b
$ forall (m :: * -> *) a r s.
Monad m =>
(a -> a -> Ordering)
-> Stream (Of a) m r
%1 -> Stream (Of a) m s
%1 -> Stream (Of a) m (r, s)
mergeBy a -> a -> Ordering
comp Stream (Of a) m r
s1' Stream (Of a) m s
s2
Step (a
a :> Stream (Of a) m r
as) ->
Stream (Of a) m s
s2 forall a b (p :: Multiplicity) (q :: Multiplicity).
a %p -> (a %p -> b) %q -> b
& \case
Return s
s ->
forall (m :: * -> *) (f :: * -> *) r.
m (Stream f m r) -> Stream f m r
Effect forall a b (p :: Multiplicity) (q :: Multiplicity).
(a %p -> b) %q -> a %p -> b
$ forall a (m :: * -> *) r. Monad m => Stream (Of a) m r %1 -> m r
effects Stream (Of a) m r
as forall (m :: * -> *) a b.
Monad m =>
m a %1 -> (a %1 -> m b) %1 -> m b
Control.>>= \r
r -> forall (m :: * -> *) a. Monad m => a %1 -> m a
Control.return forall a b (p :: Multiplicity) (q :: Multiplicity).
(a %p -> b) %q -> a %p -> b
$ forall r (f :: * -> *) (m :: * -> *). r -> Stream f m r
Return (r
r, s
s)
Effect m (Stream (Of a) m s)
ms ->
forall (m :: * -> *) (f :: * -> *) r.
m (Stream f m r) -> Stream f m r
Effect forall a b (p :: Multiplicity) (q :: Multiplicity).
(a %p -> b) %q -> a %p -> b
$
m (Stream (Of a) m s)
ms forall (m :: * -> *) a b.
Monad m =>
m a %1 -> (a %1 -> m b) %1 -> m b
Control.>>= \Stream (Of a) m s
s2' ->
forall (m :: * -> *) a. Monad m => a %1 -> m a
Control.return forall a b (p :: Multiplicity) (q :: Multiplicity).
(a %p -> b) %q -> a %p -> b
$ forall (m :: * -> *) a r s.
Monad m =>
(a -> a -> Ordering)
-> Stream (Of a) m r
%1 -> Stream (Of a) m s
%1 -> Stream (Of a) m (r, s)
mergeBy a -> a -> Ordering
comp (forall (f :: * -> *) (m :: * -> *) r.
f (Stream f m r) -> Stream f m r
Step (a
a forall a b. a -> b -> Of a b
:> Stream (Of a) m r
as)) Stream (Of a) m s
s2'
Step (a
b :> Stream (Of a) m s
bs) -> case a -> a -> Ordering
comp a
a a
b of
Ordering
LT -> forall (f :: * -> *) (m :: * -> *) r.
f (Stream f m r) -> Stream f m r
Step (a
a forall a b. a -> b -> Of a b
:> forall (f :: * -> *) (m :: * -> *) r.
f (Stream f m r) -> Stream f m r
Step (a
b forall a b. a -> b -> Of a b
:> forall (m :: * -> *) a r s.
Monad m =>
(a -> a -> Ordering)
-> Stream (Of a) m r
%1 -> Stream (Of a) m s
%1 -> Stream (Of a) m (r, s)
mergeBy a -> a -> Ordering
comp Stream (Of a) m r
as Stream (Of a) m s
bs))
Ordering
_ -> forall (f :: * -> *) (m :: * -> *) r.
f (Stream f m r) -> Stream f m r
Step (a
b forall a b. a -> b -> Of a b
:> forall (f :: * -> *) (m :: * -> *) r.
f (Stream f m r) -> Stream f m r
Step (a
a forall a b. a -> b -> Of a b
:> forall (m :: * -> *) a r s.
Monad m =>
(a -> a -> Ordering)
-> Stream (Of a) m r
%1 -> Stream (Of a) m s
%1 -> Stream (Of a) m (r, s)
mergeBy a -> a -> Ordering
comp Stream (Of a) m r
as Stream (Of a) m s
bs))
{-# INLINEABLE mergeBy #-}