• Advertisement
Sign in to follow this  

How you would define metaprogramming?

This topic is 4396 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Am I correct in thinking that metaprogramming is "programming of programming", such as writing program that generates code, and the like? (or creating templates that nontrivially generate code). So the quine is simple, agreed upon "helloworld-ish" example of metaprogram. Just were discussing it in russian and have feeling that there's some language barrier... either i or other side simply doesn't understand what "meta" as in "meta-programming" means. (they defined metaprogramming as programming in "other language", whatever it means (meta-language? lol)...)

Share this post


Link to post
Share on other sites
Advertisement
Guest Anonymous Poster
You are correct, although I would say 'Programming about programming'.

One could define a program in a self defined language and then use some sort
of compiler to create the program in another language, say C or Java.

I have in the past used XML and XSLT to transform my XML to C# code. Just search for metaprogramming or try something out yourself.

Share this post


Link to post
Share on other sites
Typical examples of metaprogramming include C preprocessor directives, Flex definitions, YACC grammars, and just about anything using the word "macro".

Share this post


Link to post
Share on other sites
Quote:
Original post by Dmytry
Am I correct in thinking that metaprogramming is "programming of programming", such as writing program that generates code, and the like? (or creating templates that nontrivially generate code).


Something like that, code generation is a specific instance of metaprogramming so there is more to it than simply generating code, you might want to read this. It can be done at compile-time, runtime, mix of both, it can be stagged as in a number of stages/levels occuring a different periods of time.

Refraction & Reflection is a form of metaprogramming since you are inspecting, querying, and/or modifying the actual program at runtime and/or compile-time.

Quote:
Original post by Dmytry
So the quine is simple, agreed upon "helloworld-ish" example of metaprogram.


Not really although i guess it is some form of metaprogramming.

Quote:
Original post by Dmytry
Just were discussing it in russian and have feeling that there's some language barrier... either i or other side simply doesn't understand what "meta" as in "meta-programming" means. (they defined metaprogramming as programming in "other language", whatever it means (meta-language? lol)...)


Yes a meta-language, it is higher level than the language it inspects/queries/manipulates, you write metaprograms with metadata, metafunctions, etc, etc.

The meta-language doesn't necessarily have to be different or completely different from the language in terms of syntax altough it may appear to look different when writing metaprograms.

[Edited by - snk_kid on December 29, 2005 11:45:30 AM]

Share this post


Link to post
Share on other sites
Thanks.

That basically confirms my definition. My definition is quite broad, not just explicit code generation of course, but almost any sort of nontrivial programming the programming. Sorta processing/creating the program programmatically. I meant that other side's definition requirs "other language" (very vaguely defined) and is much narrower (and IMO narrower in C++ - specific way).

Share this post


Link to post
Share on other sites
Dmytry,
The person you are arguing with is sort-of right about using another language for metaprogramming. It's not a reuirement, but most examples of metaprogramming are in a meta-language. C++'s templates form a language quite seperate from C++. The main flow control is pattern matching on template arguments, there's no iteration (only recursion), etc...

The C Preprocessor is clearly a seperate language.

LISP's macros are the only example I can think of where the meta-language is the same as the programming language.

-Alex

Share this post


Link to post
Share on other sites
I always assumed the origin of the term was meta-data in databases. Meta-data is your data about the data, basically the information stored in the system tables. A simple example of using meta-data in a decision support system is allowing a user to select tables to use in a query, then listing the union of the columns of the tables to let them select sorted order, the result set and selection criteria, then using the intersection of the index fields to build the where clause.

So to me I would include enum and typedefs in templated classes as metaprogramming. numeric_limits would be a prime example of supplying meta-data for a data type. Then using that information generate appropriate code would be meta-programming. Mostly, as I see the term used, it's basically writing code executed by the compiler. Examples being calculating n! or unwinding a loop. I assume that's the main use of the term because at that stage meta-programming is about all you are doing. As I view the term it would include many parts of the STL. Not the algorithms themselves, but much of what makes them work, i.e. iterators, traits, predicates, function objects, etc.

Share this post


