context Num a = (Monoid a, Group a, Multiplicative a, FromInteger a)
Now, what this means is that when you write
Num a in a constraint, you really mean all of
Group a and so on. This means that the following program is valid, and presumably computes the number 7:
= fromInteger 2 `mappend` fromInteger 5foo
This lets you write shorter type signatures in programs which make ubiquitous use of type classes. However, in the brave new world of type families an obvious generalisation is to allow class-associated constraints. In particular, this lets us solve the classic problem where you can't make Set an instance of Monad:
class RMonad m where context RMonadElem a instance RMonad  where context RMonadElem a = () return x = [x] (>>=) = concatMap instance RMonad Set where context RMonadElem a = Ord a return x = singleton x (>>=) = fold (\a s' -> union (f a) s') empty s
A few interesting points:
- What is the kind signature of the context synonym? We probably need another "kind" - that of class constraints - which is preserved by n-ary tupling.
- Can you provide a default implementation for the kind synonym? This would let us change the definition of the
Monadtype class in a backward compatible way, by defaulting
- I mentioned this idea to Ganesh at Fun In The Afternoon, and he told me about his rmonad package, which essentially does exactly this, but by reifying the dictionaries explicitly as data. This is a nice demonstration that the approach is workable, but I think we could really do without the boilerplate dictionary management.
- Amusingly, GHC currently represents type classes internall as a normal data type, with some extra invariants. This means that most of the existing machinery for dealing with associated type synonyms could probably be used changed to implement this extension!
I don't think that this introduces any horrendous type checking problems, and I can see how the desugarer has to treat dictionaries arising from such contexts. Nonetheless, there are probably some weirdnesses that I'm forgetting, so I should probably try to come up with a real specification (and implementation!) when I get some time...
(P.S: It looks like some guys at Hac5 were working on adding the simple constraint families to GHC - does anyone know how far they got with that?)