|Version 43 (modified by 5 years ago) (diff),|
Support for generic programming
GHC includes a new (in 2010) mechanism to let you write generic functions. It is described in paper A generic deriving mechanism for Haskell. This page sketches the specifics of the implementation; we assume you have read the paper. The HaskellWiki page gives a more general overview.
This mechanism replaces the previous generic classes implementation. What we describe until the "Kind polymorphic overhaul" section is implemented and released in GHC 7.0.1.
TcDeriv.tcDerivingnow allows deriving
- The representation types and core functionality of the library live on
- Many names have been added as known in
- Most of the code generation is handled by
Things that have been removed
- All of the generic classes stuff. In particular, the following have been removed:
-XGenericsflag is now deprecated.
What already works
Genericinstances can be derived when
defaultkeyword can used for generic default method signatures when
- Generic defaults are properly instantiated when giving an instance without defining the generic default method.
- Base types like
Maybe, tuples, come with Generic instances.
- Tests are available under the
genericsdirectory of the testsuite.
Kind polymorphic overhaul
The current implementation supports defining both functions over types of kind
* (such as
show) and functions over types of kind
* -> * (such as
fmap). Although care has been taken to reduce code duplication, we still need two generic classes, one for each kind (
With the new
-XPolyKinds functionality, we can make the support for generic programming better typed and more general. The basic idea is to define the universe codes (
:+:, etc.) as constructors of a datatype. Promotion then lifts these constructors to types, which we can use as before, only that now we have them all classified under a new kind:
data Universe x = U | K x | P Nat | Universe x :+: Universe x | Universe x :*: Universe x | M MetaData (Universe x) data MetaData = C | D | S