my-server
← Wiki

Template Haskell

Template Haskell (Template Meta-Haskell for early versions) is an experimental language extension to the functional programming language Haskell, implemented in the Glasgow Haskell Compiler (GHC) version 6 and later.

It allows compile time metaprogramming and generative programming by means of manipulating abstract syntax trees and results back into a program. The abstract syntax is represented using ordinary Haskell data types and the manipulations are performed using ordinary Haskell functions.

'Quasi-quote' brackets <code>[|</code> and <code>|]</code> are used to get the abstract syntax tree for the enclosed expression and 'splice' brackets <code>$(</code> and <code>)</code> are used to convert from abstract syntax tree into code.

As of GHC-6.10, Template Haskell provides support for user-defined quasi-quoters, which allows users to write parsers which can generate Haskell code from an arbitrary syntax. This syntax is also enforced at compile time. For example, using a custom quasi-quoter for regular expressions could look like this:

Example

A common idiom is to quasi-quote an expression, perform some transformation on the expression and splice the result back into the program. It could be written as:

References

External links