12 Metaprogramming in Haskell

 

This chapter covers

  • Haskell features to support deriving type class instances automatically
  • Exploiting a generic representation for any data type to write data-type-generic code
  • Generating code at compile time with Template Haskell

When we write code in Haskell, we call it programming. When we write code that generates or processes other code on our behalf, we call it metaprogramming, or generative programming. With metaprogramming techniques, we usually automate repetitive boilerplate code, which is too dull to write manually. This leads to boosting developer productivity. Sometimes, there is a price to pay—such code can be quite hard to write and maintain.

12.1 Deriving instances

12.1.1 Basic deriving strategies

12.1.2 The problem of type safety and generalized newtype deriving

12.1.3 Deriving by an example with DerivingVia

12.2 Data-type-generic programming

12.2.1 Generic data-type representation

12.2.2 Example: Generating SQL queries

12.3 Template Haskell and quasiquotes

12.3.1 A tutorial on Template Haskell

12.3.2 Example: Generating remote function calls

Summary

sitemap