Share to: share facebook share twitter share wa share telegram print page

Lambda calculus definition

The lambda calculus is a formal mathematical system consisting of constructing lambda terms and performing reduction operations on them. The definition of a lambda term is simply a variable, a lambda abstraction, or a function application, but a formal presentation can be somewhat lengthy. The focus of this article is to present a full and complete definition of the lambda calculus, specifically the pure untyped lambda calculus without extensions, although a lambda calculus extended with numbers and arithmetic is used for explanatory purposes.

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: [1]

  1. A variable is a character or string representing a parameter, itself a valid lambda term.
  2. A lambda abstraction is a function definition, taking as input the bound variable (between the λ and the punctum/dot .) and returning the body . 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 binds the variable x in the term M. For example, is an abstraction representing the anonymous function . More concretely, we might give this function the name , and then we could write , although this name is superfluous when using the lambda calculus.
  3. An application represents the application of a function to an argument . Both and are lambda terms. The application represents the act of calling function M on input N to produce .

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

Name BNF Description
Expression
<expression> ::= <abstraction> | <application> | <variable> | <bracket>
A lambda term is either an abstraction, an application, a variable, or a bracketed expression.
Abstraction
<abstraction> ::= λ <variable-list> . <expression>
Anonymous function definition.
Variable list
<variable-list> ::= <variable> , <variable-list> | <variable>
A comma separated list of variables.
Application
<application> ::= <expression> <expression>+
An application (function call) is two or more expressions in a row.
Variable
<variable> ::= <alpha> (<alpha> | <digit> | '_')*
A variable name, e.g. x, y, fact, sum, ...
Grouping
<bracket> ::= ( <expression> )
Expression bracketed with parentheses.

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.[2]

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 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 could be represented as the lambda term , 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 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 in the expression is bound by the second lambda: A variable may occur both free and bound in a term; for example in .

More formally, the sets of free variables and bound variables of a lambda expression, , are denoted as and and can be defined by recursion on the structure of the terms, as follows:[3][4]

- Free Variable Set Comment - Bound Variable Set Comment
where x is a variable. In words, the free variables of are just . where x is a variable
Free variables of M, but with removed Bound variables of M plus x.
Union of the free variables from the function and the parameter Union of the bound variables from the function and the parameter

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 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— instead of . However, not all parentheses can be eliminated. For example,

  1. is of form and is therefore an abstraction, while
  2. is of form 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 , where is , an anonymous function , with input ; while example 2,  , is M applied to N, where is the lambda term being applied to the input which is . Both examples 1 and 2 would evaluate to the identity function .

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

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

Transformation and reduction

The meaning of lambda expressions is defined by how expressions can be transformed and reduced.[9]

There are three kinds of transformation:

  • α-conversion: changing bound variables (alpha);
  • β-reduction: applying functions to their arguments (beta), calling functions;
  • η-reduction: which captures a notion of extensionality (eta).

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, is a β-redex in expressing the substitution of for in ; if is not free in , is an η-redex. The expression to which a redex reduces is called its reduct; using the previous example, the reducts of these expressions are respectively and .

α-conversion

α-conversion (alpha-conversion), sometimes known as α-renaming,[10] allows bound variable names to be changed. For example, alpha-conversion of might yield . The terms and by themselves are not alpha-equivalent, because they are not bound in an abstraction. Terms that differ only by alpha-conversion are called α-equivalent, capturing the intuition that the particular choice of a bound variable, in an abstraction, does not (usually) matter. 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 by the same abstraction. For example, an alpha-conversion of could result in , but it could not result in . The latter has a different meaning from the original. This is analogous to the programming notion of variable shadowing.

Second, alpha-conversion is not possible if it would result in a variable getting captured by a different abstraction. For example, if we replace with in , we get , which is not at all the same. In the De Bruijn index notation, any two α-equivalent terms are syntactically identical, and confusion in this way cannot occur.

See example:

