Type Systems for Programming Languages1 (DRAFT)
Total Page:16
File Type:pdf, Size:1020Kb
Type Systems for Programming Languages1 (DRAFT) Robert Harper School of Computer Science Carnegie Mellon University Pittsburgh, PA 15213-3891 E-mail: [email protected] WWW: http://www.cs.cmu.edu/~rwh Spring, 2000 Copyright c 1995-2000. All rights reserved. 1These are course notes Computer Science 15–814 at Carnegie Mellon University. This is an incomplete, working draft, not intended for publication. Citations to the literature are spotty at best; no results presented here should be considered original unless explicitly stated otherwise. Please do not distribute these notes without the permission of the author. ii Contents I Type Structure 1 1 Basic Types 3 1.1 Introduction . 3 1.2 Statics . 3 1.3 Dynamics . 4 1.3.1 Contextual Semantics . 4 1.3.2 Evaluation Semantics . 5 1.4 Type Soundness . 7 1.5 References . 8 2 Function and Product Types 9 2.1 Introduction . 9 2.2 Statics . 9 2.3 Dynamics . 10 2.4 Type Soundness . 11 2.5 Termination . 12 2.6 References . 13 3 Sums 15 3.1 Introduction . 15 3.2 Sum Types . 15 3.3 References . 16 4 Subtyping 17 4.1 Introduction . 17 4.2 Subtyping . 17 4.2.1 Subsumption . 18 4.3 Primitive Subtyping . 18 4.4 Tuple Subtyping . 19 4.5 Record Subtyping . 22 4.6 References . 24 iii 5 Variable Types 25 5.1 Introduction . 25 5.2 Variable Types . 25 5.3 Type Definitions . 26 5.4 Constructors, Types, and Kinds . 28 5.5 References . 30 6 Recursive Types 31 6.1 Introduction . 31 6.2 G¨odel’s T ............................... 31 6.2.1 Statics . 31 6.2.2 Dynamics . 32 6.2.3 Termination . 32 6.3 Mendler’s S .............................. 34 6.4 General Recursive Types . 36 6.5 References . 37 7 Polymorphism 39 7.1 Introduction . 39 7.2 Statics . 39 7.3 Dynamics . 40 7.4 Impredicative Polymorphism . 41 7.4.1 Termination . 42 7.4.2 Definability of Types . 46 7.5 References . 47 8 Abstract Types 49 8.1 Statics . 49 8.2 Dynamics . 50 8.3 Impredicativity . 50 8.4 Dot Notation . 51 8.5 References . 53 9 Higher Kinds 55 9.1 Introduction . 55 9.2 Functional Kinds . 55 9.3 Subtyping and Higher Kinds . 56 9.4 Bounded Quantification and Power Kinds . 57 9.5 Singleton Kinds, Dependent Kinds, and Subkinding . 59 9.6 References . 59 10 Modularity 61 10.1 Introduction . 61 10.2 A Critique of Some Modularity Mechanisms . 68 10.3 A Modules Language . 71 10.3.1 Structures . 72 iv 10.3.2 Module Hierarchies . 76 10.3.3 Parameterized Modules . 77 10.4 Second-Class Modules . 79 10.5 Higher-Order Modules . 79 10.6 References . 79 11 Objects 81 11.1 Introduction . 81 11.2 Primitive Objects . 81 11.3 Object Subtyping . 81 11.4 Second-Order Object Types . 81 11.5 References . 81 12 Dynamic Types 83 12.1 Type Dynamic . 83 12.2 Hierarchical Tagging . 83 13 Classes 85 13.1 Introduction . 85 13.2 Public, Private, and Protected . 85 13.3 Constructors . 85 13.4 Subtyping and Inheritance . 85 13.5 Dynamic Dispatch . 85 13.6 References . 85 II Computational Effects 87 14 Recursive Functions 89 14.1 Introduction . 89 14.2 Statics . 89 14.3 Dynamics . 90 14.4 Type Soundness . 90 14.5 Compactness . 91 14.6 References . 93 15 Continuations 95 15.1 Introduction . 95 15.2 Statics . 95 15.3 Dynamics . 96 15.4 Soundness . 98 15.5 References . 100 v 16 References 101 16.1 Introduction . 101 16.2 Statics . 101 16.3 Dynamics . 102 16.4 Type System . 102 16.5 Allocation and Collection . 103 16.6 References . 105 17 Exceptions 107 17.1 Introduction . 107 17.2 Statics . 107 17.3 Dynamics . 108 17.4 Exceptions and First-Class Continuations . 110 17.5 References . 111 III Implementation 113 18 Type Checking 115 18.1 Introduction . 115 18.2 Type Synthesis . 115 18.3 Definitional Equality . 116 18.4 Subtyping . 118 18.5 Subtyping . 119 18.6 References . 120 19 Type Reconstruction 121 19.1 Introduction . 121 →,∀ 19.2 Type Reconstruction for LΩ . 121 19.3 Type Reconstruction as Static Semantics . 122 19.4 Reconstruction as Constraint Solving . 124 19.4.1 Unification Logic . 124 19.4.2 Constraint Generation . 126 19.5 Reconstruction and Definitions . 128 19.6 References . 129 20 Coercion Interpretation of Subtyping 131 21 Named Form 133 21.1 Introduction . ..