Algorithm Search⌘K

Modelcore

Simply typed lambda calculus

Also called STLC, Church's simple theory of types.

The lambda calculus with base types and function types; each well-typed term has a unique type, evaluation never gets stuck, and every reduction sequence terminates.

The simply typed lambda calculus (STLC) is the lambda calculus in which each bound variable has a type built from base types with the arrow →\to. Church introduced it in 1940 as a logic that avoids the paradoxes of the untyped system. It is the smallest useful type system with functions, and most typed languages start from it. Its type checks run in linear time, its well-typed terms never get stuck, and every reduction of a well-typed term terminates. That last property also means it cannot express every computable function.

The machine

The STLC is a model of programs, not a machine model. A "run" is a sequence of small steps on a term. The version below has two base types, Booleans and integers, and call-by-value evaluation, as in Chapter 9 of Pierce's book with integers added.

Syntax

T::=Bool∣Int∣T→Tt::=x∣λx:T. t∣t t∣true∣false∣if t then t else t∣n∣t+tv::=λx:T. t∣true∣false∣n\begin{aligned} T &::= \mathsf{Bool} \mid \mathsf{Int} \mid T \to T \\ t &::= x \mid \lambda x{:}T.\,t \mid t\ t \mid \mathsf{true} \mid \mathsf{false} \mid \mathsf{if}\ t\ \mathsf{then}\ t\ \mathsf{else}\ t \mid n \mid t + t \\ v &::= \lambda x{:}T.\,t \mid \mathsf{true} \mid \mathsf{false} \mid n \end{aligned}

Here nn ranges over the integer literals. The arrow associates to the right: Int→Int→Int\mathsf{Int} \to \mathsf{Int} \to \mathsf{Int} is Int→(Int→Int)\mathsf{Int} \to (\mathsf{Int} \to \mathsf{Int}). Application associates to the left. Terms are equal up to the renaming of bound variables. In the surface syntax of this site, λx:T. t\lambda x{:}T.\,t is written fun x:T -> t.

Typing

A context Γ\Gamma is a list x1:T1,…,xk:Tkx_1{:}T_1, \dots, x_k{:}T_k of distinct variables with types. The judgment Γ⊢t:T\Gamma \vdash t : T is the smallest relation closed under these rules.

x:T in Gamma               Gamma, x:T1 |- t2 : T2               Gamma |- t1 : T11 -> T12    Gamma |- t2 : T11
-------------  T-Var       ---------------------------  T-Abs   ---------------------------------------------  T-App
Gamma |- x : T             Gamma |- \x:T1. t2 : T1 -> T2         Gamma |- t1 t2 : T12

---------------------  T-True      ----------------------  T-False      -----------------  T-Int
Gamma |- true : Bool               Gamma |- false : Bool                Gamma |- n : Int

Gamma |- t1 : Bool    Gamma |- t2 : T    Gamma |- t3 : T          Gamma |- t1 : Int    Gamma |- t2 : Int
------------------------------------------------------  T-If     ---------------------------------------  T-Add
Gamma |- if t1 then t2 else t3 : T                                Gamma |- t1 + t2 : Int

The rules are syntax directed: for each form of term, exactly one rule can end a derivation. This gives the inversion lemma (Pierce, Lemma 9.3.1): if Γ⊢t1 t2:R\Gamma \vdash t_1\,t_2 : R, then Γ⊢t1:T11→R\Gamma \vdash t_1 : T_{11} \to R and Γ⊢t2:T11\Gamma \vdash t_2 : T_{11} for some T11T_{11}, and the same for each other form.

Evaluation

The call-by-value small-step relation t→t′t \to t' has these rules.

t1 --> t1'                     t2 --> t2'
-----------------  E-App1      -----------------  E-App2      ---------------------------------  E-AppAbs
t1 t2 --> t1' t2               v1 t2 --> v1 t2'               (\x:T. t) v2 --> [x := v2] t

-----------------------------------  E-IfTrue     ------------------------------------  E-IfFalse
if true then t2 else t3 --> t2                    if false then t2 else t3 --> t3

t1 --> t1'
-----------------------------------------------------  E-If
if t1 then t2 else t3 --> if t1' then t2 else t3

t1 --> t1'                     t2 --> t2'                     n = n1 + n2
-------------------  E-Add1    -------------------  E-Add2    --------------  E-AddInt
t1 + t2 --> t1' + t2           v1 + t2 --> v1 + t2'           n1 + n2 --> n

