Masters-Thesis-Lasse-Blaauwbroek
Total Page:16
File Type:pdf, Size:1020Kb
Eindhoven University of Technology MASTER On the interaction between unrestricted union and intersection types and computational effects Blaauwbroek, L. Award date: 2017 Link to publication Disclaimer This document contains a student thesis (bachelor's or master's), as authored by a student at Eindhoven University of Technology. Student theses are made available in the TU/e repository upon obtaining the required degree. The grade received is not published on the document as presented in the repository. The required complexity or quality of research of student theses may vary by program, and the required minimum study period may vary in duration. General rights Copyright and moral rights for the publications made accessible in the public portal are retained by the authors and/or other copyright owners and it is a condition of accessing publications that users recognise and abide by the legal requirements associated with these rights. • Users may download and print one copy of any publication from the public portal for the purpose of private study or research. • You may not further distribute the material or use it for any profit-making activity or commercial gain Technical University Eindhoven Department of Mathematics and Computer Science Master’s Thesis On the Interaction Between Unrestricted Union and Intersection Types and Computational Effects Lasse Blaauwbroek Eindhoven, August 2017 Supervisor prof.dr. Herman Geuvers Advisors prof.dr. Tim Willemse prof.dr. Alexander Serebrenik Master’s Thesis Contents Contents 1. Introduction4 2. Background6 2.1. Unrestricted Union and Intersection Types.................6 _ 2.1.1. Informal Treatment of the Semantics of λ^ .............8 _ 2.1.2. The Feature-subsuming Power of λ^ ................. 10 _ 2.1.3. Challenges of λ^ ............................ 15 2.2. Models for Computational Effects...................... 22 2.2.1. Pass by Reference............................ 23 2.2.2. Reference Types............................. 23 2.2.3. Pass by Sharing............................. 27 3. Preliminaries 29 _ 4. Formalization of λ^ 32 + 4.1. Algebraic Data Types λ× ............................ 33 4.1.1. Definitions................................ 33 + 4.1.2. Correctness Properties of λ× ..................... 35 + 4.1.3. Strong Normalization of λ× ...................... 39 _ 4.2. Unrestricted Union and Intersection Types λ^ ................ 44 4.2.1. Definitions................................ 44 _ 4.2.2. Correctness Properties of λ^ ...................... 52 _ 5. Formalization of λ^ with Mutations 60 + 5.1. Mutable Algebraic Data Types λ× ....................... 61 5.1.1. Definitions................................ 61 5.1.2. A Notion of Equivalence........................ 63 + 5.1.3. Correctness Properties of λ× ..................... 67 + 5.1.4. Turing Completeness of λ× with Mutations............. 71 _ 5.2. Mutable Unrestricted Union and Intersection Types λ^ .......... 75 5.2.1. Definitions................................ 75 _ 5.2.2. Correctness Properties of λ^ ...................... 76 _ 5.2.3. Turing Completeness of λ^ with Mutations............. 79 6. Conclusion and Further Work 80 A. Generic Variables 86 2 Technical University Eindhoven Contents Master’s Thesis B. Generic Types 87 C. Definitions for Algebraic Data Types 88 D. Proofs for Algebraic Data Types 93 E. Definitions for Unrestricted Union and Intersection Types 99 F. Proofs for Unrestricted Union and Intersection Types 108 G. Definitions for Mutable Algebraic Data Types 113 H. Proofs for Mutable Algebraic Data Types 119 I. Definitions for Mutable Union and Intersection Types 125 J. Proofs for Mutable Union and Intersection Types 134 Department of Mathematics and Computer Science 3 Master’s Thesis 1. Introduction 1. Introduction Within the academic domain of programming language research the never-ending quest is to synthesize models for languages that are evermore expressive while still being simple to reason about and convenient to use for a programmer. A big part of this is the design of type-systems that can type as many “desired” programs as possible. Traditionally, an important tool to do this is parametric polymorphism. Much research exists on this topic and many (functional) programming languages offer this facility, including the convenience of ML-style type-inference. A close cousin of parametric polymorphism is the notion of intersection types, which is essentially a discrete version of parametric polymorphism. This paradigm is very powerful in the sense that it is able to subsume many programming language features that would otherwise have to be introduced separately. However, although it was invented in the eighties, this paradigm has not received nearly as much attention. This can partially be contributed to the fact that the expressivity of intersection types is a double-edged sword. Many of the desired properties of type-systems, such as type-inference, do not hold for intersection types, and need to be worked around. For this reason, intersection types have historically not been used in the design of programming languages in the industry. Only in recent times have we started to see some languages, such as the Ceylon programming language, adopt simple versions of intersection types due to advancements in its theory. However, as we will see, quite some issues remain that need to solved in order to enable broad applicability. In this thesis, we are interested in a λ-calculus outfitted with a very general notion of intersection types, usually referred to as unrestricted intersection types. Together with its dual, union types, this makes for a very interesting calculus that has only recently started to be researched in-depth. We will give an overview of the current state-of-the-art of this calculus, both of its (potential) uses and the challenges that need to be solved. Then we will focus on one particular challenge, namely the integration of computational effects with unrestricted union and intersection types. Within the world of functional program- ming languages, the most prevalent method of introducing computational effects is through ML-style reference types. For this reason, the combination of intersection types and computational effects has always been studied within this paradigm of reference types. As we will show, a naive combinations of intersection types and computational effects leads to a calculus that does not preserve typing under evaluation. Existing research focuses on finding ways to restrict the expressiveness of this calculus so that this subject reduction property can be restored. In this thesis, we propose that a slightly different model for computational effects should be used instead. Instead of reference types, we use a model called pass-by-sharing. Although this terminology is relatively obscure, this 4 Technical University Eindhoven Master’s Thesis is the model that is used by all major object oriented programming languages such as Java and C#. We will show that by using pass-by-sharing instead of reference types, all of the issues around the subject reduction property vanish, while allowing for a more expressive type-system. To that end, we present a formalization of a λ-calculus outfitted with unrestricted union and intersection types, using the pass-by-sharing style of computational effects, and prove that this combination is well-behaved. Apart from a paper proof, we also present a mechanical proof, formalized in the Coq proof assistant. This thesis is organized as follows. Chapter2, contains a treatment of the relevant background information. In Section 2.1, we give an overview of intersection types, how they might be used and their challenges. Then, in Section 2.2, we discuss different models for computational effects, and show how they interact with intersection types. Chapter4 contains a formalization of unrestricted union and intersection types, which is extended with computational effects in Chapter5. Department of Mathematics and Computer Science 5 Master’s Thesis 2. Background 2. Background In this chapter, we provide some background on the concept of unrestricted union and intersection types and how they compare to different models of computational effects. 2.1. Unrestricted Union and Intersection Types The concept of a λ-calculus with intersection types can be traced back independently to Coppo and Dezani-Ciancaglini [1] and Pottinger [2]. The original purpose of this calculus was the ability to type exactly all strongly normalizing λ-terms, see Section 2.1.3.1 on page 15. This was mainly meant as a theoretical exercise to increase the number of typable terms. Later, it it became clear that intersection types are also of interest for practical programming languages. We will therefore begin our motivation for intersection types with an example of a more practical nature. Consider a λ-calculus outfitted with natural numbers and booleans. We are interested in the following λ-term M ≡ λ f . f 5 + f true. This term accepts a function f , applies it to both a number and a boolean and finally sums the result of these applications. We would now like to give this term a type. In the Simply Typed Lambda Calculus (STLC), it is not difficult to see that this is impossible, because we need f to be a function that is simultaneously of type N ! N and of type B ! N. One solution to this problem is to use parametric polymorphism [3, p. 316], in which case we can type f as 8α.α ! N. This then enables us to give M the type (8α.α ! N) ! N. A downside of this approach is that the type 8α.α ! N is far more restrictive than required. A function of this type needs to be able to handle any kind of input, while M only applies it to booleans and natural numbers. The idea behind intersection types is to solve this problem by typing M as (N ! N ^ N ! B) ! N (note that intersections have weaker precedence than arrows). The intersection type-constructor τ ^ σ literally means that a term is both of type τ and of type σ. This is not unlike the concept of overloading as is present in many programming languages such as Java. As we will see later, this paradigm is very powerful.