Final year project Bertus: Implementing Observational Equality Among this systems, Intuitionistic...

download Final year project Bertus: Implementing Observational Equality Among this systems, Intuitionistic Type

of 87

  • date post

    15-Mar-2020
  • Category

    Documents

  • view

    1
  • download

    0

Embed Size (px)

Transcript of Final year project Bertus: Implementing Observational Equality Among this systems, Intuitionistic...

  • Final year project

    Bertus: Implementing Observational Equality

    Francesco Mazzoli

    Supervisor: Dr. Steffen van Bakel

    Second marker: Dr. Philippa Gardner

    July 1, 2013

    mailto:fm2209@ic.ac.uk

  • Abstract

    The marriage between programming and logic has been a fertile one. In particular, since the definition of the simply typed λ-calculus, a number of type systems have been devised with increasing expressive power.

    Among this systems, Intuitionistic Type Theory (ITT) has been a popular framework for theorem provers and programming languages. However, reasoning about equality has always been a tricky business in ITT and related theories. In this thesis we shall explain why this is the case, and present Observational Type Theory (OTT), a solution to some of the problems with equality.

    To bring OTT closer to the current practice of interactive theorem provers, we de- scribe Bertus, a system featuring OTT in a setting more close to the one found in widely used provers such as Agda and Coq. Most notably, we feature user defined inductive and record types and a cumulative, implicit type hierarchy. Having imple- mented part of Bertus as a Haskell program, we describe some of the implementation issues faced.

  • Acknowledgements

    I would like to thank Steffen van Bakel, my supervisor, who was brave enough to believe in my project and who provided support and invaluable advice.

    I would also like to thank the Haskell and Agda community on IRC, which guided me through the strange world of types; and in particular Andrea Vezzosi and James Deikun, with whom I entertained countless insightful discussions over the past year. Andrea suggested Observational Type Theory as a topic of study: this thesis would not exist without him. Before them, Tony Field introduced me to Haskell, unknowingly filling most of my free time from that time on.

    Finally, most of the work stems from the research of Conor McBride, who answered many of my doubts through these months. I also owe him the colours.

  • Contents

    1 Introduction 1 1.1 Structure . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 1.2 Contributions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2 1.3 Notation and syntax . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3

    2 Simple and not-so-simple types 4 2.1 The untyped λ-calculus . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4 2.2 The simply typed λ-calculus . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5 2.3 The Curry-Howard correspondence . . . . . . . . . . . . . . . . . . . . . . . . 6 2.4 Inductive data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8

    3 Intuitionistic Type Theory 9 3.1 Extending the STLC . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9 3.2 A Bit of History . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10 3.3 A simple type theory . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10

    3.3.1 Types are terms, some terms are types . . . . . . . . . . . . . . . . . . 10 3.3.2 Contexts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11 3.3.3 Unit, Empty . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12 3.3.4 Bool, and dependent if . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12 3.3.5 →, or dependent function . . . . . . . . . . . . . . . . . . . . . . . . . . 13 3.3.6 ×, or dependent product . . . . . . . . . . . . . . . . . . . . . . . . . . 13 3.3.7 W, or well-order . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14

    4 The struggle for equality 16 4.1 Propositional equality . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16 4.2 Common extensions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17

    4.2.1 η-expansion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17 4.2.2 Uniqueness of identity proofs . . . . . . . . . . . . . . . . . . . . . . . 17

    4.3 Limitations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18 4.4 Equality reflection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19

    5 The observational approach 21 5.1 A simpler theory, a propositional fragment . . . . . . . . . . . . . . . . . . . . 21 5.2 Equality proofs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22

    5.2.1 Type equality, and coercions . . . . . . . . . . . . . . . . . . . . . . . . 23 5.2.2 coe, laziness, and coherence . . . . . . . . . . . . . . . . . . . . . . . . 24 5.2.3 Value-level equality . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25

    5.3 Proof irrelevance and stuck coercions . . . . . . . . . . . . . . . . . . . . . . . 25

    6 Bertus: the theory 26 6.1 Bidirectional type checking . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26 6.2 Base terms and types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27 6.3 Elaboration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28

    6.3.1 Term vectors, telescopes, and assorted notation . . . . . . . . . . . . . 28 6.3.2 Declarations syntax . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29 6.3.3 User defined types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29 6.3.4 Why user defined types? Why eliminators? . . . . . . . . . . . . . . . 35

    6.4 Cumulative hierarchy and typical ambiguity . . . . . . . . . . . . . . . . . . . 35 6.5 Observational equality, Bertus style . . . . . . . . . . . . . . . . . . . . . . . . 37

  • 6.5.1 The Bertus prelude, and Propositions . . . . . . . . . . . . . . . . . . . 37 6.5.2 Some OTT examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38 6.5.3 Only one equality . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39 6.5.4 Coercions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41 6.5.5 Prop and the hierarchy . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42 6.5.6 Quotation and definitional equality . . . . . . . . . . . . . . . . . . . . 43 6.5.7 Why Prop? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43

    7 Bertus: the practice 45 7.1 Syntax . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46 7.2 Term representation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47

    7.2.1 Naming and substituting . . . . . . . . . . . . . . . . . . . . . . . . . . 47 7.2.2 Evaluation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51 7.2.3 Parameterize everything! . . . . . . . . . . . . . . . . . . . . . . . . . . 51

    7.3 Turning a hierarchy into some graphs . . . . . . . . . . . . . . . . . . . . . . . 53 7.4 (Web) REPL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 54

    8 Evaluation 56 8.1 A type holes tutorial . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 56

    9 Future work 61

    A Notation and syntax 64

    B Code 66 B.1 ITT renditions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 66

    B.1.1 Agda . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 66 B.1.2 Bertus . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 69

    B.2 Bertus examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 70 B.3 Bertus’ hierachy . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 71 B.4 Term representation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 72 B.5 Graph and constraints modules . . . . . . . . . . . . . . . . . . . . . . . . . . . 73

    B.5.1 Data.LGraph . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73 B.5.2 Data.Constraint . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 74

    C Addendum 75

    Bibliography 77

  • 1 | Introduction Functional programming is in good shape. In particular the ‘well-typed’ line of work orig- inating from Milner’s ML has been extremely fruitful, in various directions. Nowadays functional, well-typed programming languages like Haskell or OCaml are slowly being absorbed by the mainstream. An important related development—and in fact the origi- nal motivator for ML’s existence—is the advancement of the practice of interactive theorem provers.

    An interactive theorem prover, or proof assistant, is a tool that lets the user develop formal proofs with the confidence of the machine checking them for correctness. While the effort towards a full formalisation of mathematics has been ongoing for more than a century, theorem provers have been the first class of software whose implementation depends directly on these theories.

    In a fortunate turn of events, it was discovered that well-typed functional programming and proving theorems in an intuitionistic logic are the same activity. Under this discipline, the types in our programming language can be interpreted as proposition in our logic; and the programs implementing the specification given by the types as their proofs. This fact stimulated an active transfer of techniques and knowledge between logic and programming language theory, in both directions.

    Mathematics could provide programming with a wealth of abstractions and constructs developed over centuries. Moreover, identifying our types with a logic lets us focus on foundational questions regarding programming with a much more solid approach, given the years of rigorous study of logic. Programmers, on th