为什么MonadFix的实例不能用于连续monad? [英] Why can't there be an instance of MonadFix for the continuation monad?

查看:174
本文介绍了为什么MonadFix的实例不能用于连续monad?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们如何证明延续monad 没有 MonadFix

How can we prove that the continuation monad has no valid instance of MonadFix?

推荐答案

不是不能有一个 MonadFix 实例,只是库的类型有点过于拘束。如果您在所有可能的 r s中定义 ContT ,则不仅 MonadFix 成为可能,但是所有高达 Monad 的实例都不需要底层仿函数:

Well actually, it's not that there can't be a MonadFix instance, just that the library's type is a bit too constrained. If you define ContT over all possible rs, then not only does MonadFix become possible, but all instances up to Monad require nothing of the underlying functor :

newtype ContT m a = ContT { runContT :: forall r. (a -> m r) -> m r }
instance Functor (ContT m) where
  fmap f (ContT k) = ContT (\kb -> k (kb . f))
instance Monad (ContT m) where
  return a = ContT ($a)
  join (ContT kk) = ContT (\ka -> kk (\(ContT k) -> k ka))
instance MonadFix m => MonadFix (ContT m) where
  mfix f = ContT (\ka -> mfixing (\a -> runContT (f a) ka<&>(,a)))
    where mfixing f = fst <$> mfix (\ ~(_,a) -> f a )

这篇关于为什么MonadFix的实例不能用于连续monad?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