Programming Type-Safe Transformations Using Higher-Order Abstract Syntax

Compiling syntax to native code requires complex code transformations which rearrange the abstract syntax tree. This can be particularly challenging for languages containing binding constructs, and often leads to subtle, hard to find errors. In this paper, we exploit higherorder abstract syntax (HOAS) to implement a type-preserving compiler for the simply-typed lambda-calculus, including transformations such as closure conversion and hoisting, in the dependently-typed language Beluga. Unlike previous implementations, which have to abandon HOAS locally in favor of a first-order binder representation, we are able to take advantage of HOAS throughout the compiler pipeline, so that we do not have to include any lemmas about binder manipulation. Scope and type safety of the code transformations are statically guaranteed, and our implementation directly mirrors the proofs of type preservation. Our work demonstrates that HOAS encodings offer substantial benefits to certified programming.

[1]  Lasse Nielsen,et al.  A Denotational Investigation of Defunctionalization , 2000 .

[2]  Zhong Shao,et al.  Implementing typed intermediate languages , 1998, ICFP '98.

[3]  Brigitte Pientka,et al.  Structural Recursion over Contextual Objects , 2014 .

[4]  Adam Chlipala,et al.  A certified type-preserving compiler from lambda calculus to assembly language , 2007, PLDI '07.

[5]  Andrew W. Appel,et al.  Shrinking lambda Expressions in Linear Time , 1997, J. Funct. Program..

[6]  Christian Urban,et al.  alpha-Prolog: A Logic Programming Language with Names, Binding and a-Equivalence , 2004, ICLP.

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

[8]  Amy P. Felty,et al.  The Next 700 Challenge Problems for Reasoning with Higher-Order Abstract Syntax Representations , 2015, Journal of Automated Reasoning.

[9]  John Hatcliff,et al.  CPS Translations and Applications: The Cube and Beyond , 1999, High. Order Symb. Comput..

[10]  Mark Lillibridge,et al.  Explicit polymorphism and CPS conversion , 1993, POPL '93.

[11]  Carsten Schürmann,et al.  Practical Programming with Higher-Order Encodings and Dependent Types , 2008, ESOP.

[12]  François Pottier,et al.  Polymorphic typed defunctionalization , 2004, POPL.

[13]  Stefan Monnier,et al.  A type-preserving closure conversion in haskell , 2007, Haskell '07.

[14]  Brigitte Pientka,et al.  Case Analysis of Higher-Order Data , 2009, Electron. Notes Theor. Comput. Sci..

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

[16]  Furio Honsell,et al.  A framework for defining logics , 1993, JACM.

[17]  Andrew M. Pitts,et al.  A new approach to abstract syntax involving binders , 1999, Proceedings. 14th Symposium on Logic in Computer Science (Cat. No. PR00158).

[18]  Anindya Banerjee,et al.  Design and Correctness of Program Transformations Based on Control-Flow Analysis , 2001, TACS.

[19]  Amy P. Felty,et al.  Reasoning with Higher-Order Abstract Syntax and Contexts: A Comparison , 2010, ITP.

[20]  John Hannan Type Systems for Closure Conversions , 2007 .

[21]  Frank Pfenning,et al.  Elf: a language for logic definition and verified metaprogramming , 1989, [1989] Proceedings. Fourth Annual Symposium on Logic in Computer Science.

[22]  Richard Statman,et al.  Higher-Order Rewriting with Dependent Types , 1999 .

[23]  Andrew Gacek The Abella Interactive Theorem Prover (System Description) , 2008, IJCAR.

[24]  Frank Pfenning,et al.  System Description: Twelf - A Meta-Logical Framework for Deductive Systems , 1999, CADE.

[25]  Xavier Leroy,et al.  Unboxed objects and polymorphic typing , 1992, POPL '92.

[26]  Adam Chlipala Parametric higher-order abstract syntax for mechanized semantics , 2008, ICFP.

[27]  Stefan Monnier,et al.  Statically Verified Type-Preserving Code Transformations in Haskell , 2006 .

[28]  Derek Dreyer,et al.  How to make ad hoc proof automation less ad hoc , 2011, ICFP '11.

[29]  Robert Harper,et al.  Mechanizing metatheory in a logical framework , 2007, Journal of Functional Programming.

[30]  John C. Reynolds,et al.  Towards a theory of type structure , 1974, Symposium on Programming.

[31]  Brigitte Pientka,et al.  Inductive Beluga: Programming Proofs , 2015, CADE.

[32]  Andrew W. Appel,et al.  A type-based compiler for standard ML , 1995, PLDI '95.

[33]  Brigitte Pientka,et al.  Programming with binders and indexed data-types , 2012, POPL '12.

[34]  Chiyan Chen,et al.  Implementing typeful program transformations , 2003, PEPM '03.

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

[36]  David Delahaye,et al.  A Tactic Language for the System Coq , 2000, LPAR.

[37]  Xavier Leroy,et al.  Mechanized Verification of CPS Transformations , 2007, LPAR.

[38]  Peter Lee,et al.  TIL: A Type-Directed Optimizing Compiler , 1996 .

[39]  John C. Reynolds,et al.  Definitional Interpreters for Higher-Order Programming Languages , 1972, ACM '72.

[40]  Brigitte Pientka,et al.  First-class substitutions in contextual type theory , 2013, LFMTP '13.

[41]  John Hatcliff,et al.  Weak normalization implies strong normalization in a class of non-dependent pure type systems , 2001, Theor. Comput. Sci..

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

[43]  Nick Benton,et al.  Compiling standard ML to Java bytecodes , 1998, ICFP '98.

[44]  Xavier Leroy,et al.  Formal certification of a compiler back-end or: programming a compiler with a proof assistant , 2006, POPL '06.

[45]  Brigitte Pientka,et al.  Bidirectional Elaboration of Dependently Typed Programs , 2014, PPDP '14.

[46]  Olivier Danvy,et al.  Representing Control: a Study of the CPS Transformation , 1992, Mathematical Structures in Computer Science.

[47]  Stefan Monnier,et al.  A type-preserving compiler in Haskell , 2008, ICFP 2008.

[48]  Brigitte Pientka,et al.  Beluga: A Framework for Programming and Reasoning with Deductive Systems (System Description) , 2010, IJCAR.

[49]  Brigitte Pientka,et al.  Beluga: Programming with Dependent Types, Contextual Data, and Contexts , 2010, FLOPS.

[50]  Brigitte Pientka,et al.  An insider's look at LF type reconstruction: everything you (n)ever wanted to know , 2013, J. Funct. Program..

[51]  Carsten Schürmann Certifying Voting Protocols , 2013, ITP.