Semantic Types for Class-Based Objects
Total Page:16
File Type:pdf, Size:1020Kb
Imperial College London Department of Computing Semantic Types for Class-based Objects Reuben N. S. Rowe July 2012 Supervised by Dr. Steffen van Bakel Submitted in part fulfilment of the requirements for the degree of Doctor of Philosophy in Computing of Imperial College London and the Diploma of Imperial College London 1 Abstract We investigate semantics-based type assignment for class-based object-oriented programming. Our mo- tivation is developing a theoretical basis for practical, expressive, type-based analysis of the functional behaviour of object-oriented programs. We focus our research using Featherweight Java, studying two notions of type assignment:- one using intersection types, the other a ‘logical’ restriction of recursive types. We extend to the object-oriented setting some existing results for intersection type systems. In do- ing so, we contribute to the study of denotational semantics for object-oriented languages. We define a model for Featherweight Java based on approximation, which we relate to our intersection type system via an Approximation Result, proved using a notion of reduction on typing derivations that we show to be strongly normalising. We consider restrictions of our system for which type assignment is decid- able, observing that the implicit recursion present in the class mechanism is a limiting factor in making practical use of the expressive power of intersection types. To overcome this, we consider type assignment based on recursive types. Such types traditionally suffer from the inability to characterise convergence, a key element of our approach. To obtain a se- mantic system of recursive types for Featherweight Java we study Nakano’s systems, whose key feature is an approximation modality which leads to a ‘logical’ system expressing both functional behaviour and convergence. For Nakano’s system, we consider the open problem of type inference. We introduce insertion variables (similar to the expansion variables of Kfoury and Wells), which allow to infer when the approximation modality is required. We define a type inference procedure, and conjecture its sound- ness based on a technique of Cardone and Coppo. Finally, we consider how Nakano’s approach may be applied to Featherweight Java and discuss how intersection and logical recursive types may be brought together into a single system. 3 I dedicate this thesis to the memory of my grandfather, David W. Hyam, who I very much wish was here to see it. 5 Acknowledgements I would like to acknowledge the help, input and inspiration of a number of people who have all helped me, in their own larger and smaller ways, to bring this thesis into being. Firstly, my supervisor, Steffen, deserves my sincere thanks for all his guidance over the past five years. If it weren’t for him, I would never have embarked upon this line of research that I have found so absolutely fascinating. Also, if it weren’t for him I would probably have lost myself in many unnecessary details – his gift for being able to cut through to the heart of many a problem has been invaluable when I couldn’t see the wood for the trees. I would also like to thank my second supervisor, Sophia Drossopoulou, who has always been more than willing to offer many insightful suggestions and opinions, and above all a friendly ear. I owe a huge debt to my parents, Peter and Catherine, and all of my family who have been so sup- portive and encouraging of my efforts. They have always brought me up to believe that I can achieve everything that I put my mind to, and without them I would never have reached this point. I must thank, in particular, my late aunt Helen whose financial legacy, in part, made my higher education aspirations a reality. My wonderful girlfriend Carlotta has shared some of this journey with me, and this thesis is just as much hers as it is mine, having borne my distractions and preoccupations with grace. Her encouragement and faith in me has carried me through more than a few difficult days. I thank Jayshan Raghunandan, Ioana Boureanu, Juan Vaccari and Alex Summers for their friendship, interesting discussions, and for making the start of my PhD such an enjoyable experience. I especially thank Alex, who may be one of the most intelligent, friendly and modest people I have met. Finally, I would like to extend my appreciation to the SLURP group, and the staff and students of the Imperial College Department of Computing, who have all contributed to my wider academic environ- ment. 7 Contents 1. Introduction 11 I. Simple Intersection Types 17 2. The Intersection Type Discipline 19 2.1. LambdaCalculus ................................. ..... 19 2.2. ObjectCalculus ................................. ...... 23 3. Intersection Types for Featherweight Java 29 3.1. FeatherweightJava ..... ...... ..... ...... ...... .. ....... 29 3.2. Intersection Type Assignment . .......... 31 3.3. Subject Reduction & Expansion . ......... 33 4. Strong Normalisation of Derivation Reduction 37 5. The Approximation Result: Linking Types with Semantics 61 5.1. Approximation Semantics . ........ 61 5.2. The Approximation Result . ........ 66 5.3. Characterisation of Normalisation . ............. 69 6. Worked Examples 73 6.1. A Self-Returning Object . ........ 73 6.2. AnUnsolvableProgram. ....... 74 6.3. Lists.......................................... 76 6.4. Object-Oriented Arithmetic . ........... 78 6.5. A Type-Preserving Encoding of Combinatory Logic . ............... 80 6.6. Comparison with Nominal Typing . ......... 89 7. Type Inference 99 7.1. A Restricted Type Assignment System . ........... 99 7.2. Substitution and Unification . ..........102 7.3. PrincipalTypings ............................... .......106 9 II. Logical Recursive Types 121 8. Logical vs. Non-Logical Recursive Types 123 8.1. Non-Logical Recursive Types . ..........124 8.2. Nakano’s Logical Systems . ........126 8.2.1. TheTypeSystems ............................... 127 8.2.2. Convergence Properties . 130 8.2.3. A Type for Fixed-Point Operators . ........132 9. Type Inference for Nakano’s System 135 9.1. Types .......................................... 136 9.2. TypeAssignment ................................. 139 9.3. OperationsonTypes .............................. 141 9.4. A Decision Procedure for Subtyping . ...........144 9.5. Unification ..................................... 149 9.6. TypeInference .................................. 165 9.6.1. Typing Curry’s Fixed Point Operator Y ......................169 9.6.2. Incompleteness of the Algorithm . .........174 9.6.3. OnPrincipalTypings . 176 10. Extending Nakano Types to Featherweight Java 181 10.1.ClassesAsFunctions . ........181 10.2. Nakano Types for Featherweight Java . ............184 10.3.TypedExamples................................. 190 10.3.1. A Self-Returning Object . .......191 10.3.2. A Nonterminating Program . .......193 10.3.3. Mutually Recursive Class Definitions . ...........193 10.3.4. A Fixed-Point Operator Construction . ...........196 10.3.5.Lists....................................... 197 10.3.6. Object-Oriented Arithmetic . .........202 10.4. Extending The Type Inference Algorithm . .............204 10.5. Nakano Intersection Types . ..........208 11. Summary of Contributions & Future Work 219 Bibliography 223 A. Type-Based Analysis of Ackermann’s Function 233 A.1. The Ackermann Function in Featherweight Java . ..............233 A.2. Strong Normalisation of Ackfj ...............................235 A.3. Typing the Parameterized Ackermann Function . ..............239 A.3.1. Rank 0 Typeability of Ack[0]............................242 A.3.2. Rank 0 Typeability of Ack[1]............................243 A.3.3. Rank 4 Typeability of Ack[2]............................244 10 1. Introduction Type theory constitutes a form of abstract reasoning, or interpretation of programs. It provides a way of classifying programs according to the kinds of values they compute [88]. More generally, type systems specify schemes for associating syntactic entities (types) with programs, in such a way that they reflect abstract properties about the behaviour of those programs. Thus, typeability effectively guarantees well- behavedness, as famously stated by Milner when he said that “Well-typed programs can’t go wrong” [79], where ‘wrong’ is a semantic concept defined in that paper. In type theory, systems in the intersection type discipline (itd) stand out as being able to express both the functional behaviour of programs and their termination properties. Intersection types were first introduced in [40] as an extension to Curry’s basic functionality theory for the Lambda Calculus (λ-calculus or lc) [45]. Since then, the intersection type approach has been extended to many different models of computation including Term Rewriting Systems (trs) [14, 15], sequent calculi [101, 102], object calculi [18, 13], and concurrent calculi [37, 87] proving its versatility as an analytical technique for program verification. Furthermore, intersection types have been put to use in analysing not just termination properties but in dead code analysis [47], strictness analysis [70], and control-flow analysis [17]. It is obvious, then, that intersection types have a great potential as a basis for expressive, type-based analysis of programs. The expressive power of intersection types stems from their deep connection with the mathematical, or denotational, semantics of programming languages [96, 97]. It was first demonstrated in [20] that the set of intersection types assignable to any given term forms a filter, and that the set of such