Lambda lifting

From HandWiki
Short description: Globalization meta-process

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;

  • Eliminating free variables in the function by adding parameters.
  • Moving functions from a restricted scope to broader or global scope.

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 [math]\displaystyle{ O(n^2) }[/math] on processing time for the compiler.[2]

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.[3]

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)) in
sum 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)) in
sum 100

Next, lift f into a global function:

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

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

// Initial version

function sum(n) {
    function f(x) {
        return n + x;
    }

    if (n == 1)
        return 1;
    else
        return f(sum(n - 1));
}

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

function sum(n) {
    function f(w, x) {
        return w + x;
    }

    if (n == 1)
        return 1;
    else
        return f(n, sum(n - 1));
}

// After lifting function f into the global scope

function f(w, x) {
    return w + x;
}

function sum(n) {
    if (n == 1)
        return 1;
    else
        return f(n, sum(n - 1));
}

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 [math]\displaystyle{ f\ x = y \equiv f = \lambda x.y }[/math]
Let-Lambda equivalence [math]\displaystyle{ f \notin FV(E) \to (\operatorname{let} f : f = E \operatorname{in} L \equiv (\lambda f.L)\ E) \ \text{(where }f\text{ is a variable name.)} }[/math]
Let combination [math]\displaystyle{ x \notin FV(E) \to (\operatorname{let} v, \dots, w, x : E \land F \operatorname{in} L \equiv \operatorname{let} v, \dots, w: E \operatorname{in} \operatorname{let} x : F \operatorname{in} L) }[/math]

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,

  • Square brackets [] will be used to represent function application in the meta program.
  • Capital letters will be used for variables in the meta program. Lower case letters represent variables in the program.
  • [math]\displaystyle{ \equiv }[/math] will be used for equals in the meta program.
  • [math]\displaystyle{ \_ }[/math] 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 [math]\displaystyle{ L[G := S] }[/math] 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,

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

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

Two types of lift are possible.

  • Anonymous lift
  • Named lift

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;

  • Create a name for the function that will replace S (called V). Make sure that the name identified by V has not been used.
  • Add parameters to V, for all the free variables in S, to create an expression G (see make-call).

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.

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

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,

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

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

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

where [math]\displaystyle{ \operatorname{vars}[E] }[/math] is a meta function that returns the set of variables used in E.

Example for anonymous lift.
For example,
[math]\displaystyle{ \begin{align} F &= \operatorname{true} \\ L &= \lambda f.(\lambda x.f\ (x\ x))\ (\lambda x.f\ (x\ x)) \\ S &= \lambda x.f\ (x\ x) \\ G &= p\ f \end{align} }[/math]
[math]\displaystyle{ \operatorname{de-lambda}[p\ f = \lambda x.f\ (x\ x)] \equiv p\ f\ x = f \ (x\ x) }[/math]

See de-lambda in Conversion from lambda to let expressions. The result is,

[math]\displaystyle{ \operatorname{lambda-lift}[\lambda x.f\ (x\ x), \operatorname{let} \operatorname{true} \operatorname{in} \lambda f.(\lambda x.f\ (x\ x))\ (\lambda x.f\ (x\ x))] \equiv \operatorname{let} p\ f\ x = f\ (x\ x) \operatorname{in} \lambda f.(p\ f)\ (p\ f) }[/math]
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,

  • [math]\displaystyle{ X \in V \to \operatorname{make-call}[H, V] \equiv \operatorname{make-call}[H, V \cap \neg \{X\}]\ X }[/math]
  • [math]\displaystyle{ \operatorname{make-call}[H, \{\}] \equiv H }[/math]
Example of call construction.
[math]\displaystyle{ S = \lambda x.f\ (x\ x) }[/math]
[math]\displaystyle{ \operatorname{FV}(S) = \{f\} }[/math]
[math]\displaystyle{ G \equiv \operatorname{make-call}[p, \operatorname{FV}[S]] \equiv \operatorname{make-call}[p, \{f\}] \equiv \operatorname{make-call}[p, \{\}]\ f \equiv p\ f }[/math]

Named lift

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

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

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

[math]\displaystyle{ G = \operatorname{make-call}[V, \operatorname{FV}[S]] }[/math]
Example for named lift.

For example,

[math]\displaystyle{ \begin{align} V &= x \\ E &= f\ (x\ x) \\ S &= (\lambda x.f\ (x\ x)) \\ L &= \lambda f.(\lambda x.f\ (x\ x))\ (\lambda x.f\ (x\ x)) \\ G &= x\ f \end{align} }[/math]
[math]\displaystyle{ E[V:=G] = f\ (x\ x)[x := x\ f] = f\ ((x\ f)\ (x\ f)) }[/math]
[math]\displaystyle{ L[(\lambda V.E)\ F:=E[V:=G]] = L[(\lambda x.f\ (x\ x))\ (\lambda x.f\ (x\ x)) := f\ ((x\ f)\ (x\ f))] = \lambda f.f\ ((x\ f)\ (x\ f)) }[/math]
[math]\displaystyle{ \operatorname{de-lambda}[x\ f = \lambda y.f\ (y\ y)] \equiv x\ f\ y = f \ (y\ y) }[/math]

See de-lambda in Conversion from lambda to let expressions. The result is,

gives,

[math]\displaystyle{ \operatorname{lambda-lift}[(\lambda x.f\ (x\ x))\ (\lambda x.f\ (x\ x)), \lambda f.(\lambda x.f\ (x\ x))\ (\lambda x.f\ (x\ x))] \equiv \operatorname{let} x\ f\ y = f \ (y\ y) \operatorname{in} \lambda f.(x\ f)\ (x\ f) }[/math]

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,

  • [math]\displaystyle{ \operatorname{let} M \operatorname{in} N }[/math]

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

For example,

[math]\displaystyle{ \operatorname{lambda-lift-tran}[\lambda f.(\lambda x.f\ (x\ x))\ (\lambda x.f\ (x\ x))] \equiv \operatorname{let} p\ f\ x = f\ (x\ x) \land q\ p\ f = (p\ f)\ (p\ f) \operatorname{in} q\ p }[/math]

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

[math]\displaystyle{ \operatorname{de-let}[\operatorname{lambda-lift-tran}[\lambda f.(\lambda x.f\ (x\ x))\ (\lambda x.f\ (x\ x))]] \equiv (\lambda p.(\lambda q.q\ p)\ \lambda p.\lambda f.(p\ f)\ (p\ f))\ \lambda f.\lambda x.f\ (x\ x) }[/math]

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

  • A sub expression chosen for it by the function lift-choice. The sub expression should be chosen so that it may be converted into an equation with no lambdas.
  • The lift is performed by a call to the lambda-lift meta function, described in the next section,
[math]\displaystyle{ \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} }[/math]

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

[math]\displaystyle{ \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 \land F \operatorname{in} G] \\ \operatorname{merge-let}[E] = E \end{cases} }[/math]

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.

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

lambda-anon - An anonymous function. An expression like [math]\displaystyle{ \lambda x_1.\ ...\ \lambda x_n.X }[/math] where X is lambda free.

[math]\displaystyle{ \begin{cases} \operatorname{lambda-anon}[\lambda F.X] = \operatorname{lambda-free}[X] \lor \operatorname{lambda-anon}[X] \\ \operatorname{lambda-anon}[V] = \operatorname{false} \\ \operatorname{lambda-anon}[M\ N] = \operatorname{false} \end{cases} }[/math]
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.

  1. [math]\displaystyle{ \operatorname{lambda-anon}[X] \to \operatorname{lift-choice}[X] = X }[/math]
  2. [math]\displaystyle{ \operatorname{lift-choice}[\lambda F.X] = \operatorname{lift-choice}[X] }[/math]
  3. [math]\displaystyle{ \operatorname{lift-choice}[M] \ne \operatorname{none} \to \operatorname{lift-choice}[M\ N] = \operatorname{lift-choice}[M] }[/math]
  4. [math]\displaystyle{ \operatorname{lift-choice}[M\ N] = \operatorname{lift-choice}[N] }[/math]
  5. [math]\displaystyle{ \operatorname{lift-choice}[V] = \operatorname{none} }[/math]

For example,

