Typing rule

From HandWiki
Short description: How a type system assigns a type to a syntactic construction

In type theory, a typing rule is an inference rule that describes how a type system assigns a type to a syntactic construction.[1]:94 These rules may be applied by the type system to determine if a program is well-typed and what type expressions have. A prototypical example of the use of typing rules is in defining type inference in the simply typed lambda calculus, which is the internal language of Cartesian closed categories.[2]

Notation

Typing rules specify the structure of a typing relation that relates syntactic terms to their types.[1]:92 Syntactically, the typing relation is usually denoted by a colon, so for example [math]\displaystyle{ e:\tau }[/math] denotes that an expression [math]\displaystyle{ e }[/math] has type [math]\displaystyle{ \tau }[/math]. The rules themselves are usually specified using the notation of natural deduction.[1]:26 For example, the following typing rules specify the typing relation for a simple language of booleans:[1]:93

[math]\displaystyle{ \frac{}{\mathsf{true} : \mathsf{Bool}} \qquad \frac{}{\mathsf{false} : \mathsf{Bool}} \qquad \frac{e_1 : \mathsf{Bool} \quad \; e_2 : \tau \quad \; e_3 : \tau}{\mathbf{if}\ e_1\ \mathbf{then}\ e_2\ \mathbf{else}\ e_3 : \tau} }[/math]

Each rule states that the conclusion below the line may be derived from the premises above the line. The first two rules have no premises above the line, so they are axioms. The third rule has premises above the line (specifically, three premises), so it is an inference rule.

In programming languages, the type of a variable depends on where it is bound, which necessitates context-sensitive typing rules. These rules are given by a typing judgment, usually written [math]\displaystyle{ \Gamma \vdash e : \tau }[/math], which states that an expression [math]\displaystyle{ e }[/math] has type [math]\displaystyle{ \tau }[/math] under a typing context [math]\displaystyle{ \Gamma }[/math] that relates variables to their types. Typing contexts are occasionally supplemented by the types of individual variables; for example, [math]\displaystyle{ \Gamma,x{:}\tau_1 \vdash e : \tau_2 }[/math] can be read as "the context [math]\displaystyle{ \Gamma }[/math] supplemented by the information that the expression [math]\displaystyle{ x }[/math] has type [math]\displaystyle{ \tau_1 }[/math] yields the judgement that expression [math]\displaystyle{ e }[/math] has type [math]\displaystyle{ \tau_2 }[/math]". This notation can be used to give typing rules for variable references and lambda abstraction in the simply typed lambda calculus:[1](pp101–102)

[math]\displaystyle{ \frac{x{:}\tau \in \Gamma}{\Gamma \vdash x : \tau} \qquad \frac{\Gamma,x{:}\tau_1 \vdash e : \tau_2}{\Gamma \vdash (\lambda x{:}\tau_1.\,e) : \tau_1 \rightarrow \tau_2} }[/math]

Similarly, the following typing rule describes the [math]\displaystyle{ \mathbf{let} }[/math] construct of Standard ML:

[math]\displaystyle{ \frac{\Gamma \vdash e_1 : \tau_1 \qquad \Gamma, x{:}\tau_1 \vdash e_2 : \tau_2}{ \Gamma \vdash \mathbf{let}\ x = e_1\ \mathbf{in}\ e_2\ \mathbf{end} : \tau_2} }[/math]

Not all systems of typing rules directly specify a type checking algorithm. For example, the typing rule for applying a parametrically polymorphic function in the Hindley–Milner type system requires "guessing" the appropriate type at which the function should be instantiated.[3] Adapting a declarative rule system to a decidable algorithm requires the production of a separate, algorithmic system that can be proven to specify the same typing relation.[4]

See also

References

  1. 1.0 1.1 1.2 1.3 1.4 Pierce, Benjamin C. (2002). Types and Programming Languages (1st ed.). Cambridge, Mass.: MIT Press. ISBN 0262162091. 
  2. Baez, John. "The n-Category Café" (in en). https://golem.ph.utexas.edu/category/2006/08/cartesian_closed_categories_an_1.html. 
  3. Clément, Dominique; Despeyroux, Thierry; Kahn, Gilles; Despeyroux, Joëlle (8 August 1986). "A simple applicative language: Mini-ML". Proceedings of the 1986 ACM conference on LISP and functional programming - LFP '86. Association for Computing Machinery. pp. 13–27. doi:10.1145/319838.319847. ISBN 0897912004. https://dl.acm.org/doi/10.1145/319838.319847. 
  4. Dunfield, Jana; Krishnaswami, Neel (23 May 2021). "Bidirectional Typing". ACM Computing Surveys 54 (5): 98:19. doi:10.1145/3450952. ISSN 0360-0300. 

Further reading