Modelcore
System F
Also called polymorphic lambda calculus, second-order lambda calculus, Girard-Reynolds calculus.
The simply typed lambda calculus with abstraction over types, so one term works at every type. It is strongly normalizing, and Church-style checking is decidable but Curry-style typing is not.
System F extends the simply typed lambda calculus with functions over types. The term takes a type and returns the identity function at that type, and it has the type . Girard (1972) found the system as the proof terms of second-order logic, and Reynolds (1974) found it independently as a model of polymorphic programs. Every well-typed term terminates, and yet the calculus can define every function that second-order arithmetic proves total. Church-style checking is easy, but inference is undecidable.
The machine
System F is a model of programs, not a machine model. A run is a sequence of small steps on a term, as for the STLC.
Syntax
is a type abstraction and a type application. The quantifier binds ; types are equal up to renaming of bound type variables. A context holds term variables with types and type variables. Base types such as are not needed: they can be encoded, as shown below.
Typing
The rules are those of the STLC (T-Var, T-Abs, T-App) plus two more (Pierce, Chapter 23).
Gamma, a |- t : T Gamma |- t : forall a. T1
------------------------------ T-TAbs ----------------------------- T-TApp
Gamma |- /\a. t : forall a. T Gamma |- t [T2] : [a := T2] T1
In T-TAbs, the variable must be fresh for , which the context extension expresses. Without that side condition, would get the wrong type .
The quantifier is impredicative: ranges over all types, including itself. So is well typed, and the polymorphic identity is applied to itself.
Evaluation
Call-by-value small steps: E-App1, E-App2 and E-AppAbs as in the STLC, plus
t1 --> t1'
--------------------- E-TApp ----------------------------- E-TappTabs
t1 [T2] --> t1' [T2] (/\a. t) [T2] --> [a := T2] t
Type soundness holds by the same progress and preservation proof, with one new lemma: substitution of a type for a type variable keeps typing ( implies ).
Church encodings
Data types are definable from and alone (Pierce, Section 23.4; Girard, Lafont and Taylor, Chapter 11).
- Booleans: , with and .
- Natural numbers: , with .
- Pairs: . Sums: .
- Lists: , the type of
fold_right. - Existential types: . These are abstract data types.
A script with a Church-style checker for System F gives these types, and runs the erased terms on numerals:
| term | type | value on numerals |
|---|---|---|
Here , and is the Ackermann function: on all , the script matches the recursive definition , , . The second row is self-application, which has no type in the STLC. The fourth row uses a numeral at the type , which is possible only because can be instantiated at a type that contains .
What it counts
The cost measure is again the number of reduction steps and the size of terms. Type abstraction and type application cost a step each in the semantics above, but they do no work at run time. The erasure removes all types, all and all . In the pure calculus, reduces to a value exactly when does, with the same number of term-level beta steps, so compilers drop the types. With side effects, this needs care: is a value, so its body is not evaluated, but its erasure is. Pierce (Section 23.7) gives an erasure that keeps a dummy abstraction, and ML's value restriction solves the same problem.
The cost of type checking is measured in the size of the annotated term, on a word RAM.
Relation to other models
- STLC. The fragment without . System F types more terms, for example self-application above and every function in the table.
- Hindley-Milner. ML's type system is the predicative, prenex fragment: only at the top of a type, only at
let, and instantiated only with types that have no . That restriction makes inference decidable. Full System F inference is undecidable. - Rank- fragments. A type has rank if no is to the left of or more arrows, counting nesting. Rank 1 is the ML fragment. Inference is decidable at rank 2 and undecidable at every rank of 3 or more (Kfoury and Wells 1994).
- Logic. By the Curry-Howard correspondence, System F types are the formulas of second-order intuitionistic propositional logic, and the encodings above are its definitions of , and from and .
- Extensions. adds functions from types to types. adds bounded quantification. The Calculus of Constructions adds dependent types. GHC compiles Haskell to System FC, a Church-style System F with type equality coercions.
- Implementation. Compilers realize polymorphism either by passing type information at run time (dictionary passing for type classes), by a uniform boxed representation, or by monomorphization, which copies each polymorphic function at each type it is used.
Known results in the model
Strong normalization (Girard 1972). Every well-typed term is strongly normalizing. The Tait method of the reducibility method page does not work directly: would be defined from for all , including itself, which is circular. Girard's fix is reducibility candidates: sets of terms that satisfy CR1, CR2 and CR3. Interpret a type relative to an assignment of candidates to its free type variables, and let denote the intersection of the interpretations of over all candidates for . This intersection is again a candidate. Then every typed term is in the interpretation of its type, and CR1 gives strong normalization (Girard, Lafont and Taylor, Chapter 14). The proof quantifies over all sets of terms, which is the impredicativity of System F reflected in the proof.
Representable functions. The functions definable by closed terms of type are exactly the functions that second-order Peano arithmetic proves total (Girard 1972; Girard, Lafont and Taylor, Chapter 15). This includes the Ackermann function and much more. It excludes the evaluator for System F itself: by a diagonal argument, no total language can define its own interpreter.
Consistency strength. Strong normalization of System F implies the consistency of second-order arithmetic. So by Gödel's second incompleteness theorem, it cannot be proved in second-order arithmetic.
Parametricity (Reynolds 1983). A polymorphic function must act the same way at every type, because it cannot inspect its type argument. Reynolds' abstraction theorem makes this precise with relations: every closed term of type preserves every relation built from the structure of . Consequences, called free theorems by Wadler (1989): every closed term of type behaves as the identity; every closed term of type returns its first or its second argument; and a function of type commutes with map f for every .
No set-theoretic model (Reynolds 1984). There is no model of System F that interprets types as sets and as the full set of functions. Models exist with other notions of function, for example PER models and Girard's coherence spaces.
Type checking and inference. For Church-style terms, type checking is decidable by one bottom-up pass, with types compared up to renaming of bound variables. For Curry-style terms, where types, and are erased, typability and type checking are both undecidable (Wells 1999), by a reduction from semi-unification. See Wells' theorem.
Inhabitation. Under Curry-Howard, inhabitation of System F types is provability in second-order intuitionistic propositional logic, which is undecidable (Löb 1976). For the STLC the same problem is PSPACE-complete.
References
- Jean-Yves Girard, Interpretation fonctionnelle et elimination des coupures de l'arithmetique d'ordre superieur, these de doctorat d'Etat, Universite Paris VII, 1972, the system F, its strong normalization by reducibility candidates, and the representation of the functions provably total in second-order arithmetic
- John C. Reynolds, Towards a theory of type structure, in B. Robinet (ed.), Programming Symposium, Paris 1974, LNCS 19, Springer, 1974, the polymorphic lambda calculus with type abstraction and type application, found independently of Girard
- Benjamin C. Pierce, Types and Programming Languages, MIT Press, 2002, Chapter 23 (the rules T-TAbs, T-TApp, E-TApp, E-TappTabs; Section 23.4 Church encodings; Section 23.5 basic properties; Section 23.6 erasure, typability and type reconstruction; Section 23.7 erasure and evaluation order)
- Jean-Yves Girard, Yves Lafont and Paul Taylor, Proofs and Types, Cambridge University Press, 1989, Chapter 11 (System F and its representation of data types), Chapter 14 (strong normalization by reducibility candidates), Chapter 15 (the representation theorem)
- John C. Reynolds, Types, abstraction and parametric polymorphism, Information Processing 83 (IFIP Congress), North-Holland, 1983, the abstraction theorem (relational parametricity)
- John C. Reynolds, Polymorphism is not set-theoretic, in Semantics of Data Types, LNCS 173, Springer, 1984, the main theorem (no set-theoretic model interprets the polymorphic types)
- Joe B. Wells, Typability and type checking in System F are equivalent and undecidable, Annals of Pure and Applied Logic 98(1-3), 1999, pages 111 to 156, Theorem 4.1 and Section 6 (Curry-style type checking and typability are undecidable)
- Philip Wadler, Theorems for free!, Functional Programming Languages and Computer Architecture (FPCA), 1989, the free theorems derived from parametricity for polymorphic types
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 4JA4HE Edit this page History Report an error Markdown