{-| The core functionality for the 'Proxy' monad transformer

    Read "Pipes.Tutorial" if you want a beginners tutorial explaining how to use
    this library.  The documentation in this module targets more advanced users
    who want to understand the theory behind this library.

    This module is not exported by default, and I recommend you use the
    unidirectional operations exported by the "Pipes" module if you can.  You
    should only use this module if you require advanced features like:

    * bidirectional communication, or:

    * push-based 'Pipe's.
-}

{-# LANGUAGE RankNTypes, Trustworthy #-}

module Pipes.Core (
    -- * Proxy Monad Transformer
    -- $proxy
      Proxy
    , runEffect

    -- * Categories
    -- $categories

    -- ** Respond
    -- $respond
    , respond
    , (/>/)
    , (//>)

    -- ** Request
    -- $request
    , request
    , (\>\)
    , (>\\)

    -- ** Push
    -- $push
    , push
    , (>~>)
    , (>>~)

    -- ** Pull
    -- $pull
    , pull
    , (>+>)
    , (+>>)

    -- ** Reflect
    -- $reflect
    , reflect

    -- * Concrete Type Synonyms
    , X
    , Effect
    , Producer
    , Pipe
    , Consumer
    , Client
    , Server

    -- * Polymorphic Type Synonyms
    , Effect'
    , Producer'
    , Consumer'
    , Client'
    , Server'

    -- * Flipped operators
    , (\<\)
    , (/</)
    , (<~<)
    , (~<<)
    , (<+<)
    , (<\\)
    , (//<)
    , (<<+)

    -- * Re-exports
    , closed
    ) where

import Pipes.Internal (Proxy(..), X, closed)

{- $proxy
    Diagrammatically, you can think of a 'Proxy' as having the following shape:

@
 Upstream | Downstream
     +---------+
     |         |
 a' <==       <== b'
     |         |
 a  ==>       ==> b
     |    |    |
     +----|----+
          v
          r
@

    You can connect proxies together in five different ways:

    * ('Pipes.>+>'): connect pull-based streams

    * ('Pipes.>~>'): connect push-based streams

    * ('Pipes.\>\'): chain folds

    * ('Pipes./>/'): chain unfolds

    * ('Control.Monad.>=>'): sequence proxies

-}

-- | Run a self-contained 'Effect', converting it back to the base monad
runEffect :: Monad m => Effect m r -> m r
runEffect :: Effect m r -> m r
runEffect = Effect m r -> m r
forall (m :: * -> *) a b' b. Monad m => Proxy X a b' X m b -> m b
go
  where
    go :: Proxy X a b' X m b -> m b
go Proxy X a b' X m b
p = case Proxy X a b' X m b
p of
        Request X
v a -> Proxy X a b' X m b
_ -> X -> m b
forall a. X -> a
closed X
v
        Respond X
v b' -> Proxy X a b' X m b
_ -> X -> m b
forall a. X -> a
closed X
v
        M       m (Proxy X a b' X m b)
m   -> m (Proxy X a b' X m b)
m m (Proxy X a b' X m b) -> (Proxy X a b' X m b -> m b) -> m b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= Proxy X a b' X m b -> m b
go
        Pure    b
r   -> b -> m b
forall (m :: * -> *) a. Monad m => a -> m a
return b
r
{-# INLINABLE runEffect #-}

{- * Keep proxy composition lower in precedence than function composition, which
     is 9 at the time of of this comment, so that users can write things like:


> lift . k >+> p
>
> hoist f . k >+> p

   * Keep the priorities different so that users can mix composition operators
     like:

> up \>\ p />/ dn
>
> up >~> p >+> dn

   * Keep 'request' and 'respond' composition lower in precedence than 'pull'
     and 'push' composition, so that users can do:

> read \>\ pull >+> writer

   * I arbitrarily choose a lower priority for downstream operators so that lazy
     pull-based computations need not evaluate upstream stages unless absolutely
     necessary.
-}
infixl 3 //>
infixr 3 <\\      -- GHC will raise a parse error if either of these lines ends
infixr 4 />/, >\\ -- with '\', which is why this comment is here
infixl 4 \<\, //<
infixl 5 \>\      -- Same thing here
infixr 5 /</
infixl 6 <<+
infixr 6 +>>
infixl 7 >+>, >>~
infixr 7 <+<, ~<<
infixl 8 <~<
infixr 8 >~>

{- $categories
    A 'Control.Category.Category' is a set of components that you can connect
    with a composition operator, ('Control.Category..'), that has an identity,
    'Control.Category.id'.  The ('Control.Category..') and 'Control.Category.id'
    must satisfy the following three 'Control.Category.Category' laws:

@
\-\- Left identity
'Control.Category.id' 'Control.Category..' f = f

\-\- Right identity
f 'Control.Category..' 'Control.Category.id' = f

\-\- Associativity
(f 'Control.Category..' g) 'Control.Category..' h = f 'Control.Category..' (g 'Control.Category..' h)
@

    The 'Proxy' type sits at the intersection of five separate categories, four
    of which are named after their identity:

@
                     Identity   | Composition |  Point-ful
                  +-------------+-------------+-------------+
 respond category |   'respond'   |     '/>/'     |     '//>'     |
 request category |   'request'   |     '\>\'     |     '>\\'     |
    push category |   'push'      |     '>~>'     |     '>>~'     |
    pull category |   'pull'      |     '>+>'     |     '+>>'     |
 Kleisli category |   'return'    |     'Control.Monad.>=>'     |     '>>='     |
                  +-------------+-------------+-------------+
@

    Each composition operator has a \"point-ful\" version, analogous to how
    ('>>=') is the point-ful version of ('Control.Monad.>=>').  For example,
    ('//>') is the point-ful version of ('/>/').  The convention is that the
    odd character out faces the argument that is a function.
-}

{- $respond
    The 'respond' category closely corresponds to the generator design pattern.

    The 'respond' category obeys the category laws, where 'respond' is the
    identity and ('/>/') is composition:

@
\-\- Left identity
'respond' '/>/' f = f

\-\- Right identity
f '/>/' 'respond' = f

\-\- Associativity
(f '/>/' g) '/>/' h = f '/>/' (g '/>/' h)
@

#respond-diagram#

    The following diagrams show the flow of information:

@
'respond' :: 'Functor' m
       =>  a -> 'Proxy' x' x a' a m a'

\          a
          |
     +----|----+
     |    |    |
 x' <==   \\ /==== a'
     |     X   |
 x  ==>   / \\===> a
     |    |    |
     +----|----+
          v
          a'

('/>/') :: '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                   /===> b                      a
          |                  /      |                      |
     +----|----+            /  +----|----+            +----|----+
     |    v    |           /   |    v    |            |    v    |
 x' <==       <== b' <==\\ / x'<==       <== c'    x' <==       <== c'
     |    f    |         X     |    g    |     =      | f '/>/' g |
 x  ==>       ==> b  ===/ \\ x ==>       ==> c     x  ==>       ==> c
     |    |    |           \\   |    |    |            |    |    |
     +----|----+            \\  +----|----+            +----|----+
          v                  \\      v                      v
          a'                  \\==== b'                     a'

('//>') :: 'Functor' m
      => 'Proxy' x' x b' b m a'
      -> (b -> 'Proxy' x' x c' c m b')
      -> 'Proxy' x' x c' c m a'

\                              /===> b
                             /      |
     +---------+            /  +----|----+            +---------+
     |         |           /   |    v    |            |         |
 x' <==       <== b' <==\\ / x'<==       <== c'    x' <==       <== c'
     |    f    |         X     |    g    |     =      | f '//>' g |
 x  ==>       ==> b  ===/ \\ x ==>       ==> c     x  ==>       ==> c'
     |    |    |           \\   |    |    |            |    |    |
     +----|----+            \\  +----|----+            +----|----+
          v                  \\      v                      v
          a'                  \\==== b'                     a'
@

-}

{-| Send a value of type @a@ downstream and block waiting for a reply of type
    @a'@

    'respond' is the identity of the respond category.
-}
respond :: Functor m => a -> Proxy x' x a' a m a'
respond :: a -> Proxy x' x a' a m a'
respond a
a = a -> (a' -> Proxy x' x a' a m a') -> Proxy x' x a' a m a'
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
a a' -> Proxy x' x a' a m a'
forall a' a b' b (m :: * -> *) r. r -> Proxy a' a b' b m r
Pure
{-# INLINABLE [1] respond #-}

{-| Compose two unfolds, creating a new unfold

@
(f '/>/' g) x = f x '//>' g
@

    ('/>/') is the composition operator of the respond category.
-}
(/>/)
    :: 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'
fa />/ :: (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'
fb) a
a = a -> Proxy x' x b' b m a'
fa a
a 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'
//> b -> Proxy x' x c' c m b'
fb
{-# INLINABLE (/>/) #-}

{-| @(p \/\/> f)@ replaces each 'respond' in @p@ with @f@.

    Point-ful version of ('/>/')
-}
(//>)
    :: Functor m
    =>       Proxy x' x b' b m a'
    -- ^
    -> (b -> Proxy x' x c' c m b')
    -- ^
    ->       Proxy x' x c' c m a'
    -- ^
Proxy x' x b' b m a'
p0 //> :: Proxy x' x b' b m a'
-> (b -> Proxy x' x c' c m b') -> Proxy x' x c' c m a'
//> b -> Proxy x' x c' c m b'
fb = Proxy x' x b' b m a' -> Proxy x' x c' c m a'
forall r. Proxy x' x b' b m r -> Proxy x' x c' c m r
go Proxy x' x b' b m a'
p0
  where
    go :: Proxy x' x b' b m r -> Proxy x' x c' c m r
go Proxy x' x b' b m r
p = case Proxy x' x b' b m r
p of
        Request x'
x' x -> Proxy x' x b' b m r
fx  -> x' -> (x -> Proxy x' x c' c m r) -> Proxy x' x c' c 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 x'
x' (\x
x -> Proxy x' x b' b m r -> Proxy x' x c' c m r
go (x -> Proxy x' x b' b m r
fx x
x))
        Respond b
b  b' -> Proxy x' x b' b m r
fb' -> b -> Proxy x' x c' c m b'
fb b
b Proxy x' x c' c m b'
-> (b' -> Proxy x' x c' c m r) -> Proxy x' x c' c m r
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \b'
b' -> Proxy x' x b' b m r -> Proxy x' x c' c m r
go (b' -> Proxy x' x b' b m r
fb' b'
b')
        M          m (Proxy x' x b' b m r)
m   -> m (Proxy x' x c' c m r) -> Proxy x' x c' c 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' x b' b m r -> Proxy x' x c' c m r
go (Proxy x' x b' b m r -> Proxy x' x c' c m r)
-> m (Proxy x' x b' b m r) -> m (Proxy x' x c' c m r)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> m (Proxy x' x b' b m r)
m)
        Pure       r
a   -> r -> Proxy x' x c' c m r
forall a' a b' b (m :: * -> *) r. r -> Proxy a' a b' b m r
Pure r
a
{-# INLINE [1] (//>) #-}

{-# RULES
    "(Request x' fx ) //> fb" forall x' fx  fb .
        (Request x' fx ) //> fb = Request x' (\x -> fx x //> fb);
    "(Respond b  fb') //> fb" forall b  fb' fb .
        (Respond b  fb') //> fb = fb b >>= \b' -> fb' b' //> fb;
    "(M          m  ) //> fb" forall    m   fb .
        (M          m  ) //> fb = M ((\p' -> p' //> fb) <$> m);
    "(Pure      a   ) //> fb" forall a      fb .
        (Pure    a     ) //> fb = Pure a;
  #-}

{- $request
    The 'request' category closely corresponds to the iteratee design pattern.

    The 'request' category obeys the category laws, where 'request' is the
    identity and ('\>\') is composition:

@
-- Left identity
'request' '\>\' f = f

\-\- Right identity
f '\>\' 'request' = f

\-\- Associativity
(f '\>\' g) '\>\' h = f '\>\' (g '\>\' h)
@

#request-diagram#

    The following diagrams show the flow of information:

@
'request' :: 'Functor' m
        =>  a' -> 'Proxy' a' a y' y m a

\          a'
          |
     +----|----+
     |    |    |
 a' <=====/   <== y'
     |         |
 a  ======\\   ==> y
     |    |    |
     +----|----+
          v
          a

('\>\') :: 'Functor' m
      => (b' -> 'Proxy' a' a y' y m b)
      -> (c' -> 'Proxy' b' b y' y m c)
      -> (c' -> 'Proxy' a' a y' y m c)

\          b'<=====\\                c'                     c'
          |        \\               |                      |
     +----|----+    \\         +----|----+            +----|----+
     |    v    |     \\        |    v    |            |    v    |
 a' <==       <== y'  \\== b' <==       <== y'    a' <==       <== y'
     |    f    |              |    g    |     =      | f '\>\' g |
 a  ==>       ==> y   /=> b  ==>       ==> y     a  ==>       ==> y
     |    |    |     /        |    |    |            |    |    |
     +----|----+    /         +----|----+            +----|----+
          v        /               v                      v
          b ======/                c                      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

\          b'<=====\\
          |        \\
     +----|----+    \\         +---------+            +---------+
     |    v    |     \\        |         |            |         |
 a' <==       <== y'  \\== b' <==       <== y'    a' <==       <== y'
     |    f    |              |    g    |     =      | f '>\\' g |
 a  ==>       ==> y   /=> b  ==>       ==> y     a  ==>       ==> y
     |    |    |     /        |    |    |            |    |    |
     +----|----+    /         +----|----+            +----|----+
          v        /               v                      v
          b ======/                c                      c
@
-}

{-| Send a value of type @a'@ upstream and block waiting for a reply of type @a@

    'request' is the identity of the request category.
-}
request :: Functor m => a' -> Proxy a' a y' y m a
request :: a' -> Proxy a' a y' y m a
request a'
a' = a' -> (a -> Proxy a' a y' y m a) -> Proxy a' a y' y m a
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 -> Proxy a' a y' y m a
forall a' a b' b (m :: * -> *) r. r -> Proxy a' a b' b m r
Pure
{-# INLINABLE [1] request #-}

{-| Compose two folds, creating a new fold

@
(f '\>\' g) x = f '>\\' g x
@

    ('\>\') is the composition operator of the request category.
-}
(\>\)
    :: Functor m
    => (b' -> Proxy a' a y' y m b)
    -- ^
    -> (c' -> Proxy b' b y' y m c)
    -- ^
    -> (c' -> Proxy a' a y' y m c)
    -- ^
(b' -> Proxy a' a y' y m b
fb' \>\ :: (b' -> Proxy a' a y' y m b)
-> (c' -> Proxy b' b y' y m c) -> c' -> Proxy a' a y' y m c
\>\ c' -> Proxy b' b y' y m c
fc') c'
c' = b' -> Proxy a' a y' y m b
fb' (b' -> Proxy a' a y' y m b)
-> Proxy b' 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
>\\ c' -> Proxy b' b y' y m c
fc' c'
c'
{-# INLINABLE (\>\) #-}

{-| @(f >\\\\ p)@ replaces each 'request' in @p@ with @f@.

    Point-ful version of ('\>\')
-}
(>\\)
    :: Functor m
    => (b' -> Proxy a' a y' y m b)
    -- ^
    ->        Proxy b' b y' y m c
    -- ^
    ->        Proxy a' a y' y m c
    -- ^
b' -> Proxy a' a y' y m b
fb' >\\ :: (b' -> Proxy a' a y' y m b)
-> Proxy b' b y' y m c -> Proxy a' a y' y m c
>\\ Proxy b' b y' y m c
p0 = Proxy b' b y' y m c -> Proxy a' a y' y m c
forall r. Proxy b' b y' y m r -> Proxy a' a y' y m r
go Proxy b' b y' y m c
p0
  where
    go :: Proxy b' b y' y m r -> Proxy a' a y' y m r
go Proxy b' b y' y m r
p = case Proxy b' b y' y m r
p of
        Request b'
b' b -> Proxy b' b y' y m r
fb  -> b' -> Proxy a' a y' y m b
fb' b'
b' Proxy a' a y' y m b
-> (b -> Proxy a' a y' y m r) -> Proxy a' a y' y m r
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \b
b -> Proxy b' b y' y m r -> Proxy a' a y' y m r
go (b -> Proxy b' b y' y m r
fb b
b)
        Respond y
x  y' -> Proxy b' b y' y m r
fx' -> y -> (y' -> Proxy a' a y' y m r) -> Proxy a' a y' y 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 y
x (\y'
x' -> Proxy b' b y' y m r -> Proxy a' a y' y m r
go (y' -> Proxy b' b y' y m r
fx' y'
x'))
        M          m (Proxy b' b y' y m r)
m   -> m (Proxy a' a y' y m r) -> Proxy a' a y' y 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 b' b y' y m r -> Proxy a' a y' y m r
go (Proxy b' b y' y m r -> Proxy a' a y' y m r)
-> m (Proxy b' b y' y m r) -> m (Proxy a' a y' y m r)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> m (Proxy b' b y' y m r)
m)
        Pure       r
a   -> r -> Proxy a' a y' y m r
forall a' a b' b (m :: * -> *) r. r -> Proxy a' a b' b m r
Pure r
a
{-# INLINE [1] (>\\) #-}

{-# RULES
    "fb' >\\ (Request b' fb )" forall fb' b' fb  .
        fb' >\\ (Request b' fb ) = fb' b' >>= \b -> fb' >\\ fb  b;
    "fb' >\\ (Respond x  fx')" forall fb' x  fx' .
        fb' >\\ (Respond x  fx') = Respond x (\x' -> fb' >\\ fx' x');
    "fb' >\\ (M          m  )" forall fb'    m   .
        fb' >\\ (M          m  ) = M ((\p' -> fb' >\\ p') <$> m);
    "fb' >\\ (Pure    a    )" forall fb' a      .
        fb' >\\ (Pure    a     ) = Pure a;
  #-}

{- $push
    The 'push' category closely corresponds to push-based Unix pipes.

    The 'push' category obeys the category laws, where 'push' is the identity
    and ('>~>') is composition:

@
\-\- Left identity
'push' '>~>' f = f

\-\- Right identity
f '>~>' 'push' = f

\-\- Associativity
(f '>~>' g) '>~>' h = f '>~>' (g '>~>' h)
@

    The following diagram shows the flow of information:

@
'push'  :: 'Functor' m
      =>  a -> 'Proxy' a' a a' a m r

\          a
          |
     +----|----+
     |    v    |
 a' <============ a'
     |         |
 a  ============> a
     |    |    |
     +----|----+
          v
          r

('>~>') :: 'Functor' m
      => (a -> 'Proxy' a' a b' b m r)
      -> (b -> 'Proxy' b' b c' c m r)
      -> (a -> 'Proxy' a' a c' c m r)

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

-}

{-| Forward responses followed by requests

@
'push' = 'respond' 'Control.Monad.>=>' 'request' 'Control.Monad.>=>' 'push'
@

    'push' is the identity of the push category.
-}
push :: Functor m => a -> Proxy a' a a' a m r
push :: a -> Proxy a' a a' a m r
push = a -> Proxy a' a a' a m r
forall a a' (m :: * -> *) r. a -> Proxy a' a a' a m r
go
  where
    go :: a -> Proxy a' a a' a m r
go a
a = a -> (a' -> Proxy a' a a' a m r) -> Proxy a' a 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
a (\a'
a' -> a' -> (a -> Proxy a' a a' a m r) -> Proxy a' a 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 -> Proxy a' a a' a m r
go)
{-# INLINABLE [1] push #-}

{-| Compose two proxies blocked while 'request'ing data, creating a new proxy
    blocked while 'request'ing data

@
(f '>~>' g) x = f x '>>~' g
@

    ('>~>') is the composition operator of the push category.
-}
(>~>)
    :: Functor m
    => (_a -> Proxy a' a b' b m r)
    -- ^
    -> ( b -> Proxy b' b c' c m r)
    -- ^
    -> (_a -> Proxy a' a c' c m r)
    -- ^
(_a -> Proxy a' a b' b m r
fa >~> :: (_a -> Proxy a' a b' b m r)
-> (b -> Proxy b' b c' c m r) -> _a -> Proxy a' a c' c m r
>~> b -> Proxy b' b c' c m r
fb) _a
a = _a -> Proxy a' a b' b m r
fa _a
a Proxy a' a b' b m r
-> (b -> Proxy b' b c' c m r) -> Proxy a' a c' c m r
forall (m :: * -> *) a' a b' b r c' c.
Functor m =>
Proxy a' a b' b m r
-> (b -> Proxy b' b c' c m r) -> Proxy a' a c' c m r
>>~ b -> Proxy b' b c' c m r
fb
{-# INLINABLE (>~>) #-}

{-| @(p >>~ f)@ pairs each 'respond' in @p@ with a 'request' in @f@.

    Point-ful version of ('>~>')
-}
(>>~)
    :: Functor m
    =>       Proxy a' a b' b m r
    -- ^
    -> (b -> Proxy b' b c' c m r)
    -- ^
    ->       Proxy a' a c' c m r
    -- ^
Proxy a' a b' b m r
p >>~ :: Proxy a' a b' b m r
-> (b -> Proxy b' b c' c m r) -> Proxy a' a c' c m r
>>~ b -> Proxy b' b c' c m r
fb = case Proxy a' a b' b m r
p of
    Request a'
a' a -> Proxy a' a b' b m r
fa  -> a' -> (a -> Proxy a' a c' c m r) -> Proxy a' a c' c 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 -> a -> Proxy a' a b' b m r
fa a
a Proxy a' a b' b m r
-> (b -> Proxy b' b c' c m r) -> Proxy a' a c' c m r
forall (m :: * -> *) a' a b' b r c' c.
Functor m =>
Proxy a' a b' b m r
-> (b -> Proxy b' b c' c m r) -> Proxy a' a c' c m r
>>~ b -> Proxy b' b c' c m r
fb)
    Respond b
b  b' -> Proxy a' a b' b m r
fb' -> b' -> Proxy a' a b' b m r
fb' (b' -> Proxy a' a b' b m r)
-> Proxy b' 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
+>> b -> Proxy b' b c' c m r
fb b
b
    M          m (Proxy a' a b' b m r)
m   -> m (Proxy a' a c' c m r) -> Proxy a' a c' c 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 a' a b' b m r
p' -> Proxy a' a b' b m r
p' Proxy a' a b' b m r
-> (b -> Proxy b' b c' c m r) -> Proxy a' a c' c m r
forall (m :: * -> *) a' a b' b r c' c.
Functor m =>
Proxy a' a b' b m r
-> (b -> Proxy b' b c' c m r) -> Proxy a' a c' c m r
>>~ b -> Proxy b' b c' c m r
fb) (Proxy a' a b' b m r -> Proxy a' a c' c m r)
-> m (Proxy a' a b' b m r) -> m (Proxy a' a c' c m r)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> m (Proxy a' a b' b m r)
m)
    Pure       r
r   -> r -> Proxy a' a c' c m r
forall a' a b' b (m :: * -> *) r. r -> Proxy a' a b' b m r
Pure r
r
{-# INLINE [1] (>>~) #-}

{- $pull
    The 'pull' category closely corresponds to pull-based Unix pipes.

    The 'pull' category obeys the category laws, where 'pull' is the identity
    and ('>+>') is composition:

@
\-\- Left identity
'pull' '>+>' f = f

\-\- Right identity
f '>+>' 'pull' = f

\-\- Associativity
(f '>+>' g) '>+>' h = f '>+>' (g '>+>' h)
@

#pull-diagram#

    The following diagrams show the flow of information:

@
'pull'  :: 'Functor' m
      =>  a' -> 'Proxy' a' a a' a m r

\          a'
          |
     +----|----+
     |    v    |
 a' <============ a'
     |         |
 a  ============> a
     |    |    |
     +----|----+
          v
          r

('>+>') :: 'Functor' m
      -> (b' -> 'Proxy' a' a b' b m r)
      -> (c' -> 'Proxy' b' b c' c m r)
      -> (c' -> 'Proxy' a' a c' c m r)

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

-}

{-| Forward requests followed by responses:

@
'pull' = 'request' 'Control.Monad.>=>' 'respond' 'Control.Monad.>=>' 'pull'
@

    'pull' is the identity of the pull category.
-}
pull :: Functor m => a' -> Proxy a' a a' a m r
pull :: a' -> Proxy a' a a' a m r
pull = a' -> Proxy a' a a' a m r
forall b' b (m :: * -> *) r. b' -> Proxy b' b b' b m r
go
  where
    go :: b' -> Proxy b' b b' b m r
go b'
a' = b' -> (b -> Proxy b' b b' b m r) -> Proxy b' b 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 b'
a' (\b
a -> b -> (b' -> Proxy b' b b' b m r) -> Proxy b' b 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 b' b b' b m r
go)
{-# INLINABLE [1] pull #-}

{-| Compose two proxies blocked in the middle of 'respond'ing, creating a new
    proxy blocked in the middle of 'respond'ing

@
(f '>+>' g) x = f '+>>' g x
@

    ('>+>') is the composition operator of the pull category.
-}
(>+>)
    :: Functor m
    => ( b' -> Proxy a' a b' b m r)
    -- ^
    -> (_c' -> Proxy b' b c' c m r)
    -- ^
    -> (_c' -> Proxy a' a c' c m r)
    -- ^
(b' -> Proxy a' a b' b m r
fb' >+> :: (b' -> Proxy a' a b' b m r)
-> (_c' -> Proxy b' b c' c m r) -> _c' -> Proxy a' a c' c m r
>+> _c' -> Proxy b' b c' c m r
fc') _c'
c' = b' -> Proxy a' a b' b m r
fb' (b' -> Proxy a' a b' b m r)
-> Proxy b' 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
+>> _c' -> Proxy b' b c' c m r
fc' _c'
c'
{-# INLINABLE (>+>) #-}

{-| @(f +>> p)@ pairs each 'request' in @p@ with a 'respond' in @f@.

    Point-ful version of ('>+>')
-}
(+>>)
    :: Functor m
    => (b' -> Proxy a' a b' b m r)
    -- ^
    ->        Proxy b' b c' c m r
    -- ^
    ->        Proxy a' a c' c m r
    -- ^
b' -> Proxy a' a b' b m r
fb' +>> :: (b' -> Proxy a' a b' b m r)
-> Proxy b' b c' c m r -> Proxy a' a c' c m r
+>> Proxy b' b c' c m r
p = case Proxy b' b c' c m r
p of
    Request b'
b' b -> Proxy b' b c' c m r
fb  -> b' -> Proxy a' a b' b m r
fb' b'
b' Proxy a' a b' b m r
-> (b -> Proxy b' b c' c m r) -> Proxy a' a c' c m r
forall (m :: * -> *) a' a b' b r c' c.
Functor m =>
Proxy a' a b' b m r
-> (b -> Proxy b' b c' c m r) -> Proxy a' a c' c m r
>>~ b -> Proxy b' b c' c m r
fb
    Respond c
c  c' -> Proxy b' b c' c m r
fc' -> c -> (c' -> Proxy a' a c' c m r) -> Proxy a' a c' c 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 c
c (\c'
c' -> b' -> Proxy a' a b' b m r
fb' (b' -> Proxy a' a b' b m r)
-> Proxy b' 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
+>> c' -> Proxy b' b c' c m r
fc' c'
c')
    M          m (Proxy b' b c' c m r)
m   -> m (Proxy a' a c' c m r) -> Proxy a' a c' c 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 b' b c' c m r
p' -> b' -> Proxy a' a b' b m r
fb' (b' -> Proxy a' a b' b m r)
-> Proxy b' 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' b c' c m r
p') (Proxy b' b c' c m r -> Proxy a' a c' c m r)
-> m (Proxy b' b c' c m r) -> m (Proxy a' a c' c m r)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> m (Proxy b' b c' c m r)
m)
    Pure       r
r   -> r -> Proxy a' a c' c m r
forall a' a b' b (m :: * -> *) r. r -> Proxy a' a b' b m r
Pure r
r
{-# INLINABLE [1] (+>>) #-}

{- $reflect
    @(reflect .)@ transforms each streaming category into its dual:

    * The request category is the dual of the respond category

@
'reflect' '.' 'respond' = 'request'

'reflect' '.' (f '/>/' g) = 'reflect' '.' f '/</' 'reflect' '.' g
@

@
'reflect' '.' 'request' = 'respond'

'reflect' '.' (f '\>\' g) = 'reflect' '.' f '\<\' 'reflect' '.' g
@

    * The pull category is the dual of the push category

@
'reflect' '.' 'push' = 'pull'

'reflect' '.' (f '>~>' g) = 'reflect' '.' f '<+<' 'reflect' '.' g
@

@
'reflect' '.' 'pull' = 'push'

'reflect' '.' (f '>+>' g) = 'reflect' '.' f '<~<' 'reflect' '.' g
@
-}

-- | Switch the upstream and downstream ends
reflect :: Functor m => Proxy a' a b' b m r -> Proxy b b' a a' m r
reflect :: Proxy a' a b' b m r -> Proxy b b' a a' m r
reflect = Proxy a' a b' b m r -> Proxy b b' a a' m r
forall (m :: * -> *) b b' a a' r.
Functor m =>
Proxy b b' a a' m r -> Proxy a' a b' b m r
go
  where
    go :: Proxy b b' a a' m r -> Proxy a' a b' b m r
go Proxy b b' a a' m r
p = case Proxy b b' a a' m r
p of
        Request b
a' b' -> Proxy b b' a a' m r
fa  -> 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'
a  -> Proxy b b' a a' m r -> Proxy a' a b' b m r
go (b' -> Proxy b b' a a' m r
fa  b'
a ))
        Respond a'
b  a -> Proxy b b' a a' m r
fb' -> 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'
b  (\a
b' -> Proxy b b' a a' m r -> Proxy a' a b' b m r
go (a -> Proxy b b' a a' m r
fb' a
b'))
        M          m (Proxy 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 (Proxy b b' a a' m r -> Proxy a' a b' b m r
go (Proxy b b' a a' m r -> Proxy a' a b' b m r)
-> m (Proxy b b' a a' m r) -> m (Proxy a' a b' b m r)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> m (Proxy b b' a a' m r)
m)
        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
{-# INLINABLE reflect #-}

{-| An effect in the base monad

    'Effect's neither 'Pipes.await' nor 'Pipes.yield'
-}
type Effect = Proxy X () () X

-- | 'Producer's can only 'Pipes.yield'
type Producer b = Proxy X () () b

-- | 'Pipe's can both 'Pipes.await' and 'Pipes.yield'
type Pipe a b = Proxy () a () b

-- | 'Consumer's can only 'Pipes.await'
type Consumer a = Proxy () a () X

{-| @Client a' a@ sends requests of type @a'@ and receives responses of
    type @a@.

    'Client's only 'request' and never 'respond'.
-}
type Client a' a = Proxy a' a () X

{-| @Server b' b@ receives requests of type @b'@ and sends responses of type
    @b@.

    'Server's only 'respond' and never 'request'.
-}
type Server b' b = Proxy X () b' b

-- | Like 'Effect', but with a polymorphic type
type Effect' m r = forall x' x y' y . Proxy x' x y' y m r

-- | Like 'Producer', but with a polymorphic type
type Producer' b m r = forall x' x . Proxy x' x () b m r

-- | Like 'Consumer', but with a polymorphic type
type Consumer' a m r = forall y' y . Proxy () a y' y m r

-- | Like 'Server', but with a polymorphic type
type Server' b' b m r = forall x' x . Proxy x' x b' b m r

-- | Like 'Client', but with a polymorphic type
type Client' a' a m r = forall y' y . Proxy a' a y' y m r

-- | Equivalent to ('/>/') 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'
p1 \<\ :: (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'
p2 = a -> Proxy x' x b' b m a'
p2 (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'
p1
{-# INLINABLE (\<\) #-}

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

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

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

-- | Equivalent to ('//>') with the arguments flipped
(<\\)
    :: Functor m
    => (b -> Proxy x' x c' c m b')
    -- ^
    ->       Proxy x' x b' b m a'
    -- ^
    ->       Proxy x' x c' c m a'
    -- ^
b -> Proxy x' x c' c m b'
f <\\ :: (b -> Proxy x' x c' c m b')
-> Proxy x' x b' b m a' -> Proxy x' x c' c m a'
<\\ Proxy x' x b' b m a'
p = Proxy x' x b' b m a'
p 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'
//> b -> Proxy x' x c' c m b'
f
{-# INLINABLE (<\\) #-}

-- | Equivalent to ('>\\') with the arguments flipped
(//<)
    :: Functor m
    =>        Proxy b' b y' y m c
    -- ^
    -> (b' -> Proxy a' a y' y m b)
    -- ^
    ->        Proxy a' a y' y m c
    -- ^
Proxy b' b y' y m c
p //< :: Proxy b' b y' y m c
-> (b' -> Proxy a' a y' y m b) -> Proxy a' a y' y m c
//< b' -> Proxy a' a y' y m b
f = b' -> Proxy a' a y' y m b
f (b' -> Proxy a' a y' y m b)
-> Proxy b' 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' b y' y m c
p
{-# INLINABLE (//<) #-}

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

-- | Equivalent to ('+>>') with the arguments flipped
(<<+)
    :: Functor m
    =>         Proxy b' b c' c m r
    -- ^
    -> (b'  -> Proxy a' a b' b m r)
    -- ^
    ->         Proxy a' a c' c m r
    -- ^
Proxy b' b c' c m r
k <<+ :: Proxy b' b c' c m r
-> (b' -> Proxy a' a b' b m r) -> Proxy a' a c' c m r
<<+ b' -> Proxy a' a b' b m r
p = b' -> Proxy a' a b' b m r
p (b' -> Proxy a' a b' b m r)
-> Proxy b' 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' b c' c m r
k
{-# INLINABLE (<<+) #-}

{-# RULES
    "(p //> f) //> g" forall p f g . (p //> f) //> g = p //> (\x -> f x //> g)

  ; "p //> respond" forall p . p //> respond = p

  ; "respond x //> f" forall x f . respond x //>  f = f x

  ; "f >\\ (g >\\ p)" forall f g p . f >\\ (g >\\ p) = (\x -> f >\\ g x) >\\ p

  ; "request >\\ p" forall p . request >\\ p = p

  ; "f >\\ request x" forall f x . f >\\ request x = f x

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

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

  ; "push x >>~ f" forall x f . push x >>~ f = f x

  ; "f +>> (g +>> p)" forall f g p . f +>> (g +>> p) = (\x -> f +>> g x) +>> p

  ; "pull +>> p" forall p . pull +>> p = p

  ; "f +>> pull x" forall f x . f +>> pull x = f x

  #-}