α-conversion λ-expression de Brujin notation Comment
Original expressions.
correctly rename y to k, (because k is not used in the body) No change to de Brujin expression.
naively rename y to z, (wrong because z free in ) is captured.

Substitution

Substitution, written , is the process of replacing all free occurrences of the variable in the expression with expression . Substitution on terms of the lambda calculus is defined by recursion on the structure of terms, as follows (note: x and y are variables while M and N are any λ expression).

  •  ; with substituted for , becomes
  • if  ; with substituted for , (which is not ) remains
  •  ; substitution distributes to both sides of an application
  •  ; a variable bound by an abstraction is not subject to substitution; substituting such a variable leaves the abstraction unchanged
  • if and ; substituting a variable which is not bound by an abstraction proceeds in the abstraction's body, provided that the abstracted variable is "fresh" for the substitution term , meaning it does not appear among the free variables of

For example, , and .

The freshness condition (requiring that is not in the free variables of ) is crucial in order to ensure that substitution does not change the meaning of functions. The situation where the substituted was supposed to be free but ended up being bound is a situation known as capturing . To substitute into a lambda abstraction, it is sometimes necessary to α-convert the expression. For example, this substitution is erroneous because it would turn the constant function into the identity . The correct substitution is to rename the bound variable using α-equivalence, in this case .

In general, failure to meet the freshness condition can be remedied by alpha-renaming first, with a suitable fresh variable. Substitution is defined uniquely up to α-equivalence. Most implementations of substitution use alpha-conversion automatically to avoid capture during substitution, an operation called capture-avoiding substitution. In programming languages with static scope, capture-avoiding substitution can be used to implement name resolution by carefully handling variable shadowing in containing scopes. Another strategy to require alpha renaming in the source program to make name resolution trivial. If De Bruijn indexing is used, then α-conversion is no longer required as there will be no name collisions. Similarly variable names are not needed if using a universal lambda function, such as Iota and Jot, which can create any function behavior by calling it on itself in various combinations.

β-reduction

β-reduction (beta reduction) captures the idea of function application. β-reduction is defined in terms of substitution: the β-reduction of is . The β-reduction rule states that an application of the form reduces to the term . The notation is used to indicate that β-reduces to . β-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. β-reduction is defined in terms of substitution. β-reduction can be seen to be the same as the concept of local reducibility in natural deduction, via the Curry–Howard isomorphism.

For example, for every , . This demonstrates that really is the identity. Similarly, , which demonstrates that is a constant function. Assuming some encoding of , we have the following β-reduction: .

More formally, β-reduction may be performed on the lambda abstraction without alpha renaming only if no variable names are free in the actual parameter and bound in the body:[a]

Alpha renaming may be used on to rename names that are free in but bound in , to meet the pre-condition for this transformation. See example:

β-reduction λ-expression de Brujin notation Comment
Original expressions.
Naive beta 1,
Correct
Incorrect
x and y have been captured in the substitution.
Alpha rename inner, x → a, y → b
Beta 2, x and y not captured.

Looking closer, the key substitutions are

In this example,

  1. In the β-redex,
    1. The free variables are,
    2. The bound variables are,
  2. The naive β-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.
  3. 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.
    1. The free variables are,
    2. The bound variables are,
  4. The β-redex then proceeded with the intended meaning.

η-reduction

η-reduction (eta reduction) converts from to , given that does not appear free in . The problem with using an η-redex when f has free variables is shown in this example,

Reduction Lambda expression β-reduction
Naive η-reduction

This improper use of η-reduction changes the meaning by leaving x in unsubstituted.

η-reduction is often paired with its inverse, η-expansion, which converts from to , again given that does not appear free in . The two processes together are called η-conversion. η-conversion expresses the idea of extensionality,[12] which in this context is that two functions are the same if and only if they give the same result for all arguments. η-conversion can be seen to be the same as the concept of local completeness in natural deduction, via the Curry–Howard isomorphism.

Evaluation and normalization

