Let expression

Last updated

In computer science, a "let" expression associates a function definition with a restricted scope.

Contents

The "let" expression may also be defined in mathematics, where it associates a Boolean condition with a restricted scope.

The "let" expression may be considered as a lambda abstraction applied to a value. Within mathematics, a let expression may also be considered as a conjunction of expressions, within an existential quantifier which restricts the scope of the variable.

The let expression is present in many functional languages to allow the local definition of expression, for use in defining another expression. The let-expression is present in some functional languages in two forms; let or "let rec". Let rec is an extension of the simple let expression which uses the fixed-point combinator to implement recursion.

History

Dana Scott's LCF language [1] was a stage in the evolution of lambda calculus into modern functional languages. This language introduced the let expression, which has appeared in most functional languages since that time.

The languages Scheme, [2] ML, and more recently Haskell [3] have inherited let expressions from LCF.

Stateful imperative languages such as ALGOL and Pascal essentially implement a let expression, to implement restricted scope of functions, in block structures.[ citation needed ]

A closely related "where" clause, together with its recursive variant "where rec", appeared already in Peter Landin's The mechanical evaluation of expressions. [4]

Description

A "let" expression defines a function or value for use in another expression. As well as being a construct used in many functional programming languages, it is a natural language construct often used in mathematical texts. It is an alternate syntactical construct for a where clause.

Let expressionWhere clause

Let

and

in

where

and

In both cases the whole construct is an expression whose value is 5. Like the if-then-else the type returned by the expression is not necessarily Boolean.

A let expression comes in 4 main forms,

FormAndRecursiveDefinition / ConstraintDescription
SimpleNoNoDefinitionSimple non recursive function definition.
RecursiveNoYesDefinitionRecursive function definition (implemented using the Y combinator).
MutualYesYesDefinitionMutually recursive function definition.
MathematicalYesYesConstraintMathematical definition supporting a general Boolean let condition.

In functional languages the let expression defines functions which may be called in the expression. The scope of the function name is limited to the let expression structure.

In mathematics, the let expression defines a condition, which is a constraint on the expression. The syntax may also support the declaration of existentially quantified variables local to the let expression.

The terminology, syntax and semantics vary from language to language. In Scheme, let is used for the simple form and let rec for the recursive form. In ML let marks only the start of a block of declarations with fun marking the start of the function definition. In Haskell, let may be mutually recursive, with the compiler figuring out what is needed.

Definition

A lambda abstraction represents a function without a name. This is a source of the inconsistency in the definition of a lambda abstraction. However lambda abstractions may be composed to represent a function with a name. In this form the inconsistency is removed. The lambda term,

is equivalent to defining the function by in the expression , which may be written as the let expression;

The let expression is understandable as a natural language expression. The let expression represents the substitution of a variable for a value. The substitution rule describes the implications of equality as substitution.

Let definition in mathematics

In mathematics the let expression is described as the conjunction of expressions. In functional languages the let expression is also used to limit scope. In mathematics scope is described by quantifiers. The let expression is a conjunction within an existential quantifier.

where E and F are of type Boolean.

The let expression allows the substitution to be applied to another expression. This substitution may be applied within a restricted scope, to a sub expression. The natural use of the let expression is in application to a restricted scope (called lambda dropping). These rules define how the scope may be restricted;

where F is not of type Boolean. From this definition the following standard definition of a let expression, as used in a functional language may be derived.

For simplicity the marker specifying the existential variable, , will be omitted from the expression where it is clear from the context.

Derivation

To derive this result, first assume,

then

Using the rule of substitution,

so for all L,

Let where K is a new variable. then,

So,

But from the mathematical interpretation of a beta reduction,

Here if y is a function of a variable x, it is not the same x as in z. Alpha renaming may be applied. So we must have,

so,

This result is represented in a functional language in an abbreviated form, where the meaning is unambiguous;

Here the variable x is implicitly recognised as both part of the equation defining x, and the variable in the existential quantifier.

No lifting from Boolean

A contradiction arises if E is defined by . In this case,

becomes,

and using,

This is false if G is false. To avoid this contradiction F is not allowed to be of type Boolean. For Boolean F the correct statement of the dropping rule uses implication instead of equality,

It may appear strange that a different rule applies for Boolean than other types. The reason for this is that the rule,

only applies where F is Boolean. The combination of the two rules creates a contradiction, so where one rule holds, the other does not.

Joining let expressions

