Sat Nov 19 09:30:57 EST 2011
( This is an expansion of the blog header. )
Metaprogramming is about dealing with programs as data. This is a
general principle that hides under the following names:
- code generation
- model based design
- model driven development
- active libraries
- multi-stage programming
- macro programming
- language towers
- domain specific languages
There is a continuum of different techniques and not really a standard
one-size-fits-all approach. I focus on formal languages (programming
languages) as models. In this approach the main problems are:
- Design of a Domain Specific Language (DSL) that is used to
express a model. A DSL lives in the trade-off space between:
* Simplicity: Make analysis and compilation practically
* Expressivity: Allow full specification of a solution in the
- Translation in the form of interpretation and compilation of DSL
* Static analysis: determine properties of the model without
executing it in its final destination environment.
* Code generation. Transfer from high level form that allows
static analysis, to low level form that allows execution in
the field, i.e. as machine (or C) code executing on a
Translation can involve a wide spectrum of techniques, but the
basic trade-off seems to be about how to add implementation hints
to a model. This is a trade-off between:
* A black-box compiler that does not need hints. This is
usually catalogued under the name ``partial evaluation''.
* A white-box compiler that uses implementation strategies
explicitly provided by the programmer, next to the model
specification. This is called ``staging''.
Ideally, you'd want a black-box compiler to just solve the
problem for you. However due to a usually astronomically huge
implementation space this is not feasible. Aiming at a
well-designed approach to providing implementation knowledge
seems a second best approach.
Concerning the tool set I focus on two approaches, both based on the
idea that side-effect-free functional programs are easier to
manipulate (transform, analyze, verify).
* Hygienic macros/modules combined with first class units. Racketc
(PLT Scheme) is a framework for building language towers based on
the Scheme programming language. See Staapl.
* Polymorphism and types in typed functional programming. Haskell's
type class based polymorphism, (Meta)OCaml modules and MetaOCaml
multi-stage code types.