[Haskell-cafe] Specializing classes with classes

Stuart Cook scook0 at gmail.com
Sun Dec 30 05:48:29 EST 2007

On Dec 30, 2007 2:43 PM, Jake McArthur <jake.mcarthur at gmail.com> wrote:
> Really? This code doesn't even really make any sense to me. In order
> to be an instance of Bar, t has to already be an instance of Foo,
> implying that the function hi is already defined for t. What would the
> function in this example do?

Here's a different example:

  class Functor f where
      fmap :: (a -> b) -> f a -> f b

  class (Functor m) => Monad m where
      (>>=) :: m a -> (a -> m b) -> m b
      return :: a -> m a
      fmap = liftM

In this case, any instance of Monad would automatically get a default
Functor instance, which could presumably be overridden by an explicit
Functor instance. The point is that (by default) you don't have to
give the definition of *fmap* yourself. As AJB pointed out, the
details can be subtle, so this sort of thing hasn't been implemented
by anybody as far as I'm aware.


More information about the Haskell-Cafe mailing list