Data package:generic-data

Generic combinators to derive type class instances.

Orphans

The Orphans module should be imported to derive the following classes using this library:

Minor discrepancies

Here are documented some corner cases of deriving, both by GHC and generic-data. They are all minor and unlikely to cause problems in practice.

Empty types

  • Some of the derived methods are lazy, which might result in errors being silenced, though unlikely.
  • The only generic-data implementation which differs from GHC stock instances is gfoldMap.
TODO: table

Single-constructor single-field types

data types with one constructor and one field are extremely rare. newtype is almost always more appropriate (for which there is no issue). That said, for data types both strict and lazy, all generic-data implementations are lazy (they don't even force the constructor), whereas GHC stock implementations, when they exist, are strict.

Functor composition

Fields of functors involving the composition of two or more functors f (g (h a)) result in some overhead using GHC.Generics.Generic1. This is due to a particular encoding choice of GHC.Generics, where composition are nested to the right instead of to the left. f (g (h _)) is represented by the functor f :.: (g :.: Rec1 h), so one must use fmap on f to convert that back to f (g (h _)). A better choice would have been to encode it as (Rec1 f :.: g) :.: h, because that is coercible back to f (g (h _)).
Generic representations as data types.

Warning

This is an internal module: it is not subject to any versioning policy, breaking changes can happen at any time. If something here seems useful, please report it or create a pull request to export it from an external module.
Synthetic data type. A wrapper to view a generic Rep as the datatype it's supposed to represent, without needing a declaration.
Synthetic data type. A wrapper to view a generic Rep as the datatype it's supposed to represent, without needing a declaration.
Deriving instances with GHC.Generics and related utilities Generic implementations of standard type classes. Operations on generic representations to help using GHC.Generics. See README.
Generic representations that contain datatype metadata.
Name of the module where the data type is defined (MetaData)
Name of the data type (MetaData).
True if the data type is a newtype (MetaData).
Name of the package where the data type is defined (MetaData)
Name of the first data constructor in a type as a string.
>>> gdatatypeName @(Maybe Int)
"Maybe"
Inverse of toData.
Conversion between a generic type and the synthetic type made using its representation. Inverse of fromData.
onData :: _ => (Data r x -> Data s y) -> (Data r x -> Data s y)  -- possible specialization
Can be used with generic-lens for type-changing field updates with field_ (and possibly other generic optics). A specialization of the identity function to be used to fix types of functions on Data, unifying the "spines" of input and output generic representations (the "spine" is everything except field types, which may thus change).
Returns True if the argument is a symbolic data constructor name (e.g., (:+:)). Returns False otherwise.