Lectures onType Theory
Chapter 181
Chapter 181Optional

Differential Lambda Calculus and Resource Taylor Expansion

Prerequisites. Direct starred prerequisites: none. No later core chapter depends on this route.

In the term (λx(x)x)N the argument N is used twice, and β-reduction shows this by copying N. The copy destroys the information: after one step the term is (N)N, and nothing in it records that the two occurrences came from one argument. A calculus that keeps that record must resolve a prior question — how many copies of N are supplied? — and must do something definite when the supply does not match the demand.

Neither of the two obvious answers works. Forbidding copies altogether, as in the linear discipline of chapter 18, rejects λx(x)x outright, so nothing is learned about it. Allowing copies silently returns us to β-reduction. The answer developed here supplies a multiset of copies at each application, lets each reduction step consume exactly one copy and place it at exactly one occurrence of the variable, and sums over the possible placements. When the supply and the demand disagree the sum is empty, and the term reduces to 0.

Two things then have to be proved and one has to be constructed. The placement-and-sum operation is a derivative, and reduction using it must be confluent and terminating, or the calculus computes nothing. That is theorem 181.15. The construction is the passage back: an ordinary λ-term must be recovered as an infinite linear combination of resource terms, one for each way of fixing the supply, with coefficients that make normalization commute with the decomposition. Those coefficients are factorials, the decomposition is a Taylor expansion, and the exact sense in which it commutes with normalization is stated in theorem 181.23, theorem 181.27.

Differentiating a term

Fix a commutative semiring R of coefficients; for this chapter R is either N or Q, and the difference matters exactly once, in theorem 181.15. For a set I, write RI for the R-module of finite formal linear combinations of elements of I, and supp(σ)I for the set of elements with nonzero coefficient in σ.

Definition 181.1 — Differential terms

The simple terms of the differential λ-calculus are generated by s,t ::= x  λxs  (s)σ  Dsσ, where σ ranges over RΔ with Δ the set of simple terms, taken up to renaming of bound variables. A term is an element of RΔ. Application and differential application are extended to terms by linearity in both arguments, and λx by linearity; so (iaisi)σ:=iai(si)σ and similarly for the others.

The constructor Dsu is read: apply s to u once, linearly, leaving the ordinary argument still to come. The next definition is what gives that reading its content.

Definition 181.2 — Partial derivative

For a simple term s, a variable x, and a term u with x not free in u, the partial derivative sxuRΔ is defined by induction on s: yxu:=u  if y=x, and 0 otherwise;λysxu:=λy(sxu)(yx, yFV(u));Dsvxu:=D(sxu)v+Ds(vxu);(s)vxu:=(sxu)v+(Dsvxu)v, extended to terms by linearity in s and in u.

Three clauses are the product rule and one is not. Abstraction and the function position of an application are linear, so the derivative passes through them. The argument position of an ordinary application is not linear: the argument may be used any number of times. The last clause therefore does not differentiate v in place; it first creates one linear copy of the argument, by replacing (s)v with (Dsv)v, and differentiates that copy. This is the chain rule, and it is the reason D belongs to the syntax: without a linear application there is no place to put the differentiated copy.

Example 181.3 — Two derivatives

Let xFV(u). Then (x)xxu=definition181.2(xxu)x+(Dxxxu)x=(u)x+(Dxu)x. The two summands are the two occurrences of x: the first is the occurrence in function position, which is linear and receives u directly; the second is the occurrence in argument position, which receives u through a linear copy. By contrast λy(y)xxu=λy((y)xxu)=λy(0+(Dyu)x)=λy(Dyu)x, because yxu=0 kills the first summand.

Definition 181.4 — Differential reduction

The root reduction rules are (λxs)σ 0 s[σ/x],D(λxs)u 0 λx(sxu), where s[σ/x] is ordinary capture-avoiding substitution extended by linearity in σ. Write for the contextual closure of 0, extended linearly to RΔ, and for its reflexive-transitive closure.

Theorem 181.5 — Confluence of the differential calculus; imported

The relation on terms of the pure differential λ-calculus is Church–Rosser.

