Lambda lifting explained

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;

The term "lambda lifting" was first introduced by Thomas Johnsson around 1982 and was historically considered as a mechanism for implementing functional programming languages. It is used in conjunction with other techniques in some modern compilers.

Lambda lifting is not the same as closure conversion. It requires all call sites to be adjusted (adding extra arguments to calls) and does not introduce a closure for the lifted lambda expression. In contrast, closure conversion does not require call sites to be adjusted but does introduce a closure for the lambda expression mapping free variables to values.

The technique may be used on individual functions, in code refactoring, to make a function usable outside the scope in which it was written. Lambda lifts may also be repeated, in order to transform the program. Repeated lifts may be used to convert a program written in lambda calculus into a set of recursive functions, without lambdas. This demonstrates the equivalence of programs written in lambda calculus and programs written as functions.[1] However it does not demonstrate the soundness of lambda calculus for deduction, as the eta reduction used in lambda lifting is the step that introduces cardinality problems into the lambda calculus, because it removes the value from the variable, without first checking that there is only one value that satisfies the conditions on the variable (see Curry's paradox).

Lambda lifting is expensive on processing time for the compiler. An efficient implementation of lambda lifting is

O(n2)

on processing time for the compiler.

In the untyped lambda calculus, where the basic types are functions, lifting may change the result of beta reduction of a lambda expression. The resulting functions will have the same meaning, in a mathematical sense, but are not regarded as the same function in the untyped lambda calculus. See also intensional versus extensional equality.

The reverse operation to lambda lifting is lambda dropping.[2]

Lambda dropping may make the compilation of programs quicker for the compiler, and may also increase the efficiency of the resulting program, by reducing the number of parameters, and reducing the size of stack frames.However it makes a function harder to re-use. A dropped function is tied to its context, and can only be used in a different context if it is first lifted.

Algorithm

The following algorithm is one way to lambda-lift an arbitrary program in a language which doesn't support closures as first-class objects:

  1. Rename the functions so that each function has a unique name.
  2. Replace each free variable with an additional argument to the enclosing function, and pass that argument to every use of the function.
  3. Replace every local function definition that has no free variables with an identical global function.
  4. Repeat steps 2 and 3 until all free variables and local functions are eliminated.

If the language has closures as first-class objects that can be passed as arguments or returned from other functions, the closure will need to be represented by a data structure that captures the bindings of the free variables.

Example

The following OCaml program computes the sum of the integers from 1 to 100:

let rec sum n = if n = 1 then 1 else let f x = n + x in f (sum (n - 1)) insum 100

(The let rec declares sum as a function that may call itself.) The function f, which adds sum's argument to the sum of the numbers less than the argument, is a local function. Within the definition of f, n is a free variable. Start by converting the free variable to a parameter:

let rec sum n = if n = 1 then 1 else let f w x = w + x in f n (sum (n - 1)) insum 100

Next, lift f into a global function:

let rec f w x = w + xand sum n = if n = 1 then 1 else f n (sum (n - 1)) insum 100

The following is the same example, this time written in JavaScript:

// Initial version

function sum(n)

// After converting the free variable n to a formal parameter w

function sum(n)

// After lifting function f into the global scope

function f(w, x)

function sum(n)

Lambda lifting versus closures

Lambda lifting and closure are both methods for implementing block structured programs. It implements block structure by eliminating it. All functions are lifted to the global level. Closure conversion provides a "closure" which links the current frame to other frames. Closure conversion takes less compile time.

Recursive functions, and block structured programs, with or without lifting, may be implemented using a stack based implementation, which is simple and efficient. However a stack frame based implementation must be strict (eager). The stack frame based implementation requires that the life of functions be last-in, first-out (LIFO). That is, the most recent function to start its calculation must be the first to end.

Some functional languages (such as Haskell) are implemented using lazy evaluation, which delays calculation until the value is needed. The lazy implementation strategy gives flexibility to the programmer. Lazy evaluation requires delaying the call to a function until a request is made to the value calculated by the function. One implementation is to record a reference to a "frame" of data describing the calculation, in place of the value. Later when the value is required, the frame is used to calculate the value, just in time for when it is needed. The calculated value then replaces the reference.

The "frame" is similar to a stack frame, the difference being that it is not stored on the stack. Lazy evaluation requires that all the data required for the calculation be saved in the frame. If the function is "lifted", then the frame needs only record the function pointer, and the parameters to the function. Some modern languages use garbage collection in place of stack based allocation to manage the life of variables. In a managed, garbage collected environment, a closure records references to the frames from which values may be obtained. In contrast a lifted function has parameters for each value needed in the calculation.

Let expressions and lambda calculus

The Let expression is useful in describing lifting and dropping, and in describing the relationship between recursive equations and lambda expressions. Most functional languages have let expressions. Also, block structured programming languages like ALGOL and Pascal are similar in that they too allow the local definition of a function for use in a restricted scope.

The let expression used here is a fully mutually recursive version of let rec, as implemented in many functional languages.

Let expressions are related to Lambda calculus. Lambda calculus has a simple syntax and semantics, and is good for describing Lambda lifting. It is convenient to describe lambda lifting as a translations from lambda to a let expression, and lambda dropping as the reverse. This is because let expressions allow mutual recursion, which is, in a sense, more lifted than is supported in lambda calculus. Lambda calculus does not support mutual recursion and only one function may be defined at the outermost global scope.

Conversion rules which describe translation without lifting are given in the Let expression article.

The following rules describe the equivalence of lambda and let expressions,

Name Law
Eta-reduction equivalence

fx=y\equivf=λx.y

Let-Lambda equivalence

f\notinFV(E)\to(\operatorname{let}f:f=E\operatorname{in}L\equiv(λf.L)E)(wherefisavariablename.)

Let combination

x\notinFV(E)\to(\operatorname{let}v,...,w,x:E\landF\operatorname{in}L\equiv\operatorname{let}v,...,w:E\operatorname{in}\operatorname{let}x:F\operatorname{in}L)

Meta-functions will be given that describe lambda lifting and dropping. 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,

\equiv

will be used for equals in the meta program.

\

represents a dummy variable, or an unknown value.

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 used extensively. The expression

L[G:=S]

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).

