# Simply typed lambda calculus

The simply typed lambda calculus (${\displaystyle \lambda ^{\to }}$), a form of type theory, is a typed interpretation of the lambda calculus with only one type constructor: ${\displaystyle \to }$ that builds function types. It is the canonical and simplest example of a typed lambda calculus. The simply typed lambda calculus was originally introduced by Alonzo Church in 1940 as an attempt to avoid paradoxical uses of the untyped lambda calculus, and it exhibits many desirable and interesting properties.

The term simple type is also used to refer to extensions of the simply typed lambda calculus such as products, coproducts or natural numbers (System T) or even full recursion (like PCF). In contrast, systems which introduce polymorphic types (like System F) or dependent types (like the Logical Framework) are not considered simply typed. The former are still considered simple because the Church encodings of such structures can be done using only ${\displaystyle \to }$ and suitable type variables, while polymorphism and dependency cannot.

## Syntax

In this article, we use ${\displaystyle \sigma }$ and ${\displaystyle \tau }$ to range over types. Informally, the function type ${\displaystyle \sigma \to \tau }$ refers to the type of functions that, given an input of type ${\displaystyle \sigma }$, produce an output of type ${\displaystyle \tau }$. By convention, ${\displaystyle \to }$ associates to the right: we read ${\displaystyle \sigma \to \tau \to \rho }$ as ${\displaystyle \sigma \to (\tau \to \rho )}$.

To define the types, we begin by fixing a set of base types, ${\displaystyle B}$. These are sometimes called atomic types or type constants. With this fixed, the syntax of types is:

${\displaystyle \tau ::=\tau \to \tau \mid T\quad {\mathrm {where} }\quad T\in B}$.

We also fix a set of term constants for the base types. For example, we might assume a base type nat, and the term constants could be the natural numbers. In the original presentation, Church used only two base types: ${\displaystyle o}$ for "the type of propositions" and ${\displaystyle \iota }$ for "the type of individuals". The type ${\displaystyle o}$ has no term constants, whereas ${\displaystyle \iota }$ has one term constant. Frequently the calculus with only one base type, usually ${\displaystyle o}$, is considered.

The syntax of the simply typed lambda calculus is essentially that of the lambda calculus itself. We write ${\displaystyle x{\mathbin {:}}\tau }$ to denote that the variable ${\displaystyle x}$ is of type ${\displaystyle \tau }$. The term syntax is then:

${\displaystyle e::=x\mid \lambda x{\mathbin {:}}\tau .e\mid e\,e\mid c}$

where ${\displaystyle c}$ is a term constant.

That is, variable reference, abstractions, application, and constant. A variable reference ${\displaystyle x}$ is bound if it is inside of an abstraction binding ${\displaystyle x}$. A term is closed if there are no unbound variables.

Compare this to the syntax of untyped lambda calculus:

${\displaystyle e::=x\mid \lambda x.e\mid e\,e}$

We see that in typed lambda calculus every function (abstraction) must specify the type of its argument.

## Typing rules

To define the set of well typed lambda terms of a given type, we will define a typing relation between terms and types. First, we introduce typing contexts or typing environments ${\displaystyle \Gamma ,\Delta ,\dots }$, which are sets of typing assumptions. A typing assumption has the form ${\displaystyle x{\mathbin {:}}\sigma }$, meaning ${\displaystyle x}$ has type ${\displaystyle \sigma }$.

The typing relation ${\displaystyle \Gamma \vdash e{\mathbin {:}}\sigma }$ indicates that ${\displaystyle e}$ is a term of type ${\displaystyle \sigma }$ in context ${\displaystyle \Gamma }$. In this case ${\displaystyle e}$ is said to be well-typed (having type ${\displaystyle \sigma }$)". Instances of the typing relation are called typing judgements. The validity of a typing judgement is shown by providing a typing derivation, constructed using typing rules (wherein the premises above the line allow us to derive the conclusion below the line). Simply-typed lambda calculus uses these rules:

In other words,

