{-# LANGUAGE CPP                   #-}
{-# LANGUAGE RankNTypes            #-}
{-# LANGUAGE FlexibleInstances     #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE UndecidableInstances  #-}
{-# LANGUAGE Trustworthy           #-}

{-| This module is the recommended entry point to the @pipes@ library.

    Read "Pipes.Tutorial" if you want a tutorial explaining how to use this
    library.
-}

module Pipes (
    -- * The Proxy Monad Transformer
      Proxy
    , X
    , Effect
    , Effect'
    , runEffect

    -- ** Producers
    -- $producers
    , Producer
    , Producer'
    , yield
    , for
    , (~>)
    , (<~)

    -- ** Consumers
    -- $consumers
    , Consumer
    , Consumer'
    , await
    , (>~)
    , (~<)

    -- ** Pipes
    -- $pipes
    , Pipe
    , cat
    , (>->)
    , (<-<)

    -- * ListT
    , ListT(..)
    , runListT
    , Enumerable(..)

    -- * Utilities
    , next
    , each
    , every
    , discard

    -- * Re-exports
    -- $reexports
    , module Control.Monad
    , module Control.Monad.IO.Class
    , module Control.Monad.Trans.Class
    , module Control.Monad.Morph
    , Foldable
    ) where

import Control.Monad (void, MonadPlus(mzero, mplus))
import Control.Monad.Catch (MonadThrow(..), MonadCatch(..))
import Control.Monad.Except (MonadError(..))
import Control.Monad.Fail (MonadFail(..))
import Control.Monad.IO.Class (MonadIO(liftIO))
import Control.Monad.Reader (MonadReader(..))
import Control.Monad.State (MonadState(..))
import Control.Monad.Trans.Class (MonadTrans(lift))
import Control.Monad.Trans.Except (ExceptT, runExceptT)
import Control.Monad.Trans.Identity (IdentityT(runIdentityT))
import Control.Monad.Trans.Maybe (MaybeT(runMaybeT))
import Control.Monad.Writer (MonadWriter(..))
import Control.Monad.Zip (MonadZip(..))
import Pipes.Core
import Pipes.Internal (Proxy(..))
import qualified Data.Foldable as F

#if MIN_VERSION_base(4,8,0)
import Control.Applicative (Alternative(..))
#else
import Control.Applicative
import Data.Foldable (Foldable)
import Data.Traversable (Traversable(..))
#endif
import Data.Semigroup

-- Re-exports
import Control.Monad.Morph (MFunctor(hoist), MMonad(embed))

infixl 4 <~
infixr 4 ~>
infixl 5 ~<
infixr 5 >~
infixl 7 >->
infixr 7 <-<

{- $producers
    Use 'yield' to produce output and ('~>') \/ 'for' to substitute 'yield's.

    'yield' and ('~>') obey the 'Control.Category.Category' laws:

@
\-\- Substituting \'yield\' with \'f\' gives \'f\'
'yield' '~>' f = f

\-\- Substituting every \'yield\' with another \'yield\' does nothing
f '~>' 'yield' = f

\-\- \'yield\' substitution is associative
(f '~>' g) '~>' h = f '~>' (g '~>' h)
@

    These are equivalent to the following \"for loop laws\":

@
\-\- Looping over a single yield simplifies to function application
'for' ('yield' x) f = f x

\-\- Re-yielding every element of a stream returns the original stream
'for' s 'yield' = s

\-\- Nested for loops can become a sequential 'for' loops if the inner loop
\-\- body ignores the outer loop variable
'for' s (\\a -\> 'for' (f a) g) = 'for' ('for' s f) g = 'for' s (f '~>' g)
@

-}

{-| Produce a value

@
'yield' :: 'Monad' m => a -> 'Producer' a m ()
'yield' :: 'Monad' m => a -> 'Pipe'   x a m ()
@
-}
yield :: Functor m => a -> Proxy x' x () a m ()
yield :: a -> Proxy x' x () a m ()
yield = a -> Proxy x' x () a m ()
forall (m :: * -> *) a x' x a'.
Functor m =>
a -> Proxy x' x a' a m a'
respond
{-# INLINABLE [1] yield #-}

{-| @(for p body)@ loops over @p@ replacing each 'yield' with @body@.

@
'for' :: 'Functor' m => 'Producer' b m r -> (b -> 'Effect'       m ()) -> 'Effect'       m r
'for' :: 'Functor' m => 'Producer' b m r -> (b -> 'Producer'   c m ()) -> 'Producer'   c m r
'for' :: 'Functor' m => 'Pipe'   x b m r -> (b -> 'Consumer' x   m ()) -> 'Consumer' x   m r
'for' :: 'Functor' m => 'Pipe'   x b m r -> (b -> 'Pipe'     x c m ()) -> 'Pipe'     x c m r
@

    The following diagrams show the flow of information:

@
                              .--->   b
                             /        |
   +-----------+            /   +-----|-----+                 +---------------+
   |           |           /    |     v     |                 |               |
   |           |          /     |           |                 |               |
x ==>    p    ==> b   ---'   x ==>   body  ==> c     =     x ==> 'for' p body  ==> c
   |           |                |           |                 |               |
   |     |     |                |     |     |                 |       |       |
   +-----|-----+                +-----|-----+                 +-------|-------+
         v                            v                               v
         r                            ()                              r
@

    For a more complete diagram including bidirectional flow, see "Pipes.Core#respond-diagram".
-}
for :: Functor m
    =>       Proxy x' x b' b m a'
    -- ^
    -> (b -> Proxy x' x c' c m b')
    -- ^
    ->       Proxy x' x c' c m a'
for :: Proxy x' x b' b m a'
-> (b -> Proxy x' x c' c m b') -> Proxy x' x c' c m a'
for = Proxy x' x b' b m a'
-> (b -> Proxy x' x c' c m b') -> Proxy x' x c' c m a'
forall (m :: * -> *) x' x b' b a' c' c.
Functor m =>
Proxy x' x b' b m a'
-> (b -> Proxy x' x c' c m b') -> Proxy x' x c' c m a'
(//>)
-- There are a number of useful rewrites which can be performed on various uses
-- of this combinator; to ensure that they fire we defer inlining until quite
-- late.
{-# INLINABLE [0] for #-}

{-# RULES
    "for (for p f) g" forall p f g . for (for p f) g = for p (\a -> for (f a) g)

  ; "for p yield" forall p . for p yield = p

  ; "for (yield x) f" forall x f . for (yield x) f = f x

  ; "for cat f" forall f .
        for cat f =
            let go = do
                    x <- await
                    f x
                    go
            in  go

  ; "f >~ (g >~ p)" forall f g p . f >~ (g >~ p) = (f >~ g) >~ p

  ; "await >~ p" forall p . await >~ p = p

  ; "p >~ await" forall p . p >~ await = p

  ; "m >~ cat" forall m .
        m >~ cat =
            let go = do
                    x <- m
                    yield x
                    go
            in  go

  ; "p1 >-> (p2 >-> p3)" forall p1 p2 p3 .
        p1 >-> (p2 >-> p3) = (p1 >-> p2) >-> p3

  ; "p >-> cat" forall p . p >-> cat = p

  ; "cat >-> p" forall p . cat >-> p = p

  #-}

{-| Compose loop bodies

@
('~>') :: 'Functor' m => (a -> 'Producer' b m r) -> (b -> 'Effect'       m ()) -> (a -> 'Effect'       m r)
('~>') :: 'Functor' m => (a -> 'Producer' b m r) -> (b -> 'Producer'   c m ()) -> (a -> 'Producer'   c m r)
('~>') :: 'Functor' m => (a -> 'Pipe'   x b m r) -> (b -> 'Consumer' x   m ()) -> (a -> 'Consumer' x   m r)
('~>') :: 'Functor' m => (a -> 'Pipe'   x b m r) -> (b -> 'Pipe'     x c m ()) -> (a -> 'Pipe'     x c m r)
@

    The following diagrams show the flow of information:

@
         a                    .--->   b                              a
         |                   /        |                              |
   +-----|-----+            /   +-----|-----+                 +------|------+
   |     v     |           /    |     v     |                 |      v      |
   |           |          /     |           |                 |             |
x ==>    f    ==> b   ---'   x ==>    g    ==> c     =     x ==>   f '~>' g  ==> c
   |           |                |           |                 |             |
   |     |     |                |     |     |                 |      |      |
   +-----|-----+                +-----|-----+                 +------|------+
         v                            v                              v
         r                            ()                             r
@

    For a more complete diagram including bidirectional flow, see "Pipes.Core#respond-diagram".
-}
(~>)
    :: Functor m
    => (a -> Proxy x' x b' b m a')
    -- ^
    -> (b -> Proxy x' x c' c m b')
    -- ^
    -> (a -> Proxy x' x c' c m a')
~> :: (a -> Proxy x' x b' b m a')
-> (b -> Proxy x' x c' c m b') -> a -> Proxy x' x c' c m a'
(~>) = (a -> Proxy x' x b' b m a')
-> (b -> Proxy x' x c' c m b') -> a -> Proxy x' x c' c m a'
forall (m :: * -> *) a x' x b' b a' c' c.
Functor m =>
(a -> Proxy x' x b' b m a')
-> (b -> Proxy x' x c' c m b') -> a -> Proxy x' x c' c m a'
(/>/)
{-# INLINABLE (~>) #-}

-- | ('~>') with the arguments flipped
(<~)
    :: Functor m
    => (b -> Proxy x' x c' c m b')
    -- ^
    -> (a -> Proxy x' x b' b m a')
    -- ^
    -> (a -> Proxy x' x c' c m a')
b -> Proxy x' x c' c m b'
g <~ :: (b -> Proxy x' x c' c m b')
-> (a -> Proxy x' x b' b m a') -> a -> Proxy x' x c' c m a'
<~ a -> Proxy x' x b' b m a'
f = a -> Proxy x' x b' b m a'
f (a -> Proxy x' x b' b m a')
-> (b -> Proxy x' x c' c m b') -> a -> Proxy x' x c' c m a'
forall (m :: * -> *) a x' x b' b a' c' c.
Functor m =>
(a -> Proxy x' x b' b m a')
-> (b -> Proxy x' x c' c m b') -> a -> Proxy x' x c' c m a'
~> b -> Proxy x' x c' c m b'
g
{-# INLINABLE (<~) #-}

{- $consumers
    Use 'await' to request input and ('>~') to substitute 'await's.

    'await' and ('>~') obey the 'Control.Category.Category' laws:

@
\-\- Substituting every \'await\' with another \'await\' does nothing
'await' '>~' f = f

\-\- Substituting \'await\' with \'f\' gives \'f\'
f '>~' 'await' = f

\-\- \'await\' substitution is associative
(f '>~' g) '>~' h = f '>~' (g '>~' h)
@

-}

{-| Consume a value

@
'await' :: 'Functor' m => 'Pipe' a y m a
@
-}
await :: Functor m => Consumer' a m a
await :: Consumer' a m a
await = () -> Proxy () a y' y m a
forall (m :: * -> *) a' a y' y.
Functor m =>
a' -> Proxy a' a y' y m a
request ()
{-# INLINABLE [1] await #-}

{-| @(draw >~ p)@ loops over @p@ replacing each 'await' with @draw@

@
('>~') :: 'Functor' m => 'Effect'       m b -> 'Consumer' b   m c -> 'Effect'       m c
('>~') :: 'Functor' m => 'Consumer' a   m b -> 'Consumer' b   m c -> 'Consumer' a   m c
('>~') :: 'Functor' m => 'Producer'   y m b -> 'Pipe'     b y m c -> 'Producer'   y m c
('>~') :: 'Functor' m => 'Pipe'     a y m b -> 'Pipe'     b y m c -> 'Pipe'     a y m c
@

    The following diagrams show the flow of information:

@
   +-----------+                 +-----------+                 +-------------+
   |           |                 |           |                 |             |
   |           |                 |           |                 |             |
a ==>    f    ==> y   .--->   b ==>    g    ==> y     =     a ==>   f '>~' g  ==> y
   |           |     /           |           |                 |             |
   |     |     |    /            |     |     |                 |      |      |
   +-----|-----+   /             +-----|-----+                 +------|------+
         v        /                    v                              v
         b   ----'                     c                              c
@

    For a more complete diagram including bidirectional flow, see "Pipes.Core#request-diagram".
-}
(>~)
    :: Functor m
    => Proxy a' a y' y m b
    -- ^
    -> Proxy () b y' y m c
    -- ^
    -> Proxy a' a y' y m c
Proxy a' a y' y m b
p1 >~ :: Proxy a' a y' y m b -> Proxy () b y' y m c -> Proxy a' a y' y m c
>~ Proxy () b y' y m c
p2 = (\() -> Proxy a' a y' y m b
p1) (() -> Proxy a' a y' y m b)
-> Proxy () b y' y m c -> Proxy a' a y' y m c
forall (m :: * -> *) b' a' a y' y b c.
Functor m =>
(b' -> Proxy a' a y' y m b)
-> Proxy b' b y' y m c -> Proxy a' a y' y m c
>\\ Proxy () b y' y m c
p2
{-# INLINABLE [1] (>~) #-}

-- | ('>~') with the arguments flipped
(~<)
    :: Functor m
    => Proxy () b y' y m c
    -- ^
    -> Proxy a' a y' y m b
    -- ^
    -> Proxy a' a y' y m c
Proxy () b y' y m c
p2 ~< :: Proxy () b y' y m c -> Proxy a' a y' y m b -> Proxy a' a y' y m c
~< Proxy a' a y' y m b
p1 = Proxy a' a y' y m b
p1 Proxy a' a y' y m b -> Proxy () b y' y m c -> Proxy a' a y' y m c
forall (m :: * -> *) a' a y' y b c.
Functor m =>
Proxy a' a y' y m b -> Proxy () b y' y m c -> Proxy a' a y' y m c
>~ Proxy () b y' y m c
p2
{-# INLINABLE (~<) #-}

{- $pipes
    Use 'await' and 'yield' to build 'Pipe's and ('>->') to connect 'Pipe's.

    'cat' and ('>->') obey the 'Control.Category.Category' laws:

@
\-\- Useless use of cat
'cat' '>->' f = f

\-\- Redirecting output to cat does nothing
f '>->' 'cat' = f

\-\- The pipe operator is associative
(f '>->' g) '>->' h = f '>->' (g '>->' h)
@

-}

-- | The identity 'Pipe', analogous to the Unix @cat@ program
cat :: Functor m => Pipe a a m r
cat :: Pipe a a m r
cat = () -> Pipe a a m r
forall (m :: * -> *) a' a r. Functor m => a' -> Proxy a' a a' a m r
pull ()
{-# INLINABLE [1] cat #-}

{-| 'Pipe' composition, analogous to the Unix pipe operator

@
('>->') :: 'Functor' m => 'Producer' b m r -> 'Consumer' b   m r -> 'Effect'       m r
('>->') :: 'Functor' m => 'Producer' b m r -> 'Pipe'     b c m r -> 'Producer'   c m r
('>->') :: 'Functor' m => 'Pipe'   a b m r -> 'Consumer' b   m r -> 'Consumer' a   m r
('>->') :: 'Functor' m => 'Pipe'   a b m r -> 'Pipe'     b c m r -> 'Pipe'     a c m r
@

    The following diagrams show the flow of information:

@
   +-----------+     +-----------+                 +-------------+
   |           |     |           |                 |             |
   |           |     |           |                 |             |
a ==>    f    ==> b ==>    g    ==> c     =     a ==>  f '>->' g  ==> c
   |           |     |           |                 |             |
   |     |     |     |     |     |                 |      |      |
   +-----|-----+     +-----|-----+                 +------|------+
         v                 v                              v
         r                 r                              r
@

    For a more complete diagram including bidirectional flow, see "Pipes.Core#pull-diagram".
-}
(>->)
    :: Functor m
    => Proxy a' a () b m r
    -- ^
    -> Proxy () b c' c m r
    -- ^
    -> Proxy a' a c' c m r
Proxy a' a () b m r
p1 >-> :: Proxy a' a () b m r -> Proxy () b c' c m r -> Proxy a' a c' c m r
>-> Proxy () b c' c m r
p2 = (\() -> Proxy a' a () b m r
p1) (() -> Proxy a' a () b m r)
-> Proxy () b c' c m r -> Proxy a' a c' c m r
forall (m :: * -> *) b' a' a b r c' c.
Functor m =>
(b' -> Proxy a' a b' b m r)
-> Proxy b' b c' c m r -> Proxy a' a c' c m r
+>> Proxy () b c' c m r
p2
{-# INLINABLE [1] (>->) #-}

{-| The list monad transformer, which extends a monad with non-determinism

    The type variables signify:

      * @m@ - The base monad
      * @a@ - The values that the computation 'yield's throughout its execution

    For basic construction and composition of 'ListT' computations, much can be
    accomplished using common typeclass methods.

      * 'return' corresponds to 'yield', yielding a single value.
      * ('>>=') corresponds to 'for', calling the second computation once
        for each time the first computation 'yield's.
      * 'mempty' neither 'yield's any values nor produces any effects in the
        base monad.
      * ('<>') sequences two computations, 'yield'ing all the values of the
        first followed by all the values of the second.
      * 'lift' converts an action in the base monad into a ListT computation
        which performs the action and 'yield's a single value.

    'ListT' is a newtype wrapper for 'Producer'. You will likely need to use
    'Select' and 'enumerate' to convert back and forth between these two types
    to take advantage of all the 'Producer'-related utilities that
    "Pipes.Prelude" has to offer.

      * To lift a plain list into a 'ListT' computation, first apply 'each'
        to turn the list into a 'Producer'. Then apply the 'Select'
        constructor to convert from 'Producer' to 'ListT'.
      * For other ways to construct 'ListT' computations, see the
        “Producers” section in "Pipes.Prelude" to build 'Producer's.
        These can then be converted to 'ListT' using 'Select'.
      * To aggregate the values from a 'ListT' computation (for example,
        to compute the sum of a 'ListT' of numbers), first apply
        'enumerate' to obtain a 'Producer'. Then see the “Folds”
        section in "Pipes.Prelude" to proceed.
-}
newtype ListT m a = Select { ListT m a -> Producer a m ()
enumerate :: Producer a m () }

instance Functor m => Functor (ListT m) where
    fmap :: (a -> b) -> ListT m a -> ListT m b
fmap a -> b
f ListT m a
p = Producer b m () -> ListT m b
forall (m :: * -> *) a. Producer a m () -> ListT m a
Select (Proxy X () () a m () -> (a -> Producer b m ()) -> Producer b m ()
forall (m :: * -> *) x' x b' b a' c' c.
Functor m =>
Proxy x' x b' b m a'
-> (b -> Proxy x' x c' c m b') -> Proxy x' x c' c m a'
for (ListT m a -> Proxy X () () a m ()
forall (m :: * -> *) a. ListT m a -> Producer a m ()
enumerate ListT m a
p) (\a
a -> b -> Producer b m ()
forall (m :: * -> *) a x' x. Functor m => a -> Proxy x' x () a m ()
yield (a -> b
f a
a)))
    {-# INLINE fmap #-}

instance Functor m => Applicative (ListT m) where
    pure :: a -> ListT m a
pure a
a = Producer a m () -> ListT m a
forall (m :: * -> *) a. Producer a m () -> ListT m a
Select (a -> Producer a m ()
forall (m :: * -> *) a x' x. Functor m => a -> Proxy x' x () a m ()
yield a
a)
    {-# INLINE pure #-}
    ListT m (a -> b)
mf <*> :: ListT m (a -> b) -> ListT m a -> ListT m b
<*> ListT m a
mx = Producer b m () -> ListT m b
forall (m :: * -> *) a. Producer a m () -> ListT m a
Select (
        Proxy X () () (a -> b) m ()
-> ((a -> b) -> Producer b m ()) -> Producer b m ()
forall (m :: * -> *) x' x b' b a' c' c.
Functor m =>
Proxy x' x b' b m a'
-> (b -> Proxy x' x c' c m b') -> Proxy x' x c' c m a'
for (ListT m (a -> b) -> Proxy X () () (a -> b) m ()
forall (m :: * -> *) a. ListT m a -> Producer a m ()
enumerate ListT m (a -> b)
mf) (\a -> b
f ->
        Proxy X () () a m () -> (a -> Producer b m ()) -> Producer b m ()
forall (m :: * -> *) x' x b' b a' c' c.
Functor m =>
Proxy x' x b' b m a'
-> (b -> Proxy x' x c' c m b') -> Proxy x' x c' c m a'
for (ListT m a -> Proxy X () () a m ()
forall (m :: * -> *) a. ListT m a -> Producer a m ()
enumerate ListT m a
mx) (\a
x ->
        b -> Producer b m ()
forall (m :: * -> *) a x' x. Functor m => a -> Proxy x' x () a m ()
yield (a -> b
f a
x) ) ) )

instance Monad m => Monad (ListT m) where
    return :: a -> ListT m a
return   = a -> ListT m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure
    {-# INLINE return #-}
    ListT m a
m >>= :: ListT m a -> (a -> ListT m b) -> ListT m b
>>= a -> ListT m b
f  = Producer b m () -> ListT m b
forall (m :: * -> *) a. Producer a m () -> ListT m a
Select (Proxy X () () a m () -> (a -> Producer b m ()) -> Producer b m ()
forall (m :: * -> *) x' x b' b a' c' c.
Functor m =>
Proxy x' x b' b m a'
-> (b -> Proxy x' x c' c m b') -> Proxy x' x c' c m a'
for (ListT m a -> Proxy X () () a m ()
forall (m :: * -> *) a. ListT m a -> Producer a m ()
enumerate ListT m a
m) (\a
a -> ListT m b -> Producer b m ()
forall (m :: * -> *) a. ListT m a -> Producer a m ()
enumerate (a -> ListT m b
f a
a)))
    {-# INLINE (>>=) #-}
#if !MIN_VERSION_base(4,13,0)
    fail _   = mzero
    {-# INLINE fail #-}
#endif

instance Monad m => MonadFail (ListT m) where
    fail :: String -> ListT m a
fail String
_ = ListT m a
forall (m :: * -> *) a. MonadPlus m => m a
mzero
    {-# INLINE fail #-}

instance Foldable m => Foldable (ListT m) where
    foldMap :: (a -> m) -> ListT m a -> m
foldMap a -> m
f = Proxy X () () a m () -> m
forall (t :: * -> *) a r. Foldable t => Proxy X a () a t r -> m
go (Proxy X () () a m () -> m)
-> (ListT m a -> Proxy X () () a m ()) -> ListT m a -> m
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ListT m a -> Proxy X () () a m ()
forall (m :: * -> *) a. ListT m a -> Producer a m ()
enumerate
      where
        go :: Proxy X a () a t r -> m
go Proxy X a () a t r
p = case Proxy X a () a t r
p of
            Request X
v a -> Proxy X a () a t r
_  -> X -> m
forall a. X -> a
closed X
v
            Respond a
a () -> Proxy X a () a t r
fu -> a -> m
f a
a m -> m -> m
forall a. Monoid a => a -> a -> a
`mappend` Proxy X a () a t r -> m
go (() -> Proxy X a () a t r
fu ())
            M       t (Proxy X a () a t r)
m    -> (Proxy X a () a t r -> m) -> t (Proxy X a () a t r) -> m
forall (t :: * -> *) m a.
(Foldable t, Monoid m) =>
(a -> m) -> t a -> m
F.foldMap Proxy X a () a t r -> m
go t (Proxy X a () a t r)
m
            Pure    r
_    -> m
forall a. Monoid a => a
mempty
    {-# INLINE foldMap #-}

instance (Functor m, Traversable m) => Traversable (ListT m) where
    traverse :: (a -> f b) -> ListT m a -> f (ListT m b)
traverse a -> f b
k (Select Producer a m ()
p) = (Producer b m () -> ListT m b)
-> f (Producer b m ()) -> f (ListT m b)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Producer b m () -> ListT m b
forall (m :: * -> *) a. Producer a m () -> ListT m a
Select (Producer a m () -> f (Producer b m ())
forall (m :: * -> *) a r a' a b'.
Traversable m =>
Proxy X a () a m r -> f (Proxy a' a b' b m r)
traverse_ Producer a m ()
p)
      where
        traverse_ :: Proxy X a () a m r -> f (Proxy a' a b' b m r)
traverse_ (Request X
v a -> Proxy X a () a m r
_ ) = X -> f (Proxy a' a b' b m r)
forall a. X -> a
closed X
v
        traverse_ (Respond a
a () -> Proxy X a () a m r
fu) = b -> Proxy a' a b' b m r -> Proxy a' a b' b m r
forall b a' a b' (m :: * -> *) r.
b -> Proxy a' a b' b m r -> Proxy a' a b' b m r
_Respond (b -> Proxy a' a b' b m r -> Proxy a' a b' b m r)
-> f b -> f (Proxy a' a b' b m r -> Proxy a' a b' b m r)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> a -> f b
k a
a f (Proxy a' a b' b m r -> Proxy a' a b' b m r)
-> f (Proxy a' a b' b m r) -> f (Proxy a' a b' b m r)
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Proxy X a () a m r -> f (Proxy a' a b' b m r)
traverse_ (() -> Proxy X a () a m r
fu ())
          where
            _Respond :: b -> Proxy a' a b' b m r -> Proxy a' a b' b m r
_Respond b
a_ Proxy a' a b' b m r
a' = b -> (b' -> Proxy a' a b' b m r) -> Proxy a' a b' b m r
forall a' a b' b (m :: * -> *) r.
b -> (b' -> Proxy a' a b' b m r) -> Proxy a' a b' b m r
Respond b
a_ (\b'
_ -> Proxy a' a b' b m r
a')
        traverse_ (M       m (Proxy X a () a m r)
m   ) = (m (Proxy a' a b' b m r) -> Proxy a' a b' b m r)
-> f (m (Proxy a' a b' b m r)) -> f (Proxy a' a b' b m r)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap m (Proxy a' a b' b m r) -> Proxy a' a b' b m r
forall a' a b' b (m :: * -> *) r.
m (Proxy a' a b' b m r) -> Proxy a' a b' b m r
M ((Proxy X a () a m r -> f (Proxy a' a b' b m r))
-> m (Proxy X a () a m r) -> f (m (Proxy a' a b' b m r))
forall (t :: * -> *) (f :: * -> *) a b.
(Traversable t, Applicative f) =>
(a -> f b) -> t a -> f (t b)
traverse Proxy X a () a m r -> f (Proxy a' a b' b m r)
traverse_ m (Proxy X a () a m r)
m)
        traverse_ (Pure     r
r  ) = Proxy a' a b' b m r -> f (Proxy a' a b' b m r)
forall (f :: * -> *) a. Applicative f => a -> f a
pure (r -> Proxy a' a b' b m r
forall a' a b' b (m :: * -> *) r. r -> Proxy a' a b' b m r
Pure r
r)

instance MonadTrans ListT where
    lift :: m a -> ListT m a
lift m a
m = Producer a m () -> ListT m a
forall (m :: * -> *) a. Producer a m () -> ListT m a
Select (do
        a
a <- m a -> Proxy X () () a m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift m a
m
        a -> Producer a m ()
forall (m :: * -> *) a x' x. Functor m => a -> Proxy x' x () a m ()
yield a
a )

instance (MonadIO m) => MonadIO (ListT m) where
    liftIO :: IO a -> ListT m a
liftIO IO a
m = m a -> ListT m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (IO a -> m a
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO IO a
m)
    {-# INLINE liftIO #-}

instance (Functor m) => Alternative (ListT m) where
    empty :: ListT m a
empty = Producer a m () -> ListT m a
forall (m :: * -> *) a. Producer a m () -> ListT m a
Select (() -> Producer a m ()
forall (m :: * -> *) a. Monad m => a -> m a
return ())
    {-# INLINE empty #-}
    ListT m a
p1 <|> :: ListT m a -> ListT m a -> ListT m a
<|> ListT m a
p2 = Producer a m () -> ListT m a
forall (m :: * -> *) a. Producer a m () -> ListT m a
Select (do
        ListT m a -> Producer a m ()
forall (m :: * -> *) a. ListT m a -> Producer a m ()
enumerate ListT m a
p1
        ListT m a -> Producer a m ()
forall (m :: * -> *) a. ListT m a -> Producer a m ()
enumerate ListT m a
p2 )

instance (Monad m) => MonadPlus (ListT m) where
    mzero :: ListT m a
mzero = ListT m a
forall (f :: * -> *) a. Alternative f => f a
empty
    {-# INLINE mzero #-}
    mplus :: ListT m a -> ListT m a -> ListT m a
mplus = ListT m a -> ListT m a -> ListT m a
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
(<|>)
    {-# INLINE mplus #-}

instance MFunctor ListT where
    hoist :: (forall a. m a -> n a) -> ListT m b -> ListT n b
hoist forall a. m a -> n a
morph = Producer b n () -> ListT n b
forall (m :: * -> *) a. Producer a m () -> ListT m a
Select (Producer b n () -> ListT n b)
-> (ListT m b -> Producer b n ()) -> ListT m b -> ListT n b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (forall a. m a -> n a) -> Proxy X () () b m () -> Producer b n ()
forall k (t :: (* -> *) -> k -> *) (m :: * -> *) (n :: * -> *)
       (b :: k).
(MFunctor t, Monad m) =>
(forall a. m a -> n a) -> t m b -> t n b
hoist forall a. m a -> n a
morph (Proxy X () () b m () -> Producer b n ())
-> (ListT m b -> Proxy X () () b m ())
-> ListT m b
-> Producer b n ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ListT m b -> Proxy X () () b m ()
forall (m :: * -> *) a. ListT m a -> Producer a m ()
enumerate
    {-# INLINE hoist #-}

instance MMonad ListT where
    embed :: (forall a. m a -> ListT n a) -> ListT m b -> ListT n b
embed forall a. m a -> ListT n a
f (Select Producer b m ()
p0) = Producer b n () -> ListT n b
forall (m :: * -> *) a. Producer a m () -> ListT m a
Select (Producer b m () -> Producer b n ()
forall c' c. Proxy X () c' c m () -> Proxy X () c' c n ()
loop Producer b m ()
p0)
      where
        loop :: Proxy X () c' c m () -> Proxy X () c' c n ()
loop (Request X
a' () -> Proxy X () c' c m ()
fa ) = X -> (() -> Proxy X () c' c n ()) -> Proxy X () c' c n ()
forall a' a b' b (m :: * -> *) r.
a' -> (a -> Proxy a' a b' b m r) -> Proxy a' a b' b m r
Request X
a' (\()
a  -> Proxy X () c' c m () -> Proxy X () c' c n ()
loop (() -> Proxy X () c' c m ()
fa  ()
a ))
        loop (Respond c
b  c' -> Proxy X () c' c m ()
fb') = c -> (c' -> Proxy X () c' c n ()) -> Proxy X () c' c n ()
forall a' a b' b (m :: * -> *) r.
b -> (b' -> Proxy a' a b' b m r) -> Proxy a' a b' b m r
Respond c
b  (\c'
b' -> Proxy X () c' c m () -> Proxy X () c' c n ()
loop (c' -> Proxy X () c' c m ()
fb' c'
b'))
        loop (M          m (Proxy X () c' c m ())
m  ) = Proxy X () () (Proxy X () c' c n ()) n ()
-> (Proxy X () c' c n () -> Proxy X () c' c n ())
-> Proxy X () c' c n ()
forall (m :: * -> *) x' x b' b a' c' c.
Functor m =>
Proxy x' x b' b m a'
-> (b -> Proxy x' x c' c m b') -> Proxy x' x c' c m a'
for (ListT n (Proxy X () c' c n ())
-> Proxy X () () (Proxy X () c' c n ()) n ()
forall (m :: * -> *) a. ListT m a -> Producer a m ()
enumerate ((Proxy X () c' c m () -> Proxy X () c' c n ())
-> ListT n (Proxy X () c' c m ()) -> ListT n (Proxy X () c' c n ())
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Proxy X () c' c m () -> Proxy X () c' c n ()
loop (m (Proxy X () c' c m ()) -> ListT n (Proxy X () c' c m ())
forall a. m a -> ListT n a
f m (Proxy X () c' c m ())
m))) Proxy X () c' c n () -> Proxy X () c' c n ()
forall a. a -> a
id
        loop (Pure    ()
r     ) = () -> Proxy X () c' c n ()
forall a' a b' b (m :: * -> *) r. r -> Proxy a' a b' b m r
Pure ()
r
    {-# INLINE embed #-}

instance (Functor m) => Semigroup (ListT m a) where
    <> :: ListT m a -> ListT m a -> ListT m a
(<>) = ListT m a -> ListT m a -> ListT m a
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
(<|>)
    {-# INLINE (<>) #-}

instance (Functor m) => Monoid (ListT m a) where
    mempty :: ListT m a
mempty = ListT m a
forall (f :: * -> *) a. Alternative f => f a
empty
    {-# INLINE mempty #-}
#if !(MIN_VERSION_base(4,11,0))
    mappend = (<|>)
    {-# INLINE mappend #-}
#endif

instance (MonadState s m) => MonadState s (ListT m) where
    get :: ListT m s
get     = m s -> ListT m s
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift  m s
forall s (m :: * -> *). MonadState s m => m s
get
    {-# INLINE get #-}

    put :: s -> ListT m ()
put   s
s = m () -> ListT m ()
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (s -> m ()
forall s (m :: * -> *). MonadState s m => s -> m ()
put   s
s)
    {-# INLINE put #-}

    state :: (s -> (a, s)) -> ListT m a
state s -> (a, s)
f = m a -> ListT m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift ((s -> (a, s)) -> m a
forall s (m :: * -> *) a. MonadState s m => (s -> (a, s)) -> m a
state s -> (a, s)
f)
    {-# INLINE state #-}

instance (MonadWriter w m) => MonadWriter w (ListT m) where
    writer :: (a, w) -> ListT m a
writer = m a -> ListT m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (m a -> ListT m a) -> ((a, w) -> m a) -> (a, w) -> ListT m a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (a, w) -> m a
forall w (m :: * -> *) a. MonadWriter w m => (a, w) -> m a
writer
    {-# INLINE writer #-}

    tell :: w -> ListT m ()
tell w
w = m () -> ListT m ()
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (w -> m ()
forall w (m :: * -> *). MonadWriter w m => w -> m ()
tell w
w)
    {-# INLINE tell #-}

    listen :: ListT m a -> ListT m (a, w)
listen ListT m a
l = Producer (a, w) m () -> ListT m (a, w)
forall (m :: * -> *) a. Producer a m () -> ListT m a
Select (Proxy X () () a m () -> w -> Producer (a, w) m ()
forall (m :: * -> *) a a' a b' a r.
MonadWriter a m =>
Proxy a' a b' a m r -> a -> Proxy a' a b' (a, a) m r
go (ListT m a -> Proxy X () () a m ()
forall (m :: * -> *) a. ListT m a -> Producer a m ()
enumerate ListT m a
l) w
forall a. Monoid a => a
mempty)
      where
        go :: Proxy a' a b' a m r -> a -> Proxy a' a b' (a, a) m r
go Proxy a' a b' a m r
p a
w = case Proxy a' a b' a m r
p of
            Request a'
a' a -> Proxy a' a b' a m r
fa  -> a' -> (a -> Proxy a' a b' (a, a) m r) -> Proxy a' a b' (a, a) m r
forall a' a b' b (m :: * -> *) r.
a' -> (a -> Proxy a' a b' b m r) -> Proxy a' a b' b m r
Request a'
a' (\a
a  -> Proxy a' a b' a m r -> a -> Proxy a' a b' (a, a) m r
go (a -> Proxy a' a b' a m r
fa  a
a ) a
w)
            Respond a
b  b' -> Proxy a' a b' a m r
fb' -> (a, a)
-> (b' -> Proxy a' a b' (a, a) m r) -> Proxy a' a b' (a, a) m r
forall a' a b' b (m :: * -> *) r.
b -> (b' -> Proxy a' a b' b m r) -> Proxy a' a b' b m r
Respond (a
b, a
w)  (\b'
b' -> Proxy a' a b' a m r -> a -> Proxy a' a b' (a, a) m r
go (b' -> Proxy a' a b' a m r
fb' b'
b') a
w)
            M          m (Proxy a' a b' a m r)
m   -> m (Proxy a' a b' (a, a) m r) -> Proxy a' a b' (a, a) m r
forall a' a b' b (m :: * -> *) r.
m (Proxy a' a b' b m r) -> Proxy a' a b' b m r
M (do
                (Proxy a' a b' a m r
p', a
w') <- m (Proxy a' a b' a m r) -> m (Proxy a' a b' a m r, a)
forall w (m :: * -> *) a. MonadWriter w m => m a -> m (a, w)
listen m (Proxy a' a b' a m r)
m
                Proxy a' a b' (a, a) m r -> m (Proxy a' a b' (a, a) m r)
forall (m :: * -> *) a. Monad m => a -> m a
return (Proxy a' a b' a m r -> a -> Proxy a' a b' (a, a) m r
go Proxy a' a b' a m r
p' (a -> Proxy a' a b' (a, a) m r) -> a -> Proxy a' a b' (a, a) m r
forall a b. (a -> b) -> a -> b
$! a -> a -> a
forall a. Monoid a => a -> a -> a
mappend a
w a
w') )
            Pure    r
r      -> r -> Proxy a' a b' (a, a) m r
forall a' a b' b (m :: * -> *) r. r -> Proxy a' a b' b m r
Pure r
r

    pass :: ListT m (a, w -> w) -> ListT m a
pass ListT m (a, w -> w)
l = Producer a m () -> ListT m a
forall (m :: * -> *) a. Producer a m () -> ListT m a
Select (Proxy X () () (a, w -> w) m () -> w -> Producer a m ()
forall a (m :: * -> *) a' a b' b r.
MonadWriter a m =>
Proxy a' a b' (b, a -> a) m r -> a -> Proxy a' a b' b m r
go (ListT m (a, w -> w) -> Proxy X () () (a, w -> w) m ()
forall (m :: * -> *) a. ListT m a -> Producer a m ()
enumerate ListT m (a, w -> w)
l) w
forall a. Monoid a => a
mempty)
      where
        go :: Proxy a' a b' (b, a -> a) m r -> a -> Proxy a' a b' b m r
go Proxy a' a b' (b, a -> a) m r
p a
w = case Proxy a' a b' (b, a -> a) m r
p of
            Request  a'
a'     a -> Proxy a' a b' (b, a -> a) m r
fa  -> a' -> (a -> Proxy a' a b' b m r) -> Proxy a' a b' b m r
forall a' a b' b (m :: * -> *) r.
a' -> (a -> Proxy a' a b' b m r) -> Proxy a' a b' b m r
Request a'
a' (\a
a  -> Proxy a' a b' (b, a -> a) m r -> a -> Proxy a' a b' b m r
go (a -> Proxy a' a b' (b, a -> a) m r
fa  a
a ) a
w)
            Respond (b
b, a -> a
f)  b' -> Proxy a' a b' (b, a -> a) m r
fb' -> m (Proxy a' a b' b m r) -> Proxy a' a b' b m r
forall a' a b' b (m :: * -> *) r.
m (Proxy a' a b' b m r) -> Proxy a' a b' b m r
M (m (Proxy a' a b' b m r, a -> a) -> m (Proxy a' a b' b m r)
forall w (m :: * -> *) a. MonadWriter w m => m (a, w -> w) -> m a
pass ((Proxy a' a b' b m r, a -> a) -> m (Proxy a' a b' b m r, a -> a)
forall (m :: * -> *) a. Monad m => a -> m a
return
                (b -> (b' -> Proxy a' a b' b m r) -> Proxy a' a b' b m r
forall a' a b' b (m :: * -> *) r.
b -> (b' -> Proxy a' a b' b m r) -> Proxy a' a b' b m r
Respond b
b (\b'
b' -> Proxy a' a b' (b, a -> a) m r -> a -> Proxy a' a b' b m r
go (b' -> Proxy a' a b' (b, a -> a) m r
fb' b'
b') (a -> a
f a
w)), \a
_ -> a -> a
f a
w) ))
            M               m (Proxy a' a b' (b, a -> a) m r)
m   -> m (Proxy a' a b' b m r) -> Proxy a' a b' b m r
forall a' a b' b (m :: * -> *) r.
m (Proxy a' a b' b m r) -> Proxy a' a b' b m r
M (do
                (Proxy a' a b' (b, a -> a) m r
p', a
w') <- m (Proxy a' a b' (b, a -> a) m r)
-> m (Proxy a' a b' (b, a -> a) m r, a)
forall w (m :: * -> *) a. MonadWriter w m => m a -> m (a, w)
listen m (Proxy a' a b' (b, a -> a) m r)
m
                Proxy a' a b' b m r -> m (Proxy a' a b' b m r)
forall (m :: * -> *) a. Monad m => a -> m a
return (Proxy a' a b' (b, a -> a) m r -> a -> Proxy a' a b' b m r
go Proxy a' a b' (b, a -> a) m r
p' (a -> Proxy a' a b' b m r) -> a -> Proxy a' a b' b m r
forall a b. (a -> b) -> a -> b
$! a -> a -> a
forall a. Monoid a => a -> a -> a
mappend a
w a
w') )
            Pure     r
r          -> r -> Proxy a' a b' b m r
forall a' a b' b (m :: * -> *) r. r -> Proxy a' a b' b m r
Pure r
r

instance (MonadReader i m) => MonadReader i (ListT m) where
    ask :: ListT m i
ask = m i -> ListT m i
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift m i
forall r (m :: * -> *). MonadReader r m => m r
ask
    {-# INLINE ask #-}

    local :: (i -> i) -> ListT m a -> ListT m a
local i -> i
f ListT m a
l = Producer a m () -> ListT m a
forall (m :: * -> *) a. Producer a m () -> ListT m a
Select ((i -> i) -> Producer a m () -> Producer a m ()
forall r (m :: * -> *) a. MonadReader r m => (r -> r) -> m a -> m a
local i -> i
f (ListT m a -> Producer a m ()
forall (m :: * -> *) a. ListT m a -> Producer a m ()
enumerate ListT m a
l))
    {-# INLINE local #-}

    reader :: (i -> a) -> ListT m a
reader i -> a
f = m a -> ListT m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift ((i -> a) -> m a
forall r (m :: * -> *) a. MonadReader r m => (r -> a) -> m a
reader i -> a
f)
    {-# INLINE reader #-}

instance (MonadError e m) => MonadError e (ListT m) where
    throwError :: e -> ListT m a
throwError e
e = m a -> ListT m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (e -> m a
forall e (m :: * -> *) a. MonadError e m => e -> m a
throwError e
e)
    {-# INLINE throwError #-}

    catchError :: ListT m a -> (e -> ListT m a) -> ListT m a
catchError ListT m a
l e -> ListT m a
k = Producer a m () -> ListT m a
forall (m :: * -> *) a. Producer a m () -> ListT m a
Select (Producer a m () -> (e -> Producer a m ()) -> Producer a m ()
forall e (m :: * -> *) a.
MonadError e m =>
m a -> (e -> m a) -> m a
catchError (ListT m a -> Producer a m ()
forall (m :: * -> *) a. ListT m a -> Producer a m ()
enumerate ListT m a
l) (\e
e -> ListT m a -> Producer a m ()
forall (m :: * -> *) a. ListT m a -> Producer a m ()
enumerate (e -> ListT m a
k e
e)))
    {-# INLINE catchError #-}

instance MonadThrow m => MonadThrow (ListT m) where
    throwM :: e -> ListT m a
throwM = Producer a m () -> ListT m a
forall (m :: * -> *) a. Producer a m () -> ListT m a
Select (Producer a m () -> ListT m a)
-> (e -> Producer a m ()) -> e -> ListT m a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. e -> Producer a m ()
forall (m :: * -> *) e a. (MonadThrow m, Exception e) => e -> m a
throwM
    {-# INLINE throwM #-}

instance MonadCatch m => MonadCatch (ListT m) where
    catch :: ListT m a -> (e -> ListT m a) -> ListT m a
catch ListT m a
l e -> ListT m a
k = Producer a m () -> ListT m a
forall (m :: * -> *) a. Producer a m () -> ListT m a
Select (Producer a m () -> (e -> Producer a m ()) -> Producer a m ()
forall (m :: * -> *) e a.
(MonadCatch m, Exception e) =>
m a -> (e -> m a) -> m a
Control.Monad.Catch.catch (ListT m a -> Producer a m ()
forall (m :: * -> *) a. ListT m a -> Producer a m ()
enumerate ListT m a
l) (\e
e -> ListT m a -> Producer a m ()
forall (m :: * -> *) a. ListT m a -> Producer a m ()
enumerate (e -> ListT m a
k e
e)))
    {-# INLINE catch #-}

instance Monad m => MonadZip (ListT m) where
    mzipWith :: (a -> b -> c) -> ListT m a -> ListT m b -> ListT m c
mzipWith a -> b -> c
f = ListT m a -> ListT m b -> ListT m c
forall (m :: * -> *).
Monad m =>
ListT m a -> ListT m b -> ListT m c
go
      where
        go :: ListT m a -> ListT m b -> ListT m c
go ListT m a
xs ListT m b
ys = Producer c m () -> ListT m c
forall (m :: * -> *) a. Producer a m () -> ListT m a
Select (Producer c m () -> ListT m c) -> Producer c m () -> ListT m c
forall a b. (a -> b) -> a -> b
$ do
            Either () (a, Producer a m ())
xres <- m (Either () (a, Producer a m ()))
-> Proxy X () () c m (Either () (a, Producer a m ()))
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (m (Either () (a, Producer a m ()))
 -> Proxy X () () c m (Either () (a, Producer a m ())))
-> m (Either () (a, Producer a m ()))
-> Proxy X () () c m (Either () (a, Producer a m ()))
forall a b. (a -> b) -> a -> b
$ Producer a m () -> m (Either () (a, Producer a m ()))
forall (m :: * -> *) a r.
Monad m =>
Producer a m r -> m (Either r (a, Producer a m r))
next (ListT m a -> Producer a m ()
forall (m :: * -> *) a. ListT m a -> Producer a m ()
enumerate ListT m a
xs)
            case Either () (a, Producer a m ())
xres of
                Left ()
r -> () -> Producer c m ()
forall (m :: * -> *) a. Monad m => a -> m a
return ()
r
                Right (a
x, Producer a m ()
xnext) -> do
                    Either () (b, Producer b m ())
yres <- m (Either () (b, Producer b m ()))
-> Proxy X () () c m (Either () (b, Producer b m ()))
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (m (Either () (b, Producer b m ()))
 -> Proxy X () () c m (Either () (b, Producer b m ())))
-> m (Either () (b, Producer b m ()))
-> Proxy X () () c m (Either () (b, Producer b m ()))
forall a b. (a -> b) -> a -> b
$ Producer b m () -> m (Either () (b, Producer b m ()))
forall (m :: * -> *) a r.
Monad m =>
Producer a m r -> m (Either r (a, Producer a m r))
next (ListT m b -> Producer b m ()
forall (m :: * -> *) a. ListT m a -> Producer a m ()
enumerate ListT m b
ys)
                    case Either () (b, Producer b m ())
yres of
                        Left ()
r -> () -> Producer c m ()
forall (m :: * -> *) a. Monad m => a -> m a
return ()
r
                        Right (b
y, Producer b m ()
ynext) -> do
                            c -> Producer c m ()
forall (m :: * -> *) a x' x. Functor m => a -> Proxy x' x () a m ()
yield (a -> b -> c
f a
x b
y)
                            ListT m c -> Producer c m ()
forall (m :: * -> *) a. ListT m a -> Producer a m ()
enumerate (ListT m a -> ListT m b -> ListT m c
go (Producer a m () -> ListT m a
forall (m :: * -> *) a. Producer a m () -> ListT m a
Select Producer a m ()
xnext) (Producer b m () -> ListT m b
forall (m :: * -> *) a. Producer a m () -> ListT m a
Select Producer b m ()
ynext))

-- | Run a self-contained `ListT` computation
runListT :: Monad m => ListT m a -> m ()
runListT :: ListT m a -> m ()
runListT ListT m a
l = Effect m () -> m ()
forall (m :: * -> *) r. Monad m => Effect m r -> m r
runEffect (ListT m X -> Effect m ()
forall (m :: * -> *) a. ListT m a -> Producer a m ()
enumerate (ListT m a
l ListT m a -> ListT m X -> ListT m X
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> ListT m X
forall (m :: * -> *) a. MonadPlus m => m a
mzero))
{-# INLINABLE runListT #-}

{-| 'Enumerable' generalizes 'Data.Foldable.Foldable', converting effectful
    containers to 'ListT's.

    Instances of 'Enumerable' must satisfy these two laws:

> toListT (return r) = return r
>
> toListT $ do x <- m  =  do x <- toListT m
>              f x           toListT (f x)

    In other words, 'toListT' is monad morphism.
-}
class Enumerable t where
    toListT :: Monad m => t m a -> ListT m a

instance Enumerable ListT where
    toListT :: ListT m a -> ListT m a
toListT = ListT m a -> ListT m a
forall a. a -> a
id

instance Enumerable IdentityT where
    toListT :: IdentityT m a -> ListT m a
toListT IdentityT m a
m = Producer a m () -> ListT m a
forall (m :: * -> *) a. Producer a m () -> ListT m a
Select (Producer a m () -> ListT m a) -> Producer a m () -> ListT m a
forall a b. (a -> b) -> a -> b
$ do
        a
a <- m a -> Proxy X () () a m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (m a -> Proxy X () () a m a) -> m a -> Proxy X () () a m a
forall a b. (a -> b) -> a -> b
$ IdentityT m a -> m a
forall k (f :: k -> *) (a :: k). IdentityT f a -> f a
runIdentityT IdentityT m a
m
        a -> Producer a m ()
forall (m :: * -> *) a x' x. Functor m => a -> Proxy x' x () a m ()
yield a
a

instance Enumerable MaybeT where
    toListT :: MaybeT m a -> ListT m a
toListT MaybeT m a
m = Producer a m () -> ListT m a
forall (m :: * -> *) a. Producer a m () -> ListT m a
Select (Producer a m () -> ListT m a) -> Producer a m () -> ListT m a
forall a b. (a -> b) -> a -> b
$ do
        Maybe a
x <- m (Maybe a) -> Proxy X () () a m (Maybe a)
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (m (Maybe a) -> Proxy X () () a m (Maybe a))
-> m (Maybe a) -> Proxy X () () a m (Maybe a)
forall a b. (a -> b) -> a -> b
$ MaybeT m a -> m (Maybe a)
forall (m :: * -> *) a. MaybeT m a -> m (Maybe a)
runMaybeT MaybeT m a
m
        case Maybe a
x of
            Maybe a
Nothing -> () -> Producer a m ()
forall (m :: * -> *) a. Monad m => a -> m a
return ()
            Just a
a  -> a -> Producer a m ()
forall (m :: * -> *) a x' x. Functor m => a -> Proxy x' x () a m ()
yield a
a

instance Enumerable (ExceptT e) where
    toListT :: ExceptT e m a -> ListT m a
toListT ExceptT e m a
m = Producer a m () -> ListT m a
forall (m :: * -> *) a. Producer a m () -> ListT m a
Select (Producer a m () -> ListT m a) -> Producer a m () -> ListT m a
forall a b. (a -> b) -> a -> b
$ do
        Either e a
x <- m (Either e a) -> Proxy X () () a m (Either e a)
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (m (Either e a) -> Proxy X () () a m (Either e a))
-> m (Either e a) -> Proxy X () () a m (Either e a)
forall a b. (a -> b) -> a -> b
$ ExceptT e m a -> m (Either e a)
forall e (m :: * -> *) a. ExceptT e m a -> m (Either e a)
runExceptT ExceptT e m a
m
        case Either e a
x of
            Left  e
_ -> () -> Producer a m ()
forall (m :: * -> *) a. Monad m => a -> m a
return ()
            Right a
a -> a -> Producer a m ()
forall (m :: * -> *) a x' x. Functor m => a -> Proxy x' x () a m ()
yield a
a

{-| Consume the first value from a 'Producer'

    'next' either fails with a 'Left' if the 'Producer' terminates or succeeds
    with a 'Right' providing the next value and the remainder of the 'Producer'.
-}
next :: Monad m => Producer a m r -> m (Either r (a, Producer a m r))
next :: Producer a m r -> m (Either r (a, Producer a m r))
next = Producer a m r -> m (Either r (a, Producer a m r))
forall (m :: * -> *) a a a.
Monad m =>
Proxy X a () a m a -> m (Either a (a, Proxy X a () a m a))
go
  where
    go :: Proxy X a () a m a -> m (Either a (a, Proxy X a () a m a))
go Proxy X a () a m a
p = case Proxy X a () a m a
p of
        Request X
v a -> Proxy X a () a m a
_  -> X -> m (Either a (a, Proxy X a () a m a))
forall a. X -> a
closed X
v
        Respond a
a () -> Proxy X a () a m a
fu -> Either a (a, Proxy X a () a m a)
-> m (Either a (a, Proxy X a () a m a))
forall (m :: * -> *) a. Monad m => a -> m a
return ((a, Proxy X a () a m a) -> Either a (a, Proxy X a () a m a)
forall a b. b -> Either a b
Right (a
a, () -> Proxy X a () a m a
fu ()))
        M         m (Proxy X a () a m a)
m  -> m (Proxy X a () a m a)
m m (Proxy X a () a m a)
-> (Proxy X a () a m a -> m (Either a (a, Proxy X a () a m a)))
-> m (Either a (a, Proxy X a () a m a))
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= Proxy X a () a m a -> m (Either a (a, Proxy X a () a m a))
go
        Pure    a
r    -> Either a (a, Proxy X a () a m a)
-> m (Either a (a, Proxy X a () a m a))
forall (m :: * -> *) a. Monad m => a -> m a
return (a -> Either a (a, Proxy X a () a m a)
forall a b. a -> Either a b
Left a
r)
{-# INLINABLE next #-}

{-| Convert a 'F.Foldable' to a 'Producer'

@
'each' :: ('Functor' m, 'Foldable' f) => f a -> 'Producer' a m ()
@
-}
each :: (Functor m, Foldable f) => f a -> Proxy x' x () a m ()
each :: f a -> Proxy x' x () a m ()
each = (a -> Proxy x' x () a m () -> Proxy x' x () a m ())
-> Proxy x' x () a m () -> f a -> Proxy x' x () a m ()
forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
F.foldr (\a
a Proxy x' x () a m ()
p -> a -> Proxy x' x () a m ()
forall (m :: * -> *) a x' x. Functor m => a -> Proxy x' x () a m ()
yield a
a Proxy x' x () a m ()
-> Proxy x' x () a m () -> Proxy x' x () a m ()
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> Proxy x' x () a m ()
p) (() -> Proxy x' x () a m ()
forall (m :: * -> *) a. Monad m => a -> m a
return ())
{-# INLINABLE each #-}
{-  The above code is the same as:

> each = Data.Foldable.mapM_ yield

    ... except writing it directly in terms of `Data.Foldable.foldr` improves
    build/foldr fusion
-}

{-| Convert an 'Enumerable' to a 'Producer'

@
'every' :: ('Monad' m, 'Enumerable' t) => t m a -> 'Producer' a m ()
@
-}
every :: (Monad m, Enumerable t) => t m a -> Proxy x' x () a m ()
every :: t m a -> Proxy x' x () a m ()
every t m a
it = X -> Proxy x' x () a m ()
forall (m :: * -> *) a. Monad m => a -> m ()
discard (X -> Proxy x' x () a m ())
-> Proxy X () () a m () -> Proxy x' x () a m ()
forall (m :: * -> *) b' a' a y' y b c.
Functor m =>
(b' -> Proxy a' a y' y m b)
-> Proxy b' b y' y m c -> Proxy a' a y' y m c
>\\ ListT m a -> Proxy X () () a m ()
forall (m :: * -> *) a. ListT m a -> Producer a m ()
enumerate (t m a -> ListT m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(Enumerable t, Monad m) =>
t m a -> ListT m a
toListT t m a
it)
{-# INLINABLE every #-}

-- | Discards a value
discard :: Monad m => a -> m ()
discard :: a -> m ()
discard a
_ = () -> m ()
forall (m :: * -> *) a. Monad m => a -> m a
return ()
{-# INLINABLE discard #-}

-- | ('>->') with the arguments flipped
(<-<)
    :: Functor m
    => Proxy () b c' c m r
    -- ^
    -> Proxy a' a () b m r
    -- ^
    -> Proxy a' a c' c m r
Proxy () b c' c m r
p2 <-< :: Proxy () b c' c m r -> Proxy a' a () b m r -> Proxy a' a c' c m r
<-< Proxy a' a () b m r
p1 = Proxy a' a () b m r
p1 Proxy a' a () b m r -> Proxy () b c' c m r -> Proxy a' a c' c m r
forall (m :: * -> *) a' a b r c' c.
Functor m =>
Proxy a' a () b m r -> Proxy () b c' c m r -> Proxy a' a c' c m r
>-> Proxy () b c' c m r
p2
{-# INLINABLE (<-<) #-}

{- $reexports
    "Control.Monad" re-exports 'void'

    "Control.Monad.IO.Class" re-exports 'MonadIO'.

    "Control.Monad.Trans.Class" re-exports 'MonadTrans'.

    "Control.Monad.Morph" re-exports 'MFunctor'.

    "Data.Foldable" re-exports 'Foldable' (the class name only).
-}