Lambda lifting in lambda calculus

Each lambda lift takes a lambda abstraction which is a sub expression of a lambda expression and replaces it by a function call (application) to a function that it creates. The free variables in the sub expression are the parameters to the function call.

Lambda lifts may be used on individual functions, in code refactoring, to make a function usable outside the scope in which it was written. Such lifts may also be repeated, until the expression has no lambda abstractions, in order to transform the program.

Lambda lift

A lift is given a sub-expression within an expression to lift to the top of that expression. The expression may be part of a larger program. This allows control of where the sub-expression is lifted to. The lambda lift operation used to perform a lift within a program is,

\operatorname{lambda-lift-op}[S,L,P]=P[L:=\operatorname{lambda-lift}[S,L]]

The sub expression may be either a lambda abstraction, or a lambda abstraction applied to a parameter.

Two types of lift are possible.

An anonymous lift has a lift expression which is a lambda abstraction only. It is regarded as defining an anonymous function. A name must be created for the function.

A named lift expression has a lambda abstraction applied to an expression. This lift is regarded as a named definition of a function.

Anonymous lift

An anonymous lift takes a lambda abstraction (called S). For S;

The lambda lift is the substitution of the lambda abstraction S for a function application, along with the addition of a definition for the function.

\operatorname{lambda-lift}[S,L]\equiv\operatorname{let}V:\operatorname{de-lambda}[G=S]\operatorname{in}L[S:=G]

The new lambda expression has S substituted for G. Note that L[''S'':=''G''] means substitution of S for G in L. The function definitions has the function definition G = S added.

In the above rule G is the function application that is substituted for the expression S. It is defined by,

G=\operatorname{make-call}[V,\operatorname{FV}[S]]

where V is the function name. It must be a new variable, i.e. a name not already used in the lambda expression,

V\not\in\operatorname{vars}[\operatorname{let}F\operatorname{in}L]

where

\operatorname{vars}[E]

is a meta function that returns the set of variables used in E.
Constructing the call

The function call G is constructed by adding parameters for each variable in the free variable set (represented by V), to the function H,

X\inV\to\operatorname{make-call}[H,V]\equiv\operatorname{make-call}[H,V\cap\neg\{X\}]X

