|
|
|
# Derived Instances
|
|
|
|
|
|
|
|
## Minor adjustments
|
|
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
```wiki
|
|
|
|
minBound = K1 minBound ... minBound
|
|
|
|
maxBound = Kn maxBound ... maxBound
|
|
|
|
```
|
|
|
|
|
|
|
|
## Automatic derivation
|
|
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
```wiki
|
|
|
|
data Rose f a = Fork a (f (Rose f a)) deriving Show
|
|
|
|
```
|
|
|
|
|
|
|
|
as 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](newtype-deriving)).
|
|
|
|
|
|
|
|
## User-specified derived instances
|
|
|
|
|
|
|
|
|
|
|
|
There are various extant proposals to enable the user-specification of the derivation of class instances:
|
|
|
|
|
|
|
|
- PolyP ([ Jeuring and Jansson](http://www.cs.uu.nl/~johanj/publications/notes.ps), AFP 1996)
|
|
|
|
- ghc -fgenerics (Hinze, [ PoPL 2000](http://www.informatik.uni-bonn.de/~ralf/publications/POPL00.ps.gz), [ Haskell Workshop 1999](http://www.informatik.uni-bonn.de/~ralf/publications/HW99.ps.gz), [ Haskell Workshop 2000](http://www.informatik.uni-bonn.de/~ralf/publications/HW00.ps.gz))
|
|
|
|
- Lightweight Generics ([ Cheney and Hinze](http://www.informatik.uni-bonn.de/~ralf/publications/HW2002.pdf), Haskell Workshop 2002)
|
|
|
|
- Scrap your Boilerplate ([ Lämmel and Peyton Jones](http://www.cs.vu.nl/boilerplate/), TLDI 2003 etc)
|
|
|
|
- Generics for the Masses ([ Hinze](http://www.informatik.uni-bonn.de/~ralf/publications/ICFP04.pdf), ICFP 2004)
|
|
|
|
- Type-indexed datatypes ([ Hinze, Jeuring, Löh](http://www.cs.uu.nl/~johanj/publications/tidata.pdf), SoCP 2004)
|
|
|
|
- TemplateHaskell? is capable of this amongst other things
|
|
|
|
- external tools: [ DrIFT](http://repetae.net/~john/computer/haskell/DrIFT/), [ Strafunski](http://www.cs.vu.nl/Strafunski/)
|
|
|
|
- [ Generic Haskell](http://www.generic-haskell.org/) preprocessor
|
|
|
|
- fill out this list with some more…?
|
|
|
|
|
|
|
|
## Standalone derived instances
|
|
|
|
|
|
|
|
- Even in the absence of user-defined deriving, it should be easy for a 'newtype' decl 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.
|
|
|
|
|
|
|
|
The development version of GHC adds a top-level declaration on the form:
|
|
|
|
|
|
|
|
```wiki
|
|
|
|
'deriving' qtycls 'for' qtycon
|
|
|
|
```
|
|
|
|
There are various extant proposals to enable the user-specification of the derivation of class instances.
|
|
|
|
|
|
|
|
|
|
|
|
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](http://haskell.org/haskellwiki/GHC/StandAloneDeriving) on the GHC Wiki. |
|
|
|
- fill out a list |