This is Theorem 18 of Ehrhard and Regnier, The Differential Lambda-Calculus, Theoretical Computer Science, volume 309, pages 1–41, 2003, proved there for an arbitrary commutative semiring of coefficients by a parallel-reduction argument. It is imported for exactly the statement displayed: confluence of the untyped calculus. Nothing about normalization, about typed terms, or about the analytic meaning of D is imported with it, and the chapter’s own normalization result is proved below for the resource fragment, where the argument is short and self-contained.

Resource terms

The differential calculus still contains ordinary application, hence still copies. Deleting ordinary application and keeping only a supply of linear copies gives the fragment in which the counting is exact.

Definition 181.6 — Simple resource terms and poly-terms

The simple resource terms Δ and poly-terms are generated simultaneously by s,t,u ::= x  λxs  sS,S ::= [t1,,tk](k0), where [t1,,tk] is a finite multiset of simple resource terms; [] is the empty multiset and ST is multiset union. A resource term is an element of RΔ. All constructions extend to RΔ by linearity, and a poly-term of terms means the multilinear extension: [σ1,,σk] expands into a linear combination of multisets of simple terms.

Definition 181.7 — Differential substitution

For sΔ, a variable x, and uRΔ with xFV(u), xxu:=u,yxu:=0(yx),λysxu:=λy(sxu),sSxu:=sxuS+s(Sxu), where for S=[t1,,tk], Sxu:=i=1k[t1,,tixu,,tk]. Also write s[0/x] for s when xFV(s) and for 0 otherwise.

Lemma 181.8 — Differential substitution is placement

Let sΔ have exactly n free occurrences of x, and let uΔ. Then sxu is the sum of the n simple terms obtained from s by replacing exactly one occurrence of x by u, each with coefficient 1. In particular the sum is 0 when n=0.

Proof of Lemma 181.8 — Differential substitution is placement

Proof. Induction on s. For s=x there is one occurrence and the sum is u; for s=yx there is none and the sum is 0. For s=λys the free occurrences of x in s are those in s, and the clause applies λy to each summand given by the induction hypothesis. For s=s[t1,,tk] the occurrences of x are those in s together with those in each ti, and the two clauses of definition 181.7 produce exactly the replacements in s and the replacements in each ti, the latter keeping the other members of the multiset fixed. ◻

Definition 181.9 — Resource reduction

The root rules are Empty:λxs[] 0 s[0/x],Place:λxs(uT) 0 λx(sxu)T, the rule Place applying for any choice of an element u of the nonempty poly-term. Write for the contextual closure extended linearly to RΔ, and for its reflexive-transitive closure.

Example 181.10 — Supply meets demand

Let u,v be simple resource terms with xFV(u)FV(v), and reduce λxx[x][u,v]. Choosing u first, λxx[x][u,v]Placeλx(x[x]xu)[v]=lemma181.8λx(u[x]+x[u])[v]Placeλxu[v][]+λxv[u][]Emptyu[v]+v[u]. The last step uses xFV(u[v]), so that s[0/x]=s. The normal form is the sum over the two bijections between the two supplied copies and the two occurrences of x.

Now change the supply. With one copy, λxx[x][u]Placeλx(u[x]+x[u])[]Empty0, because x is still free in both summands, so both are killed by [0/x]. With three copies the third step reaches λxσ[] where every summand of σ has no occurrence of x left to receive the third copy, and σ=0 already by lemma 181.8. A resource term computes to a nonzero value exactly when the supply matches the demand.

Exercise 181.1

★☆☆ Compute x(x[x,y])u and x(λyy[x,x])u, and check each against the count of occurrences promised by lemma 181.8.

Exercise 181.2

★★☆ Normalize λxx[x,x][u,v,w] completely, and count the summands of the normal form. Then predict, without reducing, the number of summands of the normal form of λxx[xn][u1,,un+1] for pairwise distinct ui, where [xn] is the multiset with n copies of x.

Termination and confluence

Definition 181.11 — Size

|x|:=1, |λxs|:=|s|+1, and |s[t1,,tk]|:=|s|+ik|ti|+1.

Lemma 181.12 — Size strictly decreases

If sΔ and sσ, then |t|<|s| for every tsupp(σ).

Proof of Lemma 181.12 — Size strictly decreases

Proof. By induction on the position of the contracted redex, the contextual cases being immediate because size is a sum over the immediate subterms and one summand strictly decreases. Two root cases remain.