\operatorname{make-call}[H,\{\}]\equivH

Named lift

The named lift is similar to the anonymous lift except that the function name V is provided.

\operatorname{lambda-lift}[(λV.E)S,L]\equiv\operatorname{let}V:\operatorname{de-lambda}[G=S]\operatorname{in}L[(λV.E)S:=E[V:=G]]

As for the anonymous lift, the expression G is constructed from V by applying the free variables of S. It is defined by,

G=\operatorname{make-call}[V,\operatorname{FV}[S]]

Lambda-lift transformation

A lambda lift transformation takes a lambda expression and lifts all lambda abstractions to the top of the expression. The abstractions are then translated into recursive functions, which eliminates the lambda abstractions. The result is a functional program in the form,

\operatorname{let}M\operatorname{in}N

where M is a series of function definitions, and N is the expression representing the value returned.

For example,

\operatorname{lambda-lift-tran}[λf.(λx.f(xx))(λx.f(xx))]\equiv\operatorname{let}pfx=f(xx)\landqpf=(pf)(pf)\operatorname{in}qp

The de-let meta function may then be used to convert the result back into lambda calculus.

\operatorname{de-let}[\operatorname{lambda-lift-tran}[λf.(λx.f(xx))(λx.f(xx))]]\equiv(λp.(λq.qp) λp.λf.(pf)(pf)) λf.λx.f(xx)

The processing of transforming the lambda expression is a series of lifts. Each lift has,

\begin{cases} \operatorname{lambda-lift-tran}[L]=\operatorname{drop-params-tran}[\operatorname{merge-let}[\operatorname{lambda-apply}[L]]]\\ \operatorname{lambda-apply}[L]=\operatorname{lambda-process}[\operatorname{lift-choice}[L],L]\\ \operatorname{lambda-process}[\operatorname{none},L]=L\\ \operatorname{lambda-process}[S,L]=\operatorname{lambda-apply}[\operatorname{lambda-lift}[S,L]] \end{cases}

After the lifts are applied the lets are combined together into a single let.

\begin{cases} \operatorname{merge-let}[\operatorname{let}V:E\operatorname{in}\operatorname{let}W:F\operatorname{in}G]=\operatorname{merge-let}[\operatorname{let}V,W:E\landF\operatorname{in}G]\\ \operatorname{merge-let}[E]=E\end{cases}

Then Parameter dropping is applied to remove parameters that are not necessary in the "let" expression. The let expression allows the function definitions to refer to each other directly, whereas lambda abstractions are strictly hierarchical, and a function may not directly refer to itself.

Choosing the expression for lifting

There are two different ways that an expression may be selected for lifting. The first treats all lambda abstractions as defining anonymous functions. The second, treats lambda abstractions which are applied to a parameter as defining a function. Lambda abstractions applied to a parameter have a dual interpretation as either a let expression defining a function, or as defining an anonymous function. Both interpretations are valid.

These two predicates are needed for both definitions.

lambda-free - An expression containing no lambda abstractions.

\begin{cases} \operatorname{lambda-free}[λF.X]=\operatorname{false}\\ \operatorname{lambda-free}[V]=\operatorname{true}\\ \operatorname{lambda-free}[MN]=\operatorname{lambda-free}[M]\land\operatorname{lambda-free}[N]\end{cases}

lambda-anon - An anonymous function. An expression like

λx1.... λxn.X

where X is lambda free.

\begin{cases} \operatorname{lambda-anon}[λF.X]=\operatorname{lambda-free}[X]\lor\operatorname{lambda-anon}[X]\\ \operatorname{lambda-anon}[V]=\operatorname{false}\\ \operatorname{lambda-anon}[MN]=\operatorname{false}\end{cases}

Choosing anonymous functions only for lifting

Search for the deepest anonymous abstraction, so that when the lift is applied the function lifted will become a simple equation. This definition does not recognize a lambda abstractions with a parameter as defining a function. All lambda abstractions are regarded as defining anonymous functions.

lift-choice - The first anonymous found in traversing the expression or none if there is no function.

\operatorname{lambda-anon}[X]\to\operatorname{lift-choice}[X]=X

\operatorname{lift-choice}[λF.X]=\operatorname{lift-choice}[X]

