12 Metaprogramming in Haskell

 

This chapter covers:

  • Haskell features to support deriving type classes instances automatically
  • Exploiting a generic representation for any datatype to write a datatype-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 a 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  Datatype generic programming

12.2.1  Generic datatype 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 functions calls

12.4  Summary

sitemap