Lambda choice on [math]\displaystyle{ \lambda f.(\lambda x.f\ (x\ x))\ (\lambda y.f\ (y\ y)) }[/math] is [math]\displaystyle{ \lambda x.f\ (x\ x) }[/math]
Rule function type choice
2 [math]\displaystyle{ \operatorname{lift-choice}[\lambda f.(\lambda x.f\ (x\ x))\ (\lambda y.f\ (y\ y))] }[/math]
3 [math]\displaystyle{ \operatorname{lift-choice}[(\lambda x.f\ (x\ x))\ (\lambda y.f\ (y\ y))] }[/math]
1 anon [math]\displaystyle{ \operatorname{lift-choice}[\lambda x.f\ (x\ x)] }[/math]
[math]\displaystyle{ \lambda x.f\ (x\ x) }[/math]
Lambda choice on [math]\displaystyle{ \lambda f.(p\ f)\ (p\ f) }[/math] is [math]\displaystyle{ \lambda f.(p\ f)\ (p\ f) }[/math]
Rule function type choice
2 anon [math]\displaystyle{ \operatorname{lift-choice}[\lambda f.(p\ f)\ (p\ f)] }[/math]
2 [math]\displaystyle{ \lambda f.(p\ f)\ (p\ f) }[/math]
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 [math]\displaystyle{ (\lambda F.M)\ N }[/math] where M is lambda free and N is lambda free or an anonymous function.
[math]\displaystyle{ \begin{array}{l} \operatorname{lambda-named}[(\lambda F.M)\ N] = \operatorname{lambda-free}[M] \land \operatorname{lambda-anon}[N] \\ \operatorname{lambda-named}[\lambda F.X] = \operatorname{false} \\ \operatorname{lambda-named}[V] = \operatorname{false} \end{array} }[/math]
lift-choice
The first anonymous or named function found in traversing the expression or none if there is no function.
  1. [math]\displaystyle{ \operatorname{lambda-named}[X] \lor \operatorname{lambda-anon}[X] \to \operatorname{lift-choice}[X] = X }[/math]
  2. [math]\displaystyle{ \operatorname{lift-choice}[\lambda F.X] = \operatorname{lift-choice}[X] }[/math]
  3. [math]\displaystyle{ \operatorname{lift-choice}[M] \ne \operatorname{none} \to \operatorname{lift-choice}[M\ N] = \operatorname{lift-choice}[M] }[/math]
  4. [math]\displaystyle{ \operatorname{lift-choice}[M\ N] = \operatorname{lift-choice}[N] }[/math]
  5. [math]\displaystyle{ \operatorname{lift-choice}[V] = \operatorname{none} }[/math]

For example,

Lambda choice on [math]\displaystyle{ \lambda f.(\lambda x.f\ (x\ x))\ (\lambda y.f\ (y\ y)) }[/math] is [math]\displaystyle{ (\lambda x.f\ (x\ x))\ (\lambda y.f\ (y\ y)) }[/math]
Rule function type choice
2 [math]\displaystyle{ \operatorname{lift-choice}[\lambda f.(\lambda x.f\ (x\ x))\ (\lambda y.f\ (y\ y))] }[/math]
1 named [math]\displaystyle{ \operatorname{lift-choice}[(\lambda x.f\ (x\ x))\ (\lambda y.f\ (y\ y))] }[/math]
[math]\displaystyle{ (\lambda x.f\ (x\ x))\ (\lambda y.f\ (y\ y)) }[/math]
Lambda choice on [math]\displaystyle{ \lambda f.f\ ((x\ f)\ (x\ f)) }[/math] is [math]\displaystyle{ \lambda f.f\ ((x\ f)\ (x\ f)) }[/math]
Rule function type choice
1 anon [math]\displaystyle{ \operatorname{lift-choice}[\lambda f.f\ ((x\ f)\ (x\ f))] }[/math]
[math]\displaystyle{ \lambda f.f\ ((x\ f)\ (x\ f)) }[/math]

Examples

For example, the Y combinator,

[math]\displaystyle{ \lambda f.(\lambda x.f\ (x\ x))\ (\lambda x.f\ (x\ x)) }[/math]

is lifted as,

[math]\displaystyle{ \operatorname{let} x\ f\ y = f\ (y\ y) \land q\ x\ f = f\ ((x\ f)\ (x\ f)) \operatorname{in} q\ x }[/math]

and after Parameter dropping,

[math]\displaystyle{ \operatorname{let} x\ f\ y = f\ (y\ y) \land q\ f = f\ ((x\ f)\ (x\ f)) \operatorname{in} q }[/math]

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

[math]\displaystyle{ (\lambda x.(\lambda q.q)\ \lambda f.f\ (x\ f)\ (x\ f))\ \lambda f.\lambda y.f\ (y\ y) }[/math]
Lifting named and anonymous functions
Lambda Expression Function From To Variables
1 [math]\displaystyle{ \lambda f.(\lambda x.f\ (x\ x))\ (\lambda x.f\ (x\ x)) }[/math] true [math]\displaystyle{ (\lambda x.f\ (x\ x))\ (\lambda x.f\ (x\ x)) }[/math] [math]\displaystyle{ x\ f }[/math] [math]\displaystyle{ \{x, f\} }[/math]
2

[math]\displaystyle{ (\lambda f.(\lambda x.f\ (x\ x)) (\lambda x.f\ (x\ x))) }[/math]

[math]\displaystyle{ [(\lambda x.f\ (x\ x))\ (\lambda x.f\ (x\ x)) := f\ ((x\ f)\ (x\ f))] }[/math]
[math]\displaystyle{ x\ f = \lambda y.f\ (y\ y) }[/math] [math]\displaystyle{ \{x, f, p\} }[/math]
3 [math]\displaystyle{ \lambda f.f\ ((x\ f)\ (x\ f)) }[/math] [math]\displaystyle{ x\ f\ y = f\ (y\ y) }[/math] [math]\displaystyle{ \lambda f.f\ ((x\ f)\ (x\ f)) }[/math] [math]\displaystyle{ q\ x }[/math] [math]\displaystyle{ \{x, f, p\} }[/math]
4 [math]\displaystyle{ \lambda f.f\ ((x\ f)\ (x\ f))[\lambda f.f\ ((x\ f)\ (x\ f)) := q\ x] }[/math] [math]\displaystyle{ x\ f\ y = f\ (y\ y) \land q\ x = \lambda f.f\ ((x\ f)\ (x\ f)) }[/math] [math]\displaystyle{ \{x, f, p, q\} }[/math]
5 [math]\displaystyle{ q\ x }[/math] [math]\displaystyle{ x\ f\ y = f\ (y\ y) \land q\ x\ f = f\ ((x\ f)\ (x\ f)) }[/math] [math]\displaystyle{ \{x, f, p, q\} }[/math]

If lifting anonymous functions only, the Y combinator is,

[math]\displaystyle{ \operatorname{let} p\ f\ x = f\ (x\ x) \land q\ p\ f = (p\ f)\ (p\ f) \operatorname{in} q\ p }[/math]

and after Parameter dropping,

[math]\displaystyle{ \operatorname{let} p\ f\ x = f\ (x\ x) \land q\ f = (p\ f)\ (p\ f) \operatorname{in} q }[/math]

As a lambda expression,

[math]\displaystyle{ (\lambda p.(\lambda q.q)\ \lambda f.(p\ f)\ (p\ f))\ \lambda f.\lambda x.f\ (x\ x) }[/math]
Lifting anonymous functions only
Lambda Expression Function From To Variables
1 [math]\displaystyle{ \lambda f.(\lambda x.f\ (x\ x))\ (\lambda x.f\ (x\ x)) }[/math] true [math]\displaystyle{ \lambda x.f\ (x\ x) }[/math] [math]\displaystyle{ p\ f }[/math] [math]\displaystyle{ \{x, f\} }[/math]
2 [math]\displaystyle{ (\lambda f.(\lambda x.f\ (x\ x)) (\lambda x.f\ (x\ x)))[\lambda x.f\ (x\ x) := p\ f] }[/math] [math]\displaystyle{ p\ f = \lambda x.f\ (x\ x) }[/math] [math]\displaystyle{ \{x, f, p\} }[/math]
3 [math]\displaystyle{ \lambda f.(p\ f)\ (p\ f) }[/math] [math]\displaystyle{ p\ f\ x = f\ (x\ x) }[/math] [math]\displaystyle{ \lambda f.(p\ f)\ (p\ f) }[/math] [math]\displaystyle{ q\ p }[/math] [math]\displaystyle{ \{x, f, p\} }[/math]
4 [math]\displaystyle{ \lambda f.(p\ f)\ (p\ f)[\lambda f.(p\ f)\ (p\ f) := q\ p] }[/math] [math]\displaystyle{ p\ f\ x = f\ (x\ x) \land q\ p = \lambda f.(p\ f)\ (p\ f) }[/math] [math]\displaystyle{ \{x, f, p, q\} }[/math]
5 [math]\displaystyle{ q\ p }[/math] [math]\displaystyle{ p\ f\ x = f\ (x\ x) \land q\ p\ f = (p\ f)\ (p\ f) }[/math] [math]\displaystyle{ \{x, f, p, q\} }[/math]

The first sub expression to be chosen for lifting is [math]\displaystyle{ \lambda x.f\ (x\ x) }[/math]. This transforms the lambda expression into [math]\displaystyle{ \lambda f.(p\ f)\ (p\ f) }[/math] and creates the equation [math]\displaystyle{ p\ f\ x = f (x\ x) }[/math].

The second sub expression to be chosen for lifting is [math]\displaystyle{ \lambda f.(p\ f)\ (p\ f) }[/math]. This transforms the lambda expression into [math]\displaystyle{ q\ p }[/math] and creates the equation [math]\displaystyle{ q\ p\ f = (p\ f)\ (p\ f) }[/math].

And the result is,

[math]\displaystyle{ \operatorname{let} p\ f\ x = f\ (x\ x) \land q\ p\ f = (p\ f)\ (p\ f) \operatorname{in} q\ p\ }[/math]

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

Execution

Apply function to K,

