Contextual Polymorphism
Total Page:16
File Type:pdf, Size:1020Kb
Contextual Polymorphism by Glen Jerey Ditcheld A thesis presented to the University of Waterloo in fullment of the thesis requirement for the degree of Do ctor of Philosophy in Computer Science Waterloo Ontario Canada c Glen Jerey Ditcheld I hereby declare that I am the sole author of this thesis I authorize the University of Waterloo to lend this thesis to other institutions or individuals for the purp ose of scholarly research I further authorize the University of Waterloo to repro duce this thesis by photo copying or by other means in total or in part at the request of other institutions or individuals for the purp ose of scholarly research ii The University of Waterloo requires the signatures of all p ersons using or photo copying this thesis Please sign b elow and give address and date iii Abstract Programming languages often provide for various sorts of static type checking as a way of detecting invalid programs However statically checked type systems often require unnecessarily sp ecic type information which leads to frustratingly inexible languages Polymorphic type systems restore exibility by allowing entities to take on more than one type This thesis discusses p olymorphism in statically typed programming languages It provides precise denitions of the term p olymorphism and for its varieties adhoc p olymorphism uni versal p olymorphism inclusion p olymorphism and parametric p olymorphism and surveys and compares many existing mechanisms for providing p olymorphism in programming languages Finally it introduces a new p olymorphism mechanism contextual polymorphism Contextual p olymorphism is a variant of parametric p olymorphism that is based on contexts which are ab stractions of collections of declarations and assertions which link p olymorphic routines to the environments that call them Contexts themselves provide a useful structuring mechanism for software b ecause they represent the notions and relationships b etween notions that program mers have in mind when they design programs Contextual p olymorphism avoids many problems asso ciated with other p olymorphism mechanisms while preserving their b enets The formal denition of these language constructs is given in terms of an extension of F the order p oly morphic typed lambda calculus The practicality of the constructs is shown by a discussion of Cforall an extension of the programming language C that supp orts contextual p olymorphism iv Acknowledgements I would like to thank Professor Don Cowan Professor Doug Lea and Professor Bruno Preiss for agreeing to serve on my examining committee and for their many helpful suggestions and questions Professor Gord Cormack Professor Dominic Duggan Dr John Ophel and Dr Bob Zarnke for comments and conversations stretching back over many years and my sup ervisor Professor Peter Buhr for not letting me quit v Dedication To Helen Cameron for the past and the future vi Contents Introduction Denitions and Notations Desirable Prop erties Eciency Strong Static Interface Checking Expressiveness Separate Interfaces Co de Reuse Simple Base Language Polymorphism Polymorphism Mechanisms AdHoc Mechanisms Overloading Transfer Functions SetTheoretic Union Types Universal Mechanisms Inclusion Polymorphism Innite Unions vii Record Subtyping Statically Typed Ob jectOriented Languages Parametric Polymorphism Universal Quantication FBounded Quantication Op eration Inference Descriptive Classes Parameterized Mo dules Summary Contextual Polymorphism Denition of Contexts and Assertions Types and Kinds Environments Contexts and Assertions Judgements Type Judgements Kind Judgements Sp ecialization Argument Inference Use of Contexts and Assertions Guidance Abstract Sup erclasses Context Hierarchies Abstract Types Existential Types Inheritance Contexts and Polymorphism viii Subtypes and FBounded Quantication Parameterized Mo dules Descriptive Classes Summary Notes on Cforall Overloading Lvalues Polymorphism Type Abstraction Contexts and Assertions Lo cal Routines Overload Resolution Implicit Conversion Safe Conversions Conversion Cost Degrees of Polymorphism Overload Resolution Rules Summary of Overload Resolution Opaque Types Assertions and Type Declarations Context Examples C Types Scalar Arithmetic and Integral Types Mo diable Types Pointer and Array Types Relationships Between Op erations Relational and Equality Op erators Arithmetic and Integer Op erations ix Conclusions Future Work Bibliography x Chapter Introduction Expressiveness and safety are two imp ortant criteria for judging programming languages In formally the expressiveness of a language measures the variety of useful programs that can b e written with it and its safety measures the variety of meaningless programs that can not b e written with it These attributes can conict with each other to some extent and part of the art of programming language design lies in balancing their demands Many languages provide strong type checking as a safety feature Every data item has an asso ciated type which denes the set of op erations that can b e applied to the data if a program attempts to apply an op eration to data of the wrong type the languages translator or runtime system detects and rep orts the error as opp osed to silently allowing the program to attempt a meaningless computation Type checking can o ccur statically or dynamically Static checking o ccurs b efore program execution based on information ab out the types of values deduced from the text of the program In some programming languages the actual type of a value during execution may dier from its statically determined type In those cases a static type checker must make conservative assumptions Dynamic checking o ccurs during execution and can use exact type information For some programs a programmer may b e able to prove that a program will encounter no type errors when it executes even though that theorem is b eyond the capabilities of a static type checker for that programming language In such cases dynamic type checking provides greater expressiveness However programmers rarely attempt pro ofs of dynamic type safety b ecause of their dicult nature and programming errors are notoriously plentiful so static type checking provides greater safety This conict has led to a search for programming languages that are statically checkable but CHAPTER INTRODUCTION less restrictive Ideally no useful semantically welldened computation would b e statically type unsafe Polymorphic languages aim for that goal by allowing programmers to dene computations that are valid for op erands of many dierent types This thesis examines p olymorphic languages in detail The remainder of this chapter denes some useful terms and gives some criteria for comparing languages Chapter two reviews the kinds of p olymorphism and surveys a number of existing p olymorphism mechanisms Chapter three discusses a novel p olymorphism mechanism called contextual p olymorphism Chapter four demonstrates contextual p olymorphism in the context of Cforall an extension of the C program ming language Denitions and Notations A set of values that can b e represented by a computer and that is distinguishable from all other such sets is a type Dierent languages use dierent rules to distinguish b etween sets One programming language