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?
No, you can't write this function for
Cofree
in general. Considerf ~ Proxy
(wheredata Proxy a = Proxy
):Has to get an
a
out of nowhere.However, you can write
xembed
forFree
:wrap :: f (Free f a) -> Free f a
. And similarly you can't writexproject :: Free f a -> f (Free f a)
in general.