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
Recursion occurs when the definition of a concept or process depends on a simpler or previous version of itself. Recursion is used in a variety of disciplines ranging from linguistics to logic. The most common application of recursion is in m ...
.
History
Dana Scott'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
Haskell () is a general-purpose, statically typed, purely functional programming language with type inference and lazy evaluation. Designed for teaching, research, and industrial applications, Haskell pioneered several programming language ...
[
] 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.
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.
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
Mathematics is a field of study that discovers and organizes methods, Mathematical theory, theories and theorems that are developed and Mathematical proof, proved for the needs of empirical sciences and mathematics itself. There are many ar ...
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'',
: