Practical implementation of a dependently typed functional programming language

Types express a program's meaning, and checking types ensures that a program has the intended meaning. In a dependently typed programming language types are predicated on values, leading to the possibility of expressing invariants of a program's behaviour in its type. Dependent types allow us to give more detailed meanings to programs, and hence be more confident of their correctness. This thesis considers the practical implementation of a dependently typed programming language, using the Epigram notation defined by McBride and McKinna. Epigram is a high level notation for dependently typed functional programming elaborating to a core type theory based on Lu๙s UTT, using Dybjer's inductive families and elimination rules to implement pattern matching. This gives us a rich framework for reasoning about programs. However, a naive implementation introduces several run-time overheads since the type system blurs the distinction between types and values; these overheads include the duplication of values, and the storage of redundant information and explicit proofs. A practical implementation of any programming language should be as efficient as possible; in this thesis we see how the apparent efficiency problems of dependently typed programming can be overcome and that in many cases the richer type information allows us to apply optimisations which are not directly available in traditional languages. I introduce three storage optimisations on inductive families; forcing, detagging and collapsing. I further introduce a compilation scheme from the core type theory to G-machine code, including a pattern matching compiler for elimination rules and a compilation scheme for efficient run-time implementation of Peano's natural numbers. We also see some low level optimisations for removal of identity functions, unused arguments and impossible case branches. As a result, we see that a dependent type theory is an effective base on which to build a feasible programming language.

[1]  Simon Peyton Jones,et al.  Wobbly types: type inference for generalised algebraic data types∗ , 2004 .

[2]  Simon L. Peyton Jones,et al.  A modular fully‐lazy lambda lifter in Haskell , 1991, Softw. Pract. Exp..

[3]  Thomas Johnsson,et al.  Lambda Lifting: Treansforming Programs to Recursive Equations , 1985, FPCA.

[4]  Venanzio Capretta,et al.  Nested General Recursion and Partiality in Type Theory , 2001, TPHOLs.

[5]  S. Jones,et al.  A Transformation-Based Optimiser for Haskell , 1998, Sci. Comput. Program..

[6]  Gérard P. Huet,et al.  The Constructive Engine , 1989, A Perspective in Theoretical Computer Science.

[7]  Lennart Augustsson,et al.  A compiler for lazy ML , 1984, LFP '84.

[8]  Frank Pfenning,et al.  Eliminating array bound checking through dependent types , 1998, PLDI.

[9]  Simon L. Peyton Jones,et al.  Secrets of the Glasgow Haskell Compiler inliner , 2002, Journal of Functional Programming.

[10]  Conor McBride,et al.  Elimination with a Motive , 2000, TYPES.

[11]  Thierry Coquand,et al.  An Algorithm for Type-Checking Dependent Types , 1996, Sci. Comput. Program..

[12]  Richard J. Boulton,et al.  An ML editor based on proofs-as-programs , 1999, 14th IEEE International Conference on Automated Software Engineering.

[13]  Joyce L. Vedral,et al.  Functional Programming Languages and Computer Architecture , 1989, Lecture Notes in Computer Science.

[14]  Simon L. Peyton Jones,et al.  Optimistic evaluation: an adaptive evaluation strategy for non-strict programs , 2003, ICFP '03.

[15]  J. H. Geuvers Logics and type systems , 1993 .

[16]  Benjamin Grégoire,et al.  A compiled implementation of strong reduction , 2002, ICFP '02.

[17]  Philip Wadler,et al.  Views: a way for pattern matching to cohabit with data abstraction , 1987, POPL '87.

[18]  Simon L. Peyton Jones,et al.  The Implementation of Functional Programming Languages , 1987 .

[19]  Lena Magnusson,et al.  The implementation of ALF : a proof editor based on Martin-Löf's monomorphic type theory with explicit substitution , 1994 .

[20]  Eduardo Giménez,et al.  Codifying Guarded Definitions with Recursive Schemes , 1994, TYPES.

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

[22]  C. Barry Jay,et al.  Shape in computing , 1996, CSUR.

[23]  John McCarthy,et al.  Recursive functions of symbolic expressions and their computation by machine, Part I , 1960, Commun. ACM.

[24]  Per Martin-Löf,et al.  Intuitionistic type theory , 1984, Studies in proof theory.

[25]  Peyton Jones,et al.  Haskell 98 language and libraries : the revised report , 2003 .

[26]  Koen Claessen,et al.  QuickCheck: a lightweight tool for random testing of Haskell programs , 2000, ICFP.

[27]  Alfred V. Aho,et al.  Compilers: Principles, Techniques, and Tools , 1986, Addison-Wesley series in computer science / World student series edition.

[28]  Robert Harper,et al.  Type Checking with Universes , 1991, Theor. Comput. Sci..

[29]  Healfdene Goguen A typed operational semantics for type theory , 1994 .

