TY - GEN
T1 - Combinators for impure yet hygienic code generation
AU - Kameyama, Yukiyoshi
AU - Kiselyov, Oleg
AU - Shan, Chung Chieh
PY - 2014
Y1 - 2014
N2 - Code generation is the leading approach to making high-performance software reusable. Effects are indispensable in code generators, whether to report failures or to insert let-statements and if- guards. Extensive painful experience shows that unrestricted effects interact with generated binders in undesirable ways to produce unexpectedly unbound variables, or worse, unexpectedly bound ones. These subtleties hinder domain experts in using and extending the generator. A pressing problem is thus to express the desired effects while regulating them so that the generated code is correct, or at least correctly scoped, by construction. We present a code-combinator framework that lets us express arbitrary monadic effects, including mutable references and delimited control, that move open code across generated binders. The static types of our generator expressions not only ensure that a well-typed generator produces well-typed and well-scoped code. They also express the lexical scopes of generated binders and prevent mixing up variables with different scopes. For the first time ever we demonstrate statically safe and well-scoped loop exchange and constant factoring from arbitrarily nested loops. Our framework is implemented as a Haskell library that embeds an extensible typed higher-order domain-specific language. It may be regarded as 'staged Haskell.' To become practical, the library relies on higher-order abstract syntax and polymorphism over generated type environments, and is written in the mature language. Categories and Subject Descriptors D.3.1 [Programming Languages]: Formal Definitions and Theory; D.3.3 [Programming Languages]: Language Constructs and Features-Control structures; polymorphism; F.3.3 [Logics and Meanings of Programs]: Studies of Program Constructs-Type structure.
AB - Code generation is the leading approach to making high-performance software reusable. Effects are indispensable in code generators, whether to report failures or to insert let-statements and if- guards. Extensive painful experience shows that unrestricted effects interact with generated binders in undesirable ways to produce unexpectedly unbound variables, or worse, unexpectedly bound ones. These subtleties hinder domain experts in using and extending the generator. A pressing problem is thus to express the desired effects while regulating them so that the generated code is correct, or at least correctly scoped, by construction. We present a code-combinator framework that lets us express arbitrary monadic effects, including mutable references and delimited control, that move open code across generated binders. The static types of our generator expressions not only ensure that a well-typed generator produces well-typed and well-scoped code. They also express the lexical scopes of generated binders and prevent mixing up variables with different scopes. For the first time ever we demonstrate statically safe and well-scoped loop exchange and constant factoring from arbitrarily nested loops. Our framework is implemented as a Haskell library that embeds an extensible typed higher-order domain-specific language. It may be regarded as 'staged Haskell.' To become practical, the library relies on higher-order abstract syntax and polymorphism over generated type environments, and is written in the mature language. Categories and Subject Descriptors D.3.1 [Programming Languages]: Formal Definitions and Theory; D.3.3 [Programming Languages]: Language Constructs and Features-Control structures; polymorphism; F.3.3 [Logics and Meanings of Programs]: Studies of Program Constructs-Type structure.
KW - Binders
KW - CPS
KW - Higher-order abstract syntax
KW - Lexical scope
KW - Multi-stage programming
KW - Mutable state and control effects
UR - http://www.scopus.com/inward/record.url?scp=84897688579&partnerID=8YFLogxK
UR - http://www.scopus.com/inward/citedby.url?scp=84897688579&partnerID=8YFLogxK
U2 - 10.1145/2543728.2543740
DO - 10.1145/2543728.2543740
M3 - Conference contribution
AN - SCOPUS:84897688579
SN - 9781450326193
T3 - PEPM 2014 - Proceedings of the ACM SIGPLAN Workshop on Partial Evaluation and Program Manipulation, Co-located with POPL 2014
SP - 3
EP - 14
BT - PEPM 2014 - Proceedings of the ACM SIGPLAN Workshop on Partial Evaluation and Program Manipulation, Co-located with POPL 2014
PB - Association for Computing Machinery
T2 - ACM SIGPLAN Workshop on Partial Evaluation and Program Manipulation, PEPM 2014 - Co-located with POPL 2014
Y2 - 20 January 2014 through 21 January 2014
ER -