Difference between revisions of "Functor hierarchy proposal"

From HaskellWiki
Jump to navigation Jump to search
(No difference)

Revision as of 21:53, 21 January 2006

Currently the standard libraries include a Functor class and a Monad class, and Functor is not a superclass of Monad, even though logically every monad is a functor. Various schemes have been proposed to create a richer hierarchy of Functor classes. Here's a typical example:

class Functor f where
  fmap :: (a -> b) -> f a -> f b
class (Functor f) => Idiom f where
  ap :: f (a -> b) -> f a -> f b
  return :: a -> f a
class (Idiom f) => Monad f where
  (>>=) :: f a -> (a -> f b) -> f b

satisfying the usual Functor and Monad laws, and

 fmap = ap . return
 ap fab fa = fab >>= (\ab -> fmap ab fa)

The downside of this is that every instance declaration of Monad would have to be accompanied by instance declarations for the superclasses.