Combinators for impure yet hygienic code generation

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.

[1]  Olivier Danvy,et al.  Abstracting control , 1990, LISP and Functional Programming.

[2]  Jim Davies,et al.  Science of Computer Programming , 2014 .

[3]  StatePeter Thiemann Partial Evaluation for Higher-Order Languages with , 1996 .

[4]  J Hage DOMain Specific Type Error Diagnosis (DOMSTED) , 2014 .

[5]  François Pottier,et al.  A fresh look at programming with names and binders , 2010, ICFP '10.

[6]  J. Gregory Morrisett,et al.  Monadic regions , 2006, J. Funct. Program..

[7]  Gérard P. Huet,et al.  Proving and applying program transformations expressed with second-order patterns , 1978, Acta Informatica.

[8]  Martin Odersky,et al.  Lightweight modular staging: a pragmatic approach to runtime code generation and compiled DSLs , 2010, GPCE '10.

[9]  Martin Odersky,et al.  Higher-order and Symbolic Computation Manuscript No. Scala-virtualized: Linguistic Reuse for Deep Embeddings , 2022 .

[10]  Chiyan Chen,et al.  Meta-programming through typeful code representation , 2005, J. Funct. Program..

[11]  Morten Rhiger,et al.  Staged Computation with Staged Lexical Scope , 2012, ESOP.

[12]  Conor McBride,et al.  Applicative programming with effects , 2008, J. Funct. Program..

[13]  Jan Vitek,et al.  Terra: a multi-stage language for high-performance computing , 2013, PLDI.

[14]  Kwangkeun Yi,et al.  A polymorphic modal type system for lisp-like multi-staged languages , 2006, POPL '06.

[15]  Jacques Carette,et al.  Multi-stage programming with functors and monads: eliminating abstraction overhead from generic code , 2005, GPCE'05.

[16]  David Herman,et al.  A theory of typed hygienic macros , 2010 .

[17]  Anders Bondorf,et al.  Improving binding times without explicit CPS-conversion , 1992, LFP '92.

[18]  Carsten Schürmann,et al.  System Description: Delphin - A Functional Programming Language for Deductive Systems , 2008, LFMTP@LICS.

[19]  Walid Taha,et al.  Environment classifiers , 2003, POPL '03.

[20]  Walid Taha,et al.  A methodology for generating verified combinatorial circuits , 2004, EMSOFT '04.

[21]  Matthias Felleisen,et al.  Hygienic macro expansion , 1986, LFP '86.

[22]  Brigitte Pientka A type-theoretic foundation for programming with higher-order abstract syntax and first-class substitutions , 2008, POPL '08.

[23]  Simon L. Peyton Jones,et al.  State in Haskell , 1995, LISP Symb. Comput..

[24]  Geoffrey Mainland,et al.  Explicitly heterogeneous metaprogramming with MetaHaskell , 2012, ICFP.

[25]  Jurriaan Hage,et al.  Scripting the type inference process , 2003, ICFP '03.

[26]  Walid Taha,et al.  Relating FFTW and Split-Radix , 2004, ICESS.

[27]  Walid Taha,et al.  A sound reduction semantics for untyped CBN mutli-stage computation. Or, the theory of MetaML is non-trival (extended abstract) , 1999, PEPM '00.

[28]  Frank Pfenning,et al.  Higher-order abstract syntax , 1988, PLDI '88.

[29]  Jacques Carette,et al.  Finally tagless, partially evaluated: Tagless staged interpreters for simpler typed languages , 2007, Journal of Functional Programming.

[30]  Manuel M. T. Chakravarty,et al.  Specialising Simulator Generators for High-Performance Monte-Carlo Methods , 2008, PADL.

[31]  Mitchell Wand,et al.  A Theory of Hygienic Macros , 2008, ESOP.

[32]  Walid Taha,et al.  A Sound Reduction Semantics for Untyped CBN Multi-stage Computation. Or, the Theory of MetaML is Non-trivial (Extended Abstract) , 1999, PEPM.

[33]  Andrei P. Ershov,et al.  On the Partial Computation Principle , 1977, Inf. Process. Lett..

[34]  Steven McCanne,et al.  BPF+: exploiting global data-flow optimization in a generalized packet filter architecture , 1999, SIGCOMM '99.

[35]  Franz Franchetti,et al.  SPIRAL: Code Generation for DSP Transforms , 2005, Proceedings of the IEEE.

[36]  Tim Sheard,et al.  Closed types for a safe imperative MetaML , 2003, J. Funct. Program..

[37]  Jurriaan Hage,et al.  Type Class Directives , 2005, PADL.

[38]  Jeremy Gibbons,et al.  Generic and Indexed Programming , 2012, Lecture Notes in Computer Science.

[39]  Walid Taha,et al.  Implicitly Heterogeneous Multi-Stage Programming , 2005, GPCE'05.

[40]  Olivier Danvy,et al.  Continuation-based partial evaluation , 1994, LFP '94.

[41]  Daan Leijen First-class labels for extensible rows , 2004 .

[42]  Walid Taha,et al.  A monadic approach for avoiding code duplication when staging memoized functions , 2006, PEPM '06.

[43]  Frank Pfenning,et al.  Contextual modal type theory , 2008, TOCL.

[44]  François Pottier,et al.  Static Name Control for FreshML , 2007, 22nd Annual IEEE Symposium on Logic in Computer Science (LICS 2007).

[45]  Gopalan Nadathur,et al.  A Logic Programming Approach to Manipulating Formulas and Programs , 1987, SLP.

[46]  Mitchell Wand,et al.  The Theory of Fexprs is Trivial , 1998, LISP Symb. Comput..

[47]  Chung-chieh Shan,et al.  Shifting the stage - Staging with delimited control , 2011, J. Funct. Program..

[48]  Jeremy Yallop,et al.  Lightweight Higher-Kinded Polymorphism , 2014, FLOPS.

[49]  Peter Thiemann,et al.  Combinators for program generation , 1999, Journal of Functional Programming.

[50]  Walid Taha,et al.  Closed Types as a Simple Approach to Safe Imperative Multi-stage Programming , 2000, ICALP.

[51]  Gang Chen,et al.  Guarded recursive datatype constructors , 2003, POPL '03.

[52]  Oleg Kiselyov,et al.  Typed Tagless Final Interpreters , 2010, SSGIP.

[53]  Alonzo Church,et al.  A formulation of the simple theory of types , 1940, Journal of Symbolic Logic.

[54]  Kwangkeun Yi,et al.  Type and Effect System for Multi-staged Exceptions , 2006, APLAS.

[55]  David A. Padua,et al.  In search of a program generator to implement generic transformations for high-performance computing , 2006, Sci. Comput. Program..

[56]  Walid Taha,et al.  Mint: Java multi-stage programming using weak separability , 2010, PLDI '10.