\operatorname{lift-choice}[M]\ne\operatorname{none}\to\operatorname{lift-choice}[MN]=\operatorname{lift-choice}[M]

\operatorname{lift-choice}[MN]=\operatorname{lift-choice}[N]

\operatorname{lift-choice}[V]=\operatorname{none}

For example,

Lambda choice on

λf.(λx.f(xx))(λy.f(yy))

is

λx.f(xx)

Rule function typechoice
2

\operatorname{lift-choice}[λf.(λx.f(xx))(λy.f(yy))]

3

\operatorname{lift-choice}[(λx.f(xx))(λy.f(yy))]

1 anon

\operatorname{lift-choice}[λx.f(xx)]

λx.f(xx)

Lambda choice on

λf.(pf)(pf)

is

λf.(pf)(pf)

Rule function typechoice
2 anon

\operatorname{lift-choice}[λf.(pf)(pf)]

2

λf.(pf)(pf)

Choosing named and anonymous functions for lifting

Search for the deepest named or anonymous function definition, so that when the lift is applied the function lifted will become a simple equation. This definition recognizes a lambda abstraction with an actual parameter as defining a function. Only lambda abstractions without an application are treated as anonymous functions.

lambda-named : A named function. An expression like

(λF.M)N

where M is lambda free and N is lambda free or an anonymous function.

\begin{array}{l} \operatorname{lambda-named}[(λF.M)N]=\operatorname{lambda-free}[M]\land\operatorname{lambda-anon}[N]\\ \operatorname{lambda-named}[λF.X]=\operatorname{false}\\ \operatorname{lambda-named}[V]=\operatorname{false} \end{array}

lift-choice : The first anonymous or named function found in traversing the expression or none if there is no function.

\operatorname{lambda-named}[X]\lor\operatorname{lambda-anon}[X]\to\operatorname{lift-choice}[X]=X

\operatorname{lift-choice}[λF.X]=\operatorname{lift-choice}[X]

\operatorname{lift-choice}[M]\ne\operatorname{none}\to\operatorname{lift-choice}[MN]=\operatorname{lift-choice}[M]

\operatorname{lift-choice}[MN]=\operatorname{lift-choice}[N]

\operatorname{lift-choice}[V]=\operatorname{none}

For example,

Lambda choice on

λf.(λx.f(xx))(λy.f(yy))

is

(λx.f(xx))(λy.f(yy))

Rule function type choice
2

\operatorname{lift-choice}[λf.(λx.f(xx))(λy.f(yy))]

1 named

\operatorname{lift-choice}[(λx.f(xx))(λy.f(yy))]

(λx.f(xx))(λy.f(yy))

Lambda choice on

λf.f((xf)(xf))

is

λf.f((xf)(xf))

Rule function type choice
1 anon

\operatorname{lift-choice}[λf.f((xf)(xf))]

λf.f((xf)(xf))

Examples

For example, the Y combinator,

λf.(λx.f(xx))(λx.f(xx))

is lifted as,

\operatorname{let}xfy=f(yy)\landqxf=f((xf)(xf))\operatorname{in}qx

and after Parameter dropping,

\operatorname{let}xfy=f(yy)\landqf=f((xf)(xf))\operatorname{in}q

As a lambda expression (see Conversion from let to lambda expressions),

(λx.(λq.q) λf.f(xf)(xf)) λf.λy.f(yy)

Lifting named and anonymous functions
Lambda Expression Function From To Variables
1

λf.(λx.f(xx))(λx.f(xx))

true

(λx.f(xx))(λx.f(xx))

xf

\{x,f\}

2

(λf.(λx.f(xx))(λx.f(xx)))

[(λx.f(xx))(λx.f(xx)):=f((xf)(xf))]

xf=λy.f(yy)

\{x,f,p\}

3

λf.f((xf)(xf))

xfy=f(yy)

λf.f((xf)(xf))

qx

\{x,f,p\}

4

λf.f((xf)(xf))[λf.f((xf)(xf)):=qx]

xfy=f(yy)\landqx=λf.f((xf)(xf))

\{x,f,p,q\}

5

qx

xfy=f(yy)\landqxf=f((xf)(xf))

\{x,f,p,q\}

