Ehrhard and Regnier (2003) introduced the Differential Lambda Calculus and with it the Taylor expansion of lambda-terms.
\[ P x = \sum_{n=0}^\infty \frac{1}{n!} (\mathsf{D}^n P \cdot x^n) 0 \]
\(\mathsf{D}^n P \cdot x^n\) represents a version of \(P\) linearly applied to \(n\) occurrences of its argument.
The fully developed Taylor expansion — in which subterms are recursively expanded — can be seen as an infinite sum of linear terms drawn from a resource calculus.
This has led to a great deal of further developments, including:
The resource calculus replaces regular application of lambda-terms \(MN\) with a resource-sensitive application:
Intuition: execution of \(MN\) is like execution of \(M[N,\ldots,N]\) for some number of copies of \(N\).
\[ (\lambda x.m)\bar{n} \rightarrow_\beta m[\bar{n}/x] \]
Linear substitution generates a finite sum of terms:
All syntactic operations can be extended to sums via linearity. This includes preservation of \(\rZero\), so any zero that appears destroys the whole term.
(Technically we’re working in the free module over the terms, with coefficients drawn from a suitable semiring.)
Reduction is strongly normalizing and (locally) confluent.
Terms can experience starvation e.g. \[ (\lambda x. x[x]) ([\lambda x.x[x], \lambda x.x [x]]) \rightarrow (\lambda x.x[x])[\lambda x.x[x]] \rightarrow \rZero \]
Surfeit also destroys a term: \[ (\lambda x. x[x]) ([\lambda x.x[x], \lambda x.x [x], \lambda x.x [x]]) \rightarrow \rZero \]
The Taylor expansion of a lambda-term is now readily defined:
(If using an idempotent semiring — which suffices for our purposes — the rational coefficient can be ignored.)
Ehrhard and Regnier (2008) demonstrated that normalised Taylor expansions coincide with Taylor-expanded Böhm trees: \[ NF(\mathbf{T}(M)) = \mathbf{T}(\mathbf{BT}(M)). \]
So equivalence of Taylor expansions is the same as equivalence of Böhm trees.
Barbarossa and Manzonetto (2019) used this to redevelop several classical lambda-calculus results using Taylor expansions.
Taylor expansion provides a powerful new approximation theory for the lambda-calculus.
These ideas grew from quantitative semantics of lambda-calculus and linear logic (Girard, 1988).
The simplest variant of quantitative semantics is the relational model:
(Reddy, 1996) described a relational model of imperative programs based on lists rather than multisets.
A (natural?) question: can we adapt the resource calculus and associated theory to a noncommutative setting, replacing multisets with lists?
(We remark that Tsukada, Asada and Ong (2017) introduced a “rigid” resource calculus, using lists, but subject to permutations; we abandon the permutations and embrace the fixed ordering.)
The syntax looks the same as before, thanks to the magic of overloading.
\[ (\lambda x.m)\vec{n} \rightarrow_\beta m[\vec{n}/x] \]
What is the substitution?
In contrast to the original resource calculus, the “sums” here are either \(\rZero\) or singletons.
Confluence is immediately problematic:
We can avoid this situation by restricting our syntax.
A linear application rule
\[ \frac{\Gamma \vdash m \quad \Delta \vdash n_i \quad \quad (i = 1, \ldots, k)}{\Gamma, \Delta \vdash m[n_1, \ldots, n_k]} \] prevents variables from occurring in both function and argument.
The resulting calculus is very boring: just linear lambda-calculus. But it is confluent!
Perhaps we can restore some interesting computations to the calculus by adding a sequencing construct \[ m \rSeq n \] with an additive style rule \[ \frac{\Gamma \vdash m \quad \Gamma \vdash n}{\Gamma \vdash m \rSeq n} \]
We can add constants and assertions to obtain a noncommutative resource calculus.
\[ \rM, \rN \inddef \vX \mid \cC \mid \rAbs{\vX}{\rM} \mid \rApp{\rM}{\bN} \mid \rM \rSeq \rN \mid \rAssert{\cC}{\rM} \]
Assertions will allow us to encode conditionals using formal sums: \[ \rAssert{\rTrue}{B} \rSeq M + \rAssert{\rFalse}{B}\rSeq N \] behaves like if-then-else.
Linear substitution now works as expected.
Multiple occurrences of a variable are always separated by \(\rSeq\) or occur in separate parts of a list: there is a well-defined order.
Linear substitution respects this order.
\[ (\lambda x.m)\vec{n} \rightarrow_\beta m[\vec{n}/x] \quad\quad \rSkip \rSeq m \rightarrow m \]
\[ \rAssert{\cC}{\cC} \rightarrow \rSkip \quad\quad \rAssert{\cC}{\cD} \rightarrow \rZero \]
\[ \rAssert{\cC}{(m \rSeq n)} \rightarrow m \rSeq \rAssert{\cC}{n} \]
\[ \frac{m \rightarrow m'}{m \rSeq n \rightarrow m' \rSeq n} \quad\quad \frac{n \rightarrow n'}{m \rSeq n \rightarrow m \rSeq n'} \quad\quad \]
What could this be good for?
(Reddy, 1996) pioneered a relational semantics of imperative programs.
His model replaces the multisets of the familiar relational model of linear logic with lists: a non-commutative relational semantics.
It was later shown [M 2002] that this model is fully abstract for a programming language called Basic SCI.
Reddy’s work was an important precursor to the game semantics of local variables [Abramsky & M, 1996].
Reynolds (1978) introduced syntactic control of interference (SCI) as an approach to control of aliasing in imperative programming.
Key idea: functions and their arguments should not share identifiers.
Reynolds used this property to ensure that distinct identifiers never interfere; this enables cleaner reasoning about programs.
Nowadays we see this as a linear (or affine) type system.
SCI is a restricted Idealized Algol. It has an important place in the development of programming language theory.
Basic SCI is a higher-order imperative programming language with local variables and an affine type system.
Types:
Constants: \[ \sConst{k} \inddef \sSkip \mid \sTrue \mid \sFalse \mid \sConst{i} \]
Syntax:
\(l\) ranges over an infinite set of locations.
Note that there is no recursion: a function cannot call itself because \[ f(f(\ldots)) \] violates the linearity constraint.
Type judgements \( \sTypes{\Gamma}{L}{M}{A} \):
\[ \frac{\sTypes{\Gamma}{L}{M}{A \sFun B} \quad\quad \sTypes{\Delta}{L'}{N}{A}} {\sTypes{\Gamma,\Delta}{L,L'}{\sApp{M}{N}}{B}} \]
\[ \frac{\sTypes{\Gamma}{L}{M}{\sUnit} \quad\quad \sTypes{\Gamma}{L}{N}{\sGround}}{\sTypes{\Gamma}{L}{\sSeq{M}{N}}{\sGround}} \]
\[ \frac{\sTypes{\Gamma}{L,l}{M}{\sGround}} {\sTypes{\Gamma}{L}{\sNew{l}{M}}{\sGround}} \]
This language can be given a small-step operational semantics: \[ M, \sigma \rightarrow M', \sigma' \] where \(\sigma, \sigma'\) are states: functions from locations to integers.
Key reductions: \[ (\lambda x.M)N, \sigma \rightarrow M[N/x], \sigma \]
\[ \sAssign{l}{i}, \sigma \rightarrow \sSkip,\sUpd{\sigma}{l}{i} \]
\[ \sDeref{l},\sigma \rightarrow \sigma(i), \sigma \]
\[ \sNew{l}{M}, \sigma \rightarrow M, \sUpd{\sigma}{l}{0} \]
In the last case the set of available locations grows to include \(l\).
We’ll now see how to extend our noncommutative resource calculus to incorporate the ideas from Reddy’s model.
The corresponding Taylor expansion will map SCI terms to sums of resource terms, following the ideas of Reddy’s interpretation.
Resource calculus (reminder): \[ \rM, \rN \inddef \vX \mid \cC \mid \rAbs{\vX}{\rM} \mid \rApp{\rM}{\bN} \mid \rM \rSeq \rN \mid \rAssert{\cC}{\rM} \]
To this we add assignment and lookup operations: \[ \rM, \rN \inddef \ldots \mid \rGet{\lL} \mid \rAssign{\lL}{i} \]
Note that the more general \(\rAssign{\lL}{\rM}\) can be encoded via sums and asserts as \[ \sum_i \rAssert{i}{\rM}\rSeq \rAssign{\lL}{i} \]
We add no more reductions to the calculus!
The calculus is strongly normalizing and (locally) confluent.
So how does it model state? Via the Taylor expansion of local variables.
We can begin to give the Taylor expansion of SCI terms.
A term \(M\) will be taken to a formal sum \(\mathbf{T}(M)\) of resource terms.
For the lambda-calculus part, the definition appears identical to Ehrhard and Regnier’s:
It is not the same — it is much dumber!
Arithmetic and conditionals are handled using sequencing, assert, and sums:
Basic imperative constructs:
We just need to figure out variable allocation…
Consider the term \[ f: \sUnit \rightarrow \sUnit \vdash^l \sSeq{f(\sAssign{l}{\sDeref{l}+1})}{\sDeref{l}}:\sNat \]
The first terms of its Taylor expansion are
(after simplifying some assertions.)
Each term contains a single trace of reads and writes to location \(l\).
The behaviour of such a term after \(l\) is allocated to a cell will be given by:
We can perform this replacement using a variant of linear substitution.
We define an operation of linear substitution of state traces into resource terms.
State traces are sequences of operations: \[ \oOp \inddef \oWI \mid \oRI \]
Given a trace \(\trT\) and a location \(l\), we define the substitution \[ \rFeed{m}{\lL}{\trT} \] on resource terms.
This substitution represents the result of term \(m\), executed in an environment where location \(l\) insists on performing the trace \(\trT\).
(Compare this to a linear application to a fixed list of inputs.)
The key cases: \[ \rFeed{(\rGet{\lL'})}{\lL}{\trT}\eqdef\left\{\begin{array}{rl} i&\text{if $\trT=\oRI$ and $\lL=\lL'$} \\ \rGet{\lL'}&\text{if $\trT=\emptyword$ and $\lL\not=\lL'$} \\ \rZero&\text{otherwise} \end{array}\right. \]
\[ \rFeed{(\rAssign{\lL'}{i})}{\lL}{\trT}\eqdef\left\{\begin{array}{rl} \rSkip&\text{if $\trT=\oWI$ and $\lL=\lL'$} \\ \rAssign{\lL'}{i}&\text{if $\trT=\emptyword$ and $\lL\not=\lL'$} \\ \rZero&\text{otherwise} \end{array}\right. \]
\[ \rFeed{\cC}{\lL}{\trT}\eqdef\left\{\begin{array}{rl} \cC&\text{if $\trT=\emptyword$} \\ \rZero&\text{otherwise} \end{array}\right. \] \[ \rFeed{x}{\lL}{\trT}\eqdef\left\{\begin{array}{rl} x&\text{if $\trT=\emptyword$} \\ \rZero&\text{otherwise} \end{array}\right. \] \[ \rFeed{(\rAbs{\vX}{\rM})}{\lL}{\trT}\eqdef \rAbs{\vX}{\rFeed{\rM}{\lL}{\trT}} \] \[ \rFeed{(\rApp{\rM}{\bN})}{\lL}{\trT}\eqdef\left\{\begin{array}{rl} \rApp{\rFeed{\rM}{\lL}{\trT}}{\bN}&\text{if $\lL$ is a location of $\rM$} \\ \rApp{\rM}{\rFeed{\bN}{\lL}{\trT}}&\text{if $\lL$ is a location of $\rN$} \end{array}\right. \] \[ \rFeed{\bag{\rM_1,\ldots,\rM_k}}{\lL}{\trT}\eqdef \sum_{\trT=t_1\cdots t_k}\bag{\rFeed{\rM_1}{\lL}{t_1},\dotsc,\rFeed{\rM_k}{\lL}{t_k}} \] \[ \rFeed{(\rM\rSeq \rN)}{\lL}{\trT}\eqdef \sum_{\trT=t_1\cdot t_2}\rFeed{\rM}{\lL}{t_1}\rSeq \rFeed{\rN}{\lL}{t_2} \] \[ \rFeed{(\rAssert{\cC}{\rM})}{\lL}{\trT}\eqdef \rAssert{\cC}{\rFeed{\rM}{\lL}{\trT}} \]
Variable allocation is expanded via substitution of all possible valid traces: those where the reads and writes match up as you would expect. \[ \mathbf{T}(\sNew{\lL}{M}) = \sum_{\trT\in\allValidTr} \rFeed{\mathbf{T}(M)}{\lL}{\trT}. \]
Recall the term \[ f: \sUnit \rightarrow \sUnit \vdash^l \sSeq{f(\sAssign{l}{\sDeref{l}+1})}{\sDeref{l}}:\sNat \]
with its Taylor expansion
To compute the expansion of \[ f: \sUnit \rightarrow \sUnit \vdash^{\emptyset} \sNew{l}{\sSeq{f(\sAssign{l}{\sDeref{l}+1})}{\sDeref{l}}}:\sNat \] we perform all possible substitutions with valid state-traces, and discard any \(\rZero\)s that arise.
When \(f\) does not invoke its argument, the location is left unchanged: \[ \sum_{\trT\in\allValidTr} \rFeed{(f([])\rSeq \rGet{l})}{l}{\trT} = \sum_{t = \opRead{0}} \rFeed{(f([])\rSeq \rGet{l})}{l}{\trT} = f[]; 0 \]
When \(f\) invokes its argument once, the location is incremented once: \[ \sum_{\trT\in\allValidTr} \rFeed{(\sum_i f[\rAssert{i}{\rGet{l}}\rSeq \rAssign{l}{i+1}] \rSeq \rGet{l})}{l}{\trT} = \sum_i f[\rAssert{i}{0}\rSeq\rSkip]\rSeq i+1 \] because the only traces yielding a non-zero substitution are of the form \[ \opRead{0}\opWrite{i+1}\opRead{i+1}. \]
Discarding failed asserts, this is \[ f[\rAssert{0}{0}\rSeq\rSkip]\rSeq 1 \] which reduces to \[ f[\rSkip]\rSeq 1 \]
When \(f\) invokes its argument twice, the location is incremented twice:
This reduces to \[ f[\rAssert{0}{0}\rSeq \rSkip, \rAssert{1}{1} \rSeq\rSkip] \rSeq 2 \] and then to \[ f[\rSkip,\rSkip]\rSeq 2 \]
The Taylor expansion accurately reflects the operational semantics of SCI:
If \(M, \sigma \rightarrow M', \sigma'\) then for every \(t' \in \validTr{\sigma'}{-}\) and every \(m' \in \mathbf{T}(M')\), there is \(m \in \mathbf{T}(M)\) and \(t \in \validTr{\sigma}{-}\) such that \[ NF(\rFeed{m}{L}{t}) = NF(\rFeed{m'}{L'}{t'}). \]
This is proved by straightforward induction.
We can prove an adequacy result: if the Taylor expansion of a closed program is non-zero, the program terminates.
For a program \(\vdash^{\emptyset}M:\sUnit\), if \(NF(\mathbf{T}(M)) \not = \rZero\) then \(M, \sigma \rightarrow^* \sSkip, \sigma'\), where \(\sigma\) is the unique state on no locations and \(\sigma'\) is some state.
The proof is a typical logical relations argument.
Combining soundness and adequacy with the compositionality of the Taylor expansion we obtain:
For terms \(\Gamma \vdash^L M,N:A\) of SCI, if \(NF(\mathbf{T}(M)) = NF(\mathbf{T}(N))\) then \(M\) and \(N\) are observably equivalent.
However, the Taylor expansion is not a fully abstract semantics. For example, in SCI \[ \lambda x.\lambda y. \sSeq{x}{y} \cong \lambda x.\lambda y. \sSeq{y}{x}: \sUnit\rightarrow\sUnit\rightarrow\sUnit \] but their Taylor expansions are clearly different.
We introduced a noncommutative resource calculus which serves as a target of a Taylor expansion of imperative programs.
The calculus is confluent, strongly normalizing, and has no reduction rules for handling imperative behaviour!
Normal forms of the calculus can be seen as basic bricks of approximants to SCI programs.
The ideas of differential lambda calculus and Taylor expansion grew from quantitative semantics of lambda-calculus and linear logic [Girard 1988].
The simplest variant of quantitative semantics is the relational model:
E.g. \[ \sem{x:\mathbf{Z} \vdash x - x:\mathbf{Z}} = \{ ([n,m], n-m) \mid n,m \in \mathbf{Z}\}. \]
In function application, the argument can provide multiple inputs to the function.
For closed terms: \[ \sem{MN} = \{ b \mid ([a_1, \ldots, a_k], b) \in \sem{M}, a_1, \ldots, a_k \in \sem{N} \}. \] Note that all inputs are drawn from \(\sem{N}\) and the order does not matter.
cf. \(\mathbf{T}(M)[\mathbf{T}(N)^k]. \)
Resource terms provide finer control over the contents of the input bags.
Interpretation of resource calculus terms:
\[ \sem{[M_1, \ldots, M_n]} = \{ [m_1, \ldots, m_n] \mid m_i \in \sem{M_i} \} \]
Resource terms describe building blocks of the relational semantics.
In some situations, a normal-form resource term denotes a single element of the relational model.
We outline the basic definitions of Reddy’s model of SCI, for comparison.
The unit type \(\sUnit\) is modelled as a singleton set \(\{*\}\).
Locations are treated as variables of a special type \(\sVars\), modelled as the set of possible read and write actions. \[ \{ \oRI \mid i \in \sNat \} \cup \{ \oWI \mid i \in \sNat \} \]
The term
\[f:\sUnit \rightarrow \sUnit \vdash^{l} \sSeq{f(\sAssign{l}{\sDeref{l}+1})}{\sDeref{l}}: \sNat \] is interpreted as a relation with entries such as
As in the relational model of lambda-calculus, the semantics of application of closed terms is easy to describe: \[ \sem{MN} = \{ b \mid ([a_1, \ldots, a_k], b) \in \sem{M}, a_1, \ldots, a_k \in \sem{N} \}. \]
Semantics of local variable application is similar!
Given \[ \Gamma \vdash^{l} M:\sGround \] we have \[ \sem{\Gamma \vdash^{\emptyset}\sNew{l}{M}} = \{ (s,b) \mid (s,t,b) \in \sem{M}, t\text{ is a valid state trace}\} \]
Recalling our example:
After allocation of \(l\) as a local variable, the only elements in play are those with valid state traces
and the final semantics is