Let expressions may be defined with multiple variables,

then it can be derived,

so,

Laws relating lambda calculus and let expressions

The Eta reduction gives a rule for describing lambda abstractions. This rule along with the two laws derived above define the relationship between lambda calculus and let expressions.

NameLaw
Eta-reduction equivalence
Let-lambda equivalence (where is a variable name.)
Let combination

Let definition defined from lambda calculus

To avoid the potential problems associated with the mathematical definition, Dana Scott originally defined the let expression from lambda calculus. This may be considered as the bottom up, or constructive, definition of the let expression, in contrast to the top down, or axiomatic mathematical definition.

The simple, non recursive let expression was defined as being syntactic sugar for the lambda abstraction applied to a term. In that definition,

The simple let expression definition was then extended to allow recursion using the fixed-point combinator.

Fixed-point combinator

The fixed-point combinator may be represented by the expression,

This representation may be converted into a lambda term. A lambda abstraction does not support reference to the variable name, in the applied expression, so x must be passed in as a parameter to x.

Using the eta reduction rule,

gives,

A let expression may be expressed as a lambda abstraction using,

gives,

This is possibly the simplest implementation of a fixed point combinator in lambda calculus. However one beta reduction gives the more symmetrical form of Curry's Y combinator.

Recursive let expression

The recursive let expression called "let rec" is defined using the Y combinator for recursive let expressions.

Mutually recursive let expression

This approach is then generalized to support mutual recursion. A mutually recursive let expression may be composed by rearranging the expression to remove any and conditions. This is achieved by replacing multiple function definitions with a single function definition, which sets a list of variables equal to a list of expressions. A version of the Y combinator, called the Y* poly-variadic fix-point combinator [5] is then used to calculate fixed point of all the functions at the same time. The result is a mutually recursive implementation of the let expression.

Multiple values

A let expression may be used to represent a value that is a member of a set,

Under function application, of one let expression to another,

But a different rule applies for applying the let expression to itself.

There appear no simple rule for combining values. What is required is a general form of expression that represents a variable whose value is a member of a set of values. The expression should be based on the variable and the set.

Function application applied to this form should give another expression in the same form. In this way any expression on functions of multiple values may be treated as if it had one value.

It is not sufficient for the form to represent only the set of values. Each value must have a condition that determines when the expression takes the value. The resulting construct is a set of pairs of conditions and values, called a "value set". See narrowing of algebraic value sets.

Rules for conversion between lambda calculus and let expressions

Meta-functions will be given that describe the conversion between lambda and let expressions. A meta-function is a function that takes a program as a parameter. The program is data for the meta-program. The program and the meta program are at different meta-levels.

The following conventions will be used to distinguish program from the meta program,

For simplicity the first rule given that matches will be applied. The rules also assume that the lambda expressions have been pre-processed so that each lambda abstraction has a unique name.

The substitution operator is also used. The expression means substitute every occurrence of G in L by S and return the expression. The definition used is extended to cover the substitution of expressions, from the definition given on the Lambda calculus page. The matching of expressions should compare expressions for alpha equivalence (renaming of variables).

Conversion from lambda to let expressions

The following rules describe how to convert from a lambda expression to a let expression, without altering the structure.

Rule 6 creates a unique variable V, as a name for the function.

Example

For example, the Y combinator,

is converted to,

RuleLambda expression
6
4
5
3
8
8
4
2
1

Conversion from let to lambda expressions

These rules reverse the conversion described above. They convert from a let expression to a lambda expression, without altering the structure. Not all let expressions may be converted using these rules. The rules assume that the expressions are already arranged as if they had been generated by de-lambda.

There is no exact structural equivalent in lambda calculus for let expressions that have free variables that are used recursively. In this case some addition of parameters is required. Rules 8 and 10 add these parameters.

Rules 8 and 10 are sufficient for two mutually recursive equations in the let expression. However they will not work for three or more mutually recursive equations. The general case needs an extra level of looping which makes the meta function a little more difficult. The rules that follow replace rules 8 and 10 in implementing the general case. Rules 8 and 10 have been left so that the simpler case may be studied first.

  1. lambda-form - Convert the expression into a conjunction of expressions, each of the form variable = expression.
    1. ...... where V is a variable.
  2. lift-vars - Get the set of variables that need X as a parameter, because the expression has X as a free variable.
  3. sub-vars - For each variable in the set substitute it for the variable applied to X in the expression. This makes X a variable passed in as a parameter, instead of being a free variable in the right hand side of the equation.
  4. de-let - Lift each condition in E so that X is not a free variable on the right hand side of the equation.

