Lably
Skip to content

Lambda calculus definition

Mathematical formalism

  • 11 min read
  • 3 chapters

Related on Lably

Through Lambda calculus, Alpha and Scope (computer programming)

Lambda terms

The lambda calculus consists of a language of lambda terms, that are defined by a certain formal syntax. The syntax of the lambda calculus defines some expressions as valid lambda calculus expressions and some as invalid, just as some strings of characters are valid computer programs and some are not. A valid lambda calculus expression is called a "lambda term". In the simplest form of lambda calculus, terms are built using only the following three rules. These rules give an inductive definition that can be applied to build all syntactically valid lambda terms, and produce expressions such as: (λx.λy.(λz.(λx.zx)(λy.zy))(xy)).

  1. A variable x is a character or string representing a parameter, itself a valid lambda term.
  2. A lambda abstraction (λx.M) is a function definition, taking as input the bound variable x (between the λ and the punctum/dot .) and returning the body M. The definition of a function with an abstraction merely "sets up" the function but does not invoke it. An abstraction denotes an anonymous function that takes a single input x and returns M. The syntax (λx.M) binds the variable x in the term M. For example, λx.(x2+2) is an abstraction representing the anonymous function x↦x2+2. More concretely, we might give this function the name f, and then we could write f(x)=x2+2,, although this name f is superfluous when using the lambda calculus.
  3. An application (MN) represents the application of a function M to an argument N. Both M and N are lambda terms. The application represents the act of calling function M on input N to produce M(N).

In Extended Backus-Naur Form, this might be summarized as e::=v∣(λv.e)∣(ee), where the variables v come from an infinite set v1,v2,v3,…, and the other symbols consist of lambda 'λ', dot '.', and parentheses '(' and ')'. A more formal and permissive presentation of the grammar might be as follows:

The set of lambda expressions is defined inductively, for example as a set Λ, where the results of applying rules 1-3 are all and only the elements of Λ. In the strictest sense, nothing else is a lambda term. That is, a lambda term is valid if and only if it can be obtained by repeated application of these three rules. Formally:

  1. If x is a variable, then x ∈ Λ.
  2. If x is a variable and M ∈ Λ, then (λx.M) ∈ Λ.
  3. If M, N ∈ Λ, then (M N) ∈ Λ.

Instances of rule 2 are known as abstractions and instances of rule 3 are known as applications.

It is also common to extend the syntax presented here with additional operations, for example introducing terms for mathematical constants and operations, which allows making sense of terms such as λx.x2. The untyped lambda calculus is flexible in that it does not distinguish between different kinds of data. For instance, there may be a function intended to operate on numbers. However, in the untyped lambda calculus, there is no way to prevent a function from being applied to truth values, strings, or other non-number objects. Depending on the encoding of the data, this may lead to nonsensical results, or work as intended.

Free and bound variables

Following the mathematical concepts of free variables and bound variables, the abstraction operator, λ, is said to bind its variable wherever it occurs in the body of the abstraction. Variables that fall within the scope of an abstraction are said to be bound, and the part λx is often called the binder of x. Variables that are not bound are called free. For example, the function definition f(x)=x+y could be represented as the lambda term λx.(x+y), which contains two variables, x and y. The variable x is bound by the lambda abstraction, while y is free. The free variable y has not been defined and is considered an unknown. The abstraction λx.(x+y) is a syntactically valid term and represents a function that adds its input to the yet-unknown y. Also note that a variable is bound by its "nearest" abstraction. In the following example the single occurrence of x in the expression is bound by the second lambda: λx.y(λx.zx) A variable may occur both free and bound in a term; for example y in y(λy.y).

More formally, the sets of free variables and bound variables of a lambda expression, M, are denoted as FV⁡(M) and BV⁡(M) and can be defined by recursion on the structure of the terms, as follows:

An expression that contains no free variables is said to be closed. Closed lambda expressions are also known as combinators and are equivalent to terms in combinatory logic. It is common to restrict discussion to only closed terms, and some presentations of the lambda calculus only consider closed terms. For example, the lambda term representing the identity λx.x has no free variables and is closed.

Notation

For convenience, parentheses can be dropped if the expression is unambiguous. For example, the outermost parentheses can always be dropped—MN instead of (MN). However, not all parentheses can be eliminated. For example,

  1. λx.((λx.x)x) is of form λx.B and is therefore an abstraction, while
  2. (λx.(λx.x))x is of form MN and is therefore an application.

The examples 1 and 2 denote different terms, differing only in where the parentheses are placed. They have different meanings: example 1 is a function definition, while example 2 is a function application. The lambda variable x is a placeholder in both examples.

Here, example 1 defines a function λx.B, where B is (λx.x)x, an anonymous function (λx.x), with input x; while example 2, M N, is M applied to N, where M is the lambda term (λx.(λx.x)) being applied to the input N which is x. Both examples 1 and 2 would evaluate to the identity function λx.x.

To allow further concision in these situations, the following conventions are usually applied:

  • Applications are assumed to be left-associative: MNP may be written instead of ((MN)P)
  • The body of an abstraction extends as far right as possible: λx.MN means λx.(MN) and not (λx.M)N. Said another way, a lambda abstraction has a lower precedence than an application.
  • A sequence of abstractions is contracted: λx.λy.λz.N is abbreviated as λxyz.N
  • When all variables are single-letter, the space in applications may be omitted: MNP instead of M N P.

Sign in to keep reading

2 more chapters are free with a Lably account.

More in Computing and communication