[30]  Luca Cardelli,et al.  Phase Distinctions in Type Theory , 1988 .

[31]  Nicolas Magaud,et al.  Changing Data Structures in Type Theory: A Study of Natural Numbers , 2000, TYPES.

[32]  Hongwei Xi,et al.  Dead Code Elimination through Dependent Types , 1999, PADL.

[33]  Magnus Carlsson,et al.  An exercise in dependent types: A well-typed interpreter , 1999 .

[34]  Frank Pfenning,et al.  Dependent types in practical programming , 1999, POPL '99.

[35]  Simon L. Peyton Jones,et al.  Pattern Guards and Transformational Patterns , 2000, Electron. Notes Theor. Comput. Sci..

[36]  Simon L. Peyton Jones,et al.  Strictness Analysis - A Practical Approach , 1985, FPCA.

[37]  Andrew W. Appel,et al.  Compiling with Continuations , 1991 .

[38]  Ulrich Berger,et al.  An inverse of the evaluation functional for typed lambda -calculus , 1991, [1991] Proceedings Sixth Annual IEEE Symposium on Logic in Computer Science.

[39]  Thierry Coquand,et al.  Pattern Matching with Dependent Types , 1992 .

[40]  Andrzej Filinski,et al.  Normalization by Evaluation for the Computational Lambda-Calculus , 2001, TLCA.

[41]  Jacek Chrzaszcz,et al.  Modules in Coq Are and Will Be Correct , 2003, TYPES.

[42]  David Thomas,et al.  The Art in Computer Programming , 2001 .

[43]  Conor McBride,et al.  Inductive Families Need Not Store Their Indices , 2003, TYPES.

[44]  Helmut Schwichtenberg,et al.  Normalisation by Evaluation , 1998, Prospects for Hardware Foundations.

[45]  Pierre Letouzey,et al.  A New Extraction for Coq , 2002, TYPES.

[46]  Simon L. Peyton Jones,et al.  Implementing lazy functional languages on stock hardware: the Spineless Tagless G-machine , 1992, Journal of Functional Programming.

[47]  André L. M. Santos,et al.  Compilation by transformation in non-strict functional languages , 1995 .

[48]  Simon L. Peyton Jones,et al.  C-: A Portable Assembly Language , 1997, Implementation of Functional Languages.

[49]  Lennart Augustsson,et al.  Compiling Pattern Matching , 1985, FPCA.

[50]  Thomas Johnsson,et al.  The GRIN Project: A Highly Optimising Back End for Lazy Functional Languages , 1996, Implementation of Functional Languages.

[51]  de Ng Dick Bruijn,et al.  Telescopic Mappings in Typed Lambda Calculus , 1991, Inf. Comput..

[52]  Niklas Röjemo,et al.  Highlights from nhc—a space-efficient Haskell compiler , 1995, FPCA '95.

[53]  Harold T. Hodes,et al.  The | lambda-Calculus. , 1988 .

[54]  Yanhong A. Liu,et al.  From recursion to iteration: what are the optimizations? , 1999, PEPM '00.

[55]  Olga Caprotti,et al.  How to formally and efficiently prove prime (2999) , 2000 .

[56]  Simon Peyton Jones,et al.  Implementing Functional Languages: a tutorial , 2000 .

[57]  Thorsten Altenkirch,et al.  Constructions, inductive types and strong normalization , 1993, CST.

[58]  A. Bove Mutual General Recursion in Type Theory , 2002 .

[59]  Mayer Goldberg An adequate and efficient left-associated binary numeral system in the λ-calculus , 2000 .

[60]  Donald E. Knuth,et al.  The art of computer programming. Vol.2: Seminumerical algorithms , 1981 .

[61]  Nicolas Magaud,et al.  A Proof of GMP Square Root , 2004, Journal of Automated Reasoning.

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

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

[64]  A. Church The calculi of lambda-conversion , 1941 .

[65]  Simon L. Peyton Jones,et al.  C--: A Portable Assembly Language that Supports Garbage Collection , 1999, PPDP.

[66]  Henk Barendregt,et al.  The Lambda Calculus: Its Syntax and Semantics , 1985 .

[67]  Urban Boquist,et al.  Code Optimisation Techniques for Lazy Functional Languages , 1999 .

[68]  Ana Bove,et al.  General Recursion in Type Theory , 2002, TYPES.

[69]  Daniel Fridlender,et al.  Do we need dependent types? , 2000, Journal of Functional Programming.

[70]  Haskell B. Curry,et al.  Combinatory Logic, Volume I , 1959 .

[71]  R. J. M. Hughes,et al.  Design and Implementation of Programming Languages , 1977, Lecture Notes in Computer Science.

[72]  Peter Aczel,et al.  An Introduction to Inductive Definitions , 1977 .

