为什么 Hackage 上的对的 Monad 实例没有返回实现?

Enr*_*lis 4 monads haskell

在写下这个答案时,主要是为了更好地理解对作为 monads 的理解,我偶然发现了我阅读的 Hackage 上的源代码Monad(,) a仅参考了这个实例

instance Monoid a => Monad ((,) a) where
    (u, a) >>= k = case k a of (v, b) -> (u <> v, b)
Run Code Online (Sandbox Code Playgroud)

在哪儿return???我希望能找到这样的东西

    return a = (mempty, a)
Run Code Online (Sandbox Code Playgroud)

除了上面的两行。这个定义是否return以某种方式隐含在其他事物中?或者它可能是在其他地方定义的?

Jos*_*ica 9

在Haskell的现代版本(具体base版本4.8.0.0和更新,对应于GHC版本7.10.1和更新版本),Monad班有默认实现return = pure,所以它的情况下,只需要定义>>=。这是Functor-Applicative-Monad 提案的结果

  • (...所以我们找到 `instance Monoid a =&gt; Applicative ((,) a) where pure x = (mempty, x)` [就在同一源之上](https://hackage.haskell.org/package /base-4.14.1.0/docs/src/GHC.Base.html#line-446)链接在问题中) (2认同)