Template Haskell
{{Short description|Computer language extension}}
{{Multiple issues|
{{More citations needed|date=October 2021}}
{{Notability|date=December 2019}}
}}
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.{{cite book |last1=Sheard |first1=Tim |last2=Jones |first2=Simon Peyton |author2-link=Simon Peyton Jones |date=3 October 2002 |title=Proceedings of the 2002 ACM SIGPLAN workshop on Haskell |chapter=Template meta-programming for Haskell |pages=1–16 |doi=10.1145/581690.581691 |chapter-url=https://dl.acm.org/doi/10.1145/581690.581691 |publisher=Association for Computing Machinery |isbn=1581136056 |s2cid=6096655}}
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
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
{{Reflist}}
External links
- [http://www.haskell.org/haskellwiki/Template_Haskell Haskell.org Template Haskell Wiki page]
{{Haskell programming}}
Category:Experimental programming languages