Formal definitions of the Lambda calculus. Lambda calculus is a programming language based on lambda abstraction and function application. Two definitions of the language are given here.
Contents
- Standard definition
- Definition
- Notation
- Free and bound variables
- Reduction
- conversion
- reduction
- Normalization
- Evaluation strategy
- Syntax definition in BNF
- Definition as mathematical formulas
- Semantics
- Canonym Canonical Names
- Map Operators
- Substitution Operator
- Free and Bound Variable Sets
- Normal form
- Weak head normal form
- Derivation of standard from the math definition
- Changes to the substitution operator
- Transformation
- Alpha Conversion
- Beta reduction capture avoiding
- Eta reduction
- References
Standard definition
This formal definition was given by Alonzo Church.
Definition
Lambda expressions are composed of
The set of lambda expressions,
- If
x is a variable, thenx ∈ Λ - If
x is a variable andM ∈ Λ , then( λ x . M ) ∈ Λ - If
M , N ∈ Λ , then( M N ) ∈ Λ
Instances of rule 2 are known as abstractions and instances of rule 3 are known as applications.
Notation
To keep the notation of lambda expressions uncluttered, the following conventions are usually applied.
Free and bound variables
The abstraction operator,
The set of free variables of a lambda expression,
-
FV ( x ) = { x } , wherex is a variable -
FV ( λ x . M ) = FV ( M ) ∖ { x } -
FV ( M N ) = FV ( M ) ∪ FV ( N )
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.
Reduction
The meaning of lambda expressions is defined by how expressions can be reduced.
There are three kinds of reduction:
We also speak of the resulting equivalences: two expressions are β-equivalent, if they can be β-converted into the same expression, and α/η-equivalence are defined similarly.
The term redex, short for reducible expression, refers to subterms that can be reduced by one of the reduction rules. For example,
α-conversion
Alpha-conversion, sometimes known as alpha-renaming, allows bound variable names to be changed. For example, alpha-conversion of λx.x might yield λy.y. Terms that differ only by alpha-conversion are called α-equivalent. Frequently in uses of lambda calculus, α-equivalent terms are considered to be equivalent.
The precise rules for alpha-conversion are not completely trivial. First, when alpha-converting an abstraction, the only variable occurrences that are renamed are those that are bound to the same abstraction. For example, an alpha-conversion of λx.λx.x could result in λy.λx.x, but it could not result in λy.λx.y. The latter has a different meaning from the original.
Second, alpha-conversion is not possible if it would result in a variable getting captured by a different abstraction. For example, if we replace x with y in λx.λy.x, we get λy.λy.y, which is not at all the same.
In programming languages with static scope, alpha-conversion can be used to make name resolution simpler by ensuring that no variable name masks a name in a containing scope (see alpha renaming to make name resolution trivial).
Substitution
Substitution, written E[V := R], is the process of replacing all free occurrences of the variable V in the expression E with expression R. Substitution on terms of the λ-calculus is defined by recursion on the structure of terms, as follows (note: x and y are only variables while M and N are any λ expression).
To substitute into a lambda abstraction, it is sometimes necessary to α-convert the expression. For example, it is not correct for (λx.y)[y := x] to result in (λx.x), because the substituted x was supposed to be free but ended up being bound. The correct substitution in this case is (λz.x), up to α-equivalence. Notice that substitution is defined uniquely up to α-equivalence.
β-reduction
Beta-reduction captures the idea of function application. Beta-reduction is defined in terms of substitution: the beta-reduction of
For example, assuming some encoding of
η-conversion
Eta-conversion expresses the idea of extensionality, which in this context is that two functions are the same if and only if they give the same result for all arguments. Eta-conversion converts between λx.(f x) and f whenever x does not appear free in f.
Normalization
The purpose of beta-reduction is to calculate a value. A value in Lambda Calculus is a function. So beta-reduction continues until the expression looks like a function abstraction.
An lambda expression that cannot be reduced further, by either beta-redex, or eta-redex is in normal form. Note that alpha-conversion may convert functions. All normal forms that can be converted into each other by alpha conversion are defined to be equal. See the main article on Beta normal form for details.
Evaluation strategy
Whether a term is normalising or not, and how much work needs to be done in normalising it if it is, depends to a large extent on the reduction strategy used. The distinction between reduction strategies relates to the distinction in functional programming languages between eager evaluation and lazy evaluation.
Applicative order is not a normalising strategy. The usual counterexample is as follows: define Ω = ωω where ω = λx.xx. This entire expression contains only one redex, namely the whole expression; its reduct is again Ω. Since this is the only available reduction, Ω has no normal form (under any evaluation strategy). Using applicative order, the expression KIΩ = (λx.λy.x) (λx.x)Ω is reduced by first reducing Ω to normal form (since it is the rightmost redex), but since Ω has no normal form, applicative order fails to find a normal form for KIΩ.
In contrast, normal order is so called because it always finds a normalising reduction, if one exists. In the above example, KIΩ reduces under normal order to I, a normal form. A drawback is that redexes in the arguments may be copied, resulting in duplicated computation (for example, (λx.xx) ((λx.x)y) reduces to ((λx.x)y) ((λx.x)y) using this strategy; now there are two redexes, so full evaluation needs two more steps, but if the argument had been reduced first, there would now be none).
The positive tradeoff of using applicative order is that it does not cause unnecessary computation, if all arguments are used, because it never substitutes arguments containing redexes and hence never needs to copy them (which would duplicate work). In the above example, in applicative order (λx.xx) ((λx.x)y) reduces first to (λx.xx)y and then to the normal order yy, taking two steps instead of three.
Most purely functional programming languages (notably Miranda and its descendents, including Haskell), and the proof languages of theorem provers, use lazy evaluation, which is essentially the same as call by need. This is like normal order reduction, but call by need manages to avoid the duplication of work inherent in normal order reduction using sharing. In the example given above, (λx.xx) ((λx.x)y) reduces to ((λx.x)y) ((λx.x)y), which has two redexes, but in call by need they are represented using the same object rather than copied, so when one is reduced the other is too.
Syntax definition in BNF
Lambda Calculus has a simple syntax. A Lambda Calculus program has the syntax of an expression where,
The variable list is defined as,
A variable as used by computer scientists has the syntax,
Mathematicians will sometimes restrict a variable to be a single alphabetic character. When using this convention the comma is omitted from the variable list.
A lambda abstraction has a lower precedence than an application, so;
Applications are left associative;
An abstraction with multiple parameters is equivalent to multiple abstractions of one parameter.
where,
Definition as mathematical formulas
The problem of how variables may be renamed is difficult. This definition avoids the problem by substituting all names with canonical names, which are constructed based on the position of the definition of the name in the expression. The approach is analogous to what a compiler does, but has been adapted to work within the constraints of mathematics.
Semantics
The execution of a lambda expression proceeds using the following reductions and transformations,
- alpha conversion -
a l p h a - c o n ( a ) → canonym [ A , P ] = canonym [ a [ A ] , P ] - beta reduction -
b e t a - r e d e x [ λ p . b v ] = b [ p := v ] - eta reduction -
x ∉ FV ( f ) → e t a - r e d e x [ λ x . ( f x ) ] = f
where,
Execution is performing beta reductions and eta reductions on sub expressions in the canonym of a lambda expression until the result is a lambda function (abstraction) in the normal form.
All alpha conversions of a lambda expression are considered to be equivalent.
Canonym - Canonical Names
Canonym is a function that takes a lambda expression and renames all names canonically, based on their positions in the expression. This might be implemented as,
Where, N is the string "N", F is the string "F", S is the string "S", + is concatenation, and "name" converts a string into a name
Map Operators
Map from one value to another if the value is in the map. O is the empty map.
-
O [ x ] = x -
M [ x := y ] [ x ] = y -
x ≠ z → M [ x := y ] [ z ] = M [ z ]
Substitution Operator
If L is a lambda expression, x is a name, and y is a lambda expression;
-
( λ p . b ) [ x := y ] = λ p . b [ x := y ] -
( X Y ) [ x := y ] = X [ x := y ] Y [ x := y ] -
z = x → ( z ) [ x := y ] = y -
z ≠ x → ( z ) [ x := y ] = z
Note that rule 1 must be modified if it is to be used on non canonically renamed lambda expressions. See Changes to the substitution operator.
Free and Bound Variable Sets
The set of free variables of a lambda expression, M, is denoted as FV(M). This is the set of variable names that have instances not bound (used) in a lambda abstraction, within the lambda expression. They are the variable names that may be bound to formal parameter variables from outside the lambda expression.
The set of bound variables of a lambda expression, M, is denoted as BV(M). This is the set of variable names that have instances bound (used) in a lambda abstraction, within the lambda expression.
The rules for the two sets are given below.
Usage;
Evaluation strategy
This mathematical definition is structured so that it represents the result, and not the way it gets calculated. However the result may be different between lazy and eager evaluation. This difference is described in the evaluation formulas.
The definitions given here assume that the first definition that matches the lambda expression will be used. This convention is used to make the definition more readable. Otherwise some if conditions would be required to make the definition precise.
Running or evaluating a lambda expression L is,
Where Q is a name prefix possibly an empty string.
where eval is defined by,
Then the evaluation strategy may be chosen as either,
The result may be different depending on the strategy used. Eager evaluation will apply all reductions possible, leaving the result in normal form, while lazy evaluation will omit some reductions in parameters, leaving the result in "weak head normal form".
Normal form
All reductions that can be applied have been applied. This is the result obtained from applying eager evaluation.
In all other cases,
Weak head normal form
Reductions to the function (the head) have been applied, but not all reductions to the parameter have been applied. This is the result obtained from applying lazy evaluation.
In all other cases,
Derivation of standard from the math definition
The standard definition of Lambda Calculus uses some definitions which may be considered as theorems, which can be proved based on the definition as mathematical formulas.
The canonical naming definition deals with the problem of variable identity by constructing a unique name for each variable based on the position of the lambda abstraction for the variable name in the expression.
This definition introduces the rules used in the standard definition and relates explains them in terms of the canonical renaming definition.
Free and bound variables
The lambda abstraction operator, λ, takes a formal parameter variable and a body expression. When evaluated the formal parameter variable is identified with the value of the actual parameter.
Variables in a lambda expression may either be "bound" or "free". Bound variables are variable names that are already attached to formal parameter variables in the expression.
The formal parameter variable is said to bind the variable name wherever it occurs free in the body. Variable (names) that have already been matched to formal parameter variable are said to be bound. All other variables in the expression are called free.
For example, in the following expression y is a bound variable and x is free:
Changes to the substitution operator
In the definition of the Substitution Operator the rule,
must be replaced with,
-
( λ x . b ) [ x := y ] = λ x . b -
z ≠ x → ( λ z . b ) [ x := y ] = λ z . b [ x := y ]
This is to stop bound variables with the same name being substituted. This would not have occurred in a canonically renamed lambda expression.
For example the previous rules would have wrongly translated,
The new rules block this substitution so that it remains as,
Transformation
The meaning of lambda expressions is defined by how expressions can be transformed or reduced.
There are three kinds of transformation:
We also speak of the resulting equivalences: two expressions are β-equivalent, if they can be β-converted into the same expression, and α/η-equivalence are defined similarly.
The term redex, short for reducible expression, refers to subterms that can be reduced by one of the reduction rules.
Alpha Conversion
Alpha-conversion, sometimes known as alpha-renaming, allows bound variable names to be changed. For example, alpha-conversion of
In an alpha conversion, names may be substituted for new names if the new name is not free in the body, as this would lead to the capture of free variables.
Note that the substitution will not recurse into the body of lambda expressions with formal parameter
See example;
Beta reduction (capture avoiding)
Beta-reduction captures the idea of function application (also called a function call), and implements the substitution of the actual parameter expression for the formal parameter variable. Beta-reduction is defined in terms of substitution.
If no variable names are free in the actual parameter and bound in the body, beta reduction may be performed on the lambda abstraction without canonical renaming.
Alpha renaming may be used on
See example;
In this example,
- In the beta-redex,
- The free variables are,
FV ( x y ) = { x , y } - The bound variables are,
BV ( ( λ x . z x ) ( λ y . z y ) ) = { x , y }
- The free variables are,
- The naive beta-redex changed the meaning of the expression because x and y from the actual parameter became captured when the expressions were substituted in the inner abstractions.
- The alpha renaming removed the problem by changing the names of x and y in the inner abstraction so that they are distinct from the names of x and y in the actual parameter.
- The free variables are,
FV ( x y ) = { x , y } - The bound variables are,
BV ( ( λ a . z a ) ( λ b . z b ) ) = { a , b }
- The free variables are,
- The beta-redex then proceeded with the intended meaning.
Eta reduction
Eta-conversion expresses the idea of extensionality, which in this context is that two functions are the same if and only if they give the same result for all arguments.
Eta reduction may be used without change on lambda expressions that are not canonically renamed.
The problem with using an eta-redex when f has free variables is shown in this example,
This improper use of eta-reduction changes the meaning by leaving x in