Introduction to the Untyped λ\lambda-Calculus

Key Points:

The untyped λ\lambda-Calculus is the smallest universal programming language first developed by Alonzo Church in 1932.

It has the same expressive power as Turing machines. It lends itself more the the study of properties of computation than Turing machines do.

The λ\lambda-Calculus contains terms (syntax) and rules to compute terms

Syntax

The λ\lambda-Calculus has only 3 constructs:

  1. Variables
  2. Functions
  3. (Function) Applications

Formal definition

let V\mathcal{V} be a set of variables
let x,y,z,x,y,z,\ldots range over V\mathcal{V}

The following Backus-Naur form inductively defines the syntax:

M::=xλx.MMMM ::= x | \lambda x.M | M M

**terms can be surrounded with parenthesis in ambiguous cases

A λ\lambda-term λx.M\lambda x.M is an anonymous function, also known as a λ\lambda abstraction. It has 1 parameter xx with a body MM

Applications of the form MNM N are used to pass arguments to functions

we sometimes write λx1,,xn.M\lambda x_1,\ldots,x_n.M instead of λx1λxn.M\lambda x_1 \ldots \lambda x_n.M

Officially a function with 1 parameter would take the form λx.M\lambda x.M and a function with 2 parameters would take the form λx.λy.M\lambda x. \lambda y.M

We use brackets to remove ambiguity but in general, our precedence rule is that application has higher precedence over λ\lambda
We also use an associativity rule, application is left associative, i.e: xyzxyz stands for (xy)z(xy)z


Exercise:

Write parentheses appropriately around all lambda as applications:

  1. λx.λy.xy\lambda x.\lambda y.xy

(λx.(λy.(xy)))(\lambda x.(\lambda y.(xy)))

  1. λx.xλx.xx\lambda x.x\lambda x.xx

(λx.(x(λx.(xx))))(\lambda x.(x(\lambda x.(xx))))

  1. λx.xxλx.x\lambda x.xx\lambda x.x

(λx.((xx)(λx.x)))(\lambda x.((xx)(\lambda x.x)))


Implementation of Types in the λ\lambda-Calculus

Boolean

T = λx.λy.x\lambda x.\lambda y.x
F = λx.λy.y\lambda x.\lambda y.y
and=λa.λb.abF\texttt{and} = \lambda a.\lambda b.a b F
or=λa.λb.aTB\texttt{or} = \lambda a.\lambda b.a T B
notλx.aFT\texttt{not} \lambda x.a F T

Numbers (Church Numerals)

0=λf.λx.x\underline{0} = \lambda f . \lambda x.x
1=λf.λx.fx\underline{1} = \lambda f.\lambda x . f x
2=λf.λx.f(fx)\underline{2} = \lambda f.\lambda x . f (f x)
\cdots
succ=λa.λf.λx.f(afx)\texttt{succ} = \lambda a . \lambda f .\lambda x.f (a f x)
add=λa.λbλf.λx.afx(bfx)\texttt{add} = \lambda a. \lambda b \lambda f.\lambda x.a f x (b f x)

essentially with x being a base case (0) each application of ff represents another successor

Abstract Syntax Trees

We can use these rules to derive trees where:

We refer to these trees as abstract syntax trees

Examples

λx.x\lambda x.x

graph TD; \ --> x. \ --> x

λx.λy.xy\lambda x.\lambda y.xy

graph TD; \ --> x \ --> \, \, --> y \, --> * * --> x, * --> y,

x(λy.z)x(\lambda y.z)

graph TD; * --> x * --> \ \ --> y \ --> z

Exercise

What are the abstract syntax trees corresponding to:

λf.λx.x\lambda f.\lambda x.x

graph TD; \ --> f \ --> \. \. --> x \. --> x.

λf.λx.λx\lambda f.\lambda x.\lambda x

graph TD; \ --> f \ --> \. \. --> x \. --> * * --> f. * --> x.

λf.λx.f(fx)\lambda f.\lambda x.f(fx)

graph TD \ --> f \ --> \. \. --> x \. --> * * --> f. *--> *. *. --> f, *. --> x.

Free and Bound Variables

Given a λ\lambda- term, λx.M\lambda x.M

A variable xx is either

xx can occur both free and bound in some cases: x(λx.x)x(\lambda x.x)

We say that the occurrence of a bound variable xx is bound to a λ\lambda in a term MM, fi the corresponding λ\lambda-abstraction is the smallest λ\lambda-abstraction in MM with parameter xx containing that bound occurrence

For example:
(λx.x)(λx.x)(\lambda x.x)(\lambda x.x)

The set of free variables occurring in a term is recursively computed as:

FV(x)={x}FV(λx.M)=FV(M) {x}FV(MN)=FV(M)FV(N)FV(x) = \{x\} \\ FV(\lambda x.M) = FV(M) \ \{x\} \\ FV(M N) = FV(M) \cup FV(N)

We can then say a variable xx occurs free in MM iff xFV(M)x \in FV(M)

A term MM is closed if FV(M)=FV(M) = \empty

α\alpha-equivalence

Bound variable names are not important and can be renamed without changing the meaning of a term

Two terms MM and NN are α\alpha-equivalent - denoted MαNM \equiv_{\alpha} N if they only differ by their bound variables, i.e

TODO Finish