Define methods for subclasses of type classes

haskell, typeclass

Solution

What you're asking for isn't (yet) implemented for Haskell. However, there is a proposal for such a feature called Default Superclass Instances, which would allow you to do declare:

class Functor f => Applicative f where
  return :: x -> f x
  (<*>) :: f (s -> t) -> f s -> f t

  instance Functor f where
    fmap = (<*>) . pure

Problem

I tried this: ``` class Functor f where fmap :: (a -> b) -> f a -> f b class (Functor f) => Applicative f where pure :: a -> f a (<*>) :: f (a -> b) -> f a -> f b fmap f x = pure f <*> x ``` I got this: ``` `fmap' is not a (visible) method of class `Applicative' ``` How to define `fmap` for `Applicative` and other subclasses of `Functor`?

Original source