[Haskell-cafe] Poor man's generic programming

Henning Thielemann schlepptop at henning-thielemann.de
Sat Jan 16 14:51:42 EST 2010


Is any of the existing Generics packages able to work without compiler 
extensions, that is Haskell 98?
I mean, it is ok if the developer of the generic parts of a library 
needs compiler extensions or extra tools, but the user who calls 'cabal 
install' shall not need them and the developer, who does not touch 
generic definitions, should not need them as well.

I think this could be done with pragmas in the following way:

class C a where
  f :: a -> [Int]
  {-# GENERICDEFAULT
  f {| Unit |} Unit = []
  #-}

{-# GENERATEDINSTANCEBEGIN Automatically generated - Do not edit! #-}
instance C () where
  f () = []
{-# GENERATEDINSTANCEEND Automatically generated - Do not edit! #-}

Now, by running a tool, the part between the GENERATEDINSTANCE pragmas 
can generated or updated.
Everything outside the pragmas can be Haskell 98.

It does not only avoid extensions of the building compiler, but also 
extensions of a programmer's brain. Because for programmers it is often 
easier to understand a set of instances and see the common patterns, 
than to understand generic code that builds the instances.



More information about the Haskell-Cafe mailing list