Difference between revisions of "Functor hierarchy proposal"

From HaskellWiki
Jump to navigation Jump to search
 
Line 15: Line 15:
 
fmap = ap . return
 
fmap = ap . return
 
ap fab fa = fab >>= (\ab -> fmap ab fa)
 
ap fab fa = fab >>= (\ab -> fmap ab fa)
  +
:(and others)
   
 
The downside of this is that every instance declaration of <tt>Monad</tt> would have to be accompanied by instance declarations for the superclasses.
 
The downside of this is that every instance declaration of <tt>Monad</tt> would have to be accompanied by instance declarations for the superclasses.

Revision as of 08:33, 5 March 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)
(and others)

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