![]() | This article is rated C-class on Wikipedia's
content assessment scale. It is of interest to the following WikiProjects: | ||||||||||||||||||||||||||||||||||||||||
|
If you understand functional programming with functions as fist-class data-types, you know that there is no difference between code and data (or algorithms and data structures). So there is no point in a concept/term like “metaprogramming”. Everything is data. Everything is code. Then code can naturally modify itself. And data is just a language of a specific syntax that other code can process. It’s the generalization of all that, that is functional programming.
“Metaprogramming” really is the word that those use, who were not introduced to full-scale functional programming, or haven’t understood it. :)
— 94.220.250.151 ( talk) 09:13, 23 November 2009 (UTC)
term = factor $(('*':MPY|'/':DIV) factor!2);
MPYfactor,factor] DIVfactor,factor] or simply a factor MPY DIV / \ / \ factor factor factor factor
Steamerandy ( talk) 18:44, 12 December 2014 (UTC)
If you need to get examples of metaprogramming, read up on Assembly and LISP programming.. Especially about the use of pointers and how instructions and data are represented in a CPU.. You may realize something profound about historical programming that makes metaprogramming look relatively common..
I just included the compiler metaprogramming example at the spur of the moment, probably someone else (or me) can do a much better job of it at a later time. --Anon
Metaprogramming is quite a subtle concept. Sometimes it depends on the intention of the programmer rather than the form of the program. For instance, writing a program like:
is not an example of metaprogramming when the program is fed to an interpreter. However it is, when the program is fed to a compiler.
Note also that other programming methods can be thought of as metaprogramming. For instance, the genetic programming method or the automatic generation of a DB front end viewer/editor utility from the data model. -- Derek Ross
I think I take issue with the statement that "This allows programmers to produce a larger amount of code and get more done in the same amount of time as they would take to write all the code manually." This falls into the same class as many other claims that ignore the fact that programming, done correctly, accumulates functionality exponentially, not linearly, with the amount of code written.
I'm think of claims that writing in (FORTRAN, C, 4GL, whatever) multiplies a programmer's productivity because "each line of FORTRAN compiles into ten lines of assembly language" or whatever (although, for some reason, the factor claimed is almost invariably ten). Yes, and once you've written a subroutine, the one-line subroutine call causes ten lines of code to be executed.
It seems to me that the relative advantages of writing subroutines (macros)(reusable objects) (whatever); versus inventing higher language levels (FORTRAN, Smalltalk, VB, whatever); versus various forms of code generation is very subtle and should not be thought of as a lines-of-code multiplier, but on the basis of whether the improved programming environment is a better fit to a) the programmer's cognitive model, and b) the problem domain.
I'd add that systems in which visible lines of code are produced are especially subject to marketing hype; I wish I could remember the ads a few years back that showed a page of greenbar paper with most of the lines highlighted in yellow, and the explanation "the yellow lines are the lines of code that XYZ wrote for you."
In most cases, it seems to me that the effect of most of these techniques is to give you a huge but constant additive head start, and a very small—probably too small to measure—productivity multiplier. -- Dpbsmith 18:15, 8 Jan 2004 (UTC)
Might be interesting to: show the example programme in
Might be interesting to: show a short example programme that does "something useful".
I have a good example of the metaprogramming using Lisp macros: [1]
It is possible to extend an article with this sort of examples (and it correlates with the idea of the compiler metaprogramming).
Here is a proposal definition.
This excludes cases such as
Thoughts? -- Dysprosia 09:34, 11 Mar 2005 (UTC)
In a number of famous algorithms, one may find that a "metaprogramming" technique is indeed used. For instance, consider the Knuth-Morris-Pratt algorithm. You will find that for any search string, a state machine is necessarily constructed to be run over input to find matching strings. In database work, to find the result set of a query in a reasonable amount of time, a query execution plan is generated. I often have to wonder what separates these techniques from the techniques mentioned here and if there is a true difference, or if its concept renaming.
Walid Taha's work (Metaml/Meta Ocaml) is certainly a unique variety, as you could say his programming model allows for (hopefully easy) meta programming, meta-meta programming, meta-meta-meta programming, etc. ad infinitum. But many others don't seem much different than embedding a compiler of some sort into the language and using it. Root4(one) 05:59, 26 November 2006 (UTC)
I prefer the more direct definition of
In this view the programmer who writes a compiler is engaged in meta-programming because the code they write -- the compiler -- manipulates the abstract syntax tree (AST) of some program and translates it into another data structure representing the assembler instruction sequence (for example) of the target program. (Compiler users are not engaged in meta-programming because the program they write and compile does not itself manipulate code as data.) -- Krisrose ( talk)
Steamerandy ( talk) 22:44, 12 December 2014 (UTC)
This is a good article, congratulations to all the authors!
One useful addition might be a history of meta-programming. I came here interested to learn when the idea first started to surface (the idea of human-oriented meta-programming that is: not the compiler itself). E.g., the C++ article states that templates were added some point after 1990 (and I'm sure C++ was not the first language to provide explicit support for meta-programming either). -- Jon Dowland 09:18, 8 September 2005 (UTC)
There ought to be some discussion of the Forth programming language which has native support for metaprogramming through CREATE and DOES> and other words. The functions used to build functions are generally made available to the user and it's straightforward to flag newly defined words to act as compile-time metaprograms rather than as words to be compiled. sigfpe Dec 5 2005
You should check out this page: http://www.flare.org . It's about a programming language specificly for metaprogramming.
It seems to me that PHP, ASP, JSP, HTML::Mason, and the like, usually referred to as "Web templating systems", are a form of metaprogramming. The programmer writes code in one language - PHP, VB, Java, Perl, and the like - that generates HTML, instead of writing the HTML directly. You may argue that HTML is not a programming language, just a markup language. Nevertheless, the process is largely the same. Also, these tools can also insert JavaScript into the HTML, and that is definitely a programming language.
Note that the file they create may start with the "high-level language" at the outermost level, and escape into the markup language, or vice-versa. These two approaches are essentially equivalent.
I'm going to think about adding something to the page that describes this common example.
I would like to suggest this article as a relevant external link. But, well, I'm the author of it, so I fell like it would be more appropriate for someone else to actually add it (if you find it relevant) LotLE× talk 06:53, 29 June 2006 (UTC)
Shouldn't computer viruses be included in the article, as they manipulate executables to copy themselves?-- Theodore Kloba ( talk) 21:24, 28 April 2008 (UTC)
Can "Qt's meta object compiler (moc)" be added as an example of meta programming? It generates more C++ code based on the contents of a .h file. Vdboor ( talk) 10:33, 17 September 2009 (UTC)
...or that do part of the work at compile time that would otherwise be done at runtime.
It makes more sense the other way around.
-- Sina ( talk) 13:10, 24 October 2010 (UTC)
There is a lack if defination of reflection or disagreement with the reflection wiki topic.
It simply states reflection is a programs ability to modify itself. However wiki reflection states it as the ability to modify itself at runtime.
These two topics should agree. Steamerandy ( talk) 16:59, 23 December 2014 (UTC)
Metacompiler have long been defined as taking a matalanguage program and translating it into a compiler.
metacompiler from the XDS 940 TREEMETA manual(1968):
Metacompiler definition as stated in CWIC (Compiler for Writing and Implementing Compilers) documents(1969-1985):
Now in the metaprogramming topic we have:
Now that fits a metacompiler written in it's own metalanguage compiling itself. It seams a special case of reflection equivalent to self-hosting compiler. Am I right in explaining a metacompiler compiling itself as:
I have been programming sense 1965 and written many commercial compilers. My computer lingo is a bit old school. Could someone here check out the metacompiler topic. I have redid it based on metaprogramming terminology (I hope).
I made an RFC because the original author defined metacompiler way to much like the FORTH definition of "Written in it's own language, compiling itself this way" making it a metacompiler. When historically some metacompilers have been written on other languages (assembly, LISP, CL/1 and PL/1) and of course did not compile themselves. When I last looked at FORTH it was not being called a metacompiler. -- Steamerandy ( talk) 22:29, 19 November 2014 (UTC)
The statement:
Is inconsistent with the defination of metalanguage.
Metaprogramming is programming that treats programs as data. Writing a compiler is metaprogramming. Compilers may be written in most any programming language. A metalanguage is used to describe other languages or talk about them. A compiler may be written in a metalanguage. But for example metaprogramming may be done in c++. Would then, c++ be a metalanguage?
Steamerandy ( talk) 02:41, 27 November 2014 (UTC)
...is quite poor. And it's consistent throughout the entire article, not just a few things here and there. Unfortunately, I don't know anything about the subject, so I'm just going to tag it and run. Deacon Vorbis ( talk) 01:57, 22 January 2017 (UTC)
The definition given in the introduction is very restrictive ("treating programs as data"). It implies that metaprogramming is possible only within homoiconic (therefore interpreted) programming languages. It then talks about C++ metaprogramming, which is purely static, creating an worrying level of inconsistency in the article. Vincent Lextrait ( talk) 01:17, 30 December 2017 (UTC)
R allows meta programming, it don't have macros and templates but it have substitute function that all to get expression being passed to function and prevent evaluation. I think you can write macro system in that language. In R everything is evaluated in reverse order (lazy evaluation) function get expressions as arguments and only when they are used, they get evaluated. That's why you can stop evaluation, modify the code and evaluate in your own way. This is powerful meta programming were you can define whole new syntax or new operators that are not supported by the language. (this is how purrr, dplyr etc. libraries define new syntax to process data).
jcubic ( talk) 14:23, 22 April 2020 (UTC)
Many script languages with run-time interpretation have the ability to produce a text string containing valid code in the same language and then interpret this string and execute it. Many of the examples mentioned belong to this category. Other categories are:
The article would be more clear if the sections were organized along such categories. Agnerf ( talk) 06:36, 15 July 2022 (UTC)
constexpr functions and "if constexpr" are used in C++ to explicitly specify that certain calculations and decisions are done at compile time.
An example of such compile-time calculations is the C++ Vector Class Library which is using compile-time calculations to find the optimal instruction sequence to implement a particular algorithm. [1] Agnerf ( talk) 06:36, 15 July 2022 (UTC)
References
![]() | This article is rated C-class on Wikipedia's
content assessment scale. It is of interest to the following WikiProjects: | ||||||||||||||||||||||||||||||||||||||||
|
If you understand functional programming with functions as fist-class data-types, you know that there is no difference between code and data (or algorithms and data structures). So there is no point in a concept/term like “metaprogramming”. Everything is data. Everything is code. Then code can naturally modify itself. And data is just a language of a specific syntax that other code can process. It’s the generalization of all that, that is functional programming.
“Metaprogramming” really is the word that those use, who were not introduced to full-scale functional programming, or haven’t understood it. :)
— 94.220.250.151 ( talk) 09:13, 23 November 2009 (UTC)
term = factor $(('*':MPY|'/':DIV) factor!2);
MPYfactor,factor] DIVfactor,factor] or simply a factor MPY DIV / \ / \ factor factor factor factor
Steamerandy ( talk) 18:44, 12 December 2014 (UTC)
If you need to get examples of metaprogramming, read up on Assembly and LISP programming.. Especially about the use of pointers and how instructions and data are represented in a CPU.. You may realize something profound about historical programming that makes metaprogramming look relatively common..
I just included the compiler metaprogramming example at the spur of the moment, probably someone else (or me) can do a much better job of it at a later time. --Anon
Metaprogramming is quite a subtle concept. Sometimes it depends on the intention of the programmer rather than the form of the program. For instance, writing a program like:
is not an example of metaprogramming when the program is fed to an interpreter. However it is, when the program is fed to a compiler.
Note also that other programming methods can be thought of as metaprogramming. For instance, the genetic programming method or the automatic generation of a DB front end viewer/editor utility from the data model. -- Derek Ross
I think I take issue with the statement that "This allows programmers to produce a larger amount of code and get more done in the same amount of time as they would take to write all the code manually." This falls into the same class as many other claims that ignore the fact that programming, done correctly, accumulates functionality exponentially, not linearly, with the amount of code written.
I'm think of claims that writing in (FORTRAN, C, 4GL, whatever) multiplies a programmer's productivity because "each line of FORTRAN compiles into ten lines of assembly language" or whatever (although, for some reason, the factor claimed is almost invariably ten). Yes, and once you've written a subroutine, the one-line subroutine call causes ten lines of code to be executed.
It seems to me that the relative advantages of writing subroutines (macros)(reusable objects) (whatever); versus inventing higher language levels (FORTRAN, Smalltalk, VB, whatever); versus various forms of code generation is very subtle and should not be thought of as a lines-of-code multiplier, but on the basis of whether the improved programming environment is a better fit to a) the programmer's cognitive model, and b) the problem domain.
I'd add that systems in which visible lines of code are produced are especially subject to marketing hype; I wish I could remember the ads a few years back that showed a page of greenbar paper with most of the lines highlighted in yellow, and the explanation "the yellow lines are the lines of code that XYZ wrote for you."
In most cases, it seems to me that the effect of most of these techniques is to give you a huge but constant additive head start, and a very small—probably too small to measure—productivity multiplier. -- Dpbsmith 18:15, 8 Jan 2004 (UTC)
Might be interesting to: show the example programme in
Might be interesting to: show a short example programme that does "something useful".
I have a good example of the metaprogramming using Lisp macros: [1]
It is possible to extend an article with this sort of examples (and it correlates with the idea of the compiler metaprogramming).
Here is a proposal definition.
This excludes cases such as
Thoughts? -- Dysprosia 09:34, 11 Mar 2005 (UTC)
In a number of famous algorithms, one may find that a "metaprogramming" technique is indeed used. For instance, consider the Knuth-Morris-Pratt algorithm. You will find that for any search string, a state machine is necessarily constructed to be run over input to find matching strings. In database work, to find the result set of a query in a reasonable amount of time, a query execution plan is generated. I often have to wonder what separates these techniques from the techniques mentioned here and if there is a true difference, or if its concept renaming.
Walid Taha's work (Metaml/Meta Ocaml) is certainly a unique variety, as you could say his programming model allows for (hopefully easy) meta programming, meta-meta programming, meta-meta-meta programming, etc. ad infinitum. But many others don't seem much different than embedding a compiler of some sort into the language and using it. Root4(one) 05:59, 26 November 2006 (UTC)
I prefer the more direct definition of
In this view the programmer who writes a compiler is engaged in meta-programming because the code they write -- the compiler -- manipulates the abstract syntax tree (AST) of some program and translates it into another data structure representing the assembler instruction sequence (for example) of the target program. (Compiler users are not engaged in meta-programming because the program they write and compile does not itself manipulate code as data.) -- Krisrose ( talk)
Steamerandy ( talk) 22:44, 12 December 2014 (UTC)
This is a good article, congratulations to all the authors!
One useful addition might be a history of meta-programming. I came here interested to learn when the idea first started to surface (the idea of human-oriented meta-programming that is: not the compiler itself). E.g., the C++ article states that templates were added some point after 1990 (and I'm sure C++ was not the first language to provide explicit support for meta-programming either). -- Jon Dowland 09:18, 8 September 2005 (UTC)
There ought to be some discussion of the Forth programming language which has native support for metaprogramming through CREATE and DOES> and other words. The functions used to build functions are generally made available to the user and it's straightforward to flag newly defined words to act as compile-time metaprograms rather than as words to be compiled. sigfpe Dec 5 2005
You should check out this page: http://www.flare.org . It's about a programming language specificly for metaprogramming.
It seems to me that PHP, ASP, JSP, HTML::Mason, and the like, usually referred to as "Web templating systems", are a form of metaprogramming. The programmer writes code in one language - PHP, VB, Java, Perl, and the like - that generates HTML, instead of writing the HTML directly. You may argue that HTML is not a programming language, just a markup language. Nevertheless, the process is largely the same. Also, these tools can also insert JavaScript into the HTML, and that is definitely a programming language.
Note that the file they create may start with the "high-level language" at the outermost level, and escape into the markup language, or vice-versa. These two approaches are essentially equivalent.
I'm going to think about adding something to the page that describes this common example.
I would like to suggest this article as a relevant external link. But, well, I'm the author of it, so I fell like it would be more appropriate for someone else to actually add it (if you find it relevant) LotLE× talk 06:53, 29 June 2006 (UTC)
Shouldn't computer viruses be included in the article, as they manipulate executables to copy themselves?-- Theodore Kloba ( talk) 21:24, 28 April 2008 (UTC)
Can "Qt's meta object compiler (moc)" be added as an example of meta programming? It generates more C++ code based on the contents of a .h file. Vdboor ( talk) 10:33, 17 September 2009 (UTC)
...or that do part of the work at compile time that would otherwise be done at runtime.
It makes more sense the other way around.
-- Sina ( talk) 13:10, 24 October 2010 (UTC)
There is a lack if defination of reflection or disagreement with the reflection wiki topic.
It simply states reflection is a programs ability to modify itself. However wiki reflection states it as the ability to modify itself at runtime.
These two topics should agree. Steamerandy ( talk) 16:59, 23 December 2014 (UTC)
Metacompiler have long been defined as taking a matalanguage program and translating it into a compiler.
metacompiler from the XDS 940 TREEMETA manual(1968):
Metacompiler definition as stated in CWIC (Compiler for Writing and Implementing Compilers) documents(1969-1985):
Now in the metaprogramming topic we have:
Now that fits a metacompiler written in it's own metalanguage compiling itself. It seams a special case of reflection equivalent to self-hosting compiler. Am I right in explaining a metacompiler compiling itself as:
I have been programming sense 1965 and written many commercial compilers. My computer lingo is a bit old school. Could someone here check out the metacompiler topic. I have redid it based on metaprogramming terminology (I hope).
I made an RFC because the original author defined metacompiler way to much like the FORTH definition of "Written in it's own language, compiling itself this way" making it a metacompiler. When historically some metacompilers have been written on other languages (assembly, LISP, CL/1 and PL/1) and of course did not compile themselves. When I last looked at FORTH it was not being called a metacompiler. -- Steamerandy ( talk) 22:29, 19 November 2014 (UTC)
The statement:
Is inconsistent with the defination of metalanguage.
Metaprogramming is programming that treats programs as data. Writing a compiler is metaprogramming. Compilers may be written in most any programming language. A metalanguage is used to describe other languages or talk about them. A compiler may be written in a metalanguage. But for example metaprogramming may be done in c++. Would then, c++ be a metalanguage?
Steamerandy ( talk) 02:41, 27 November 2014 (UTC)
...is quite poor. And it's consistent throughout the entire article, not just a few things here and there. Unfortunately, I don't know anything about the subject, so I'm just going to tag it and run. Deacon Vorbis ( talk) 01:57, 22 January 2017 (UTC)
The definition given in the introduction is very restrictive ("treating programs as data"). It implies that metaprogramming is possible only within homoiconic (therefore interpreted) programming languages. It then talks about C++ metaprogramming, which is purely static, creating an worrying level of inconsistency in the article. Vincent Lextrait ( talk) 01:17, 30 December 2017 (UTC)
R allows meta programming, it don't have macros and templates but it have substitute function that all to get expression being passed to function and prevent evaluation. I think you can write macro system in that language. In R everything is evaluated in reverse order (lazy evaluation) function get expressions as arguments and only when they are used, they get evaluated. That's why you can stop evaluation, modify the code and evaluate in your own way. This is powerful meta programming were you can define whole new syntax or new operators that are not supported by the language. (this is how purrr, dplyr etc. libraries define new syntax to process data).
jcubic ( talk) 14:23, 22 April 2020 (UTC)
Many script languages with run-time interpretation have the ability to produce a text string containing valid code in the same language and then interpret this string and execute it. Many of the examples mentioned belong to this category. Other categories are:
The article would be more clear if the sections were organized along such categories. Agnerf ( talk) 06:36, 15 July 2022 (UTC)
constexpr functions and "if constexpr" are used in C++ to explicitly specify that certain calculations and decisions are done at compile time.
An example of such compile-time calculations is the C++ Vector Class Library which is using compile-time calculations to find the optimal instruction sequence to implement a particular algorithm. [1] Agnerf ( talk) 06:36, 15 July 2022 (UTC)
References