Lightweight Linear Types in System F°
Total Page:16
File Type:pdf, Size:1020Kb
University of Pennsylvania ScholarlyCommons Departmental Papers (CIS) Department of Computer & Information Science 1-23-2010 Lightweight linear types in System F° Karl Mazurak University of Pennsylvania Jianzhou Zhao University of Pennsylvania Stephan A. Zdancewic University of Pennsylvania, [email protected] Follow this and additional works at: https://repository.upenn.edu/cis_papers Part of the Computer Sciences Commons Recommended Citation Karl Mazurak, Jianzhou Zhao, and Stephan A. Zdancewic, "Lightweight linear types in System F°", . January 2010. Karl Mazurak, Jianzhou Zhao, and Steve Zdancewic. Lightweight linear types in System Fo. In ACM SIGPLAN International Workshop on Types in Languages Design and Implementation (TLDI), pages 77-88, 2010. doi>10.1145/1708016.1708027 © ACM, 2010. This is the author's version of the work. It is posted here by permission of ACM for your personal use. Not for redistribution. The definitive version was published in ACM SIGPLAN International Workshop on Types in Languages Design and Implementation, {(2010)} http://doi.acm.org/10.1145/1708016.1708027" Email [email protected] This paper is posted at ScholarlyCommons. https://repository.upenn.edu/cis_papers/591 For more information, please contact [email protected]. Lightweight linear types in System F° Abstract We present Fo, an extension of System F that uses kinds to distinguish between linear and unrestricted types, simplifying the use of linearity for general-purpose programming. We demonstrate through examples how Fo can elegantly express many useful protocols, and we prove that any protocol representable as a DFA can be encoded as an Fo type. We supply mechanized proofs of Fo's soundness and parametricity properties, along with a nonstandard operational semantics that formalizes common intuitions about linearity and aids in reasoning about protocols. We compare Fo to other linear systems, noting that the simplicity of our kind-based approach leads to a more explicit account of what linearity is meant to capture, allowing otherwise-conflicting interpretations of linearity (in particular, restrictions on aliasing versus restrictions on resource usage) to coexist peacefully. We also discuss extensions to Fo aimed at making the core language more practical, including the additive fragment of linear logic, algebraic datatypes, and recursion. Disciplines Computer Sciences Comments Karl Mazurak, Jianzhou Zhao, and Steve Zdancewic. Lightweight linear types in System Fo. In ACM SIGPLAN International Workshop on Types in Languages Design and Implementation (TLDI), pages 77-88, 2010. doi>10.1145/1708016.1708027 © ACM, 2010. This is the author's version of the work. It is posted here by permission of ACM for your personal use. Not for redistribution. The definitive version was published in ACM SIGPLAN International Workshop on Types in Languages Design and Implementation, {(2010)} http://doi.acm.org/ 10.1145/1708016.1708027" Email [email protected] This other is available at ScholarlyCommons: https://repository.upenn.edu/cis_papers/591 Lightweight Linear Types in System F◦ Karl Mazurak Jianzhou Zhao Steve Zdancewic University of Pennsylvania fmazurak,jianzhou,[email protected] Abstract Given these success stories, it is perhaps surprising that we have ◦ yet to see general linear types seriously considered for inclusion in We present System F , an extension of System F that uses kinds to 1 distinguish between linear and unrestricted types, simplifying the a mainstream functional programming language. But alas, linear use of linearity for general-purpose programming. We demonstrate types can easily lead to awkward programming models and poten- through examples how System F◦ can elegantly express many use- tially complicated language designs that are difficult both to imple- ment and to program with. This paper seeks to address these issues ful protocols, and we prove that any protocol representable as a ◦ DFA can be encoded as an F◦ type. We supply mechanized proofs by introducing System F —pronounced “F-pop”—a language that ◦ is intended to be a simple foundation for practical linear program- of System F ’s soundness and parametricity properties, along with ◦ a nonstandard operational semantics that formalizes common intu- ming. Rather than aiming at one particular problem, System F lets itions about linearity and aids in reasoning about protocols. programmers enforce their own protocol abstractions through the We compare System F◦ to other linear systems, noting that the power of linearity and polymorphism, yet its typing discipline is lightweight enough to expose in a surface language. simplicity of our kind-based approach leads to a more explicit ac- ◦ count of what linearity is meant to capture, allowing otherwise- System F is simply the Girard–Reynolds polymorphic λ- conflicting interpretations of linearity (in particular, restrictions on calculus [14, 23] extended with two base kinds: ?, classifying aliasing versus restrictions on resource usage) to coexist peace- ordinary, unrestricted types, and ◦, classifying linear types. A sub- fully. We also discuss extensions to System F◦ aimed at making kinding relation ? ≤ ◦ makes explicit the observation that values the core language more practical, including the additive fragment of unrestricted types may safely be treated linearly; i.e., since vari- of linear logic, algebraic datatypes, and recursion. ables of unrestricted type may be used any number of times and linear variables must be used exactly once, it is always safe to use Categories and Subject Descriptors D.3.3 [Programming Lan- unrestricted variables as though they were linear. Any System F◦ guages]: Language Constructs and Features expression with kinds erased is a well-typed System F expression. In introducing System F◦, this paper contributes the following: General Terms Design, Languages, Theory • The design of System F◦, and in particular its use of kinds and Keywords Linear logic, Polymorphism, Type systems kind subsumption, which is lightweight and structured so as to integrate well with existing functional languages. (Section 2) 1. Introduction • Mechanized proofs of standard soundness and parametricity re- sults for System F◦, which ensure that the properties functional Linear logic [15, 16] models resource usage by restricting the prop- programmers rely on continue to hold. (Section 2.1) erties of contraction (the ability to duplicate a resource) and weak- ◦ ening (the ability to discard a resource). In the context of program- • A second, linearity-aware semantics for System F , which for- ming languages, ideas from linear logic were quickly adopted, at malizes common intuitions about linearity and shows that these first to eliminate garbage collection [21] and shortly thereafter to intuitions do indeed hold. (Section 4) handle mutable state [29]. • Several examples—including all regular languages—along Since their introduction, variants, refinements, and improve- with extensions and proposals for compiler support, remark- ments on linear type systems have been proposed for many ap- able primarily for their simplicity, which showcase System F◦’s plications, including explicit memory management and control of potential usefulness. (Sections 3 and 5) aliasing [2, 13, 17, 28, 36], capabilities [9, 10], and tracking state In the rest of this section we discuss the design of System F◦ in changes for program analysis [11, 32]. Of particular interest is work ◦ on typestates, which ensure that a sequence of API calls is well- the context of prior work, and we showcase System F ’s ability to behaved [13, 12], and on session types, which check that the end- enforce programmer-defined protocols with a familiar example. points of a channel agree on the next message to be sent or re- 1.1 Prior work: Linear type system design considerations ceived [18, 25, 27]. Walker has a more comprehensive survey [33]. There are many variants on linear type systems in the litera- ture [33], but the crucial design decision from our perspective is how linear and unrestricted variables are differentiated and how that mechanism interacts with polymorphism. Our use of kinds Permission to make digital or hard copies of all or part of this work for personal or and kind subsumption is intended to capture the essence of linear- classroom use is granted without fee provided that copies are not made or distributed for profit or commercial advantage and that copies bear this notice and the full citation ity simply and generally while remaining faithful to the standard on the first page. To copy otherwise, to republish, to post on servers or to redistribute to lists, requires prior specific permission and/or a fee. 1 Clean is often seen as the exception to this, but there are subtle differences TLDI’10 January 23, 2010, Madrid, Spain. between its uniqueness types, which concern aliasing, and standard linear Copyright c 2010 ACM 978-1-60558-891-9/10/01. $5.00 types (see Section 3.1). semantics and programming model of System F. This division be- become overwhelming if exposed to the programmer. As we are tween linear and unrestricted types is inspired by that proposed for incorporating the full power of System F, exposing at least some monomorphic systems by Wadler [29] and Benton [5]. Here we types is unavoidable; we also believe that System F◦ convincingly contrast our approach with some alternatives from the literature. demonstrates that just the simple distinction between linear and un- Broadly speaking, there are two other approaches to distinguish-