# Foundations of Functional Programming/Untyped λ-calculus

The untyped λ-calculus is a λ-calculus which does not include any notion of types. It is a bare-bones model of computation, based entirely on the notion of functions. It is Turing complete.

In the untyped λ-calculus (henceforth just λ-calculus), functions are the only form of data. Functions take functions as arguments, and return functions. The functions in λ-calculus are pure functions, which compute an output from an input without producing side effects.

## Syntax

In the λ-calculus there are three kinds of expressions: variables, function applications, and λ-abstractions. We use upper case letters ${\displaystyle A,B,C,...}$  to stand for expressions. We write variables as lower case letters ${\displaystyle a,b,c,...}$ . The application of the function ${\displaystyle A}$  to the argument ${\displaystyle B}$  is written ${\displaystyle AB}$ . A λ-abstraction is an expression of the form ${\displaystyle \lambda x.A}$ , which represents the function which takes the argument ${\displaystyle x}$  and returns the result ${\displaystyle A}$  (where ${\displaystyle A}$  is an expression that might involve ${\displaystyle x}$ ).

Here is a BNF grammar for the λ-calculus:

${\displaystyle {\begin{array}{rcl}{\text{Var}}&::=&a\ |\ b\ |\ c\ |\ \cdots \\{\text{Expr}}&::=&{\text{Var}}\\&|&{\text{Expr}}\ {\text{Expr}}\\&|&\lambda \ {\text{Var}}\ .\ {\text{Expr}}\end{array}}}$

One may add parentheses to expressions to disambiguate scope. ${\displaystyle \lambda }$  binds everything after it, and application is left associative. We use ${\displaystyle \lambda x_{1},...,x_{n}.A}$  to abbreviate ${\displaystyle \lambda x_{1}.\lambda x_{2}.\cdots \lambda x_{n}.A}$ . Multi-argument functions are curried.

## Variables and substitution

We say that a given occurrence of a variable ${\displaystyle x}$  in an expression ${\displaystyle A}$  is a "free occurrence" if ${\displaystyle x}$  is not in the scope of a λ-abstraction whose argument is named ${\displaystyle x}$ . Otherwise, it is a "bound occurrence."

A bound variable ${\displaystyle x}$  binds to the innermost λ-abstraction over ${\displaystyle x}$  which surrounds it, if there are multiple such λ-abstractions. For example, in ${\displaystyle \lambda x\lambda y\lambda x.xy}$ , the ${\displaystyle x}$  in ${\displaystyle xy}$  binds to the innermost λ-abstraction, rather than the outermost one.

One can rename bound variables in an expression without changing the meaning of the expression. The process of renaming bound variables is sometimes called α-conversion. We will usually regard expressions which can be produced from each other by α-conversion as being the same expression; this is convenient for mathematical exposition, making certain theorems simpler to state, for example.

Renaming free variables in an expression does, in general, change its meaning. To see why this makes sense, consider that in a programming language, while you can rename an argument to a function without changing what the function does, you cannot replace the name of a global variable or function it references with some other name without changing what the function does.

We write ${\displaystyle A[x/B]}$  to denote the result of replacing all free occurrences of ${\displaystyle x}$  in expression ${\displaystyle A}$  with expression ${\displaystyle B}$ . When we do this replacement, if any free occurrences of variables in ${\displaystyle B}$  would become bound by λ-abstractions in ${\displaystyle A}$ , we rename the bound variables in ${\displaystyle A}$  to avoid the conflict.

## β-reduction

The notion of program execution corresponds, in the λ-calculus, to the notion of β-reduction. The untyped λ-calculus is a language with non-strict evaluation, which admits of multiple possible evaluation strategies.

The basic relationship of β-reduction is:

${\displaystyle (\lambda x.A)B\to _{\beta }A[x/B]}$

One can β-reduce an expression by performing the above transformation on it. One can also β-reduce an expression by performing the above transformation on any of its subexpressions. This rule is stated more formally as follows.

• Suppose ${\displaystyle A\to _{\beta }B}$ . Then, for all expressions ${\displaystyle C}$  and variables ${\displaystyle x}$ :
• ${\displaystyle AC\to _{\beta }BC}$ .
• ${\displaystyle CA\to _{\beta }CB}$ .
• ${\displaystyle \lambda x.A\to _{\beta }\lambda x.B}$

Note that this rule is to be applied recursively, so that one can β-reduce expressions nested arbitrarily deep in an expression in order to β-reduce the whole expression.

It is clear that the β-reduction relationship allows for multiple evaluation strategies, since for example one can β-reduce an expression ${\displaystyle AB}$  by first β-reducing ${\displaystyle A}$  and then β-reducing ${\displaystyle B}$ , or the other way around.

## Normalization

