allow to give default implementatoions for methods of base class

Bulat Ziganshin bulat.ziganshin at gmail.com
Mon Aug 14 07:09:01 EDT 2006


Hello Jon,

Monday, August 14, 2006, 1:49:58 PM, you wrote:

>> > 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.

This shrinks size of code that is especially important when writing
a lot of small instances. second, it allows me to think that all the
methods belongs to the same class instead of specifying each and every class:

class Show s => Stream s where
  sTell :: ..
class Stream s => OutputStream s where
  sPutChar :: ..

instance Show s where
  show = ..
instance Stream s where
  sTell = ..
instance OutputStream s where
  sPutChar = ..

i might prefer to write just

instance OutputStream s where
  sPutChar = ..
  sTell = ..
  show = ..

- and as you can see i also changed the ordering/grouping of
operations. of course it's just syntax sugar, but i like it - it will
shrink class declarations and bring them closer to OOP style when
derived class also "owns" all the methods of base classes


> 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

i think, you mean:

>> class Monad m where
>>       instance Functor m where
>>                fmap f =  (>>= return . f)
>>       (>>=):: ...
>>       return:: ...
>>       join:: ...

i support this too. but bringing these two ideas together the class
declaration should look as

class Functor m => Monad m where
      fmap f =  (>>= return . f)
      (>>=):: ...
      return:: ...
      join:: ...

and instance declaration should be:

instance Monad [] where
    fmap = map
    return x = [x]
    join = concat

instead of:

> instance Monad [] where
>     return x = [x]
>     join = concat
>     instance Functor [] where
>              fmap = map

this proposal should be named as subj, independent of syntax form used

--
Best regards,
 Bulat                            mailto:Bulat.Ziganshin at gmail.com



More information about the Haskell-prime mailing list