Examples

For example, the let expression obtained from the Y combinator,

is converted to,

RuleLambda expression
6
1
2
3
7
4
4
5
1
2
3
4
5

For a second example take the lifted version of the Y combinator,

is converted to,

RuleLambda expression
8
7
1, 2
7, 4, 5
1, 2

For a third example the translation of,

is,

RuleLambda expression
9
1
2
7
1
2

For a forth example the translation of,

is,

which is the famous y combinator.

RuleLambda expression
9
2
7
1
2

Key people

See also

Related Research Articles

Lambda calculus is a formal system in mathematical logic for expressing computation based on function abstraction and application using variable binding and substitution. It is a universal model of computation that can be used to simulate any Turing machine. It was introduced by the mathematician Alonzo Church in the 1930s as part of his research into the foundations of mathematics.

<span class="mw-page-title-main">Negative binomial distribution</span> Probability distribution

In probability theory and statistics, the negative binomial distribution is a discrete probability distribution that models the number of failures in a sequence of independent and identically distributed Bernoulli trials before a specified (non-random) number of successes occurs. For example, we can define rolling a 6 on a die as a success, and rolling any other number as a failure, and ask how many failure rolls will occur before we see the third success. In such a case, the probability distribution of the number of failures that appear will be a negative binomial distribution.

<span class="mw-page-title-main">Exponential distribution</span> Probability distribution

In probability theory and statistics, the exponential distribution or negative exponential distribution is the probability distribution of the time between events in a Poisson point process, i.e., a process in which events occur continuously and independently at a constant average rate. It is a particular case of the gamma distribution. It is the continuous analogue of the geometric distribution, and it has the key property of being memoryless. In addition to being used for the analysis of Poisson point processes it is found in various other contexts.

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 computation and also as a basis for the design of functional programming languages. It is based on combinators, which were introduced by Schönfinkel in 1920 with the idea of providing an analogous way to build up functions—and to remove any mention of variables—particularly in predicate logic. A combinator is a higher-order function that uses only function application and earlier defined combinators to define a result from its arguments.

In combinatory logic for computer science, a fixed-point combinator, denoted , is a higher-order function that returns some fixed point of its argument function, if one exists.

<span class="mw-page-title-main">Cayley–Hamilton theorem</span> Every square matrix over a commutative ring satisfies its own characteristic equation

In linear algebra, the Cayley–Hamilton theorem states that every square matrix over a commutative ring satisfies its own characteristic equation.

In mathematics, a self-adjoint operator on an infinite-dimensional complex vector space V with inner product is a linear map A that is its own adjoint. If V is finite-dimensional with a given orthonormal basis, this is equivalent to the condition that the matrix of A is a Hermitian matrix, i.e., equal to its conjugate transpose A. By the finite-dimensional spectral theorem, V has an orthonormal basis such that the matrix of A relative to this basis is a diagonal matrix with entries in the real numbers. This article deals with applying generalizations of this concept to operators on Hilbert spaces of arbitrary dimension.

In Riemannian geometry, the sectional curvature is one of the ways to describe the curvature of Riemannian manifolds. The sectional curvature Kp) depends on a two-dimensional linear subspace σp of the tangent space at a point p of the manifold. It can be defined geometrically as the Gaussian curvature of the surface which has the plane σp as a tangent plane at p, obtained from geodesics which start at p in the directions of σp. The sectional curvature is a real-valued function on the 2-Grassmannian bundle over the manifold.

<span class="mw-page-title-main">Jensen's inequality</span> Theorem of convex functions

In mathematics, Jensen's inequality, named after the Danish mathematician Johan Jensen, relates the value of a convex function of an integral to the integral of the convex function. It was proved by Jensen in 1906, building on an earlier proof of the same inequality for doubly-differentiable functions by Otto Hölder in 1889. Given its generality, the inequality appears in many forms depending on the context, some of which are presented below. In its simplest form the inequality states that the convex transformation of a mean is less than or equal to the mean applied after convex transformation; it is a simple corollary that the opposite is true of concave transformations.

In physics, a partition function describes the statistical properties of a system in thermodynamic equilibrium. Partition functions are functions of the thermodynamic state variables, such as the temperature and volume. Most of the aggregate thermodynamic variables of the system, such as the total energy, free energy, entropy, and pressure, can be expressed in terms of the partition function or its derivatives. The partition function is dimensionless.

<span class="mw-page-title-main">Weierstrass elliptic function</span> Class of mathematical functions

In mathematics, the Weierstrass elliptic functions are elliptic functions that take a particularly simple form. They are named for Karl Weierstrass. This class of functions are also referred to as ℘-functions and they are usually denoted by the symbol ℘, a uniquely fancy script p. They play an important role in the theory of elliptic functions. A ℘-function together with its derivative can be used to parameterize elliptic curves and they generate the field of elliptic functions with respect to a given period lattice.

In the theory of stochastic processes, the Karhunen–Loève theorem, also known as the Kosambi–Karhunen–Loève theorem states that a stochastic process can be represented as an infinite linear combination of orthogonal functions, analogous to a Fourier series representation of a function on a bounded interval. The transformation is also known as Hotelling transform and eigenvector transform, and is closely related to principal component analysis (PCA) technique widely used in image processing and in data analysis in many fields.

Lambda lifting is a meta-process that restructures a computer program so that functions are defined independently of each other in a global scope. An individual "lift" transforms a local function into a global function. It is a two step process, consisting of;

In mathematics, Church encoding is a means of representing data and operators in the lambda calculus. The Church numerals are a representation of the natural numbers using lambda notation. The method is named for Alonzo Church, who first encoded data in the lambda calculus this way.

A ratio distribution is a probability distribution constructed as the distribution of the ratio of random variables having two other known distributions. Given two random variables X and Y, the distribution of the random variable Z that is formed as the ratio Z = X/Y is a ratio distribution.

In mathematics, the spectral theory of ordinary differential equations is the part of spectral theory concerned with the determination of the spectrum and eigenfunction expansion associated with a linear ordinary differential equation. In his dissertation, Hermann Weyl generalized the classical Sturm–Liouville theory on a finite closed interval to second order differential operators with singularities at the endpoints of the interval, possibly semi-infinite or infinite. Unlike the classical case, the spectrum may no longer consist of just a countable set of eigenvalues, but may also contain a continuous part. In this case the eigenfunction expansion involves an integral over the continuous part with respect to a spectral measure, given by the Titchmarsh–Kodaira formula. The theory was put in its final simplified form for singular differential equations of even degree by Kodaira and others, using von Neumann's spectral theorem. It has had important applications in quantum mechanics, operator theory and harmonic analysis on semisimple Lie groups.

<span class="mw-page-title-main">Poisson distribution</span> Discrete probability distribution

In probability theory and statistics, the Poisson distribution is a discrete probability distribution that expresses the probability of a given number of events occurring in a fixed interval of time or space if these events occur with a known constant mean rate and independently of the time since the last event. It is named after French mathematician Siméon Denis Poisson. The Poisson distribution can also be used for the number of events in other specified interval types such as distance, area, or volume. It plays an important role for discrete-stable distributions.

In linear algebra, particularly projective geometry, a semilinear map between vector spaces V and W over a field K is a function that is a linear map "up to a twist", hence semi-linear, where "twist" means "field automorphism of K". Explicitly, it is a function T : VW that is:

Deductive lambda calculus considers what happens when lambda terms are regarded as mathematical expressions. One interpretation of the untyped lambda calculus is as a programming language where evaluation proceeds by performing reductions on an expression until it is in normal form. In this interpretation, if the expression never reduces to normal form then the program never terminates, and the value is undefined. Considered as a mathematical deductive system, each reduction would not alter the value of the expression. The expression would equal the reduction of the expression.

Lambda calculus is a formal mathematical system based on lambda abstraction and function application. Two definitions of the language are given here: a standard definition, and a definition using mathematical formulas.

References

  1. "PCF is a programming language for computable functions, based on LCF, Scott’s logic of computable functions" ( Plotkin 1977 ). Programming Computable Functions is used by ( Mitchell 1996 ). It is also referred to as Programming with Computable Functions or Programming language for Computable Functions.
  2. "Scheme - Variables and Let Expressions".
  3. Simon, Marlow (2010). "Haskell 2010 Language Report - Let Expressions".
  4. Landin, Peter J. (1964). "The mechanical evaluation of expressions". The Computer Journal . British Computer Society. 6 (4): 308–320. doi: 10.1093/comjnl/6.4.308 .
  5. "Simplest poly-variadic fix-point combinators for mutual recursion".

    Works cited