Hubbry Logo
search button
Sign in
Template Haskell
Template Haskell
Comunity Hub
History
arrow-down
starMore
arrow-down
bob

Bob

Have a question related to this hub?

bob

Alice

Got something to say related to this hub?
Share it here.

#general is a chat channel to discuss anything related to the hub.
Hubbry Logo
search button
Sign in
Template Haskell
Community hub for the Wikipedia article
logoWikipedian hub
Welcome to the community hub built on top of the Template Haskell Wikipedia article. Here, you can discuss, collect, and organize anything related to Template Haskell. The purpose of the hub is to connect...
Add your contribution
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.[1]

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:

 digitsFollowedByLetters = [$re| \d+ \s+ |]

Example

[edit]

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:

 result = $( transform [| input |] )

References

[edit]
  1. ^ Sheard, Tim; Jones, Simon Peyton (3 October 2002). "Template meta-programming for Haskell". Proceedings of the 2002 ACM SIGPLAN workshop on Haskell. Association for Computing Machinery. pp. 1–16. doi:10.1145/581690.581691. ISBN 1581136056. S2CID 6096655.
[edit]