Semi-automatic instances (was Re: Re[6]: All Monads are Functors)

Jón Fairbairn jon.fairbairn at cl.cam.ac.uk
Mon Aug 28 07:12:58 EDT 2006


Jón Fairbairn <jon.fairbairn at cl.cam.ac.uk> writes:

> John Meacham <john at repetae.net> writes:
> > The problem is you can't have working code change its behavior because
> > of a module import (other than failing), say, by bringing an instance
> > into scope that wasn't before. There is no way to have a monad instance
> > 'automatically' declare a functor instance without changing what the mo
> > instance looks like somehow which would be a backwards incompatable
> > change.
> 
> In that case we really ought just to accept that we have to
> declare the instances and swallow
> 
>    class Functor m => Monad m where ...
> 
> without any cleverness.

Or, alternatively, make my previous suggestion less
automatic. I was suggesting that we could allow default
instances of superclasses in class declarations; we'd say

> class Functor m =>
>       Monad m where
>       (>>=):: ...
>       instance Functor m where
>                fmap = ...<a>
>       ...

what I'm proposing here is that we do that, but the
"default" instances are never supplied automatically.
Instead we allow a "deriving" clause on instance
declarations:

> instance Monad [] deriving Functor where
>          ...

Now this would be exactly equivalent to 

> instance Monad [] where
>          ...
> instance Functor [] where
>          fmap = ...<a>

(and just as much an error if there's another instance
declaration for Functor somwhere), while an instance
declaration without the deriving clause would not say
anything about a Functor instance at all.

So, not fully automatic -- so avoiding the above problem,
but more concise than a whole instance declaration.


-- 
Jón Fairbairn                                 Jon.Fairbairn at cl.cam.ac.uk




More information about the Haskell-prime mailing list