Multi-Stage Programming Walid Taha & Tim Sheard Oregon Graduate Institute of Science & Technology fwalidt,
[email protected]
1 Introduction The concept of a multi-stage computation has recently been the focus of a number of studies [2, 1]. A computational stage roughly corresponds to the \running" of a program on some input. Practical interest in models of multi-stage computations stems from their signi cant potential for enabling meaning-preserving optimizations whenever the inputs to a program are available at \dierent times".
2 Examples of Multi-Stage Computation Despite the numerous examples that can be seen in almost every kind of application, the multi-stage nature of a computation can be obscured by the fact that a stage often coincides with a translation from one language to another. Consider compilation: First, the source program is compiled or translated to machine code, and then later, the machine code is executed. With respect to the source program, the translation step is really the rst stage in executing this program, and executing the machine code is the second stage. However, the translation from one language to another is not fundamental to the rst stage. Evidence of this is that we can imagine the object language being the same as the source language, and the \compiler" would still be doing useful work, including performing any of a number of optimizations on the input program. The last stage of any multi-stage computation usually does not generate a program, but rather, a value that the end-user can understand. Another example is using yacc for parser-generation. There are three distinct stages: 1) \compiling"the grammar and generating a C program, 2) compiling the the C program, and 3) executing the object code. Here, again, in a language such as SML/NJ, the grammar and all the contents of the yacc input le can be represented in the same language as the generated language, The research reported in this paper was supported by the USAF Air Materiel Command, contract # F19628-93-C-0069, and NSF Grant IRI-9625462.
in which case, again, the input and the output of the rst stage are in the same language. And still, there is useful work being done. The second and third stages are exactly the same as in classic compilation. Finally, one of the most interesting applications of the idea of multi-stage computation can be seen in partial evaluation. In particular, an oine partial evaluation takes a single-stage program and generates a twostage program. The two-stage program is then run on the inputs that are known at \partial-evaluation time". The resulting program is a specialized, more ecient version of the original program that has just been partially evaluated. Recently, oine partial evaluation has been generalized to general multi-stage programs [2].
3 MetaML
Until recently, multi-stage computations were developed using ad hoc methods, such as generating text-strings representing programs. Systems developed this way are prone to error. MetaML is a statically-typed multistage language based on an SML-like core. The language was developed to allow multi-stage programming with explicit annotations [3].
References [1] Rowan Davies and Frank Pfenning. A modal analysis of staged computation. In 23rd Annual ACM Symposium on Principles of Programming Languages (POPL'96), St.Petersburg Beach, Florida, January 1996. [2] Robert Gluck and Jesper Jrgensen. Fast bindingtime analysis for multi-level specialization. In PSI96: Andrei Ershov Second International Memorial Conference, Perspectives of System Informatics, volume 1181 of Lecture Notes in Computer Science. Springer-Verlag, 1996. [3] Walid Taha and Tim Sheard. Multi-stage programming with explicit annotations. In Proceedings of the Symposium on Partial Evaluation and Semantics-Based Program Manipulation, Amsterdam, June 1997.