For λxs[]0s[0/x]: the support of the right side is empty or {s}, and |λxs[]|=|s|+2.

For λxs(uT)0λx(sxu)T: by lemma 181.8 every element of supp(sxu) is s with one occurrence of x, of size 1, replaced by u, so it has size |s|1+|u|. Hence every element of the support of the right side has size (|s|1+|u|)+1+tT|t|+1=|s|+|u|+tT|t|+1, while the left side has size (|s|+1)+|u|+tT|t|+1, larger by exactly one. ◻

Lemma 181.13 — Symmetry of the second derivative

Let sΔ and let u,vRΔ with xFV(u)FV(v). Then x(sxu)v=x(sxv)u.

Proof of Lemma 181.13 — Symmetry of the second derivative

Proof. By lemma 181.8, both sides are sums over ordered pairs (p,q) of distinct occurrences of x in s, the left side placing u at p and v at q, the right side placing v at p and u at q. The pairs (p,q) and (q,p) contribute the same simple term to the two sides, since placement at distinct occurrences is independent of order, so the two sums have the same summands. The occurrences are distinct because x is not free in u or in v: the first placement removes the occurrence it fills and creates no new one. ◻

Lemma 181.14 — Reduction commutes with placement

Let s,sΔ and u,uRΔ with x free in neither u nor u.

  1. If sσ then sxuσxu.

  2. If uυ then sxusxυ.

Proof of Lemma 181.14 — Reduction commutes with placement

Proof. (ii) is immediate from lemma 181.8: each summand contains one copy of u in some position, and reduction is contextual, so reducing that copy in each summand takes sxu to sxυ in as many steps as there are summands.

(i) Induction on the position of the redex contracted in s. If the redex is inside a subterm not containing the occurrence of x being filled, the two operations act on disjoint positions and commute. The remaining case is the root redex s=λyr(wT) with x free in r, w, or T. Placing u at an occurrence of x inside r commutes with the reduction because x(ryw)u=y(rxu)w when xy and neither variable is free in the other’s argument, which holds by lemma 181.8 for the same reason as lemma 181.13: both sides sum over pairs consisting of an occurrence of x and an occurrence of y. Placing u inside w or inside T commutes because those positions are untouched by the reduction, up to the linearity already used in (ii). ◻

Theorem 181.15 — Church–Rosser and strong normalization

The reduction on RΔ is Church–Rosser. If R=N it is strongly normalizing, so every resource term has a unique normal form NF(σ).

Proof of Theorem 181.15 — Church–Rosser and strong normalization

Proof. Strong normalization. Assign to σNΔ the finite multiset sz(σ):={|t|:tsupp(σ)} of natural numbers. A step σσ replaces one element t of the support by the elements of the support of its reduct, each of strictly smaller size by lemma 181.12, and leaves the other elements unchanged. So sz(σ) is strictly smaller than sz(σ) in the multiset order on finite multisets of natural numbers, which is well founded. Hence there is no infinite reduction sequence.

Local confluence. Let σσ1 and σσ2. By linearity it suffices to treat a simple s in the support with two redex occurrences. If the two occurrences are disjoint, contracting either leaves the other unchanged, and contracting both in either order gives the same term, since reduction is contextual and the contexts do not overlap.

If one occurrence is inside the other, the outer redex is r=λxpS and the inner one lies in p or in a member of S. Both cases are lemma 181.14: reducing the inner redex first and then the outer one, or the other way round, reaches the same term, by (i) when the inner redex is in p and by (ii) when it is in a member of S.

The remaining overlap is at one redex with two admissible choices of the consumed element: λxp(uvT) contracts to λx(pxu)(vT) or to λx(pxv)(uT). One further step from each, consuming the other element, gives λxx(pxu)vTandλxx(pxv)uT, which are equal by lemma 181.13. If S is empty there is no choice, and if S is a singleton the two contractions coincide.

Conclusion. For R=N, strong normalization and local confluence give confluence by Newman’s lemma (lemma 40.27), and a confluent terminating relation has unique normal forms. For a general commutative semiring the same local confluence holds, and confluence is then the specialization to this fragment of theorem 181.5. ◻

