Conceptintro
Type system
Also called static type system.
A set of rules that assigns a type to each phrase of a program and rejects the programs whose phrases cannot be typed.
A type system is a set of rules that gives a type, such as or , to each phrase of a program, using only the program text. A program that has a phrase with no type is rejected before it runs. When the rules are sound, a program that passes cannot reach a state where an operation gets a value of the wrong kind, such as adding a number to a function. The price is that the rules must be conservative: some programs that would run without error are rejected too.
In plain words
A type says what kind of value a piece of a program can produce: a number, a truth value, or a function from numbers to numbers. A type system is a list of rules. Each rule gives a piece of the program its type from the types of its parts. If some piece gets no type, the program is rejected. The checker reads the text of the program and does not run it. So the checker cannot know which branch a test will take, and it must be careful: it rejects every program that could misuse a value, and as a price it also rejects some programs that would run fine. In return the guarantee holds for every input at once. A program that passes never calls a number as a function and never asks if a function is zero.
The idea
Types, contexts and the judgment
A type system has three parts: a grammar of types, a grammar of terms, and a set of inference rules for a typing judgment. The judgment reads "in context , the term has type ". The context is a finite map from variables to types. It records the types of the variables that are free in . A term is well typed if there is a with in the empty context, and a derivation is a finite tree of rule instances that ends in the judgment.
The smallest useful example is the language of typed arithmetic expressions in Chapter 8 of Pierce's book. It has no variables, so the context is empty and is left out.
The values are , and the numeric values . The typing rules have the premises above the line and the conclusion below it.
e1 : Bool e2 : T e3 : T
----------- T-True (also ----------------------------- T-If
true : Bool T-False) if e1 then e2 else e3 : T
e : Nat e : Nat e : Nat
-------- T-Zero ---------- T-Succ ---------- T-Pred -------------- T-IsZero
0 : Nat succ e : Nat pred e : Nat iszero e : Bool
The small-step semantics has the computation rules E-IfTrue (), E-IfFalse, E-PredZero (), E-PredSucc (), E-IszeroZero () and E-IszeroSucc (), and one congruence rule per form (E-If, E-Succ, E-Pred, E-IsZero) that steps the first subterm that is not yet a value.
A term is stuck if it is not a value and no rule applies to it. The terms and are stuck. A stuck term is the formal picture of a run-time type error: the machine has an operation and an operand of the wrong kind.
What the rules buy: safety
The rules are sound if a well-typed term never reaches a stuck term. Wright and Felleisen (1994) split this into two lemmas that are each proved by induction on a typing derivation:
- Progress. If , then is a value or there is an with .
- Preservation. If and , then .
By induction on the number of steps, every term that a well-typed term reaches is well typed, so it is not stuck. The full statement and proof for the simply typed lambda calculus are on the type soundness page. Soundness is always relative to one semantics and one definition of "stuck". A division by zero, an array index out of range or a null dereference is usually not a stuck state: the semantics defines it to raise an exception, and the type system says nothing about it.
Why the rules must be conservative
No static type system for a Turing-complete language can accept exactly the programs that never get stuck. Suppose a checker did. For any program , build the term . Under call-by-value, this term gets stuck if and only if halts. So would decide the halting problem. This is a case of Rice's theorem. A real checker therefore errs on one side. A sound checker rejects some safe programs; an unsound checker accepts some programs that go wrong. Designers choose soundness for the core of the language and add run-time checks for the rest.
Two questions about the same rules
The rules define a relation. Two algorithmic questions come from it, and they have very different difficulty.
- Type checking: given , and , decide whether .
- Type inference: given , find a and a that make the judgment hold, or report that none exist.
When the programmer writes the type of every bound variable (Church style, or explicit typing), checking is easy for the simple systems. When the types are left out (Curry style, or implicit typing), the checker must infer them. For the simply typed lambda calculus inference is first-order unification. For System F it is undecidable (Wells' theorem).
Static and dynamic checking
A statically typed language checks the rules before the program runs. A dynamically typed language keeps a tag with each value and checks the tag at each operation. Both are safe in Cardelli's sense: every error is trapped, which means the run stops with a defined error instead of reading memory as the wrong kind of data. C is typed but not safe, because a cast or an out-of-range write gives an untrapped error. The static check gives a guarantee for all inputs before the first run. The dynamic check rejects only runs that really fail. Gradual typing mixes the two in one language.
Other axes
- Structural type equality compares the shape of two types. Nominal equality compares their declared names. Java classes are nominal; OCaml object types are structural.
- Subtyping lets a value of one type be used where another type is expected.
- Polymorphism lets one term have many types: parametric polymorphism (System F, Hindley-Milner), subtype polymorphism, and ad hoc polymorphism (type classes).
- Richer systems track more than the shape of values: effect systems track side effects, linear types track how often a value is used, and dependent types let types mention values.
Examples
A typed term. The term has type . The derivation uses T-Zero, then T-Succ for , then T-IsZero for the test, which has type . Both branches have type , so T-If gives . The term takes two steps: E-IsZero with E-IszeroSucc gives , and E-IfFalse gives the value , which has type , as preservation says.
A stuck term is rejected. The term is stuck: it is not a value, and no rule steps it. T-Succ needs , and the only rule for gives , so the term has no type.
A safe term is rejected. The term steps in one step to the value , so it never gets stuck. But T-If needs both branches to have the same type, and . The checker does not know which branch runs, so it rejects the term. This is the price of a static check.
A safe function with no simple type. In the simply typed lambda calculus, the term has no type. If , then T-App needs , and no finite type satisfies this equation. Unification fails on it with the occurs check. A system with recursive types gives it the type .
A dynamic check. In Python, 1 + "a" raises a TypeError at run time. The same expression in OCaml, 1 + "a", is rejected by the compiler before the program runs. Both errors are trapped. Only the time of the report differs.
Where it is used
- Compilers. Every statically typed language (ML, Haskell, Java, C#, Rust, Go, TypeScript) runs a type checker after parsing, on the abstract syntax tree. The types then guide code generation: they fix the size and layout of values, select the right machine operation for
+on integers or floats, and drive monomorphization and dictionary passing. - Optimization. A sound type system lets the compiler drop run-time tag checks and unbox values, because the types prove the checks always succeed.
- Security. The Java bytecode verifier is a type checker for the bytecode. It lets a virtual machine run code it does not trust without a check at each instruction.
- Proofs. By the Curry-Howard correspondence, a type checker for a rich enough system is a proof checker. Coq, Agda and Lean work on this basis.
- Static analysis. Many program analyses are phrased as type systems (effect systems, borrow checking in Rust, refinement types). Others are phrased as abstract interpretation. The two views often describe the same analysis.
References
- Benjamin C. Pierce, Types and Programming Languages, MIT Press, 2002, Chapter 1, Section 1.1 (the definition of a type system as a tractable syntactic method), and Chapter 8 (typed arithmetic expressions, the rules T-True to T-IsZero, Theorems 8.3.2 and 8.3.3 on progress and preservation)
- Luca Cardelli, Type Systems, in Allen B. Tucker (ed.), The Computer Science and Engineering Handbook, CRC Press, 1997, Chapter 103, pages 2208 to 2236 (trapped and untrapped errors, safe and typed languages, static and dynamic checking)
- Robin Milner, A theory of type polymorphism in programming, Journal of Computer and System Sciences 17(3), 1978, pages 348 to 375, Section 3 (the semantic soundness theorem, stated as "well-typed expressions do not go wrong")
- Andrew K. Wright and Matthias Felleisen, A syntactic approach to type soundness, Information and Computation 115(1), 1994, pages 38 to 94, the syntactic method, soundness as subject reduction plus progress for a small-step semantics
Where this leads
About this page
The build computes these levels from the evidence in the repository. Each row says what the next level needs. Read what the levels mean.
| Ladder | Level | What the next level needs |
|---|---|---|
| Human proof | H0 no proof |
|
| Lean | L0 no Lean part |
|
| Visual | V0 none |
|
| Writing | W4 complete |
|
Tag 4GFJEY Edit this page History Report an error Markdown