Practical Reflection and Metaprogramming for Dependent Types

Embedded domain-specific languages are special-purpose programming languages that are implemented within existing generalpurpose programming languages. Dependent type systems allow strong invariants to be encoded in representations of domain-specific languages, but it can also make it difficult to program in these embedded languages. Interpreters and compilers must always take these invariants into account at each stage, and authors of embedded languages must work hard to relieve users of the burden of proving these properties. Idris is a dependently typed functional programming language whose semantics are given by elaboration to a core dependent type theory through a tactic language. This dissertation introduces elaborator reflection, in which the core operators of the elaborator are realized as a type of computations that are executed during the elaboration process of Idris itself, along with a rich API for reflection. Elaborator reflection allows domain-specific languages to be implemented using the same elaboration technology as Idris itself, and it gives them additional means of interacting with native Idris code. It also allows Idris to be used as its own metalanguage, making it into a programmable programming language and allowing code re-use across all three stages: elaboration, type checking, and execution. Beyond elaborator reflection, other forms of compile-time reflection have proven useful for embedded languages. This dissertation also describes error reflection, in which Idris code can rewrite DSL error messages before presenting domain-specific messages to users, as well as a means for integrating quasiquotation into a tactic-based elaborator so that high-level syntax can be used for low-level reflected terms.

[1]  Christian Höner zu Siederdissen,et al.  The HERMIT in the stream: fusing stream fusion's concatMap , 2014, PEPM '14.

[2]  Venanzio Capretta,et al.  Modelling general recursion in type theory , 2005, Mathematical Structures in Computer Science.

[3]  Don Syme,et al.  Leveraging .NET meta-programming components from F#: integrated queries and interoperable heterogeneous execution , 2006, ML '06.

[4]  Edwin Brady,et al.  Resource-Safe Systems Programming with Embedded Domain Specific Languages , 2012, PADL.

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

[6]  José Pedro Magalhães,et al.  Optimizing SYB is easy! , 2014, PEPM '14.

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

[8]  Kenji Takeda,et al.  Strongly-Typed Language Support for Internet- Scale Information Sources , 2012 .

[9]  John Hughes,et al.  The Design of a Pretty-printing Library , 1995, Advanced Functional Programming.

[10]  Dirk Draheim,et al.  A type system for reflective program generators , 2011, Sci. Comput. Program..

[11]  Peter Dybjer,et al.  Inductive families , 2005, Formal Aspects of Computing.

[12]  Jacques Malenfant,et al.  Reflection in logic, functional and object-oriented programming: a Short Comparative Study , 1995 .

[13]  Eugene Charles Ciccarelli,et al.  Presentation based user interfaces , 1984 .

[14]  de Ng Dick Bruijn Lambda calculus notation with nameless dummies, a tool for automatic formula manipulation, with application to the Church-Rosser theorem , 1972 .

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

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

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

[18]  Jon Louis Bentley,et al.  Programming pearls: little languages , 1986, CACM.

[19]  Ed Komp,et al.  The HERMIT in the machine: a plugin for the interactive transformation of GHC core language programs , 2013, Haskell 2013.

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

[21]  Willard Van Orman Quine Mathematical Logic: Revised Edition , 1982 .

[22]  Christian Hofer,et al.  Polymorphic embedding of dsls , 2008, GPCE '08.

[23]  Simon L. Peyton Jones,et al.  Regular, shape-polymorphic, parallel arrays in Haskell , 2010, ICFP '10.

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

[25]  Eugene Burmako,et al.  Scala macros: let our powers combine!: on how rich syntax and static types work with metaprogramming , 2013, SCALA@ECOOP.

[26]  Dominique Devriese,et al.  Typed syntactic meta-programming , 2013, ICFP.

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

[28]  Brian Cantwell Smith,et al.  Reflection and semantics in LISP , 1984, POPL.

[29]  David Raymond Christiansen Reflect on Your Mistakes ! Lightweight Domain-Specific Error Messages , 2014 .

[30]  Conor McBride,et al.  The view from the left , 2004, Journal of Functional Programming.

[31]  Ulf Norell,et al.  Prototyping Generic Programming in Template Haskell , 2004, MPC.

[32]  Peter Dybjer,et al.  A general formulation of simultaneous inductive-recursive definitions in type theory , 2000, Journal of Symbolic Logic.

