这个问题使用了来自http://hackage.haskell.org/package/recursion-schemes-4.0/docs/Data-Functor-Foldable.html的概念/导入
我试图将其扩展到将给定的单子连接到变形过程中。下面是我正在试图编译的代码:
class (Monad m, Foldable t) => FoldableM m t where
distM :: Base t (m a) -> m (Base t a)
cataM :: (Base t a -> m a) -> t -> m a
cataM f = join . liftM f . distM . fmap (cataM f) . project但是,由于某种原因,对distM在cataM中的调用无法确定如何使用相同的t。
我得到的错误是:
Expected type: Base t (m a) -> m (Base t a)
Actual type: Base t0 (m a) -> m (Base t0 a) 在这里,我让代码变得不那么性感,更容易调试了:
cataM :: forall a . (Base t a -> m a) -> t -> m a
cataM f t = join d
where
a :: Base t t
a = project t
b :: Base t (m a)
b = fmap (cataM f) a
g :: Base t (m a) -> m (Base t a)
g = distM
c :: m (Base t a)
c = g b
d :: m (m a)
d = liftM f cg的定义是引起问题的原因。
编辑:据我所知,问题是当它调用distM时,它只有Base t来推断类型,所以它无法计算出t。这是令人沮丧的,因为我知道我想使用什么t。事实上,我认为如果我能够手动向distM提供类型参数,它将解决问题,但我认为这是不可能的。
这里有一个解决方案,但我对此并不满意:
class (Monad m, Foldable t) => FoldableM m t where
distM :: t -> Base t (m a) -> m (Base t a)
cataM :: forall a . (Base t a -> m a) -> t -> m a
cataM f = join . liftM f . distM (undefined :: t) . fmap (cataM f) . project编辑2:学习Proxy很酷(谢谢Antal)。我已经干了好几年了,刚刚又学到了一件新的东西。我喜欢这种语言。我使用的解决方案是:
class (Monad m, Foldable t) => FoldableM m t where
distM :: proxy t -> Base t (m a) -> m (Base t a)
cataM :: forall a . (Base t a -> m a) -> t -> m a
cataM f = join . liftM f . distM (Proxy :: Proxy t) . fmap (cataM f) . project发布于 2013-11-19 16:29:21
Base t是一个类型家族,因此不能从Base t中识别t。如果不提到distM,就无法让t工作。
https://stackoverflow.com/questions/20076044
复制相似问题