Instances of multiple classes at once

Fergus Henderson fjh@cs.mu.oz.au
Sat, 10 Feb 2001 16:48:30 +1100


On 08-Feb-2001, Dylan Thurston <dpt@math.harvard.edu> wrote:
> On Thu, Feb 08, 2001 at 09:41:56PM +1100, Fergus Henderson wrote:
> > One point that needs to be  resolved is the interaction with default methods.
> > Consider
> > 
> >         class foo a where
> >                 f :: ...
> > 		f = ...
> >                 f2 :: ...
> > 		f2 = ...
> > 
> >         class (foo a) => bar a where
> >                 b :: ...
> >  
> >         instance bar T where
> > 		-- no definitions for f or f2
> > 		b = 42
> > 
> > Should this define an instance for `foo T'?
> > (I think not.)
> 
> Whyever not?

Because too much Haskell code uses classes where the methods are
defined in terms of each other:

	class Foo a where
		-- you should define either f or f2
		f :: ...
		f = ... f2 ...
		f2 :: ...
		f2 = ... f ...

> Because there is no textual mention of class Foo in the
> instance for Bar?

Right, and because allowing the compiler to automatically generate
instances for class Foo without the programmer having considered
whether those instances are OK is too dangerous.

> Think about the case of a superclass with no methods;
> wouldn't you want to allow automatic instances in this case?

Yes.

I think Marcin has a better idea: 

| So maybe there should be a way to specify that default definitions
| are cyclic and some of them must be defined?

-- 
Fergus Henderson <fjh@cs.mu.oz.au>  |  "I have always known that the pursuit
                                    |  of excellence is a lethal habit"
WWW: <http://www.cs.mu.oz.au/~fjh>  |     -- the last words of T. S. Garp.