If lifting anonymous functions only, the Y combinator is,

\operatorname{let}pfx=f(xx)\landqpf=(pf)(pf)\operatorname{in}qp

and after Parameter dropping,

\operatorname{let}pfx=f(xx)\landqf=(pf)(pf)\operatorname{in}q

As a lambda expression,

(λp.(λq.q) λf.(pf)(pf)) λf.λx.f(xx)

Lifting anonymous functions only
Lambda Expression Function From To Variables
1

λf.(λx.f(xx))(λx.f(xx))

true

λx.f(xx)

pf

\{x,f\}

2

(λf.(λx.f(xx))(λx.f(xx)))[λx.f(xx):=pf]

pf=λx.f(xx)

\{x,f,p\}

3

λf.(pf)(pf)

pfx=f(xx)

λf.(pf)(pf)

qp

\{x,f,p\}

4

λf.(pf)(pf)[λf.(pf)(pf):=qp]

pfx=f(xx)\landqp=λf.(pf)(pf)

\{x,f,p,q\}

5

qp

pfx=f(xx)\landqpf=(pf)(pf)

\{x,f,p,q\}

The first sub expression to be chosen for lifting is

λx.f(xx)

. This transforms the lambda expression into

λf.(pf)(pf)

and creates the equation

pfx=f(xx)

.

The second sub expression to be chosen for lifting is

λf.(pf)(pf)

. This transforms the lambda expression into

qp

and creates the equation

qpf=(pf)(pf)

.

And the result is,

\operatorname{let}pfx=f(xx)\landqpf=(pf)(pf)\operatorname{in}qp

Surprisingly this result is simpler than the one obtained from lifting named functions.

Execution

Apply function to,

\begin{cases} λf.(λx.f(xx))(λx.f(xx))K&\operatorname{let}pfx=f(xx)\landqpf=(pf)(pf)\operatorname{in}qpK\\ (λx.K(xx))(λx.K(xx))&\operatorname{let}pfx=f(xx)\landqpf=(pf)(pf)\operatorname{in}pK(pK)\\ K((λx.K(xx))(λx.K(xx)))&\operatorname{let}pfx=f(xx)\landqpf=pf(pf)\operatorname{in}K(pK(pK))\\ \end{cases}

So,

(λx.K(xx))(λx.K(xx))=K((λx.K(xx))(λx.K(xx))))

or

pK(pK)=K(pK(pK))

The Y-Combinator calls its parameter (function) repeatedly on itself. The value is defined if the function has a fixed point. But the function will never terminate.

Lambda dropping in lambda calculus

Lambda dropping[3] is making the scope of functions smaller and using the context from the reduced scope to reduce the number of parameters to functions. Reducing the number of parameters makes functions easier to comprehend.

In the Lambda lifting section, a meta function for first lifting and then converting the resulting lambda expression into recursive equation was described. The Lambda Drop meta function performs the reverse by first converting recursive equations to lambda abstractions, and then dropping the resulting lambda expression, into the smallest scope which covers all references to the lambda abstraction.

Lambda dropping is performed in two steps,

Lambda drop

A Lambda drop is applied to an expression which is part of a program. Dropping is controlled by a set of expressions from which the drop will be excluded.

\operatorname{lambda-drop-op}[L,P,X]=P[L:=\operatorname{drop-params-tran}[\operatorname{sink-test}[L,X]]]

where,

L is the lambda abstraction to be dropped.

P is the program

X is a set of expressions to be excluded from dropping.

Lambda drop transformation

The lambda drop transformation sinks all abstractions in an expression. Sinking is excluded from expressions in a set of expressions,

\operatorname{lambda-drop-tran}[L,X]=\operatorname{drop-params-tran}[\operatorname{sink-tran}[\operatorname{de-let}[L,X]]]

where,

L is the expression to be transformed.

X is a set of sub expressions to be excluded from the dropping.

sink-tran sinks each abstraction, starting from the innermost,

\begin{cases} \operatorname{sink-tran}[(λN.B)Y,X]=\operatorname{sink-test}[(λN.\operatorname{sink-tran}[B])\operatorname{sink-tran}[Y],X]\\ \operatorname{sink-tran}[λN.B,X]=λN.\operatorname{sink-tran}[B,X]\\ \operatorname{sink-tran}[MN,X]=\operatorname{sink-tran}[M,X]\operatorname{sink-tran}[M,X]\\ \operatorname{sink-tran}[V,X]=V\end{cases}

