{-# LANGUAGE TypeOperators, RankNTypes, TypeFamilies, NoImplicitPrelude #-}
module Data.Category.Yoneda where
import Data.Category
import Data.Category.Functor
import Data.Category.NaturalTransformation
type YonedaEmbedding k =
Postcompose (Hom k) (Op k) :.:
(Postcompose (Swap k (Op k)) (Op k) :.: Tuple k (Op k))
yonedaEmbedding :: Category k => YonedaEmbedding k
yonedaEmbedding = postcompose Hom :.: (postcompose swap :.: Tuple)
data Yoneda (k :: * -> * -> *) f = Yoneda
instance (Category k, Functor f, Dom f ~ Op k, Cod f ~ (->)) => Functor (Yoneda k f) where
type Dom (Yoneda k f) = Op k
type Cod (Yoneda k f) = (->)
type Yoneda k f :% a = Nat (Op k) (->) (k :-*: a) f
Yoneda % Op ab = \n -> n . yonedaEmbedding % ab
fromYoneda :: (Category k, Functor f, Dom f ~ Op k, Cod f ~ (->)) => f -> Nat (Op k) (->) (Yoneda k f) f
fromYoneda f = Nat Yoneda f (\(Op a) n -> (n ! Op a) a)
toYoneda :: (Category k, Functor f, Dom f ~ Op k, Cod f ~ (->)) => f -> Nat (Op k) (->) f (Yoneda k f)
toYoneda f = Nat f Yoneda (\(Op a) fa -> Nat (hom_X a) f (\_ h -> (f % Op h) fa))