# System F

System F, also known as the (Girard–Reynolds) polymorphic lambda calculus or the second-order lambda calculus, is a typed lambda calculus that differs from the simply typed lambda calculus by the introduction of a mechanism of universal quantification over types. System F thus formalizes the notion of parametric polymorphism in programming languages, and forms a theoretical basis for languages such as Haskell and ML. System F was discovered independently by logician Jean-Yves Girard (1972) and computer scientist John C. Reynolds (1974).

Whereas simply typed lambda calculus has variables ranging over functions, and binders for them, System F additionally has variables ranging over types, and binders for them. As an example, the fact that the identity function can have any type of the form A→ A would be formalized in System F as the judgment

${\displaystyle \vdash \Lambda \alpha .\lambda x^{\alpha }.x:\forall \alpha .\alpha \to \alpha }$

where ${\displaystyle \alpha }$ is a type variable. The upper-case ${\displaystyle \Lambda }$ is traditionally used to denote type-level functions, as opposed to the lower-case ${\displaystyle \lambda }$ which is used for value-level functions. (The superscripted ${\displaystyle \alpha }$ means that the bound x is of type ${\displaystyle \alpha }$; the expression after the colon is the type of the lambda expression preceding it.)

As a term rewriting system, System F is strongly normalizing. However, type inference in System F (without explicit type annotations) is undecidable. Under the Curry–Howard isomorphism, System F corresponds to the fragment of second-order intuitionistic logic that uses only universal quantification. System F can be seen as part of the lambda cube, together with even more expressive typed lambda calculi, including those with dependent types.

According to Girard, the "F" in System F was picked by chance.[1]

## Logic and predicates

The ${\displaystyle \scriptstyle {\mathsf {Boolean}}}$ type is defined as: ${\displaystyle \scriptstyle \forall \alpha .\alpha \to \alpha \to \alpha }$, where ${\displaystyle \scriptstyle \alpha }$ is a type variable. This means: ${\displaystyle \scriptstyle {\mathsf {Boolean}}}$ is the type of all functions which take as input a type α and two expressions of type α, and produce as output an expression of type α (note that we consider ${\displaystyle \to }$ to be right-associative.)

The following two definitions for the boolean values ${\displaystyle \scriptstyle \mathbf {T} }$ and ${\displaystyle \scriptstyle \mathbf {F} }$ are used, extending the definition of Church booleans:

${\displaystyle \mathbf {T} =\Lambda \alpha {.}\lambda x^{\alpha }\lambda y^{\alpha }{.}x}$
${\displaystyle \mathbf {F} =\Lambda \alpha {.}\lambda x^{\alpha }\lambda y^{\alpha }{.}y}$

(Note that the above two functions require three not two parameters. The latter two should be lambda expressions, but the first one should be a type. This fact is reflected in the fact that the type of these expressions is ${\displaystyle \scriptstyle \forall \alpha .\alpha \to \alpha \to \alpha }$; the universal quantifier binding the α corresponds to the Λ binding the alpha in the lambda expression itself. Also, note that ${\displaystyle \scriptstyle {\mathsf {Boolean}}}$ is a convenient shorthand for ${\displaystyle \scriptstyle \forall \alpha .\alpha \to \alpha \to \alpha }$, but it is not a symbol of System F itself, but rather a "meta-symbol". Likewise, ${\displaystyle \scriptstyle \mathbf {T} }$ and ${\displaystyle \scriptstyle \mathbf {F} }$ are also "meta-symbols", convenient shorthands, of System F "assemblies" (in the Bourbaki sense); otherwise, if such functions could be named (within System F), then there would be no need for the lambda-expressive apparatus capable of defining functions anonymously.)

Then, with these two ${\displaystyle \scriptstyle \lambda }$-terms, we can define some logic operators (which are of type ${\displaystyle \scriptstyle {\mathsf {Boolean}}\rightarrow {\mathsf {Boolean}}\rightarrow {\mathsf {Boolean}}}$):

{\displaystyle {\begin{aligned}\mathrm {AND} &=\lambda x^{\mathsf {Boolean}}\lambda y^{\mathsf {Boolean}}{.}xy\,\mathbf {F} \\\mathrm {OR} &=\lambda x^{\mathsf {Boolean}}\lambda y^{\mathsf {Boolean}}{.}x\mathbf {T} \,y\\\mathrm {NOT} &=\lambda x^{\mathsf {Boolean}}{.}x\mathbf {F} \,\mathbf {T} \end{aligned}}}