Remark 181.16 — Where R= N is used

Only the strong normalization clause depends on the coefficients, and it depends on them only through the multiset order: with coefficients in Q a term may still normalize, but the argument above no longer bounds the number of steps by a well-founded measure on supports, because a sum with cancelling coefficients can have a smaller support than its summands suggest. Confluence is insensitive to that distinction.

Taylor expansion

An ordinary λ-term is now decomposed into resource terms. The decomposition has to choose, at every application, how large a supply to provide; so it is a sum over all choices, and the coefficients must be fixed so that repeated patterns are not counted twice.

Definition 181.17 — Multiplicity

For sΔ define m(s)N>0 by m(x):=1,m(λxs):=m(s),m(sT):=m(s)T!tΔm(t)T(t), where T(t) is the multiplicity of t in T and T!:=tΔT(t)!.

Definition 181.18 — Taylor expansion

For an ordinary λ-term M define a set T(M)Δ of simple resource terms by T(x):={x},T(λxM):={λxt:tT(M)}, T((M)N):={tT:tT(M), T a finite multiset over T(N)}, and put M:=tT(M)1m(t)t, an element of the module of possibly infinite linear combinations of simple resource terms with rational coefficients.

Example 181.19 — Why the name

For M=(x)x we get T(M)={x[xn]:n0} and m(x[xn])=1n!1=n!, so M=n01n!x[xn]. The coefficients are those of the exponential series, and the reason is visible in definition 181.17. The n copies in the supply are interchangeable. The n! orders in which reduction may consume them all produce one and the same term, and dividing by n! counts that supply once.

Example 181.20 — Expanding and normalizing

Let M:=(λx(x)x)y, whose ordinary normal form is (y)y. By definition 181.18, T(M)={tn,k:=λxx[xn][yk] : n,k0}. The simple term λxx[xn] has n+1 free occurrences of x, so by example 181.10 the term tn,k normalizes to 0 unless k=n+1. For k=n+1 each of the n+1 reduction steps consumes one copy of y and places it at one of the remaining occurrences, so NF(tn,n+1)=(n+1)! y[yn], the coefficient counting the orders of placement, all of which produce the same simple term because all supplied copies are y. The multiplicities are m(λxx[xn])=n!,m(tn,n+1)=n!(n+1)!,m(y[yn])=n!. Therefore NF(M)=n01n!(n+1)!(n+1)!y[yn]=n01n!y[yn]=((y)y). Normalizing the expansion of M gives the expansion of the normal form of M, with every coefficient matching. Theorem 181.27 states the general form of this calculation, and theorem 181.23 states the form of the coefficient (n+1)! that made it work.

Exercise 181.3

★☆☆ Compute m(t) for t=x[x,x], t=x[x,y], and t=λxx[x[x]], and explain in each case which repetition the factor T! counts.

Exercise 181.4

★★☆ Compute T(λxx) and T((λxx)y), normalize every element of the second set, and verify NF(((λxx)y))=y by summing the coefficients.

Uniformity

Example 181.20 used one fact that is not obvious: distinct elements of T(M) contributed to distinct normal simple terms, so no coefficient was ever formed by adding two contributions. That is the content of the uniformity theorem.

Definition 181.21 — Coherence and uniform terms

Define a symmetric relation on Δ by: xx; λxsλxs when ss; and sSsS when ss and every tS is coherent with every tS. A simple term is uniform when tt.

Lemma 181.22 — Members of an expansion are uniform and pairwise coherent

For every ordinary λ-term M and all t,tT(M) we have tt; in particular every tT(M) is uniform.

Proof of Lemma 181.22 — Members of an expansion are uniform and pairwise coherent

Proof. Induction on M. For M=x both terms are x. For M=λxN both are abstractions whose bodies lie in T(N), and the induction hypothesis applies. For M=(N)P we have t=rS and t=rS with r,rT(N) and S,S multisets over T(P); the induction hypothesis gives rr and gives coherence of every member of S with every member of S, since all of them lie in T(P). ◻

Theorem 181.23 — Uniformity and multiplicity; imported

Let M be an ordinary λ-term and let u be a normal simple resource term. There is at most one tT(M) with usupp(NF(t)), and for that t the coefficient of u in NF(t) is NF(t)u=m(t)m(u).

