In the simply typed lambda calculus, we begin with a syntax for types. Since we have no constants in our set of lambda terms, we fix a single type constant 0 that represents an atomic type. The syntax of types is then given by the grammar
Thus, examples of types are , , . As we have seen in Haskell, the operator associates to the right.
We now build up typed terms inductively in such a way that only well typed terms are generated.
First, for each type , we fix a separate set of variables. Then, we define by mutual induction the set of lambda terms of each type as follows:
Notice that every term we construct has a well defined type. Terms like that gave us some apparently contradictory behaviour earlier cannot be assigned a type (why?) and are thus not legal terms of this calculus.
The rule for the simply typed lambda calculus is the usual one, with a check for type consistency:
provided and are of the same type | |
and is of type |
It turns out that the corresponding reduction relation for this calculus is Church-Rosser. In fact, this calculus has a much stronger property--it is strongly normalizing.
We say that a term is normalizing if it has a normal form. Further, at term is strongly normalizing if every reduction sequence that the term admits leads to a normal form. We have seen examples of non-normalizing and non-strongly normalizing terms in the untyped calculus. For instance
We say that a lambda calculus is strongly normalizing if every term in the calculus is strongly normalizing. It turns out that the simply typed lambda calculus is strongly normalizing. We will not prove this result, but one intuitive explanation is that every application of reduction strictly reduces the type complexity of the lambda term, so there is a natural limitation on the length of a reduction from a fixed starting term.
We have observed that the syntax of the simply typed lambda calculus permits only well-typed terms. What if we ask the converse question--given an arbitrary term in the untyped lambda calculus, is it well-typed? That is, is it a valid term in the simply typed lambda calculus? For instance, a term of the form cannot be assigned a sensible type and is hence clearly not definable in the simply typed calculus. It turns out that this problem is decidable.
In fact, if is a typable term, then we can find a unique type for such that every possible type for is an instance of (that is, can be obtained by replacing some of the 's in by another type). Such a type is called the ``principal type scheme'' for M.
Observe also that the encodings for natural numbers that we have provided are well typed. Hence, we can carry over much of our arithmetic from the untyped calculus to the simply typed calculus.