[x:=v]t[x := v]t is capture-avoiding substitution. The relation is deterministic: each term has at most one next step. A closed term tt is stuck if it is not a value and has no next step, for example true+1\mathsf{true} + 1 or 3 43\ 4. Evaluation does not go under λ\lambda, so a λ\lambda is a value even if its body has a redex.

Church style and Curry style

The calculus above is Church style: each λ\lambda carries the type of its variable. In the Curry-style version, terms are untyped (λx. t\lambda x.\,t), and T-Abs may choose any T1T_1. A Curry-style term then has many types: λx. x\lambda x.\,x has Int→Int\mathsf{Int} \to \mathsf{Int}, Bool→Bool\mathsf{Bool} \to \mathsf{Bool} and every T→TT \to T. Hindley (1969) showed that each typable Curry-style term has a principal type with type variables, here α→α\alpha \to \alpha, of which every other type is an instance. The two styles have the same typable terms after erasure of the annotations, and they behave the same under evaluation.

A worked derivation and run

Let twice=λf:Int→Int. λx:Int. f (f x)\mathit{twice} = \lambda f{:}\mathsf{Int}{\to}\mathsf{Int}.\ \lambda x{:}\mathsf{Int}.\ f\ (f\ x) and inc=λy:Int. y+1\mathit{inc} = \lambda y{:}\mathsf{Int}.\ y + 1. Write Γ=f:Int→Int,x:Int\Gamma = f{:}\mathsf{Int}{\to}\mathsf{Int}, x{:}\mathsf{Int}. The derivation of the type of twice\mathit{twice} is:

                         Gamma |- f : Int->Int (T-Var)   Gamma |- x : Int (T-Var)
                         --------------------------------------------------- T-App
Gamma |- f : Int->Int    Gamma |- f x : Int
------------------------------------------- T-App
Gamma |- f (f x) : Int
------------------------------------------- T-Abs
f:Int->Int |- \x:Int. f (f x) : Int -> Int
------------------------------------------- T-Abs
|- twice : (Int -> Int) -> Int -> Int

Then T-App twice gives ⊢twice inc 3:Int\vdash \mathit{twice}\ \mathit{inc}\ 3 : \mathsf{Int}. The run takes six steps, and a script with a checker and a stepper for this calculus gives the same type and the same trace.

step term rule
0 twice inc 3\mathit{twice}\ \mathit{inc}\ 3
1 (λx:Int. inc (inc x)) 3(\lambda x{:}\mathsf{Int}.\ \mathit{inc}\ (\mathit{inc}\ x))\ 3 E-App1 with E-AppAbs
2 inc (inc 3)\mathit{inc}\ (\mathit{inc}\ 3) E-AppAbs
3 inc (3+1)\mathit{inc}\ (3 + 1) E-App2 with E-AppAbs
4 inc 4\mathit{inc}\ 4 E-App2 with E-AddInt
5 4+14 + 1 E-AppAbs
6 55 E-AddInt

Each term in the trace has type Int\mathsf{Int}, which is what preservation says.

What it counts

The cost measures are the ones of the untyped lambda calculus: the number of reduction steps under a chosen strategy, and the size of terms, counted as nodes of the syntax tree. Two facts make the typed setting different.

  • Every run ends. The number of steps is finite for every strategy (strong normalization, below). So the STLC computes only total functions, and it is not Turing complete.
  • Runs can still be very long. Let 2‾=λf.λx. f (f x)\underline{2} = \lambda f.\lambda x.\,f\,(f\,x) in Curry style, and let Ek=2‾ 2‾⋯2‾E_k = \underline{2}\ \underline{2} \cdots \underline{2} with kk copies, applied to the left. Each copy is typed with its own instance of (α→α)→α→α(\alpha \to \alpha) \to \alpha \to \alpha, and EkE_k itself has the principal type (α→α)→α→α(\alpha \to \alpha) \to \alpha \to \alpha for k=1k = 1 to 55 (checked by a script with unification). Since m‾ n‾=nm‾\underline{m}\ \underline{n} = \underline{n^m}, EkE_k normalizes to the numeral for the tower 22⋅⋅22^{2^{\cdot^{\cdot^{2}}}} of height kk: the script gives 2, 4, 16 and 65536 for k=1k = 1 to 44. E4E_4 has 31 nodes, and its normal form has 2⋅65536+3=1310752 \cdot 65536 + 3 = 131075 nodes. The growth in kk is faster than any fixed tower of exponentials, which is the root of Statman's lower bound below.