This is Theorem 21 of the authors’ long version of Uniformity and the Taylor Expansion of Ordinary Lambda-Terms by Ehrhard and Regnier, the manuscript retained in this book’s reference library; the published article is [ER08], whose numbering is not assumed to agree with the long version. Its proof rests on the coherence analysis of definition 181.21 together with a quantitative study of normal forms that is not reproduced here. What is imported is exactly the displayed statement: uniqueness of the contributing expansion term, and the value of its coefficient. Nothing is imported about which normal terms arise this way; that is the subject of section 181.6.

Example 181.24 — Checking the coefficient

Take M=(λx(x)x)y and u=y[yn], which is normal. By example 181.20 the only tT(M) whose normal form contains u is tn,n+1, confirming the uniqueness clause; and m(tn,n+1)m(u)=n!(n+1)!n!=(n+1)!=NF(tn,n+1)u, confirming the coefficient clause. The calculation also shows what would go wrong without uniqueness: two contributing terms would make the coefficient of u in NF(M) a sum of two fractions, and the identity NF(M)=(NF(M)) would fail.

Böhm trees and the resource-driven machine

Theorem 181.23 says that a normal resource term is reached from at most one expansion term. It does not say which normal resource terms are reached, nor how to find the expansion term that reaches a given one. Both are answered by the Böhm tree of M.

Definition 181.25 — B"ohm tree

For an ordinary λ-term M, the Böhm tree BT(M) is the possibly infinite tree defined corecursively: if M has a head normal form λx1xk.(y)M1Mn, then BT(M) has root labelled λx1xk.y with children BT(M1),,BT(Mn); if M has no head normal form, BT(M) is the empty tree . The Taylor expansion T(BT(M)) of a Böhm tree is defined by the clauses of definition 181.18 read corecursively, with T()=.

Theorem 181.26 — Normal resource terms come from B"ohm trees; imported

Let M be an ordinary λ-term and u a normal simple resource term. Then uT(BT(M)) if and only if there exists sT(M) with usupp(NF(s)), and in that case s is unique.

Theorem 181.27 — Normalization commutes with expansion; imported

For every ordinary λ-term M, BT(M)=NF(M)=sT(M)1m(s)NF(s).

These are Theorems 10 and 11 of [ER06]. The proof of the first constructs a modified Krivine abstract machine that takes an ordinary term M, an environment, and a normal resource term u, and computes the unique sT(M) whose normal form contains u; the partial function so obtained is the content of the theorem’s uniqueness clause. The second theorem is a consequence of the first together with the quantitative statement imported as theorem 181.23. Both are imported for exactly the displayed statements; in particular nothing is imported about the complexity of the machine, and the observation in that paper that the number of machine steps measures reduction cost is not used here.

Example 181.28 — The commutation on the worked example

For M=(λx(x)x)y the head normal form is (y)y, so BT(M) is the finite tree with root y and one child y, and BT(M)=((y)y)=n1n!y[yn]. Example 181.20 computed NF(M) and obtained the same sum, which is theorem 181.27 in this instance. The unique s promised by theorem 181.26 for u=y[yn] is tn,n+1, and the machine of that theorem finds it by running the head reduction of M while consuming the structure of u.

Two bounded comparisons

Differential linear logic. The syntax of definition 181.1 is the term calculus of a proof system in which the exponential modality carries, besides dereliction, weakening and contraction, a codereliction rule that extracts one linear use from a duplicable resource. Under that reading Dsu is the codereliction applied to u followed by application, and lemma 181.13 is the commutation of two coderelictions. The comparison is recorded because it explains the shape of definition 181.2; no proof-theoretic result is used or imported here, and no statement of this chapter depends on it.

Typed automatic differentiation. A reader arriving from a numerical setting will recognize the product rule and the chain rule in definition 181.2, and may expect a correctness statement relating sxu to a derivative of a smooth function. No such statement is available here and none is claimed: definition 181.2 is a syntactic operation on terms, its correctness criteria are theorem 181.15, theorem 181.23, and neither mentions a smooth function or a limit. A semantic differentiation operator with an analytic correctness theorem is a different development, with its own source language, its own model, and its own hypotheses.

