All Monads are Functors

Marc A. Ziegert coeus at gmx.de
Tue Aug 15 22:18:18 EDT 2006


Am Dienstag, 15. August 2006 23:34 schrieb Jon Fairbairn:
> On 2006-08-15 at 16:25CDT Taral wrote:
> > On 8/15/06, Bulat Ziganshin <bulat.ziganshin at gmail.com> wrote:
> > > in this case we lose "class Functor a => Monad a" base class
> > > declaration. so what will be the meaning of this:
> > 
> > I don't see why that is the case.
> > 
> > class Functor m => Monad m where
> >     return :: a -> m a
> >     (>>=) :: m a -> (a -> m b) -> m b
> >     instance Functor m where
> >         fmap f = (>>= return . f)
> > 
> > What's wrong with this? All Monads are Functors. If you don't provide
> > a Functor, it gets defined for you. The problem is working out whether
> > to use the default Functor or an external Functor.
> 
> It seems obvious to me that we always use an external
> definition if one exists, so I suppose the problem is
> knowing whether an external instance exists -- so this
> proposal would rely on doing something about scoping for
> instances, I suppose.
> 

I see a solution in different ways of writitng an instance:

[code]

-- to define the functions in the Functor, using the defaults defined in the class _Functor_ (assumed there would be any default):
instance Monad m where
  return = ...
  (>>=) = ...
instance Functor m where

-- to define the functions in the Functor, using the defaults defined in the class _Monad_:
instance Monad m where
  return = ...
  (>>=) = ...
  instance Functor m where

--to define a new Functor:
instance Monad m where
  return = ...
  (>>=) = ...
  instance Functor m where
    fmap = ...
--or
instance Monad m where
  return = ...
  (>>=) = ...
instance Functor m where
  fmap = ...

[/code]

the advantages are, that we can decide which default functions we want to use, and that it will be compatible to the old library.


- marc


More information about the Haskell-prime mailing list