#if __GLASGOW_HASKELL__ >= 702 && __GLASGOW_HASKELL__ < 710
#endif
module Control.Monad.Trans.Contravariant.Adjoint
( Adjoint
, runAdjoint
, adjoint
, AdjointT(..)
) where
import Prelude hiding (sequence)
#if __GLASGOW_HASKELL__ < 710
import Control.Applicative
#endif
import Control.Comonad
import Control.Monad (ap)
import Data.Functor.Identity
import Data.Functor.Contravariant
import Data.Functor.Contravariant.Adjunction
type Adjoint f g = AdjointT f g Identity
newtype AdjointT f g w a = AdjointT { runAdjointT :: g (w (f a)) }
adjoint :: Contravariant g => g (f a) -> Adjoint f g a
adjoint = AdjointT . contramap runIdentity
runAdjoint :: Contravariant g => Adjoint f g a -> g (f a)
runAdjoint = contramap Identity . runAdjointT
instance (Adjunction f g, Functor w) => Functor (AdjointT f g w) where
fmap f (AdjointT g) = AdjointT $ contramap (fmap (contramap f)) g
instance (Adjunction f g, Comonad w) => Applicative (AdjointT f g w) where
pure = AdjointT . leftAdjunct extract
(<*>) = ap
instance (Adjunction f g, Comonad w) => Monad (AdjointT f g w) where
return = AdjointT . leftAdjunct extract
AdjointT m >>= f = AdjointT $ contramap (extend (rightAdjunct (runAdjointT . f))) m