For the type checker, the measure is the size nn of the term with its annotations, and the cost model is a word RAM.

Relation to other models

  • Untyped lambda calculus. Erasing the annotations maps each STLC term to an untyped term with the same reductions. The image is a proper subset: λx. x x\lambda x.\,x\,x and the fixed-point combinators have no simple type.
  • Combinatory logic. The typed combinators K:A→B→AK : A \to B \to A and S:(A→B→C)→(A→B)→A→CS : (A \to B \to C) \to (A \to B) \to A \to C generate exactly the simply typed terms up to translation (combinatory logic). Their types are the axioms of the Hilbert system for implication.
  • Logic. By the Curry-Howard correspondence, simple types are formulas of intuitionistic implicational logic, terms are natural deduction proofs, and normalization is the removal of detours in proofs.
  • Extensions. Adding a fixed-point operator fix\mathsf{fix} gives PCF, which is Turing complete and loses termination. Adding natural numbers with primitive recursion at all types gives Gödel's System T. Adding ∀\forall over types gives System F. Adding polymorphism only at let gives Hindley-Milner. Adding subtyping or recursive types gives the systems on those pages.
  • Categories. Lambek showed that the STLC with products is the internal language of cartesian closed categories.

Known results in the model

Uniqueness of types. In Church style, if Γ⊢t:T\Gamma \vdash t : T and Γ⊢t:T′\Gamma \vdash t : T', then T=T′T = T' (Pierce, Theorem 9.3.3). Proof. Induction on tt. A variable has the one type that Γ\Gamma gives it. For λx:T1. t2\lambda x{:}T_1.\,t_2, inversion gives T=T1→T2T = T_1 \to T_2 and T′=T1→T2′T' = T_1 \to T_2' with both T2T_2 and T2′T_2' types of t2t_2 under Γ,x:T1\Gamma, x{:}T_1, so T2=T2′T_2 = T_2' by induction. For t1 t2t_1\,t_2, inversion gives t1:T11→Tt_1 : T_{11} \to T and t1:T11′→T′t_1 : T_{11}' \to T', so by induction the two arrow types are equal and T=T′T = T'. The other forms are similar.

Type soundness. If ⊢t:T\vdash t : T, then tt never reaches a stuck term: each closed well-typed term is a value or steps (progress), and a step keeps the type (preservation). The full proof, with the lemmas for inversion, canonical forms and substitution, is on the type soundness page.

Strong normalization (Tait 1967). If Γ⊢t:T\Gamma \vdash t : T, then there is no infinite sequence of full beta reductions t→βt1→βt2→β⋯t \to_\beta t_1 \to_\beta t_2 \to_\beta \cdots, where a step may contract any redex, also under λ\lambda. This is a strong form of termination: every strategy ends, not only call by value. We give a sketch here. The full proof is on the reducibility method page and in Chapter 6 of Girard, Lafont and Taylor. Sketch. Define a set REDT\mathrm{RED}_T of terms for each type: REDB\mathrm{RED}_B is the set of strongly normalizing terms of base type BB, and t∈REDA→Bt \in \mathrm{RED}_{A \to B} if t u∈REDBt\,u \in \mathrm{RED}_B for every u∈REDAu \in \mathrm{RED}_A. Prove by induction on TT the three conditions CR1 (every term in REDT\mathrm{RED}_T is strongly normalizing), CR2 (the set is closed under reduction), and CR3 (a neutral term, that is a term not of the form λx.t\lambda x.t, whose one-step reducts are all in REDT\mathrm{RED}_T is in REDT\mathrm{RED}_T; so variables are in it). Then prove by induction on the typing derivation that if x1:A1,…,xk:Ak⊢t:Tx_1{:}A_1, \dots, x_k{:}A_k \vdash t : T and ui∈REDAiu_i \in \mathrm{RED}_{A_i}, then t[x1:=u1,…,xk:=uk]∈REDTt[x_1 := u_1, \dots, x_k := u_k] \in \mathrm{RED}_T. Taking ui=xiu_i = x_i and CR1 gives the theorem. The induction on types is what fails for the untyped calculus: there is no type to induct on. Weak normalization, that one strategy always ends, has a simpler proof by a measure on the redexes (weak normalization).

Confluence. Typed beta reduction is confluent, as a restriction of the untyped calculus (Church-Rosser theorem). With strong normalization, each term has exactly one normal form, and beta equality is decidable: normalize both terms and compare.