[math]\displaystyle{ \begin{cases} \lambda f.(\lambda x.f\ (x\ x))\ (\lambda x.f\ (x\ x))\ K & \ \operatorname{let}\ p\ f\ x = f\ (x\ x) \land q\ p\ f = (p\ f)\ (p\ f) \ \operatorname{in}\ q\ p\ K \\ (\lambda x.K\ (x\ x))\ (\lambda x.K\ (x\ x)) & \ \operatorname{let}\ p\ f\ x = f\ (x\ x) \land q\ p\ f = (p\ f)\ (p\ f) \ \operatorname{in}\ p\ K\ (p\ K) \\ K\ ((\lambda x.K\ (x\ x))\ (\lambda x.K\ (x\ x))) & \ \operatorname{let}\ p\ f\ x = f\ (x\ x) \land q\ p\ f = p\ f\ (p\ f) \ \operatorname{in}\ K\ (p\ K\ (p\ K)) \\ \end{cases} }[/math]

So,

[math]\displaystyle{ (\lambda x.K\ (x\ x))\ (\lambda x.K\ (x\ x)) = K\ ((\lambda x.K\ (x\ x))\ (\lambda x.K\ (x\ x))))\ }[/math]

or

[math]\displaystyle{ p\ K\ (p\ K) = K\ (p\ K\ (p\ K)) }[/math]

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[4] 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,

  • Sinking
  • Parameter dropping

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.

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

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,

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

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,

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

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.

[math]\displaystyle{ \begin{cases} E \not \in \operatorname{FV}[G] \land E \not \in \operatorname{FV}[H] \to \operatorname{sink}[(\lambda E.G\ H)\ Y, X] = G\ H \\ E \not \in \operatorname{FV}[G] \land E \in \operatorname{FV}[H] \to \operatorname{sink}[(\lambda E.G\ H)\ Y, X] = \operatorname{sink-test}[G\ \operatorname{sink-test}[(\lambda E.H)\ Y, X]] \\ E \in \operatorname{FV}[G] \land E \not \in \operatorname{FV}[H] \to \operatorname{sink}[(\lambda E.G\ H)\ Y, X] = (\operatorname{sink-test}[(\lambda E.G)\ Y, X])\ H \\ E \in \operatorname{FV}[G] \land E \in \operatorname{FV}[H] \to \operatorname{sink}[(\lambda E.G\ H)\ Y, X] = (\lambda E.G\ H)\ Y \end{cases} }[/math]

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

[math]\displaystyle{ V \ne W \to \operatorname{sink}[(\lambda V.\lambda W.E)\ Y, X] = \lambda W.\operatorname{sink-test}[(\lambda V.E)\ Y, X] }[/math]

Variable - 2 cases.

[math]\displaystyle{ E \ne V \to \operatorname{sink}[(\lambda E.V)\ Y, X] = V }[/math]
[math]\displaystyle{ E = V \to \operatorname{sink}[(\lambda E.V)\ Y, X] = Y }[/math]

Sink test excludes expressions from dropping,

[math]\displaystyle{ L \in X \to \operatorname{sink-test}[L, X] = L }[/math]
[math]\displaystyle{ L \not \in X \to \operatorname{sink-test}[L, X] = \operatorname{sink}[L, X] }[/math]

Example

Example of sinking

For example,

Rule Expression
de-let [math]\displaystyle{ \operatorname{sink-tran}[\operatorname{de-let}[\operatorname{let} p\ f\ x = f\ (x\ x) \land q\ p\ f = (p\ f)\ (p\ f) \operatorname{in} q\ p]] }[/math]
sink-tran [math]\displaystyle{ \operatorname{sink-tran}[(\lambda p.(\lambda q.q\ p)\ (\lambda p. \lambda f.(p\ f)\ (p\ f)))\ (\lambda f.\lambda x.f\ (x\ x))] }[/math]
Application
[math]\displaystyle{ \operatorname{sink}[(\lambda p.\operatorname{sink}[(\lambda q.q\ p)\ (\lambda p. \lambda f.(p\ f)\ (p\ f))])\ (\lambda f.\lambda x.f\ (x\ x))] }[/math]
[math]\displaystyle{ \operatorname{sink}[(\lambda q.q\ p)\ (\lambda p. \lambda f.(p\ f)\ (p\ f))] }[/math]
[math]\displaystyle{ E \in \operatorname{FV}[G] \land E \not \in \operatorname{FV}[H] \to \operatorname{sink}[(\lambda E.G\ H)\ Y, X] }[/math]
[math]\displaystyle{ E = q, G = q, H = p, Y = (\lambda p. \lambda f.(p\ f)\ (p\ f)), X = \{\} }[/math]
[math]\displaystyle{ (\operatorname{sink}[(\lambda E.G)\ Y, X])\ H }[/math]
[math]\displaystyle{ (\operatorname{sink}[(\lambda q.q)\ (\lambda p. \lambda f.(p\ f)\ (p\ f)), X])\ p }[/math]
Variable
[math]\displaystyle{ \operatorname{sink}[(\lambda p.\operatorname{sink}[(\lambda q.q)\ (\lambda p. \lambda f.(p\ f)\ (p\ f))]\ p)\ (\lambda f.\lambda x.f\ (x\ x))] }[/math]
[math]\displaystyle{ \operatorname{sink}[(\lambda q.q)\ (\lambda p. \lambda f.(p\ f)\ (p\ f))] }[/math]
[math]\displaystyle{ E = V \to \operatorname{sink}[(\lambda E.V)\ Y, X] }[/math]
[math]\displaystyle{ E = q, V = q, Y = (\lambda p. \lambda f.(p\ f)\ (p\ f)), X = \{\} }[/math]
[math]\displaystyle{ Y }[/math]
[math]\displaystyle{ (\lambda p. \lambda f.(p\ f)\ (p\ f)) }[/math]
Application
[math]\displaystyle{ \operatorname{sink}[(\lambda p.(\lambda p.\lambda f.(p\ f)\ (p\ f))\ p)\ (\lambda f.\lambda x.f\ (x\ x))] }[/math]
[math]\displaystyle{ E \not \in \operatorname{FV}[G] \land E \in \operatorname{FV}[H] \to \operatorname{sink}[(\lambda E.G\ H)\ Y, X] }[/math]
[math]\displaystyle{ E = p, G = (\lambda p.\lambda f.(p\ f)\ (p\ f)), H = p, Y = (\lambda f.\lambda x.f\ (x\ x)) }[/math]
[math]\displaystyle{ \operatorname{sink}[G\ \operatorname{sink}[(\lambda E.H)\ Y, X]] }[/math]
Variable
[math]\displaystyle{ \operatorname{sink}[(\lambda p.\lambda f.(p\ f)\ (p\ f))\ \operatorname{sink-test}[(\lambda p.p)\ (\lambda f.\lambda x.f\ (x\ x)), X]] }[/math]
[math]\displaystyle{ \operatorname{sink}[(\lambda p.p)\ (\lambda f.\lambda x.f\ (x\ x)), X] }[/math]
[math]\displaystyle{ E = V \to \operatorname{sink}[(\lambda E.V)\ Y, X] }[/math]
[math]\displaystyle{ E = p, V = p, Y = (\lambda f.\lambda x.f\ (x\ x)), X = \{\} }[/math]
[math]\displaystyle{ Y }[/math]
[math]\displaystyle{ (\lambda f.\lambda x.f\ (x\ x)) }[/math]
Abstraction
[math]\displaystyle{ \operatorname{sink}[(\lambda p.\lambda f.(p\ f)\ (p\ f))\ (\lambda f.\lambda x.f\ (x\ x))] }[/math]
[math]\displaystyle{ V \ne W \to \operatorname{sink}[(\lambda V.\lambda W.E)\ Y, X] }[/math]
[math]\displaystyle{ V = p, W = f, E = (p\ f)\ (p\ f), Y = (\lambda f.\lambda x.f\ (x\ x)) }[/math]
[math]\displaystyle{ \lambda W.\operatorname{sink}[(\lambda V.E)\ Y, X] }[/math]
Application
[math]\displaystyle{ \lambda f.\operatorname{sink}[(\lambda p.(p\ f)\ (p\ f))\ (\lambda f.\lambda x.f\ (x\ x)), X] }[/math]
[math]\displaystyle{ \operatorname{sink}[(\lambda p.(p\ f)\ (p\ f))\ (\lambda f.\lambda x.f\ (x\ x)), X] }[/math]
[math]\displaystyle{ E \in \operatorname{FV}[G] \land E \in \operatorname{FV}[H] \to \operatorname{sink}[(\lambda E.G\ H)\ Y, X] }[/math]
[math]\displaystyle{ E = p, G = (p\ f), H = (p\ f), Y = (\lambda f.\lambda x.f\ (x\ x)) }[/math]
[math]\displaystyle{ (\lambda E.G\ H)\ Y }[/math]
[math]\displaystyle{ (\lambda p.(p\ f)\ (p\ f))\ (\lambda f.\lambda x.f\ (x\ x)) }[/math]
[math]\displaystyle{ \lambda f.(\lambda p.(p\ f)\ (p\ f))\ (\lambda f.\lambda x.f\ (x\ x)) }[/math]

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,

[math]\displaystyle{ \lambda m,p,q.(\lambda g.\lambda n.(n\ (g\ m\ p\ n)\ (g\ q\ p\ n)))\ \lambda x.\lambda o.\lambda y.o\ x\ y }[/math]

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,

