2Mon𝒲𝒾𝓀𝒾

System F

System F is a type system historically developed independently by Jean-Yves Girard and John Reynolds. It enriches simply typed lambda calculus with an impredicative universal quantifier over types.

Logically, it corresponds to second order minimal logic, that is, a second order logic without first-order quantifiers. The idea of having second order quantifications, but not first order ones, stems from the observation that first order quantiication doesn’t have a clear computational interpretation.

Definition

Let us define inductively types as follows

𝑇≔𝛼|𝑇→𝑇|βˆ€π›Ό.𝑇

then terms

π‘‘β‰”πœ†π‘₯.𝑑|𝑑𝑑|Λ𝛼.𝑑|𝑑𝑇|π‘₯

We define straightforwardly the typing judgement

π‘₯:π΄βˆˆΞ“
Ξ“βŠ’π‘₯:𝐴
Ax
Ξ“,π‘₯:π΄βŠ’π‘‘:𝐡
Ξ“βŠ’πœ†π‘₯.𝑑:𝐴→𝐡
I→
Ξ“βŠ’π‘’:π΄β†’π΅Ξ“βŠ’π‘£:𝐴
Ξ“βŠ’π‘’π‘£:𝐡
E→
Ξ“βŠ’π‘‘:π΄π›Όβˆ‰fv(Ξ“)
Ξ“βŠ’Ξ›π›Ό.𝑑:βˆ€π›Ό,𝐴
Iβˆ€
Ξ“βŠ’π‘‘:βˆ€π›Ό,𝐴
Ξ“βŠ’π‘‘π‘‡:𝐴[𝛼↦𝑇]
Eβˆ€

Encodings

Impredicative encodings are extremely powerful, as they are enough to define a large class of inductive types with their plain recursor.

The trick is to encode a type as the type of (polymorphic) recursors. Thus, encodings are similar to Church encodings in plain πœ†-calculus.

It should be noted that, although these encodings are logically enough to be as expressive as inductive types, they are computationally bad encodings, in the sense that they are very wasteful. Natural numbers, for instance, end up being represented in a unary form that additionally has extra overhead than plain unary numbers.

Empty type

The empty type is encoded as

πŸŽβ‰”βˆ€π›Ό,𝛼

Indeed, if I have a term 𝑑:𝟎, and any type 𝑇, one can derive a witness 𝑑𝑇:𝑇 of 𝑇, which makes 𝟎 validate the ex falso quodlibet principle.

Unit type

The unit type is encoded as

πŸβ‰”βˆ€π›Ό,𝛼→𝛼

as well as having a constructor

⋆≔Λ𝛼.πœ†π‘₯.π‘₯:𝟏

We will prove that every closed value 𝑣:𝟏 has the canonicity property:

𝑣≑⋆

This will stem from a more general adequacy result.

Natural numbers

Natural numbers are encoded as

β„•β‰”βˆ€π›Ό,(𝛼→𝛼)→𝛼→𝛼

with

0≔Λ𝛼.πœ†π‘ .πœ†π‘§.𝑧𝑛+1≔Λ𝛼.πœ†π‘ .πœ†π‘§.𝑛(𝛼,𝑠𝑧)

Product type

Let π‘ˆ, 𝑉 be two types. We can define

π‘ˆΓ—π‘‰β‰”βˆ€π›Ό,(π‘ˆβ†’π‘‰β†’π›Ό)→𝛼

and, for 𝑒:π‘ˆ and 𝑣:𝑉, we have

(𝑒,𝑣)≔Λ𝛼.πœ†π‘“.𝑓(𝑒,𝑣)

Sum type

Let π‘ˆ,𝑉 be two types. We can define

π‘ˆ+π‘‰β‰”βˆ€π›Ό,(π‘ˆβ†’π›Ό)β†’(𝑉→𝛼)→𝛼

and

πœ„1β‰”πœ†π‘’.Λ𝛼.πœ†π‘“.πœ†π‘”.π‘“π‘’πœ„2β‰”πœ†π‘£.Λ𝛼.πœ†π‘“.πœ†π‘”.𝑔𝑣

Existential type

Let π‘ˆ be a type, we can define

βˆƒπ›Ό,π‘ˆβ‰”βˆ€π›½,(βˆ€π›Ό,π‘ˆβ†’π›½)→𝛽

