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 . 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
Here ranges over the integer literals. The arrow associates to the right: is . Application associates to the left. Terms are equal up to the renaming of bound variables. In the surface syntax of this site, is written fun x:T -> t.
Typing
A context is a list of distinct variables with types. The judgment 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 , then and for some , and the same for each other form.
Evaluation
The call-by-value small-step relation 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
is capture-avoiding substitution. The relation is deterministic: each term has at most one next step. A closed term is stuck if it is not a value and has no next step, for example or . Evaluation does not go under , so a is a value even if its body has a redex.
Church style and Curry style
The calculus above is Church style: each carries the type of its variable. In the Curry-style version, terms are untyped (), and T-Abs may choose any . A Curry-style term then has many types: has , and every . Hindley (1969) showed that each typable Curry-style term has a principal type with type variables, here , 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 and . Write . The derivation of the type of 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 . 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 | ||
| 1 | E-App1 with E-AppAbs | |
| 2 | E-AppAbs | |
| 3 | E-App2 with E-AppAbs | |
| 4 | E-App2 with E-AddInt | |
| 5 | E-AppAbs | |
| 6 | E-AddInt |
Each term in the trace has type , 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 in Curry style, and let with copies, applied to the left. Each copy is typed with its own instance of , and itself has the principal type for to (checked by a script with unification). Since , normalizes to the numeral for the tower of height : the script gives 2, 4, 16 and 65536 for to . has 31 nodes, and its normal form has nodes. The growth in 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 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: and the fixed-point combinators have no simple type.
- Combinatory logic. The typed combinators and 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 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 over types gives System F. Adding polymorphism only at
letgives 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 and , then (Pierce, Theorem 9.3.3). Proof. Induction on . A variable has the one type that gives it. For , inversion gives and with both and types of under , so by induction. For , inversion gives and , so by induction the two arrow types are equal and . The other forms are similar.
Type soundness. If , then 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 , then there is no infinite sequence of full beta reductions , where a step may contract any redex, also under . 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 of terms for each type: is the set of strongly normalizing terms of base type , and if for every . Prove by induction on the three conditions CR1 (every term in is strongly normalizing), CR2 (the set is closed under reduction), and CR3 (a neutral term, that is a term not of the form , whose one-step reducts are all in is in ; so variables are in it). Then prove by induction on the typing derivation that if and , then . Taking 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 runs in 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 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 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 ?" is PSPACE-complete in the size of . Under Curry-Howard it is provability in intuitionistic implicational logic. For example, is inhabited, but Peirce's law 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 . The term family above shows why normalization alone cannot be faster.
Expressive power (Schwichtenberg 1976). The functions that closed terms of type compute on Church numerals, with , are exactly the extended polynomials: the functions built from constants, projections, addition, multiplication and the test "if then else ". 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
- 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)
- 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)
- 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
- 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)
- 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
- 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)
- 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
- 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.
| Ladder | Level | What the next level needs |
|---|---|---|
| Human proof | H0 no proof |
|
| Lean | L0 no Lean part |
|
| Visual | V0 none |
|
| Writing | W4 complete |
|
Tag Z1D969 Edit this page History Report an error Markdown