The notion of halting computation corresponds in the λ-calculus to the notion of "normalization." We say that an expression is a "β-normal form" if no further β-reduction steps on it are possible. This is equivalent to saying that it contains no expression of the form ${\displaystyle (\lambda x.A)B}$ . A β-normal form is analogous to the output of a computation that has halted.

We say that an expression has a β-normal form if there is a series of β-reduction steps which can be performed on it to turn it into a β-normal form. To say that an expression has a β-normal form means that it expresses a halting computation.

Not all expressions have β-normal forms; not all computations halt. An example of an expression with no β-normal form is the expression:

${\displaystyle \Omega =(\lambda x.xx)(\lambda x.xx)}$

You can easily see that the only β-reduction step on ${\displaystyle \Omega }$  which is possible reduces it to itself. Since it is not itself a β-normal form, and β-reduction can only turn it into itself, ${\displaystyle \Omega }$  has no β-normal form.

## Evaluation strategy and uniqueness of β-normal forms

One question one might ask is this: does every expression which has a β-normal form, have a unique β-normal form? There are in general many different strategies for β-reducing an expression, and a priori it is not obvious that they should all produce the same result in the end. But, we would like it to be true that they all produce the same result in the end, because we don't want the output of a computation to depend on the evaluation strategy used to reduce it.

The Church-Rosser theorem implies that there is a unique β-normal form for any expression which has one. The Church-Rosser theorem states that if ${\displaystyle A}$  is an expression, and ${\displaystyle B_{1}}$  and ${\displaystyle B_{2}}$  can both be produced from ${\displaystyle A}$  by a series of β-reduction steps, then there is another expression ${\displaystyle C}$  such that both ${\displaystyle B_{1}}$  and ${\displaystyle B_{2}}$  can be β-reduced to ${\displaystyle C}$ .

It follows from the Church-Rosser theorem that every expression has at most one β-normal form. This means that the result of a computation is independent of evaluation strategy.

One qualifier should be made to this statement. Some evaluation strategies might never reduce an expression to a β-normal form, even though the expression has a β-normal form which can be reached through a different evaluation strategy. For instance, eager evaluation, which always tries to β-reduce the arguments to a function before applying the function, will fail to reach a β-normal form on this expression:

• ${\displaystyle (\lambda x\lambda y.x)(\lambda z.z)\Omega }$

This expression consists of the function ${\displaystyle \lambda x\lambda y.x}$  which just returns its first argument, applied to the identity function ${\displaystyle \lambda z.z}$  and the non-terminating computation ${\displaystyle \Omega }$ . Its β-normal form is ${\displaystyle \lambda z.z}$ , but this normal form will never be reached by eager evaluation, because eager evaluation will try to finish β-reducing ${\displaystyle \Omega }$  before calling ${\displaystyle \lambda x\lambda y.x}$ .

The following evaluation strategy will always reach the β-normal form of an expression, if it has one: look for the first subexpression of the form ${\displaystyle (\lambda x.A)B}$  (i.e., the one whose λ is leftmost in the whole expression), β-reduce that subexpression, and repeat.

This evaluation strategy bears some resemblance to lazy evaluation, but it is not the same thing. Lazy evaluation, or at least one version of it which makes sense for the λ-calculus, is the following strategy: if the whole expression is of the form ${\displaystyle (\lambda x.A)B}$ , then β-reduce it to ${\displaystyle A[x/B]}$ , and otherwise do nothing.

Lazy evaluation, at least on this presentation, will stop evaluating an expression if the whole expression is a λ-abstraction, even though the expression might not be in normal form, because it might have subexpressions which are not in normal form. One thing this illustrates is that lazy evaluation isn't even necessarily a normalizing evaluation strategy; it may stop and consider the expression evaluated before the expression reaches a normal form. The same is true of eager evaluation: eager evaluation will generally halt when the whole expression is a λ-abstraction, even if the λ-abstraction contains subexpressions which are not normalized.

## Programming in the λ-calculus

The λ-calculus is Turing complete; all Turing computable functions can be expressed in it. This might be a surprise, since it does not even include basic notions like numbers. In fact, all of the ordinary data types can be coded as functions. This is not an efficient way to represent ordinary data types in a real programming language, but conceptually it is perfectly possible. In this section we explain how to do this for booleans, lists, and natural numbers.

### Booleans

We can code the boolean values as follows:

• ${\displaystyle \mathbf {true} =\lambda x\lambda y.x}$
• ${\displaystyle \mathbf {false} =\lambda x\lambda y.y}$

A boolean is a function which takes two arguments, representing what to do if the value is true, and what to do if the value is false. Given this definition, if-then-else can be defined as follows:

• ${\displaystyle \mathbf {if} =\lambda b\lambda t\lambda e.bte}$

