University of Groningen
Supporting software reusability with polymorphic types Laverman, Bert
IMPORTANT NOTE: You are advised to consult the publisher's version (publisher's PDF) if you wish to cite from it. Please check the document version below.
Document Version
Publisher's PDF, also known as Version of record
Publication date: 1995
Link to publication in University of Groningen/UMCG research database
Citation for published version (APA):
Laverman, B. (1995). Supporting software reusability with polymorphic types s.n.
Copyright
Other than for strictly personal use, it is not permitted to download or to forward/distribute the text or part of it without the consent of the author(s) and/or copyright holder(s), unless the work is under an open content license (like Creative Commons).
Take-down policy
If you believe that this document breaches copyright please contact us providing details, and we will remove access to the work immediately and investigate your claim.
Downloaded from the University of Groningen/UMCG research database (Pure): http://www.rug.nl/research/portal. For technical reasons the number of authors shown on this cover page is limited to 10 maximum.
Supporting Software Reusability
with Polymorphic Types
Copyright 1995 by B. Laverman
Printed by CopyPrint 2000, Enschede, the Netherlands
RIJKSUNIVERSITEIT GRONINGEN
Supporting Software Reusability
with Polymorphic Types
Proefschrift
ter verkrijging van het doctoraat in de Wiskunde en Natuurwetenschappen
aan de Rijksuniversiteit Groningen op gezag van de
Rector Magnificus Dr F. van der Woude in het openbaar te verdedigen op
maandag 12 juni 1995 des namiddags te 4.00 uur
door
Bert Laverman
geboren op 27 februari 1964 te Rotterdam.
i
Contents
Samenvatting 1 Summary 3 Ackowledgements 5 1 Software Reuse 7 1.1 Introduction : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 7 1.1.1 Reuse : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 8 1.1.2 Reusability : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 9 1.1.3 Reusable Software : : : : : : : : : : : : : : : : : : : : : : : : : : : : 101.2 The Lesson of Reuse : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 11
1.3 Why Software Reuse : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 12
1.3.1 Arguments against Software Reuse : : : : : : : : : : : : : : : : : : : 12
1.3.2 Reasons for Software Reuse : : : : : : : : : : : : : : : : : : : : : : : 14
1.4 The Waterfall Model Revisited : : : : : : : : : : : : : : : : : : : : : : : : : : 14
1.5 Approaches to Software Reuse : : : : : : : : : : : : : : : : : : : : : : : : : : 16
1.5.1 Libraries : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 17
1.5.2 Generators : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 18
1.6 Requirements for Reusable Software: : : : : : : : : : : : : : : : : : : : : : : 18
1.6.1 Specification : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 18
1.6.2 Documentation: : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 19
1.6.3 Code : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 19
1.7 Problems in Software Reuse : : : : : : : : : : : : : : : : : : : : : : : : : : : 20
2 Reuse Support in Programming Languages 23
2.1 Short-circuiting the development: : : : : : : : : : : : : : : : : : : : : : : : : 23
2.2 Reuse support in programming languages : : : : : : : : : : : : : : : : : : : : 24
2.2.1 Pascal & Modula-2 : : : : : : : : : : : : : : : : : : : : : : : : : : : 25
2.2.2 C : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 27
2.2.3 Ada : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 28
2.2.4 SML : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 30
2.2.5 Object oriented programming languages : : : : : : : : : : : : : : : : : 30
2.2.6 The veterans : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 36
2.3 Language evolution : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 37
ii CONTENTS
3 Programming Languages and Types 39
3.1 The Role of Types in Programs: : : : : : : : : : : : : : : : : : : : : : : : : : 39
3.1.1 Types as a Tool for Structuring Data : : : : : : : : : : : : : : : : : : : 39
3.1.2 Types as a Specification Tool : : : : : : : : : : : : : : : : : : : : : : 40
3.1.3 Types as a Consistency Checking Tool: : : : : : : : : : : : : : : : : : 42
3.2 Types and Terminology : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 43
3.3 Type Systems : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 46
3.3.1 Emphasis on Specification : : : : : : : : : : : : : : : : : : : : : : : : 46
3.3.2 Emphasis on Consistency : : : : : : : : : : : : : : : : : : : : : : : : 47
3.3.3 Emphasis on Implementation : : : : : : : : : : : : : : : : : : : : : : 50
4 Reuse Support in Type Systems 53
4.1 Writing Reusable Components : : : : : : : : : : : : : : : : : : : : : : : : : : 53
4.1.1 A Jungle of Languages: : : : : : : : : : : : : : : : : : : : : : : : : : 53
4.1.2 The Quest for Flexibility : : : : : : : : : : : : : : : : : : : : : : : : : 56
4.1.3 The Quest for Vagueness: : : : : : : : : : : : : : : : : : : : : : : : : 57
4.2 Types in Programming Languages : : : : : : : : : : : : : : : : : : : : : : : : 59
4.2.1 The Interpretation of Types : : : : : : : : : : : : : : : : : : : : : : : 60
4.2.2 The Introduction of Subtypes : : : : : : : : : : : : : : : : : : : : : : 62
4.2.3 The Introduction of Generics: : : : : : : : : : : : : : : : : : : : : : : 65
4.3 Polymorphism in Programming : : : : : : : : : : : : : : : : : : : : : : : : : 69
4.3.1 Quantified types : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 70
4.3.2 Known Type vs Wanted Type : : : : : : : : : : : : : : : : : : : : : : 71
4.3.3 Polymorphic Types and Parameterized Types : : : : : : : : : : : : : : 74
4.4 Conclusions : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 75
5 M3--, Discussion and Rationale 77
5.1 A Discussion of the Types inm3-- : : : : : : : : : : : : : : : : : : : : : : : : 78
5.1.1 Ordinal Types : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 78
5.1.2 Constructed Types : : : : : : : : : : : : : : : : : : : : : : : : : : : : 80
5.1.3 Polymorphic Types : : : : : : : : : : : : : : : : : : : : : : : : : : : 82
5.2 Type Equality and Subtyping : : : : : : : : : : : : : : : : : : : : : : : : : : : 88
5.2.1 Ordinal types: : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 89
5.2.2 Constructed types : : : : : : : : : : : : : : : : : : : : : : : : : : : : 90
5.2.3 Polymorphic Types : : : : : : : : : : : : : : : : : : : : : : : : : : : 93
5.2.4 Assignability and Parameter Binding : : : : : : : : : : : : : : : : : : 97
5.3 Modules and Interfaces: : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 98
5.3.1 Declarations : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 99
5.3.2 Constant Declarations : : : : : : : : : : : : : : : : : : : : : : : : : : 100
5.3.3 Type Declarations : : : : : : : : : : : : : : : : : : : : : : : : : : : : 100
5.3.4 Variable Declarations : : : : : : : : : : : : : : : : : : : : : : : : : : 101
5.4 Values and Expressions : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 101
5.5 Statements : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 102
CONTENTS iii
6 M3--, Syntax and Rules 105
6.1 Modules and Interfaces: : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 105
6.2 Types: : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 106
6.2.1 Ordinal Types : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 107
6.2.2 Constructed Types : : : : : : : : : : : : : : : : : : : : : : : : : : : : 109
6.2.3 Polymorphic Types : : : : : : : : : : : : : : : : : : : : : : : : : : : 112
6.2.4 Predefined Type Functions : : : : : : : : : : : : : : : : : : : : : : : : 116
6.2.5 TheNARROWfunction : : : : : : : : : : : : : : : : : : : : : : : : : : : 116
6.3 Declarations : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 116
6.3.1 Constant declarations : : : : : : : : : : : : : : : : : : : : : : : : : : 117
6.3.2 Type declarations and Revelations : : : : : : : : : : : : : : : : : : : : 117
6.3.3 Variable Declarations : : : : : : : : : : : : : : : : : : : : : : : : : : 118
6.4 Statements : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 118
6.5 Expressions : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 118
6.6 Assignability : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 120
7 A Formal Type System for M3-- 123
7.1 Syntax : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 123
7.2 Judgements: : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 124
7.3 Rules : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 125
7.3.1 Environments : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 125
7.3.2 Instantiation : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 125
7.3.3 Transfer of theCurrentType : : : : : : : : : : : : : : : : : : : : : : 126
7.3.4 Types: : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 126
7.3.5 Equality: : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 129
7.3.6 Subtyping: : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 130
7.4 Formulas as Types : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 131
8 Some Examples: M3-- in Action 133
8.1 Some Examples of Code inm3-- : : : : : : : : : : : : : : : : : : : : : : : : : 133
8.2 Two Approaches to Linked Lists : : : : : : : : : : : : : : : : : : : : : : : : : 135
8.2.1 The Generic Approach to Lists: : : : : : : : : : : : : : : : : : : : : : 135
8.2.2 Lists Using Inheritance : : : : : : : : : : : : : : : : : : : : : : : : : 138
8.3 Generic Queues : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 143
8.3.1 The Queue module : : : : : : : : : : : : : : : : : : : : : : : : : : : : 144
8.3.2 An Interface for FIFO Queues : : : : : : : : : : : : : : : : : : : : : : 147
9 Implementing M3-- 149
9.1 Goals of the Implementation : : : : : : : : : : : : : : : : : : : : : : : : : : : 149
9.1.1 Theoretical Aspects : : : : : : : : : : : : : : : : : : : : : : : : : : : 149
9.1.2 Practical Aspects: : : : : : : : : : : : : : : : : : : : : : : : : : : : : 149
9.2 Design Goals : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 150
9.3 The Structure of them3--Compiler : : : : : : : : : : : : : : : : : : : : : : : 151
9.3.1 The Object Hierarchy : : : : : : : : : : : : : : : : : : : : : : : : : : 151
9.3.2 Building the Parse-Tree : : : : : : : : : : : : : : : : : : : : : : : : : 154
9.3.3 Scopes : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 154
iv CONTENTS
9.4.1 Type Management : : : : : : : : : : : : : : : : : : : : : : : : : : : : 156
9.4.2 Type Equality and Subtyping : : : : : : : : : : : : : : : : : : : : : : 157
9.4.3 Generic Types : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 160
9.4.4 Type Inference : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 162
9.4.5 Deciding on Type Checks : : : : : : : : : : : : : : : : : : : : : : : : 164
10 Conclusions 167 10.1 Polymorphism in Programming : : : : : : : : : : : : : : : : : : : : : : : : : 167 10.2 Them3--Language : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 168 10.3 The Implementation ofm3-- : : : : : : : : : : : : : : : : : : : : : : : : : : : 168 10.4 Reusability of Code : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 168 10.5 Further Research : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 169 Bibliography 171 Index 175
v
List of Listings
2.1 The ripple sort algorithm : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 25
2.2 Framework for the ripple sort algorithm in Modular Pascal : : : : : : : : : : : 26
2.3 An Implementation of thebefore()Parameter : : : : : : : : : : : : : : : : : 27
2.4 Ada specification of the list package and the sort routine: : : : : : : : : : : : : 28
2.5 An instantiation of theLISTpackage andLIST_SORT() : : : : : : : : : : : : : 29
2.6 Framework for the ripple sort algorithm in Ada : : : : : : : : : : : : : : : : : 29
2.7 A framework for the simple list type in Eiffel using inheritance : : : : : : : : : 31
2.8 A parameterized list class in OBJ : : : : : : : : : : : : : : : : : : : : : : : : 32
2.9 Ripple Sort in C++ : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 33
3.1 The Modula-3SequenceInterface : : : : : : : : : : : : : : : : : : : : : : : : 40
3.2 The Modula-3SequenceRepInterface : : : : : : : : : : : : : : : : : : : : : : 41
4.1 A GenericBSEARCH()in Ada : : : : : : : : : : : : : : : : : : : : : : : : : : 57
4.2 Appending two lists in Modula-3: : : : : : : : : : : : : : : : : : : : : : : : : 58
4.3 A Generic Queue Package in Ada : : : : : : : : : : : : : : : : : : : : : : : : 65
4.4 A Generic Queue Interface in Modula-3 : : : : : : : : : : : : : : : : : : : : : 65
4.5 An Instantiation of the Generic Queue Interface : : : : : : : : : : : : : : : : : 66
4.6 A Generic Queue in C++ : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 67
4.7 A Generic Queue in SML : : : : : : : : : : : : : : : : : : : : : : : : : : : : 68
4.8 A Generic Queue in OBJ : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 68
4.9 An Opaque Type in Modula-2 : : : : : : : : : : : : : : : : : : : : : : : : : : 70
4.10 Example Procedures with9and S
Parameter Types : : : : : : : : : : : : : : : 72
4.11 Two Record Types in Oberon-2 : : : : : : : : : : : : : : : : : : : : : : : : : 73
5.1 Some Examples of Object Types : : : : : : : : : : : : : : : : : : : : : : : : : 87
5.2 Some Examples of Object Declarations : : : : : : : : : : : : : : : : : : : : : 87
5.3 An Implementation ofMove : : : : : : : : : : : : : : : : : : : : : : : : : : : 88
5.4 Enumerations vs Subranges : : : : : : : : : : : : : : : : : : : : : : : : : : : 89
5.5 Three Examples of record types : : : : : : : : : : : : : : : : : : : : : : : : : 91
5.6 A Generic Procedure with a Record Parameter: : : : : : : : : : : : : : : : : : 91
5.7 Some Examples of Polymorphic Procedures : : : : : : : : : : : : : : : : : : : 92
5.8 Two Modules and an Interface : : : : : : : : : : : : : : : : : : : : : : : : : : 99
5.9 A Generic Function : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 103
8.1 TheGENERICSInterface : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 133
8.2 TheArrayInterface : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 134
vi LIST OF LISTINGS
8.4 A GenericListInterface inm3-- : : : : : : : : : : : : : : : : : : : : : : : : 137
8.5 The Modular PascallistsModule : : : : : : : : : : : : : : : : : : : : : : : 139
8.6 Lists Using Inheritance inm3-- : : : : : : : : : : : : : : : : : : : : : : : : : 141
8.7 TheQueueInterface inm3-- : : : : : : : : : : : : : : : : : : : : : : : : : : : 143
8.8 TheQueueModule inm3-- : : : : : : : : : : : : : : : : : : : : : : : : : : : 144
8.9 AFIFOInterface usingQueue : : : : : : : : : : : : : : : : : : : : : : : : : : 147
9.1 TheBareNodeInterface : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 151
9.2 TheNodeInterface : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 152
9.3 TheScopeInterface : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 154
9.4 TheType.TDeclaration : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 156
9.5 TheType.EqualProcedure : : : : : : : : : : : : : : : : : : : : : : : : : : : 158
9.6 TheisEqualmethod of theArrayType.Tobject type : : : : : : : : : : : : : : 159
9.7 TheisSubtypemethod of theArrayType.Tobject type : : : : : : : : : : : : 160
9.8 The Resolver Method forGenericType.T: : : : : : : : : : : : : : : : : : : : 160
9.9 The Instantiate Method forGenericType.T : : : : : : : : : : : : : : : : : : : 161
9.10 ThedoMatchmethod forOpaqueType.T : : : : : : : : : : : : : : : : : : : : 163
9.11 ThematchParmsmethod forGenericType.T : : : : : : : : : : : : : : : : : : 164
vii
List of Examples
3.1 The Modula-3SequenceModule : : : : : : : : : : : : : : : : : : : : : : : : 40
3.2 Implicit Conversion in C++ : : : : : : : : : : : : : : : : : : : : : : : : : : : 43
3.3 The ORDINAL Type: : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 46
3.4 Truth Values in the Lambda Calculus : : : : : : : : : : : : : : : : : : : : : : 47
3.5 Abstract Data Types in SML : : : : : : : : : : : : : : : : : : : : : : : : : : : 47
3.6 Types in Lawine : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 48
3.7 Implementation Dependent Types : : : : : : : : : : : : : : : : : : : : : : : : 50
3.8 Packed record structures : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 51
4.1 The Modula-3WrModule : : : : : : : : : : : : : : : : : : : : : : : : : : : : 58
4.2 Appending two lists : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 58
4.3 Open array parameters, part 1 : : : : : : : : : : : : : : : : : : : : : : : : : : 62
4.4 Open array parameters, part 2 : : : : : : : : : : : : : : : : : : : : : : : : : : 64
4.5 A Generic Queue: : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 65
4.6 A Generic Queue, part 2 : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 68
4.7 Generic types : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 70
4.8 Opaque types: : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 70
4.9 The Modula-3REFANYType : : : : : : : : : : : : : : : : : : : : : : : : : : : 73
5.1 Subtyping versus Generic Types : : : : : : : : : : : : : : : : : : : : : : : : : 95
ix
List of Rules
6.1 Interface-Module Linkage : : : : : : : : : : : : : : : : : : : : : : : : : : : : 106
6.2 TheMainModule : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 106
6.3 Equality implies Subtyping: : : : : : : : : : : : : : : : : : : : : : : : : : : : 106
6.4 Transitivity of Subtyping : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 107
6.5 Equality for Enumeration Types : : : : : : : : : : : : : : : : : : : : : : : : : 107
6.6 Equality for Subrange Types : : : : : : : : : : : : : : : : : : : : : : : : : : : 107
6.7 The Empty Subrange Type : : : : : : : : : : : : : : : : : : : : : : : : : : : : 107
6.8 Subtyping for Subrange Types : : : : : : : : : : : : : : : : : : : : : : : : : : 108
6.9 TheINTEGERType : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 108
6.10 TheCHARType : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 108
6.11 Equality for Array Types : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 109
6.12 Subtyping for Array Types : : : : : : : : : : : : : : : : : : : : : : : : : : : : 109
6.13 Field Declarations : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 109
6.14 Equality for Record Types : : : : : : : : : : : : : : : : : : : : : : : : : : : : 110
6.15 Subtyping for Record Types : : : : : : : : : : : : : : : : : : : : : : : : : : : 110
6.16 Equality for Pointer Types : : : : : : : : : : : : : : : : : : : : : : : : : : : : 110
6.17 Parameter Declarations: : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 111
6.18 Equality for Procedure Types: : : : : : : : : : : : : : : : : : : : : : : : : : : 111
6.19 Subtyping for Procedure Types : : : : : : : : : : : : : : : : : : : : : : : : : : 111
6.20 Proper Procedures vs Function Procedures : : : : : : : : : : : : : : : : : : : : 111
6.21 Equality for Generic Types : : : : : : : : : : : : : : : : : : : : : : : : : : : : 112
6.22 Subtyping for Generic Types : : : : : : : : : : : : : : : : : : : : : : : : : : : 112
6.23 Usage of Existential Types : : : : : : : : : : : : : : : : : : : : : : : : : : : : 112
6.24 Equality for Existential Types : : : : : : : : : : : : : : : : : : : : : : : : : : 112
6.25 Subtyping for Existential Types : : : : : : : : : : : : : : : : : : : : : : : : : 113
6.26 Usage of Union Types : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 113
6.27 Equality for Union Types: : : : : : : : : : : : : : : : : : : : : : : : : : : : : 113
6.28 Subtyping for Union Types : : : : : : : : : : : : : : : : : : : : : : : : : : : : 113
6.29 Equality for Object Types : : : : : : : : : : : : : : : : : : : : : : : : : : : : 114
6.30 Subtyping for Object Types : : : : : : : : : : : : : : : : : : : : : : : : : : : 114
6.31 The Type of a Method Implementation : : : : : : : : : : : : : : : : : : : : : : 115
6.32 TheANYType : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 115
6.33 TheORDINALType : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 115
6.34 TheROOTType : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 116
6.35 TheTYPEOFtype function : : : : : : : : : : : : : : : : : : : : : : : : : : : : 116
6.36 TheBOUNDtype function : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 116
x LIST OF RULES
6.38 Constant Declarations : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 117
6.39 Opaque Type Declarations : : : : : : : : : : : : : : : : : : : : : : : : : : : : 117
6.40 Revelations: : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 117
6.41 Consistency of Revelations: : : : : : : : : : : : : : : : : : : : : : : : : : : : 117
6.42 Opaque Revelations : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 117
6.43 TheEVALStatement : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 118
6.44 TheRETURNstatement : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 118
6.45 The Selector Expressionss : : : : : : : : : : : : : : : : : : : : : : : : : : : : 119
6.46 Constructor Expressions : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 120
6.47 The Basetype of a Type : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 120
6.48 Assignability with Type Inference : : : : : : : : : : : : : : : : : : : : : : : : 121
1
Samenvatting
Dit proefschrift richt zich op de technische aspecten van hergebruik van software en de –daaraan gerelateerde– problemen rond de constructie van software met een hoge mate van herbruikbaar-heid. Hoewel veel van de problemen rond software hergebruik een psychologische en/of economi-sche achtergrond hebben, zijn er nog steeds vele technieconomi-sche problemen onopgelost. Een van deze technische problemen betreft de programmeertaal die wordt gebruikt voor de feitelijke implemen-tatie, en de ondersteuning die de taal kan bieden bij het schrijven van herbruikbare software. Dit proefschrift introduceert de (programmeer-) taalm3--. De taalm3--is afgeleid van Modula-3[39] en richt zich speciaal op de ondersteuning van de herbruikbaarheid van software door middel van polymorfe typen. De polymorfe typen die hierin worden aangetroffen zijn afgeleid van vergelijk-bare typen uit de getypeerdeλ-calculus[16]. Ik zal verwijzen naar zowel ‘m3--’ als ‘de taalm3--,’ alhoewelm3--niet een ‘af’ produkt is. De lezer mag dit interpreteren als de uitgesproken intentie om de implementatie vanm3--te voltooien. De huidige compiler doet uitsluitend een analyse van de typen in de gegeven programmatekst en geeft aan waar type tests nodig zijn om sterke typering te kunnen garanderen.
Er zijn veel facetten aan de problemen rond software hergebruik, waarvan vele niet eens te maken hebben met de feitelijke constructie van software[48]. In dit proefschrift wordt geargu-menteerd dat er een kloof bestaat tussen, aan de ene kant, specificatie en ontwerp en, aan de andere kant, het eigenlijke programmeren. Er zijn veel manieren waarop software beter geschikt kan wor-den gemaakt voor hergebruik. De mogelijkheid echter, een algoritme zodanig op te schrijven dat deze een hoge mate van herbruikbaarheid bezit, liever dan het bereiken van deze herbruikbaarheid met de hulp van gereedschappen buiten de taal, verhoogt de ontwikkelsnelheid en de kwaliteit van software waarin deze algoritmes worden toegepast. Parameterisatie is het aangewezen middel om de mate van herbruikbaarheid te verhogen[24]. Bij generisch programmeren is het ook mogelijk typen als parameter te gebruiken. In veel huidige programmeertalen wordt generisch program-meren aangepakt met hulpmiddelen op het niveau van de programmatekst. Ik laat zien dat de taal
m3--voldoende generische capaciteiten bevat, en dat deze kunnen worden geı¨mplementeerd tegen acceptabele kosten. Bovendien laat ik zien dat veel polymorfe typen en programmeer constructies die nu in gebruik zijn, expliciet kunnen worden gemaakt inm3--. Als gevolg hiervan kan polymor-fie worden verwijderd uit die delen van het programma waar polymorpolymor-fie niet nodig is, waardoor juist daar een efficie¨ntere implementatie mogelijk wordt.
Het proefschrift begint met een discussie van de problemen rond software hergebruik en her-bruikbaarheid. Hoofdstuk 1 bespreekt software hergebruik en introduceert definities voor de ter-men ‘hergebruik,’ ‘herbruikbaarheid,’ en ‘herbruikbare software.’ Diverse redenen om aan ware hergebruik te doen worden besproken, maar ook enkele, vaak gehoorde, bezwaren tegen soft-ware hergebruik komen aan de orde. De belangrijkste methoden van aanpak voor de invoering van software hergebruik komen daarna, en het hoofdstuk eindigt met een overzicht van een aantal
2 SAMENVATTING
problemen in het gebied.
Hoofdstuk 2 gaat verder met de bespreking van herbruikbaarheid, en bespreekt de waarde van de programmeertaal als hulpmiddel. De aan- of afwezigheid van diverse taalelementen heeft een directe invloed op de mogelijkheden tot verhoging van de herbruikbaarheid van een programma. Implementaties (en schetsen van implementaties) in verschillende programmeertalen van een voor-beeld probleem worden ten tonele gevoerd en vergeleken. Als laatste wordt ook de ‘evolutie’ van een taal besproken als een van de invloeden op de keuze voor een taal bij een gegeven project.
In een module of programmadeel dat bestemd is voor hergebruik worden data over het alge-meen beschreven door middel van een type. Hoofdstuk 3 bespreekt typen in programmeertalen. De rol die typen spelen in een taal –zoals de ontwerpers van de taal deze bedoeld hadden– heeft een grote invloed op het hele type systeem. Hoofdstuk 3 begint daarom met het aanwijzen van de verschillende rollen die typen kunnen spelen. Enkele voorbeelden van programmeertalen waarin zo’n rol aan de orde komt worden gepresenteerd. In dit hoofdstuk komen diverse termen aan de orde die van belang zijn bij de bespreking van typen in programmeertalen. Tot slot worden de drie grote ‘stromingen’ in type systemen besproken met hun respectievelijke voor- en nadelen.
Hoofdstuk 4 neemt opnieuw diverse programmeertalen onder de loupe, zij het nu met een nadruk op hun toepassingsgebieden en de mate waarin ze worden gebruikt. De nadruk wordt ver-der gelegd op type systemen en hun onver-dersteuning voor herbruikbaarheid. Van oudsher bestaat er een controverse tussen de behoefte aan strikte beschrijvingstechnieken enerzijds, en een grote flexibiliteit anderzijds. Deze controverse leidt in het algemeen tot compromissen in het type sys-teem. Het hoofdstuk sluit af met een bespreking van polymorfe programmeertechnieken. Di-verse gebruiksmogelijkheden voor polymorfe typen worden besproken en voor elk wordt een rol aangegeven, varie¨rend van geparameteriseerde typen tot polymorfe variabelen.
Hoofdstuk 5 beschrijft de taalm3--, in het bijzonder zijn polymorfe typen. De taal wordt op informele wijze besproken, met een duidelijke nadruk op typen en hun gebruiksregels. Aangezien
m3--een experiment betreft aangaande typering, zijn zowel expressies als opdrachten onderont-wikkeld en komen ze ook niet uitgebreid aan de orde.
In hoofdstuk 6 wordt de formele syntax vanm3--gegeven samen met een meer formele be-spreking van de gebruiksregels voor typen. In hoofdstuk 7 wordt een formeel type systeem voor
m3--gepresenteerd, zoals tijdens de ontwikkeling vanm3--is ontstaan. Het formele type systeem verschafte diverse nuttige inzichten, en is mogelijk een interessant onderwerp voor verdere studie. In zijn huidige vorm is het niet af.
In hoofdstuk 8 worden enkele voorbeelden van herbruikbare modules inm3--gepresenteerd en worden ze vergeleken met modules geschreven in Modula-3 en Modular Pascal die een ver-gelijkbare functionaliteit bieden. Hoofdstuk 9 beschrijft de implementatie van een compiler voor
m3--, met een duidelijke nadruk op de afhandeling van de type informatie en controle.
In hoofdstuk 10 wordt dit proefschrift afgesloten met diverse conclusies betreffende polymorf programmeren, de taalm3--, en de mogelijkheden diem3--biedt voor het bouwen van herbruik-bare software. Tevens worden enkele punten voor verdere studie aangegeven.
3
Summary
This thesis concerns the technical problems of software reuse and the –related– problem of con-structing highly reusable software. Although several of the problems of software reuse are to be found in the fields of psychology and/or economics, the technical problems are there to be solved as well. One of these technical problems concerns the actual programming language used for im-plementation, and its support for expressing highly reusable software. This thesis introduces the (programming) languagem3--. Them3--language is based on Modula-3[39] and has been de-signed to support software reusability through the availability of polymorphic types. The poly-morphic types presented are based on similar types from the typedλ-calculus[16]. I will refer to either ‘m3--’ or ‘the languagem3--’, althoughm3--is not a finished programming language such as Modula-3, Pascal, or Ada. The reader may interpret this as the outspoken intent to –one day– finish the implementation of them3--language. The current compiler only performs the analysis of types in the program and indicates where type tests are needed to be able to guarantee strong typing.
The problem of software reuse has many facets, many of which do not even concern the ac-tual software construction process[48]. In this thesis it is argued that there exists a gap between on one hand specification and design, and on the other hand the actual coding. There are several approaches to enhancing the reusability of software. However, actually being able to express an algorithm in such a way as to make it highly reusable, rather than achieve this reusability by the application of tools, will allow a faster development, and support a higher quality of software using these algorithms. Parameterization is taken as the tool for achieving this level of reusability[24].
Generic programming extends the parameterization possibilities to parameterization with types.
In many languages currently in use generic programming tends to be solved with source-level tools. I will show that them3--language offers a sufficient generic capability which can be im-plemented in code at acceptable costs. Furthermore I will show that many polymorphic types and programming constructs in wide use now can be made explicit inm3--. This makes it possible to remove this polymorphism from those places where it is not needed and will allow especially those parts to be implemented more efficiently.
The thesis starts by looking at the problem of software reuse and -reusability. Chapter 1 dis-cusses the field of reuse, introducing definitions for the terms ‘reuse,’ ‘reusability’ and ‘reusable software.’ Several points in favor of practicing reuse are discussed, but also some often raised ob-jections against the reuse of software will be discussed. Major approaches to implementing soft-ware reuse are presented next, and the chapter ends with a discussion of several problems in the field.
Chapter 2 continues the discussion of reusability issues by looking at the value of a program-ming language as a tool. The presence or absence of certain language elements has a direct influ-ence on the possibilities to enhance the reusability of a piece of code. Implementations (or
imple-4 SUMMARY
mentation frameworks) in several programming languages of an example problem are presented and compared. Also the ‘evolution’ of programming languages is discussed as one of the aspects which play a role during the choice of a language for a certain project.
In a module or section of code to be reused, data are generally described by types. Chapter 3 discusses types in programming languages. The role a type is to play –as intended by the lan-guage’s designers– influences the entire type system. The chapter therefore starts by identifying the possible roles of types and presents some examples in languages that have adopted such roles. The chapter continues with the introduction of several useful terms for discussing types. Finally it describes the three major ‘kinds’ of type systems, discussing the chosen role of types and their strengths and weaknesses.
Chapter 4 again takes a look at several programming languages, but now the emphasis is on their application domains and their success. Emphasis is placed on type systems and their support for software reusability. There exists a conflict between the need for strict descriptions as well as –at the same time– a high flexibility. This conflict generally leads to compromises in the type system. The chapter ends with a discussion of polymorphism in programming. Several uses for polymorphic types are discussed and a role is assigned to each, ranging from parameterized types to polymorphic variables.
Chapter 5 discusses the languagem3--, especially its polymorphic types. The language is in-formally discussed, with a clear emphasis on the type system and its rules. Sincem3--is in ef-fect an experiment in polymorphic types, statements and expressions are underdeveloped and only lightly discussed.
Chapter 6 gives the formal syntax ofm3--and a more formal description of the rules for type compatibility. In chapter 7 a formal type system for m3--is presented, which was constructed during the development of m3--. The formal type system provided several useful insights and may be an interesting subject for study in itself. As it is now, it is essentially incomplete.
Chapter 8 gives several examples of reusable modules inm3--, comparing them with modules written in Modula-3 and Modular Pascal that provide similar functionality. Chapter 9 discusses the implementation of a compiler form3--, with the emphasis on the handling of types and type checks.
In chapter 10 several conclusions are presented concerning polymorphic programming, the
m3--language and its ability to support the construction of reusable software. Also some direc-tions for further research are given.
5
Ackowledgements
I would like to start by expressing my thanks to everyone somehow involved with this thesis and my research. Several people must have been quite surprised that a Computer Science Engineer would like to know more about typedλ-calculus. The fact that a small poll among the PhD stu-dents at the Department of Computing science placed me as the one with the most practice-oriented project shows that nobody was fooled, and indeed I caused some type theorists a few surprises with my application of their favourite subject. I will always treasure Jan Terlouw’s reaction at my dismissal of certain possibilities in ‘my’ type system, on the grounds that they were impractical (“Ah... Yes... Hmmmm... That would make it easier to handle”). I would also like to mention Luca Cardelli, who not only inspired a lot of my work through the numerous articles by his hand on polymorphism and formal type systems, but was also willing to take a shot at an early version of my thesis. I hope he is not too disappointed about the way section 4.3.3 turned out. Further thanks go to the PhD students at the CS Department; we had some nice times together.
I cannot help but mention Prof. D. Graham Stuart, who got me started on the idea of getting a PhD. Although I did not actually write my thesis on work related to his research, I hope he will be happy to hear that I did it anyway.
This thesis would probably have never come into being without the initial invitation by Prof. Bron to me, offering me the possibility to become an AIO at Groningen University. His invita-tion brought me back from Japan to the Netherlands and allowed me to pick up my studies in In-formatics. He aimed me at the subject of software reuse and let me go. After an initial –very broad-spectrum– study on everything related to software reuse, I soon ‘returned’ to my old love: programming languages, their design and their implementation. I suspect he had some very mixed feelings about my visible lack of progress at first, followed by even more mixed feelings when I shifted into high gear on Modula-3, generic programming, and type theory. His constant questions on my motives, not as much on my research subject as well on my ‘modus operandi’ Modula-3, of-ten forced me to think about what I was actually doing and where it would lead me. Since I ended up with taking a standpoint very close to his own views on software engineering, supporting the view of software construction as an art, I hope he won’t be too disappointed I did not use Modular Pascal.
Next I would like to thank my parents. Both are of the medical profession and my father must have been somewhat disappointed that I preferred the study of computing over the study of man. He never showed his disappointment however, and supported me not only in my studies, but also in a rather unusual, and not very well thought through, trip to Japan. I am tremendously relieved he always expressed his happiness that I was studying what I wanted to study, even if in a somewhat unusual manner. He managed to hold back his lectures on my responsibility until I actually got married, after which I could only agree with him.
Japan has had a profound effect on me, not in the least through my choice of partner. Supported by the determination of her father to let her go into an uncertain future, my wife moved from Japan
6 ACKOWLEDGEMENTS
to join me in Groningen in the end of 1992, after which we got married in january 1993. Her father died of cancer shortly before her departure and has not been able to realize his strong intention to visit Europe. I wish he had been given a little more time, if only so I would have been able to thank him fully for what he has done for us. Endless thanks of course go to my wife Rie. Given the rather small base of support for a family I offered, she never complained that I preferred getting a PhD over finding a well-paid job. I therefore dedicate this work to her.