Associated type instances

Simon Peyton Jones simonpj at
Mon Jun 23 16:36:00 UTC 2014

I want to make withdraw (or, rather, simplify) a little-known feature in GHC, but before I do so I want to check that no one is going to have a heart attack.
Relevant bits of the user manual:
All of this arose when thinking about fixing Trac #9063.
I believe that this change will affect essentially nobody, and I propose to implement forthwith in HEAD (and hence 7.10).
Does anyone object?

The issue
Consider this:

class C a where

   type T a b :: *

instance C [x] where

   type T [x] b = x -> b
That is just what you'd expect.  But currently this is allowed too:

instance C [x] where

   type T [x] Int = x -> Int

   type T [x] Bool = Bool -> x
That is, GHC 7.8 allows many associated type instances, provided they don't overlap.  But, oddly you can't further instantiate the instance pattern. This would make just as much sense, but isn't allowed:

instance C [x] where

   type T [Int] b = b -> Int

   type T [Bool] b = Bool -> b
Moreover, as the user manual says, for an open kind like *, none of this really makes sense. It really only makes sense for a closed kind. Something like

class D a where

   type S (b :: Bool) a :: *
Now this would make some kind of sense:

instance D [x] where

   type S True [x] = x -> x

   type S False [x] = x
But for closed kinds, you really want a closed type family.  So this would be better:

instance D [x] where

   type S b [x] = SHelp x b

type family SHelp x b where

  SHelp x True = x -> x

  SHelp x False = x

So yes, you do have to declare a named helper type, but you get something much more perspicuous and explicit in exchange.
All of this also applies to the default declaration(s) which you can supply for an associated type (see in the link above), only it's a bit more complicated and indirect.
My solution
I propose to simplify substantially, as follows:

*         The "shared arguments" of an associated type are the argument positions that mention a type variable from the class header.  So in class C above, the first argument position of T is "shared"; and in class D, the second argument position of S is shared.

*         A instance for an associated type (in a class instance declaration) cf must have

o   type variables in the non-shared argument positions, and

o   an exact copy of the corresponding instance header type in the shared positions

*         For each associated type you can have

o   at most one default declaration in the class declaration

o   at most one type instance declaration in the class instance declaration

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <>

More information about the ghc-devs mailing list