문제

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. 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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top