As in Church encodings, there is no need for an IFTHENELSE function as one can just use raw ${\displaystyle \scriptstyle {\mathsf {Boolean}}}$-typed terms as decision functions. However, if one is requested:

${\displaystyle \mathrm {IFTHENELSE} =\Lambda \alpha .\lambda x^{\mathsf {Boolean}}\lambda y^{\alpha }\lambda z^{\alpha }.x\alpha yz}$

will do. A predicate is a function which returns a ${\displaystyle \scriptstyle {\mathsf {Boolean}}}$-typed value. The most fundamental predicate is ISZERO which returns ${\displaystyle \scriptstyle \mathbf {T} }$ if and only if its argument is the Church numeral 0:

${\displaystyle \mathrm {ISZERO} =\lambda n^{\forall \alpha .(\alpha \rightarrow \alpha )\rightarrow \alpha \rightarrow \alpha }{.}n^{\mathsf {Boolean}}(\lambda x^{\mathsf {Boolean}}{.}\mathbf {F} )\,\mathbf {T} }$

## System F structures

System F allows recursive constructions to be embedded in a natural manner, related to that in Martin-Löf's type theory. Abstract structures (S) are created using constructors. These are functions typed as:

${\displaystyle K_{1}\rightarrow K_{2}\rightarrow \dots \rightarrow S}$.

Recursivity is manifested when ${\displaystyle S}$ itself appears within one of the types ${\displaystyle K_{i}}$. If you have ${\displaystyle m}$ of these constructors, you can define the type of ${\displaystyle S}$ as:

${\displaystyle \forall \alpha .(K_{1}^{1}[\alpha /S]\rightarrow \dots \rightarrow \alpha )\dots \rightarrow (K_{1}^{m}[\alpha /S]\rightarrow \dots \rightarrow \alpha )\rightarrow \alpha }$

For instance, the natural numbers can be defined as an inductive datatype ${\displaystyle N}$ with constructors

${\displaystyle {\mathit {zero}}:\mathrm {N} }$
${\displaystyle {\mathit {succ}}:\mathrm {N} \rightarrow \mathrm {N} }$

The System F type corresponding to this structure is ${\displaystyle \forall \alpha .\alpha \to (\alpha \to \alpha )\to \alpha }$. The terms of this type comprise a typed version of the Church numerals, the first few of which are:

0 := ${\displaystyle \Lambda \alpha .\lambda x^{\alpha }.\lambda f^{\alpha \to \alpha }.x}$
1 := ${\displaystyle \Lambda \alpha .\lambda x^{\alpha }.\lambda f^{\alpha \to \alpha }.fx}$
2 := ${\displaystyle \Lambda \alpha .\lambda x^{\alpha }.\lambda f^{\alpha \to \alpha }.f(fx)}$
3 := ${\displaystyle \Lambda \alpha .\lambda x^{\alpha }.\lambda f^{\alpha \to \alpha }.f(f(fx))}$

If we reverse the order of the curried arguments (i.e., ${\displaystyle \forall \alpha .(\alpha \rightarrow \alpha )\rightarrow \alpha \rightarrow \alpha }$), then the Church numeral for ${\displaystyle n}$ is a function that takes a function f as argument and returns the ${\displaystyle n}$th power of f. That is to say, a Church numeral is a higher-order function – it takes a single-argument function f, and returns another single-argument function.

## Use in programming languages

The version of System F used in this article is as an explicitly typed, or Church-style, calculus. The typing information contained in λ-terms makes type-checking straightforward. Joe Wells (1994) settled an "embarrassing open problem" by proving that type checking is undecidable for a Curry-style variant of System F, that is, one that lacks explicit typing annotations.[2][3]

Wells's result implies that type inference for System F is impossible. A restriction of System F known as "Hindley–Milner", or simply "HM", does have an easy type inference algorithm and is used for many statically typed functional programming languages such as Haskell 98 and the ML family. Over time, as the restrictions of HM-style type systems have become apparent, languages have steadily moved to more expressive logics for their type systems. GHC a Haskell compiler, goes beyond HM (as of 2008) and uses System F extended with non-syntactic type equality;[4] non-HM features in OCaml's type system include GADT.[5][6]

