{-| This is an internal module, meaning that it is unsafe to import unless you
    understand the risks.

    This module provides a fast implementation by weakening the monad
    transformer laws.  These laws do not hold if you can pattern match on the
    constructors, as the following counter-example illustrates:

@
'lift' '.' 'return' = 'M' '.' 'return' '.' 'Pure'

'return' = 'Pure'

'lift' '.' 'return' /= 'return'
@

    You do not need to worry about this if you do not import this module, since
    the other modules in this library do not export the constructors or export
    any functions which can violate the monad transformer laws.
-}

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

module Pipes.Internal (
    -- * Internal
      Proxy(..)
    , unsafeHoist
    , observe
    , X
    , closed
    ) where

import qualified Control.Monad.Fail as F (MonadFail(fail))
import Control.Monad.IO.Class (MonadIO(liftIO))
import Control.Monad.Trans.Class (MonadTrans(lift))
import Control.Monad.Morph (MFunctor(hoist), MMonad(embed))
import Control.Monad.Except (MonadError(..))
import Control.Monad.Catch (MonadThrow(..), MonadCatch(..))
import Control.Monad.Reader (MonadReader(..))
import Control.Monad.State (MonadState(..))
import Control.Monad.Writer (MonadWriter(..), censor)
import Data.Void (Void)

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

import qualified Data.Void

{-| A 'Proxy' is a monad transformer that receives and sends information on both
    an upstream and downstream interface.

    The type variables signify:

    * @a'@ and @a@ - The upstream interface, where @(a')@s go out and @(a)@s
      come in

    * @b'@ and @b@ - The downstream interface, where @(b)@s go out and @(b')@s
      come in

    * @m @ - The base monad

    * @r @ - The return value
-}
data Proxy a' a b' b m r
    = Request a' (a  -> Proxy a' a b' b m r )
    | Respond b  (b' -> Proxy a' a b' b m r )
    | M          (m    (Proxy a' a b' b m r))
    | Pure    r

instance Functor m => Functor (Proxy a' a b' b m) where
    fmap :: (a -> b) -> Proxy a' a b' b m a -> Proxy a' a b' b m b
fmap a -> b
f Proxy a' a b' b m a
p0 = Proxy a' a b' b m a -> Proxy a' a b' b m b
forall (m :: * -> *) a' a b' b.
Functor m =>
Proxy a' a b' b m a -> Proxy a' a b' b m b
go Proxy a' a b' b m a
p0 where
        go :: Proxy a' a b' b m a -> Proxy a' a b' b m b
go Proxy a' a b' b m a
p = case Proxy a' a b' b m a
p of
            Request a'
a' a -> Proxy a' a b' b m a
fa  -> a' -> (a -> Proxy a' a b' b m b) -> Proxy a' a b' b m b
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 m a -> Proxy a' a b' b m b
go (a -> Proxy a' a b' b m a
fa  a
a ))
            Respond b
b  b' -> Proxy a' a b' b m a
fb' -> b -> (b' -> Proxy a' a b' b m b) -> Proxy a' a b' b m b
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 m a -> Proxy a' a b' b m b
go (b' -> Proxy a' a b' b m a
fb' b'
b'))
            M          m (Proxy a' a b' b m a)
m   -> m (Proxy a' a b' b m b) -> Proxy a' a b' b m b
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 a -> Proxy a' a b' b m b
go (Proxy a' a b' b m a -> Proxy a' a b' b m b)
-> m (Proxy a' a b' b m a) -> m (Proxy a' a b' b m b)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> m (Proxy a' a b' b m a)
m)
            Pure    a
r      -> b -> Proxy a' a b' b m b
forall a' a b' b (m :: * -> *) r. r -> Proxy a' a b' b m r
Pure (a -> b
f a
r)

instance Functor m => Applicative (Proxy a' a b' b m) where
    pure :: a -> Proxy a' a b' b m a
pure      = a -> Proxy a' a b' b m a
forall a' a b' b (m :: * -> *) r. r -> Proxy a' a b' b m r
Pure
    Proxy a' a b' b m (a -> b)
pf <*> :: Proxy a' a b' b m (a -> b)
-> Proxy a' a b' b m a -> Proxy a' a b' b m b
<*> Proxy a' a b' b m a
px = Proxy a' a b' b m (a -> b) -> Proxy a' a b' b m b
forall r. Proxy a' a b' b m (a -> r) -> Proxy a' a b' b m r
go Proxy a' a b' b m (a -> b)
pf where
        go :: Proxy a' a b' b m (a -> r) -> Proxy a' a b' b m r
