|Version 9 (modified by ross@…, 9 years ago) (diff)|
Haskell 98 allows derived instances of Bounded only for enumerations and single constructor types. Proposal: allow derived instances of Bounded for all algebraic data types, with
minBound = K1 minBound ... minBound maxBound = Kn maxBound ... maxBound
Automatic derivation of instances is jolly useful. Current problems are:
- Automatic derivation is only available for certain classes defined in the Prelude and standard libraries.
- There is no way to specify how to derive a user-defined class in the general case.
- There is no way to derive an instance of a class for a data type that is defined elsewhere (in another module).
- There is no easy way to refer to the definitions that would have been derived when defining an explicit instance.
- Deriving fails for some Haskell-98 data types. Try
data Rose f a = Fork a (f (Rose f a)) deriving Showas an example.
- Even in the absence of user-defined deriving, it should be easy for a newtype declaration to inherit some of the instances of its parent type, no matter where the class was defined, but this is currently only available as a GHC extension to Haskell 98 (see NewtypeDeriving).
User-specified derived instances
There are various extant proposals to enable the user-specification of the derivation of class instances:
- PolyP (Jeuring and Jansson, AFP 1996)
- ghc -fgenerics (Hinze, PoPL 2000, Haskell Workshop 1999, Haskell Workshop 2000)
- Lightweight Generics (Cheney and Hinze, Haskell Workshop 2002)
- Scrap your Boilerplate (Lämmel and Peyton Jones, TLDI 2003 etc)
- Generics for the Masses (Hinze, ICFP 2004)
- Type-indexed datatypes (Hinze, Jeuring, Löh, SoCP 2004)
- TemplateHaskell? is capable of this amongst other things
- external tools: DrIFT, Strafunski
- Generic Haskell preprocessor
- fill out this list with some more…?
Standalone derived instances
The development version of GHC adds a top-level declaration on the form:
'deriving' qtycls 'for' qtycon
which produces the same instance as would be produced by a deriving clause in a data or newtype declaration, but allows this derivation to be decoupled from the datatype declaration, e.g. in another module. See Stand-alone deriving declarations on the GHC Wiki.