Link to post
Share on other sites
Quote:
Original post by cypherx
Dmytry,
The person you are arguing with is sort-of right about using another language for metaprogramming. It's not a reuirement, but most examples of metaprogramming are in a meta-language. C++'s templates form a language quite seperate from C++. The main flow control is pattern matching on template arguments, there's no iteration (only recursion), etc...

The C Preprocessor is clearly a seperate language.

LISP's macros are the only example I can think of where the meta-language is the same as the programming language.

-Alex


I argee about him being sort of right(i.e. not entirely wrong) (my definition mostly includes his), it's the requiring another language that was disputed... i.e. we argued if that's metaprogramming or not if is not about another language.
as about another example when it is same language, consider code generator written in say C++ that produces C++ code. It's just that definition requiring "other language" is really vague (C++ templates it is part of C++ too)

[Edited by - Dmytry on December 29, 2005 11:05:22 AM]

Share this post


Link to post
Share on other sites
I think that functional programming languages (not only lisp) are examples of languages that feature metaprogramming.
@Dmytry: As you are really smart when it comes to mathematics, you may find interesting comparing metaprogramming with metalanguages in mathematic and philosophy (ie Russel or Wittgenstein works).
I don't know much about it myself, but it should be a quite interesting topic (paradoxes and logical fallacies).

Share this post


Link to post
Share on other sites
Are we not all missing a great point here? The great deveoplers of the past developed "meta" languages (templates, lisp, even xml etc.) so we could "describe" language..nouns not verbs. BUT it exists for us to use, full stop.

I don't think they were great philosophers but great scientists. The philosophy of language is far older than the computer age and meta langauges have existed as long as any language...aristole says that language descibes the soul and we all use it's many different and varied varities.

Lets thank all the wonderful people that came before us, but lets just write some good stuff, cross our fingers and get on with it.

PS. Happy New Year...xxxooo

Share this post


Link to post
Share on other sites
Now, for some serious brain hurt: Early C compilers would produce ASM output, which had to be compiled to machine code.

Wouldn't this make C coding match the definition of meta-programming? :)

