Search code examples
haskellghcscrap-your-boilerplateghc-generics

Convert from type `T a` to `T b` without boilerplate


So, I have an AST data type with a large number of cases, which is parameterized by an "annotation" type

data Expr a = Plus a Int Int
    | ...
    | Times a Int Int

I have annotation types S and T, and some function f :: S -> T. I want to take an Expr S and convert it to an Expr T using my conversion f on each S which occurs within an Expr value.

Is there a way to do this using SYB or generics and avoid having to pattern match on every case? It seems like the type of thing that this is suited for. I just am not familiar enough with SYB to know the specific way to do it.


Solution

  • It sounds like you want a Functor instance. This can be automatically derived by GHC using the DeriveFunctor extension.