next up previous contents
Next: Polymorphic typed calculi Up: Introducing types into the Previous: Introducing types into the   Contents

Simply typed lambda calculus

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


\begin{displaymath}
t := 0 \mid t \to t
\end{displaymath}

Thus, examples of types are $0$, $0 \to 0$, $(0 \to 0) \to 0 \to 0$. As we have seen in Haskell, the operator $\to$ 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 $s$, we fix a separate set of variables. Then, we define by mutual induction the set $\Lambda_s$ of lambda terms of each type $s$ as follows:

  1. For each $s$, every variable of type $s$ is in $\Lambda_s$.

  2. If $M$ is a term in $\Lambda_{s \to t}$ and $N$ is a term in $\Lambda_s$ then $(M N)$ is a term in $\Lambda_t$.

  3. If $M$ is a term in $\Lambda_t$ and $x$ is a variable of type $s$ then $(\lambda x.M)$ is a term in $\Lambda_{s \to t}$.

Notice that every term we construct has a well defined type. Terms like $(f f)$ that gave us some apparently contradictory behaviour earlier cannot be assigned a type (why?) and are thus not legal terms of this calculus.

The $\beta $ rule for the simply typed lambda calculus is the usual one, with a check for type consistency:

$(\lambda x.M) N \rightarrow _\beta M\{x \leftarrow N\}$ provided $x$ and $N$ are of the same type $s$
  and $M$ is of type $s \to t$

It turns out that the corresponding reduction relation $\rightarrow ^*$ 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 $\beta $ 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 $(f f)$ 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 $M$ is a typable term, then we can find a unique type $s$ for $M$ such that every possible type for $M$ is an instance of $s$ (that is, can be obtained by replacing some of the $0$'s in $s$ by another type). Such a type is called the ``principal type scheme'' for M.

Observe also that the encodings $\langle{m}\rangle $ 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.


next up previous contents
Next: Polymorphic typed calculi Up: Introducing types into the Previous: Introducing types into the   Contents
Madhavan Mukund 2004-04-29