Suggested first pass.

None of these problems is a prerequisite for a later chapter. Begin with exercise 181.5, then exercise 181.6; the implementation project exercise 181.8 may be attempted at any time.

Exercise 181.5

★★☆ Prove lemma 181.13 again, this time by induction on s rather than by the occurrence count of lemma 181.8, and identify the case in which the hypothesis xFV(u)FV(v) is used. Then give a term s and terms u,v violating that hypothesis for which the two iterated derivatives differ.

Exercise 181.6

★★☆ Let M=(λx(x)(x)x)y. Compute T(M), determine for which supplies the normal form is nonzero, compute NF(M), and check the coefficient identity of theorem 181.23 for u=y[y[yn]] by computing m(t) and m(u) separately.

Exercise 181.7

★★☆ Exhibit a simple resource term that is not uniform in the sense of definition 181.21, and prove that it belongs to no T(M). Then explain why theorem 181.23 would be false without the restriction to terms of the form T(M), by exhibiting two coherent-but-distinct terms whose normal forms share a summand.

Exercise 181.8

★★★ Practical project.resource-taylor-normalizer Implement in Agda or Kappa the resource calculus of definition 181.6, definition 181.9 and its Taylor expansion. Represent simple terms with de Bruijn indices or with names and explicit renaming, poly-terms as sorted multisets, and resource terms as finite maps from simple terms to natural coefficients. Implement: differential substitution sxu; one reduction step; normalization; the multiplicity m(t) of definition 181.17; and the enumeration of T(M) for an ordinary λ-term M up to a bound on the size of every supply.

The invariant to maintain is the one proved in lemma 181.8, lemma 181.12: differential substitution produces exactly one summand per free occurrence of the variable, and every summand produced by one reduction step is strictly smaller in the size of definition 181.11, so normalization terminates. The concrete result is a function from an ordinary term and a supply bound to the list of pairs (t,NF(t)) for tT(M) within the bound, together with m(t).

Acceptance test. With the terms of this chapter: λxx[x][u,v] normalizes to u[v]+v[u]; the same term with supply [u] or [u,v,w] normalizes to 0; for M=(λx(x)x)y and supply bound 4 the tool reports NF(tn,n+1)=(n+1)!y[yn] and m(tn,n+1)=n!(n+1)! for n3, and 0 for every other tn,k within the bound; and the ratio m(t)/m(u) equals the reported coefficient in each nonzero case, which is the instance of theorem 181.23 checked in example 181.24. A mutation of differential substitution that places the argument at every occurrence simultaneously, rather than summing over single placements, must fail the first test by returning u[u]+ instead of the displayed sum.

The program computes with finitely many resource terms within a supply bound. It illustrates theorem 181.15, theorem 181.23 and proves neither; in particular it never inspects the infinite sum M.

Bibliographic notes

The differential λ-calculus of section 181.1 is that of Ehrhard and Regnier, The Differential Lambda-Calculus, Theoretical Computer Science 309(1–3):1–41, 2003; definition 181.2 is its partial derivative and theorem 181.5 is its Theorem 18. The resource calculus, the multiplicity coefficients, and the Taylor expansion of section 181.2, section 181.4 are from the authors’ long version of Uniformity and the Taylor Expansion of Ordinary Lambda-Terms, whose published form is [ER08]; theorem 181.15 is its Theorem 3, proved here in full because the argument is short, and theorem 181.23 is its Theorem 21, imported. Theorem numbers are quoted from the long version, since the published pagination and numbering are not assumed to coincide with it. Resource terms in the sense used here go back to Boudol’s resource calculus; the terms that occur in some T(M) are the well-formed terms of Kfoury’s analysis, called uniform here because the other terms are also of interest.

Theorem 181.26, Theorem 181.27 are Theorems 10 and 11 of [ER06], which also contains the resource-driven Krivine machine that computes the unique contributing expansion term. An OCaml demonstrator accompanying that work is retained in the reference library and was used to check the calculations of example 181.20, example 181.28 independently of the implementation asked for in exercise 181.8; it is execution evidence and owns no theorem. Regnier’s later tutorial exposition presents the same route from differential substitution to resource approximants and is the recommended companion for a first reading.

Search the book

Type to search the local edition.