In type theory and programming language theory, a typing rule is an inference rule that specifies sufficient conditions for a typing judgment to hold. A common form of typing judgment is
Γ⊢e:τ, read as “under typing context Γ, expression e has type τ”. A collection of typing rules normally defines a typing relation inductively: a judgment holds when it has a finite derivation constructed from the rules. A program is well-typed when its required top-level typing judgment can be derived. Typing rules are relational specifications rather than necessarily functions from expressions to types. Depending on the type system, an expression may have no derivable type, one type, or several types. Rules may be presented as a declarative mathematical specification or organized into a procedure for type checking or type inference. A common algorithmic organization is bidirectional typing, which distinguishes rules that synthesize a type from rules that check an expression against a type already known from its context.
Judgments, contexts and derivations The context Γ records assumptions needed to type the free variables or other names occurring in an expression. In a simple type system it is often represented as a finite mapping from variables to types:
Γ=x1:τ1,…,xn:τn. The notation Γ,x:τ means that the context has been extended with the assumption that x has type τ. A closed expression, having no free variables, is commonly typed under the empty context ∅. More elaborate systems may use several contexts or auxiliary judgments. These can record type variables, kinds, locations, constraints, effects, modules, or resources. Examples of auxiliary judgments include type formation, type equality, subtyping, and kinding. The structure of a context is also significant: ordinary structural type systems often treat contexts as finite mappings, whereas linear and other substructural systems may make order or multiplicity relevant. An inference rule is conventionally written
J1⋯JnJ, where J1,…,Jn are the rule's premises and J is its conclusion. The rule states that the conclusion may be derived whenever all its premises have been derived. A rule with no premises is an axiom. Rules can also contain side conditions, such as a freshness requirement or a membership test on a context. Typing rules are generally rule schemes: their metavariables range over all expressions, types, and contexts of the language. A typing derivation is a finite tree whose nodes are judgments, whose root is the judgment being established, and whose children are the premises of the rule used at that node. The inductive structure of derivations is later used in proofs about the type system.
Example: simply typed lambda calculus Consider a version of the simply typed lambda calculus with booleans. Its types and expressions can be described by the grammars
τ::=𝖡𝗈𝗈𝗅∣τ→τ and
e::=x∣𝗍𝗋𝗎𝖾∣𝖿𝖺𝗅𝗌𝖾∣𝐢𝐟 e 𝐭𝐡𝐞𝐧 e 𝐞𝐥𝐬𝐞 e∣λx:τ.e∣e e. Boolean constants are typed by axiom schemes:
Γ⊢𝗍𝗋𝗎𝖾:𝖡𝗈𝗈𝗅(T-True)Γ⊢𝖿𝖺𝗅𝗌𝖾:𝖡𝗈𝗈𝗅(T-False). A conditional requires a Boolean condition and branches having the same type:
Γ⊢e1:𝖡𝗈𝗈𝗅Γ⊢e2:τΓ⊢e3:τΓ⊢𝐢𝐟 e1 𝐭𝐡𝐞𝐧 e2 𝐞𝐥𝐬𝐞 e3:τ(T-If). Variables, abstractions, and applications are typed by the following rules:
x:τ∈ΓΓ⊢x:τ(T-Var) Γ,x:τ1⊢e:τ2Γ⊢λx:τ1.e:τ1→τ2(T-Abs) Γ⊢e1:τ1→τ2Γ⊢e2:τ1Γ⊢e1e2:τ2(T-App). These rules are schemes because Γ, e, and the types may be replaced by arbitrary instances. For example, the rules derive
∅⊢(λx:𝖡𝗈𝗈𝗅.x)𝗍𝗋𝗎𝖾:𝖡𝗈𝗈𝗅. The abstraction rule checks the body under a context extended with an assumption about the bound variable. In its conclusion, that assumption has been abstracted into the function type. In proof-theoretic terms, T-Abs is an introduction rule for the function type and T-App is its elimination rule. This correspondence with the introduction and elimination rules of natural deduction is part of the Curry–Howard correspondence.
Let binding A monomorphic let construct can be specified by
Γ⊢e1:τ1Γ,x:τ1⊢e2:τ2Γ⊢𝐥𝐞𝐭 x=e1 𝐢𝐧 e2:τ2. Here x is available in e2 at the single type τ1. This is not the full rule for Hindley–Milner let-polymorphism. In a Hindley–Milner system, eligible type variables in the type of e1 can be generalized into a type scheme, whose different occurrences may subsequently be instantiated at different types.
Declarative and algorithmic presentations A declarative type system characterizes which typing judgments are valid without necessarily prescribing how to find their derivations. A declarative rule may leave some information to be chosen nondeterministically. For example, a subsumption rule in a system with subtyping may be written
Γ⊢e:ττ<:σΓ⊢e:σ(Sub). The conclusion does not indicate at which point subsumption should be applied or which intermediate type should be chosen. Similarly, a rule that instantiates a universally quantified type can require a choice of the type used for the instantiation. A rule system is syntax-directed, relative to a chosen division between inputs and outputs, when the input components of a judgment determine at most one rule that can apply. In a simple expression language, this often means that the outermost syntactic constructor—variable, abstraction, application, conditional, and so on—selects the rule. Syntax-directed rules can frequently be translated directly into recursive type-checking code. Rules such as unrestricted subsumption are not syntax-directed because they can be inserted without regard to the outer form of the expression. An algorithmic presentation resolves or controls such choices and specifies how information flows through the rules. It is normally related to the declarative presentation by metatheoretic results:
Soundness: every judgment produced by the algorithmic system is valid in the declarative system. Completeness: every relevant declaratively valid input is accepted by the algorithmic system, sometimes after explicit type annotations have been inserted. Termination is additionally required for the rules to constitute a decision procedure. A classic example is the Hindley–Milner type system. Damas and Milner first give a declarative inference system and then a distinct procedure, Algorithm W, for computing type schemes. They prove that the algorithm is sound and complete with respect to their inference system and that it computes a principal type scheme for every typable expression in the language considered.
… excerpt ends here. Continue reading the full article.