go Proxy a' a b' b m (a -> r)
p = case Proxy a' a b' b m (a -> r)
p of
            Request a'
a' a -> Proxy a' a b' b m (a -> 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 m (a -> r) -> Proxy a' a b' b m r
go (a -> Proxy a' a b' b m (a -> r)
fa  a
a ))
            Respond b
b  b' -> Proxy a' a b' b m (a -> r)
fb' -> 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 m (a -> r) -> Proxy a' a b' b m r
go (b' -> Proxy a' a b' b m (a -> r)
fb' b'
b'))
            M          m (Proxy a' a b' b m (a -> 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 a' a b' b m (a -> r) -> Proxy a' a b' b m r
go (Proxy a' a b' b m (a -> r) -> Proxy a' a b' b m r)
-> m (Proxy a' a b' b m (a -> r)) -> m (Proxy a' a b' b m r)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> m (Proxy a' a b' b m (a -> r))
m)
            Pure    a -> r
f      -> (a -> r) -> Proxy a' a b' b m a -> Proxy a' a b' b m r
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap a -> r
f Proxy a' a b' b m a
px
    Proxy a' a b' b m a
l *> :: Proxy a' a b' b m a -> Proxy a' a b' b m b -> Proxy a' a b' b m b
*> Proxy a' a b' b m b
r = Proxy a' a b' b m a -> Proxy a' a b' b m b
forall r. Proxy a' a b' b m r -> Proxy a' a b' b m b
go Proxy a' a b' b m a
l where
        go :: Proxy a' a b' b m r -> Proxy a' a b' b m b
go Proxy a' a b' b m r
p = 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 b' b m b) -> Proxy a' a b' b m b
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 m r -> Proxy a' a b' b m b
go (a -> Proxy a' a b' b m r
fa  a
a ))
            Respond b
b  b' -> Proxy a' a b' b m r
fb' -> b -> (b' -> Proxy a' a b' b m b) -> Proxy a' a b' b m b
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 m r -> Proxy a' a b' b m b
go (b' -> Proxy a' a b' b m r
fb' b'
b'))
            M          m (Proxy a' a b' b m r)
m   -> m (Proxy a' a b' b m b) -> Proxy a' a b' b m b
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 -> Proxy a' a b' b m b
go (Proxy a' a b' b m r -> Proxy a' a b' b m b)
-> m (Proxy a' a b' b m r) -> m (Proxy a' a b' b m b)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> m (Proxy a' a b' b m r)
m)
            Pure    r
_      -> Proxy a' a b' b m b
r

instance Functor m => Monad (Proxy a' a b' b m) where
    return :: a -> Proxy a' a b' b m a
return = a -> Proxy a' a b' b m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure
    >>= :: Proxy a' a b' b m a