Meta-theoretic properties

Meta-properties of System F are mainly established through two ways.

Firstly, by reduction to πœ†-calculus: System F introduces type binders and application, but that’s an extremely tame calculus because it

  • doesn’t interfere with itself: reducing a redex (Λ𝛼.𝑑)𝑇 cannot create an other similar redex
  • doesn’t interfere with the standard 𝛽 reduction: reducing a redex as above cannot create a 𝛽 redex

Hence one can essentially erase all the second order part from a System F term to retrieve important properties of the πœ†-calculus, such as confluence.

Secondly, by crafting adequate logical relations, as with STLC. Note, however, that historically this second part has been considered a hard problem, because System F’s impredicativity breaks the naive way to make logical relations. Indeed, System F’s strong normalisation (which follows directly from being able to define logical relations) need the meta theory to itself have impredicativity, and it cannot be proved, for instance, in MLTT.

Logical relations

For this section, let 𝑛:β„• be a natural number, which is a parameter of our logical relation construction. 𝑛 will define the arity of the logical relation. Common values are 𝑛=1,2.

We will define, by induction on 𝑇, an 𝑛-ary relation VβŸ¦π‘‡βŸ§πœŒπ‘›, where fv(𝑇)βŠ†dom𝜌, and 𝜌 is a family of 𝑛-ary relations on values of System F, used to interpret free variables of 𝑇.

VβŸ¦π›ΌβŸ§πœŒπ‘›β‰”πœŒ(𝛼)VβŸ¦π΄β†’π΅βŸ§πœŒπ‘›β‰”{(𝑓1,…,𝑓𝑛)∈Value𝑛|βˆ€(𝑒1,…,𝑒𝑛)∈VβŸ¦π΄βŸ§πœŒπ‘›,(𝑓1𝑒1,…,𝑓𝑛𝑒𝑛)∈EβŸ¦π΅βŸ§πœŒπ‘›}VβŸ¦βˆ€π›Ό,π‘‡βŸ§πœŒπ‘›β‰”{(𝑒1,…,𝑒𝑛)∈Value𝑛|βˆ€π‘…βŠ†Value𝑛,(𝑒1,…,𝑒𝑛)∈VβŸ¦π‘‡βŸ§πœŒ[𝛼↦𝑅]𝑛}

where

EβŸ¦π‘‡βŸ§πœŒπ‘›β‰”{(𝑑1,…,𝑑𝑛)∈Term𝑛|βˆƒ(𝑣1,…,𝑣𝑛)∈VβŸ¦π‘‡βŸ§πœŒπ‘›,βˆ€π‘–βˆˆ{1,…,𝑛},π‘‘π‘–βŸΆπ›½βˆ—π‘£π‘–}

Note that, crucially, the interpretation of a universal type requires an impredicative universal quantification in the meta-theory.

Theorem Fundamental theorem of logical relations Let 𝑇 be a type with free variables in Ξ“, and 𝑒:Term such that βŠ’π‘’:𝑇. For any environment 𝜌, it holds that

(𝑒,…,𝑒)∈EβŸ¦π‘‡βŸ§πœŒπ‘›
Proof.

By induction on 𝑇:

  • βŠ’π‘’:𝛼.

Strong normalisation

Decidability problems

System F is a simple extension of previewSTLC, that is expressive enough to be used as a programming language: for instance, the core type system of OCaml is a subset of System F.

Yet, System F is not used for programming languages in practice, because of a fundamental limitation:

Theorem In System F, type checking and type inference are undecidable.

Type checking

The precise statement of the theorem is: for 𝑑 a term, and 𝑇 a type, it is undecidable to know whether

βŠ’π‘‘:𝑇

Type inference

For 𝑑 a term, it is undecidable whether there exists a type 𝑇 such that

βŠ’π‘‘:𝑇

Mitigation

Undecidability of System F can be mitigated using an intrinsic presentation of its calculus, rather than an extrinsic one.

However, even intrinsic System F is rarely used in practice, because programming languages that abandon decidable type checking (and thus, in the general case, need user annotations) usually go for even more expressive type theories, for instance with higher kinded types (see System Fπœ”), or even with full type dependency (see the Calculus of Constructions).