Functor hierarchy proposal

From HaskellWiki
Revision as of 08:33, 5 March 2006 by Ashley Y (talk | contribs)
Jump to navigation Jump to search

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)
(and others)

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