This article has multiple issues. Please helpimprove it or discuss these issues on thetalk page.(Learn how and when to remove these messages) (Learn how and when to remove this message)
|
Template Haskell (Template Meta-Haskell for early versions) is an experimental language extension to thefunctionalprogramming languageHaskell, implemented in theGlasgow Haskell Compiler (GHC) version 6 and later.[1]
It allowscompile timemetaprogramming andgenerative programming by means of manipulatingabstract syntax trees and 'splicing' results back into a program. The abstract syntax is represented using ordinary Haskelldata types and the manipulations are performed using ordinary Haskellfunctions.
'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 forregular expressions could look like this:
digitsFollowedByLetters=[$re|\d+\s+|]
A common idiom is to quasi-quote anexpression, perform some transformation on the expression and splice the result back into the program. It could be written as:
result=$(transform[|input|])