All Monads are Functors
Jon Fairbairn
jon.fairbairn at cl.cam.ac.uk
Mon Aug 14 05:49:58 EDT 2006
On 2006-08-14 at 12:03+0400 Bulat Ziganshin wrote:
> Hello Taral,
>
> Monday, August 14, 2006, 9:02:58 AM, you wrote:
>
> > In my opinion, an instance definition of a subclass should allow the
> > superclass's methods to be defined as if they were part of the
> > subclass, e.g.:
>
> > instance Monad [] where
> > fmap = map
> > return x = [x]
> > join = concat
>
> i support this idea. [...]
I'm not sure it's quite right. Surely it only makes sense if
it defines all the (necessary) superclass methods -- in
other words, what you are doing is defining an instance,
just omitting the "instance Functor []" line, which doesn't
seem like a great advantage. If we are going to play around
with this stuff, here's another suggestion that solves my
original problem more neatly:
In a class declaration, a superclass context is a
requirement that instances of the class have instances of
the superclass; this is similar to the type declarations of
the methods. We could have had
class Monad m where
instance Functor m
(>>=):: ...
instead of
class Functor m => Monad m where
(>>=):: ...
of course, there's no reason to do that, but what I'm
proposing is that we allow default instance declarations in
class declarations in much the same way as default methods:
> class Functor m => Monad m where
> (>>=):: ...
> return:: ...
> join:: ...
> instance Functor m where
> fmap f = (>>= return . f)
This shouldn't be hard to implement: all the compiler has to
do when reading an instance declaration for Monad is to
generate an instance declaration for Functor, substituting
the espression for m that comes from the instance
declaration for Monad.
I don't know whether there's anything to be gained by adding
the option of overriding the default inside an instance
declaration:
> instance Monad [] where
> return x = [x]
> join = concat
> instance Functor [] where
> fmap = map
but clearly a top-level instance declaration would override
the default anyway.
--
Jón Fairbairn Jon.Fairbairn at cl.cam.ac.uk
More information about the Haskell-prime
mailing list