3

While writing down this answer, mostly to get a better understanding of pairs as monads, I stumbed into this source code on Hackage where I read, in reference to the Monad instance of (,) a only this

instance Monoid a => Monad ((,) a) where
    (u, a) >>= k = case k a of (v, b) -> (u <> v, b)

where's return??? I expected to find something like this

    return a = (mempty, a)

in addition to the two lines above. Is this definition of return somehow implied by something else? Or maybe it's defined somewhere else?

duplode
  • 33,731
  • 7
  • 79
  • 150
Enlico
  • 23,259
  • 6
  • 48
  • 102

1 Answers1

9

In modern versions of Haskell (specifically, base version 4.8.0.0 and newer, corresponding to GHC version 7.10.1 and newer), the Monad class has the default implementation return = pure, so instances of it only need to define >>=. This was a result of the Functor-Applicative-Monad Proposal.

Enlico
  • 23,259
  • 6
  • 48
  • 102
  • 2
    (... 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 ) – Will Ness Mar 20 '21 at 18:19