2018-04-17 Let Arguments Go First
Total Page:16
File Type:pdf, Size:1020Kb
Let Arguments Go First Ningning Xie, Bruno C. d. S. Oliveira The University of Hong Kong ESOP 2018, Thessaloniki, Greece 2018-04-17 1 Background 2 Bi-Directional Type Checking • Well known in the folklore of type system for a long time • Popularized by Pierce and Turner’s work* • Can support many type system features: refinements, indexed types, intersections and unions, contextual modal types, object-oriented subtyping, … * Benjamin C Pierce and David N Turner. Local type inference. TOPLAS, 22(1):1–44, 2000. 3 Bi-Directional Type Checking Two Modes in type-checking • Inference (synthesis): e synthesizes A Γ e A ` ) • Checked: check e against A Γ e A ` ( • Which constructs should be in which mode? 4 Let Arguments Go First Ningning Xie and Bruno C. d. S. Oliveira The University of Hong Kong nnxie,bruno @cs.hku.hk { } Abstract. Bi-directional type checking has proved to be an extremely useful and versatile tool for type checking and type inference. The con- ventional presentation of bi-directional type checking consists of two modes: inference mode and checked mode. In traditional bi-directional type-checking, type annotations are used to guide (via the checked mode) the type inference/checking procedure to determine the type of an ex- pression, and type information flows from functions to arguments. 21 This paper presents a variant of bi-directional type checking where the type information5.1 flows Combining from arguments Application to functions. and This Checkedvariant retains Modes the inference mode, but adds a so-called application mode. Such design can remove annotationsAlthough that the basicapplication bi-directional mode type provides checking us cannot, with alternative design choices in and is useful whena bi-directional type information type from system, arguments a checked is required mode to type- can still be easily added. One check the functionsmotivation being applied. for the We checked present mode two applications would be annotated and de- expressions e : A,where velop the meta-theorythe type (mostly of expressions verified in is Coq) known of the and application is therefore mode. used to check expressions. Consider adding e : A for introducing the third checked mode for the lan- 1 Introductionguage in Section 3. Notice that, since the checked mode is stronger than appli- cation mode, when entering checked mode the application context is no longer Bi-directional typeuseful. checking Instead has been we use known application in the folklore subtyping of type to satisfy systems the application context for a long time. Itrequirements. was popularized A possible by Pierce typing and Turner’s rule for work annotation on local expressions type is: inference [29]. Local type inference was introduced as an alternative to Hindley- Milner (henceforth HM system) type systems A [11<, 17: ],B whichΓ coulde easilyA deal ` ` ( T-Ann with polymorphic languages with subtyping. Bi-directionalΓ p (e : A type) checkingB is one component of local type inference that, aided by some` type) annotations, en- ables type inferenceHere, in ane expressiveis checked language using its with annotation polymorphismA, and and then subtyp- we instantiate A to B using ing. Since Pierce andBisubtyping Turner’s-Directional with work, application various other context authorsType . have provedChecking the ef- fectiveness of bi-directionalNow we type can checking have a in rule several set of other the settings, checked including mode for all expressions. For many di↵erent systemsexample, with one subtyping useful rule [12, for15, abstractions14], systems in with checked dependent mode could be Abs-Chk, * types [37, 10• ,A2 ,recipe21,where3], and thefrom various parameter Dunfield other works typeandA [1,serves 13Pfenning, 28, as7, the22]. typeFurthermore, of x, and typing checks the bi-directional typebody checking with hasB. also Also, been combined combined with with the HM-style information techniques flow, the checked rule for for providing type• Introduction inferenceapplication in the checks presencerules the function of higher-ranked with the types full type. [27, 14]. The key idea in bi-directional type checking( is simple. In its basic form typing is split into inferenceΓ and,x: Acheckede modes.B The mostΓ saliente2 featureA ofΓ a bi-e1 A B directional type-checker is when` information( Abs-Chk deduced from` inference) mode` is ( ! App-Chk Γ λx. e A B Γ e e B used to guide checking` of an expression( ! in checked mode. One of such interactions` 1 2 ( between modes• happensEliminationNote in the that typing addingrules rule expression for function annotations applications: might bring convenience for pro- grammers, since annotations) can be moreNotice freely placed here inhow a program. type For exam- Γ e1 A B Γ e2 A ple,` (λf). f 1)! : (Int `Int) ( Int APPbecomesinformation valid. However flows this from does not add Γ e1 e2 !B ! expressive power,` since) programs that are typeablefunctions under to expression arguments annotations, * Joshua Dunfieldwouldand remain Frank Pfenning typeable. Tridirectional aftertypechecking moving. POPL’04, the annotations 2004. to bindings. For example 5 the previous program is equivalent to (λf :(Int Int). f 1). ! This discussion is a sketch. We have not defined the corresponding declarative system nor algorithm. However we believe that the addition of a checked mode will not bring surprises to the meta-theory. 5.2 Additional Constructs In this section, we show that the application mode is compatible with other constructs, by discussing how to add support for pairs in the language given in Section 3. A similar methodology would apply to other constructs like sum types, data types, if-then-else expressions and so on. The introduction rule for pairs must be in the inference mode with an empty application context. Also, the subtyping rule for pairs is as expected. Γ e1 A Γ e2 B A1 <: B1 A2 <: B2 ` ) ` ) T-Pair S-Pair Γ (e ,e ) (A, B) (A ,A ) <: (B ,B ) ` 1 2 ) 1 2 1 2 33 –– AA new new design design for for type type inference inference of of higher-ranked higher-ranked types types whichwhich gen- gen- eralizeseralizes the the HM HM type type system, system, supports supports a a polymorphic polymorphic letlet asas syntactic syntactic sugar, sugar, andand infers infers higher higher rank rank types. types. We We present present a a syntax-directed syntax-directed specification, specification, an an elaborationelaboration semantics semantics to to System System F, F, some some meta-theory meta-theory in in Coq, Coq, and and an an algo- algo- rithmicrithmic type type system system with with completeness completeness and and soundness soundness proofs. proofs. –– AA System-F-like System-F-like calculus calculusasas a a theoretical theoretical response response to to the the challenge challenge noted noted byby Pierce Pierce and and Turner Turner [ [2929].]. It It shows shows that that the the application application mode mode is is compatible compatible withwith type type applications, applications, which which also also enables enables encoding encoding type type declarations. declarations. We We presentpresent a a type type system system and and meta-theory, meta-theory, including including proofs proofs of of type type safety safety and and uniquenessuniqueness of of typing typing in in Coq. Coq. 2Overview2Overview 2.12.1 Background: Background: Bi-Directional Bi-Directional Type Type Checking Checking TraditionalTraditional type type checking checking rules rules can can be be heavyweight heavyweight on on annotations, annotations, in in the the sense sense thatthat lambda-bound lambda-bound variables variables always always need need explicit explicit annotations. annotations. Bi-directional Bi-directional typetype checking checking [ [2929]] provides provides an an alternative, alternative, which which allows allows types types to to propagate propagate down- down- wardward the the syntax syntax tree. tree. For For example, example, in in the the expression expression ((λλff::IntInt IntInt.. ff)()(λλyy.. !! yy)),, the the type type of of yy isis provided provided by by the the type type annotation annotation on on ff.. This This is is supported supported by by thethe bi-directional bi-directional typing typing rule rule for for applications: applications: ΓΓ ee11 AA BB ΓΓ ee22 AA `` )) !! `` (( APPAPP ΓΓ ee ee BB `` 11 22 )) Specifically,Specifically, if if we we know know that that the the type type of ofee11 isis a a function function from from AA BB,, we we can can check check !! thatthat ee22 hashas type type AA.. Notice Notice that that here here the the type type information information flows flows from from functions functions toto arguments. arguments. OneOne guideline guideline for for designing designing bi-directional bi-directional type type checking checking rules rules [ [1515]istodis-]istodis- tinguishtinguishBi introduction introduction-Directional rules rules from from elimination eliminationType rules. rules. Constructs ConstructsChecking which which correspond correspond toto introduction introduction forms forms are are checkedchecked againstagainst a a given given type, type, while while constructs constructs cor- cor- respondingresponding to to elimination elimination forms forms inferinfer (or(or synthesize) synthesize) their their types. types. For For instance, instance, • Considerunderunder this this design designdesigning principle, principle, rules the the introduction introduction for pairs rule rule for for pairs pairs is is supposed supposed to to be be in in checkedchecked