为什么Hackage上的对的Monad实例没有返回实现?
在写下这个答案时,主要是为了更好地理解对作为 monads 的理解,我偶然发现了我阅读的 Hackage 上的源代码Monad,(,) a仅参考了这个实例
instance Monoid a => Monad ((,) a) where
(u, a) >>= k = case k a of (v, b) -> (u <> v, b)
在哪儿return???我希望能找到这样的东西
return a = (mempty, a)
除了上面的两行。这个定义是否return以某种方式隐含在其他事物中?或者它可能是在其他地方定义的?
回答
在Haskell的现代版本(具体base版本4.8.0.0和更新,对应于GHC版本7.10.1和更新版本),该Monad班有默认实现return = pure,所以它的情况下,只需要定义>>=。这是Functor-Applicative-Monad 提案的结果。
- (... and so we find `instance Monoid a => Applicative ((,) a) where pure x = (mempty, x)` [just above the same source](https://hackage.haskell.org/package/base-4.14.1.0/docs/src/GHC.Base.html#line-446) linked in the question )