module Data.Functor.Contravariant.Adjunction
( Adjunction(..)
, corepAdjunction
, coindexAdjunction
) where
import Control.Monad.Instances ()
import Data.Functor.Contravariant
import Data.Functor.Corepresentable
class (Contravariant f, Corepresentable g) => Adjunction f g | f -> g, g -> f where
unit :: a -> g (f a)
counit :: a -> f (g a)
leftAdjunct :: (b -> f a) -> a -> g b
rightAdjunct :: (a -> g b) -> b -> f a
unit = leftAdjunct id
counit = rightAdjunct id
leftAdjunct f = contramap f . unit
rightAdjunct f = contramap f . counit
instance Adjunction (Op r) (Op r) where
unit a = Op (\k -> getOp k a)
counit = unit
instance Adjunction Predicate Predicate where
unit a = Predicate (\k -> getPredicate k a)
counit = unit
corepAdjunction :: Adjunction f g => (a -> f ()) -> g a
corepAdjunction = flip leftAdjunct ()
coindexAdjunction :: Adjunction f g => g a -> a -> f ()
coindexAdjunction = rightAdjunct . const