[math]\displaystyle{ \operatorname{drop-params-tran}[\lambda m,p,q.(\lambda g.\lambda n.n\ (g\ m\ p\ n)\ (g\ q\ p\ n))\ \lambda x.\lambda o.\lambda y.o\ x\ y }[/math]
[math]\displaystyle{ \equiv \lambda m,p,q.(\lambda g.\lambda n.n\ (g\ m\ n)\ (g\ q\ n))\ \lambda x.\lambda y.p\ x\ y }[/math]

For the main example,

[math]\displaystyle{ \operatorname{drop-params-tran}[ \lambda f.(\lambda p.(p\ f)\ (p\ f))\ (\lambda f.\lambda x.f\ (x\ x))] }[/math]
[math]\displaystyle{ \equiv \lambda f.(\lambda p.p\ p)\ (\lambda x.f\ (x\ x)) }[/math]

The definition of drop-params-tran is,

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

where,

[math]\displaystyle{ \operatorname{build-param-list}[L, D, V, \_] }[/math]

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,

[math]\displaystyle{ \lambda m,p,q.(\lambda g.\lambda n.(n\ (g\ m\ p\ n)\ (g\ q\ p\ n)))\ \lambda x.\lambda o.\lambda y.o\ x\ y }[/math]

the parameters to the function g are,

Formal Parameter All Same Value Actual parameter expression
x false _
o true p
y true 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.

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

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

  • The lambda expression being analyzed.
  • The table parameter lists for names.
  • The table of values for parameters.
  • The returned parameter list, which is used internally by the

Abstraction - A lambda expression of the form [math]\displaystyle{ (\lambda N.S)\ L }[/math] is analyzed to extract the names of parameters for the function.

[math]\displaystyle{ \begin{cases} \operatorname{build-param-lists}[(\lambda 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}[\lambda N.S, D, V, R] \equiv \operatorname{build-param-lists}[S, D, V, R] \end{cases} }[/math]

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

[math]\displaystyle{ \begin{cases} \operatorname{build-list}[\lambda 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} }[/math]

Variable - A call to a function.

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

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.

[math]\displaystyle{ \operatorname{build-param-lists}[E\ P, D, V, R] \equiv \operatorname{build-param-lists}[E, D, V, T] \land \operatorname{build-param-lists}[P, D, V, K] }[/math]
[math]\displaystyle{ \land T = [F, S, A]::R \land (S \implies (\operatorname{equate}[A, P] \land V[F] = A)) \land D[F] = K }[/math]

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.

[math]\displaystyle{ \begin{cases} \operatorname{equate}[A, N] \equiv A = N \lor (\operatorname{def}[V[N]] \land A = V[N]) & \text{if }N\text{ is a variable.} \\ \operatorname{equate}[A, E] \equiv A = E & \text{otherwise.} \end{cases} }[/math]

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.

[math]\displaystyle{ \operatorname{ask}[S] \equiv S \in \{ X : X = S \} }[/math]

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

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

Let - Let expression.

[math]\displaystyle{ \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, \_] }[/math]

And - For use in "let".

[math]\displaystyle{ \operatorname{build-param-lists}[E \land F, D, V, \_] \equiv \operatorname{build-param-lists}[E, D, V, \_] \land \operatorname{build-param-lists}[F, D, V, \_] }[/math]
Examples

For example, building the parameter lists for,

[math]\displaystyle{ \lambda m,p,q.(\lambda g.\lambda n.(n\ (g\ m\ p\ n)\ (g\ q\ p\ n)))\ \lambda x.\lambda o.\lambda y.o\ x\ y }[/math]

gives,

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

and the parameter o is dropped to give,

[math]\displaystyle{ \lambda m,p,q.(\lambda g.\lambda n.(n\ (g\ m\ n)\ (g\ q\ n)))\ \lambda x.\lambda y.p\ x\ y }[/math]
Build parameter list for [math]\displaystyle{ \lambda m,p,q.(\lambda g.\lambda n.(n\ (g\ m\ p\ n)\ (g\ q\ p\ n)))\ \lambda x.\lambda o.\lambda y.o\ x\ y }[/math]
Build parameter list example
[math]\displaystyle{ \operatorname{build-param-list}[\lambda m,p,q.(\lambda g.\lambda n.(n\ (g\ m\ p\ n)\ (g\ q\ p\ n)))\ \lambda x.\lambda o.\lambda y.o\ x\ y, D, V, \_] }[/math]
Rule Expression
Abstraction [math]\displaystyle{ \operatorname{build-param-list}[\lambda m,p,q.(\lambda g.\lambda n.(n\ (g\ m\ p\ n)\ (g\ q\ p\ n)))\ \lambda x.\lambda o.\lambda y.o\ x\ y, D, V, \_] }[/math]
Abstraction [math]\displaystyle{ \operatorname{build-param-list}[(\lambda g.\lambda n.(n\ (g\ m\ p\ n)\ (g\ q\ p\ n)))\ \lambda x.\lambda o.\lambda y.o\ x\ y, D, V, \_] }[/math]
[math]\displaystyle{ \operatorname{build-param-lists}[n\ (g\ m\ p\ n)\ (g\ q\ p\ n), D, V, R] \land \operatorname{build-list}[\lambda x.\lambda o.\lambda y.o\ x\ y, D, V, D[g]] }[/math]
[math]\displaystyle{ \operatorname{build-list}[\lambda x.\lambda o.\lambda y.o\ x\ y, D, V, D[g]] }[/math]
Rule Expression
Add param

[math]\displaystyle{ \operatorname{build-list}[\lambda x.\lambda o.\lambda y.o\ x\ y, D, V, D[g]] \land D[g] = L_1 }[/math]

Add param

[math]\displaystyle{ \operatorname{build-list}[\lambda o.\lambda y.o\ x\ y, D, V, L_1] \land D[g] = [x, \_, \_]::L_1 }[/math]

Add param

[math]\displaystyle{ \operatorname{build-list}[\lambda y.o\ x\ y, D, V, L_2] \land D[g] = [x, \_, \_]::[o, \_, \_]::L_2 }[/math]

End list [math]\displaystyle{ \operatorname{build-list}[o\ x\ y, D, V, L_3] \land D[g] = [x, \_, \_]::[o, \_, \_]::[y, \_, \_]::L_3 }[/math]
[math]\displaystyle{ \operatorname{build-param-lists}[o\ x\ y, D, V, []] \land D[g] = [x, \_, \_]::[o, \_, \_]::[y, \_, \_]::[] }[/math]
[math]\displaystyle{ \operatorname{build-param-lists}[n\ (g\ m\ p\ n)\ (g\ q\ p\ n), D, V, R] }[/math]
Rule Expression
Application [math]\displaystyle{ \operatorname{build-param-lists}[n\ (g\ m\ p\ n)\ (g\ q\ p\ n), D, V, R] }[/math]
Application

[math]\displaystyle{ \operatorname{build-param-lists}[n\ (g\ m\ p\ n), D, V, T_1] \land \operatorname{build-param-lists}[g\ q\ p\ n, D, V, K_1] }[/math]

[math]\displaystyle{ \land ((T_1 = [F_1, S_1, A_1]::R }[/math]
[math]\displaystyle{ \land (S_1 \implies (\operatorname{equate}[A_1, g\ q\ p\ n] \land V[F_1] = g\ q\ p\ n)) \land D[F_1] = K_1) }[/math]
Variable

[math]\displaystyle{ \operatorname{build-param-lists}[n, D, V, T_2] \land \operatorname{build-param-lists}[g\ m\ p\ n, D, V, K_2] \land \operatorname{build-param-lists}[g\ q\ p\ n, D, V, K_1] }[/math]

[math]\displaystyle{ \land ((T_2 = [F_2, S_2, A_2]::[F_1, S_1, A_1]::R }[/math]
[math]\displaystyle{ \land (S_1 \implies (\operatorname{equate}[A_1, g\ q\ p\ n] \land V[F_1] = g\ q\ p\ n)) \land D[F_1] = K_1) }[/math]
[math]\displaystyle{ \land (S_2 \implies (\operatorname{equate}[A_2, g\ m\ p\ n] \land V[F_2] = g\ m\ p\ n)) \land D[F_2] = K_2) }[/math]
Variable

[math]\displaystyle{ \operatorname{build-param-lists}[g\ m\ p\ n, D, V, K_2] \land \operatorname{build-param-lists}[g\ q\ p\ n, D, V, K_1] }[/math]

[math]\displaystyle{ \land ((D[n] = [F_2, S_2, A_2]::[F_1, S_1, A_1]::R }[/math]
[math]\displaystyle{ \land (S_1 \implies (\operatorname{equate}[A_1, g\ q\ p\ n] \land V[F_1] = g\ q\ p\ n)) \land D[F_1] = K_1) }[/math]
[math]\displaystyle{ \land (S_2 \implies (\operatorname{equate}[A_2, g\ m\ p\ n] \land V[F_2] = g\ m\ p\ n)) \land D[F_2] = K_2) }[/math]

Gives,

[math]\displaystyle{ D[n] = [\_, \_, g\ m\ p\ n]::[\_, \_, g\ q\ p\ n]::R }[/math]
[math]\displaystyle{ \operatorname{build-param-lists}[g\ m\ p\ n, D, V, K_2] }[/math]
Rule Expression
application

[math]\displaystyle{ \operatorname{build-param-lists}[g\ m\ p\ n, D, V, K_2] }[/math]

[math]\displaystyle{ \operatorname{build-param-lists}[g\ m\ p, D, V, T_3] \land \operatorname{build-param-lists}[n, D, V, K_3] }[/math]

[math]\displaystyle{ \land ((T_3 = [F_3, S_3, A_3]::K_2 }[/math]
[math]\displaystyle{ \land (S_3 \implies (\operatorname{equate}[A_3, n] \land V[F_3] = n)) \land D[F_3] = D[n]) }[/math]
application, Variable

[math]\displaystyle{ \operatorname{build-param-lists}[g\ m, D, V, T_4] \land \operatorname{build-param-lists}[p, D, V, K_4] }[/math]

[math]\displaystyle{ \land T_4 = [\_, S_4, A_4]::[\_, S_3, A_3]::K_2 }[/math]
[math]\displaystyle{ \land (S_3 \implies (\operatorname{equate}[A_3, n] \land V[F_3] = n)) \land D[F_3] = D[n]) }[/math]
[math]\displaystyle{ \land (S_4 \implies (\operatorname{equate}[A_4, p] \land V[F_4] = p)) \land D[F_4] = D[p] }[/math]
application, Variable

[math]\displaystyle{ \operatorname{build-param-lists}[g, D, V, T_5] \land \operatorname{build-param-lists}[m, D, V, K_5] }[/math]

[math]\displaystyle{ \land T_5 = [F_5, S_5, A_5]::[F_4, S_4, A_4]::[F_3, S_3, A_3]::K_2 }[/math]
[math]\displaystyle{ \land (S_3 \implies (\operatorname{equate}[A_3, n] \land V[F_3] = n)) \land D[F_3] = D[n]) }[/math]
[math]\displaystyle{ \land (S_4 \implies (\operatorname{equate}[A_4, p] \land V[F_4] = p)) \land D[F_4] = D[p] }[/math]
[math]\displaystyle{ \land (S_5 \implies (\operatorname{equate}[A_5, m] \land V[F_5] = m)) \land D[F_5] = D[m] }[/math]
Variable

[math]\displaystyle{ D[g] = [x, S_5, A_5]::[o, S_4, A_4]::[y, S_3, A_3]::K_2 }[/math]

[math]\displaystyle{ \land (S_3 \implies (\operatorname{equate}[A_3, n] \land V[y] = n)) \land D[y] = D[n]) }[/math]
[math]\displaystyle{ \land (S_4 \implies (\operatorname{equate}[A_4, p] \land V[o] = p)) \land D[o] = D[p] }[/math]
[math]\displaystyle{ \land (S_5 \implies (\operatorname{equate}[A_5, m] \land V[x] = m)) \land D[x] = D[m] }[/math]
[math]\displaystyle{ \operatorname{build-param-lists}[g\ q\ p\ n, D, V, K_1] }[/math]
Rule Expression
application