[33]  Conor McBride,et al.  Dependently typed functional programs and their proofs , 2000 .

[34]  Adam Chlipala,et al.  Certified Programming with Dependent Types - A Pragmatic Introduction to the Coq Proof Assistant , 2013 .

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

[36]  Eli Barzilay,et al.  Implementing Reflection in Nuprl , 2005 .

[37]  S. McKay,et al.  A presentation manager based on application semantics , 1989, UIST '89.

[38]  Peter Morris,et al.  The gentle art of levitation , 2010, ICFP '10.

[39]  David Raymond Christiansen,et al.  Dependent type providers , 2013, WGP '13.

[40]  Viktor Vafeiadis,et al.  Mtac: A monad for typed tactic programming in Coq , 2015, J. Funct. Program..

[41]  Wouter Swierstra,et al.  The power of Pi , 2008, ICFP.

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

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

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

[45]  Jeremy G. Siek,et al.  Compile-time reflection and metaprogramming for Java , 2014, PEPM '14.

[46]  Paul Hudak,et al.  Building domain-specific embedded languages , 1996, CSUR.

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

[48]  Lennart Augustsson,et al.  Cayenne—a language with dependent types , 1998, ICFP '98.

[49]  Robert L. Constable,et al.  The semantics of reflected proof , 1990, [1990] Proceedings. Fifth Annual IEEE Symposium on Logic in Computer Science.

[50]  Sam Tobin-Hochstadt,et al.  Languages as libraries , 2011, PLDI '11.

[51]  Per Martin-Löf,et al.  Constructive mathematics and computer programming , 1984 .

[52]  Zoltán Horváth,et al.  Feldspar: A domain specific language for digital signal processing algorithms , 2010, Eighth ACM/IEEE International Conference on Formal Methods and Models for Codesign (MEMOCODE 2010).

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

[54]  Rance Cleaveland,et al.  Implementing mathematics with the Nuprl proof development system , 1986 .

[55]  David Raymond Christiansen Type-Directed Elaboration of Quasiquotations: A High-Level Syntax for Low-Level Reflection , 2014, IFL.

[56]  Yves Bertot,et al.  Interactive Theorem Proving and Program Development: Coq'Art The Calculus of Inductive Constructions , 2010 .

[57]  Bengt Nordström,et al.  The ALF Proof Editor and Its Proof Engine , 1994, TYPES.

[58]  J. Hickey The MetaPRL Logical Programming Environment , 2000 .

[59]  Philip Wadler,et al.  A prettier printer , 2002 .

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

[61]  Don Syme,et al.  The F# Computation Expression Zoo , 2014, PADL.

[62]  Martin Odersky,et al.  Quasiquotes for Scala , 2013 .

[63]  Jurriaan Hage,et al.  Scripting the type inference process , 2003, ACM SIGPLAN Notices.

[64]  Edwin Brady Resource-Dependent Algebraic Effects , 2014, Trends in Functional Programming.

[65]  Edwin Brady,et al.  Programming and reasoning with algebraic effects and dependent types , 2013, ICFP.

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

[67]  P. Sestoft,et al.  An Actuarial Programming Language for Life Insurance and Pensions , 2013 .

[68]  Peter Sestoft,et al.  Referential transparency, definiteness and unfoldability , 1990, Acta Informatica.

[69]  Wouter Swierstra,et al.  Engineering Proof by Reflection in Agda , 2012, IFL.

[70]  Edwin Brady,et al.  Idris, a general-purpose dependently typed programming language: Design and implementation , 2013, Journal of Functional Programming.

[71]  Koen Claessen,et al.  QuickCheck: a lightweight tool for random testing of Haskell programs , 2011, SIGP.

[72]  Shan Shan Huang,et al.  Morphing: Safely Shaping a Class in the Image of Others , 2007, ECOOP.

[73]  P. D. van der Walt,et al.  Reflection in Agda , 2012 .

[74]  Emil Axelsson,et al.  Combining Deep and Shallow Embedding for EDSL , 2012, Trends in Functional Programming.

[75]  Philip Wadler,et al.  Everything old is new again: quoted domain-specific languages , 2015, PEPM.

[76]  Todd B. Knoblock Metamathematical Extensibility in Type Theory , 1988 .

[77]  Alan Bawden,et al.  Quasiquotation in Lisp , 1999, PEPM.