Introduction
Template Haskell (TH) is an extension of GHC that allows the user to do type-safe compile-time meta-programming in Haskell. The idea behind template haskell comes from the paper “Template Meta-programming for Haskell” by S.P. Jones and T. Sheard. Template Haskell was shipped with GHC version 6.0. The compiler extension has evolved a lot since 2003 and its current state is well described at the GHC: User Manual and template-haskell package.
Initially, TH offered the ability to generate code at compile time and allowed the programmer to manipulate the abstract syntax tree (AST) of the program. The addition of new capabilities such as lifting, TExp , runIO, and quasiquoting opened a bunch of new use cases to explore.
In this blog post, we are going explore a bunch of interesting Template Haskell’s use cases:
- Type Class Derivation
- N-ary Function Generation
- Compile-time Static Input Validation
- Arbitrary IO at Compile Time
This article assumes some familiarity with Haskell and, in particular, with Template Haskell. There are many well-written tutorials on the internet such as A Practical Template Haskell Tutorial or Template Haskell Tutorial. We strongly recommend reading the previously mentioned tutorials before continuing with the reading.
Use Cases
Before we start, these are the list of language extensions and imports that we are going to use in the following sections:
1 |
|
1 |
|
Ex 1: Type Class Derivation
Automatic derivation of type class instances is one of many problems that TH can solve. Although this problem can also be solved by generics, the compilation times are usually longer. For this reason, template haskell is still the preferred way to generate type class instances at compile time.
Here we present an example of how to derive the type class Foldable for arbitrary datatypes.
1 |
|
The implementation is as follows:
1 |
|
Ex 2: N-ary Function Generation
Have you ever written a function like snd3 :: (a, b, c) -> b
or snd4 :: (a, b, c, d) -> b
? Then, you can do better by letting the compiler write those boilerplate and error-prone functions for you.
Here we present an example of how to write an arbitrary-sized zipWith.
We have chosen zipWith
since it is a fairly simple function but complex enough to be used as the base to write more complex functions.
Here is a first implementation of zipWithN
:
1 |
|
This implementation works fine but the compiler will warn
you about a missing signature on a top-level definition.
In order to fix this, we need to add a type signature to the generated term:
1 |
|
Now you can use zipWithN
to generate arbitrary-sized zipWith
functions:
1 |
|
Manually generating each instance partially defeats the purpose of zipWithN
.
In order to address this issue, we need the auxiliary function genZipWith
. Notice, we will use an alternative simplified definition of zipWithN
that exploits slicing and lifting to showcase.
1 |
|
Now, we can generate arbitrary sized zipWith
functions
1 |
|
which will produce zipWith1
, zipWith2
, …, zipWith19
, zipWith20
.
Ex 3: Compile-time Input Validation
Static input data is expected to be “correct” on a strongly typed programming language.
For example, assigning the decimal number 256
to a variable of type Byte
is expected to fail at compile time.
So, let’s try it on Haskell:
1 |
|
Ops! Indeed, the code has compiled with an unexpected overflow.
The keen-eyed reader may be thinking that this bug could have been prevented with the appropriate GHC flags -Wall
and -Werror
.
Here we present a more general approach to validate static input data from the user using quasiquoting. This example can be easily adapted to all sorts of input data.
1 |
|
The word
quasiquoter can validate static input data
1 |
|
and emit a compilation-time error if the data is not valid
1 |
|
Ex 4: Arbitrary IO at Compile Time
Running arbitrary IO on compile-time is one of the features of TH. This allows the user to make compilation dependant on external conditions such as the database schema, the current git branch or a local file content.
In this last example, we are going to use runIO
and quasiquoting
to get static pictures from remote and local files at compile-time. Notice, this example has been simplified to avoid all the overhead of proper error handling.
- Try to parse the input string as an url
- If it succeeds, request the content of the url as a bytestring
- Otherwise, interpret the input string as a file path and read its content.
- Decode the contents as a DynamicImage
- Lift the
DynamicImage
1 |
|
The img
quasiquoter can be used to load pictures as static data inside your binary
1 |
|
Conclusion
During this blog post, we have seem some of the use cases of template haskell and how to implement them. We encourage the reader to use our examples to build new and more compelling use cases of template haskell and to share them with the community.
We hope you enjoyed this post and don’t forget to share your own use cases for template haskell in the comments.