liveBook · Manning

Template Haskell

As explained in Learn Haskell by Example

Template Haskell is a GHC language extension for compile-time metaprogramming and manipulation of Haskell abstract syntax.

Template Haskell is a GHC extension for compile-time metaprogramming: it manipulates Haskell abstract syntax and generates functions that would otherwise require manual construction. section 9.5.3

A practical testing pattern uses Template Haskell to:

  • Collect properties whose names use the prop_ suffix. section 9.5.3
  • Generate a test-running action with quickCheckAll. section 9.5.3
  • Enable the TemplateHaskell language extension and invoke generated code with the $ syntax. section 9.5.3

Generated QuickCheck suites

A suite defines prop_true and prop_false, then exposes runTests = $quickCheckAll. A separate main module runs several suites and fails when any generated check fails.

How Learn Haskell by Example covers it

How deep it goes
Explains AST manipulation and demonstrates generated test runners, but does not develop Template Haskell internals.
Examples
Uses prop_true, prop_false, prop_addPos, prop_multZero, two generated suites, and Stack test output.
What it assumes
Assumes basic Haskell modules, properties, QuickCheck, and command-line test execution; teaches the extension setup in context.
What you can do after
Apply it via QuickCheck to generate property runners and integrate their results into a command-line test process.
How it connects
Connects metaprogramming with GHC extensions, abstract syntax trees, QuickCheck, property naming, and test-suite exit codes.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Learn Haskell by Example. The section links point to the parts of the book it draws on.