HOME

TheInfoList



OR:

Lambda calculus (also written as ''λ''-calculus) is a
formal system A formal system is an abstract structure used for inferring theorems from axioms according to a set of rules. These rules, which are used for carrying out the inference of theorems from axioms, are the logical calculus of the formal system. A for ...
in
mathematical logic Mathematical logic is the study of formal logic within mathematics. Major subareas include model theory, proof theory, set theory, and recursion theory. Research in mathematical logic commonly addresses the mathematical properties of formal ...
for expressing
computation Computation is any type of arithmetic or non-arithmetic calculation that follows a well-defined model (e.g., an algorithm). Mechanical or electronic devices (or, historically, people) that perform computations are known as '' computers''. An esp ...
based on function
abstraction Abstraction in its main sense is a conceptual process wherein general rules and concepts are derived from the usage and classification of specific examples, literal ("real" or " concrete") signifiers, first principles, or other methods. "An a ...
and application using variable binding and substitution. It is a universal
model of computation In computer science, and more specifically in computability theory and computational complexity theory, a model of computation is a model which describes how an output of a mathematical function is computed given an input. A model describes h ...
that can be used to simulate any
Turing machine A Turing machine is a mathematical model of computation describing an abstract machine that manipulates symbols on a strip of tape according to a table of rules. Despite the model's simplicity, it is capable of implementing any computer algor ...
. It was introduced by the mathematician
Alonzo Church Alonzo Church (June 14, 1903 – August 11, 1995) was an American mathematician, computer scientist, logician, philosopher, professor and editor who made major contributions to mathematical logic and the foundations of theoretical computer scie ...
in the 1930s as part of his research into the
foundations of mathematics Foundations of mathematics is the study of the philosophical and logical and/or algorithmic basis of mathematics, or, in a broader sense, the mathematical investigation of what underlies the philosophical theories concerning the nature of mathe ...
. Lambda calculus consists of constructing § lambda terms and performing § reduction operations on them. In the simplest form of lambda calculus, terms are built using only the following rules: * x – variable, a character or string representing a parameter or mathematical/logical value. * (\lambda x.M) – abstraction, function definition (M is a lambda term). The variable x becomes bound in the expression. * (M\ N) – application, applying a function M to an argument N. M and N are lambda terms. The reduction operations include: * (\lambda x.M \rightarrow(\lambda y.M – α-conversion, renaming the bound variables in the expression. Used to avoid name collisions. * ((\lambda x.M)\ E)\rightarrow (M :=E – β-reduction, replacing the bound variables with the argument expression in the body of the abstraction. If
De Bruijn index In mathematical logic, the De Bruijn index is a tool invented by the Dutch mathematician Nicolaas Govert de Bruijn for representing terms of lambda calculus without naming the bound variables. Terms written using these indices are invariant wi ...
ing is used, then α-conversion is no longer required as there will be no name collisions. If repeated application of the reduction steps eventually terminates, then by the
Church–Rosser theorem In lambda calculus, the Church–Rosser theorem states that, when applying reduction rules to terms, the ordering in which the reductions are chosen does not make a difference to the eventual result. More precisely, if there are two distinct r ...
it will produce a β-normal form. Variable names are not needed if using a universal lambda function, such as
Iota and Jot In formal language theory and computer science, Iota and Jot (from Greek iota ι, Hebrew yodh י, the smallest letters in those two alphabets) are languages, extremely minimalist formal systems, designed to be even simpler than other more p ...
, which can create any function behavior by calling it on itself in various combinations.


Explanation and applications

Lambda calculus is
Turing complete Alan Mathison Turing (; 23 June 1912 – 7 June 1954) was an English mathematician, computer scientist, logician, cryptanalyst, philosopher, and theoretical biologist. Turing was highly influential in the development of theoretical co ...
, that is, it is a universal
model of computation In computer science, and more specifically in computability theory and computational complexity theory, a model of computation is a model which describes how an output of a mathematical function is computed given an input. A model describes h ...
that can be used to simulate any
Turing machine A Turing machine is a mathematical model of computation describing an abstract machine that manipulates symbols on a strip of tape according to a table of rules. Despite the model's simplicity, it is capable of implementing any computer algor ...
. Its namesake, the Greek letter lambda (λ), is used in lambda expressions and lambda terms to denote binding a variable in a function. Lambda calculus may be ''untyped'' or ''typed''. In typed lambda calculus, functions can be applied only if they are capable of accepting the given input's "type" of data. Typed lambda calculi are ''weaker'' than the untyped lambda calculus, which is the primary subject of this article, in the sense that ''typed lambda calculi can express less'' than the untyped calculus can, but on the other hand typed lambda calculi allow more things to be proven; in the
simply typed lambda calculus The simply typed lambda calculus (\lambda^\to), a form of type theory, is a typed interpretation of the lambda calculus with only one type constructor (\to) that builds function types. It is the canonical and simplest example of a typed lambda ...
it is, for example, a theorem that every evaluation strategy terminates for every simply typed lambda-term, whereas evaluation of untyped lambda-terms §need not terminate. One reason there are many different typed lambda calculi has been the desire to do more (of what the untyped calculus can do) without giving up on being able to prove strong theorems about the calculus. Lambda calculus has applications in many different areas in mathematics, philosophy,
linguistics Linguistics is the scientific study of human language. It is called a scientific study because it entails a comprehensive, systematic, objective, and precise analysis of all aspects of language, particularly its nature and structure. Lingu ...
, and
computer science Computer science is the study of computation, automation, and information. Computer science spans theoretical disciplines (such as algorithms, theory of computation, information theory, and automation) to practical disciplines (includin ...
. Lambda calculus has played an important role in the development of the theory of programming languages.
Functional programming language In computer science, functional programming is a programming paradigm where programs are constructed by applying and composing functions. It is a declarative programming paradigm in which function definitions are trees of expressions that ...
s implement lambda calculus. Lambda calculus is also a current research topic in category theory.


History

The lambda calculus was introduced by mathematician
Alonzo Church Alonzo Church (June 14, 1903 – August 11, 1995) was an American mathematician, computer scientist, logician, philosopher, professor and editor who made major contributions to mathematical logic and the foundations of theoretical computer scie ...
in the 1930s as part of an investigation into the
foundations of mathematics Foundations of mathematics is the study of the philosophical and logical and/or algorithmic basis of mathematics, or, in a broader sense, the mathematical investigation of what underlies the philosophical theories concerning the nature of mathe ...
. The original system was shown to be logically inconsistent in 1935 when Stephen Kleene and J. B. Rosser developed the
Kleene–Rosser paradox In mathematics, the Kleene–Rosser paradox is a paradox that shows that certain systems of formal logic are inconsistent, in particular the version of Haskell Curry's combinatory logic introduced in 1930, and Alonzo Church's original lambda ...
. Subsequently, in 1936 Church isolated and published just the portion relevant to computation, what is now called the untyped lambda calculus. In 1940, he also introduced a computationally weaker, but logically consistent system, known as the
simply typed lambda calculus The simply typed lambda calculus (\lambda^\to), a form of type theory, is a typed interpretation of the lambda calculus with only one type constructor (\to) that builds function types. It is the canonical and simplest example of a typed lambda ...
. Until the 1960s when its relation to programming languages was clarified, the lambda calculus was only a formalism. Thanks to
Richard Montague Richard Merritt Montague (September 20, 1930 – March 7, 1971) was an American mathematician and philosopher who made contributions to mathematical logic and the philosophy of language. He is known for proposing Montague grammar to formalize t ...
and other linguists' applications in the semantics of natural language, the lambda calculus has begun to enjoy a respectable place in both linguistics and computer science.


Origin of the lambda symbol

There is some uncertainty over the reason for Church's use of the Greek letter
lambda Lambda (}, ''lám(b)da'') is the 11th letter of the Greek alphabet, representing the voiced alveolar lateral approximant . In the system of Greek numerals, lambda has a value of 30. Lambda is derived from the Phoenician Lamed . Lambda gave ris ...
(λ) as the notation for function-abstraction in the lambda calculus, perhaps in part due to conflicting explanations by Church himself. According to Cardone and Hindley (2006):
By the way, why did Church choose the notation “λ”? In n unpublished 1964 letter to Harald Dicksonhe stated clearly that it came from the notation “\hat” used for class-abstraction by Whitehead and Russell, by first modifying “\hat” to “\land x” to distinguish function-abstraction from class-abstraction, and then changing “\land” to “λ” for ease of printing. This origin was also reported in osser, 1984, p.338 On the other hand, in his later years Church told two enquirers that the choice was more accidental: a symbol was needed and λ just happened to be chosen.
Dana Scott Dana Stewart Scott (born October 11, 1932) is an American logician who is the emeritus Hillman University Professor of Computer Science, Philosophy, and Mathematical Logic at Carnegie Mellon University; he is now retired and lives in Berkeley, C ...
has also addressed this question in various public lectures. Scott recounts that he once posed a question about the origin of the lambda symbol to Church's former student and son-in-law John W. Addison Jr., who then wrote his father-in-law a postcard:
Dear Professor Church, Russell had the iota operator, Hilbert had the epsilon operator. Why did you choose lambda for your operator?
According to Scott, Church's entire response consisted of returning the postcard with the following annotation: "
eeny, meeny, miny, moe "Eeny, meeny, miny, moe"—which can be spelled a number of ways—is a children's counting-out rhyme, used to select a person in games such as tag, or for selecting various other things. It is one of a large group of similar rhymes in which the ...
".


Informal description


Motivation

Computable function Computable functions are the basic objects of study in computability theory. Computable functions are the formalized analogue of the intuitive notion of algorithms, in the sense that a function is computable if there exists an algorithm that can d ...
s are a fundamental concept within computer science and mathematics. The lambda calculus provides simple
semantics Semantics (from grc, σημαντικός ''sēmantikós'', "significant") is the study of reference, meaning, or truth. The term can be used to refer to subfields of several distinct disciplines, including philosophy, linguistics and compu ...
for computation which are useful for formally studying properties of computation. The lambda calculus incorporates two simplifications that make its semantics simple. The first simplification is that the lambda calculus treats functions "anonymously;" it does not give them explicit names. For example, the function : \operatorname(x, y) = x^2 + y^2 can be rewritten in ''anonymous form'' as : (x, y) \mapsto x^2 + y^2 (which is read as "a
tuple In mathematics, a tuple is a finite ordered list (sequence) of elements. An -tuple is a sequence (or ordered list) of elements, where is a non-negative integer. There is only one 0-tuple, referred to as ''the empty tuple''. An -tuple is defi ...
of and is mapped to x^2 + y^2"). Similarly, the function : \operatorname(x) = x can be rewritten in anonymous form as : x \mapsto x where the input is simply mapped to itself. The second simplification is that the lambda calculus only uses functions of a single input. An ordinary function that requires two inputs, for instance the \operatorname function, can be reworked into an equivalent function that accepts a single input, and as output returns ''another'' function, that in turn accepts a single input. For example, : (x, y) \mapsto x^2 + y^2 can be reworked into : x \mapsto (y \mapsto x^2 + y^2) This method, known as
currying In mathematics and computer science, currying is the technique of translating the evaluation of a function that takes multiple arguments into evaluating a sequence of functions, each with a single argument. For example, currying a function f tha ...
, transforms a function that takes multiple arguments into a chain of functions each with a single argument.
Function application In mathematics, function application is the act of applying a function to an argument from its domain so as to obtain the corresponding value from its range. In this sense, function application can be thought of as the opposite of function abs ...
of the \operatorname function to the arguments (5, 2), yields at once : ((x, y) \mapsto x^2 + y^2)(5, 2) : = 5^2 + 2^2 : = 29, whereas evaluation of the curried version requires one more step : \Bigl(\bigl(x \mapsto (y \mapsto x^2 + y^2)\bigr)(5)\Bigr)(2) : = (y \mapsto 5^2 + y^2)(2) // the definition of x has been used with 5 in the inner expression. This is like β-reduction. : = 5^2 + 2^2 // the definition of y has been used with 2. Again, similar to β-reduction. : = 29 to arrive at the same result.


The lambda calculus

The lambda calculus consists of a language of lambda terms, that are defined by a certain formal syntax, and a set of transformation rules for manipulating the lambda terms. These transformation rules can be viewed as an
equational theory Universal algebra (sometimes called general algebra) is the field of mathematics that studies algebraic structures themselves, not examples ("models") of algebraic structures. For instance, rather than take particular groups as the object of stud ...
or as an
operational definition An operational definition specifies concrete, replicable procedures designed to represent a construct. In the words of American psychologist S.S. Stevens (1935), "An operation is the performance which we execute in order to make known a concept." F ...
. As described above, having no names, all functions in the lambda calculus are anonymous functions. They only accept one input variable, so
currying In mathematics and computer science, currying is the technique of translating the evaluation of a function that takes multiple arguments into evaluating a sequence of functions, each with a single argument. For example, currying a function f tha ...
is used to implement functions of several variables.


Lambda terms

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 C programs and some are not. A valid lambda calculus expression is called a "lambda term". The following three rules give an
inductive definition In mathematics and computer science, a recursive definition, or inductive definition, is used to define the elements in a set in terms of other elements in the set ( Aczel 1977:740ff). Some examples of recursively-definable objects include fact ...
that can be applied to build all syntactically valid lambda terms: * variable is itself a valid lambda term. *if is a lambda term, and is a variable, then (\lambda x.t) is a lambda term (called an abstraction); *if and are lambda terms, then (t   s) is a lambda term (called an application). Nothing else is a lambda term. Thus a lambda term is valid if and only if it can be obtained by repeated application of these three rules. However, some parentheses can be omitted according to certain rules. For example, the outermost parentheses are usually not written. See ''
Notation In linguistics and semiotics, a notation is a system of graphics or symbols, characters and abbreviated expressions, used (for example) in artistic and scientific disciplines to represent technical facts and quantities by convention. Therefore, ...
'', below. An abstraction \lambda x.t denotes an § anonymous function that takes a single input and returns . For example, \lambda x.x^2+2 is an abstraction for the function f(x) = x^2 + 2 using the term x^2+2 for . The name f(x) is superfluous when using abstraction. (\lambda x.t) binds the variable in the term . The definition of a function with an abstraction merely "sets up" the function but does not invoke it. An application t   s represents the application of a function to an input , that is, it represents the act of calling function on input to produce t(s). There is no concept in lambda calculus of variable declaration. In a definition such as \lambda x.x+y (i.e. f(x) = x + y), in lambda calculus is a variable that is not yet defined. The abstraction \lambda x.x+y is syntactically valid, and represents a function that adds its input to the yet-unknown . Parentheses may be used and may be needed to disambiguate terms. For example, #\lambda x.((\lambda x.x)x) which is of form \lambda x.B —an abstraction, and #((\lambda x.x)x) which is of form M N —an application. The examples 1 and 2 denote different terms; however example 1 is a function definition, while example 2 is an application. Here, example 1 defines a function \lambda x.B, where B is ((\lambda x.x)x), the result of applying (\lambda x.x) to x, while example 2 is M N; M is the lambda term (\lambda x.x) to be applied to the input N. Both examples 1 and 2 would evaluate to the
identity function Graph of the identity function on the real numbers In mathematics, an identity function, also called an identity relation, identity map or identity transformation, is a function that always returns the value that was used as its argument, unc ...
\lambda x.x.


Functions that operate on functions

In lambda calculus, functions are taken to be ' first class values', so functions may be used as the inputs, or be returned as outputs from other functions. For example, \lambda x.x represents the
identity function Graph of the identity function on the real numbers In mathematics, an identity function, also called an identity relation, identity map or identity transformation, is a function that always returns the value that was used as its argument, unc ...
, x \mapsto x, and (\lambda x.x)y represents the identity function applied to y. Further, (\lambda x.y) represents the constant function x \mapsto y, the function that always returns y, no matter the input. In lambda calculus, function application is regarded as left-associative, so that stx means (st)x. There are several notions of "equivalence" and "reduction" that allow lambda terms to be "reduced" to "equivalent" lambda terms.


Alpha equivalence

A basic form of equivalence, definable on lambda terms, is alpha equivalence. It captures the intuition that the particular choice of a bound variable, in an abstraction, does not (usually) matter. For instance, \lambda x.x and \lambda y.y are alpha-equivalent lambda terms, and they both represent the same function (the identity function). The terms x and y are not alpha-equivalent, because they are not bound in an abstraction. In many presentations, it is usual to identify alpha-equivalent lambda terms. The following definitions are necessary in order to be able to define β-reduction:


Free variables

The free variables of a term are those variables not bound by an abstraction. The set of free variables of an expression is defined inductively: * The free variables of x are just x * The set of free variables of \lambda x.t is the set of free variables of t, but with x removed * The set of free variables of t s is the union of the set of free variables of t and the set of free variables of s. For example, the lambda term representing the identity \lambda x.x has no free variables, but the function \lambda x. y x has a single free variable, y.


Capture-avoiding substitutions

A systematic change in variables to avoid capture of a free variable can introduce error, in a functional programming language where functions are first class citizens. Suppose t, s and r are lambda terms and x and y are variables. The notation t := r/math> indicates substitution of r for x in t in a ''capture-avoiding'' manner. This is defined so that: * x := r= r; x substituted for r is just r * y := r= y if x \neq y; x substituted for r when dealing with y is just y * (t s) := r= (t := r(s := r; substitution distributes to further application of the variable * (\lambda x.t) := r= \lambda x.t; although x has been mapped to r, subsequently mapping all x to t will not change the lambda function (\lambda x.t) * (\lambda y.t) := r= \lambda y.(t := r if x \neq y and y is not in the free variables of r. The variable y is said to be "fresh" for r. For example, (\lambda x.x) := y= \lambda x.(x := y = \lambda x.x, and ((\lambda x.y)x) := y= ((\lambda x.y) := y(x := y = (\lambda x.y)y. The freshness condition (requiring that y is not in the free variables of r) is crucial in order to ensure that substitution does not change the meaning of functions. For example, a substitution that ignores the freshness condition can lead to errors: (\lambda x.y) := x= \lambda x.(y := x = \lambda x.x. This substitution turns the constant function \lambda x.y into the identity \lambda x.x by substitution. In general, failure to meet the freshness condition can be remedied by alpha-renaming with a suitable fresh variable. For example, switching back to our correct notion of substitution, in (\lambda x.y) := x/math> the abstraction can be renamed with a fresh variable z, to obtain (\lambda z.y) := x= \lambda z.(y := x = \lambda z.x, and the meaning of the function is preserved by substitution.


β-reduction

The β-reduction rule states that an application of the form ( \lambda x . t) s reduces to the term t x := s/math>. The notation ( \lambda x . t ) s \to t x := s is used to indicate that ( \lambda x .t ) s β-reduces to t x := s . For example, for every s, ( \lambda x . x ) s \to x x := s = s . This demonstrates that \lambda x . x really is the identity. Similarly, ( \lambda x . y ) s \to y x := s = y , which demonstrates that \lambda x . y is a constant function. The lambda calculus may be seen as an idealized version of a functional programming language, like Haskell or
Standard ML Standard ML (SML) is a general-purpose, modular, functional programming language with compile-time type checking and type inference. It is popular among compiler writers and programming language researchers, as well as in the development of ...
. 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. In the untyped lambda calculus, as presented here, this reduction process may not terminate. For instance, consider the term \Omega = (\lambda x . xx)( \lambda x . xx ). Here ( \lambda x . xx)( \lambda x . xx) \to ( xx ) x := \lambda x . xx = ( x x := \lambda x . xx )( x x := \lambda x . xx ) = ( \lambda x . xx)( \lambda x . xx ). That is, the term reduces to itself in a single β-reduction, and therefore the reduction process will never terminate. Another aspect of the untyped lambda calculus is that it does not distinguish between different kinds of data. For instance, it may be desirable to write a function that only operates on numbers. However, in the untyped lambda calculus, there is no way to prevent a function from being applied to
truth value In logic and mathematics, a truth value, sometimes called a logical value, is a value indicating the relation of a proposition to truth, which in classical logic has only two possible values ('' true'' or ''false''). Computing In some prog ...
s, strings, or other non-number objects.


Formal definition


Definition

Lambda expressions are composed of: * variables ''v''1, ''v''2, ...; * the abstraction symbols λ (lambda) and . (dot); * parentheses (). The set of lambda expressions, , can be defined inductively: # If ''x'' is a variable, then # If ''x'' is a variable and then # If then 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: * Outermost parentheses are dropped: ''M'' ''N'' instead of (''M'' ''N''). * Applications are assumed to be left associative: ''M'' ''N'' ''P'' may be written instead of ((''M'' ''N'') ''P''). * When all variables are single-letter, the space in applications may be omitted: ''MNP'' instead of ''M'' ''N'' ''P''. * The body of an abstraction extends as far right as possible: λ''x''.''M N'' means λ''x''.(''M N'') and not (λ''x''.''M'') ''N''. * A sequence of abstractions is contracted: λ''x''.λ''y''.λ''z''.''N'' is abbreviated as λ''xyz''.''N''.


Free 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''. In an expression λ''x''.''M'', the part λ''x'' is often called ''binder'', as a hint that the variable ''x'' is getting bound by appending λ''x'' to ''M''. All other variables are called ''free''. For example, in the expression λ''y''.''x x y'', ''y'' is a bound variable and ''x'' is a free variable. Also 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''.''z x''). The set of ''free variables'' of a lambda expression, ''M'', is denoted as FV(''M'') and is defined by recursion on the structure of the terms, as follows: # FV(''x'') = , where ''x'' is a variable. # FV(λ''x''.''M'') = FV(''M'') \ . # 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 Combinatory logic is a notation to eliminate the need for quantified variables in mathematical logic. It was introduced by Moses Schönfinkel and Haskell Curry, and has more recently been used in computer science as a theoretical model of compu ...
.


Reduction

The meaning of lambda expressions is defined by how expressions can be reduced. There are three kinds of reduction: * α-conversion: changing bound variables; * β-reduction: applying functions to their arguments; * η-reduction: which captures a notion of extensionality. We also speak of the resulting equivalences: two expressions are ''α-equivalent'', if they can be α-converted into the same expression. β-equivalence 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, (λ''x''.''M'') ''N'' is a β-redex in expressing the substitution of ''N'' for ''x'' in ''M''. The expression to which a redex reduces is called its ''reduct''; the reduct of (λ''x''.''M'') ''N'' is ''M'' 'x'' := ''N'' If ''x'' is not free in ''M'', λ''x''.''M x'' is also an η-redex, with a reduct of ''M''.


α-conversion

α-conversion, sometimes known as α-renaming, allows bound variable names to be changed. For example, α-conversion of λ''x''.''x'' might yield λ''y''.''y''. Terms that differ only by α-conversion are called ''α-equivalent''. Frequently, in uses of lambda calculus, α-equivalent terms are considered to be equivalent. The precise rules for α-conversion are not completely trivial. First, when α-converting an abstraction, the only variable occurrences that are renamed are those that are bound to the same abstraction. For example, an α-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. This is analogous to the programming notion of variable shadowing. Second, α-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, α-conversion can be used to make name resolution simpler by ensuring that no variable name
masks A mask is an object normally worn on the face, typically for protection, disguise, performance, or entertainment and often they have been employed for rituals and rights. Masks have been used since antiquity for both ceremonial and practic ...
a name in a containing
scope Scope or scopes may refer to: People with the surname * Jamie Scope (born 1986), English footballer * John T. Scopes (1900–1970), central figure in the Scopes Trial regarding the teaching of evolution Arts, media, and entertainment * CinemaS ...
(see α-renaming to make name resolution trivial). In the
De Bruijn index In mathematical logic, the De Bruijn index is a tool invented by the Dutch mathematician Nicolaas Govert de Bruijn for representing terms of lambda calculus without naming the bound variables. Terms written using these indices are invariant wi ...
notation, any two α-equivalent terms are syntactically identical.


Substitution

Substitution, written ''M'' 'x'' := ''N'' is the process of replacing all ''free'' occurrences of the variable ''x'' in the expression ''M'' with expression ''N''. Substitution on terms of the lambda 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 lambda expression): : ''x'' 'x'' := ''N''= ''N'' : ''y'' 'x'' := ''N''= ''y'', if ''x'' ≠ ''y'' : (''M''1 ''M''2) 'x'' := ''N''= ''M''1 'x'' := ''N''''M''2 'x'' := ''N'': (λ''x''.''M'') 'x'' := ''N''= λ''x''.''M'' : (λ''y''.''M'') 'x'' := ''N''= λ''y''.(''M'' 'x'' := ''N'', if ''x'' ≠ ''y'' and ''y'' ∉ FV(''N'') ''See above for the FV'' To substitute into an 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 Two mathematical objects ''a'' and ''b'' are called equal up to an equivalence relation ''R'' * if ''a'' and ''b'' are related by ''R'', that is, * if ''aRb'' holds, that is, * if the equivalence classes of ''a'' and ''b'' with respect to ''R'' a ...
α-equivalence. Substitution is defined uniquely up to α-equivalence.


β-reduction

β-reduction captures the idea of function application. β-reduction is defined in terms of substitution: the β-reduction of (λ''x''.''M'') ''N'' is ''M'' 'x'' := ''N'' For example, assuming some encoding of 2, 7, ×, we have the following β-reduction: (λ''n''.''n'' × 2) 7 → 7 × 2. β-reduction can be seen to be the same as the concept of ''local reducibility'' in
natural deduction In logic and proof theory, natural deduction is a kind of proof calculus in which logical reasoning is expressed by inference rules closely related to the "natural" way of reasoning. This contrasts with Hilbert-style systems, which instead use a ...
, via the Curry–Howard isomorphism.


η-reduction

η-reduction (eta reduction) expresses the idea of
extensionality In logic, extensionality, or extensional equality, refers to principles that judge objects to be equal if they have the same external properties. It stands in contrast to the concept of intensionality, which is concerned with whether the internal ...
,Luke Palme
(29 Dec 2010) Haskell-cafe: What's the motivation for η rules?
/ref> which in this context is that two functions are the same
if and only if In logic and related fields such as mathematics and philosophy, "if and only if" (shortened as "iff") is a biconditional logical connective between statements, where either both statements are true or both are false. The connective is bi ...
they give the same result for all arguments. η-reduction converts between λ''x''.''f'' ''x'' and ''f'' whenever ''x'' does not appear free in ''f''. η-reduction can be seen to be the same as the concept of ''local completeness'' in
natural deduction In logic and proof theory, natural deduction is a kind of proof calculus in which logical reasoning is expressed by inference rules closely related to the "natural" way of reasoning. This contrasts with Hilbert-style systems, which instead use a ...
, via the Curry–Howard isomorphism.


Normal forms and confluence

For the untyped lambda calculus, β-reduction as a rewriting rule is neither strongly normalising nor weakly normalising. However, it can be shown that β-reduction is confluent when working up to α-conversion (i.e. we consider two normal forms to be equal if it is possible to α-convert one into the other). Therefore, both strongly normalising terms and weakly normalising terms have a unique normal form. For strongly normalising terms, any reduction strategy is guaranteed to yield the normal form, whereas for weakly normalising terms, some reduction strategies may fail to find it.


Encoding datatypes

The basic lambda calculus may be used to model booleans,
arithmetic Arithmetic () is an elementary part of mathematics that consists of the study of the properties of the traditional operations on numbers—addition, subtraction, multiplication, division, exponentiation, and extraction of roots. In the 19th c ...
, data structures and recursion, as illustrated in the following sub-sections.


Arithmetic in lambda calculus

There are several possible ways to define the
natural number In mathematics, the natural numbers are those numbers used for counting (as in "there are ''six'' coins on the table") and ordering (as in "this is the ''third'' largest city in the country"). Numbers used for counting are called '' cardinal ...
s in lambda calculus, but by far the most common are the Church numerals, which can be defined as follows: : : : : and so on. Or using the alternative syntax presented above in ''
Notation In linguistics and semiotics, a notation is a system of graphics or symbols, characters and abbreviated expressions, used (for example) in artistic and scientific disciplines to represent technical facts and quantities by convention. Therefore, ...
'': : : : : A Church numeral is a
higher-order function In mathematics and computer science, a higher-order function (HOF) is a function that does at least one of the following: * takes one or more functions as arguments (i.e. a procedural parameter, which is a parameter of a procedure that is itse ...
—it takes a single-argument function , and returns another single-argument function. The Church numeral is a function that takes a function as argument and returns the -th composition of , i.e. the function composed with itself times. This is denoted and is in fact the -th power of (considered as an operator); is defined to be the identity function. Such repeated compositions (of a single function ) obey the laws of exponents, which is why these numerals can be used for arithmetic. (In Church's original lambda calculus, the formal parameter of a lambda expression was required to occur at least once in the function body, which made the above definition of impossible.) One way of thinking about the Church numeral , which is often useful when analysing programs, is as an instruction 'repeat ''n'' times'. For example, using the and functions defined below, one can define a function that constructs a (linked) list of ''n'' elements all equal to ''x'' by repeating 'prepend another ''x'' element' ''n'' times, starting from an empty list. The lambda term is : By varying what is being repeated, and varying what argument that function being repeated is applied to, a great many different effects can be achieved. We can define a successor function, which takes a Church numeral and returns by adding another application of , where '(mf)x' means the function 'f' is applied 'm' times on 'x': : Because the -th composition of composed with the -th composition of gives the -th composition of , addition can be defined as follows: : can be thought of as a function taking two natural numbers as arguments and returning a natural number; it can be verified that : and : are β-equivalent lambda expressions. Since adding to a number can be accomplished by adding 1 times, an alternative definition is: : Similarly, multiplication can be defined as : Alternatively : since multiplying and is the same as repeating the add function times and then applying it to zero. Exponentiation has a rather simple rendering in Church numerals, namely : The predecessor function defined by for a positive integer and is considerably more difficult. The formula : can be validated by showing inductively that if ''T'' denotes , then for . Two other definitions of are given below, one using conditionals and the other using pairs. With the predecessor function, subtraction is straightforward. Defining : , yields when and otherwise.


Logic and predicates

By convention, the following two definitions (known as Church booleans) are used for the boolean values and : : : Then, with these two lambda terms, we can define some logic operators (these are just possible formulations; other expressions are equally correct): : : : : We are now able to compute some logic functions, for example: : :: :: and we see that is equivalent to . A ''predicate'' is a function that returns a boolean value. The most fundamental predicate is , which returns if its argument is the Church numeral , and if its argument is any other Church numeral: : The following predicate tests whether the first argument is less-than-or-equal-to the second: : , and since , if and , it is straightforward to build a predicate for numerical equality. The availability of predicates and the above definition of and make it convenient to write "if-then-else" expressions in lambda calculus. For example, the predecessor function can be defined as: : which can be verified by showing inductively that is the add − 1 function for > 0.


Pairs

A pair (2-tuple) can be defined in terms of and , by using the Church encoding for pairs. For example, encapsulates the pair (,), returns the first element of the pair, and returns the second. : : : : : A linked list can be defined as either NIL for the empty list, or the of an element and a smaller list. The predicate tests for the value . (Alternatively, with , the construct obviates the need for an explicit NULL test). As an example of the use of pairs, the shift-and-increment function that maps to can be defined as : which allows us to give perhaps the most transparent version of the predecessor function: :


Additional programming techniques

There is a considerable body of programming idioms for lambda calculus. Many of these were originally developed in the context of using lambda calculus as a foundation for programming language semantics, effectively using lambda calculus as a
low-level programming language A low-level programming language is a programming language that provides little or no abstraction from a computer's instruction set architecture—commands or functions in the language map that are structurally similar to processor's instructions ...
. Because several programming languages include the lambda calculus (or something very similar) as a fragment, these techniques also see use in practical programming, but may then be perceived as obscure or foreign.


Named constants

In lambda calculus, a
library A library is a collection of materials, books or media that are accessible for use and not just for display purposes. A library provides physical (hard copies) or digital access (soft copies) materials, and may be a physical location or a vi ...
would take the form of a collection of previously defined functions, which as lambda-terms are merely particular constants. The pure lambda calculus does not have a concept of named constants since all atomic lambda-terms are variables, but one can emulate having named constants by setting aside a variable as the name of the constant, using abstraction to bind that variable in the main body, and apply that abstraction to the intended definition. Thus to use to mean ''N'' (some explicit lambda-term) in ''M'' (another lambda-term, the "main program"), one can say : ''M'' ''N'' Authors often introduce
syntactic sugar In computer science, syntactic sugar is syntax within a programming language that is designed to make things easier to read or to express. It makes the language "sweeter" for human use: things can be expressed more clearly, more concisely, or in a ...
, such as , to permit writing the above in the more intuitive order : ''N'M'' By chaining such definitions, one can write a lambda calculus "program" as zero or more function definitions, followed by one lambda-term using those functions that constitutes the main body of the program. A notable restriction of this is that the name be not defined in ''N'', for ''N'' to be outside the scope of the abstraction binding ; this means a recursive function definition cannot be used as the ''N'' with . The construction would allow writing recursive function definitions.


Recursion and fixed points

Recursion Recursion (adjective: ''recursive'') occurs when a thing is defined in terms of itself or of its type. Recursion is used in a variety of disciplines ranging from linguistics to logic. The most common application of recursion is in mathematic ...
is the definition of a function using the function itself. Lambda calculus cannot express this as directly as some other notations: all functions are anonymous in lambda calculus, so we can't refer to a value which is yet to be defined, inside the lambda term defining that same value. However, recursion can still be achieved by arranging for a lambda expression to receive itself as its argument value, for example in  . Consider the
factorial In mathematics, the factorial of a non-negative denoted is the product of all positive integers less than or equal The factorial also equals the product of n with the next smaller factorial: \begin n! &= n \times (n-1) \times (n-2) ...
function recursively defined by : . In the lambda expression which is to represent this function, a ''parameter'' (typically the first one) will be assumed to receive the lambda expression itself as its value, so that calling it – applying it to an argument – will amount to recursion. Thus to achieve recursion, the intended-as-self-referencing argument (called here) must always be passed to itself within the function body, at a call point: : ::: with   to hold, so   and : The self-application achieves replication here, passing the function's lambda expression on to the next invocation as an argument value, making it available to be referenced and called there. This solves it but requires re-writing each recursive call as self-application. We would like to have a generic solution, without a need for any re-writes: : ::: with   to hold, so   and :  where  ::: so that  Given a lambda term with first argument representing recursive call (e.g. here), the ''fixed-point'' combinator will return a self-replicating lambda expression representing the recursive function (here, ). The function does not need to be explicitly passed to itself at any point, for the self-replication is arranged in advance, when it is created, to be done each time it is called. Thus the original lambda expression is re-created inside itself, at call-point, achieving
self-reference Self-reference occurs in natural or formal languages when a sentence, idea or formula refers to itself. The reference may be expressed either directly—through some intermediate sentence or formula—or by means of some encoding. In philos ...
. In fact, there are many possible definitions for this operator, the simplest of them being: : In the lambda calculus,   is a fixed-point of , as it expands to: : : : : : Now, to perform our recursive call to the f