I've currently got code following this pattern:
f1' x1 x2 .. xm1 = ...
f2' x1 x2 .. xm2 = ...
.
.
,
fn' x1 x2 .. xmn = ...
instance C D1 where
f1 = f1'
f2 = f2'
.
.
.
fn = fn'
instance C D2 where
f1 = f1'
f2 = f2'
.
.
.
fn = fn'
.
.
.
instance C DN where
f1 = f1'
f2 = f2'
.
.
.
fn = fn'
Basically, I've got a class that I want to implement for a number of datatypes, but all these implementations are the same. But there's a lot of repeated code with the instance implementations, for m
functions and n
instances, I have to write roughly O(m*n)
lines of code, I'd prefer it to be more like O(m+n)
.
Is there a good way to remove the repetition here? I imagine Template Haskell would do the trick, but I don't want to reinvent the wheel if something to handle this already exists.