That is, "if ${\displaystyle b}$  then ${\displaystyle t}$ , else ${\displaystyle e}$ " can be coded as ${\displaystyle bte}$ . Boolean negation can be defined as follows:

• ${\displaystyle \mathbf {not} =\lambda b\lambda t\lambda e.bet}$

Logical "and" and "or" can be defined as follows:

• ${\displaystyle \mathbf {and} =\lambda a\lambda b.a\ b\ \mathbf {false} }$
• ${\displaystyle \mathbf {or} =\lambda a\lambda b.a\ \mathbf {true} \ b}$

### Lists

We code a Lisp-style linked list as a function ${\displaystyle l}$  which takes two arguments ${\displaystyle f}$  and ${\displaystyle g}$ . If ${\displaystyle l}$  is empty, it returns ${\displaystyle g}$ . If ${\displaystyle l}$  is nonempty, it calls ${\displaystyle f}$  with two arguments: the first element of the list, and the rest of the list. We can define the basic list operations as follows:

• ${\displaystyle \mathbf {nil} =\lambda f\lambda g.g}$
• ${\displaystyle \mathbf {cons} =\lambda a\lambda b\lambda f\lambda g.fab}$
• ${\displaystyle \mathbf {head} =\lambda l.l(\lambda h\lambda t.h)\Omega }$
• ${\displaystyle \mathbf {tail} =\lambda l.l(\lambda h\lambda t.t)\Omega }$
• ${\displaystyle \mathbf {isEmpty} =\lambda l.l(\lambda h\lambda t.\mathbf {false} )\mathbf {true} }$

Note: we have defined taking the head or tail of an empty list to result in non-termination.

### Natural numbers

Once we have lists, defining natural numbers is easy. One way to define them is to code 0 as ${\displaystyle \mathbf {nil} }$ , 1 as ${\displaystyle \mathbf {cons} \ \mathbf {nil} \ \mathbf {nil} }$ , and in general we code ${\displaystyle n+1}$  as ${\displaystyle \mathbf {cons} \ \mathbf {nil} \ n}$ :

• ${\displaystyle \mathbf {0} =\mathbf {nil} }$
• ${\displaystyle \mathbf {isZero} =\mathbf {isEmpty} }$
• ${\displaystyle \mathbf {succ} =\lambda n.\mathbf {cons} \ \mathbf {nil} \ n}$

We can then define addition and multiplication by recursion.

### Recursion

One sticking point remains: how do we do recursion in the λ-calculus? Since functions don't have names, the definition of a function can't refer to the function itself. We can get around this using a function called the Y combinator, which gives a function as an argument to itself:

${\displaystyle \mathbf {Y} =\lambda f.(\lambda x.f(xx))(\lambda x.f(xx))}$

The key property of ${\displaystyle \mathbf {Y} }$  is that for all functions ${\displaystyle f}$ , we have ${\displaystyle \mathbf {Y} f=f(\mathbf {Y} f)}$ . Let us prove this:

${\displaystyle \mathbf {Y} (f)=(\lambda x.f(xx))(\lambda x.f(xx))=f((\lambda x.f(xx))(\lambda x.f(xx))=f(\mathbf {Y} f)}$

Now let us see how to use the ${\displaystyle \mathbf {Y} }$  combinator to implement a recursive function. We will take the example of addition. Addition is defined recursively as follows:

• ${\displaystyle n+0=n}$
• ${\displaystyle n+(\mathbf {succ} \ m)=\mathbf {succ} \ (n+m)}$

We will write the addition function as a function which takes itself as an argument. (More precisely, it takes the recursive version of itself, which just takes two numbers, as an argument.) Here is how it is defined:

• ${\displaystyle \mathbf {plus} _{0}=\lambda p\lambda n\lambda m.\ \mathbf {if} \ (\mathbf {isZero} \ m)\ n\ (\mathbf {succ} \ (p\ n\ (\mathbf {tail} \ m)))}$
• ${\displaystyle \mathbf {plus} =\mathbf {Y} \ \mathbf {plus} _{0}}$

Let us take apart how this works. The definition of ${\displaystyle \mathbf {plus} _{0}}$  is just a straightforward transcription of the recursive definition of addition, except that it uses its argument ${\displaystyle p}$  in place of the addition function. When we take ${\displaystyle \mathbf {plus} =\mathbf {Y} \ \mathbf {plus} _{0}}$ , we get a function which is equal to ${\displaystyle \mathbf {plus} _{0}(\mathbf {Y} \ \mathbf {plus} _{0})=\mathbf {plus} _{0}(\mathbf {plus} )}$ ; so we have succeeded in defining ${\displaystyle \mathbf {plus} }$  in terms of itself.