Abstraction sinking

Sinking is moving a lambda abstraction inwards as far as possible such that it is still outside all references to the variable.

Application - 4 cases.

\begin{cases} E\not\in\operatorname{FV}[G]\landE\not\in\operatorname{FV}[H]\to\operatorname{sink}[(λE.GH)Y,X]=GH\\ E\not\in\operatorname{FV}[G]\landE\in\operatorname{FV}[H]\to\operatorname{sink}[(λE.GH)Y,X]=\operatorname{sink-test}[G\operatorname{sink-test}[(λE.H)Y,X]]\\ E\in\operatorname{FV}[G]\landE\not\in\operatorname{FV}[H]\to\operatorname{sink}[(λE.GH)Y,X]=(\operatorname{sink-test}[(λE.G)Y,X])H\\ E\in\operatorname{FV}[G]\landE\in\operatorname{FV}[H]\to\operatorname{sink}[(λE.GH)Y,X]=(λE.GH)Y\end{cases}

Abstraction. Use renaming to insure that the variable names are all distinct.

V\neW\to\operatorname{sink}[(λV.λW.E)Y,X]=λW.\operatorname{sink-test}[(λV.E)Y,X]

Variable - 2 cases.

E\neV\to\operatorname{sink}[(λE.V)Y,X]=V

E=V\to\operatorname{sink}[(λE.V)Y,X]=Y

Sink test excludes expressions from dropping,

L\inX\to\operatorname{sink-test}[L,X]=L

L\not\inX\to\operatorname{sink-test}[L,X]=\operatorname{sink}[L,X]

Example

Parameter dropping

Parameter dropping is optimizing a function for its position in the function. Lambda lifting added parameters that were necessary so that a function can be moved out of its context. In dropping, this process is reversed, and extra parameters that contain variables that are free may be removed.

Dropping a parameter is removing an unnecessary parameter from a function, where the actual parameter being passed in is always the same expression. The free variables of the expression must also be free where the function is defined. In this case the parameter that is dropped is replaced by the expression in the body of the function definition. This makes the parameter unnecessary.

For example, consider,

λm,p,q.(λg.λn.(n(gmpn)(gqpn))) λx.λo.λy.oxy

In this example the actual parameter for the formal parameter o is always p. As p is a free variable in the whole expression, the parameter may be dropped. The actual parameter for the formal parameter y is always n. However n is bound in a lambda abstraction. So this parameter may not be dropped.

The result of dropping the parameter is,

