{-# LANGUAGE CPP #-}
{-# LANGUAGE MultiParamTypeClasses #-}
#if __GLASGOW_HASKELL__ >= 702 && __GLASGOW_HASKELL__ < 710
{-# LANGUAGE Trustworthy #-}
#endif
module Control.Monad.Trans.Adjoint
( Adjoint
, runAdjoint
, adjoint
, AdjointT(..)
) where
import Prelude hiding (sequence)
#if __GLASGOW_HASKELL__ < 710
import Control.Applicative
#endif
import Control.Monad (ap, liftM)
import Control.Monad.Trans.Class
import Data.Traversable
import Data.Functor.Adjunction
import Data.Functor.Identity
type Adjoint f g = AdjointT f g Identity
newtype AdjointT f g m a = AdjointT { forall (f :: * -> *) (g :: * -> *) (m :: * -> *) a.
AdjointT f g m a -> g (m (f a))
runAdjointT :: g (m (f a)) }
adjoint :: Functor g => g (f a) -> Adjoint f g a
adjoint :: forall (g :: * -> *) (f :: * -> *) a.
Functor g =>
g (f a) -> Adjoint f g a
adjoint = forall (f :: * -> *) (g :: * -> *) (m :: * -> *) a.
g (m (f a)) -> AdjointT f g m a
AdjointT forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap forall a. a -> Identity a
Identity
runAdjoint :: Functor g => Adjoint f g a -> g (f a)
runAdjoint :: forall (g :: * -> *) (f :: * -> *) a.
Functor g =>
Adjoint f g a -> g (f a)
runAdjoint = forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap forall a. Identity a -> a
runIdentity forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (f :: * -> *) (g :: * -> *) (m :: * -> *) a.
AdjointT f g m a -> g (m (f a))
runAdjointT
instance (Adjunction f g, Monad m) => Functor (AdjointT f g m) where
fmap :: forall a b. (a -> b) -> AdjointT f g m a -> AdjointT f g m b
fmap a -> b
f (AdjointT g (m (f a))
g) = forall (f :: * -> *) (g :: * -> *) (m :: * -> *) a.
g (m (f a)) -> AdjointT f g m a
AdjointT forall a b. (a -> b) -> a -> b
$ forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (forall (m :: * -> *) a1 r. Monad m => (a1 -> r) -> m a1 -> m r
liftM (forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap a -> b
f)) g (m (f a))
g
a
b <$ :: forall a b. a -> AdjointT f g m b -> AdjointT f g m a
<$ AdjointT g (m (f b))
g = forall (f :: * -> *) (g :: * -> *) (m :: * -> *) a.
g (m (f a)) -> AdjointT f g m a
AdjointT forall a b. (a -> b) -> a -> b
$ forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (forall (m :: * -> *) a1 r. Monad m => (a1 -> r) -> m a1 -> m r
liftM (a
b forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$)) g (m (f b))
g
instance (Adjunction f g, Monad m) => Applicative (AdjointT f g m) where
pure :: forall a. a -> AdjointT f g m a
pure = forall (f :: * -> *) (g :: * -> *) (m :: * -> *) a.
g (m (f a)) -> AdjointT f g m a
AdjointT forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (f :: * -> *) (u :: * -> *) a b.
Adjunction f u =>
(f a -> b) -> a -> u b
leftAdjunct forall (m :: * -> *) a. Monad m => a -> m a
return
<*> :: forall a b.
AdjointT f g m (a -> b) -> AdjointT f g m a -> AdjointT f g m b
(<*>) = forall (m :: * -> *) a b. Monad m => m (a -> b) -> m a -> m b
ap
instance (Adjunction f g, Monad m) => Monad (AdjointT f g m) where
return :: forall a. a -> AdjointT f g m a
return = forall (f :: * -> *) a. Applicative f => a -> f a
pure
AdjointT g (m (f a))
m >>= :: forall a b.
AdjointT f g m a -> (a -> AdjointT f g m b) -> AdjointT f g m b
>>= a -> AdjointT f g m b
f = forall (f :: * -> *) (g :: * -> *) (m :: * -> *) a.
g (m (f a)) -> AdjointT f g m a
AdjointT forall a b. (a -> b) -> a -> b
$ forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= forall (f :: * -> *) (u :: * -> *) a b.
Adjunction f u =>
(a -> u b) -> f a -> b
rightAdjunct (forall (f :: * -> *) (g :: * -> *) (m :: * -> *) a.
AdjointT f g m a -> g (m (f a))
runAdjointT forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> AdjointT f g m b
f)) g (m (f a))
m
instance (Adjunction f g, Traversable f) => MonadTrans (AdjointT f g) where
lift :: forall (m :: * -> *) a. Monad m => m a -> AdjointT f g m a
lift = forall (f :: * -> *) (g :: * -> *) (m :: * -> *) a.
g (m (f a)) -> AdjointT f g m a
AdjointT forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap forall (t :: * -> *) (m :: * -> *) a.
(Traversable t, Monad m) =>
t (m a) -> m (t a)
sequence forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (f :: * -> *) (u :: * -> *) a.
Adjunction f u =>
a -> u (f a)
unit