Unfoldable instance for the cofree comonad

429 views Asked by At

I'm trying to figure out the difference between unfold/coiter from Control.Comonad.Cofree and unfold/ana from Data.Control.Fixedpoint. Hackage libraries are resp. 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 an instance of Foldable for Cofree so I can apply cata to a free monad obtained from unfold/coiter:

type instance Base (Cofree f a) = f

instance Functor f => Foldable (Cofree f a) where
    project = unwrap

But I couldn't construct an Unfoldable instance:

instance Functor f => Unfoldable (Cofree f a) where
    embed = xembed

xembed :: Functor f => f (Cofree f a) -> Cofree f a 
xembed = undefined

Is it possible at all?

1

There are 1 answers

2
shachaf On BEST ANSWER

No, you can't write this function for Cofree in general. Consider f ~ Proxy (where data Proxy a = Proxy):

xembed :: Proxy (Cofree Proxy a) -> Cofree Proxy a
-- i.e.
xembed :: () -> a

Has to get an a out of nowhere.

However, you can write xembed for Free: wrap :: f (Free f a) -> Free f a. And similarly you can't write xproject :: Free f a -> f (Free f a) in general.