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)?
512 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-derivingpackage. There's a generic definition of theFunctorclass in there. Copying (slightly simplified) fromGenerics.Deriving.Functor:To use this on a datatype, you have to derive
Generic1rather thanGeneric. The key difference of theGeneric1representation is that it makes use of thePar1datatype that encodes parameter positions.