Koen Claessen suggests:

> In this case we could allow the programmer giving a default declaration
> for the superclass methods during the class definition of the subclass. An
> example would be: 
> 
>   class Functor f where
>     map :: (a -> b) -> m a -> m b
> 
>   class Functor m => Monad m where
>     (>>=)  :: m a -> (a -> m b) -> m b
>     return :: a -> m a
>     
>     map f m = m >>= (return . f)

This seems quite a good idea.  I was pondering some sort of restricted
version of the instance syntax, but this seems a much less troublesome
way of achieving the same effect.  It might be arguable that some sort
of syntactic distinction should be made between class and superclass
defaults, but this seems clear enough to me as it stands.

> We can apply the same idea at instance declarations. We could allow the
> following notation:
>     
>   instance Monad W where   ====>   instance Functor W where
>     m >>= k  = ..           for      map f m = ..
>     return x = ..
>     map f m  = ..                  instance Monad W where
>                                      m >>= k  = ..
>                                      return x = ..

I like this much less.  Firstly, it doesn't really buy any particular
convenience (beyond not having to type a single line), and more to the
point, it significantly obfuscates the fact that an instance of Functor
is actually being created, too.

> In combination with the class declaration extensions discussed above, we
> could even omit map.

That would be necessary, yes, otherwise there'd be little point or
convenience in having the superclass defaults.  Though to continue
my argument from above, perhaps this is still somewhat less than
clear that a new Functor instance is being introduced (though at least
it's evident from the class definition itself, here).  Perhaps this
argues for the Functor instance being "hidden", and only accessible
via Monadic contexts, unless a separate instance is given too?
I'm not sure either way on this point.

Slainte,
Alex.




Reply via email to