Unfoldable instance for cofree comonad

I am trying to figure out the difference between unfold/coiter from Control.Comonad.Cofree and unfold/ana from Data.Control.Fixedpoint . Hacker libraries - respectively. free and recursion-schemes .

Cofree and Fix seem to be cousins, and I'm trying to figure out what is possible with both and what is possible with only one of them.

I could write a Foldable instance for Cofree , so I can apply cata to the free monad derived from unfold/coiter :

 type instance Base (Cofree fa) = f instance Functor f => Foldable (Cofree fa) where project = unwrap 

But I could not build an instance of Unfoldable :

 instance Functor f => Unfoldable (Cofree fa) where embed = xembed xembed :: Functor f => f (Cofree fa) -> Cofree fa xembed = undefined 

Is it possible at all?

+4
source share
1 answer

No, you cannot write this function for Cofree at all. Consider f ~ Proxy (where data Proxy a = Proxy ):

 xembed :: Proxy (Cofree Proxy a) -> Cofree Proxy a -- ie xembed :: () -> a 

Get a out of nowhere.

However, you can write xembed for Free : wrap :: f (Free fa) -> Free fa . And similarly, you cannot write xproject :: Free fa -> f (Free fa) .

+4
source

Source: https://habr.com/ru/post/1499877/


All Articles