-> (a -> Proxy a' a b' b m b) -> Proxy a' a b' b m b
(>>=)  = Proxy a' a b' b m a
-> (a -> Proxy a' a b' b m b) -> Proxy a' a b' b m b
forall (m :: * -> *) a' a b' b r r'.
Functor m =>
Proxy a' a b' b m r
-> (r -> Proxy a' a b' b m r') -> Proxy a' a b' b m r'
_bind

_bind
    :: Functor m
    => Proxy a' a b' b m r
    -> (r -> Proxy a' a b' b m r')
    -> Proxy a' a b' b m r'
Proxy a' a b' b m r
p0 _bind :: Proxy a' a b' b m r
-> (r -> Proxy a' a b' b m r') -> Proxy a' a b' b m r'
`_bind` r -> Proxy a' a b' b m r'
f = Proxy a' a b' b m r -> Proxy a' a b' b m r'
go Proxy a' a b' b m r
p0 where
    go :: Proxy a' a b' b m r -> Proxy a' a b' b m r'
go Proxy a' a b' b m r
p = 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 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 m r -> Proxy a' a b' b m r'
go (a -> Proxy a' a b' b m r
fa  a
a ))
        Respond b
b  b' -> Proxy a' a b' b m r
fb' -> 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 m r -> Proxy a' a b' b m r'
go (b' -> Proxy a' a b' b m r
fb' b'
b'))
        M          m (Proxy a' a b' b 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 a' a b' b m r -> Proxy a' a b' b m r'
go (Proxy a' a b' b m r -> Proxy a' a b' b m r')
-> m (Proxy a' a b' b m r) -> m (Proxy a' a b' b 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 b' b m r'
f r
r
{-# NOINLINE[1] _bind #-}

{-# RULES
    "_bind (Request a' k) f" forall a' k f .
        _bind (Request a' k) f = Request a' (\a  -> _bind (k a)  f);
    "_bind (Respond b  k) f" forall b  k f .
        _bind (Respond b  k) f = Respond b  (\b' -> _bind (k b') f);
    "_bind (M          m) f" forall m    f .
        _bind (M          m) f = M ((\p -> _bind p f) <$> m);
    "_bind (Pure    r   ) f" forall r    f .
        _bind (Pure    r   ) f = f r;
  #-}

instance (Functor m, Semigroup r) => Semigroup (Proxy a' a b' b m r) where
    Proxy a' a b' b m r
p1 <> :: Proxy a' a b' b m r -> Proxy a' a b' b m r -> Proxy a' a b' b m r
<> Proxy a' a b' b m r
p2 = Proxy a' a b' b m r -> Proxy a' a b' b m r
go Proxy a' a b' b m r
p1 where
        go :: Proxy a' a b' b m r -> Proxy a' a b' b m r
go Proxy a' a b' b m r
p = 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 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 m r -> Proxy a' a b' b m r
go (a -> Proxy a' a b' b m r
fa  a
a ))
            Respond b
b  b' -> Proxy a' a b' b m r
fb' -> 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 m r -> Proxy a' a b' b m r
go (b' -> Proxy a' a b' b m r
fb' b'
b'))
            M          m (Proxy a' a b' b 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 a' a b' b m r -> Proxy a' a b' b m r
go (Proxy a' a b' b m r -> Proxy a' a b' b m r)
-> m (Proxy a' a b' b m r) -> m (Proxy a' a b' b 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
r1     -> (r -> r) -> 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
fmap (r
r1 r -> r -> r
forall a. Semigroup a => a -> a -> a
<>) Proxy a' a b' b m r
p2

instance (Functor m, Monoid r, Semigroup r) => Monoid (Proxy a' a b' b m r) where
    mempty :: Proxy a' a b' b m r
mempty        = 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
forall a. Monoid a => a
mempty
#if !(MIN_VERSION_base(4,11,0))
    mappend = (<>)
#endif

instance MonadTrans (Proxy a' a b' b) where
    lift :: m a -> Proxy a' a b' b m a
lift m a
m = m (Proxy a' a b' b m a) -> Proxy a' a b' b m a
forall a' a b' b (m :: * -> *) r.
m (Proxy a' a b' b m r) -> Proxy a' a b' b m r
M (a -> Proxy a' a b' b m a
forall a' a b' b (m :: * -> *) r. r -> Proxy a' a b' b m r
Pure (a -> Proxy a' a b' b m a) -> m a -> m (Proxy a' a b' b m a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> m a
m)

{-| 'unsafeHoist' is like 'hoist', but faster.

    This is labeled as unsafe because you will break the monad transformer laws
    if you do not pass a monad morphism as the first argument.  This function is
    safe if you pass a monad morphism as the first argument.
-}
unsafeHoist
    :: Functor m
    => (forall x . m x -> n x) -> Proxy a' a b' b m r -> Proxy a' a b' b n r
unsafeHoist :: (forall x. m x -> n x)
-> Proxy a' a b' b m r -> Proxy a' a b' b n r
unsafeHoist forall x. m x -> n x
nat = Proxy a' a b' b m r -> Proxy a' a b' b n r
forall a' a b' b r. Proxy a' a b' b m r -> Proxy a' a b' b n r
go
  where
    go :: Proxy a' a b' b m r -> Proxy a' a b' b n r
go Proxy a' a b' b m r
p = 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 b' b n r) -> Proxy a' a b' b n 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 m r -> Proxy a' a b' b n r
go (a -> Proxy a' a b' b m r
fa  a
a ))
        Respond b
b  b' -> Proxy a' a b' b m r
fb' -> b -> (b' -> Proxy a' a b' b n r) -> Proxy a' a b' b n 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 m r -> Proxy a' a b' b n r
go (b' -> Proxy a' a b' b m r
fb' b'
b'))
        M          m (Proxy a' a b' b m r)
m   -> n (Proxy a' a b' b n r) -> Proxy a' a b' b n 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 n r) -> n (Proxy a' a b' b n r)
forall x. m x -> n x
nat (Proxy a' a b' b m r -> Proxy a' a b' b n r
go (Proxy a' a b' b m r -> Proxy a' a b' b n r)
-> m (Proxy a' a b' b m r) -> m (Proxy a' a b' b n 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 b' b n r
forall a' a b' b (m :: * -> *) r. r -> Proxy a' a b' b m r
Pure r
r
{-# INLINABLE unsafeHoist #-}

instance MFunctor (Proxy a' a b' b) where
    hoist :: (forall a. m a -> n a)
-> Proxy a' a b' b m b -> Proxy a' a b' b n b
hoist forall a. m a -> n a
nat Proxy a' a b' b m b
p0 = Proxy a' a b' b m b -> Proxy a' a b' b n b
forall a' a b' b r. Proxy a' a b' b m r -> Proxy a' a b' b n r
go (Proxy a' a b' b m b -> Proxy a' a b' b m b
forall (m :: * -> *) a' a b' b r.
Monad m =>
Proxy a' a b' b m r -> Proxy a' a b' b m r
observe Proxy a' a b' b m b
p0)
      where
        go :: Proxy a' a b' b m r -> Proxy a' a b' b n r
go Proxy a' a b' b m r
p = 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 b' b n r) -> Proxy a' a b' b n 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 m r -> Proxy a' a b' b n r
go (a -> Proxy a' a b' b m r
fa  a
a ))
            Respond b
b  b' -> Proxy a' a b' b m r
fb' -> b -> (b' -> Proxy a' a b' b n r) -> Proxy a' a b' b n 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 m r -> Proxy a' a b' b n r
go (b' -> Proxy a' a b' b m r
fb' b'
b'))
            M          m (Proxy a' a b' b m r)
m   -> n (Proxy a' a b' b n r) -> Proxy a' a b' b n 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 n r) -> n (Proxy a' a b' b n r)
forall a. m a -> n a
nat (Proxy a' a b' b m r -> Proxy a' a b' b n r
go (Proxy a' a b' b m r -> Proxy a' a b' b n r)
-> m (Proxy a' a b' b m r) -> m (Proxy a' a b' b n 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 b' b n r
forall a' a b' b (m :: * -> *) r. r -> Proxy a' a b' b m r
Pure r
r

instance MMonad (Proxy a' a b' b) where
    embed :: (forall a. m a -> Proxy a' a b' b n a)
-> Proxy a' a b' b m b -> Proxy a' a b' b n b
embed forall a. m a -> Proxy a' a b' b n a
f = Proxy a' a b' b m b -> Proxy a' a b' b n b
forall b. Proxy a' a b' b m b -> Proxy a' a b' b n b
go
      where
        go :: Proxy a' a b' b m b -> Proxy a' a b' b n b
go Proxy a' a b' b m b
p = case Proxy a' a b' b m b
p of
            Request a'
a' a -> Proxy a' a b' b m b
fa  -> a' -> (a -> Proxy a' a b' b n b) -> Proxy a' a b' b n b
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 m b -> Proxy a' a b' b n b
go (a -> Proxy a' a b' b m b
fa  a
a ))
            Respond b
b  b' -> Proxy a' a b' b m b
fb' -> b -> (b' -> Proxy a' a b' b n b) -> Proxy a' a b' b n b
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 m b -> Proxy a' a b' b n b
go (b' -> Proxy a' a b' b m b
fb' b'
b'))
            M          m (Proxy a' a b' b m b)
m   -> m (Proxy a' a b' b m b) -> Proxy a' a b' b n (Proxy a' a b' b m b)
forall a. m a -> Proxy a' a b' b n a
f m (Proxy a' a b' b m b)
m Proxy a' a b' b n (Proxy a' a b' b m b)
-> (Proxy a' a b' b m b -> Proxy a' a b' b n b)
-> Proxy a' a b' b n b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= Proxy a' a b' b m b -> Proxy a' a b' b n b
go
            Pure    b
r      -> b -> Proxy a' a b' b n b
forall a' a b' b (m :: * -> *) r. r -> Proxy a' a b' b m r
Pure b
r

instance F.MonadFail m => F.MonadFail (Proxy a' a b' b m) where
    fail :: String -> Proxy a' a b' b m a
fail = m a -> Proxy a' a b' b m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (m a -> Proxy a' a b' b m a)
-> (String -> m a) -> String -> Proxy a' a b' b m a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> m a
forall (m :: * -> *) a. MonadFail m => String -> m a
F.fail

instance MonadIO m => MonadIO (Proxy a' a b' b m) where
    liftIO :: IO a -> Proxy a' a b' b m a
liftIO IO a
m = m (Proxy a' a b' b m a) -> Proxy a' a b' b m a
forall a' a b' b (m :: * -> *) r.
m (Proxy a' a b' b m r) -> Proxy a' a b' b m r
M (IO (Proxy a' a b' b m a) -> m (Proxy a' a b' b m a)
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (a -> Proxy a' a b' b m a
forall a' a b' b (m :: * -> *) r. r -> Proxy a' a b' b m r
Pure (a -> Proxy a' a b' b m a) -> IO a -> IO (Proxy a' a b' b m a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> IO a
m))

instance MonadReader r m => MonadReader r (Proxy a' a b' b m) where
    ask :: Proxy a' a b' b m r
ask = m r -> Proxy a' a b' b m r
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift m r
forall r (m :: * -> *). MonadReader r m => m r
ask
    local :: (r -> r) -> Proxy a' a b' b m a -> Proxy a' a b' b m a
local r -> r
f = Proxy a' a b' b m a -> Proxy a' a b' b m a
forall (m :: * -> *) a' a b' b r.
MonadReader r m =>
Proxy a' a b' b m r -> Proxy a' a b' b m r
go
        where
          go :: Proxy a' a b' b m r -> Proxy a' a b' b m r
go Proxy a' a b' b m r
p = 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 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 m r -> Proxy a' a b' b m r
go (a -> Proxy a' a b' b m r
fa  a
a ))
              Respond b
b  b' -> Proxy a' a b' b m r
fb' -> 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 m r -> Proxy a' a b' b m r
go (b' -> Proxy a' a b' b m r
fb' b'
b'))
              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
              M       m (Proxy a' a b' b 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 a' a b' b m r -> Proxy a' a b' b m r
go (Proxy a' a b' b m r -> Proxy a' a b' b m r)
-> m (Proxy a' a b' b m r) -> m (Proxy a' a b' b m r)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (r -> r) -> m (Proxy a' a b' b m r) -> m (Proxy a' a b' b m r)
forall r (m :: * -> *) a. MonadReader r m => (r -> r) -> m a -> m a
local r -> r
f m (Proxy a' a b' b m r)
m)
    reader :: (r -> a) -> Proxy a' a b' b m a
reader = m a -> Proxy a' a b' b m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (m a -> Proxy a' a b' b m a)
-> ((r -> a) -> m a) -> (r -> a) -> Proxy a' a b' b m a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (r -> a) -> m a
forall r (m :: * -> *) a. MonadReader r m => (r -> a) -> m a
reader

instance MonadState s m => MonadState s (Proxy a' a b' b m) where
    get :: Proxy a' a b' b m s
get = m s -> Proxy a' a b' b 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
    put :: s -> Proxy a' a b' b m ()
put = m () -> Proxy a' a b' b m ()
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (m () -> Proxy a' a b' b m ())
-> (s -> m ()) -> s -> Proxy a' a b' b m ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. s -> m ()
forall s (m :: * -> *). MonadState s m => s -> m ()
put
    state :: (s -> (a, s)) -> Proxy a' a b' b m a
state = m a -> Proxy a' a b' b m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (m a -> Proxy a' a b' b m a)
-> ((s -> (a, s)) -> m a) -> (s -> (a, s)) -> Proxy a' a b' b m a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (s -> (a, s)) -> m a
forall s (m :: * -> *) a. MonadState s m => (s -> (a, s)) -> m a
state

instance MonadWriter w m => MonadWriter w (Proxy a' a b' b m) where
    writer :: (a, w) -> Proxy a' a b' b m a
writer = m a -> Proxy a' a b' b m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (m a -> Proxy a' a b' b m a)
-> ((a, w) -> m a) -> (a, w) -> Proxy a' a b' b 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
    tell :: w -> Proxy a' a b' b m ()
tell = m () -> Proxy a' a b' b m ()
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (m () -> Proxy a' a b' b m ())
-> (w -> m ()) -> w -> Proxy a' a b' b m ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. w -> m ()
forall w (m :: * -> *). MonadWriter w m => w -> m ()
tell
    listen :: Proxy a' a b' b m a -> Proxy a' a b' b m (a, w)
listen Proxy a' a b' b m a
p0 = Proxy a' a b' b m a -> w -> Proxy a' a b' b m (a, w)
forall (m :: * -> *) b a' a b' b a.
MonadWriter b m =>
Proxy a' a b' b m a -> b -> Proxy a' a b' b m (a, b)
go Proxy a' a b' b m a
p0 w
forall a. Monoid a => a
mempty
      where
        go :: Proxy a' a b' b m a -> b -> Proxy a' a b' b m (a, b)
go Proxy a' a b' b m a
p b
w = case Proxy a' a b' b m a
p of
            Request a'
a' a -> Proxy a' a b' b m a
fa  -> a' -> (a -> Proxy a' a b' b m (a, b)) -> Proxy a' a b' b m (a, b)
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 m a -> b -> Proxy a' a b' b m (a, b)
go (a -> Proxy a' a b' b m a
fa  a
a ) b
w)
            Respond b
b  b' -> Proxy a' a b' b m a
fb' -> b -> (b' -> Proxy a' a b' b m (a, b)) -> Proxy a' a b' b m (a, b)
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 m a -> b -> Proxy a' a b' b m (a, b)
go (b' -> Proxy a' a b' b m a
fb' b'
b') b
w)
            M       m (Proxy a' a b' b m a)
m      -> m (Proxy a' a b' b m (a, b)) -> Proxy a' a b' b m (a, b)
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 m a
p', b
w') <- m (Proxy a' a b' b m a) -> m (Proxy a' a b' b m a, b)
forall w (m :: * -> *) a. MonadWriter w m => m a -> m (a, w)
listen m (Proxy a' a b' b m a)
m
                Proxy a' a b' b m (a, b) -> m (Proxy a' a b' b m (a, b))
forall (m :: * -> *) a. Monad m => a -> m a
return (Proxy a' a b' b m a -> b -> Proxy a' a b' b m (a, b)
go Proxy a' a b' b m a
p' (b -> Proxy a' a b' b m (a, b)) -> b -> Proxy a' a b' b m (a, b)
forall a b. (a -> b) -> a -> b
$! b -> b -> b
forall a. Monoid a => a -> a -> a
mappend b
w b
w') )
            Pure    a
r      -> (a, b) -> Proxy a' a b' b m (a, b)
forall a' a b' b (m :: * -> *) r. r -> Proxy a' a b' b m r
Pure (a
r, b
w)

    pass :: Proxy a' a b' b m (a, w -> w) -> Proxy a' a b' b m a
pass Proxy a' a b' b m (a, w -> w)
p0 = Proxy a' a b' b m (a, w -> w) -> w -> Proxy a' a b' b m a
forall w (m :: * -> *) a' a b' b r.
MonadWriter w m =>
Proxy a' a b' b m (r, w -> w) -> w -> Proxy a' a b' b m r
go Proxy a' a b' b m (a, w -> w)
p0 w
forall a. Monoid a => a
mempty
      where
        go :: Proxy a' a b' b m (r, w -> w) -> w -> Proxy a' a b' b m r
go Proxy a' a b' b m (r, w -> w)
p w
w = case Proxy a' a b' b m (r, w -> w)
p of
            Request a'
a' a -> Proxy a' a b' b m (r, w -> w)
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 m (r, w -> w) -> w -> Proxy a' a b' b m r
go (a -> Proxy a' a b' b m (r, w -> w)
fa  a
a ) w
w)
            Respond b
b  b' -> Proxy a' a b' b m (r, w -> w)
fb' -> 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 m (r, w -> w) -> w -> Proxy a' a b' b m r
go (b' -> Proxy a' a b' b m (r, w -> w)
fb' b'
b') w
w)
            M       m (Proxy a' a b' b m (r, w -> w))
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 m (r, w -> w)
p', w
w') <- (w -> w)
-> m (Proxy a' a b' b m (r, w -> w), w)
-> m (Proxy a' a b' b m (r, w -> w), w)
forall w (m :: * -> *) a. MonadWriter w m => (w -> w) -> m a -> m a
censor (w -> w -> w
forall a b. a -> b -> a
const w
forall a. Monoid a => a
mempty) (m (Proxy a' a b' b m (r, w -> w))
-> m (Proxy a' a b' b m (r, w -> w), w)
forall w (m :: * -> *) a. MonadWriter w m => m a -> m (a, w)
listen m (Proxy a' a b' b m (r, w -> w))
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 m (r, w -> w) -> w -> Proxy a' a b' b m r
go Proxy a' a b' b m (r, w -> w)
p' (w -> Proxy a' a b' b m r) -> w -> Proxy a' a b' b m r
forall a b. (a -> b) -> a -> b
$! w -> w -> w
forall a. Monoid a => a -> a -> a
mappend w
w w
w') )
            Pure   (r
r, w -> w
f)  -> 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, w -> w) -> 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, w -> w) -> m (Proxy a' a b' b m r, w -> w)
forall (m :: * -> *) a. Monad m => a -> m a
return (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, \w
_ -> w -> w
f w
w)))

instance MonadError e m => MonadError e (Proxy a' a b' b m) where
    throwError :: e -> Proxy a' a b' b m a
throwError = m a -> Proxy a' a b' b m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (m a -> Proxy a' a b' b m a)
-> (e -> m a) -> e -> Proxy a' a b' b m a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. e -> m a
forall e (m :: * -> *) a. MonadError e m => e -> m a
throwError
    catchError :: Proxy a' a b' b m a
-> (e -> Proxy a' a b' b m a) -> Proxy a' a b' b m a
catchError Proxy a' a b' b m a
p0 e -> Proxy a' a b' b m a
f = Proxy a' a b' b m a -> Proxy a' a b' b m a
go Proxy a' a b' b m a
p0
      where
        go :: Proxy a' a b' b m a -> Proxy a' a b' b m a
go Proxy a' a b' b m a
p = case Proxy a' a b' b m a
p of
            Request a'
a' a -> Proxy a' a b' b m a
fa  -> a' -> (a -> Proxy a' a b' b m a) -> Proxy a' a b' b 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
a  -> Proxy a' a b' b m a -> Proxy a' a b' b m a
go (a -> Proxy a' a b' b m a
fa  a
a ))
            Respond b
b  b' -> Proxy a' a b' b m a
fb' -> b -> (b' -> Proxy a' a b' b m a) -> Proxy a' a b' b 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 b
b  (\b'
b' -> Proxy a' a b' b m a -> Proxy a' a b' b m a
go (b' -> Proxy a' a b' b m a
fb' b'
b'))
            Pure    a
r      -> a -> Proxy a' a b' b m a
forall a' a b' b (m :: * -> *) r. r -> Proxy a' a b' b m r
Pure a
r
            M          m (Proxy a' a b' b m a)
m   -> m (Proxy a' a b' b m a) -> Proxy a' a b' b m a
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 m a
p' <- m (Proxy a' a b' b m a)
m
                Proxy a' a b' b m a -> m (Proxy a' a b' b m a)
forall (m :: * -> *) a. Monad m => a -> m a
return (Proxy a' a b' b m a -> Proxy a' a b' b m a
go Proxy a' a b' b m a
p') ) m (Proxy a' a b' b m a)
-> (e -> m (Proxy a' a b' b m a)) -> m (Proxy a' a b' b m a)
forall e (m :: * -> *) a.
MonadError e m =>
m a -> (e -> m a) -> m a
`catchError` (\e
e -> Proxy a' a b' b m a -> m (Proxy a' a b' b m a)
forall (m :: * -> *) a. Monad m => a -> m a
return (e -> Proxy a' a b' b m a
f e
e)) )

instance MonadThrow m => MonadThrow (Proxy a' a b' b m) where
    throwM :: e -> Proxy a' a b' b m a
throwM = m a -> Proxy a' a b' b m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (m a -> Proxy a' a b' b m a)
-> (e -> m a) -> e -> Proxy a' a b' b m a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. e -> m a
forall (m :: * -> *) e a. (MonadThrow m, Exception e) => e -> m a
throwM
    {-# INLINE throwM #-}

instance MonadCatch m => MonadCatch (Proxy a' a b' b m) where
    catch :: Proxy a' a b' b m a
-> (e -> Proxy a' a b' b m a) -> Proxy a' a b' b m a
catch Proxy a' a b' b m a
p0 e -> Proxy a' a b' b m a
f = Proxy a' a b' b m a -> Proxy a' a b' b m a
go Proxy a' a b' b m a
p0
      where
        go :: Proxy a' a b' b m a -> Proxy a' a b' b m a
go Proxy a' a b' b m a
p = case Proxy a' a b' b m a
p of
            Request a'
a' a -> Proxy a' a b' b m a
fa  -> a' -> (a -> Proxy a' a b' b m a) -> Proxy a' a b' b 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
a  -> Proxy a' a b' b m a -> Proxy a' a b' b m a
go (a -> Proxy a' a b' b m a
fa  a
a ))
            Respond b
b  b' -> Proxy a' a b' b m a
fb' -> b -> (b' -> Proxy a' a b' b m a) -> Proxy a' a b' b 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 b
b  (\b'
b' -> Proxy a' a b' b m a -> Proxy a' a b' b m a
go (b' -> Proxy a' a b' b m a
fb' b'
b'))
            Pure    a
r      -> a -> Proxy a' a b' b m a
forall a' a b' b (m :: * -> *) r. r -> Proxy a' a b' b m r
Pure a
r
            M          m (Proxy a' a b' b m a)
m   -> m (Proxy a' a b' b m a) -> Proxy a' a b' b m a
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 m a
p' <- m (Proxy a' a b' b m a)
m
                Proxy a' a b' b m a -> m (Proxy a' a b' b m a)
forall (m :: * -> *) a. Monad m => a -> m a
return (Proxy a' a b' b m a -> Proxy a' a b' b m a
go Proxy a' a b' b m a
p') ) m (Proxy a' a b' b m a)
-> (e -> m (Proxy a' a b' b m a)) -> m (Proxy a' a b' b m a)
forall (m :: * -> *) e a.
(MonadCatch m, Exception e) =>
m a -> (e -> m a) -> m a
`Control.Monad.Catch.catch` (\e
e -> Proxy a' a b' b m a -> m (Proxy a' a b' b m a)
forall (m :: * -> *) a. Monad m => a -> m a
return (e -> Proxy a' a b' b m a
f e
e)) )

{-| The monad transformer laws are correct when viewed through the 'observe'
    function:

@
'observe' ('lift' ('return' r)) = 'observe' ('return' r)

'observe' ('lift' (m '>>=' f)) = 'observe' ('lift' m '>>=' 'lift' '.' f)
@

    This correctness comes at a small cost to performance, so use this function
    sparingly.

    This function is a convenience for low-level @pipes@ implementers.  You do
    not need to use 'observe' if you stick to the safe API.
-}
observe :: Monad m => Proxy a' a b' b m r -> Proxy a' a b' b m r
observe :: Proxy a' a b' b m r -> Proxy a' a b' b m r
observe Proxy a' a b' b m r
p0 = 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 a' a b' b m r -> m (Proxy a' a b' b m r)
forall (m :: * -> *) a' a b' b r.
Monad m =>
Proxy a' a b' b m r -> m (Proxy a' a b' b m r)
go Proxy a' a b' b m r
p0) where
    go :: Proxy a' a b' b m r -> m (Proxy a' a b' b m r)
go Proxy a' a b' b m r
p = case Proxy a' a b' b m r
p of
        Request a'
a' a -> Proxy a' a b' b m r
fa  -> Proxy a' a b' b m r -> m (Proxy a' a b' b m r)
forall (m :: * -> *) a. Monad m => a -> m a
return (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 m r -> Proxy a' a b' b m r
forall (m :: * -> *) a' a b' b r.
Monad m =>
Proxy a' a b' b m r -> Proxy a' a b' b m r
observe (a -> Proxy a' a b' b m r
fa  a
a )))
        Respond b
b  b' -> Proxy a' a b' b m r
fb' -> Proxy a' a b' b m r -> m (Proxy a' a b' b m r)
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 m r -> Proxy a' a b' b m r
forall (m :: * -> *) a' a b' b r.
Monad m =>
Proxy a' a b' b m r -> Proxy a' a b' b m r
observe (b' -> Proxy a' a b' b m r
fb' b'
b')))
        M          m (Proxy a' a b' b m r)
m'  -> m (Proxy a' a b' b m r)
m' m (Proxy a' a b' b m r)
-> (Proxy a' a b' b m r -> m (Proxy a' a b' b m r))
-> m (Proxy a' a b' b m r)
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= Proxy a' a b' b m r -> m (Proxy a' a b' b m r)
go
        Pure    r
r      -> Proxy a' a b' b m r -> m (Proxy a' a b' b m r)
forall (m :: * -> *) a. Monad m => a -> m a
return (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 observe #-}

-- | The empty type, used to close output ends
type X = Void

-- | Use 'closed' to \"handle\" impossible outputs
closed :: X -> a
closed :: X -> a
closed = X -> a
forall a. X -> a
Data.Void.absurd
{-# INLINABLE closed #-}