practical implementation of a dependently typed functional

Download Practical Implementation of a Dependently Typed Functional

Post on 10-Feb-2017

223 views

Category:

Documents

0 download

Embed Size (px)

TRANSCRIPT

  • Practical Implementation of a Dependently Typed FunctionalProgramming Language

    by

    Edwin C. Brady

    Submitted in conformity with the requirementsfor the degree of PhD

    Department of Computer ScienceUniversity of Durham

    Copyright c 2005 by Edwin C. Brady

  • Abstract

    Practical Implementation of a Dependently Typed Functional Programming

    Language

    Edwin C. Brady

    Types express a programs meaning, and checking types ensures that a program has theintended meaning. In a dependently typed programming language types are predicated onvalues, leading to the possibility of expressing invariants of a programs behaviour in itstype. Dependent types allow us to give more detailed meanings to programs, and hence bemore confident of their correctness.

    This thesis considers the practical implementation of a dependently typed programminglanguage, using the Epigram notation defined by McBride and McKinna. Epigram isa high level notation for dependently typed functional programming elaborating to a coretype theory based on Luos UTT, using Dybjers inductive families and elimination rules toimplement pattern matching. This gives us a rich framework for reasoning about programs.However, a nave implementation introduces several run-time overheads since the type sys-tem blurs the distinction between types and values; these overheads include the duplicationof values, and the storage of redundant information and explicit proofs.

    A practical implementation of any programming language should be as efficient as pos-sible; in this thesis we see how the apparent efficiency problems of dependently typed pro-gramming can be overcome and that in many cases the richer type information allows usto apply optimisations which are not directly available in traditional languages. I introducethree storage optimisations on inductive families; forcing, detagging and collapsing. I furtherintroduce a compilation scheme from the core type theory to G-machine code, including apattern matching compiler for elimination rules and a compilation scheme for efficient run-time implementation of Peanos natural numbers. We also see some low level optimisationsfor removal of identity functions, unused arguments and impossible case branches. As aresult, we see that a dependent type theory is an effective base on which to build a feasibleprogramming language.

    ii

  • Acknowledgements

    My thanks to my supervisors, James McKinna and Zhaohui Luo. James made the originalsuggestion for a thesis topic and has constantly provided advice and feedback. Zhaohuisenthusiasm and knowledge has been an inspiration, and I would like to thank both for theirfeedback on previous drafts of this thesis.

    I would also like to thank the other members of the Computer Assisted Reasoning Group,in particular Conor McBride. Conor taught me a lot of what I know about type theory andintroduced several implementation techniques to me. I also owe him thanks for his usefulcomments on an earlier draft of this thesis, and a collection of LATEX macros which madewriting it marginally less painful!

    My office mates, successively Yong Luo, Paul Townend, Simon Pears, David Johnstone,Robert Kiessling, and Chris Lindop helped to provide a friendly environment in which towork, and I thank them. Thank you also to the Computing Society, the Go Club andUstinov College Cricket Club for providing distractions when I needed them.

    Finally, I would like to thank my family for their love and encouragement, and Jenny forher support and limitless patience over the last few months.

    iii

  • Contents

    1 Introduction 1

    1.1 Types in Programming . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21.2 Dependent Types in Programming . . . . . . . . . . . . . . . . . . . . . . . . 3

    1.2.1 Cayenne . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31.2.2 DML . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41.2.3 Inductive Families and Epigram . . . . . . . . . . . . . . . . . . . . . 51.2.4 Benefits of Dependent Types . . . . . . . . . . . . . . . . . . . . . . . 71.2.5 Strong Normalisation . . . . . . . . . . . . . . . . . . . . . . . . . . . 10

    1.3 Contributions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 111.4 Related Work . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 121.5 Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14

    1.5.1 System Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 141.5.2 Chapter Outline . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 161.5.3 Implementation Note . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17

    2 Epigram and its Core Type Theory 19

    2.1 TT The Core Type Theory . . . . . . . . . . . . . . . . . . . . . . . . . . . 192.1.1 The Core Language . . . . . . . . . . . . . . . . . . . . . . . . . . . . 202.1.2 Inductive Datatypes . . . . . . . . . . . . . . . . . . . . . . . . . . . . 232.1.3 Elimination Rules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 262.1.4 Equality . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 282.1.5 Properties of TT . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 302.1.6 Universe Levels and Cumulativity . . . . . . . . . . . . . . . . . . . . 312.1.7 TT Examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 322.1.8 Labelled Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33

    2.2 Programming in Epigram . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 342.2.1 Basic Notation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 342.2.2 Programming with Elimination Rules . . . . . . . . . . . . . . . . . . 362.2.3 Impossible Cases . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 372.2.4 Example Vector lookup . . . . . . . . . . . . . . . . . . . . . . . . . 38

    iv

  • CONTENTS v

    2.2.5 Alternative Elimination Rules . . . . . . . . . . . . . . . . . . . . . . . 402.2.6 Derived Eliminators and Memoisation . . . . . . . . . . . . . . . . . . 422.2.7 Matching on Intermediate Values . . . . . . . . . . . . . . . . . . . . . 44

    2.3 Programming Idioms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 452.3.1 Dependent Pairs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 452.3.2 Induction Over Proofs . . . . . . . . . . . . . . . . . . . . . . . . . . . 472.3.3 Views . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 472.3.4 Termination . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 50

    2.4 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53

    3 Compiling ExTT 55

    3.1 Execution Environments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 563.1.1 Normalisation by Evaluation . . . . . . . . . . . . . . . . . . . . . . . 563.1.2 Compilation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 573.1.3 Program Extraction . . . . . . . . . . . . . . . . . . . . . . . . . . . . 593.1.4 Execution of Epigram . . . . . . . . . . . . . . . . . . . . . . . . . . 60

    3.2 The Run-Time Language RunTT . . . . . . . . . . . . . . . . . . . . . . . . . 613.2.1 Supercombinators and Lambda Lifting . . . . . . . . . . . . . . . . . . 613.2.2 RunTT Syntax . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61

    3.3 Translating Function Definitions to RunTT . . . . . . . . . . . . . . . . . . . 633.3.1 Grouping -abstractions . . . . . . . . . . . . . . . . . . . . . . . . . . 633.3.2 Lambda Lifting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 643.3.3 Tidying up . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 653.3.4 Arity . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65

    3.4 Translating Elimination Rules to RunTT . . . . . . . . . . . . . . . . . . . . . 663.5 The G-machine . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 67

    3.5.1 Graph Representation . . . . . . . . . . . . . . . . . . . . . . . . . . . 673.5.2 Machine State . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 683.5.3 Informal Semantics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 693.5.4 Operational Semantics . . . . . . . . . . . . . . . . . . . . . . . . . . . 703.5.5 Translation Scheme . . . . . . . . . . . . . . . . . . . . . . . . . . . . 713.5.6 Example plus and N-Elim . . . . . . . . . . . . . . . . . . . . . . 723.5.7 Implementing a G-machine Compiler With Dependent Types . . . . . 72

    3.6 Proper Tail Recursion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 753.7 Run-time Considerations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 76

    3.7.1 Invariants of Inductive Families . . . . . . . . . . . . . . . . . . . . . . 783.7.2 Proofs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 793.7.3 Number Representation . . . . . . . . . . . . . . . . . . . . . . . . . . 803.7.4 Dead Code In Impossible Cases . . . . . . . . . . . . . . . . . . . . . . 813.7.5 Intermediate Data Structures . . . . . . . . . . . . . . . . . . . . . . . 82

  • CONTENTS vi

    3.8 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 82

    4 Optimising Inductive Families 83

    4.1 Elimination Rules and Their Implementation . . . . . . . . . . . . . . . . . . 84

    4.1.1 Form of Elimination Rules . . . . . . . . . . . . . . . . . . . . . . . . . 84

    4.1.2 Pattern Syntax and its Run-Time Semantics . . . . . . . . . . . . . . 86

    4.1.3 Standard Implementation . . . . . . . . . . . . . . . . . . . . . . . . . 88

    4.1.4 Alternative Implementations . . . . . . . . . . .

Recommended

View more >