System F Explained

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

Whereas simply typed lambda calculus has variables ranging over terms, 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 AA would be formalized in System F as the judgement

\vdashΛ\alpha.λx\alpha.x:\forall\alpha.\alpha\to\alpha

where

\alpha

is a type variable. The upper-case

Λ

is traditionally used to denote type-level functions, as opposed to the lower-case

λ

which is used for value-level functions. (The superscripted

\alpha

means that the bound x is of type

\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]

Typing rules

The typing rules of System F are those of simply typed lambda calculus with the addition of the following:

{\Gamma\vdashMn{:
\forall\alpha.\sigma}{\Gamma\vdash

M\taun{:}\sigma[\tau/\alpha]}}

(1)
{\Gamma,\alpha~type\vdashMn{:
\sigma}{\Gamma\vdashΛ\alpha.Mn{:}\forall\alpha.\sigma}}
(2)

where

\sigma,\tau

are types,

\alpha

is a type variable, and

\alpha~type

in the context indicates that

\alpha

is bound. The first rule is that of application, and the second is that of abstraction.[2] [3]

Logic and predicates

The

Boolean

type is defined as:

\forall\alpha.\alpha\to\alpha\to\alpha

, where

\alpha

is a type variable. This means:

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

\to

to be right-associative.)

The following two definitions for the boolean values

T

and

F

are used, extending the definition of Church booleans:

T=Λ\alpha{.}λx\alphaλy\alpha{.}x

F=Λ\alpha{.}λx\alphaλy\alpha{.}y

(Note that the above two functions require three - not two - arguments. 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

\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

Boolean

is a convenient shorthand for

\forall\alpha.\alpha\to\alpha\to\alpha

, but it is not a symbol of System F itself, but rather a "meta-symbol". Likewise,

T

and

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 and for the fixed-point combinator, which works around that restriction.)

Then, with these two

λ

-terms, we can define some logic operators (which are of type

BooleanBooleanBoolean

):

\begin{align} AND&=λxBooleanλyBoolean{.}xBooleanyF\\ OR&=λxBooleanλyBoolean{.}xBooleanTy\\ NOT&=λxBoolean{.}xBooleanFT\end{align}

Note that in the definitions above,

Boolean

is a type argument to

x

, specifying that the other two parameters that are given to

x

are of type

Boolean

. As in Church encodings, there is no need for an function as one can just use raw

Boolean

-typed terms as decision functions. However, if one is requested:

IFTHENELSE=Λ\alpha.λxBooleanλy\alphaλz\alpha.x\alphayz

will do.A predicate is a function which returns a

Boolean

-typed value. The most fundamental predicate is which returns

T

if and only if its argument is the Church numeral :

ISZERO=λn\forall{.}nBoolean(λxBoolean{.}F)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 are created using constructors. These are functions typed as:

K1 → K2 → ... → S

.

Recursivity is manifested when itself appears within one of the types

Ki

. If you have of these constructors, you can define the type of as:

\forall

1[\alpha/S] → ... →
\alpha.(K
1
m[\alpha/S] → ... →
\alpha)... → (K
1

\alpha)\alpha

For instance, the natural numbers can be defined as an inductive datatype with constructors

\begin{align} zero&:N\\ succ&:NN \end{align}

The System F type corresponding to this structure is

\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:

\begin{align} 0&:=Λ\alpha.λx\alpha.λf\alpha\to\alpha.x\\ 1&:=Λ\alpha.λx\alpha.λf\alpha\to\alpha.fx\\ 2&:=Λ\alpha.λx\alpha.λf\alpha\to\alpha.f(fx)\\ 3&:=Λ\alpha.λx\alpha.λf\alpha\to\alpha.f(f(fx)) \end{align}

If we reverse the order of the curried arguments (i.e.,

\forall\alpha.(\alpha\alpha)\alpha\alpha

), then the Church numeral for is a function that takes a function as argument and returns the th power of . That is to say, a Church numeral is a higher-order function – it takes a single-argument function, 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.[4] [5]

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;[6] non-HM features in OCaml's type system include GADT.[7] [8]

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). 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. Reynolds proved the Abstraction Theorem: that every term in F2 satisfies a logical relation, which can be embedded into the logical relations P2. Reynolds proved that a Girard projection followed by a Reynolds embedding form the identity, i.e., the Girard-Reynolds Isomorphism.[9]

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:

Fn

permits kinds:

\star

(the kind of types) and

JK

where

J\inFn-1

and

K\inFn

(the kind of functions from types to types, where the argument type is of a lower order)

In the limit, we can define system

F\omega

to be

F\omega=\underset{1\leqi}{cup}Fi

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 (

λ

abstraction), mappings from types to values (

Λ

abstraction), and mappings from types to types (

λ

abstraction at the level of types).

System F<:

System F<:, pronounced "F-sub", is an extension of system F with subtyping. System F<: has been of central importance to programming language theory since the 1980s because the core of functional programming languages, like those in the ML family, support both parametric polymorphism and record subtyping, which can be expressed in System F<:.[10] [11]

See also

References

Further reading

External links

Notes and References

  1. Girard. Jean-Yves. The system F of variable types, fifteen years later. Theoretical Computer Science. 45. 160. 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.. 1986.
  2. Web site: Practical Foundations for Programming Languages, Second Edition . Harper R . Robert_Harper_(computer_scientist) . 142–3.
  3. Web site: Proofs of Programs and Formalisation of Mathematics . Geuvers H, Nordström B, Dowek G . 51.
  4. Web site: Joe Wells's Research Interests . 2005-01-20 . Heriot-Watt University . J.B. . Wells .
  5. J.B. . Wells . Typability and type checking in System F are equivalent and undecidable . Ann. Pure Appl. Logic . 98 . 1–3 . 111–156 . 1999 . 10.1016/S0168-0072(98)00047-5 . free . Web site: The Church Project: Typability and type checking in ystem are equivalent and undecidable. 29 September 2007. dead. https://web.archive.org/web/20070929211126/http://www.church-project.org/reports/Wells:APAL-1999-v98-no-note.html. 29 September 2007.
  6. Web site: System FC: equality constraints and coercions. gitlab.haskell.org. 2019-07-08.
  7. Web site: OCaml 4.00.1 release notes. 2012-10-05. 2019-09-23. ocaml.org.
  8. Web site: OCaml 4.09 reference manual. 2012-09-11. 2019-09-23.
  9. [Philip Wadler]
  10. Luca . Cardelli . Martini, Simone . Mitchell, John C. . Scedrov, Andre . An extension of system F with subtyping . Information and Computation, vol. 9 . 4–56 . 1994 . North Holland, Amsterdam . 10.1006/inco.1994.1013. free .
  11. Book: Pierce, Benjamin. Types and Programming Languages. MIT Press. 2002. 978-0-262-16209-8., Chapter 26: Bounded quantification