1. If ${\displaystyle x}$ has type ${\displaystyle \sigma }$ in the context, we know that ${\displaystyle x}$ has type ${\displaystyle \sigma }$.
2. Term constants have the appropriate base types.
3. If, in a certain context with ${\displaystyle x}$ having type ${\displaystyle \sigma }$, ${\displaystyle e}$ has type ${\displaystyle \tau }$, then, in the same context without ${\displaystyle x}$, ${\displaystyle \lambda x{\mathbin {:}}\sigma .~e}$ has type ${\displaystyle \sigma \to \tau }$.
4. If, in a certain context, ${\displaystyle e_{1}}$ has type ${\displaystyle \sigma \to \tau }$, and ${\displaystyle e_{2}}$ has type ${\displaystyle \sigma }$, then ${\displaystyle e_{1}~e_{2}}$ has type ${\displaystyle \tau }$.

Examples of closed terms, i.e. terms typable in the empty context, are:

These are the typed lambda calculus representations of the basic combinators of combinatory logic.

Each type ${\displaystyle \tau }$ is assigned an order, a number ${\displaystyle o(\tau )}$. For base types, ${\displaystyle o(T)=0}$; for function types, ${\displaystyle o(\sigma \to \tau )={\mbox{max}}(o(\sigma )+1,o(\tau ))}$. That is, the order of a type measures the depth of the most left-nested arrow. Hence:

${\displaystyle o(\iota \to \iota \to \iota )=1}$
${\displaystyle o((\iota \to \iota )\to \iota )=2}$

## Semantics

### Intrinsic vs. extrinsic interpretations

Broadly speaking, there are two different ways of assigning meaning to the simply typed lambda calculus, as to typed languages more generally, sometimes called intrinsic vs. extrinsic, or Church-style vs. Curry-style.[1] An intrinsic/Church-style semantics only assigns meaning to well-typed terms, or more precisely, assigns meaning directly to typing derivations. This has the effect that terms differing only by type annotations can nonetheless be assigned different meanings. For example, the identity term ${\displaystyle \lambda x{\mathbin {:}}{\mathtt {int}}.~x}$ on integers and the identity term ${\displaystyle \lambda x{\mathbin {:}}{\mathtt {bool}}.~x}$ on booleans may mean different things. (The classic intended interpretations are the identity function on integers and the identity function on boolean values.) In contrast, an extrinsic/Curry-style semantics assigns meaning to terms regardless of typing, as they would be interpreted in an untyped language. In this view, ${\displaystyle \lambda x{\mathbin {:}}{\mathtt {int}}.~x}$ and ${\displaystyle \lambda x{\mathbin {:}}{\mathtt {bool}}.~x}$ mean the same thing (i.e., the same thing as ${\displaystyle \lambda x.~x}$).

The distinction between intrinsic and extrinsic semantics is sometimes associated with the presence or absence of annotations on lambda abstractions, but strictly speaking this usage is imprecise. It is possible to define a Curry-style semantics on annotated terms simply by ignoring the types (i.e., through type erasure), as it is possible to give a Church-style semantics on unannotated terms when the types can be deduced from context (i.e., through type inference). The essential difference between intrinsic and extrinsic approaches is just whether the typing rules are viewed as defining the language, or as a formalism for verifying properties of a more primitive underlying language. Most of the different semantic interpretations discussed below can be seen through either Church or Curry goggles.

### Equational theory

The simply typed lambda calculus has the same equational theory of βη-equivalence as untyped lambda calculus, but subject to type restrictions. The equation for beta reduction

${\displaystyle (\lambda x{\mathbin {:}}\sigma .~t)\,u=_{\beta }t[x:=u]}$
${\displaystyle \lambda x{\mathbin {:}}\sigma .~t\,x=_{\eta }t}$

### Operational semantics

Likewise, the operational semantics of simply typed lambda calculus can be fixed as for the untyped lambda calculus, using call by name, call by value, or other evaluation strategies. As for any typed language, type safety is a fundamental property of all of these evaluation strategies. Additionally, the strong normalization property described below implies that any evaluation strategy will terminate on all simply typed terms.

### Categorical semantics

The simply typed lambda calculus (with ${\displaystyle \beta \eta }$-equivalence) is the internal language of Cartesian closed categories (CCCs), as was first observed by Lambek. Given any specific CCC, the basic types of the corresponding lambda calculus are just the objects, and the terms are the morphisms. Conversely, every simply typed lambda calculus gives a CCC whose objects are the types, and morphisms are equivalence classes of terms.

