[Haskell-cafe] class default method proposal

Duncan Coutts duncan.coutts at worc.ox.ac.uk
Tue Dec 11 09:20:52 EST 2007

I'd just like to float an idea that's related to the Class Alias
proposal[1] but is perhaps somewhat simpler.

We all know that Functor should have been a superclass of Monad, and
indeed we now know that Applicative should be too. Making such a change
would break lots of things however so the change does not happen.

However in this case the Monad operations can be used to implement the
Functor and Applicative class methods. So it would be nice if we could
get them for free if the author did not choose to write the Functor and
Applicative instances.

So my suggestion is that we let classes declare default implementations
of methods from super-classes.

class Functor m => Monad m where
  {- the ordinary bits -}

  fmap f m    = m >>= return . f

So if there already is a Functor instance for m then the default
implementation of fmap is not used.

Does this proposal have any unintended consequences? I'm not sure.
Please discuss :-)


[1] http://repetae.net/recent/out/classalias.html

More information about the Haskell-Cafe mailing list