views:

174

answers:

3

I want to do some metaprogramming in a statically typed language, where both my programs and my meta-programs will be typed. I mean this in a strong sense: if my program generator compiles, I want the type system to be strong enough that only type-correct programs can be generated.

As far as I know, only metaocaml can do this. (No, neither Template Haskell nor C++ templates fit the bill -- see this paper). Question: what other languages/systems allow this?

+2  A: 

F# can do this too through Code Quotations.

Stringer Bell
It *type checks* the quotations (in the absence of application context), *and* their application? It is easy to believe it does syntax checking.
Ira Baxter
Yes it type checks the application too, if that's what you're asking. E.g. `let x = <@square 5@>`, here x have the `Expr<int>` type. Expression evaluation is done by AST-rewrite.
Stringer Bell
+2  A: 

To do that, you have to ensure that the type system of the underlying languge is directly honored/checked by the metaprogram itself. As a practical matter, this almost forces the metaprogramming to be in the underlying language... so I guess I'm not suprised that you might be able to do this in metaocaml.

Most of us don't get metaprogramming tools built into the underlying language (C++ being rather an exception, and I reject it and reflection based systems as being too weak to carry out arbitrary transformations).

A system that can carry out arbitrary transformations (or metaprograms composed of sets of those) on code is the DMS Software Reengineering Toolkit. DMS has front ends for many real langauges, builds compiler data structures when parsing (including ASTs). DMS provides source-to-source program transformations that represent transformations as AST-rewrites using the surface syntax of the target language. It meets your requirement to a certain degree: if your transformation rules are syntactically correct (and they are checked by DMS), then the transformed program will be syntactically correct. It does not achieve your type-correctness requirement, as the type-checking mechanisms are implemented outside the target language. In principal, one could a type-safe checker to augment the program transformations; in practice, we've found that we can code transformations reliably enough.

And even if you have type-safe transformations, you don't have a guarantee of semantic safety with respect to your original program. So, you'll still have to debug the metaprograms.

Ira Baxter
How does that compare to Stratego and TXL ?
Jacques Carette
Similar, in that all three have surface-syntax AST rewriting. DMS has a variety of production-quality language front ends. DMS provides control/dataflow/callgraph/pointsto and symbolic range analsis mechanisms which Stratego and TXL do not provide. Their proponents suggest that you can compute the part of (flow) analysis on demand by coding various rewrite rules, but that's not an approach that I believe is practical. For more comparisons of DMS to a variety of program manipulation technologies, see http://www.semdesigns.com/products/DMS/DMSComparison.html
Ira Baxter
A: 

http://en.wikipedia.org/wiki/Nemerle

dotneter