To make the correspondence clear, a type constructor for the Cartesian product is typically added to the above. To preserve the categoricity of the Cartesian product, one adds type rules for pairing, projection, and a unit term. Given two terms ${\displaystyle s{\mathbin {:}}\sigma }$ and ${\displaystyle t{\mathbin {:}}\tau }$, the term ${\displaystyle (s,t)}$ has type ${\displaystyle \sigma \times \tau }$. Likewise, if one has a term ${\displaystyle u{\mathbin {:}}\tau _{1}\times \tau _{2}}$, then there are terms ${\displaystyle \pi _{1}(u){\mathbin {:}}\tau _{1}}$ and ${\displaystyle \pi _{2}(u){\mathbin {:}}\tau _{2}}$ where the ${\displaystyle \pi _{i}}$ correspond to the projections of the Cartesian product. The unit term, of type 1, is written as ${\displaystyle ()}$ and vocalized as 'nil', is the final object. The equational theory is extended likewise, so that one has

${\displaystyle \pi _{1}(s{\mathbin {:}}\sigma ,t{\mathbin {:}}\tau )=s{\mathbin {:}}\sigma }$
${\displaystyle \pi _{2}(s{\mathbin {:}}\sigma ,t{\mathbin {:}}\tau )=t{\mathbin {:}}\tau }$
${\displaystyle (\pi _{1}(u{\mathbin {:}}\sigma \times \tau ),\pi _{2}(u{\mathbin {:}}\sigma \times \tau ))=u{\mathbin {:}}\sigma \times \tau }$
${\displaystyle t{\mathbin {:}}1=()}$

This last is read as "if t has type 1, then it reduces to nil".

The above can then be turned into a category by taking the types as the objects. The morphisms ${\displaystyle \sigma \to \tau }$ are equivalence classes of pairs ${\displaystyle (x{\mathbin {:}}\sigma ,t{\mathbin {:}}\tau )}$ where x is a variable (of type ${\displaystyle \sigma }$)and t is a term (of type ${\displaystyle \tau }$), having no free variables in it, except for (optionally) x. Closure is obtained from currying and application, as usual.

More precisely, there exist functors between the category of Cartesian closed categories, and the category of simply-typed lambda theories.

It is common to extend this case to closed symmetric monoidal categories by using a linear type system. The reason for this is that the CCC is a special case of the closed symmetric monoidal category, which is typically taken to be the category of sets. This is fine for laying the foundations of set theory, but the more general topos seems to provide a superior foundation.

### Proof-theoretic semantics

The simply typed lambda calculus is closely related to the implicational fragment of propositional intuitionistic logic, i.e., minimal logic, via the Curry–Howard isomorphism: terms correspond precisely to proofs in natural deduction, and inhabited types are exactly the tautologies of minimal logic.

## Alternative syntaxes

The presentation given above is not the only way of defining the syntax of the simply typed lambda calculus. One alternative is to remove type annotations entirely (so that the syntax is identical to the untyped lambda calculus), while ensuring that terms are well-typed via Hindley-Milner type inference. The inference algorithm is terminating, sound, and complete: whenever a term is typable, the algorithm computes its type. More precisely, it computes the term's principal type, since often an unannotated term (such as ${\displaystyle \lambda x.~x}$) may have more than one type (${\displaystyle {\mathtt {int}}\to {\mathtt {int}}}$, ${\displaystyle {\mathtt {bool}}\to {\mathtt {bool}}}$, etc., which are all instances of the principal type ${\displaystyle \alpha \to \alpha }$).

Another alternative presentation of simply typed lambda calculus is based on bidirectional type checking, which requires more type annotations than Hindley-Milner inference but is easier to describe. The type system is divided into two judgments, representing both checking and synthesis, written ${\displaystyle \Gamma \vdash e\Leftarrow \tau }$ and ${\displaystyle \Gamma \vdash e\Rightarrow \tau }$ respectively. Operationally, the three components ${\displaystyle \Gamma }$, ${\displaystyle e}$, and ${\displaystyle \tau }$ are all inputs to the checking judgment ${\displaystyle \Gamma \vdash e\Leftarrow \tau }$, whereas the synthesis judgment ${\displaystyle \Gamma \vdash e\Rightarrow \tau }$ only takes ${\displaystyle \Gamma }$ and ${\displaystyle e}$ as inputs, producing the type ${\displaystyle \tau }$ as output. These judgments are derived via the following rules:

Observe that rules [1]–[4] are nearly identical to rules (1)–(4) above, except for the careful choice of checking or synthesis judgments. These choices can be explained like so:

