Generic programming library for generalised deriving.
This package offers a version of GHC.Generics with two important improvements:
The
to,from,to1, andfrom1methods have multiplicity-polymorphic types, allowing them to be used with either traditional Haskell code or linearly typed code.The representations used for
Generic1are modified slightly. As a result,to1andfrom1never need to usefmap. This can greatly improve performance, and it is necessary to support multiplicity polymorphism. A smaller change, approximately as proposed by spl, reduces the number of instances that must be written to actually useGeneric1for deriving instances of other classes.
For more details, see the Generics.Linear documentation.
The implementation is based on the generic-deriving package, first described in the paper
A generic deriving mechanism for Haskell. Jose Pedro Magalhaes, Atze Dijkstra, Johan Jeuring, and Andres Loeh. Haskell'10.
This library is organized as follows:
Generics.Lineardefines the core functionality for generics, including the multiplicity-polymorphicGeneric(1)classes and a replacement for the:.:composition type.Generics.Linear.THimplements Template Haskell functionality for deriving instances ofGeneric(1).Generics.Linear.Unsafe.ViaGHCGenericsoffersDerivingViatargets to deriveGenericandGeneric1instances fromGHC.Generics.GHC.Generics.Generic. Because these instances necessarily use unsafe coercions, their use will likely inhibit full optimization of code using them.
Educational code: the educational modules exported by generic-deriving have been copied into the tests/Generic/Deriving directory in this repository, with the very few modifications required to accommodate the differences between the Generic1 representations here and in base. All the same caveats apply as in the originals; see that package's README.
linear-generics: Generic programming library with linearity support
This package offers a version of GHC.Generics with two important improvements:
The
to,from,to1, andfrom1methods have multiplicity-polymorphic types, allowing them to be used with either traditional Haskell code or linearly typed code.The representations used for
Generic1are modified slightly.- Composition associates to the left in the generic representation. As a result,
to1andfrom1never need to usefmap. This can greatly improve performance, and it is necessary to support multiplicity polymorphism, as discussed here. - Generic representations no longer use
Rec1 f, they usePar1 :.: finstead, as proposed by spl. This way you no longer need to writeRec1instances for your derivers.
For more details, see the
Generics.Lineardocumentation.- Composition associates to the left in the generic representation. As a result,
This library is organized as follows:
Generics.Lineardefines the core functionality for generics. This includes:- multiplicity polymorphic
GenericandGeneric1classes, - a replacement for the
:.:composition type, and - an
MP1type for nonlinear and multiplicity polymorphic fields.
- multiplicity polymorphic
Generics.Linear.THimplements Template Haskell functionality for deriving instances ofGeneric(1).Generics.Linear.Unsafe.ViaGHCGenericsoffersDerivingViatargets to derive bothGenericandGeneric1instances fromGHC.Generics.Generic. Because these instances necessarily use unsafe coercions, their use will likely inhibit full optimization of code using them (see this wiki page for more on the GHC internals, along with commentary inUnsafe.Coerce).
Educational code: the educational modules exported by generic-deriving have been copied into the tests/Generic/Deriving directory in this repository, with the very few modifications required to accommodate the differences between the Generic1 representations here and in base. All the same caveats apply as in the originals; see that package's README.