我不确定如何在定点后推导出函子实例:
data FreeF f a next = PureF a | FreeF (f next) deriving (Functor)
data Mu f = In { out :: f ( Mu f ) }
newtype Free f a = Free( Mu (FreeF f a) )
instance Functor f => Functor (Free f) where
fmap h (Free (out -> PureF a)) = Free (In (PureF (h a)))
fmap h (Free (out -> FreeF fn)) = Free (In (fmap undefined undefined)) --stuck
如果我修改 Mu 以接受额外的类型参数,我可以继续直到...:
data Mu f a = In { out :: f ( Mu f a ) } deriving (Functor)
newtype Free f a = Free( Mu (FreeF f a) a )
instance Functor f => Functor (Free f ) where
fmap h (Free (out -> PureF a)) = Free . In . PureF $ h a
fmap h (Free (out -> FreeF fn)) = Free . In . FreeF $ fmap undefined fn
在这里我需要undefined :: Mu (FreeF f a) a -> Mu (FreeF f b) b
但mu f
它是相同的函子,在f
这里它的类型不同。
解决这个问题的正确方法是什么?