2013-08-30 47 views
4

我試圖找出unfold/coiterControl.Comonad.Cofreeunfold/ana之間的差異從Data.Control.Fixedpoint。 Hackage庫是resp。 freerecursion-schemescofree comonad的不可摺疊實例

CofreeFix似乎是堂兄弟,我試圖找出什麼是可能的與兩個和什麼是可能的只有他們之一。

我能寫的FoldableCofree一個實例,這樣我可以申請cataunfold/coiter獲得一個免費的單子:

type instance Base (Cofree f a) = f 

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

但我無法構造一個實例:

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

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

它有可能嗎?

回答

4

不,你一般不能爲Cofree寫這個函數。考慮f ~ Proxy(其中data Proxy a = Proxy):

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

要得到一個a無章可循。

但是,您可以寫的xembedwrap :: f (Free f a) -> Free f a。一般來說,你不能寫xproject :: Free f a -> f (Free f a)

+0

你的意思是我不能寫'免費monads的'項目,並且'embed'免費的comonads? – nponeccop

+1

是的,我修正了(這是一個cofree comonad,不是免費的comonad!:-))。然而,現在我已經看到了這些類實際上是什麼,也許你的'Base'類型不是你想要的(我不太清楚你在那之後是什麼)。 'Cofree fa'是'\ self - >(a,f self)'的固定點,'Free fa'是'\ self - >的固定點。既可以是(f self)',也可以是'f' 。 – shachaf