For an example of good uses of meta-programming (which in my opinion is a silly term that won't last too long) check aspect-oriented programming... I just found out about it a few months ago, and it looks promising.

Share this post


Link to post
Share on other sites
re: programming in C when C is compiled to asm = metaprogramming?
IMO:
Of course not - or at least not any more or less than mere usage of template "meta-library" made by somebody else. Writing the _compiler_ itself that's another story, I would call that metaprogramming. Same for writing nontrivial templates, and other things.
(i think it's consistent with what definitions we have elsewhere - using physics library is not same as writing physics library)
(tell me if i'm very off there [grin])

Share this post


Link to post
Share on other sites
well, compiling C to asm involves much more than merely templates. If so, everyone and their grandmas could write compilers from scratch.

So... IMHO meta-programming is a really convoluted term for compiling. Compiling to another kind of source, yes, but still compiling for a different language. And eventually compiling direct to machine code (or *gasp* bytecode or MSIL).

Share this post


Link to post
Share on other sites
haha... yea I saw that one.

Quote:
Metaprogramming is the writing of programs that write or manipulate other programs (or themselves) as their data or that do part of the work that is otherwise done at runtime during compile time. In many cases, this allows programmers to get more done in the same amount of time as they would take to write all the code manually.


The following scenarios fit this definition:
-writing of C programs that write asm programs as their data (C-compilers)
-writing of programs that manipulate other programs as their data (optimizers)
-writing of programs that do part of the work, that is otherwise done at runtime, during compile time (a javascript-to-Java compiler)

so yea. That doesn't exactly narrow it down either :)

Share this post


Link to post
Share on other sites
Quote:
Original post by Madster
well, compiling C to asm involves much more than merely templates. If so, everyone and their grandmas could write compilers from scratch.

I were talking about c++ templates that can be complicated (like C++ templates that implement (bad) functional programming language is comparably complicated to simple compiler-to-C++ for such language. I would even say that complicated for mere reason that C++ templates were not created for metaprogramming, and template metaprogramming is nearly useless for any "real world" work)
Quote:


So... IMHO meta-programming is a really convoluted term for compiling. Compiling to another kind of source, yes, but still compiling for a different language. And eventually compiling direct to machine code (or *gasp* bytecode or MSIL).

definately not. It's much broader term than compiling. I would call automatic program verification metaprogramming too, for example. Also there is code generators that don't compile anything, yet produce code at output.

[Edited by - Dmytry on January 2, 2006 2:39:02 AM]

Share this post


Link to post
Share on other sites
errr okay. Yes, templates aren't for metaprogramming, since by metaprogramming you would usually mean programming in a different language. C++ Templates are for C++ programming.

So, how is "automatic program verification metaprogramming" different from compiling? Does a compiler not verify a program automatically each time you compile?

About code generators that produce code as an output without compiling... that's an oxymoron.

Quote:
From Wikipedia:
A compiler is a computer program that translates a series of statements written in one computer language (called the source code) into a resulting output in another computer language (often called the object or target language).

The code generator you describe... is in fact, a compiler.

Share this post


Link to post
Share on other sites
Well, most of metaprograms is in some way compilers.
But there is automatic program verificators that do not compile anything, do not produce any code at output (well, if you don't consider line numbers with errors to be an output language of course). Them are not compilers, and them don't compile. Period.
It's as silly to say that APV is compilers because compilers do some checks too as to say that helloworld is compiler because compilers write messages on console - both things come from same logical "mistake".
Also, what's about interpreters? Them aren't compilers either, but them do load source and analyze it. Everything same, except instead of storing code somewhere it executes code as it goes.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
hi guys,

the way i see it is that metaprogramming in a host language puts a developer into the position of a language designer. he/she can create an environment (i.e. language + library) that doesn't closely reflect the syntax or idioms of the host language (in fact is sometimes very different) giving the user of this hosted language the ability to solve (specific) problems more efficiently and elegantly than using the host language itself. c++ embedded domain specific langauges would just be the most prominent example of this.

cheers,
simon

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
i need to add that the result of the constructs written in the hosted language when compiled is an intermediate (simpler or more complicated) set of instructions in the host language which then gets compiled like any other c++ instruction in the same compier run.

that would be it for me. i see meta programming as a code generation step even if no code is generated.

- simon

Share this post


Link to post
Share on other sites
I didn't realise you meant standalone verification... that's unrelated to metaprogramming though, as are interpreters.

The AP makes an interesting point though, if one was able to define a domain-specific language and easily define a translation to the general language, then it's a really different method.

In fact, that's called Aspect-oriented programming :)

I just find the meta-programming term silly, that's all. I'd say Aspect-oriented programming is about defining a language, programming in it and compiling it to a general purpose language (or even straight to machine code). No 'meta' in sight.

Has anyone dabbled in Aspect? It sounds interesting, but I haven't the slightest idea about how one would go about it.

Share this post


Link to post
Share on other sites
Metaprogramming is not just another term for "compiling." There is non-generative metaprogramming. Reflection, for example.

Share this post


Link to post
Share on other sites
How about the metaprogramming where you use "metafunctions" by passing them other functions?

On a simple level, std::sort where you chose a sorting predicate.
For a more advanced one, std::accumulate or std::inner_product or std::transform which, with nice predicates, can do all kinds of fancy things.

Share this post


Link to post
Share on other sites
Quote:
Original post by me22
How about the metaprogramming where you use "metafunctions" by passing them other functions?

On a simple level, std::sort where you chose a sorting predicate.
For a more advanced one, std::accumulate or std::inner_product or std::transform which, with nice predicates, can do all kinds of fancy things.


That isn't usually considered metaprogramming. It's better known as functional programming and functions which take other functions as arguments are usually called higher-order functions. The term "metafunction" usually refers to a function executed at compile time, usually in C++ template metaprogramming.

Share this post


Link to post
Share on other sites
Quote:
Original post by Roboguy
Metaprogramming is not just another term for "compiling." There is non-generative metaprogramming. Reflection, for example.

Can you explain what Reflection is about? I read about it a while back, but I can't quite remember.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement