Let expression
In computer science, a "let" expression associates a function definition with a restricted scope.
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
's LCF language 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, ML, and more recently Haskell 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.
A closely related "where" clause, together with its recursive variant "where rec", appeared already in Peter Landin's The mechanical evaluation of expressions.
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 expression | Where 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,
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. DefinitionA 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 mathematicsIn 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. 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. DerivationTo 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 BooleanA 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 expressionsLet expressions may be defined with multiple variables,then it can be derived, so, Laws relating lambda calculus and let expressionsThe 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.Let definition defined from lambda calculusTo 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 combinatorThe 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 expressionThe recursive let expression called "let rec" is defined using the Y combinator for recursive let expressions.Mutually recursive let expressionThis 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 combinatoris 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 valuesA 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 expressionswill 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,
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. Conversion from lambda to let expressionsThe following rules describe how to convert from a lambda expression to a let expression, without altering the structure.ExampleFor example, the Y combinator,is converted to,
|