## The Girard-Reynolds Isomorphism

In second-order intuitionistic logic, the second-order polymorphic lambda calculus (F2) was discovered by Girard (1972) and independently by Reynolds (1974).[7] Girard proved the Representation Theorem: that in second-order intuitionistic predicate logic (P2), functions from the natural numbers to the natural numbers that can be proved total, form a projection from P2 into F2.[7] Reynolds proved the Abstraction Theorem: that every term in F2 satisfies a logical relation, which can be embedded into the logical relations P2.[7] Reynolds proved that a Girard projection followed by a Reynolds embedding form the identity, i.e., the Girard-Reynolds Isomorphism.[7]

## System Fω

While System F corresponds to the first axis of Barendregt's lambda cube, System Fω or the higher-order polymorphic lambda calculus combines the first axis (polymorphism) with the second axis (type operators); it is a different, more complex system.

System Fω can be defined inductively on a family of systems, where induction is based on the kinds permitted in each system:

• ${\displaystyle F_{n}}$ permits kinds:
• ${\displaystyle \star }$ (the kind of types) and
• ${\displaystyle J\Rightarrow K}$ where ${\displaystyle J\in F_{n-1}}$ and ${\displaystyle K\in F_{n}}$ (the kind of functions from types to types, where the argument type is of a lower order)

In the limit, we can define system ${\displaystyle F_{\omega }}$ to be

• ${\displaystyle F_{\omega }={\underset {1\leq i}{\bigcup }}F_{i}}$

That is, Fω is the system which allows functions from types to types where the argument (and result) may be of any order.

Note that although Fω places no restrictions on the order of the arguments in these mappings, it does restrict the universe of the arguments for these mappings: they must be types rather than values. System Fω does not permit mappings from values to types (Dependent types), though it does permit mappings from values to values (${\displaystyle \lambda }$ abstraction), mappings from types to values (${\displaystyle \Lambda }$ abstraction, sometimes written ${\displaystyle \forall }$) and mappings from types to types (${\displaystyle \lambda }$ abstraction at the level of types)

## Notes

1. Girard, Jean-Yves (1986). "The system F of variable types, fifteen years later". Theoretical Computer Science. 45: 160. doi:10.1016/0304-3975(86)90044-7. However, in [3] it was shown that the obvious rules of conversion for this system, called F by chance, were converging.
2. http://www.macs.hw.ac.uk/~jbw/research-summary.html
3. "The Church Project: Typability and type checking in {S}ystem {F} are equivalent and undecidable". 29 September 2007. Archived from the original on 29 September 2007.
4. "System FC: equality constraints and coercions". gitlab.haskell.org. Retrieved 2019-07-08.
5. "OCaml 4.00.1 release notes". ocaml.org. 2012-10-05. Retrieved 2019-09-23.
6. "OCaml 4.09 reference manual". 2012-09-11. Retrieved 2019-09-23.

## References

• Girard, Jean-Yves (1971). "Une Extension de l'Interpretation de Gödel à l'Analyse, et son Application à l'Élimination des Coupures dans l'Analyse et la Théorie des Types". Proceedings of the Second Scandinavian Logic Symposium. Amsterdam. pp. 63–92. doi:10.1016/S0049-237X(08)70843-7.
• Girard, Jean-Yves (1972), Interprétation fonctionnelle et élimination des coupures de l'arithmétique d'ordre supérieur (Ph.D. thesis) (in French), Université Paris 7.
• Reynolds, John (1974). Towards a Theory of Type Structure.
• Girard, Lafont and Taylor, Proofs and Types. Cambridge University Press, 1989, ISBN 0-521-37181-3.
• J. B. Wells. "Typability and type checking in the second-order lambda-calculus are equivalent and undecidable." In Proceedings of the 9th Annual IEEE Symposium on Logic in Computer Science (LICS), pages 176–185, 1994.
• Pierce, Benjamin (2002). Types and Programming Languages. MIT Press. ISBN 0-262-16209-1., Chapter 23: Universal Types, and Chapter 25: An ML Implementation of System F