Deriving clause for arbitrary classes
Currently, you can use a deriving
clause, or standalone-deriving declaration, only for
- a built-in class like
Eq
orShow
, for which GHC knows how to generate the instance code - a newtype, via the "newtype-deriving" mechanism.
However, Pedros's new generic-default mechanism means that it makes perfect sense to write this:
data T a = ...blah..blah... deriving( Generic )
instance C a => C (T a) -- No 'where' clause
where C is some random user-defined class. Usually, an instance decl with no 'where' clause would be pretty useless, but now that we have default method signatures, in conjunction with deriving( Generic )
, the instance can be useful.
That in turn leads to a desire to say
data T a = ...blah..blah... deriving( Generic, C )
which is even more compact. Is the extra compactness worth it? Presumably we'd only want to do this for some specified classes C, so we'd need some way to say this at C's declaration site. Something like:
class C where
op :: a -> a
default op :: blah => a -> a
op = <code for the default method>
{-# DERIVABLE C #-} -- This says you can say
-- data T = ... deriving( C )
I'm a bit dubious about whether the payoff justifies the cost, but I'm recording the idea in this ticket.
Trac metadata
Trac field | Value |
---|---|
Version | 7.2.1 |
Type | FeatureRequest |
TypeOfFailure | OtherFailure |
Priority | normal |
Resolution | Unresolved |
Component | Compiler |
Test case | |
Differential revisions | |
BlockedBy | |
Related | |
Blocking | |
CC | wadler@inf.ed.ac.uk |
Operating system | |
Architecture |