Advanced Logical Type Systems for Untyped Languages
Total Page:16
File Type:pdf, Size:1020Kb
ADVANCED LOGICAL TYPE SYSTEMS FOR UNTYPED LANGUAGES Andrew M. Kent Submitted to the faculty of the University Graduate School in partial fulfillment of the requirements for the degree Doctor of Philosophy in the Department of Computer Science, Indiana University October 2019 Accepted by the Graduate Faculty, Indiana University, in partial fulfillment of the requirements for the degree of Doctor of Philosophy. Doctoral Committee Sam Tobin-Hochstadt, Ph.D. Jeremy Siek, Ph.D. Ryan Newton, Ph.D. Larry Moss, Ph.D. Date of Defense: 9/6/2019 ii Copyright © 2019 Andrew M. Kent iii To Caroline, for both putting up with all of this and helping me stay sane throughout. Words could never fully capture how grateful I am to have her in my life. iv ACKNOWLEDGEMENTS None of this would have been possible without the community of academics and friends I was lucky enough to have been surrounded by during these past years. From patiently helping me understand concepts to listening to me stumble through descriptions of half- baked ideas, I cannot thank enough my advisor and the professors and peers that donated a portion of their time to help me along this journey. v Andrew M. Kent ADVANCED LOGICAL TYPE SYSTEMS FOR UNTYPED LANGUAGES Type systems with occurrence typing—the ability to refine the type of terms in a control flow sensitive way—now exist for nearly every untyped programming language that has gained popularity. While these systems have been successful in type checking many prevalent idioms, most have focused on relatively simple verification goals and coarse interface specifications. We demonstrate that such systems are naturally suited for combination with more advanced type theoretic concepts—specifically refinement types and semantic subtyping—with both formal mathematical models and experiences reports from implementing such systems at scale. Sam Tobin-Hochstadt, Ph.D. Jeremy Siek, Ph.D. Ryan Newton, Ph.D. Larry Moss, Ph.D. vi TABLE OF CONTENTS Acknowledgements .................................. v Abstract ........................................ vi List of Figures ..................................... xi Chapter 1: Introduction and Background .................... 1 1.1 Refinement Types ................................ 2 1.2 Set-theoretic types ................................ 3 1.3 Thesis Statement and Outline .......................... 6 Chapter 2: Occurrence Typing .......................... 8 2.1 Occurrence Typing Examples .......................... 9 2.2 λOT : A Calculus for Occurrence Typing .................... 14 2.2.1 λOT Syntax ................................ 14 2.2.2 λOT Type System ............................ 16 2.2.3 λOT Subtyping .............................. 19 2.2.4 λOT Logic and Type Metafunctions . 19 2.2.5 λOT Semantics .............................. 23 2.2.6 λOT Soundness .............................. 25 2.2.7 Scaling Up λOT .............................. 26 2.2.8 Related Work in Occurrence Typing . 30 vii Chapter 3: Occurrence Typing with Refinement Types ............ 35 3.1 Beyond Occurrence Typing ........................... 37 3.1.1 Occurrence Typing with Linear Arithmetic . 37 3.1.2 Occurrence Typing with Bitvectors ................... 39 3.2 Formal Model ................................... 40 3.2.1 Syntax ................................... 40 3.2.2 Typing Rules ............................... 43 3.2.3 Subtyping and Proof System ...................... 46 3.2.4 Integrating Additional Theories ..................... 51 3.2.5 Semantics and Soundness ........................ 52 3.3 Scaling to a Real Implementation ........................ 55 3.3.1 Efficient, Algorithmic Subtyping .................... 55 3.3.2 Mutation ................................. 56 3.3.3 Type Inference and Polymorphism ................... 57 3.3.4 Complex Macros ............................. 57 3.4 Case Study: Safe Vector Access ......................... 59 3.4.1 Enriching the Math Library ....................... 60 3.5 Adding Refinements to Typed Racket ..................... 62 3.5.1 Compiling Dependent Types into Contracts . 64 3.5.2 Pay-as-you-go costs for developers ................... 65 3.5.3 Dealing with Existentials ........................ 65 3.6 Related Work ................................... 66 Chapter 4: Semantic Subtyping .......................... 69 4.1 Set-theoretic Types ................................ 69 viii 4.1.1 Subtyping ................................. 70 4.1.2 Semantic Subtyping ........................... 71 4.1.3 Deciding Inhabitation, Normal Forms . 72 4.2 Type Representation ............................... 75 4.2.1 Types as Data Structures ........................ 75 4.2.2 Base DNF Representation ........................ 77 4.2.3 Product and Function DNFs ...................... 80 4.2.4 Parsing and Example Types ....................... 84 4.3 Type Inhabitation ................................ 85 4.3.1 Deciding Type Inhabitation ....................... 86 4.4 Other Type-level Metafunctions ......................... 92 4.4.1 Product Projection ............................ 92 4.4.2 Function Domain ............................. 94 4.4.3 Function Application ........................... 95 4.5 Strategies for Testing ............................... 98 4.6 Related Work ................................... 99 4.6.1 Other Tutorials and Overviews ..................... 99 4.6.2 First-order or incomplete semantic subtyping . 100 4.6.3 Semantic subtyping with additional features . 101 4.6.4 Expressive Syntactic Subtyping . 101 Chapter 5: A Set-theoretic Foundation for Occurrence Typing ....... 103 5.1 Logical Inversion ................................. 103 5.1.1 Function Application Inversion . 105 5.1.2 Algorithm Intuition . 106 ix 5.1.3 Algorithm ................................. 107 5.1.4 Soundness and Completeness . 108 5.1.5 Efficient Implementation . 109 5.1.6 Conservative Function Application Inversion . 111 5.2 Formal Language Model ............................. 112 5.2.1 λSO Syntax ................................ 113 5.2.2 λSO Type System ............................ 115 5.2.3 λSO Semantics .............................. 119 5.2.4 λSO Soundness .............................. 119 5.2.5 Additional Language Features . 123 5.3 Semantic Numeric Tower ............................. 124 5.3.1 Semantic Types for Comparison Operators . 125 5.3.2 Semantic Types for Other Numeric Operators . 128 5.3.3 Semantic/Syntactic Function Type Comparison . 130 5.3.4 Challenges and Future Work . 132 5.4 Expressiveness .................................. 133 5.5 Related Work ................................... 136 Appendix A: Function Application Inversion Proofs ............. 138 Appendix B: Numeric Tower Types ....................... 156 Appendix C: CDuce Numeric Tower ....................... 166 References ....................................... 169 Curriculum Vitae x LIST OF FIGURES 2.1 λOT Syntax .................................... 15 2.2 λOT Typing Judgment .............................. 17 2.3 λOT Constant Type-Results ........................... 17 2.4 λOT Subtyping .................................. 20 2.5 λOT Type-related Logic Rules and Metafunctions . 21 2.6 λOT Big-step Reduction Relation ........................ 24 2.7 λOT Primitive Types ............................... 24 2.8 λOT Models Relation ............................... 25 3.1 max with refinement types ............................ 36 3.2 λRT R Syntax ................................... 41 3.3 λRT R Primitive Types .............................. 43 3.4 λRT R Typing Judgment ............................. 44 3.5 λRT R Subtyping ................................. 47 3.6 λRT R-specific Logic Rules ............................ 48 3.7 λRT R type-update metafunction ......................... 50 3.8 λRT R Big-step Reduction Relation ....................... 52 3.9 λRT R Primitive Semantics ............................ 53 3.10 λRT R Models Relation .............................. 53 xi 3.11 safe-vec-ref case study ............................ 60 4.1 Set-theoretic Types ................................ 69 4.2 Subtyping/Inhabitation Equivalence ...................... 72 4.3 Canonical form for representing types ..................... 74 4.4 Internal type representation ........................... 76 4.5 Top and bottom type representations ...................... 77 4.6 Internal type operations ............................. 78 4.7 Internal base type representation ........................ 79 4.8 Internal base DNF operations .......................... 79 4.9 Lazy BDDs for type representation ....................... 83 4.10 BDD node smart constructor .......................... 84 4.11 BDD difference and negation .......................... 85 4.12 BDD union and intersection ........................... 86 4.13 Type parsing ................................... 87 4.14 Semantic subtyping, defined in terms of type emptiness . 87 4.15 Type emptiness predicate ............................ 87 4.16 Sets of atoms ................................... 88 4.17 Product BDD inhabitation functions ...................... 89 4.18 Functions for checking if a function BDD is uninhabited . 91 4.19 Functions for projecting from a product type . 94 4.20 Domain calculation for function types ..................... 95 4.21 Function application result type calculations . 98 5.1 Function Application Inversion Examples . 106 xii 5.2 Function Application Inversion Algorithm . 108 5.3 Efficient algorithm for function application inversion. 110 5.4 Conservative Function Application Inversion Algorithm . 112