views:

1760

answers:

13

C++ is probably the most popular language for static metaprogramming and Java doesn't support it.

Are there any other languages besides C++ that support generative programming (programs that create programs)? Which ones are the best (for learning, for efficiency, for maintenance, for embedded systems, whatever)?

+1  A: 

Lisp supports a form of "metaprogramming", although not in the same sense as C++ template metaprogramming. Also, your term "static" could mean different things in this context, but Lisp also supports static typing, if that's what you mean.

Ben Collins
I understand “static metaprogramming” as meaning compile-time code transformation as opposed to dynamically intercepting/changing behaviour at run-time (as with metaclasses or EVAL, for instance). In this sense, Lisp macros are a kind of static metaprogramming facility. OP, is this what you mean?
Matthias Benkard
There is a good reason that lisp has been called the "programmable programming language". It allows/encourages a much more general form of "metaprogramming" than things like the c++ template approach.
simon
@Matthias, yes, that is what I mean. Thanks!
jwfearn
+19  A: 

The alternative to template style meta-programming is Macro-style that you see in various Lisp implementations. I would suggest downloading Paul Graham's On Lisp and also taking a look at Clojure if you're interested in a Lisp with macros that runs on the JVM.

Macros in Lisp are much more powerful than C/C++ style and constitute a language in their own right -- they are meant for meta-programming.