The lambda calculus may be seen as an idealized version of a functional programming language, like Haskell or Standard ML. Under this view, β-reduction corresponds to a computational step. This step can be repeated by additional β-reductions until there are no more applications left to reduce. If evaluation does terminate, the result is a lambda expression that cannot be reduced further by β-reduction. This is called the normal form of the expression. A lambda expression that cannot be reduced further by β-reductions is in beta normal form, and similarly if it also cannot be reduced by η-reductions it is in beta-eta normal form. All normal forms that can be converted into each other by α-conversion are defined to be equal. By the Church–Rosser theorem, the normal form is unique if it exists, regardless of the order in which the reductions are performed (the reduction strategy).

However, not all lambda expressions have a normal form. For example, consider the term . Here . That is, the term reduces to itself in a single β-reduction, and therefore the reduction process will never terminate. Typed lambda calculi, such as the simply typed lambda calculus, do not allow the construction of terms like , and therefore all well-typed terms in these systems have a normal form.

Notes

  1. ^ Barendregt, Barendsen (2000) call this form
    • axiom β: (λx.M[x]) N = M[N] , rewritten as (λx.M) N = M[x := N], "where M[x := N] denotes the substitution of N for every occurrence of x in M".[3]: 7  Also denoted M[N/x], "the substitution of N for x in M".[11]

References

  1. ^ Barendregt, Hendrik Pieter (1984), The Lambda Calculus: Its Syntax and Semantics, Studies in Logic and the Foundations of Mathematics, vol. 103 (Revised ed.), North Holland, Amsterdam., ISBN 978-0-444-87508-2, archived from the original on 2004-08-23Corrections
  2. ^ Barendregt, Hendrik Pieter (1984). The Lambda Calculus: Its Syntax and Semantics. Studies in Logic and the Foundations of Mathematics. Vol. 103 (Revised ed.). North Holland. ISBN 0-444-87508-5. (Corrections).
  3. ^ a b Barendregt, Henk; Barendsen, Erik (March 2000), Introduction to Lambda Calculus (PDF)
  4. ^ Barendregt, Henk (1985). The Lambda Calculus – Its Syntax and Semantics. Studies in Logic and the Foundations of Mathematics. Vol. 103. Amsterdam: North-Holland. ISBN 0444867481. Here: Def.2.1.6, p.24
  5. ^ a b "Example for Rules of Associativity". Lambda-bound.com. Retrieved 2012-06-18.
  6. ^ Selinger, Peter (2008), Lecture Notes on the Lambda Calculus (PDF), vol. 0804, Department of Mathematics and Statistics, University of Ottawa, p. 9, arXiv:0804.3434, Bibcode:2008arXiv0804.3434S
  7. ^ "Example for Rule of Associativity". Lambda-bound.com. Retrieved 2012-06-18.
  8. ^ "The Basic Grammar of Lambda Expressions". SoftOption. Some other systems use juxtaposition to mean application, so 'ab' means 'a@b'. This is fine except that it requires that variables have length one so that we know that 'ab' is two variables juxtaposed not one variable of length 2. But we want to labels like 'firstVariable' to mean a single variable, so we cannot use this juxtaposition convention.
  9. ^ de Queiroz, Ruy J. G. B. (1988). "A Proof-Theoretic Account of Programming and the Role of Reduction Rules". Dialectica. 42 (4): 265–282. doi:10.1111/j.1746-8361.1988.tb00919.x.
  10. ^ Turbak, Franklyn; Gifford, David (2008), Design concepts in programming languages, MIT press, p. 251, ISBN 978-0-262-20175-9
  11. ^ explicit substitution at the nLab
  12. ^ Luke Palmer (29 Dec 2010) Haskell-cafe: What's the motivation for η rules?
Prefix: a b c d e f g h i j k l m n o p q r s t u v w x y z 0 1 2 3 4 5 6 7 8 9

Portal di Ensiklopedia Dunia

Kembali kehalaman sebelumnya