ScholarGate
Асистент

Metaprogramming and Macros

Metaprogramming treats programs as data that other programs can generate, inspect, or transform, with macros providing controlled syntactic abstraction at compile time.

Намерете тема с PaperMindСкороFind papers & topics
Tools & resources
Изтегляне на слайдове
Learn & explore
ВидеоСкоро

Definition

Metaprogramming is the writing of programs that produce, analyze, or transform other programs (or themselves); macros are a metaprogramming facility that rewrites source syntax into expanded code before or during compilation.

Scope

This topic covers techniques by which programs manipulate programs: lexical and syntactic macros, hygienic macro expansion, compile-time code generation, multi-stage programming, reflection, and template systems. It addresses how these mechanisms extend a language's syntax and abstractions while preserving correctness, especially with respect to variable capture and binding.

Core questions

  • How can syntactic abstraction extend a language without unsafe name capture?
  • What distinguishes compile-time metaprogramming from runtime reflection?
  • How does multi-stage programming separate program generation from execution?
  • When does metaprogramming improve abstraction versus harm readability and tooling?

Key theories

Hygienic macro expansion
Kohlbecker and colleagues introduced hygienic macros, which automatically rename bound variables during expansion so that macro-generated code cannot accidentally capture or be captured by names in the use site.
Multi-stage programming
Taha and Sheard formalized staged computation with explicit annotations, giving a typed account of generating and running code across distinct evaluation stages.
Template metaprogramming
Sheard and Peyton Jones added typed compile-time metaprogramming to Haskell, allowing programs to build and splice abstract syntax at compile time.

Clinical relevance

Macros and metaprogramming let library authors create domain-specific abstractions, eliminate boilerplate, and perform compile-time specialization for performance. Hygiene and staging discipline are what make these powerful facilities safe enough for everyday use.

History

Macros date to early Lisp and assembly, but unhygienic Lisp macros were prone to accidental variable capture. Kohlbecker's 1986 hygienic expansion solved this, influencing Scheme's syntax-rules. Subsequent work formalized multi-stage programming (MetaML) and brought typed compile-time metaprogramming to languages such as Haskell, Scala, and Rust.

Debates

Power versus comprehensibility of macros
Language designers weigh the expressive power of unrestricted syntactic abstraction against the difficulty it creates for readers, debuggers, and tooling, motivating hygiene and disciplined macro systems.

Key figures

  • Eugene Kohlbecker
  • Matthias Felleisen
  • Walid Taha
  • Tim Sheard
  • Simon Peyton Jones

Related topics

Seminal works

  • kohlbecker1986
  • taha2000
  • sheard2002
  • abelson1996

Frequently asked questions

What makes a macro system hygienic?
A hygienic macro system guarantees that identifiers introduced by a macro do not unintentionally capture or shadow identifiers at the call site, and vice versa, by systematically renaming bound variables during expansion.
How does metaprogramming differ from ordinary functions?
Ordinary functions operate on runtime values, whereas metaprogramming operates on program representations, generating or transforming code, often at compile time, before any runtime values exist.

Methods for this concept

Related concepts