default superclass instances in classes

She lets you add instance declarations to classes:

class Su x => Cl x where
  cloo :: foo x
  instance Su x where
    sloo :: goo x

It's not at all necessary to define all the methods of the superclass in such a default instance, as you can add the rest elsewhere.

NB, at the moment, She does not insert the superclass constraint Su x => for you. Do it yourself: arguably, it's better documentation, but that's not my real reason. I want to experiment with higher-order fake (because inexpressible) superclasses, in my own time...

The idea goes back to a 2006 suggestion by Jón Fairbairn.

what happens in instances?

When you declare an instance of Cl, She unpacks it to separate Cl and Su instances. So

instance Cl S where
  cloo = myfoo
gives me an instance Su S with the default sloo implementation.

You can add other Su methods and override the default definitions provided by the instance inside Cl. Don't declare a subordinate superclass instance for them, just dump them in and She will sort the methods by which class owns what.

instance Cl S where
  cloo = myfoo
  sloo = overridegoo

Bright folk will have noticed that a multiparameter class can have two superclasses with the same class-former. This will fox my overriding machinery. In that case, either you're in or you're out. (The principled way to handle this would be to allow subordinate superclass instances for purposes of disambiguation.)

can I switch it off?

If you don't want the default instance, because you're getting an instance from elsewhere, just say

instance Cl S where
  cloo = myfoo
  hiding instance Su S
and you'll get Cl S without Su S (or any of its default superclass instances). Oh, and if Su has default superclass instances, you can hide any of them in Cl and still keep Su: prune the tree anywhere! (Note that this will allow you to define a Monad, keep its default Applicative, but hide its default Functor.)

gremlins

  • No checking in situ: just blind macro expansion. Go on, blow it up!
  • Multiple superclasses with the same class-former won't really play nicely.
  • She mostly knows about classes only from modules She's processed, of course.
  • Some standard superclasses are now baked in. Here's the definitive source on which: ShesHers.lhs.