一句话:将双向类型检查推广为上下文类型化,传播更丰富的上下文信息以减少标注,并用 QTAS 定量刻画可标注性。
双向类型检查通过在检查过程中传播已知类型信息,以轻量方式支持多种类型系统并减少明显标注,但其推断能力有限,且遇到子类型、交集类型、记录、重载等复杂特性时会出现困难。本文提出上下文类型化(contextual typing),将传播的信息从已知类型推广为关于项周围上下文的已知信息,例如其他项或记录标签,从而在保持轻量与可扩展的同时进一步减少标注;在带子类型的系统中,上下文包含关系也比双向子类型包含更具表达力,因为部分已知的上下文信息可被利用。为便于规约这类系统,作者引入定量类型赋值系统(QTAS),用计数器量化项通过类型检查所需的类型信息量,泛化了传统双向类型中只能表示检查/推断两种模式的 mode,并将可标注性准则形式化为定理。论文先在简单类型 lambda 演算上展示该方法,随后扩展到含子类型、交集类型、记录与重载的演算,全部元理论均在 Agda 证明助手中形式化。
Bidirectional typing is a simple, lightweight approach to type inference that propagates known type information during typing, and can scale up to many different type systems and features. It typically only requires a reasonable amount of annotations and eliminates the need for many obvious annotations. Nonetheless the power of inference is still limited, and complications arise in the presence of more complex features. In this paper we present a generalization of bidirectional typing called contextual typing . In contextual typing not only known type information is propagated during typing, but also other known information about the surrounding context of a term. This information can be of various forms, such as other terms or record labels. Due to this richer notion of contextual information, less annotations are needed, while the approach remains lightweight and scalable. For type systems with subtyping, contextual typing subsumption is also more expressive than subsumption with bidirectional typing, since partially known contextual information can be exploited. To aid specifying type systems with contextual typing, we introduce Q uantitative T ype A ssignment S ystems (QTASs). A QTAS quantifies the amount of type information that a term needs in order to type check using counters. Thus, a counter in a QTAS generalizes modes in traditional bidirectional typing, which can only model an all (checking mode) or nothing (inference mode) approach. QTASs enable precise guidelines for annotatability of contextual type systems formalized as a theorem. We illustrate contextual typing first on a simply typed lambda calculus, and then on a richer calculus with subtyping, intersection types, records and overloading. All the metatheory is formalized in the Agda theorem prover.