1. If ${\displaystyle x{\mathbin {:}}\sigma }$ is in the context, we can synthesize type ${\displaystyle \sigma }$ for ${\displaystyle x}$.
2. The types of term constants are fixed and can be synthesized.
3. To check that ${\displaystyle \lambda x.~e}$ has type ${\displaystyle \sigma \to \tau }$ in some context, we extend the context with ${\displaystyle x{\mathbin {:}}\sigma }$ and check that ${\displaystyle e}$ has type ${\displaystyle \tau }$.
4. If ${\displaystyle e_{1}}$ synthesizes type ${\displaystyle \sigma \to \tau }$ (in some context), and ${\displaystyle e_{2}}$ checks against type ${\displaystyle \sigma }$ (in the same context), then ${\displaystyle e_{1}~e_{2}}$ synthesizes type ${\displaystyle \tau }$.

Observe that the rules for synthesis are read top-to-bottom, whereas the rules for checking are read bottom-to-top. Note in particular that we do not need any annotation on the lambda abstraction in rule [3], because the type of the bound variable can be deduced from the type at which we check the function. Finally, we explain rules [5] and [6] as follows:

1. To check that ${\displaystyle e}$ has type ${\displaystyle \tau }$, it suffices to synthesize type ${\displaystyle \tau }$.
2. If ${\displaystyle e}$ checks against type ${\displaystyle \tau }$, then the explicitly annotated term ${\displaystyle (e{\mathbin {:}}\tau )}$ synthesizes ${\displaystyle \tau }$.

Because of these last two rules coercing between synthesis and checking, it is easy to see that any well-typed but unannotated term can be checked in the bidirectional system, so long as we insert "enough" type annotations. And in fact, annotations are needed only at β-redexes.

## General observations

Given the standard semantics, the simply typed lambda calculus is strongly normalizing: that is, well-typed terms always reduce to a value, i.e., a ${\displaystyle \lambda }$ abstraction. This is because recursion is not allowed by the typing rules: it is impossible to find types for fixed-point combinators and the looping term ${\displaystyle \Omega =(\lambda x.~x~x)(\lambda x.~x~x)}$. Recursion can be added to the language by either having a special operator ${\displaystyle {\mathtt {fix}}_{\alpha }}$of type ${\displaystyle (\alpha \to \alpha )\to \alpha }$ or adding general recursive types, though both eliminate strong normalization.

Since it is strongly normalising, it is decidable whether or not a simply typed lambda calculus program halts: in fact, it always halts. We can therefore conclude that the language is not Turing complete.

## Notes

1. {{#invoke:citation/CS1|citation |CitationClass=book }}

## References

• A. Church: A Formulation of the Simple Theory of Types, JSL 5, 1940
• W.W.Tait: Intensional Interpretations of Functionals of Finite Type I, JSL 32(2), 1967
• G.D. Plotkin: Lambda-definability and logical relations, Technical report, 1973
• G.P. Huet: The Undecidability of Unification in Third Order Logic Information and Control 22(3): 257-267 (1973)
• H. Friedman: Equality between functionals. LogicColl. '73, pages 22-37, LNM 453, 1975.
• H. Schwichtenberg: Functions definable in the simply-typed lambda calculus, Arch. Math Logik 17 (1976) 113-114.
• R. Statman: The Typed lambda-Calculus Is not Elementary Recursive FOCS 1977: 90-94
• W. D. Goldfarb: The undecidability of the 2nd order unification problem, TCS (1981), no. 13, 225- 230.
• R. Statman. ${\displaystyle \lambda }$-definable functionals and ${\displaystyle \beta \eta }$ conversion. Arch. Math. Logik, 23:21–26, 1983.
• J. Lambek: Cartesian Closed Categories and Typed Lambda-calculi. Combinators and Functional Programming Languages 1985: 136-175
• U. Berger, H. Schwichtenberg: An Inverse of the Evaluation Functional for Typed lambda-calculus LICS 1991: 203-211
• Jung, A.,Tiuryn, J.:A New Characterization of Lambda Definability, TLCA 1993
• R. Loader: The Undecidability of λ-definability, appeared in the Church Festschrift, 2001
• H. Barendregt, Lambda Calculi with Types, Handbook of Logic in Computer Science, Volume II, Oxford University Press, 1993. ISBN 0-19-853761-1.
• L. Baxter: The undecidability of the third order dyadic unification problem, Information and Control 38(2), 170-178 (1978)