module Control.Comonad.Trans.Density
( DensityT(..)
, liftDensityT
, densityTToAdjunction, adjunctionToDensityT
) where
import Control.Comonad
import Control.Comonad.Trans.Class
import Data.Functor.Adjunction
data DensityT k a where
DensityT :: (k b -> a) -> k b -> DensityT k a
instance Functor (DensityT f) where
fmap f (DensityT g h) = DensityT (f . g) h
instance Extend (DensityT f) where
duplicate (DensityT f ws) = DensityT (DensityT f) ws
instance Comonad (DensityT f) where
extract (DensityT f a) = f a
instance ComonadTrans DensityT where
lower (DensityT f c) = extend f c
liftDensityT :: Comonad w => w a -> DensityT w a
liftDensityT = DensityT extract
densityTToAdjunction :: Adjunction f g => DensityT f a -> f (g a)
densityTToAdjunction (DensityT f v) = fmap (leftAdjunct f) v
adjunctionToDensityT :: Adjunction f g => f (g a) -> DensityT f a
adjunctionToDensityT = DensityT counit