为什么联接是独立的,而不是Monad类型类的最小实现的一部分? [英] Why is join standalone, instead of part of the minimal implementation of the Monad typeclass?
问题描述
我宁愿通过join函数而不是>>=
来定义Monad的实例...从这种等效性开始:
I would prefer defining an instance of Monad via the join function instead of >>=
... Starting from that equivalence:
x >>= f = join (fmap f x)
您只能通过联接定义Monad实例吗?
Could you define a Monad instance only with join?
我希望加入{-# MINIMAL (>>=)| join #-}
为什么join
在顶层而不是Monad
类型类中?
Why is join
at the top level and not even in the Monad
typeclass?
推荐答案
遗憾的是,由于与广义新类型派生和角色系统相关的技术限制,join
不在GHC标准库中的Monad
类型类中.长话短说,考虑到某些新类型newtype T m a = MkT (m a)
,GHC不够聪明,无法弄清楚如何证明m (m a)
和m (T m a)
之间的表示相等性,这对于证明join
的第一个参数(这是证明这是必不可少的)的类型为m (m a) -> m a
).
Sadly, join
is not a part of the Monad
typeclass in GHC’s standard library because of technical restrictions related to generalized newtype deriving and the roles system. Long story short, given some newtype newtype T m a = MkT (m a)
, GHC is not smart enough to figure out how to prove representational equality between m (m a)
and m (T m a)
, which is necessary for proving representational equality for the first argument of join
(which has type m (m a) -> m a
).
幸运的是,最近对GHC Haskell的扩展QuantifiedConstraints
可能使角色系统足够智能以支持此功能.有关问题及其潜在解决方案的更详细处理,请参阅Ryan Scott的博客文章
Fortunately, a recent extension to GHC Haskell, QuantifiedConstraints
, might make it possible to make the roles system smart enough to support this. For a more detailed treatment of both the problem and its potential solution, see Ryan Scott’s blog post, How QuantifiedConstraints can let us put join back in Monad.
这篇关于为什么联接是独立的,而不是Monad类型类的最小实现的一部分?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!