[math]\displaystyle{ \operatorname{build-param-lists}[g\ q\ p\ n, D, V, K_1] }[/math]

[math]\displaystyle{ \operatorname{build-param-lists}[g\ q\ p, D, V, T_6] \land \operatorname{build-param-lists}[n, D, V, K_6] }[/math]

[math]\displaystyle{ \land ((T_6 = [F_6, S_6, A_6]::K_1 }[/math]
[math]\displaystyle{ \land (S_6 \implies (\operatorname{equate}[A_6, n] \land V[F_6] = n)) \land D[F_6] = D[n]) }[/math]
application, Variable

[math]\displaystyle{ \operatorname{build-param-lists}[g\ q, D, V, T_7] \land \operatorname{build-param-lists}[p, D, V, K_7] }[/math]

[math]\displaystyle{ \land T_7 = [\_, S_7, A_7]::[\_, S_6, A_6]::K_1 }[/math]
[math]\displaystyle{ \land (S_6 \implies (\operatorname{equate}[A_6, n] \land V[F_6] = n)) \land D[F_6] = D[n]) }[/math]
[math]\displaystyle{ \land (S_7 \implies (\operatorname{equate}[A_7, p] \land V[F_7] = p)) \land D[F_7] = D[p] }[/math]
application, Variable

[math]\displaystyle{ \operatorname{build-param-lists}[g, D, V, T_8] \land \operatorname{build-param-lists}[m, D, V, K_8] }[/math]

[math]\displaystyle{ \land T_8 = [F_8, S_8, A_8]::[F_7, S_7, A_7]::[F_6, S_6, A_6]::K_1 }[/math]
[math]\displaystyle{ \land (S_6 \implies (\operatorname{equate}[A_6, n] \land V[F_6] = n)) \land D[F_6] = D[n]) }[/math]
[math]\displaystyle{ \land (S_7 \implies (\operatorname{equate}[A_7, p] \land V[F_7] = p)) \land D[F_7] = D[p] }[/math]
[math]\displaystyle{ \land (S_8 \implies (\operatorname{equate}[A_8, q] \land V[F_8] = q)) \land D[F_8] = D[q] }[/math]
Variable

[math]\displaystyle{ D[g] = [x, S_8, A_8]::[o, S_6, A_7]::[y, S_6, A_6]::K_1 }[/math]

[math]\displaystyle{ \land (S_6 \implies (\operatorname{equate}[A_6, n] \land V[y] = n)) \land D[y] = D[n]) }[/math]
[math]\displaystyle{ \land (S_7 \implies (\operatorname{equate}[A_7, p] \land V[o] = p)) \land D[o] = D[p] }[/math]
[math]\displaystyle{ \land (S_8 \implies (\operatorname{equate}[A_8, q] \land V[x] = q)) \land D[x] = D[q] }[/math]

As there are no definitions for, [math]\displaystyle{ V[n], V[p], V[q], V[m] }[/math], then equate can be simplified to,

[math]\displaystyle{ \operatorname{equate}[A, N] \equiv A = N \lor (\operatorname{def}[V[N]] \land A = V[N]) \equiv A = N }[/math]

By removing expressions not needed, [math]\displaystyle{ D[g] = [x, S_5, A_5]::[o, S_4, A_4]::[y, S_3, A_3]::K_2 }[/math]

[math]\displaystyle{ \land S_3 \implies A_3 = n }[/math]
[math]\displaystyle{ \land S_4 \implies A_4 = p }[/math]
[math]\displaystyle{ \land S_5 \implies A_5 = m }[/math]

[math]\displaystyle{ D[g] = [x, S_8, A_8]::[o, S_6, A_7]::[y, S_6, A_6]::K_1 }[/math]

[math]\displaystyle{ \land S_6 \implies A_6 = n }[/math]
[math]\displaystyle{ \land S_7 \implies A_7 = p }[/math]
[math]\displaystyle{ \land S_8 \implies A_8 = q }[/math]

By comparing the two expressions for [math]\displaystyle{ D[g] }[/math], get,

[math]\displaystyle{ S_5 = S_8, A_5 = A_8, S_4 = S_7, A_4 = A_7, S_3 = S_6, A_3 = A_6 }[/math]

If [math]\displaystyle{ S_3 }[/math] is true;

[math]\displaystyle{ n = A_3 = A_6 = n }[/math]

If [math]\displaystyle{ S_3 }[/math] is false there is no implication. So [math]\displaystyle{ S_3 = \_ }[/math] which means it may be true or false.

If [math]\displaystyle{ S_4 }[/math] is true;

[math]\displaystyle{ p = A_4 = A_7 = p }[/math]

If [math]\displaystyle{ S_5 }[/math] is true;

[math]\displaystyle{ m = A_5 = A_8 = q }[/math]

So [math]\displaystyle{ S_5 }[/math] is false.

The result is, [math]\displaystyle{ D[g] = [x, \operatorname{false}, \_]::[o, \_, p]::[y, \_, n]::\_ }[/math]

[math]\displaystyle{ \operatorname{build-param-lists}[o\ x\ y, D, V, L] }[/math]
Rule Expression
application [math]\displaystyle{ \operatorname{build-param-lists}[o\ x\ y, D, V, L] }[/math]
application

[math]\displaystyle{ \operatorname{build-param-lists}[o\ x, D, V, T_9] \land \operatorname{build-param-lists}[y, D, V, K_9] }[/math]

