Practical Type Inference for the GADT Type System
Total Page:16
File Type:pdf, Size:1020Kb
View metadata, citation and similar papers at core.ac.uk brought to you by CORE provided by PDXScholar Portland State University PDXScholar Dissertations and Theses Dissertations and Theses 1-1-2010 Practical Type Inference for the GADT Type System Chuan-kai Lin Portland State University Follow this and additional works at: https://pdxscholar.library.pdx.edu/open_access_etds Let us know how access to this document benefits ou.y Recommended Citation Lin, Chuan-kai, "Practical Type Inference for the GADT Type System" (2010). Dissertations and Theses. Paper 367. 10.15760/etd.367 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. For more information, please contact [email protected]. Practical Type Inference for the GADT Type System by Chuan-kai Lin A dissertation submitted in partial fulfillment of the requirements for the degree of Doctor of Philosophy in Computer Science Dissertation Committee: Tim Sheard, Chair Andrew P. Black James Hook Mark P. Jones Andrew Tolmach Douglas V. Hall Portland State University © 2010 i ABSTRACT Generalized algebraic data types (GADTs) are a type system extension to alge- braic data types that allows the type of an algebraic data value to vary with its shape. The GADT type system allows programmers to express detailed program properties as types (for example, that a function should return a list of the same length as its input), and a general-purpose type checker will automatically check those properties at compile time. Type inference for the GADT type system and the properties of the type system are both currently areas of active research. In this dissertation, I attack both problems simultaneously by exploiting the symbiosis between type system research and type inference research. Deficiencies of GADT type inference algorithms motivate research on specific aspects of the type system, and discoveries about the type system bring in new insights that lead to improved GADT type inference algorithms. The technical contributions of this dissertation are therefore twofold: in addition to new GADT type system properties (such as the prevalence of pointwise type information flow in GADT patterns, a generalized notion of existential types, and the effects of enforcing the GADT branch reachability requirement), I will also present a new GADT type inference algorithm that is significantly more powerful than existing algorithms. These contributions should help programmers use the GADT type system more effectively, and they should also enable language implementers to provide better support for the GADT type system. ii DEDICATION To my wife Hwa-yi, Who, like Powdermilk Biscuits, Gives me the strength to get up And do what needs to be done. iii ACKNOWLEDGMENTS The work I present in this dissertation is entirely my own, but I could not have done it without the assistance and support of many others. I would like to take this opportunity to express my everlasting gratitude. Tim Sheard is a wonderful advisor. He believes that students should be free to pursue their own interests, and his support was invaluable during the difficult period when I kept digging and nothing turned up. We do not always agree on technical issues, but, after giving me his honest opinion, he always let me take the research in the direction of my choosing. He has a real talent for identifying important ideas, and we spent many hours sitting in his office trying to find the key sentence that was left unsaid in my confused writing. He worked as hard as I did in the months leading up to my defense, reading and marking up drafts at an incredible pace. I hope the end result makes him proud. Andrew P. Black took me under his wings when I first started my graduate studies and taught me how to do research. I still remember when he told me \Chuan-kai, you stuttered because you put too many words on your slides, and you were having trouble finding different words to say the same things." That was quite a revelation. The members of my dissertation committee | James Hook, Mark P. Jones, and Andrew Tolmach | worked diligently to help me improve this dissertation. Aaron Stump and Dimitrios Vytiniotis also provided useful feedback on a draft. This dissertation would certainly be a lot poorer without their insight. iv I feel extremely lucky to have shared a cubicle with Emerson Murphy-Hill during pretty much the entirety of his doctoral studies. His can-do spirit and un- orthodox wit are positively contagious, and his generous and engaging personality inspired me to be a better person. I am proud and honored to call Emerson my friend, and I spend much time wondering how much better the world would be if only there are more people like him. I met Nancy Murphy when Emerson invited me to his family Thanksgiving gathering in 2004. She treats me like her own child, and her home becomes my favorite sanctuary whenever I feel weary and discouraged. She gave me a family in this foreign land, which, because of her, is now a much less foreign place with very many happy memories. I am indebted to my parents in many different ways. Though they rarely knew what I was up to (or so I thought), they always supported my interests and my decisions without reservation. It was only with their support that I had an opportunity to develop my interests in computer science and to pursue graduate studies in the United States. Thanks Mom and Dad. I love you. Above all, I want to thank my wife Hwa-yi Tang for her love, sacrifice, and encouragement. Maintaining a long-distance relationship takes a lot of hard work, but she took it in stride because she knows how much I love research. She makes my life complete and gives me the strength to do what needs to be done. Academic research relies on the support of a stable, mature, and resourceful society, and I feel privileged to enjoy such support. Many people before me have worked hard to make the world a better place, and, one way or the other, they all contributed to the birth of this dissertation. Seven years. It was an amazing adventure, and I treasure every moment of it. v CONTENTS Abstract ::::::::::::::::::::::::::::::::::::: i Dedication :::::::::::::::::::::::::::::::::::: ii Acknowledgments ::::::::::::::::::::::::::::::: iii List of Figures ::::::::::::::::::::::::::::::::: ix 1 Introduction ::::::::::::::::::::::::::::::::: 1 1.1 Type Systems . .1 1.2 Type System Incompleteness . .5 1.3 Generalized Algebraic Data Types . 10 1.4 Type Inference . 12 1.5 Technical Challenges . 14 1.6 Contributions . 15 1.7 Outline . 16 2 Background ::::::::::::::::::::::::::::::::: 19 2.1 Notation and Syntax . 19 2.2 Algebraic Data Types . 22 2.2.1 Constructing algebraic data . 23 2.2.2 Destructing algebraic data . 25 2.2.3 Type system . 26 2.3 Generalized Algebraic Data Types . 30 2.3.1 Previous work . 31 2.3.2 Features . 33 2.3.3 Type system . 37 2.4 Inference for the GADT Type System . 40 2.4.1 Technical challenges . 40 2.4.2 Computational decidability . 43 2.4.3 Previous work . 44 vi 2.5 Types in GADT Pattern-Matching Branches . 48 2.5.1 Identifying attributes . 49 2.5.2 Decomposing ALT-GADT . 50 2.5.3 Symmetry in type roles . 52 3 The Pointwise GADT Type System :::::::::::::::::: 57 3.1 Types in GADT Patterns . 57 3.1.1 Pointwise type information flow . 57 3.1.2 Unrestricted type information flow . 61 3.2 Type System . 66 3.2.1 Pointwise unifiers . 66 3.2.2 Pointwise unification . 74 3.2.3 Formal definition . 79 3.3 Expressiveness . 81 3.3.1 Case studies . 81 3.3.2 Rejected programs . 83 3.3.3 Workarounds . 89 3.4 Summary . 90 4 The Non-Dependent GADT Type System :::::::::::::: 92 4.1 GADT without Type Refinements . 92 4.1.1 Examples . 93 4.1.2 Type inference testing . 95 4.1.3 Type system . 98 4.2 Generalizing Existential Types . 101 4.2.1 Existential types . 102 4.2.2 Generalized existential types . 103 4.2.3 Inferring scrutinee types . 113 4.3 Summary . 117 5 Simplifications to Complications :::::::::::::::::::: 119 5.1 GADT Branch Reachability Requirement . 120 5.1.1 GADT and branch reachability . 120 5.1.2 Restricting reachability enforcement . 124 5.1.3 Consequences of enforcing reachability . 128 5.1.4 Type inference . 130 5.2 Pointwise Branch Reachability . 133 vii 5.2.1 Not pointwise, but unifiable . 133 5.2.2 Interaction with let expressions . 138 5.2.3 Type inference . 142 5.3 Nested Non-Dependent GADT Branches . 143 5.4 Summary . 147 6 GADT Type Inference with Algorithm P :::::::::::::: 149 6.1 Overall Structure . 149 6.1.1 Foundation . 150 6.1.2 Compositionality . 153 6.1.3 Branch reachability . 156 6.2 Branch Type Inference . 159 6.2.1 Single-branch type inference . 162 6.2.2 Scrutinee-type specialization . 165 6.3 GADT Type Refinements . 170 6.3.1 Tabulating branch types . 171 6.3.2 Applying type refinements . 174 6.4 Summary . 183 7 Algorithm P by Example ::::::::::::::::::::::::: 185 7.1 Algorithm Implementation . 186 7.2 Non-Dependent Programs . 189 7.2.1 Type refinement aversion . 190 7.2.2 Type equality witnesses . 191 7.2.3 Generalized existential types . 194 7.3 GADT Type Refinements . 195 7.3.1 Generalized existential type escape . 196 7.3.2 Contextual type information . 197 7.3.3 Opposable thumbs . 199 7.4 Polymorphic Recursion . 201 7.5 Algorithm Limitations . 206 7.5.1 Local type reconciliation . 208 7.5.2 Compositionality . 211 7.5.3 Lack of backtracking search .