我不知道如何在不动点之后导出函式实例:
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的函子,在这里它的类型是不同的。
解决这个问题的正确方法是什么?
发布于 2016-01-14 23:43:54
mu f是同一个f的函子,在这里它的类型是不同的。
幸运的是,我们正在定义Functor (Free f),我们实际上使用这个Functor实例来映射PureF构造函数中的a,Functor (Free f)抽象了a的所有“内部”事件。
因此,每当我们想要映射两次a时(例如,当我们想实现FreeF f a (Mu (FreeF f a)) -> FreeF f b (Mu (FreeF f b))时),我们都可以通过将所有东西打包回Free,映射,然后再展开。
以下是使用原始数据定义进行的检查:
newtype Free f a = Free {unFree :: Mu (FreeF f a)} -- add "unFree"
instance Functor f => Functor (Free f) where
fmap h (Free (In (PureF a))) = Free (In (PureF (h a)))
fmap h (Free (In (FreeF fn))) =
Free (In (FreeF (fmap (unFree . fmap h . Free) fn)))一些测试:
{-# LANGUAGE UndecidableInstances, StandaloneDeriving #-}
deriving instance Show (f (Mu f)) => Show (Mu f)
deriving instance Show (Mu (FreeF f a)) => Show (Free f a)
foo :: Free [] Int
foo = Free $ In $ FreeF [ In $ PureF 100, In $ PureF 200 ]
> fmap (+100) foo
Free {unFree = In {out = FreeF [In {out = PureF 200},In {out = PureF 300}]}}发布于 2016-01-14 23:21:04
我以前没做过这件事,但我想我看到了些什么。关于向Mu添加参数的直觉是很好的,但是您需要将它传递给Free f,也就是说,f采用两个参数而不是一个参数:
newtype Mu f a = In { out :: f (Mu f a) a }Mu f应该是一个在适当条件下的Functor,这将为您提供您要寻找的实例。这些条件是什么?我们需要:
fmap' :: (a -> b) -> f (Mu f a) a -> f (Mu f b) b我们期望f在其第二个参数中是函数式,所以这是没有问题的。所以我们真正需要的是
f (Mu f a) b -> f (Mu f b) b
^ ^
+--not varying--+我们可以递归地使用实例来获取Mu f a -> Mu f b,因此看起来我们只需要f在其第一个参数中也是一个函子。因此:
class Bifunctor f where
bimap :: (a -> c) -> (b -> d) -> f a b -> f c d那么,您应该能够编写合适的实例。
instance (Functor f) => Bifunctor (FreeF f) ...
instance (Bifunctor f) => Functor (Mu f) ...https://stackoverflow.com/questions/34800925
复制相似问题