[Haskell-cafe] Selective deriving

Rui Azevedo ruihfazevedo at gmail.com
Mon Feb 20 04:51:16 UTC 2017


I don't know if there is some extension that already does this, or any 
other method of doing it.
My search on deriving ghc mechanisms and extensions gave me no 
satisfactory results.

the point is to have some "help" on derivation or some sort of derivation.
actual derivation schema seems to require that all members (constructor 
arguments) instantiate the deriving class. This is the way of doing it 
for classes like Eq, works for Show and Ord but eventually it is a very 
strong requirement even for Eq sometimes. Of course I can do a manual 
derivation to state something else, and that sometimes can be 
frustrating because the derivations are way trivial and susceptible of 

There is no fancy things added, its all standard haskell, user could 
laboriously type this.

As an alternate derivation mechanism i seek selective deriving, like this:

     class Named o where name::o->String

     data A = A String deriving (Show)
     instance Named A where name="Object"

till now just normal derivation

     data B = B A Int deriving (Show) extending (Named)

the mechanism on extending requires that one and only one of the type 
constructor's direct arguments instantiates the class.
this avoids the C++ diamond inheritance problem, user can recur on 
manual derivation.

this will generate the code:

     instance Named B where name (B a _)=name a

alternative can be: extending (Named A,...)

this latter form is rater interesting as it cuts down the implementation 
and still avoid the diamond problem even if multiple members instantiate 
the same class

Are already any extension doing this?
Can i achieve the same result by other forms?
What is the best way to implement this thing? plug-ins seem not to be 
able to change syntax (right?)
I've also looked at -F -pgmF options to use a preprocessor... can a 
preprocessor insert code to reference the original line numbers, just 
because messing with line numbers will make this unusable.


More information about the Haskell-Cafe mailing list