Skip to main content

Template Haskell tutorial

About 2 minHaskellcrashcoursehaskellparsecattoparsectrifecta

Template Haskell tutorial ๊ด€๋ จ


Template Haskell tutorial

Mark Karpov > Template Haskell tutorial

The tutorial aims to introduce the reader to Template Haskell (TH)โ€”the language extension that adds meta-programming capabilities to the Haskell language. Here I assume some familiarly with Haskell, perhaps beginner or intermediate level, although these terms are rather nebulous and subjective. To express the prerequisites in a more tangible form: if you know what a monad is, you should probably be OK.

TH has the reputation of being an expert-level topic that mere mortals are not prepared to comprehend. I donโ€™t think this is so. The ideas behind TH are simple and make sense, while specific details can be always looked up in the Haddocks.

The tutorial cannot possibly cover every use of TH, and so it is structured in such a way so we only get to see the most common, conventional, and benign uses of this GHC feature.


Motivation

One of the main difficulties with TH is perhaps deciding whether it is the best solution to a problem at hand. Writing code that generates code is generally considered an indication that the tools of expression provided by the language and/or programmerโ€™s imagination have failed to address a particular problem and meta-programming is used as a last resort to get things done. True or not, TH is quite popular and so knowing your way around it is a valuable skill that can be used to do things that often cannot be achieved otherwise.

Letโ€™s list some uses of TH:

  • Automatic deriving of type class instances is still perhaps the most common use case for TH. Even though the same problem can often be addressed by genericsopen in new window, they are known to make compilation times longer (compared to TH-based solutions), so TH is still the preferred method of automatic instance derivation in libraries like aeson and lens.
  • Creation of TH DSLs that are integrated into systems built in Haskell. Examples of such DLSs are the language for model declaration used in persistentopen in new window, and various other mini-languages used in the yesodopen in new window web framework.
  • Compile-time construction of values of refined types that turns invalid inputs into compilation failures.
  • Compile-time loading and processing of data from external files, which is very useful sometimes. Even though this involves running IO during compilation, itโ€™s a relatively innocent use case of that dangerous feature.

Reasons not to use TH:

TH helpers are often viewed as black boxes that do โ€œmagicโ€. It is not clear at all what a thing of the type Q [Dec] does, it might do anything (we will see that any code that generates declarations has the same Q [Dec] type, no matter what sort of declarations it generates). Documentation becomes the main source of information about semantics of TH code.

TH imposes restrictions on where the user should define TH functions themselves and sometimes also how to order definitions in files where TH functions are used.

์ด์ฐฌํฌ (MarkiiimarK)
Never Stop Learning.