Title
Closing the stage: from staged code to typed closures
Abstract
Code generation lets us write well-abstracted programs without performance penalty. Writing a correct code generator is easier than building a full-scale compiler but still hard. Typed multistage languages such as MetaOCaml help in two ways: they provide simple annotations to express code generation, and they assure that the generated code is well-typed and well-scoped. Unfortunately, the assurance only holds without side effects such as state and control. Without effects, generators often have to be written in a continuation-passing or monadic style that has proved inconvenient. It is thus a pressing open problem to combine effects with staging in a sound type system. This paper takes a first step towards solving the problem, by translating the staging away. Our source language models MetaOCaml restricted to one future stage. It is a call-by-value language, with a sound type system and a small-step operational semantics, that supports building open code, running closed code, cross-stage persistence, and non-termination effects. We translate each typing derivation from this source language to the unstaged System F with constants. Our translation represents future-stage code using closures, yet preserves the typing, α-equivalence (hygiene), and (we conjecture) termination and evaluation order of the staged program. To decouple evaluation from scope (a defining characteristic of staging), our translation weakens the typing environment of open code using a term coercion reminiscent of Goedel's translation from intuitionistic to modal logic. By converting open code to closures with typed environments, our translation establishes a framework in which to study staging with effects and to prototype staged languages. It already makes scope extrusion a type error.
Year
DOI
Venue
2008
10.1145/1328408.1328430
PEPM
Keywords
Field
DocType
source language,open problem,future-stage code,open code,correct code generator,call-by-value language,typed multistage language,sound type system,closed code,code generation,parametric polymorphism,side effect,operational semantics,type system,modal logic,closures
Unreachable code,Code bloat,Programming language,Computer science,Source code,Compiler,Theoretical computer science,Code generation,Redundant code,Code word,Dead code
Conference
Citations 
PageRank 
References 
6
0.57
24
Authors
3
Name
Order
Citations
PageRank
Yukiyoshi Kameyama117117.29
Oleg Kiselyov266045.44
Chung-chieh Shan348533.27