Expressive and strongly type-safe code generation

Meta-programs are programs that generate other programs, but in weakly type-safe systems, type-checking a meta-program only establishes its own type safety, and generated programs need additional type-checking after generation. Strong type safety of a meta-program implies type safety of any generated object program, a property with important engineering benefits. Current strongly type-safe systems suffer from expressivity limitations and cannot support many meta-programs found in practice, for example automatic generation of lenses. To overcome this, we move away from the idea of staged meta-programming. Instead, we use an off-the-shelf dependently-typed language as the meta-language and a relatively standard, intrinsically well-typed representation of the object language. We scale this approach to practical meta-programming, by choosing a high-level, explicitly typed intermediate representation as the object language, rather than a surface programming language. We implement our approach as a library for the Glasgow Haskell Compiler (GHC) and evaluate it on several meta-programs, including a deriveLenses meta-program taken from a real-world Haskell lens library. Our evaluation demonstrates expressivity beyond the state of the art and applicability to real settings, at little cost in terms of code size.

[1]  Samuel Boutin,et al.  Using Reflection to Build Efficient and Certified Decision Procedures , 1997, TACS.

[2]  Walid Taha,et al.  Concoqtion: indexed types now! , 2007, PEPM '07.

[3]  Nils Anders Danielsson A Formalisation of a Dependently Typed Language as an Inductive-Recursive Family , 2006, TYPES.

[4]  Russell O'Connor Functor is to Lens as Applicative is to Biplate: Introducing Multiplate , 2011, ArXiv.

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

[6]  U. Norell,et al.  Towards a practical programming language based on dependent type theory , 2007 .

[7]  Zhong Shao,et al.  VeriML: typed computation of logical terms inside a language with effects , 2010, ICFP '10.

[8]  Adam Megacz Joseph,et al.  Generalized Arrows , 2014 .

[9]  Simon L. Peyton Jones,et al.  OutsideIn(X) Modular type inference with local assumptions , 2011, J. Funct. Program..

[10]  Eugene Burmako,et al.  Unification of Compile-Time and Runtime Metaprogramming in Scala , 2017 .

[11]  Frank Pfenning,et al.  A modal analysis of staged computation , 1996, POPL '96.

[12]  Walid Taha,et al.  Environment classifiers , 2003, POPL.

[13]  Atsushi Igarashi,et al.  On Cross-Stage Persistence in Multi-Stage Programming , 2014, FLOPS.

[14]  Thorsten Altenkirch,et al.  Generic Programming within Dependently Typed Programming , 2002, Generic Programming.

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

[16]  Chiyan Chen,et al.  Meta-programming through typeful code representation , 2003, ICFP '03.

[17]  Geoffrey Mainland Why it's nice to be quoted: quasiquoting for haskell , 2007, Haskell '07.

[18]  Brigitte Pientka,et al.  Programming with proofs and explicit contexts , 2008, PPDP '08.

[19]  Walid Taha,et al.  MetaML and multi-stage programming with explicit annotations , 2000, Theor. Comput. Sci..

[20]  Zhong Shao,et al.  Static and user-extensible proof checking , 2012, POPL '12.

[21]  Simon L. Peyton Jones,et al.  Template meta-programming for Haskell , 2002, Haskell '02.

[22]  Conor McBride,et al.  Outrageous but meaningful coincidences: dependent type-safe syntax and evaluation , 2010, WGP '10.

[23]  Walid Taha,et al.  Multi-stage programming with explicit annotations , 1997, PEPM.

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

[25]  Edwin Brady,et al.  A verified staged interpreter is a verified compiler , 2006, GPCE '06.

[26]  James Chapman,et al.  Type Theory Should Eat Itself , 2008, LFMTP@LICS.

[27]  Frank Pfenning,et al.  Staged computation with names and necessity , 2005, J. Funct. Program..

[28]  Walid Taha,et al.  Multi-stage programming with explicit annotations , 1997 .

[29]  Emir Pasalic,et al.  Meta-programming With Built-in Type Equality , 2008, Electron. Notes Theor. Comput. Sci..

[30]  Walid Taha,et al.  Tagless staged interpreters for typed languages , 2002, ICFP '02.

[31]  Emir Pasalic,et al.  Meta-programming with Typed Object-Language Representations , 2004, GPCE.