Typing rule explained

In type theory, a typing rule is an inference rule that describes how a type system assigns a type to a syntactic construction.[1] 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. Syntactically, the typing relation is usually denoted by a colon, so for example

e:\tau

denotes that an expression

e

has type

\tau

. The rules themselves are usually specified using the notation of natural deduction. For example, the following typing rules specify the typing relation for a simple language of booleans:
  
true:Bool
  
false:Bool
e1:Bool    e2:\tau    e3:\tau
ife1 thene2 elsee3:\tau

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

\Gamma\vdashe:\tau

, which states that an expression

e

has type

\tau

under a typing context

\Gamma

that relates variables to their types. Typing contexts are occasionally supplemented by the types of individual variables; for example,

\Gamma,x{:}\tau1\vdashe:\tau2

can be read as "the context

\Gamma

supplemented by the information that the expression

x

has type

\tau1

yields the judgement that expression

e

has type

\tau2

". This notation can be used to give typing rules for variable references and lambda abstraction in the simply typed lambda calculus:
x{:
\tau

\in\Gamma}{\Gamma\vdashx:\tau}   

\Gamma,x{:
\tau

1\vdashe:\tau2}{\Gamma\vdash(λx{:}\tau1.e):\tau1\tau2}

Similarly, the following typing rule describes the

let

construct of Standard ML:
\Gamma\vdashe1:\tau1    \Gamma,x{:
\tau

1\vdashe2:\tau2}{ \Gamma\vdashletx=e1 ine2 end:\tau2}

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

Further reading

Notes and References

  1. Book: Pierce . Benjamin C. . Types and Programming Languages . 2002 . MIT Press . Cambridge, Mass. . 0262162091 . 1st.
  2. Web site: Baez . John . The n-Category Café . golem.ph.utexas.edu . 30 September 2022 . en.
  3. Book: Clément . Dominique . Despeyroux . Thierry . Kahn . Gilles . Despeyroux . Joëlle . Proceedings of the 1986 ACM conference on LISP and functional programming - LFP '86 . A simple applicative language: Mini-ML . 8 August 1986 . 13–27 . 10.1145/319838.319847 . https://dl.acm.org/doi/10.1145/319838.319847 . Association for Computing Machinery. 0897912004 . 5126579 .
  4. Dunfield . Jana . Krishnaswami . Neel . Bidirectional Typing . ACM Computing Surveys . 23 May 2021 . 54 . 5 . 98:19 . 10.1145/3450952 . 201058734 . 0360-0300. free . 1908.05839 .