Question

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

Was it helpful?

Solution

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.

OTHER TIPS

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. metaprogramming is not limited to two levels, and it's easier on the brain, too.
  • 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 as first class objects; i.e. they can be unnamed functions that you can keep in a local variable, or a global hashtable, etc...
  • 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 it at compile-time with the original form, and in place of the original form it compiles the form this function returns. (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). in such a function you have all the power to introduce whatever new 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 (hu.dwim.delico)
  • implement a js-to-lisp-rpc macro that you can use in javascript (which is generated from lisp). it expands into a mixture of js/lisp code that automatically posts (in the http request) all the referenced local variables, decodes them on the server side, runs the lisp code body on the server, and returns back the return value to the javascript side.
  • 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

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.

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.

Nim is a relatively new programming language that has extensive support for static meta-programming and produces efficient (C++ like) compiled code.

http://nim-lang.org/

It supports compile-time function evaluation, lisp-like AST code transformations through macros, compile-time reflection, generic types that can be parametrized with arbitrary values, and term rewriting that can be used to create user-defined high-level type-aware peephole optimizations. It's even possible to execute external programs during the compilation process that can influence the code generation. As an example, consider talking to a locally running database server in order to verify that the ORM definition in your code (supplied through some DSL) matches the schema of the database.

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

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!

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

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

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.

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.

'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
  • heavily 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 'static 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.

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.

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top