Unifying Functional Programming and Logical Reasoning in a Language Based on Mendler-Style Recursion Schemes and Term-Indexed Types
Total Page:16
File Type:pdf, Size:1020Kb
Portland State University PDXScholar Dissertations and Theses Dissertations and Theses Fall 12-16-2014 The Nax Language: Unifying Functional Programming and Logical Reasoning in a Language based on Mendler-style Recursion Schemes and Term-indexed Types Ki Yung Ahn Portland State University Follow this and additional works at: https://pdxscholar.library.pdx.edu/open_access_etds Part of the Programming Languages and Compilers Commons Let us know how access to this document benefits ou.y Recommended Citation Ahn, Ki Yung, "The Nax Language: Unifying Functional Programming and Logical Reasoning in a Language based on Mendler-style Recursion Schemes and Term-indexed Types" (2014). Dissertations and Theses. Paper 2088. https://doi.org/10.15760/etd.2086 This Dissertation is brought to you for free and open access. It has been accepted for inclusion in Dissertations and Theses by an authorized administrator of PDXScholar. Please contact us if we can make this document more accessible: [email protected]. The Nax Language : Unifying Functional Programming and Logical Reasoning in a Language based on Mendler-style Recursion Schemes and Term-indexed Types by Ki Yung Ahn A dissertation submitted in partial fulfillment of the requirements for the degree of Doctor of Philosophy in Computer Science Dissertation Committee: Tim Sheard, Chair James Hook Mark P. Jones Andrew Tolmach Douglas V. Hall Portland State University 2014 i ABSTRACT Two major applications of lambda calculi in computer science are functional programming languages and mechanized reasoning systems (or, proof assistants). According to the Curry–Howard correspondence, it is possible, in principle, to design a unified language based on a typed lambda calculus for both logical rea- soning and programming. However, the different requirements of programming languages and reasoning systems make it difficult to design such a unified language useful for both purposes. Programming languages usually extend lambda calculi with programming-friendly features (e.g., recursive datatypes, general recursion) for supporting the flexibility to model various computations, while sacrificing log- ical consistency. Logical reasoning systems usually extend lambda calculi with logic-friendly features (e.g., induction principles, dependent types) for paradox- free inference over fine-grained properties, while being more restrictive in modeling computations. In this dissertation, we design and implement a language called Nax that bal- ances between the benefits of both. Nax accepts all recursive datatypes, thus, al- lowing the same flexibility of defining recursive datatypes as functional languages. Nax supports a number of Mendler-style recursion schemes that can express various kinds of recursive computations and also grantee termination. Nax supports term- indexed types to support specifications of fine-grained properties. In addition, Nax supports a conservative extension of Hindley–Milner type inference. The theoretical contributions of this dissertation include theories for Mendler- style recursion schemes and term-indexed types, which we developed to establish strong normalization and logical consistency of Nax. ii DEDICATION To the Logos from the beginning of the universe, who was with the creator and the creator himself, the source of all beings, and the light of life shines in the darkness. iii ACKNOWLEDGMENTS I would like to thank my advisor Tim Sheard, who has always been patient and open to experimenting with new ideas, the members of the dissertation committee for their useful feedback, and special thanks to Andrew M. Pitts and Marcelo Fiore for the discussions greatetly improved my theiss research during the visit to Cambridge, UK, and also to Gabor Grief, who gave feedback on my thesis related research. Many thanks to my parents, Rev. Sam Whan Kim, and friends in Myungsung Presbyterian Church for their love and prayers from the other side of the Pacific; the young adults’ group in Oregon Eden Presbyterian Church for being a warm and dynamic community; and, to coach Ruth Aquino and friends in the Lloyd Center Ice Rink, where I chilled out in the evenings. iv Table of Contents Abstract i Dedication ii Acknowledgments iii List of Tables ix List of Figures x Part I Prelude 1 Chapter 1 Introduction 2 1.1 Programming and Formal Reasoning . 2 1.1.1 The Curry–Howard correspondence . 3 1.1.2 Logical consistency and strong normalization . 9 1.1.3 Datatypes and recursion schemes . 10 1.2 Motivation . 15 1.3 Thesis . 17 1.4 Mendler-style recursion and term-indexed types . 18 1.4.1 Restriction on recursive types for normalization . 18 1.4.2 Justification of Mendler style as a design choice. 22 1.4.3 Term-indexed types, type inference, and datatypes . 23 1.5 Contributions . 25 1.5.1 Contributions related to Mendler style . 25 1.5.2 Contributions to the theory of term-indexed types . 27 1.5.3 Contributions towards the Nax language design . 27 1.5.4 Contributions identifying open problems . 29 v 1.6 Chapter organization . 30 Chapter 2 Polymorphic type systems 35 2.1 Simply-typed lambda calculus . 35 2.1.1 Strong normalization . 38 2.1.2 Motivations for polymorphic type systems . 44 2.2 System F ................................. 45 2.2.1 Encoding datatypes in System F . 48 2.2.2 Subject reduction and strong normalization . 54 2.3 System Fω ................................ 59 2.3.1 Encodings of datatypes in System Fω . 62 2.3.2 Strong normalization . 69 2.4 The Hindley–Milner type system . 75 2.4.1 Syntax . 77 2.4.2 Declarative typing rules . 80 2.4.3 Syntax-directed typing rules . 84 2.4.4 The type inference algorithm W . 87 Part II Mendler style 90 Chapter 3 Mendler-style recursion schemes 91 3.1 Introduction . 92 3.1.1 Background - Termination and Negativity . 94 3.1.2 Historical progression . 96 3.1.3 Roadmap to a tour of the Mendler-style approach . 98 3.2 Defining regular recursive datatypes . 102 3.3 Conventional iteration for regular datatypes . 103 3.4 Mendler-style iteration for regular datatypes . 104 3.5 Mendler-style course-of-values iteration for regular datatypes . 106 3.6 Mendler-style iteration and course-of-values iteration over negative datatypes . 110 3.7 Mendler-style iteration and course-of-values iteration over non-regular datatypes and mutually recursive datatypes . 114 3.7.1 Nested datatypes . 114 3.7.2 Indexed datatypes (GADTs) . 121 vi 3.7.3 Mutually recursive datatypes . 123 3.8 Mendler-style primitive recursion (mpr) . 126 3.9 Mendler-style iteration with syntactic inverses . 131 3.9.1 Formatting HOAS . 132 3.9.2 Fω encoding ofµ ˘∗ and msfit∗ . 139 3.9.3 Evaluating Simply Typed HOAS . 142 3.9.4 A graph datatype with cycles and sharing . 145 3.9.5 Additional Mendler-style combinators . 145 3.10 Properties of recursion combinators . 148 Part III Term-Indexed Lambda Calculi 152 Chapter 4 System Fi 153 4.1 System Fi ................................156 4.1.1 Design of System Fi . 156 4.1.2 System Fi compared to System Fω . 161 4.2 Embedding datatypes and Mendler-style iterators . 164 4.2.1 Embedding datatypes using Church-encoded terms . 164 4.2.2 Embedding recursive datatypes as two-level types . 168 4.2.3 Leibniz index equality . 172 4.3 Metatheory . 174 4.3.1 Well-formedness properties and substitution lemmas . 174 4.3.2 Erasure properties . 176 4.3.3 Strong normalization and logical consistency . 183 Chapter 5 System Fixi 184 5.1 System Fixi ...............................185 5.1.1 Polarities . 190 5.1.2 Equi-recursive type operator fix . 192 5.2 Embedding datatypes and primitive recursion . 193 5.3 Embedding course-of-values recursion . 196 5.3.1 General form for the embedding of course-of-values recursion 199 5.3.2 Embedding unrollers . 200 5.3.3 Deriving uniform embeddings of the unrollers . 202 5.3.4 Properties of unrollers . 208 vii 5.4 Metatheory of Fixi . 210 5.4.1 Strong normalization and logical consistency . 210 5.4.2 Syntactic conditions for well-behaved course-of-values recur- sion . 213 Part IV Nax Language 220 Chapter 6 Introduction to Features of the Nax Language 221 6.1 Two-level types . 221 6.2 Creating values . 223 6.3 Synonyms, constructor functions, and fixpoint derivation . 223 6.4 Mendler combinators for non-indexed types . 224 6.5 Types with static indices . 230 6.6 Mendler-style combinators for indexed types . 232 6.7 Recursive types of unrestricted polarity but restricted elimination . 236 6.8 Lessons from Nax . 238 Chapter 7 Design Principles of Nax’s Type System 240 7.1 Introduction . 240 7.2 The trilingual Rosetta Stone . 242 7.2.1 Type-preserving evaluator for an expression language . 242 7.2.2 Generic Paths parametrized by a binary relation . 247 7.2.3 Stack-safe compiler for the expression language . 253 7.3 Discussion . 254 7.3.1 Universes, kinds, and well-sortedness . 254 7.3.2 Nested Term Indices and Datatypes Containing Types . 257 7.4 Related Work . 259 7.5 Summary and Future Work . 262 Chapter 8 Type Inference in Nax 263 8.1 SmallNax . 263 8.2 SmallNax with Mendler-style recursion . 269 8.2.1 A review of monomorphic recursion and polymorphic recursion270 8.2.2 Typing rules for Mendler-style recursion combinators . 270 8.3 SmallNax with GADTs . 273 8.3.1 Existential type variables . 273 viii 8.3.2 Generalized existential type variables and index transformers 275 Part V Postlude 278 Chapter 9 Related work 279 9.1 Mendler-style co-iteration and co-recursion . 279 9.2 Mendler-style recursion schemes over multiple values . 284 9.2.1 Simultaneous iteration . 284 9.2.2 Lexicographic recursion . 286 9.3 Mendler-style induction . 287 9.4 Type-based termination and sized types . 288 9.5 Logical Frameworks based on the λΠ-calculus .