7 Varieties of Object-Oriented Programming Languages 95 7.1 Multi-Methods Vs
Total Page:16
File Type:pdf, Size:1020Kb
Y L F M A E T Team-Fly® Foundations of Object-Oriented Languages This page intentionally left blank Foundations of Object-Oriented Languages Types and Semantics Kim B. Bruce The MIT Press Cambridge, Massachusetts London, England © 2002 Massachusetts Institute of Technology All rights reserved. No part of this book may be reproduced in any form by any electronic or mechanical means (including photocopying, recording, or information storage and retrieval) without permission in writing from the publisher. Library of Congress Cataloging-in-Publication Information Bruce, Kim B. Foundations of object-oriented languages: types and semantics / Kim B. Bruce. p. cm. Includes bibliographical references and index. ISBN 0-262-02523-X (hc. : alk. paper) 1. Object-oriented programming (computer science). 2. Programming lan- guages (Electronic computers). I. Title. QA76.64 .B776 2002 005.1’17–dc21 2001054613 To my mother and the memory of my late father This page intentionally left blank Contents List of Figures xi Preface xv ITypeProblemsin Object-Oriented Languages 1 1 Introduction 3 1.1 Type systems in programming languages 4 1.2 Type checking and strongly typed languages 6 1.3 Focus on statically typed class-based languages 12 1.4 Foundations: A look ahead 13 2 Fundamental Concepts of Object-Oriented Languages 17 2.1 Objects, classes, and object types 17 2.2 Subclasses and inheritance 22 2.3 Subtypes 24 2.4 Covariant and contravariant changes in types 26 2.5 Overloading versus overriding methods 27 2.6 Summary 32 3 Type Problems in Object-Oriented Languages 33 3.1 Type checking object-oriented languages is difficult 33 3.2 Simple type systems are lacking in flexibility 35 3.3 Summary of typing problems 48 4 Adding Expressiveness to Object-Oriented Languages 49 viii Contents 4.1 GJ 49 4.2 Even more flexible typing with Eiffel 60 4.3 Summary 69 5 Understanding Subtypes 71 5.1 Subtyping for non-object types 72 5.2 Object types 83 5.3 Subtyping for class types 84 5.4 Summary 86 6 Type Restrictions on Subclasses 89 6.1 Allowable changes to method types 89 6.2 Instance variable types invariant in subclasses 91 6.3 Changing visibility 92 6.4 Summary 93 7 Varieties of Object-Oriented Programming Languages 95 7.1 Multi-methods vs. object-based vs. class-based languages 95 7.2 Well-known object-oriented languages 103 7.3 Summary 111 Historical Notes and References for Section I 113 II Foundations: The Lambda Calculus 117 8 Formal Language Descriptions and the Lambda Calculus 119 8.1 The simply-typed lambda calculus 120 8.2 Adding pairs, sums, records, and references 132 8.3 Summary 140 9 The Polymorphic Lambda Calculus 141 9.1 Parameterized types and polymorphism 141 9.2 Recursive expressions and types 147 9.3 Information hiding and existential types 151 9.4 Adding subtypes to the polymorphic lambda calculus 156 9.5 Summary 165 Historical Notes and References for Section II 167 Contents ix III Formal Descriptions of Object-Oriented Languages 171 10 ËÇÇÄ, a Simple Object-Oriented Language 173 10.1 Informal description and example 173 10.2 Syntax and type-checking rules 176 10.3 Summary 200 11 A Simple Translational Semantics of Objects and Classes 201 11.1 Representing objects at runtime 201 È ËÇÇÄ £ 11.2 Modeling types in 203 È ËÇÇÄ £ 11.3 Modeling expressions in 207 11.4 Modeling classes — first try 212 11.5 Problems with modeling subclasses 218 11.6 Summary 223 12 Improved Semantics for Classes 225 12.1 (Re-)Defining classes 225 12.2 A correct subclass encoding 232 12.3 Summary and a look ahead 233 13 ËÇÇÄ’s Type System Is Safe (and Sound) 239 È Ë ÇÇÄ £ 13.1 The translation of to is sound 239 13.2 The translation is well defined 255 13.3 Ë ÇÇÄ is type safe 258 13.4 Errors 260 13.5 Summary 262 14 Completing ËÇÇÄ: super, nil, Information Hiding, and Multiple Inheritance 263 14.1 Using methods from superclasses 263 14.2 Translating nil 266 14.3 A complication with self 271 14.4 Finer control over information hiding 272 14.5 Multiple inheritance 275 14.6 Summary 279 Historical Notes and References for Section III 283 x Contents IV Extending Simple Object-Oriented Languages 289 15 Adding Bounded Polymorphism to ËÇÇÄ 291 15.1 Introducing ÈËÇÇÄ 291 15.2 Translational semantics of ÈËÇÇÄ 296 15.3 Summary 297 16 Adding MyType to Object-Oriented Programming Languages 299 16.1 Typing self with MyType 300 Ë ÇÇÄ 16.2 ÅÇÇÄ: Adding MyType to 309 16.3 Translational semantics of ÅÇ Ç Ä 319 16.4 Soundness of translation for ÅÇ Ç Ä 322 16.5 Summary 330 17 Match-Bounded Polymorphism 331 17.1 Benefits of match-bounded polymorphism 331 17.2 Introducing È ÅÇÇÄ 333 17.3 Examples and comparison with F-bounded polymorphism 334 17.4 Translational semantics of È ÅÇÇÄ 335 17.5 Soundness of the translation of ÈÅÇÇÄ 342 17.6 Summary 347 18 Simplifying: Dropping Subtyping for Matching 349 18.1 Can we drop subtyping? 349 18.2 Introducing hash types 352 18.3 Type-checking rules 356 18.4 An informal semantics of hash types 360 18.5 Summary 361 Historical Notes and References for Section IV 363 Bibliography 367 Index 379 Y L F M A E T Team-Fly® List of Figures 2.1 ClrCellClass defined as a subclass of CellClass.23 2.2 Covariant and contravariant changes in types. 26 2.3 Classes with overridden and overloaded method equals.29 3.1 Typing deepClone methods in subclasses. 38 3.2 Node class. 42 3.3 Doubly linked node class — with errors. 43 3.4 Legal doubly linked node class — with cast. 45 3.5 Example showing why IndDoubleNodeType cannot be a subtype of NodeType.46 3.6 Circle and color circle classes. 47 4.1 Point interfaces in Java. 52 4.2 Point class in Java. 53 4.3 RATIONAL class in Eiffel, part 1. 62 4.4 RATIONAL class in Eiffel, part 2. 63 4.5 Eiffel classes LINKABLE and BILINKABLE, part 1. 65 4.6 Eiffel classes LINKABLE and BILINKABLE, part 2. 66 4.7 Eiffel version of COMPARING. 69 5.1 A record r: m: S; n: T; p: U , and another record r’: m: S’; n: T’; p: U’; q: V’ masquerading as an element of type m: S; n: T; p: U .74 5.2 A function f: S T, and another function f’: S’ T’ masquerading as having type S T.75 xii List of Figures 5.3 A variable x: Ref S, and another variable x’: Ref S’ masquerading as having type Ref S.78 5.4 Summary of subtyping rules. 88 6.1 Changing types of methods in subclasses. 90 7.1 Cell and StringCell classes in Beta. 105 7.2 The Subject-Observer pattern expressed with virtual types. 106 7.3 Specializing Subject-Observer to windows. 107 7.4 Lack of least upper bounds in Java interfaces. 109 8.1 Typing rules for expressions of the typed lambda calculus. 126 £ 8.2 Type-checking rules for ÖÖ , part 1. 136 £ 8.3 Type-checking rules for ÖÖ , part 2. 137 £ 8.4 Computation rules for ÖÖ . 139 9.1 Kind checking and equivalence rules. 144 È £ 9.2 New type-checking and congruence rules in ÖÖ . 146 È £ 9.3 Type-checking rules for existentials in ÖÖ . 155 9.4 Kind checking rules for bounded polymorphic and existential types. 157 È £ 9.5 Subtyping rules for : Part 1. 159 È £ 9.6 Subtyping rules for : Part 2. 160 È £ 9.7 Type-checking rules for , part 1: revised rules. 164 È £ 9.8 Type-checking rules for , part 2: revised rules. 165 È £ 9.9 Type-checking rules for , part 3: new rules. 166 10.1 Cell class. 174 10.2 CellClass in the fully expanded language. 175 10.3 PointExample program in language with abbreviations. 182 10.4 PointExample program in language without abbreviations. 183 10.5 Parameterized Point class in language with abbreviations. 184 10.6 Parameterized Point class in language without abbreviations. 185 10.7 Type checking rules for declarations. 188 10.8 Type-checking rules for expressions of ËÇÇÄ, part 1. 189 10.9 Type-checking rules for expressions of ËÇÇÄ, part 2. 192 10.10 Type-checking rules for statements of ËÇÇÄ. 194 10.11 Subtyping rules for ËÇÇÄ. 195 10.12 Class definition from PointExample. 197 List of Figures xiii È Ë ÇÇÄ £ 11.1 Translation of types of to types in . 203 11.2 Translation of selected expressions of ËÇÇÄ to expressions in È £ . 209 11.3 Translation of more expressions and statements of ËÇÇÄ to È £ expressions in .211 11.4 Translation of class and new expressions of ËÇÇÄ to È £ expressions in . 218 12.1 Revised type-checking rules for classes and subclasses. 227 12.2 Revised type-checking rules for messages and instance variables. 229 12.3 New translation of classes and class types. 230 12.4 New translation of new expressions. 231 12.5 New translation of subclasses. 233 È ËÇÇÄ £ 12.6 Final translation of types of to types in . 234 12.7 Translation of selected expressions of ËÇÇÄ to expressions in È £ . 235 È ËÇÇÄ £ 12.8 Final translation of expressions of to . Part 2. 236 È ËÇÇÄ £ 12.9 Translation of statements of to expressions in . 237 13.1 CellExample program. 256 14.1 Type-checking rules for subclasses with super. 264 È ËÇÇÄ £ 14.2 Translation of subclass with super from to .