Bidirectional Typing

Bidirectional Typing

Bidirectional Typing JANA DUNFIELD, Queen’s University, Canada NEEL KRISHNASWAMI, University of Cambridge, United Kingdom Bidirectional typing combines two modes of typing: type checking, which checks that a program satisfies a known type, and type synthesis, which determines a type from the program. Using checking enables bidirectional typing to support features for which inference is undecidable; using synthesis enables bidirectional typing to avoid the large annotation burden of explicitly typed languages. In addition, bidirectional typing improves error locality. We highlight the design principles that underlie bidirectional type systems, survey the development of bidirectional typing from the prehistoric period before Pierce and Turner’s local type inference to the present day, and provide guidance for future investigations. ACM Reference Format: Jana Dunfield and Neel Krishnaswami. 2020. Bidirectional Typing. 1, 1 (November 2020), 37 pages. https: //doi.org/10.1145/nnnnnnn.nnnnnnn 1 INTRODUCTION Type systems serve many purposes. They allow programming languages to reject nonsensical programs. They allow programmers to express their intent, and to use a type checker to verify that their programs are consistent with that intent. Type systems can also be used to automatically insert implicit operations, and even to guide program synthesis. Automated deduction and logic programming give us a useful lens through which to view type systems: modes [Warren 1977]. When we implement a typing judgment, say Γ ` 4 : 퐴, is each of the meta-variables (Γ, 4, 퐴) an input, or an output? If the typing context Γ, the term 4 and the type 퐴 are inputs, we are implementing type checking. If the type 퐴 is an output, we are implementing type inference. (If only 4 is input, we are implementing typing inference [Jim 1995; Wells 2002]; if 4 is output, we are implementing program synthesis.) The status of each meta-variable—input or output—is its mode. As a general rule, outputs make life more difficult. In complexity theory, it is often relatively easy to check that a given solution is valid, but finding (synthesizing) a solution may be complex or even undecidable. This general rule holds for type systems: synthesizing types may be convenient for the programmer, but computationally intractable. To go beyond the specific feature set of traditional Damas–Milner typing, it might seem necessary 1 arXiv:1908.05839v2 [cs.PL] 14 Nov 2020 to abandon synthesis . Instead, however, we can combine synthesis with checking. In this approach, bidirectional typing, language designers are not forced to choose between a rich set of typing features and a reasonable volume of type annotations: implementations of bidirectional type systems alternate between treating the type as input, and treating the type as output. The practice of bidirectional typing has, at times, exceeded its foundations: the first commonly cited paper on bidirectional typing appeared in 1997 but mentioned that the idea was known as “folklore” (see Section 10). Over the next few years, several bidirectional systems appeared, but the 1We choose to say synthesis instead of inference. This is less consistent with one established usage, “type inference”, but more consistent with another, “program synthesis”. Authors’ addresses: Jana Dunfield, Queen’s University, School of Computing, Goodwin Hall 557, Kingston, ON, K7L3N6, Canada, [email protected]; Neel Krishnaswami, University of Cambridge, Computer Laboratory, William Gates Building, Cambridge, CB3 0FD, United Kingdom, [email protected]. 2020. XXXX-XXXX/2020/11-ART $15.00 https://doi.org/10.1145/nnnnnnn.nnnnnnn , Vol. 1, No. 1, Article . Publication date: November 2020. :2 Jana Dunfield and Neel Krishnaswami principles used to design them were not always made clear. Some work did present underlying design principles—but within the setting of some particular type system with other features of interest, rather than focusing on bidirectional typing as such. For example, Dunfield and Pfenning [2004] gave a broadly applicable design recipe for bidirectional typing, but their focus was on an idea—typing rules that decompose evaluation contexts—that has been applied more narrowly. Our survey has two main goals: (1) to collect and clearly explain the design principles of bidirectional typing, to the extent they have been discovered; and (2) to provide an organized summary of past research related to bidirectional typing. We begin by describing a tiny bidirectional type system (Section2). Section3 presents some design criteria for bidirectional type systems. Section4 describes a modified version of the recipe of Dunfield and Pfenning[2004], and relates it to our design criteria. Section5 discusses work on combining (implicit) polymorphism with bidirectional typing, and Section6 surveys other variations on bidirectional typing. Sections7–8 give an account of connections between bidirectional typing and topics such as proof theory, focusing and polarity, and call-by-push-value. Section9 cites other work that uses bidirectional typing. We conclude with historical notes (Section 10) and a summary of notation (Section 11). 2 BIDIRECTIONAL SIMPLY TYPED LAMBDA CALCULUS To develop our first bidirectional type system, we start with a (non-bidirectional) simply typed lambda calculus (STLC). This STLC is not the smallest possible calculus: we include a term () of type unit, to elucidate the process of bidirectionalization. We also include a gratuitous “type equality” rule. Our non-bidirectional STLC (Figure1, left side) has six rules deriving the judgment Γ ` 4 : 퐴: a variable rule, a type equality rule, a rule for type annotations, an introduction rule for unit, an introduction rule for ! and an elimination rule for !. These rules are standard except for the type equality rule TypeEq, which says that if 4 has type 퐴 and 퐴 equals 퐵, then 4 has type 퐵. If this rule does not disturb you, you may skip to the next paragraph. If we had polymorphism, we could motivate this rule by arguing that 8U.U ! U should be considered equal to 8V.V ! V. Our language of types, however, has only unit and ! and so admits no nontrivial equalities. The role of TypeEq is retrospective: it is the type assignment version of a necessary bidirectional rule, allowing us to tell a more uniform story of making type assignment bidirectional. For now, we only note that the type equality rule is sound: for example, if we have derived Γ ` 4 : unit ! unit then Γ ` 4 : unit ! unit, which was already derived. Given these six STLC typing rules, we produce each bidirectional rule in turn (treating the type equality rule last). Some of our design choices will become clear only in the light of the “recipe” in Section4, but the recipe would not be clear without seeing a bidirectional type system first. (1) The variable rule Var has no typing premise, so our only decision is whether the conclusion should synthesize 퐴 or check against 퐴. The information that G has type 퐴 is in Γ, so we synthesize 퐴. A checking rule would require that the type be known from the enclosing term, a very strong restriction: even 5 G would require a type annotation. (2) From the annotation rule Anno we produce Anno), which synthesizes its conclusion: we have the type 퐴 in (4 : 퐴), so we do not need 퐴 to be given as input. In the premise, we check 4 against 퐴; synthesizing 퐴 would prevent the rule from typing a non-synthesizing 4, which would defeat the purpose of the annotation. , Vol. 1, No. 1, Article . Publication date: November 2020. Bidirectional Typing :3 Expressions 4 ::= G j _G. 4 j 4 4 j () Types 퐴, 퐵,퐶 ::= unit j 퐴 ! 퐴 Typing contexts Γ ::= · j Γ,G : 퐴 Γ ` 4 ( 퐴 Under Γ, expression 4 checks against type 퐴 Under context Γ, Γ ` 4 : 퐴 Γ ` 4 ) 퐴 Under Γ, expression 4 synthesizes type 퐴 expression 4 has type 퐴 ¹G : 퐴º 2 Γ ¹G : 퐴º 2 Γ Var Var) Γ ` G : 퐴 Γ ` G ) 퐴 Γ ` 4 : 퐴 퐴 = 퐵 Γ ` 4 ) 퐴 퐴 = 퐵 TypeEq Sub( Γ ` 4 : 퐵 Γ ` 4 ( 퐵 Γ ` 4 : 퐴 Γ ` 4 ( 퐴 Anno Anno) Γ ` (4 : 퐴) : 퐴 Γ ` (4 : 퐴) ) 퐴 unitI unitI( Γ ` () : unit Γ ` () ( unit Γ,G : 퐴1 ` 4 : 퐴2 Γ,G : 퐴1 ` 4 ( 퐴2 !I !I( Γ ` ¹_G. 4º : 퐴1 ! 퐴2 Γ ` ¹_G. 4º( 퐴1 ! 퐴2 Γ ` 41 : 퐴 ! 퐵 Γ ` 42 : 퐴 Γ ` 41 ) 퐴 ! 퐵 Γ ` 42 ( 퐴 !E !E) Γ ` 41 42 : 퐵 Γ ` 41 42 ) 퐵 Fig. 1. A simply typed _-calculus (: judgment) and a bidirectional version () and ( judgments) (3) Unit introduction unitI checks. At this point in the paper, we prioritize internal consistency: checking () is consistent with the introduction rule for ! (discussed next), and with the introduction rule for products. (4) Arrow introduction !I checks. This decision is better motivated: to synthesize 퐴1 ! 퐴2 for _G. 4 we would have to synthesize a type for the body 4. That raises two issues. First, by requiring that the body synthesize, we would need a second rule to handle _s whose body checks but does not synthesize. Second, if we are synthesizing 퐴1 ! 퐴2 that means we don’t know 퐴1 yet, which prevents us from building the context Γ,G : 퐴1. (Placeholder mechanisms, which allow building Γ,G : Uˆ and “solving” Uˆ later, are described in Section5.) Since the conclusion is checking, we know 퐴2, so we might as well check in the premise. (5) For arrow elimination !E, the principal judgment is the premise Γ ` 41 : 퐴 ! 퐵, because that premise contains the connective being eliminated. We make that judgment synthesize; this choice is the one suggested by our “recipe”, and happens to work nicely: If 41 synthesizes 퐴 ! 퐵, we have 퐴 and can check the argument (so the rule will work even when 42 cannot synthesize), and we have 퐵 so we can synthesize 퐵 in the conclusion.

View Full Text

Details

  • File Type
    pdf
  • Upload Time
    -
  • Content Languages
    English
  • Upload User
    Anonymous/Not logged-in
  • File Pages
    37 Page
  • File Size
    -

Download

Channel Download Status
Express Download Enable

Copyright

We respect the copyrights and intellectual property rights of all users. All uploaded documents are either original works of the uploader or authorized works of the rightful owners.

  • Not to be reproduced or distributed without explicit permission.
  • Not used for commercial purposes outside of approved use cases.
  • Not used to infringe on the rights of the original creators.
  • If you believe any content infringes your copyright, please contact us immediately.

Support

For help with questions, suggestions, or problems, please contact us