Type checking is linear. Church-style checking of Γ⊢t:T\Gamma \vdash t : T runs in O(n)O(n) time on a word RAM, by a reduction to Paterson-Wegman unification or, in expected time, by one bottom-up pass with hash-consed types. Naive tree comparison of types gives Θ(n2)\Theta(n^2) in the worst case. Details are on the type checking problem page.

Typability is linear. For Curry-style terms, typability and the principal type come from constraint generation and first-order unification, in O(n)O(n) time with a DAG representation. Written out as a tree, the principal type can be exponentially larger than the term. See the type inference problem.

Inhabitation is PSPACE-complete (Statman 1979). The question "is there a closed term of type TT?" is PSPACE-complete in the size of TT. Under Curry-Howard it is provability in intuitionistic implicational logic. For example, (α→β)→(β→γ)→α→γ(\alpha \to \beta) \to (\beta \to \gamma) \to \alpha \to \gamma is inhabited, but Peirce's law ((α→β)→α)→α((\alpha \to \beta) \to \alpha) \to \alpha is not (see the Curry-Howard correspondence).

Beta equality is not elementary (Statman 1979). Deciding whether two closed terms have the same normal form is decidable, but no algorithm runs in time bounded by a fixed tower of exponentials 22⋅⋅n2^{2^{\cdot^{\cdot^{n}}}}. The term family EkE_k above shows why normalization alone cannot be faster.

Expressive power (Schwichtenberg 1976). The functions Nk→N\mathbb{N}^k \to \mathbb{N} that closed terms of type Nα→⋯→Nα\mathsf{N}_\alpha \to \dots \to \mathsf{N}_\alpha compute on Church numerals, with Nα=(α→α)→α→α\mathsf{N}_\alpha = (\alpha \to \alpha) \to \alpha \to \alpha, are exactly the extended polynomials: the functions built from constants, projections, addition, multiplication and the test "if x=0x = 0 then yy else zz". So even subtraction by one is not definable at this fixed type. More functions become definable if the input type may differ from the output type.

References

  1. Benjamin C. Pierce, Types and Programming Languages, MIT Press, 2002, Chapter 9 (the rules T-Var, T-Abs, T-App, E-App1, E-App2, E-AppAbs; Lemma 9.3.1 inversion, Theorem 9.3.3 uniqueness of types, Lemma 9.3.4 canonical forms, Theorems 9.3.5 and 9.3.9 progress and preservation) and Chapter 12 (normalization)
  2. Alonzo Church, A formulation of the simple theory of types, Journal of Symbolic Logic 5(2), 1940, pages 56 to 68, the whole paper (the simple types built from base types by the function type, with lambda abstraction carrying the type of its variable)
  3. William W. Tait, Intensional interpretations of functionals of finite type I, Journal of Symbolic Logic 32(2), 1967, pages 198 to 212, the convertibility (computability) predicate and the normalization proof for terms of finite type
  4. Jean-Yves Girard, Yves Lafont and Paul Taylor, Proofs and Types, Cambridge University Press, 1989, Chapter 3 (the simply typed calculus), Chapter 4 (weak normalization) and Chapter 6 (strong normalization by reducibility, the conditions CR1 to CR3)
  5. Richard Statman, Intuitionistic propositional logic is polynomial-space complete, Theoretical Computer Science 9(1), 1979, pages 67 to 72, the main theorem and its corollary that inhabitation of simple types is PSPACE-complete
  6. Richard Statman, The typed lambda-calculus is not elementary recursive, Theoretical Computer Science 9(1), 1979, pages 73 to 81, the main theorem (beta-convertibility of closed simply typed terms is not elementary recursive)
  7. Helmut Schwichtenberg, Definierbare Funktionen im Lambda-Kalkul mit Typen, Archiv fur mathematische Logik und Grundlagenforschung 17 (1976), 113 to 114, the characterization of the functions definable on Church numerals as the extended polynomials
  8. J. Roger Hindley, Basic Simple Type Theory, Cambridge University Press, 1997, Chapter 2 (Curry-style typing) and Chapter 3 (principal types and the principal type algorithm)

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.

LadderLevelWhat the next level needs
Human proofH0 no proof
  • add a proof in proofs/ (06.5)
LeanL0 no Lean part
  • name a Lean module in the lean field (07)
VisualV0 none
  • add viz/scene.ts or a figure (10)
WritingW4 complete
  • a review of the text: no review record reviews/Z1D969/text.yaml