World Library  
Flag as Inappropriate
Email this Article


Article Id: WHEBN0000393460
Reproduction Date:

Title: Metaprogramming  
Author: World Heritage Encyclopedia
Language: English
Subject: Metacompiler, Ruby (programming language), Domain-specific language, Homoiconicity, Nemerle
Collection: Metaprogramming, Programming Paradigms
Publisher: World Heritage Encyclopedia


Metaprogramming is the writing of computer programs with the ability to treat programs as their data. It means that a program could be designed to read, generate, analyse or transform other programs, and even modify itself while running.[1][2] In some cases, this allows programmers to minimize the number of lines of code to express a solution (hence reducing development time), or it gives programs greater flexibility to efficiently handle new situations without recompilation.

The language in which the metaprogram is written is called the metalanguage. The language of the programs that are manipulated is called the object language. The ability of a programming language to be its own metalanguage is called reflection or reflexivity.

Reflection is a valuable language feature to facilitate metaprogramming. Having the programming language itself as a first-class data type (as in Lisp, Prolog, SNOBOL, or Rebol) is also very useful; this is known as homoiconicity. Generic programming invokes a metaprogramming facility within a language, in those languages supporting it.

Metaprogramming usually works in one of three ways. The first way is to expose the internals of the run-time engine to the programming code through application programming interfaces (APIs). The second approach is dynamic execution of expressions that contain programming commands, often composed from strings, but can also be from other methods using arguments or context.[3] Thus, "programs can write programs." Although both approaches can be used in the same language, most languages tend to lean toward one or the other.

The third way is to step outside the language entirely. General purpose program transformation systems such as compilers, which accept language descriptions and can carry out arbitrary transformations on those languages, are direct implementations of general metaprogramming. This allows metaprogramming to be applied to virtually any target language without regard to whether that target language has any metaprogramming abilities of its own.


  • Approaches 1
    • In statically typed functional languages 1.1
    • Template metaprogramming 1.2
    • Staged meta-programming 1.3
    • Macro systems 1.4
      • IBM/360 assembler 1.4.1
  • Examples 2
  • Implementations 3
  • See also 4
  • Notes 5
  • External links 6


In statically typed functional languages

  • Usage of dependent types allows proving that generated code is never invalid.[4]

Template metaprogramming

Staged meta-programming

Macro systems

IBM/360 assembler

The IBM/360 and derivatives had powerful assembler macro facilities that were often used to generate complete programs or sections of programs (for different operating systems for instance). Macros provided with CICS transaction processing system had assembler macros that generated COBOL statements as a pre-processing step.


A simple example of a metaprogram is this POSIX Shell script, which is an example of generative programming:

# metaprogram
echo '#!/bin/sh' >program
for I in $(seq 992)
        echo "echo $I" >> program
chmod +x program

This script (or program) generates a new 993-line program that prints out the numbers 1–992. This is only an illustration of how to use code to write more code; it is not the most efficient way to print out a list of numbers. Nonetheless, a programmer can write and execute this metaprogram in less than a minute, and will have generated exactly 1000 lines of code in that amount of time.

A quine is a special kind of metaprogram that produces its own source code as its output.

Not all metaprogramming involves generative programming. If programs are modifiable at runtime or if incremental compilation is available (such as in C#, Forth, Frink, Groovy, JavaScript, Lisp, Lua, Perl, PHP, Python, REBOL, Ruby, Smalltalk, and Tcl), then techniques can be used to perform metaprogramming without actually generating source code.

Lisp is probably the quintessential language with metaprogramming facilities, both because of its historical precedence and because of the simplicity and power of its metaprogramming. In Lisp metaprogramming, the unquote operator (typically a comma) introduces code that is evaluated at program definition time rather than at run time; see Self-evaluating forms and quoting in Lisp. The metaprogramming language is thus identical to the host programming language, and existing Lisp routines can be directly reused for metaprogramming, if desired.

This approach has been implemented in other languages by incorporating an interpreter in the program, which works directly with the program’s data. There are implementations of this kind for some common high-level languages, such as RemObjectsPascal Script for Object Pascal.

One style of metaprogramming is to employ domain-specific languages (DSLs). A fairly common example of using DSLs involves generative metaprogramming: lex and yacc, two tools used to generate lexical analyzers and parsers, let the user describe the language using regular expressions and context-free grammars, and embed the complex algorithms required to efficiently parse the language.


See also


  1. ^ Course on Program Analysis and Transformation. By Prof. Harald Sondergaard."Course on Program Analysis and Transformation". Retrieved 18 September 2014. 
  2. ^  
  3. ^ for example, instance_eval in  
  4. ^ Chlipala, Adam (June 2010). "Ur: statically-typed metaprogramming with type-level record computation" (PDF). ACM SIGPLAN Notices. PLDI '10 45 (6): 122–133.  

External links

  • Wiki: Metaprogramming article
  • Meta Programming on the Program Transformation Wiki
  • Code generation Vs Metaprogramming
  • "Solenoid": The first metaprogramming framework for eXist-db
  • The Art of Enterprise Metaprogramming
This article was sourced from Creative Commons Attribution-ShareAlike License; additional terms may apply. World Heritage Encyclopedia content is assembled from numerous content providers, Open Access Publishing, and in compliance with The Fair Access to Science and Technology Research Act (FASTR), Wikimedia Foundation, Inc., Public Library of Science, The Encyclopedia of Life, Open Book Publishers (OBP), PubMed, U.S. National Library of Medicine, National Center for Biotechnology Information, U.S. National Library of Medicine, National Institutes of Health (NIH), U.S. Department of Health & Human Services, and, which sources content from all federal, state, local, tribal, and territorial government publication portals (.gov, .mil, .edu). Funding for and content contributors is made possible from the U.S. Congress, E-Government Act of 2002.
Crowd sourced content that is contributed to World Heritage Encyclopedia is peer reviewed and edited by our editorial staff to ensure quality scholarly research articles.
By using this site, you agree to the Terms of Use and Privacy Policy. World Heritage Encyclopedia™ is a registered trademark of the World Public Library Association, a non-profit organization.

Copyright © World Library Foundation. All rights reserved. eBooks from Project Gutenberg are sponsored by the World Library Foundation,
a 501c(4) Member's Support Non-Profit Organization, and is NOT affiliated with any governmental agency or department.