UC Santa Cruz UC Santa Cruz Electronic Theses and Dissertations
Total Page:16
File Type:pdf, Size:1020Kb
UC Santa Cruz UC Santa Cruz Electronic Theses and Dissertations Title Executable Refinement Types Permalink https://escholarship.org/uc/item/92c8b5hp Author Knowles, Kenneth Lorenz Publication Date 2014 Peer reviewed|Thesis/dissertation eScholarship.org Powered by the California Digital Library University of California UNIVERSITY OF CALIFORNIA SANTA CRUZ EXECUTABLE REFINEMENT TYPES A dissertation submitted in partial satisfaction of the requirements for the degree of DOCTOR OF PHILOSOPHY in COMPUTER SCIENCE by Kenneth L. Knowles March 2014 The dissertation of Kenneth L. Knowles is approved: Professor Cormac Flanagan, Chair Professor Luca de Alfaro Professor Ranjit Jhala Tyrus Miller Vice Provost and Dean of Graduate Studies Copyright c by Kenneth L. Knowles 2014 Table of Contents List of Figures vii Abstract ix Acknowledgments xii 1 Introduction1 1.1 Specification Disciplines...........................3 1.1.1 Dynamic Contracts..........................4 1.1.2 Refinement Types..........................6 1.1.3 Extended Static Checking......................7 1.2 Thesis Overview...............................8 2 Preliminaries 13 2.1 Preparatory Readings............................ 13 2.2 The Simply-Typed λ-Calculus........................ 14 2.2.1 Syntax of STLC........................... 15 2.2.2 Operational Semantics of STLC................... 17 2.2.3 The STLC Type System....................... 19 3 Executable Refinement Types 23 3.1 The Language λH ............................... 25 3.2 Operational Semantics of λH ........................ 28 3.3 Denotation of λH types to sets of ST LC terms.............. 30 3.4 The λH Type System............................ 32 3.4.1 Typing for λH ............................ 33 3.4.2 Type well-formedness for λH .................... 34 3.4.3 Environment well-formedness for λH ................ 35 3.4.4 Subtyping for λH ........................... 36 3.4.5 Implication and Closing Substitutions............... 39 3.5 Type Soundness for λH ........................... 41 3.6 Extensional Equivalence for λH ....................... 47 iii 3.7 Related Work................................. 56 4 Hybrid Type Checking 59 HTC 4.1 The Language λH .............................. 64 HTC 4.2 Operational Semantics of λH ....................... 66 HTC 4.3 The Type System of λH .......................... 68 HTC 4.4 Hybrid Type Checking for λH ....................... 74 4.4.1 Implication Algorithm........................ 75 4.4.2 Subtyping Algorithm......................... 76 4.4.3 Cast Insertion............................. 79 4.4.4 Correctness of Cast Insertion.................... 82 4.5 An Example of HTC............................. 84 4.6 Static Checking vs. Hybrid Checking.................... 88 4.7 Related Work................................. 95 4.7.1 Static structural types, dynamic contracts............. 95 4.7.2 Optimizing implied dynamic checks................ 96 4.7.3 Dynamic typing in a statically-typed language.......... 97 5 Type Reconstruction 99 Recon 5.1 Type Reconstruction for λH ....................... 102 5.2 Constraint Generation............................ 104 Recon 5.2.1 Constraint Generation for λH terms............... 105 Recon 5.2.2 Constraint Generation for λH types............... 108 5.3 Shape Reconstruction............................ 113 5.4 Strongest Refinements............................ 120 5.4.1 Free Variable Elimination...................... 123 5.4.2 Delayed Substitution Elimination.................. 125 5.4.3 Placeholder Solution......................... 127 5.5 Type Reconstruction is Typability-Preserving............... 130 5.6 Related Work................................. 131 6 Compositional and Decidable Checking 134 6.1 Compositional Reasoning.......................... 136 6.1.1 Dependent Types Perform Non-compositional Reasoning..... 138 6.1.2 Compositional Reasoning for Executable Refinement Types... 141 6.1.3 Expressiveness and Exactness.................... 142 6.1.4 Decidable Type Checking...................... 143 Comp 6.2 The Language λH ............................. 144 Comp 6.3 Operational Semantics of λH ....................... 146 Comp 6.4 Denotation of λH types to sets of ST LC terms............. 146 Comp 6.5 The λH Type System........................... 148 Comp 6.5.1 Typing for λH ........................... 149 Comp 6.5.2 Type well-formedness for λH ................... 152 iv Comp 6.5.3 Subtyping for λH ......................... 153 6.5.4 Non-dependent Function Application................ 154 6.5.5 Self Types............................... 157 Comp 6.6 Extensional equivalence for λH ...................... 159 Comp 6.7 The λH Type System is Exact...................... 159 Comp 6.8 Type Soundness for λH .......................... 164 Comp 6.9 A Type-checking Algorithm for λH .................... 166 Comp 6.9.1 Algorithmic typing for λH .................... 169 Comp 6.9.2 Algorithmic type-wellformedness for λH ............. 172 Comp 6.9.3 Algorithmic subtyping for λH .................. 172 Comp 6.10 Example: Binary Search Trees in λH .................. 175 6.11 Related Work................................. 178 6.11.1 Indexed Types............................ 179 6.11.2 Existential Types........................... 182 6.11.3 Liquid Types............................. 184 7 Sage: An Implementation of Executable Refinement Types 185 7.1 Overview of Sage features.......................... 187 7.1.1 Type Dynamic ............................ 188 7.1.2 Executable Refinement Types.................... 189 7.1.3 First-Class Types........................... 189 7.2 Examples of Sage programs......................... 190 7.2.1 Binary Search Trees......................... 191 7.2.2 Regular Expressions......................... 195 7.2.3 Printf................................. 198 7.3 The Sage Core Language.......................... 200 7.4 Operational Semantics of Sage Core.................... 203 7.5 The Sage Core Type System........................ 208 7.5.1 Typing for Sage Core........................ 209 7.5.2 Subtyping for Sage Core...................... 213 7.5.3 Validity for Sage Core........................ 215 7.6 Hybrid Type Checking for Sage ...................... 216 7.6.1 Cast Insertion and Checking.................... 217 7.6.2 Cast Insertion and Synthesis.................... 218 7.6.3 Algorithmic Subtyping........................ 221 7.6.4 Walkthrough of hybrid type checking in Sage ........... 226 7.7 The Sage Architecture........................... 227 7.7.1 Theorem Prover........................... 227 7.7.2 Counter-Example Database..................... 229 7.8 Experimental Results............................. 230 v 8 Contemporaneous Related Work 233 8.1 Dynamic Contracts.............................. 233 8.1.1 Foundations for untyped higher-order contracts and blame.... 234 8.1.2 Higher-order contracts for lazy, typed languages......... 237 8.1.3 Parametric polymorphism in contracts............... 238 8.2 Static Contract Checking.......................... 239 8.2.1 ESC/Haskell............................. 239 8.2.2 Liquid Types............................. 240 8.2.3 Other work.............................. 242 8.3 Dependent/Refinement Types........................ 242 8.4 Gradual Typing................................ 244 9 Conclusion 248 9.1 Summary................................... 248 9.2 Open Avenues................................ 250 9.2.1 Parametric Polymorphism...................... 251 9.2.2 Multiple Representations For Specifications............ 251 9.2.3 Contract Properties......................... 252 9.2.4 Compositionality........................... 252 9.2.5 Improving hybrid type checking................... 253 9.2.6 Side Effects.............................. 253 9.2.7 Error messages, Testing, and Counterexamples.......... 254 9.2.8 Large-Scale Implementation for Empirical Work......... 254 9.3 Closing Remarks............................... 255 Bibliography 255 vi List of Figures 2.1 Syntax of STLC............................... 15 2.2 Operational Semantics of STLC....................... 17 2.3 Typing Rules for STLC........................... 20 3.1 Syntax of λH ................................. 26 3.2 Operational Semantics of λH ........................ 29 3.3 Shape of λH types and terms........................ 30 3.4 Denotation from λH types to sets STLC terms.............. 31 3.5 Typing Rules for λH ............................. 33 3.6 Type Well-formedness for λH ........................ 35 3.7 Environment Well-formedness for λH .................... 36 3.8 Subtyping for λH ............................... 37 3.9 Implication for λH .............................. 39 3.10 Extensional Equivalence Under Reduction for λH ............. 49 4.1 Behavior of hybrid type checking for various categories of programs... 61 HTC 4.2 Syntax for λH ................................ 65 HTC 4.3 Operation Semantics of λH ........................ 66 HTC 4.4 Type rules for λH .............................. 69 HTC 4.5 Algorithmic Subtyping for λH ....................... 77 HTC 4.6 Cast insertion and Checking for λH .................... 80 HTC 4.7 Cast insertion and synthesis for λH terms................ 81 HTC 4.8 Cast insertion for λH types........................ 81 Recon 5.1 Syntax for λH with type variables.................... 103 Recon 5.2 Syntax for λH constraint generation................... 106 Recon 5.3 Constraint Generation Rules for λH terms............... 107 Recon 5.4 Constraint Generation Rules for λH types................ 109 Recon 5.5 Syntax for λH shape