Talk:Metaprogramming

From Wikipedia, the free encyclopedia

Contents

[edit] Assembly & LISP

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..

[edit] New example

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

[edit] Subtleties of Metaprogramming- what is and not

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:

DIM Radius AS Single
DIM Area AS Single
PRINT "Calculating the area of a circle"
INPUT "Enter radius of the circle: ", Radius
LET Area = 3.141592 * Radius * Radius
PRINT "The area of the circle is "; Area
END

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 don't understand how the above program can ever be a metaprogram. My understanding is that a metaprogram is a program that manipulates or reasons about other programs. The program above does not do that. The compiler or interpreter, on the otehr hand are metaprograms.

[edit] Efficiency

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)

It is large enough to measure. I was taught that, for any given programmer/language combination, the time taken to solve the problems with a piece of code varied as the square of its size in characters for the following reasons: The chance of the programmer making a mistake varied approximately linearly with the number of characters in the code and the difficulty of finding the problem varied approximately linearly with the number of characters in the code. If that is true, then there is an obvious advantage in anything that drastically reduces the number of characters that the programmer needs to write in order to specify a program. The effect becomes more noticable even for a small reduction in size as larger programs are considered.
Certainly I noticed a large productivity increase when I moved from writing database reports in VB to using a "home-made" database report generator which writes those same types of report in VB for me. Time required per report dropped from three days to three hours. The main reason is that I am far less likely to accidentally include bugs due to typos because I am now "metaprogramming" a relatively short report specification rather than "programming" a fairly large VB program. --Derek Ross


[edit] Examples: Suggestion

Might be interesting to: show the example programme in

  • Java
  • C or C++
  • other langauges

Might be interesting to: show a short example programme that does "something useful".


[edit] An addition

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).


[edit] Metaprogramming: proposed definition

Here is a proposal definition.

Metaprogramming is the writing of a program by a programmer whose intended purpose is to generate or output code, for the purpose of alleviating the need to write the code that would be generated by the program.

This excludes cases such as

compilers: not programming by a programmer, code's intended output is not code
compiler-compilers: not a program, output is not code

Thoughts? --Dysprosia 09:34, 11 Mar 2005 (UTC)

I think it's much too specific, really. Metaprogramming isn't at all limited to code generation. In some cases it extends the capabilities of the language — for example, you can perform tasks using reflection, a form of metaprogramming, that you could not perform without it, including many that don't involve code generation at all. I struggle to come up with a good definition, because it's a sort of moving target — yesterday's metaprogramming is often no longer considered metaprogramming today. The best thing I can come up with is to say it's a term used to describe many, but not all, programming technologies that operate on programs. --Deco 10:37, 11 Mar 2005 (UTC)
Can I get an example of using reflection? Perhaps the definition above can still be salvaged. Note we do need a tight definition of what metaprogramming is or it allows the whole term to degenerate, if one says that using a compiler is metaprogramming, it effectively means that all programming is metaprogramming, &/c. --Dysprosia 10:56, 11 Mar 2005 (UTC)
Hmmm, your definition does not exclude compilers or compiler compilers yet, I think:
  • Compilers output code (that's a definition of compiler). Compilers are written by programmers.
  • Compiler compilers are programs that output compilers, which are programs.
  • +Comment: "By a programmer" is redundant, since all programming is done by programmers. It can even be confusing, since the point of metaprogramming is that part of the coding work gets automated, and thus taken over by the machine.
(some text on history of metaprogramming removed here)
Note that I disagree that the current definition says "All programming is metaprogramming", so I don't see why it needs changing. But I'm willing to see what you can come up with. :-)
Thank you very much for actually looking and trying by the way. It shows that VfD sometimes still works. :-) --Kim Bruning 12:15, 11 Mar 2005 (UTC)
I have explained why the current definition does so, since it includes compilers.
Re: compilers: you miss "whose intended purpose is to generate or output code", so merely using a compiler would not be considered metaprogramming, but writing a compiler would be (is that necessarily desirable?)
Re compiler-compilers: The input to a compiler-compiler is a definition of the grammar, not a program that outputs code. --Dysprosia 00:16, 12 Mar 2005 (UTC)
But the objective is to manipulate code, and writing a compiler would indeed be considered metaprogramming, as it should be. :-) So does that mean we were actually in agreement the whole time? ;-) --Kim Bruning 13:06, 12 Mar 2005 (UTC)
I don't know... I think metaprogramming has to serve some self-referential purpose; that is, one metaprograms to avoid having to write the code one would have to anyway without it, so if a compiler writer uses metaprogramming techniques to write the compiler, then that would be metaprogramming, but if a compiler writer merely writes a compiler for the hell of it, that wouldn't. Metaprogramming, I think, has a lot to do with the intent and the object of the produced code. But this sort of aspect of metaprogramming is less clear. --Dysprosia 23:06, 13 Mar 2005 (UTC)
Let's run through an example: I have to write a lot of code, but this code has some kind of pattern to it. So to refactor, I write some separate code that creates these patterns, and I write a separate file to feed to this program which specifies which patterns I want specifically. What's this act called, according to you? --Kim Bruning 23:37, 13 Mar 2005 (UTC)
It's best you make the comparison directly without me having to guess what point you're trying to make ;) I'm guessing you're saying that this is what compilers do. But still, you haven't looked at the intent aspect. If I write a compiler to help me write a compiler, then I would say that's metaprogramming. But if I write a compiler to write a database program, then that would probably be metaprogramming too, since it's writing a program to generate code that helps me in the project I'm working on, but if i'm writing a compiler, it's a bit of a special case, since for the action to be helping what i'm doing, it would have to help write the compiler. Clearer? --Dysprosia 08:27, 14 Mar 2005 (UTC)
Guilty as charged! :-) What I'm trying to do is to try to grasp what part of writing a compiler makes it non-metaprogramming, according to you. I think it's important to understand your opinion, before I try to convince you of mine or not. :-) --Kim Bruning 09:11, 14 Mar 2005 (UTC)
Like I've said, the intent and purpose of the use of the metaprogramming technique, to me, would determine whether it would be metaprogramming or not. If one argues that compiler writing is metaprogramming, then, one would have to look at the intent. If the compiler was written to help write the compiler, that would be metaprogramming. Otherwise, it would not be. --Dysprosia 01:38, 15 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)

[edit] history of meta-programming

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)

Lisp is a much earlier example (sixties). --MarSch 12:46, 12 December 2006 (UTC)

[edit] Forth

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

[edit] Flare

You should check out this page: http://www.flare.org . It's about a programming language specificly for metaprogramming.

[edit] PHP and the like

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 tend to agree with this. Its quite often that one needs to incorporate some javascript generation in an asp.net application, and in fact if one does some research, the technique is fairly common... But in fact I think its a touch overused. Often you can find asp.net code that generates javascript that might be better generated as calls to specific javascript functions included in separate javascript files. It is much easier to define the functionality you need, place the functionality behind a few functions, test said functions, and then invoke or place calls where needed. Unless you already have a huge javascript library to use, javascript generation does not always make sense.
These days, any javascript generated is pretty low level and hidden behind few abstractions. But it is still generated. Root4(one) 05:24, 26 November 2006 (UTC)

[edit] An external link?

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)