Lou Franco
Hmm, how are Lisp macros a language in their own right? It's all Lisp, that's the beauty of it.
Luís Oliveira
Macros are a little different than Lisp since they don't get evaluated and a bunch of special operators. I don't think you can build macros with Lisp (in the sense that you can't add macros to your lisp with just a bunch of functions).
Lou Franco
You can't add functions (or variables, or special operators, or...) to your Lisp with a bunch of macros, either. So functions are a language in their own right as well?
Matthias Benkard
adding macros to a lisp that didn't have macros before is actually quite simple and does not involve changing meta-level constructs like special operators. the devil is in the details of course, but a v0.1 is a matter of a page or two...
Attila Lendvai
Would I be right in thinking that Lisp's meta-programming is not static (that is, at compile-time)?
Paul Biggar
Lisp macros can be plugged into the various phases of the compiler. I am not an expert, but there are reader macros that get called during parsing and compile time macros that generate code, for example. I think there are others.
Lou Franco
There are "read macros", these are used when reading source code (from the REPL or from file) and transform source to internal representation. There are "normal" macros, they're used during "macro-expansion time" (this is done in, essentially, "the minimal compilation phase", so pretty close to function definition). There are "compiler macros", that work as whole-function compilation replacement things, where you can emit different code depending on what you have in the way of constants, variables, known type restrictions and the like.
Vatine
+9  A: 

Nemerle and Boo are my personal favorites for such things. Nemerle has a very elegant macro syntax, despite its poor documentation. Boo's documentation is excellent but its macros are a little less elegant. Both work incredibly well, however.

Both target .NET, so they can easily interoperate with C# and other .NET languages -- even Java binaries, if you use IKVM.

Edit: To clarify, I mean macros in the Lisp sense of the word, not C's preprocessor macros. These allow definition of new syntax and heavy metaprogramming at compiletime. For instance, Nemerle ships with macros that will validate your SQL queries against your SQL server at compiletime.

Cody Brocious
Thanks for your answers, perhaps you could edit to add links for Nemerle and Boo?
jwfearn
The site for nemerle is http://nemerle.org.Boo is located at http://boo.codehaus.org/
catbert
+2  A: 

I recommend Haskell. Here is a paper describing its compile-time metaprogramming capabilities.

Ben Hoffstein
+5  A: 

The "D" programming language is C++-like but has much better metaprogramming support. Here's an example of a ray-tracer written using only compile-time metaprogramming:

Ctrace

Additionally, there is a gcc branch called "Concept GCC" that supports metaprogramming contructs that C++ doesn't (at least not yet).

Concept GCC

+1: D looks very cool
Callum Rogers
A: 

'metaprogramming' is really a bad name for this specific feature, at least when you're discussing more than one language, since this feature is only needed for a narrow slice of languages that are:

  • static
  • compiled to machine language
  • heavyly optimised for performance at compile time
  • extensible with user-defined data types (OOP in C++'s case)
  • hugely popular

take out any one of these, and 'stati metaprogramming', just doesn't make sense. therefore, i would be surprised if any remotely mainstream language had something like that, as understood on C++.

of course, dynamic languages, and several functional languages support totally different concepts that could also be called metaprogramming.

Javier
+1  A: 

The Meta-Language (ML), of course: http://cs.anu.edu.au/student/comp8033/ml.html

Nemanja Trifunovic
+8  A: 

Template metaprogramming is essentially abuse of the template mechanism. What I mean is that you get basically what you'd expect from a feature that was an unplanned side-effect --- it's a mess, and (although tools are getting better) a real pain in the ass because the language doesn't support you in doing it (I should note that my experience with state-of-the-art on this is out of date, since I essentially gave up on the approach. I've not heard of any great strides made, though)

Messing around with this in about '98 was what drove me to look for better solutions. I could write useful systems that relied on it, but they were hellish. Poking around eventually led me to Common Lisp. Sure, the template mechanism is Turing complete, but then again so is intercal.

Common Lisp does metaprogramming `right'. You have the full power of the language available while you do it, no special syntax, and because the language is very dynamic you can do more with it.

There are other options of course. No other language I've used does metaprogramming better than Lisp does, which is why I use it for research code. There are lots of reasons you might want to try something else though, but it's all going to be tradeoffs. You can look at Haskell/ML/OCaml etc. Lots of functional languages have something approaching the power of Lisp macros. You can find some .NET targeted stuff, but they're all pretty marginal (in terms of user base etc.). None of the big players in industrially used languages have anything like this, really.

simon
Thanks for your answer! MP with C++ templates is definitely messy (that's one motivation for my question). When/if C++0x is available from compiler vendors it should get a little cleaner as many of the new language features are specifically to support MP.
jwfearn
+17  A: 

let me list a few important details about how metaprogramming works in lisp (or scheme, or slate, or pick your favorite "dynamic" language):

  • when doing metaprogramming in lisp you don't have to deal with two languages. the meta level code is written in the same language as the object level code it generates. therefore metaprogramming is not limited to two levels.
  • in lisp you have the compiler available at runtime. in fact the compile-time/run-time distinction feels very artificial there and is very much subject to where you place your point of view. in lisp with a mere function call you can compile functions to machine instructions that you can use from then on. they can even be unnamed functions that you can keep in a local variable or a global hashtable...
  • macros in lisp are very simple: a bunch of functions stuffed in a hashtable and given to the compiler. for each form the compiler is about to compile, it consults that hashtable. if it finds a function then calls that function at compile-time with the original form and compiles the form this function returns instead of the original form. (modulo some non-important details) so lisp macros are basically plugins for the compiler.
  • writing a lisp function in lisp that evaluates lisp code is about two pages of code (this is usually called eval). there you have all the power to introduce whatever rules you want on the meta level. (making it run fast is going to take some effort though... about the same as bootstrapping a new language... :)

random examples of what you can implement as a user library using lisp metaprogramming (these are actual examples of common lisp libraries):

  • extend the language with delimited continuations (cl-delico)
  • implement a js-to-lisp-rpc macro that you can use in javascript (which is generated from lisp). it extpands into a mixture of js/lisp code that automatically posts all the referenced local variables in the http request, extracts them on the server side, runs the lisp code body of the macro you provided and returns back the return value to the javascript side. (it's almost done in wui, working on it right now)
  • add prolog like backtracking to the language that very seamlessly integrates with "normal" lisp code (see screamer)
  • an XML templating extension to common lisp (includes an example of reader macros that are plugins for the lisp parser)
  • a ton of small DSL's, like loop or iterate for easy looping
Attila Lendvai
Thanks for an excellent answer. As per another answer, I've been checking out Clojure (a Lisp variant that runs on the JVM). You might find it interesting. A suggestion: add more links (e.g., slate, cl-delico, screamer, etc.) search is fine but a link saves readers' time.
jwfearn
+4  A: 

Common Lisp supports programs that write programs in several different ways.

1) Program data and program "abstract syntax tree" are uniform (S-expressions!)

2) defmacro

3) Reader macros.

4) MOP

Of these, the real mind-blower is MOP. Read "The Art of the Metaobject Protocol." It will change things for you, I promise!

jfm3
Thanks for your answer, I'll definitely check out MOP! A suggestion: to save readers' time, edit answer to add links or publication information for your references.
jwfearn
MOP rocks; Kizcales is now into Aspect Oriented Programming, which can be seen as MOP for Java, C#, etc.
ja
+2  A: 

The ML family of languages were designed specifically for this purpose. One of OCaml's most famous success stories is the FFTW library for high-performance FFTs that is C code generated almost entirely by an OCaml program.

Cheers, Jon Harrop.

Jon Harrop
+2  A: 

Lots of work in Haskell: Domain Specific Languages (DSL's), Executable Specifications, Program Transformation, Partial Application, Staged Computation. Few links to get you started:

ja
+2  A: 

Most people try to find a language that has "ultimate reflection" for self-inspection and something like "eval" for reifying new code. Such languages are hard to find (LISP being a prime counterexample) and they certainly aren't mainstream.

But another approach is to use a set of tools that can inspect, generate, and manipulate program code. Jackpot is such a tool focused on Java. http://jackpot.netbeans.org/

Our DMS software reengineering toolkit is such a tool, that works on C, C++, C#, Java, COBOL, PHP, Javascript, Ada, Verilog, VHDL and variety of other languages. (It uses production quality front ends to enable it to read all these langauges). Better, it can do this with multiple languages at the same instant. See http://www.semdesigns.com/Products/DMS/DMSToolkit.html

DMS succeeds because it provides a regular method and support infrastructure for complete access to the program structure as ASTs, and in most cases additional data such a symbol tables, type information, control and data flow analysis, all necessary to do sophisticated program manipulation.

Ira Baxter