[math]\displaystyle{ \land T_9 = [F_9, S_9, A_9]::L }[/math]
[math]\displaystyle{ \land (S_9 \implies (\operatorname{equate}[A_9, y] \land V[F_9] = A_9) \land K_9 = D[F_9] }[/math]
variable

[math]\displaystyle{ \operatorname{build-param-lists}[o, D, V, T_{10}] \land \operatorname{build-param-lists}[x, D, V, K_{10}] \land \operatorname{build-param-lists}[y, D, V, K_{10}] }[/math]

[math]\displaystyle{ \land T_{10} = [F_{10}, S_{10}, A_{10}]::[F_9, S_9, A_9]::L }[/math]
[math]\displaystyle{ \land (S_9 \implies (\operatorname{equate}[A_9, y] \land V[F_9] = A_9) \land K_9 = D[F_9] }[/math]
[math]\displaystyle{ \land (S_{10} \implies (\operatorname{equate}[A_{10}, y] \land V[F_{10}] = A_{10}) \land K_{10} = D[F_{10}] }[/math]
[math]\displaystyle{ \land D[o] = [F_{10}, S_{10}, A_{10}]::[F_9, S_9, A_9]::L }[/math]
[math]\displaystyle{ \land (S_9 \implies (\operatorname{equate}[A_9, y] \land V[F_9] = A_9) \land K_9 = D[F_9] }[/math]
[math]\displaystyle{ \land (S_{10} \implies (\operatorname{equate}[A_{10}, y] \land V[F_{10}] = A_{10}) \land K_{10} = D[F_{10}] }[/math]

By similar arguments as used above get,

[math]\displaystyle{ D[o]= [\_, \_, x]::[\_, \_, y]::\_ }[/math]

and from previously,

[math]\displaystyle{ D[g] = x, \operatorname{false}, \ ], [o, \operatorname{true}, p], [y, \operatorname{true}, n }[/math]
[math]\displaystyle{ D[n] = \ , \ , (g\ m\ p\ n)], [\ , \ , (g\ q\ p\ n) }[/math]
[math]\displaystyle{ D[m] = \_ }[/math]
[math]\displaystyle{ D[p] = \_ }[/math]
[math]\displaystyle{ D[q] = \_ }[/math]

Another example is,

[math]\displaystyle{ \lambda f.((\lambda p.f\ (p\ p\ f))\ (\lambda q.\lambda x.x\ (q\ q\ x)) }[/math]

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

[math]\displaystyle{ D[p] = q, \ , p], [x, \ , f }[/math]

and the parameter x is dropped to give,

[math]\displaystyle{ \lambda f.((\lambda q.f\ (q\ q))\ (\lambda q.f\ (q\ q)) }[/math]
Build parameter list for [math]\displaystyle{ \lambda f.((\lambda p.f\ (p\ p\ f))\ (\lambda q.\lambda x.x\ (q\ q\ x)) }[/math]

The logic in equate is used in this more difficult example.

[math]\displaystyle{ \operatorname{build-param-list}[\lambda f.((\lambda p.f\ (p\ p\ f))\ (\lambda q.\lambda x.x\ (q\ q\ x)), D, V, \_] }[/math]
Rule Expression
Abstraction [math]\displaystyle{ \operatorname{build-param-list}[\lambda f.((\lambda p.f\ (p\ p\ f))\ (\lambda q.\lambda x.x\ (q\ q\ x)), D, V, \_] }[/math]
Abstraction [math]\displaystyle{ \operatorname{build-param-list}[(\lambda p.f\ (p\ p\ f))\ (\lambda q.\lambda x.x\ (q\ q\ x)), D, V, \_] }[/math]
[math]\displaystyle{ \operatorname{build-param-lists}[f\ (p\ p\ f), D, \_] \land \operatorname{build-list}[\lambda q.\lambda x.x\ (q\ q\ x), D, D[p]] }[/math]
[math]\displaystyle{ \operatorname{build-param-lists}[f\ (p\ p\ f), D, \_] \land \operatorname{build-list}[\lambda q.\lambda x.x\ (q\ q\ x), D, D[p]] }[/math]
[math]\displaystyle{ \operatorname{build-list}[\lambda q.\lambda x.x\ (q\ q\ x), D, D[p]] }[/math]
Rule Expression
Add param [math]\displaystyle{ \operatorname{build-list}[\lambda q.\lambda x.x\ (q\ q\ x), D, D[p]] \land D[p] = L_1 }[/math]
Add param [math]\displaystyle{ \operatorname{build-list}[\lambda x.x\ (q\ q\ x), D, L_2] \land D[p] = [q, \_, \_]::L_2 }[/math]
End list [math]\displaystyle{ \operatorname{build-list}[x\ (q\ q\ x), D, L_3] \land D[p] = [q, \_, \_]::[x, \_, \_]::L_3 }[/math]
[math]\displaystyle{ \operatorname{build-param-lists}[x\ (q\ q\ x), D, []] \land D[p] = [q, \_, \_]::[x, \_, \_]::[] }[/math]
[math]\displaystyle{ \operatorname{build-param-lists}[\lambda p.f\ (p\ p\ f), D, V, T_1] }[/math]
Rule Expression
Abstraction [math]\displaystyle{ \operatorname{build-param-lists}[\lambda p.f\ (p\ p\ f), D, V, T_1] }[/math]
Application [math]\displaystyle{ \operatorname{build-param-lists}[f\ (p\ p\ f), D, V, T_1] }[/math]
Name [math]\displaystyle{ \operatorname{build-param-lists}[f, D, V, T_2] \land \operatorname{build-param-lists}[p\ p\ f, D, V, K_2] }[/math]
[math]\displaystyle{ \land T_2 = [F_2, S_2, A_2]::[F_1, S_1, A_1]::\_ }[/math]
[math]\displaystyle{ \land (S_2 \implies (\operatorname{equate}[A_2, p\ p\ f] \land V[F_2] = A_2)) \land D[F_2] = K_2 }[/math]
Name [math]\displaystyle{ \operatorname{build-param-lists}[p\ p\ f, D, V, K_2] }[/math]

[math]\displaystyle{ \land D[f] = [F_2, S_2, A_2]::[F_1, S_1, A_1]::\_ }[/math]

[math]\displaystyle{ \land (S_2 \implies (\operatorname{equate}[A_2, p\ p\ f] \land V[F_2] = A_2)) \land D[F_2] = K_2 }[/math]
Name [math]\displaystyle{ \operatorname{build-param-lists}[p\ p, D, V, T_3] \land \operatorname{build-param-lists}[f, D, V, K_3] }[/math]
[math]\displaystyle{ \land T_3 = [F_3, S_3, A_3]::K_2 }[/math]
[math]\displaystyle{ \land (S_3 \implies (\operatorname{equate}[A_3, f] \land V[F_3] = A_3)) \land D[F_3] = K_3 }[/math]
Application [math]\displaystyle{ \operatorname{build-param-lists}[p\ p, D, V, T_3] }[/math]
[math]\displaystyle{ \land T_3 = [F_3, S_3, A_3]::K_2 }[/math]
[math]\displaystyle{ \land (S_2 \implies (\operatorname{equate}[A_2, p\ p\ f] \land V[F_2] = A_2)) \land D[F_2] = K_2 }[/math]
[math]\displaystyle{ \land (S_3 \implies (\operatorname{equate}[A_3, f] \land V[F_3] = A_3)) \land D[F_3] = D[f] }[/math]
Name [math]\displaystyle{ \operatorname{build-param-lists}[p, D, V, T_4] \land \operatorname{build-param-lists}[p, D, V, K_4] }[/math]
[math]\displaystyle{ \land T_4 = [F_4, S_4, A_4]::[F_3, S_3, A_3]::K_2 }[/math]
[math]\displaystyle{ \land (S_3 \implies (\operatorname{equate}[A_3, f] \land V[F_3] = A_3)) \land D[F_3] = D[f] }[/math]
[math]\displaystyle{ \land (S_4 \implies (\operatorname{equate}[A_4, p] \land V[F_4] = A_4)) \land D[F_4] = K_4 }[/math]
[math]\displaystyle{ D[p] = [F_4, S_4, A_4]::[F_3, S_3, A_3]::K_2 }[/math]
[math]\displaystyle{ \land (S_3 \implies (\operatorname{equate}[A_3, f] \land V[F_3] = A_3)) \land D[F_3] = D[f] }[/math]
[math]\displaystyle{ \land (S_4 \implies (\operatorname{equate}[A_4, p] \land V[F_4] = A_4)) \land D[F_4] = D[p] }[/math]
[math]\displaystyle{ \operatorname{build-param-lists}[x\ (q\ q\ x)), D, V, \_] }[/math]
Rule Expression
Abstraction [math]\displaystyle{ \operatorname{build-param-lists}[\lambda q.\lambda x.x\ (q\ q\ x)), D, V, \_] }[/math]
Application [math]\displaystyle{ \operatorname{build-param-lists}[x\ (q\ q\ x)), D, V, K_1] }[/math]
Name [math]\displaystyle{ \operatorname{build-param-lists}[x, D, V, T_5] \land \operatorname{build-param-lists}[q\ q\ x, D, V, K_5] }[/math]
[math]\displaystyle{ \land T_5 = [F_5, S_5, A_5]::\_ }[/math]
[math]\displaystyle{ \land (S_5 \implies (\operatorname{equate}[A_5, q\ q\ x] \land V[F_5] = A_5)) \land D[F_5] = K_5 }[/math]
Name [math]\displaystyle{ \operatorname{build-param-lists}[q\ q\ x, D, V, K_5] }[/math]
[math]\displaystyle{ \land D[x] = [F_5, S_5, A_5]::\_ }[/math]
[math]\displaystyle{ \land (S_5 \implies (\operatorname{equate}[A_5, q\ q\ x] \land V[F_5] = A_5)) \land D[F_5] = K_5 }[/math]
Name [math]\displaystyle{ \operatorname{build-param-lists}[q\ q, D, V, T_6] \land \operatorname{build-param-lists}[x, D, V, K_6] }[/math]
[math]\displaystyle{ \land T_6 = [F_6, S_6, A_6]::K_5 }[/math]
[math]\displaystyle{ \land (S_6 \implies (\operatorname{equate}[A_6, x] \land V[F_6] = A_6)) \land D[F_6] = K_6 }[/math]
Application [math]\displaystyle{ \operatorname{build-param-lists}[q\ q, D, V, T_6] }[/math]
[math]\displaystyle{ \land T_6 = [F_6, S_6, A_6]::K_5 }[/math]
[math]\displaystyle{ \land (S_6 \implies (\operatorname{equate}[A_6, x] \land V[F_6] = A_6)) \land D[F_6] = D[x] }[/math]
Name [math]\displaystyle{ \operatorname{build-param-lists}[q, D, V, T_7] \land \operatorname{build-param-lists}[q, D, V, K_7] }[/math]
[math]\displaystyle{ \land T_7 = [F_7, S_7, A_7]::[F_6, S_6, A_6]::K_5 }[/math]
[math]\displaystyle{ \land (S_6 \implies (\operatorname{equate}[A_6, x] \land V[F_6] = A_6)) \land D[F_6] = D[x] }[/math]
[math]\displaystyle{ \land (S_7 \implies (\operatorname{equate}[A_7, q] \land V[F_7] = A_7)) \land D[F_7] = K_7 }[/math]
Name [math]\displaystyle{ \operatorname{build-param-lists}[q, D, V, K_7] }[/math]
[math]\displaystyle{ \land D[q] = [F_7, S_7, A_7]::[F_6, S_6, A_6]::K_5 }[/math]
[math]\displaystyle{ \land (S_6 \implies (\operatorname{equate}[A_6, x] \land V[F_6] = A_6)) \land D[F_6] = D[x] }[/math]
[math]\displaystyle{ \land (S_7 \implies (\operatorname{equate}[A_7, q] \land V[F_7] = A_7)) \land D[F_7] = D[q] }[/math]

After collecting the results together,

[math]\displaystyle{ D[p] = [q, \_, \_]::[x, \_, \_]::L_3 }[/math]
[math]\displaystyle{ D[p] = [F_4, S_4, A_4]::[F_3, S_3, A_3]::K_2 }[/math]
[math]\displaystyle{ \land (S_3 \implies (\operatorname{equate}[A_3, f] \land V[F_3] = A_3)) \land D[F_3] = D[f] }[/math]
[math]\displaystyle{ \land (S_4 \implies (\operatorname{equate}[A_4, p] \land V[F_4] = A_4)) \land D[F_4] = D[p] }[/math]
[math]\displaystyle{ D[q] = [F_7, S_7, A_7]::[F_6, S_6, A_6]::K_5 }[/math]
[math]\displaystyle{ (S_6 \implies (\operatorname{equate}[A_6, x] \land V[F_6] = A_6)) \land D[F_6] = D[x] }[/math]
[math]\displaystyle{ (S_7 \implies (\operatorname{equate}[A_7, q] \land V[F_7] = A_7)) \land D[F_7] = D[q] }[/math]

From the two definitions for [math]\displaystyle{ D[p] }[/math];

[math]\displaystyle{ F_4 = q }[/math]
[math]\displaystyle{ F_3 = x }[/math]

so

[math]\displaystyle{ D[p] = [q, S_4, A_4]::[x, S_3, A_3]::K_2 }[/math]
[math]\displaystyle{ (S_3 \implies (\operatorname{equate}[A_3, f] \land V[x] = A_3)) \land D[x] = D[f] }[/math]
[math]\displaystyle{ (S_4 \implies (\operatorname{equate}[A_4, p] \land V[q] = A_4)) \land D[q] = D[p] }[/math]

Using [math]\displaystyle{ D[q] = D[p] }[/math] and

[math]\displaystyle{ D[p] = [F_7, S_7, A_7]::[F_6, S_6, A_6]::K_5 }[/math]
[math]\displaystyle{ (S_6 \implies (\operatorname{equate}[A_6, x] \land V[F_6] = A_6)) \land D[F_6] = D[x] }[/math]
[math]\displaystyle{ (S_7 \implies (\operatorname{equate}[A_7, q] \land V[F_7] = A_7)) \land D[F_7] = D[q] }[/math]

by comparing with the above,

[math]\displaystyle{ F_7 = q, F_6 = x, A_3 = A_6, A_4 = A_7, S_3 = S_6, S_4 = S_7 }[/math]

so,

[math]\displaystyle{ V[x] = A_3 }[/math]
[math]\displaystyle{ V[q] = A_4 }[/math]

in,

[math]\displaystyle{ S_3 \implies A_3 = f }[/math]
[math]\displaystyle{ S_3 \implies (A_3 = x \lor A_3 = v[x]) }[/math]

reduces to,

[math]\displaystyle{ S_3 \implies A_3 = f }[/math]

also,

[math]\displaystyle{ S_4 \implies A_4 = p }[/math]
[math]\displaystyle{ S_4 \implies (A_4 = q \lor A_4 = v[q]) }[/math]

reduces to,

[math]\displaystyle{ S_4 \implies A_4 = p }[/math]

So the parameter list for p is effectively;

[math]\displaystyle{ D[p] = [q, \_, p]::[x, \_, f]::\_ }[/math]

Drop parameters

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

  • The lambda expression in which the parameters are to be dropped.
  • The mapping of variable names to parameter lists (built in Build parameter lists).
  • The set of variables free in the lambda expression.
  • The returned parameter list. A parameter used internally in the algorithm.

Abstraction

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

where,

[math]\displaystyle{ F = FV[(\lambda N.S)\ L] }[/math]
[math]\displaystyle{ \operatorname{drop-params}[\lambda N.S, D, V, R] \equiv (\lambda N.\operatorname{drop-params}[S, D, F, R]) }[/math]

where,

[math]\displaystyle{ F = FV[\lambda N.S] }[/math]

Variable

[math]\displaystyle{ \operatorname{drop-params}[N, D, V, D[N]] \equiv N }[/math]

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

[math]\displaystyle{ (\operatorname{def}[F] \land \operatorname{ask}[S] \land FV[A] \subset V) \to \operatorname{drop-params}[E\ P, D, V, R] \equiv \operatorname{drop-params}[E, D, V, [F, S, A]::R] }[/math]
[math]\displaystyle{ \neg (\operatorname{def}[F] \land \operatorname{ask}[S] \land FV[A] \subset V) \to \operatorname{drop-params}[E\ P, D, V, R] \equiv \operatorname{drop-params}[E, D, V, [F, S, A]::R]\ \operatorname{drop-params}[P, D, V, \_] }[/math]

Let - Let expression.

[math]\displaystyle{ \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], []] }[/math]

And - For use in "let".

[math]\displaystyle{ \operatorname{drop-params}[E \land F, D, V, \_] \equiv \operatorname{drop-params}[E, D, V, \_] \land \operatorname{drop-params}[F, D, V, \_] }[/math]
Drop parameters from applications
[math]\displaystyle{ \lambda g.\lambda n.n\ (g\ m\ p\ n)\ (g\ q\ p\ n) }[/math]
Condition Expression
[math]\displaystyle{ \operatorname{drop-param}[ \lambda g.\lambda n.n\ (g\ m\ p\ n)\ (g\ q\ p\ n), D, \{p, q, m\}, \_] }[/math]
[math]\displaystyle{ \lambda g.\operatorname{drop-param}[\lambda n.n\ (g\ m\ p\ n)\ (g\ q\ p\ n), D, \{p, q, m\}, \_] }[/math]
[math]\displaystyle{ \neg (\operatorname{def}[F_1] \land ...) }[/math] [math]\displaystyle{ \lambda g.\lambda n.\operatorname{drop-param}[ n\ (g\ m\ p\ n), D, \{p, q, m\}, [F_1, S_1, A_1]::\_]\ \operatorname{drop-param}[(g\ q\ p\ n), D, \{p, q, m\}, \_] }[/math]
[math]\displaystyle{ \neg (\operatorname{def}[F_2] \land ...) }[/math] [math]\displaystyle{ \lambda g.\lambda n.\operatorname{drop-param}[ n\ , D, \{p, q, m\}, [F_2, S_2, A_2]::[F_1, S_1, A_1]::\_]\ \operatorname{drop-param}[(g\ m\ p\ n), D, \{p, q, m\}, \_] \ \operatorname{drop-param}[(g\ q\ p\ n), D, \{p, q, m\}, \_] }[/math]
[math]\displaystyle{ D[n] = [F_2, S_2, A_2]::[F_1, S_1, A_1]::\_] }[/math] [math]\displaystyle{ \lambda g.\lambda n.n\ \operatorname{drop-param}[(g\ m\ p\ n), D, \{p, q, m\}, \_] \ \operatorname{drop-param}[(g\ q\ p\ n), D, \{p, q, m\}, \_] }[/math]

From the results of building parameter lists;

[math]\displaystyle{ D[n] = \ , \ , (g\ m\ p\ n)], [\ , \ , (g\ q\ p\ n) }[/math]

so,

[math]\displaystyle{ F_1 = \_ }[/math]
[math]\displaystyle{ F_2 = \_ }[/math]

so,

[math]\displaystyle{ \operatorname{def}[F_1] = \operatorname{false} }[/math]
[math]\displaystyle{ \operatorname{def}[F_2] = \operatorname{false} }[/math]
[math]\displaystyle{ \operatorname{drop-param}[(g\ m\ p\ n), D, \{p, q, m\}, \_] }[/math]
Condition Expanded Expression
[math]\displaystyle{ V = \{p, q, m\} }[/math] [math]\displaystyle{ \operatorname{drop-param}[(g\ m\ p\ n), D, V, \_] }[/math]
[math]\displaystyle{ FV(A_1) \not \subset \{p, q, m\} }[/math] [math]\displaystyle{ n \not \subset \{p, q, m\} }[/math] [math]\displaystyle{ \operatorname{drop-params}[g\ m\ p, D, V, [F_1, S_1, A_1]::\_]\ \operatorname{drop-params}[n, D, V, \_] }[/math]
[math]\displaystyle{ \operatorname{def}[F_2] \land \operatorname{ask}[S_2] \land FV[A_2] \subset V }[/math] [math]\displaystyle{ \operatorname{def}[y] \land \operatorname{ask}[\_] \land FV[p] \subset \{p, q, m\} }[/math] [math]\displaystyle{ \operatorname{drop-params}[g\ m, D, V, [F_2, S_2, A_2]::[F_1, S_1, A_1]::\_]\ \operatorname{drop-params}[n, D, V, \_] }[/math]
[math]\displaystyle{ \neg \operatorname{ask}[S_3] }[/math] [math]\displaystyle{ \neg \operatorname{ask}[\operatorname{false}] }[/math] [math]\displaystyle{ \operatorname{drop-params}[g, D, V, [F_3, S_3, A_3]::[F_2, S_2, A_2]::[F_1, S_1, A_1]::\_]\ \operatorname{drop-params}[m, D, V, \_]\ \operatorname{drop-params}[n, D, V, \_] }[/math]

[math]\displaystyle{ D[g] = x, \operatorname{false}, \ ], [o, \ , p], [y, \ , n }[/math] [math]\displaystyle{ = [F_3, S_3, A_3]::[F_2, S_2, A_2]::[F_1, S_1, A_1]::\_] }[/math]

[math]\displaystyle{ F_3 = x, S_3 = \operatorname{false}, A_3 = \_ }[/math] [math]\displaystyle{ F_2 = o, S_2 = \_, A_2 = p }[/math] [math]\displaystyle{ F_1 = y, S_1 = \_, A_1 = n }[/math]

[math]\displaystyle{ g\ m\ n }[/math]

[math]\displaystyle{ \operatorname{drop-param}[(g\ q\ p\ n), D, \{p, q, m\}, \_] }[/math]
Condition Expanded Expression
V = \{p, q, m\} [math]\displaystyle{ \operatorname{drop-param}[(g\ q\ p\ n), D, V, \_] }[/math]
[math]\displaystyle{ FV(A_4) \not\subset V }[/math] [math]\displaystyle{ n \not\subset \{p, q, m\} }[/math] [math]\displaystyle{ \operatorname{drop-params}[g\ q\ p, D, V, [F_4, S_4, A_4]::\_]\ \operatorname{drop-params}[n, D, V, \_] }[/math]
[math]\displaystyle{ \operatorname{def}[F_5] \land \operatorname{ask}[S_5] \land FV[A_5] \subset V }[/math] [math]\displaystyle{ \operatorname{def}[o] \land \operatorname{ask}[\_] \land p \subset \{p, q, m\}) }[/math] [math]\displaystyle{ \operatorname{drop-params}[g\ q, D, V, [F_5, S_5, A_5]::[F_4, S_4, A_4]::\_]\ \operatorname{drop-params}[n, D, V, \_] }[/math]
[math]\displaystyle{ \neg \operatorname{ask}[S-6] }[/math] [math]\displaystyle{ \neg \operatorname{ask}[\operatorname{false}] }[/math] [math]\displaystyle{ \operatorname{drop-params}[g, D, V, [F_6, S_6, A_6]::[F_5, S_5, A_5]::[F_4, S_4, A_4]::\_]\ \operatorname{drop-params}[m, D, V, \_]\ \operatorname{drop-params}[n, D, V, \_] }[/math]

[math]\displaystyle{ D[g] = x, \operatorname{false}, \ ], [o, \ , p], [y, \ , n }[/math] [math]\displaystyle{ = [F_6, S_6, A_6]::[F_5, S_5, A_5]::[F_4, S_4, A_4]::\_] }[/math]

[math]\displaystyle{ F_6 = x, S_6 = \operatorname{false}, A_6 = \_ }[/math] [math]\displaystyle{ F_5 = o, S_5 = \_, A_5 = p }[/math] [math]\displaystyle{ F_4 = y, S_4 = \_, A_4 = n }[/math]

[math]\displaystyle{ g\ q\ n }[/math]

Drop formal parameters

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

  • The drop list,
  • The function definition (lambda abstraction).
  • The free variables from the function definition.

drop-formal is defined as,

  1. [math]\displaystyle{ (\operatorname{ask}[S] \land FV[A] \subset V) \to \operatorname{drop-formal}[[F, S, A]::Z, \lambda F.Y, V] \equiv \operatorname{drop-formal}[[F, S, A]::Z, Y[F:=A], L] }[/math]
  2. [math]\displaystyle{ \neg (\operatorname{ask}[S] \land FV[A] \subset V) \to \operatorname{drop-formal}[[F, S, A]::Z, \lambda F.Y, V] \equiv \lambda F.\operatorname{drop-formal}[[F, S, A]::Z, Y, V] }[/math]
  3. [math]\displaystyle{ \operatorname{drop-formal}[Z, Y, V] \equiv Y }[/math]

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
[math]\displaystyle{ \operatorname{false} }[/math] [math]\displaystyle{ \operatorname{drop-formal}[D, \lambda x.\lambda o.\lambda y.o\ x\ y, F] }[/math]
[math]\displaystyle{ \operatorname{true} \land \{p\} \subset F }[/math] [math]\displaystyle{ \lambda x.\operatorname{drop-formal}[D, \lambda o.\lambda y.o\ x\ y, F] }[/math]
[math]\displaystyle{ \neg (\operatorname{true} \land \{n\} \subset F }[/math]) [math]\displaystyle{ \lambda x.\operatorname{drop-formal}[D, (\lambda y.o\ x\ y)[o:=p], F] }[/math]
[math]\displaystyle{ \lambda x.\lambda y.\operatorname{drop-formal}[D, p\ x\ y, F] }[/math]
[math]\displaystyle{ \lambda x.\lambda y.p\ x\ y }[/math]

Example

Starting with the function definition of the Y-combinator,

[math]\displaystyle{ \operatorname{let} p\ f\ x = f\ (x\ x) \land q\ p\ f = (p\ f)\ (p\ f) \operatorname{in} q\ p\ }[/math]
Transformation Expression
[math]\displaystyle{ \operatorname{let} p\ f\ x = f\ (x\ x) \land q\ p\ f = (p\ f)\ (p\ f) \operatorname{in} q\ p }[/math]
abstract * 4 [math]\displaystyle{ \operatorname{let} p = \lambda f.\lambda x.f\ (x\ x) \land q = \lambda p.\lambda f.(p\ f)\ (p\ f) \operatorname{in} q\ p }[/math]
lambda-abstract-tran [math]\displaystyle{ (\lambda q.(\lambda p. q\ p)\ (\lambda f.\lambda x.f\ (x\ x)))\ (\lambda p.\lambda f.(p\ f)\ (p\ f)) }[/math]
sink-tran [math]\displaystyle{ (\lambda p.\lambda f.(p\ f)\ (p\ f))\ (\lambda f.\lambda x.f\ (x\ x)) }[/math]
sink-tran [math]\displaystyle{ \lambda f.(\lambda p.(p\ f)\ (p\ f))\ (\lambda f.\lambda x.f\ (x\ x)) }[/math]
drop-param [math]\displaystyle{ \lambda f.(\lambda p.p\ p)\ (\lambda x.f\ (x\ x)) }[/math]
beta-redex [math]\displaystyle{ \lambda f.(\lambda x.f\ (x\ x))\ (\lambda x.f\ (x\ x))\ }[/math]

Which gives back the Y combinator,

[math]\displaystyle{ \lambda f.(\lambda x.f\ (x\ x))\ (\lambda x.f\ (x\ x)) }[/math]

See also

References

  1. Johnsson, Thomas (1985). "Lambda Lifting: Transforming Programs to Recursive Equations". in Jouannaud, J.P.. 201. Springer. doi:10.1007/3-540-15975-4_37. ISBN 3-540-15975-4. 
  2. Morazán, Marco T.; Schultz, Ulrik P. (2008), "Optimal Lambda Lifting in Quadratic Time", Implementation and Application of Functional Languages — Revised Selected Papers, pp. 37–56, doi:10.1007/978-3-540-85373-2_3, ISBN 978-3-540-85372-5 
  3. Danvy, O.; Schultz, U. P. (1997). "Lambda-dropping". ACM SIGPLAN Notices 32 (12): 90. doi:10.1145/258994.259007. 
  4. Danvy, Olivier; Schultz, Ulrik P. (October 2000). "Lambda-Dropping: Transforming Recursive Equations into Programs with Block Structure". Theoretical Computer Science 248 (1–2): 243–287. doi:10.1016/S0304-3975(00)00054-2. BRICS-RS-99-27. http://www.brics.dk/RS/99/27/BRICS-RS-99-27.pdf. 

External links