Principal Type Schemes for Gradual Programs with Updates and Corrections Since Publication (Latest: May 16, 2017)
Total Page:16
File Type:pdf, Size:1020Kb
Principal Type Schemes for Gradual Programs With updates and corrections since publication (Latest: May 16, 2017) Ronald Garcia ∗ Matteo Cimini y Software Practices Lab Indiana University Department of Computer Science [email protected] University of British Columbia [email protected] Abstract to Siek and Taha (2006) has been used to integrate dynamic checks Gradual typing is a discipline for integrating dynamic checking into into a variety of type structures, including object-oriented (Siek a static type system. Since its introduction in functional languages, and Taha 2007), substructural (Wolff et al. 2011), and ownership it has been adapted to a variety of type systems, including object- types (Sergey and Clarke 2012). The key technical pillars of grad- oriented, security, and substructural. This work studies its applica- ual typing are the unknown type, ?, the consistency relation among tion to implicitly typed languages based on type inference. Siek gradual types, and support for the entire spectrum between purely and Vachharajani designed a gradual type inference system and dynamic and purely static checking. A gradual type checker rejects algorithm that infers gradual types but still rejects ill-typed static type inconsistencies in programs, accepts statically safe code, and programs. However, the type system requires local reasoning about instruments code that could plausibly be safe with runtime checks. type substitutions, an imperative inference algorithm, and a subtle This paper applies the gradual typing approach to implicitly correctness statement. typed languages, like Standard ML, OCaml, and Haskell, where This paper introduces a new approach to gradual type inference, a type inference (a.k.a. type reconstruction) procedure is integral to type checking. This problem was investigated first by Siek and driven by the principle that gradual inference should only produce ?α static types. We present a static implicitly typed language, its grad- Vachharajani (2008), defining λ! , a gradual type inference system and algorithm that infers gradual types, but rejects ill-typed static ual counterpart, and a type inference procedure. The gradual sys- 1 tem types the same programs as Siek and Vachharajani, but has a programs. This groundbreaking paper outlines principles that a modular structure amenable to extension. The language admits let- gradual implicitly typed language should satisfy, demonstrates how polymorphism, and its dynamics are defined by translation to the several plausible approaches fail, and ultimately produces a com- Polymorphic Blame Calculus (Ahmed et al. 2009, 2011). pelling type system and type inference algorithm. Though the re- The principal types produced by our initial type system mask sults satisfy the criteria for gradual typing, the type system requires the distinction between static parametric polymorphism and poly- special care in its handling of type variables, subtle statements morphism that can be attributed to gradual typing. To expose this and proofs of correctness, and a special-purpose imperative infer- difference, we distinguish static type parameters from gradual type ence algorithm. These complexities make it unclear how to adopt, parameters and reinterpret gradual type consistency accordingly. adapt, and extend this approach with modern features of implic- The resulting extension enables programs to be interpreted using itly typed languages, like let-polymorphism, row-polymorphism, either the polymorphic or monomorphic Blame Calculi. and first-class polymorphism. Furthermore, extending an existing implicitly typed language implementation with support for gradual typing would require a rewrite or substantial overhaul of the type 1. Introduction inferencer. Interest in integrating static and dynamic checking is increasing To support the extension of implicitly typed languages with among language researchers and industrial language designers. support for gradual typing, this paper introduces a new foundation (e.g. (Bierman et al. 2010; Gronski et al. 2006; Swamy et al. 2014; for gradual implicit typing. In particular, we make the following Tobin-Hochstadt and Felleisen 2008; Wrigstad et al. 2010)) Among contributions: the proposed foundations for such languages, gradual typing, due 1. We introduce a specification of static implicit typing that em- ∗ Partially funded by an NSERC discovery grant. phasizes the input-output modes of the type system. In particu- lar the types of subterms are viewed as opaque, and we rely on y Partially funded by NSF grant 1360694. partial functions to structure the system. This structure leads to a natural conception of gradual implicit typing that is easy to Permission to make digital or hard copies of all or part of this work for personal or reason about and extend. The key insight underlying the design classroom use is granted without fee provided that copies are not made or distributed is to only allow fully static types to be implicit: gradual types, for profit or commercial advantage and that copies bear this notice and the full citation in our approach, must originate in the program text. on the first page. Copyrights for components of this work owned by others than the author(s) must be honored. Abstracting with credit is permitted. To copy otherwise, or 2. Siek and Vachharajani’s type system is based explicitly on in- republish, to post on servers or to redistribute to lists, requires prior specific permission ferring gradual types and emphasizing type precision, a sub- and/or a fee. Request permissions from [email protected]. POPL ’15, January 15–17, 2015, Mumbai, India. Copyright is held by the owner/author(s). Publication rights licensed to ACM. 1 Rastogi et al. (2012) also combine gradual typing and type inference, ACM 978-1-4503-3300-9/15/01. $15.00. but focus on improving performance rather than detecting inconsistencies http://dx.doi.org/10.1145/2676726.2676992 (Sec. 13). stantially different conceptual foundation. Nonetheless, we x : T 2 Γ (Tx) (Tn) (Tb) prove that the two foundations coincide: both type systems Γ ` x : T Γ ` n : Int Γ ` b : Bool accept exactly the same programs. Γ ` t1 : T1 Γ ` t2 : T2 dom(T1) = T2 3. We define a corresponding constraint typing judgment and con- (Tapp) Γ ` t t : cod(T ) straint solver. Central to its design is that we choose to limit the 1 2 1 type inference problem to deducing only static types. In addi- Γ ` t1 : T1 Γ ` t2 : T2 Γ ` t3 : T3 T1 = Bool tion to the standard equality constraints between static types, (Tif) Γ ` if t1 then t2 else t3 : equate(T2;T3) we require consistency constraints between gradual types; our solver naturally extends unification to support them. Γ ` t : T Γ ` t : T T = Int T = Int (T+) 1 1 2 2 1 2 4. To confirm the extensibility of the type system, we extend Γ ` t1 + t2 : Int it with support for let-polymorphism, using the standard ap- Γ; x : T1 ` t : T2 proach off the shelf. We give the language dynamics by trans- (Tλ) Γ ` (λx.t): T ! T lating it to the Polymorphic Blame Calculus (Ahmed et al. 1 2 2009, 2011). The translation mirrors the analogous translation Γ; x : T1 ` t : T2 Γ ` t : TT = T1 of Hindley/Milner typing to System F, suggesting that recent (Tλ:) (T::) Γ ` (λx : T1:t): T1 ! T2 Γ ` (t :: T1): T1 approaches to first-class polymorphism may also apply. 5. We observe that the principal types of the initial system mask dom : TYPE * TYPE cod : TYPE * TYPE the distinction between static parametric polymorphism and dom(T1 ! T2) = T1 cod(T1 ! T2) = T2 polymorphism due to gradual typing. To expose this difference, dom(T ) undefined otherwise cod(T ) undefined otherwise we distinguish static type parameters from gradual type param- eters and reinterpret gradual type consistency accordingly. The equate : TYPE × TYPE * TYPE resulting language can be interpreted using either a monomor- equate(T;T ) = T phic or polymorphic intermediate language. equate(T1;T2) undefined otherwise 2. Implicit Typing and Type Inference Figure 2. Static Language (ITSL): Type System This section briefly introduces the Implicitly Typed Static Lan- guage (ITSL). Its main purpose is to introduce the core type system The type system definition pays particular attention to the mode that we extend with gradual typing, as well as to briefly review the of the typing judgment, in the sense of logic programming (Debray principles of implicit typing. We highlight some particular details and Warren 1988). In particular, the type context and term are in- of our presentation that are important to the overall approach. terpreted as inputs to the typing judgment, while the term’s type is viewed as an output. The impact of this interpretation on the struc- ture of the definition is as follows, and can be seen in the (Tapp) A 2 TPARAM;T 2 TYPE; x 2 VAR; b 2 BOOL; rule. Though the structure of the terms t in the conclusions of rules n 2 Z; t 2 TERM; v 2 VALUE Γ 2 VAR * TYPE T ::= A j Int j Bool j T ! T (types) are analyzed using pattern-matching-style syntax, the type position t ::= n j t + t j b j if t then t else t (terms) of each typing judgment in the premise is stated abstractly as some j x j λx.t j λx : T:t j t t j t :: T type T . For instance, the type rule for application usually has a v ::= n j b j x j λx.t j λx : T:t (syntactic values) premise Γ ` t1 : T11 ! T12, but in contrast the (Tapp) rule only assumes that the result is a type T1. In lieu of pattern matching on a Figure 1. Static Language (ITSL): Syntax function type, the rules appeal to partial functions that are defined only for types with the proper shape. These partial functions name Fig. 1 presents the syntax for ITSL. By implicit typing we the result type of a term (e.g., cod(T1)) and assert properties that 2 mean that the language is statically typed, but programmers may must hold between interacting types (e.g., dom(T1) = T2).