[73]  Martin Hofmann,et al.  Categorical Reconstruction of a Reduction Free Normalization Proof , 1995, Category Theory and Computer Science.

[74]  John C. Mitchell,et al.  Concepts in programming languages , 2002 .

[75]  Conor McBride,et al.  Epigram: Practical Programming with Dependent Types , 2004, Advanced Functional Programming.

[76]  Stefano Berardi,et al.  Pruning Simply Typed Lambda-Terms , 1996, J. Log. Comput..

[77]  D. A. Turner,et al.  A new implementation technique for applicative languages , 1979, Softw. Pract. Exp..

[78]  Christine Paulin-Mohring Extraction de programmes dans le Calcul des Constructions. (Program Extraction in the Calculus of Constructions) , 1989 .

[79]  D. A. Turner,et al.  Elementary Strong Functional Programming , 1995, FPLE.

[80]  David R. Musser,et al.  STL tutorial and reference guide - C++ programming with the standard template library , 1996, Addison-Wesley professional computing series.

[81]  P. J. Landin The Mechanical Evaluation of Expressions , 1964, Comput. J..

[82]  Lawrence Charles Paulson,et al.  Isabelle/HOL: A Proof Assistant for Higher-Order Logic , 2002 .

[83]  Thomas Johnsson,et al.  Parallel graph reduction with the (v , G)-machine , 1989, FPCA.

[84]  Norman Ramsey,et al.  When Do Match-compilation Heuristics Matter? , 2000 .

[85]  William A. Howard,et al.  The formulae-as-types notion of construction , 1969 .

[86]  Luc Maranget,et al.  Optimizing pattern matching , 2001, ICFP '01.

[87]  Conor McBride,et al.  Functional pearl: i am not a number--i am a free variable , 2004, Haskell '04.

[88]  Andrzej Filinski,et al.  A Semantic Account of Type-Directed Partial Evaluation , 1999, PPDP.

[89]  Olivier Danvy Functional Unparsing , 1998, J. Funct. Program..

[90]  P. J. Landin,et al.  The next 700 programming languages , 1966, CACM.

[91]  Tommy Färnqvist Number Theory Meets Cache Locality – Efficient Implementation of a Small Prime FFT for the GNU Multiple Precision Arithmetic Library , 2005 .

[92]  Ralf Hinze,et al.  Fun with phantom types , 2003 .

[93]  Zhaohui Luo,et al.  Implementation Techniques for Inductive Types in Plastic , 1999, TYPES.

[94]  Philip Wadler,et al.  Deforestation: Transforming Programs to Eliminate Trees , 1990, Theor. Comput. Sci..

[95]  Thérèse Hardin,et al.  Functional back-ends within the lambda-sigma calculus , 1996, ICFP '96.

[96]  Simon L. Peyton Jones,et al.  Unboxed Values as First Class Citizens in a Non-Strict Functional Language , 1991, FPCA.

[97]  Zhaohui Luo,et al.  Computation and reasoning - a type theory for computer science , 1994, International series of monographs on computer science.

[98]  Philip Wadler,et al.  Listlessness is better than laziness: Lazy evaluation and garbage collection at compile-time , 1984, LFP '84.

[99]  P. Martin-Löf An Intuitionistic Theory of Types: Predicative Part , 1975 .

[100]  Chris Okasaki,et al.  Red-black trees in a functional setting , 1999, Journal of Functional Programming.

[101]  Anatolij A. Karatsuba,et al.  Multiplication of Multidigit Numbers on Automata , 1963 .

[102]  Thomas Johnsson Efficient compilation of lazy evaluation , 1984, SIGP.

[103]  Will Partain,et al.  The nofib Benchmark Suite of Haskell Programs , 1992, Functional Programming.

[104]  Jon Fairbairn,et al.  TIM: A simple, lazy abstract machine to execute supercombinatorics , 1987, FPCA.

[105]  Marinus J. Plasmeijer,et al.  Generating Efficient Code for Lazy Functional Languages , 1991, FPCA.

[106]  Neil Ghani,et al.  The virtues of eta-expansion , 1995, Journal of Functional Programming.

[107]  Hongwei Xi,et al.  Dependently Typed Data Structures , 2000 .

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

[109]  Andres Löh,et al.  Generic Haskell, Specifically , 2002, Generic Programming.

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

[111]  Robert Harper,et al.  Compiling polymorphism using intensional type analysis , 1995, POPL '95.

[112]  Rod M. Burstall,et al.  Proving Properties of Programs by Structural Induction , 1969, Comput. J..

[113]  Conor McBride Faking it: Simulating dependent types in Haskell , 2002, J. Funct. Program..

[114]  Zhaohui Luo,et al.  An Implementation of LF with Coercive Subtyping & Universes , 2001, Journal of Automated Reasoning.

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

[116]  Mark P. Jones,et al.  The implementation of the Gofer functional programming system , 1994 .