Template Haskell

Computer language extension


title: "Template Haskell" type: doc version: 1 created: 2026-02-28 author: "Wikipedia contributors" status: active scope: public tags: ["experimental-programming-languages", "haskell-programming-language-family", "functional-languages", "metaprogramming", "articles-with-example-haskell-code"] description: "Computer language extension" topic_path: "technology/programming-languages" source: "https://en.wikipedia.org/wiki/Template_Haskell" license: "CC BY-SA 4.0" wikipedia_page_id: 0 wikipedia_revision_id: 0

::summary Computer language extension ::

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 'splicing' 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 [| and |] are used to get the abstract syntax tree for the enclosed expression and 'splice' brackets $( and ) 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: ::code[lang=haskell] digitsFollowedByLetters = [$re| \d+ \s+ |] ::

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: ::code[lang=haskell] result = $( transform [| input |] ) ::

References

References

  1. (3 October 2002). "Proceedings of the 2002 ACM SIGPLAN workshop on Haskell". Association for Computing Machinery.

::callout[type=info title="Wikipedia Source"] This article was imported from Wikipedia and is available under the Creative Commons Attribution-ShareAlike 4.0 License. Content has been adapted to SurfDoc format. Original contributors can be found on the article history page. ::

experimental-programming-languageshaskell-programming-language-familyfunctional-languagesmetaprogrammingarticles-with-example-haskell-code