I'm trying to learn GHC Generics. After reviewing several examples, I wanted to try to create a generic Functor
instances (disregarding that GHC can derive them automatically for me). However, I realized I have no idea how to work with a parametrized data types with Generics, all the examples I've seen were of kind *
. Is this possible, and if yes, how? (I'm also interested in other similar frameworks, such as SYB.)
How to construct generic Functor instances using GHC.Generics (or other similar frameworks)?
503 views Asked by Petr At
2
There are 2 answers
5
On
There is a Generic1
class for data types of kind * -> *
. Working with it is mostly the same as with data types of kind *
, except there's also Par1
for the parameter. I've used it in my unfoldable package for example.
The best place to look for lots of example functions using GHC Generics is the
generic-deriving
package. There's a generic definition of theFunctor
class in there. Copying (slightly simplified) fromGenerics.Deriving.Functor
:To use this on a datatype, you have to derive
Generic1
rather thanGeneric
. The key difference of theGeneric1
representation is that it makes use of thePar1
datatype that encodes parameter positions.