Template Haskell is a macro system for Haskell1, i.e. a way of writing Haskell code to generate Haskell code.
In some languages, like Lisp, macros provide a central means of generalization, i.e. a way to avoid writing many similar pieces of code and instead just write one.
In Haskell, however, macros are better thought of as a last resort option, usually for very advanced applications where the generalization capabilities of standard Haskell (polymorphism, typeclasses, generics) are insufficient.
As such, novice Haskellers should never consider writing their own macros, and only use them rarely, when offered by a library.
The typical use case of Template Haskell is in a library where it is useful to have a macro that automatically writes some boilerplate code.
For instance, the highlighted code is a Template Haskell macro to generate lenses for a newly defined type:
This will generate lenses, usable as follows:
Created: March 6, 2023