\operatorname{drop-params-tran}[λm,p,q.(λg.λn.n(gmpn)(gqpn)) λx.λo.λy.oxy

\equivλm,p,q.(λg.λn.n(gmn)(gqn)) λx.λy.pxy

For the main example,

\operatorname{drop-params-tran}[λf.(λp.(pf)(pf))(λf.λx.f(xx))]

\equivλf.(λp.pp)(λx.f(xx))

The definition of drop-params-tran is,

\operatorname{drop-params-tran}[L]\equiv(\operatorname{drop-params}[L,D,FV[L],[]])

where,

\operatorname{build-param-list}[L,D,V,\]

Build parameter lists

For each abstraction that defines a function, build the information required to make decisions on dropping names. This information describes each parameter; the parameter name, the expression for the actual value, and an indication that all the expressions have the same value.

For example, in,

λm,p,q.(λg.λn.(n(gmpn)(gqpn))) λx.λo.λy.oxy

the parameters to the function g are,

Formal Parameter All Same Value Actual parameter expression
x _
o p
y n

Each abstraction is renamed with a unique name, and the parameter list is associated with the name of the abstraction. For example, g there is parameter list.

D[g]=[[x,\operatorname{false},\],[o,\,p],[y,\,n]]

build-param-lists builds all the lists for an expression, by traversing the expression. It has four parameters;

Abstraction - A lambda expression of the form

(λN.S)L

is analyzed to extract the names of parameters for the function.

\begin{cases}\operatorname{build-param-lists}[(λN.S)L,D,V,R]\equiv\operatorname{build-param-lists}[S,D,V,R]\land\operatorname{build-list}[L,D,V,D[N]]\\ \operatorname{build-param-lists}[λN.S,D,V,R]\equiv\operatorname{build-param-lists}[S,D,V,R]\end{cases}

Locate the name and start building the parameter list for the name, filling in the formal parameter names. Also receive any actual parameter list from the body of the expression, and return it as the actual parameter list from this expression

\begin{cases} \operatorname{build-list}[λP.B,D,V,[X,\,\]::L]\equiv\operatorname{build-list}[B,D,V,L]\\ \operatorname{build-list}[B,D,V,[]]\equiv\operatorname{build-param-lists}[B,D,V,\]\end{cases}

Variable - A call to a function.

\operatorname{build-param-lists}[N,D,V,D[N]]

For a function name or parameter start populating actual parameter list by outputting the parameter list for this name.

Application - An application (function call) is processed to extract actual parameter details.

\operatorname{build-param-lists}[EP,D,V,R]\equiv\operatorname{build-param-lists}[E,D,V,T]\land\operatorname{build-param-lists}[P,D,V,K]

\landT=[F,S,A]::R\land(S\implies(\operatorname{equate}[A,P]\landV[F]=A))\landD[F]=K

Retrieve the parameter lists for the expression, and the parameter. Retrieve a parameter record from the parameter list from the expression, and check that the current parameter value matches this parameter. Record the value for the parameter name for use later in checking.

\begin{cases}\operatorname{equate}[A,N]\equivA=N\lor(\operatorname{def}[V[N]]\landA=V[N])&ifNisavariable.\\ \operatorname{equate}[A,E]\equivA=E&otherwise. \end{cases}

The above logic is quite subtle in the way that it works. The same value indicator is never set to true. It is only set to false if all the values cannot be matched. The value is retrieved by using S to build a set of the Boolean values allowed for S. If true is a member then all the values for this parameter are equal, and the parameter may be dropped.

\operatorname{ask}[S]\equivS\in\{X:X=S\}

Similarly, def uses set theory to query if a variable has been given a value;

\operatorname{def}[F]\equiv|\{X:X=F\}|

Let - Let expression.

\operatorname{build-param-list}[\operatorname{let}V:E\operatorname{in}L,D,V,\]\equiv\operatorname{build-param-list}[E,D,V,\]\land\operatorname{build-param-list}[L,D,V,\]

And - For use in "let".

\operatorname{build-param-lists}[E\landF,D,V,\]\equiv\operatorname{build-param-lists}[E,D,V,\]\land\operatorname{build-param-lists}[F,D,V,\]

Examples

For example, building the parameter lists for,

λm,p,q.(λg.λn.(n(gmpn)(gqpn))) λx.λo.λy.oxy

gives,

D[g]=[[x,\operatorname{false},\],[o,\operatorname{true},p],[y,\operatorname{true},n]]

and the parameter o is dropped to give,

λm,p,q.(λg.λn.(n(gmn)(gqn))) λx.λy.pxy

Another example is,

λf.((λp.f(ppf))(λq.λx.x(qqx))

Here x is equal to f. The parameter list mapping is,

D[p]=[[q,\,p],[x,\,f]]

and the parameter x is dropped to give,

λf.((λq.f(qq))(λq.f(qq))

Drop parameters

Use the information obtained by Build parameter lists to drop actual parameters that are no longer required. drop-params has the parameters,

Abstraction

\operatorname{drop-params}[(λN.S)L,D,V,R]\equiv(λN.\operatorname{drop-params}[S,D,F,R])\operatorname{drop-formal}[D[N],L,F]

where,

F=FV[(λN.S)L]

\operatorname{drop-params}[λN.S,D,V,R]\equiv(λN.\operatorname{drop-params}[S,D,F,R])

where,

F=FV[λN.S]

Variable

\operatorname{drop-params}[N,D,V,D[N]]\equivN

For a function name or parameter start populating actual parameter list by outputting the parameter list for this name.

Application - An application (function call) is processed to extract

(\operatorname{def}[F]\land\operatorname{ask}[S]\landFV[A]\subsetV)\to\operatorname{drop-params}[EP,D,V,R]\equiv\operatorname{drop-params}[E,D,V,[F,S,A]::R]

\neg(\operatorname{def}[F]\land\operatorname{ask}[S]\landFV[A]\subsetV)\to\operatorname{drop-params}[EP,D,V,R]\equiv\operatorname{drop-params}[E,D,V,[F,S,A]::R]\operatorname{drop-params}[P,D,V,\]

Let - Let expression.

\operatorname{drop-params}[\operatorname{let}V:E\operatorname{in}L]\equiv\operatorname{let}V:\operatorname{drop-params}[E,D,FV[E],[]]\operatorname{in}\operatorname{drop-params}[L,D,FV[L],[]]

And - For use in "let".

\operatorname{drop-params}[E\landF,D,V,\]\equiv\operatorname{drop-params}[E,D,V,\]\land\operatorname{drop-params}[F,D,V,\]

Drop formal parameters

drop-formal removes formal parameters, based on the contents of the drop lists. Its parameters are,

drop-formal is defined as,

(\operatorname{ask}[S]\landFV[A]\subsetV)\to\operatorname{drop-formal}[[F,S,A]::Z,λF.Y,V]\equiv\operatorname{drop-formal}[[F,S,A]::Z,Y[F:=A],L]

\neg(\operatorname{ask}[S]\landFV[A]\subsetV)\to\operatorname{drop-formal}[[F,S,A]::Z,λF.Y,V]\equivλF.\operatorname{drop-formal}[[F,S,A]::Z,Y,V]

\operatorname{drop-formal}[Z,Y,V]\equivY

Which can be explained as,

  1. If all the actual parameters have the same value, and all the free variables of that value are available for definition of the function then drop the parameter, and replace the old parameter with its value.
  2. else do not drop the parameter.
  3. else return the body of the function.
Condition Expression

\operatorname{false}

\operatorname{drop-formal}[D,λx.λo.λy.oxy,F]

\operatorname{true}\land\{p\}\subsetF

λx.\operatorname{drop-formal}[D,λo.λy.oxy,F]

\neg(\operatorname{true}\land\{n\}\subsetF

)

λx.\operatorname{drop-formal}[D,(λy.oxy)[o:=p],F]

λx.λy.\operatorname{drop-formal}[D,pxy,F]

λx.λy.pxy

Example

Starting with the function definition of the Y-combinator,

\operatorname{let}pfx=f(xx)\landqpf=(pf)(pf)\operatorname{in}qp

Transformation Expression

\operatorname{let}pfx=f(xx)\landqpf=(pf)(pf)\operatorname{in}qp

abstract * 4

\operatorname{let}p=λf.λx.f(xx)\landq=λp.λf.(pf)(pf)\operatorname{in}qp

lambda-abstract-tran

(λq.(λp.qp)(λf.λx.f(xx)))(λp.λf.(pf)(pf))

sink-tran

(λp.λf.(pf)(pf))(λf.λx.f(xx))

sink-tran

λf.(λp.(pf)(pf))(λf.λx.f(xx))

drop-param

λf.(λp.pp)(λx.f(xx))

beta-redex

λf.(λx.f(xx))(λx.f(xx))

Which gives back the Y combinator,

λf.(λx.f(xx))(λx.f(xx))

See also

External links

Notes and References

  1. Johnsson . Thomas . Lambda Lifting: Transforming Programs to Recursive Equations . 10.1.1.48.4346 . 3-540-15975-4 . 10.1007/3-540-15975-4_37 . Jouannaud . J.P. . Lecture Notes in Computer Science . 201 . Functional Programming Languages and Computer Architecture. FPCA 1985 . Springer . 1985.
  2. Danvy . O. . Schultz . U. P. . 10.1145/258994.259007 . Lambda-dropping . ACM SIGPLAN Notices . 32 . 12 . 90 . 1997 . free .
  3. Olivier . Danvy . Ulrik P. . Schultz . Lambda-Dropping: Transforming Recursive Equations into Programs with Block Structure . Theoretical Computer Science . 248 . 1–2 . 243–287 . October 2000. 10.1016/S0304-3975(00)00054-2 . 10.1.1.16.3943 . BRICS-RS-99-27.