Pure Subtype Systems: a Type Theory for Extensible Software
Total Page:16
File Type:pdf, Size:1020Kb
Pure Subtype Systems: A Type Theory for Extensible Software DeLesley Hutchins I V N E R U S E I T H Y T O H F G E R D I N B U Doctor of Philosophy Laboratory for Foundations of Computer Science School of Informatics University of Edinburgh 2009 Abstract This thesis presents a novel approach to type theory called “pure subtype systems”, and a core calculus called DEEP which is based on that approach. DEEP is capable of modeling a number of interesting language techniques that have been proposed in the literature, including mixin modules, virtual classes, feature-oriented programming, and partial evaluation. The design of DEEP was motivated by two well-known problems: “the expression problem”, and “the tag elimination problem.” The expression problem is concerned with the design of an interpreter that is extensible, and requires an advanced module system. The tag elimination problem is concerned with the design of an interpreter that is efficient, and requires an advanced partial evaluator. We present a solution in DEEP that solves both problems simultaneously, which has never been done before. These two problems serve as an “acid test” for advanced type theories, because they make heavy demands on the static type system. Our solution in DEEP makes use of the following capabilities. (1) Virtual types are type definitions within a module that can be extended by clients of the module. (2) Type definitions may be mutually recursive. (3) Higher-order subtyping and bounded quantification are used to represent partial information about types. (4) Dependent types and singleton types provide increased type precision. The combination of recursive types, virtual types, dependent types, higher-order subtyping, and bounded quantification is highly non-trivial. We introduce “pure sub- type systems” as a way of managing this complexity. Pure subtype systems eliminate the distinction between types and objects; every term can behave as either a type or an object depending on context. A subtype relation is defined over all terms, and sub- typing, rather than typing, forms the basis of the theory. We show that higher-order subtyping is strong enough to completely subsume the traditional type relation, and we provide practical algorithms for type checking and for finding minimal types. The cost of using pure subtype systems lies in the complexity of the meta-theory. Unfortunately, we are unable to establish some basic meta-theoretic properties, such as type safety and transitivity elimination, although we have made some progress towards these goals. We formulate the subtype relation as an abstract reduction system, and we show that the type theory is sound if the reduction system is confluent. We can prove that reductions are locally confluent, but a proof of global confluence remains elusive. In summary, pure subtype systems represent a new and interesting approach to type theory. This thesis describes the basic properties of pure subtype systems, and provides concrete examples of how they can be applied. The Deep calculus demon- strates that our approach has a number of real-world practical applications in areas that have proved to be quite difficult for traditional type theories to handle. However, the ultimate soundness of the technique remains an open question. iii Acknowledgements I would first of all like to thank my supervisor, Philip Wadler, for all the help that he has given me during my time here at the University of Edinburgh. Phil was not always convinced that my approach to type theory was the right one to take, and his initial skepticism has turned out to be well-founded in many respects. However, he was willing to give me the freedom to pursue my own path, and make my own discoveries and mistakes. Yet Phil did not just leave to my own devices; he was always there to discuss my ideas, read my drafts, send me pointers to seminars, talks, and related work, and offer suggestions for improvement. I am very grateful for his input. I would also like to thank the two examiners for my thesis, David Aspinall and Joe Wells, for taking the time to go through this entire document with a fine-toothed comb. David and Joe have really helped me to improve the thesis, by pointing out everything from minor corrections to major clarifications. In addition, I would like to thank Dave Robertson, my second supervisor, for his help and support. Dave was always there to provide encouragement, advice, and a sympathetic ear. My heartfelt thanks go out too to Bob Praus and Steve Coy of MZA Associates Corporation. I was employed by MZA before starting my PhD, and many of the ideas in this thesis were inspired by work that I initially did for the company. When I de- cided to pursue those ideas further in grad school, MZA generously offered to fund my research. Without their financial support, this work would never have been possible. As with any scientist, I have been encouraged and supported by many people in the research community in which I work. Don Batory proofread drafts of several pa- pers and offered many suggestions. William Cook championed my first major publi- cation at OOPSLA. Sven Apel worked with me as a coauthor on applications of the DEEP calculus. Conor McBride sat down with me to explain the mysterious world of dependently-typed pattern matching. Benjamin Pierce was there to answer several questions on the more subtle aspects of subtyping. Vincent Van Oostrom helped me work through the confluence proofs at the core of System lC. Randy Pollack helped me with my proof techniques, and mechanically verified some of my proofs. I’m sure that there are others that I have forgotten to mention; my thanks go out to them as well. In addition, I would like to thank my mother for being there for me during my studies. She was the one who initially suggested that I go back to school, and then was shocked when I announced that I would be moving across the Atlantic to pursue a degree in Scotland. But she saved my mail for me, stored my possessions, and spoke to me across the miles with a friendly voice and encouraging words in phone and e-mail. Last but not certainly not least, I offer grateful thanks to my wife, Laura, who has been my constant companion throughout my PhD. We met within days of my arrival in Edinburgh, fell in love, got engaged, and then got married, in what was almost a storybook romance. She has helped motivate me when I needed it, propped me up when I was down, and has shared both my triumphs and my disappointments. Her love and commitment has kept me going; I couldn’t have done this without her. iv Declaration I declare that this thesis was composed by myself, that the work contained herein is my own except where explicitly stated otherwise in the text, and that this work has not been submitted for any other degree or professional qualification except as specified. (DeLesley Hutchins) v Table of Contents 1 Introduction 1 1.1 A tale of two problems . 1 1.1.1 Pure subtype systems . 3 1.2 The expression problem . 4 1.2.1 The expression problem: OO style . 5 1.2.2 The expression problem: functional programming style . 6 1.2.3 The solution: late-binding for types . 7 1.2.4 The solution: functional programming style . 9 1.2.5 Type system requirements . 10 1.3 Doman specific languages and tag-elimination . 11 1.3.1 Performance and optimization . 12 1.3.2 Partial evaluation . 13 1.3.3 The tag-elimination problem . 13 1.3.4 Well-typed interpreters . 15 1.4 Putting it together: the DSL expression problem . 15 1.5 Pure subtype systems . 16 1.6 Types and Objects . 17 1.6.1 Symmetry . 17 1.6.2 Dependent types . 19 1.6.3 Modules . 20 1.6.4 Classes and Prototypes . 21 1.7 Subtyping . 22 1.7.1 Subtyping: a substitute for typing . 23 1.7.2 Interpretation of types and objects . 24 1.7.3 A spectrum of type information . 25 1.7.4 Combining typing and partial evaluation . 26 1.8 Outline of thesis . 26 1.8.1 Outline of individual chapters . 27 2 Pure Subtype Systems 29 2.1 Introduction . 29 w 2.2 Comparison to System F≤ and Pure Type Systems . 31 2.2.1 Deconstructing the typing judgment . 32 2.2.2 Bounded quantification and Top-types . 33 2.3 System lC ................................ 35 2.3.1 Subtyping . 37 vii 2.3.2 Well-formedness . 38 2.3.3 Example: 3 ≤ Nat ....................... 38 2.3.4 Adding Universes . 39 2.4 Embeddings of other languages into System lC . 40 2.4.1 Embedding of System F≤ ................... 41 2.4.2 Symmetry in theories: an analogy with physics . 44 2.4.3 Embedding of l∗, the pure type system with ∗ : ∗ . 46 2.4.4 Logical consistency and Girard’s paradox . 48 2.4.5 Impredicativity . 49 2.5 Type Safety . 50 2.5.1 Basic meta-theoretic properties . 50 2.5.2 The easy part of type safety . 52 2.5.3 The trouble with transitivity . 54 2.5.4 Transitivity elimination . 55 2.6 Algorithmic Subtyping . 56 2.6.1 Transitivity and Confluence . 59 2.6.2 Subtyping as a syntactic relation . 62 2.6.3 Conditional rewrite systems . 63 2.6.4 Basic meta-theory of algorithmic subtyping . 65 2.6.5 Equivalence of declarative and algorithmic subtyping. 67 2.7 Confluence and commutativity . 70 2.7.1 An overview of confluence properties and proofs .