Types and Programming Languages - Computer & Information ...

5 downloads 118 Views 46KB Size Report
Types and Programming Languages. Benjamin C. Pierce. The MIT Press. Cambridge, Massachusetts. London, EnglandĀ ...
Types and Programming Languages

Benjamin C. Pierce

The MIT Press Cambridge, Massachusetts London, England

Contents

Preface 1

Introduction 1.1 1.2 1.3 1.4 1.5

2

xiii 1

Types in Computer Science 1 What Type Systems Are Good For 4 Type Systems and Language Design 9 Capsule History 10 Related Reading 12

Mathematical Preliminaries 2.1 2.2 2.3 2.4 2.5

Sets, Relations, and Functions Ordered Sets 16 Sequences 18 Induction 19 Background Reading 20

I Untyped Systems 3

15

21

Untyped Arithmetic Expressions 3.1 3.2 3.3 3.4 3.5 3.6

15

Introduction 23 Syntax 26 Induction on Terms 29 Semantic Styles 32 Evaluation 34 Notes 43

23

vi

Contents

4

An ML Implementation of Arithmetic Expressions 4.1 4.2 4.3

5

Terms and Contexts 76 Shifting and Substitution Evaluation 80

Terms and Contexts 83 Shifting and Substitution Evaluation 87 Notes 88

II Simple Types

75 78

91

Types 91 The Typing Relation 92 Safety = Progress + Preservation

Simply Typed Lambda-Calculus 9.1 9.2 9.3 9.4 9.5 9.6 9.7

85

89

Typed Arithmetic Expressions 8.1 8.2 8.3

9

58

An ML Implementation of the Lambda-Calculus 7.1 7.2 7.3 7.4

8

51

Basics 52 Programming in the Lambda-Calculus Formalities 68 Notes 73

Nameless Representation of Terms 6.1 6.2 6.3

7

49

The Untyped Lambda-Calculus 5.1 5.2 5.3 5.4

6

Syntax 46 Evaluation 47 The Rest of the Story

Function Types 99 The Typing Relation 100 Properties of Typing 104 The Curry-Howard Correspondence Erasure and Typability 109 Curry-Style vs. Church-Style 111 Notes 111

Contexts 113 Terms and Types 115 Typechecking 115

95

99

10 An ML Implementation of Simple Types 10.1 10.2 10.3

45

108

113

83

vii

Contents

11 Simple Extensions 11.1 11.2 11.3 11.4 11.5 11.6 11.7 11.8 11.9 11.10 11.11 11.12

Base Types 117 The Unit Type 118 Derived Forms: Sequencing and Wildcards Ascription 121 Let Bindings 124 Pairs 126 Tuples 128 Records 129 Sums 132 Variants 136 General Recursion 142 Lists 146

12 Normalization 12.1 12.2

153

171

Raising Exceptions 172 Handling Exceptions 173 Exceptions Carrying Values

III Subtyping 15 Subtyping 15.1 15.2 15.3 15.4 15.5 15.6 15.7 15.8

149

Introduction 153 Typing 159 Evaluation 159 Store Typings 162 Safety 165 Notes 170

14 Exceptions 14.1 14.2 14.3

149

Normalization for Simple Types Notes 152

13 References 13.1 13.2 13.3 13.4 13.5 13.6

117

175

179 181

Subsumption 181 The Subtype Relation 182 Properties of Subtyping and Typing 188 The Top and Bottom Types 191 Subtyping and Other Features 193 Coercion Semantics for Subtyping 200 Intersection and Union Types 206 Notes 207

119

viii

Contents

16 Metatheory of Subtyping 16.1 16.2 16.3 16.4

209

Algorithmic Subtyping 210 Algorithmic Typing 213 Joins and Meets 218 Algorithmic Typing and the Bottom Type

17 An ML Implementation of Subtyping 17.1 17.2 17.3

225

What Is Object-Oriented Programming? 225 Objects 228 Object Generators 229 Subtyping 229 Grouping Instance Variables 230 Simple Classes 231 Adding Instance Variables 233 Calling Superclass Methods 234 Classes with Self 234 Open Recursion through Self 235 Open Recursion and Evaluation Order 237 A More Efficient Implementation 241 Recap 244 Notes 245

19 Case Study: Featherweight Java 19.1 19.2 19.3 19.4 19.5 19.6 19.7

221

Syntax 221 Subtyping 221 Typing 222

18 Case Study: Imperative Objects 18.1 18.2 18.3 18.4 18.5 18.6 18.7 18.8 18.9 18.10 18.11 18.12 18.13 18.14

220

247

Introduction 247 Overview 249 Nominal and Structural Type Systems Definitions 254 Properties 261 Encodings vs. Primitive Objects 262 Notes 263

251

ix

Contents

IV Recursive Types 20 Recursive Types 20.1 20.2 20.3 20.4

265 267

Examples 268 Formalities 275 Subtyping 279 Notes 279

21 Metatheory of Recursive Types 21.1 21.2 21.3 21.4 21.5 21.6 21.7 21.8 21.9 21.10 21.11 21.12

Induction and Coinduction 282 Finite and Infinite Types 284 Subtyping 286 A Digression on Transitivity 288 Membership Checking 290 More Efficient Algorithms 295 Regular Trees 298 Āµ-Types 299 Counting Subexpressions 304 Digression: An Exponential Algorithm Subtyping Iso-Recursive Types 311 Notes 312

V Polymorphism

317

Type Variables and Substitutions 317 Two Views of Type Variables 319 Constraint-Based Typing 321 Unification 326 Principal Types 329 Implicit Type Annotations 330 Let-Polymorphism 331 Notes 336

23 Universal Types 23.1 23.2 23.3 23.4 23.5 23.6

309

315

22 Type Reconstruction 22.1 22.2 22.3 22.4 22.5 22.6 22.7 22.8

281

339

Motivation 339 Varieties of Polymorphism 340 System F 341 Examples 344 Basic Properties 353 Erasure, Typability, and Type Reconstruction

354

x

Contents

23.7 23.8 23.9 23.10 23.11

Erasure and Evaluation Order Fragments of System F 358 Parametricity 359 Impredicativity 360 Notes 361

24 Existential Types 24.1 24.2 24.3 24.4

357

363

Motivation 363 Data Abstraction with Existentials Encoding Existentials 377 Notes 379

25 An ML Implementation of System F 25.1 25.2 25.3 25.4 25.5

381

Nameless Representation of Types 381 Type Shifting and Substitution 382 Terms 383 Evaluation 385 Typing 386

26 Bounded Quantification 26.1 26.2 26.3 26.4 26.5 26.6

368

389

Motivation 389 Definitions 391 Examples 396 Safety 400 Bounded Existential Types Notes 408

406

27 Case Study: Imperative Objects, Redux

411

28 Metatheory of Bounded Quantification

417

28.1 28.2 28.3 28.4 28.5 28.6 28.7 28.8

Exposure 417 Minimal Typing 418 421 Subtyping in Kernel F