{-# LANGUAGE RankNTypes #-}
newtype Coyoneda f a = Coyoneda {
uncoyo :: forall b r. ((b -> a) -> f b -> r) -> r
}
coyoneda f tx = Coyoneda (\k -> k f tx)
I think Coyoneda should work by simulating existentials with a higher-rank type, but I fail to construct values of this type. The coyoneda auxiliary function doesn't type check, because with regard to the term k f tx the higher-rank type variable b would escape its scope.
However, I cannot come up with another way to implement coyoneda. Is it possible at all?
A type
Tis isomorphic toby the Yoneda isomorphism.
In your case,
which is not what you wanted. To use coyoneda, you need to model an existential type instead:
So, let's turn this into a
forallHence, this is the correct encoding you need to use in your
Coyonedadefinition.