Beniamino Accattoli
email: beniamino.accattoli@gmail.com

Ugo Dal

Beta Reduction is Invariant, Indeed

Keywords: F.3.2 [Logics and Meaning of Programs]: Semantics of Programming Languages -Operational Semantics.; F.4.1 [Mathematical Logic and Formal Languages]: Mathematical Logic -Lambda Calculus and Related Systems λ-calculus, computational complexity, cost models, explicit substitutions, sharing

Slot and van Emde Boas' weak invariance thesis states that reasonable machines can simulate each other within a polynomially overhead in time. Is λ-calculus a reasonable machine? Is there a way to measure the computational complexity of a λ-term? This paper presents the first complete positive answer to this longstanding problem. Moreover, our answer is completely machineindependent and based over a standard notion in the theory of λ-calculus: the length of a leftmost-outermost derivation to normal form is an invariant cost model. Such a theorem cannot be proved by directly relating λ-calculus with Turing machines or random access machines, because of the size explosion problem: there are terms that in a linear number of steps produce an exponentially long output. The first step towards the solution is to shift to a notion of evaluation for which the length and the size of the output are linearly related. This is done by adopting the linear substitution calculus (LSC), a calculus of explicit substitutions modelled after linear logic proof nets and admitting a decomposition of leftmostoutermost derivations with the desired property. Thus, the LSC is invariant with respect to, say, random access machines. The second step is to show that LSC is invariant with respect to the λ-calculus. The size explosion problem seems to imply that this is not possible: having the same notions of normal form, evaluation in the LSC is exponentially longer than in the λ-calculus. We solve such an impasse by introducing a new form of shared normal form and shared reduction, deemed useful. Useful evaluation avoids those steps that only unshare the output without contributing to β-redexes, i.e. the steps that cause the blow-up in size. The main technical contribution of the paper is indeed the definition of useful reductions and the thorough analysis of their properties.

Introduction

Theoretical computer science is built around algorithms, computational models, and machines: an algorithm describes a solution to a problem with respect to a fixed computational model, whose role is to provide a handy abstraction of concrete machines. The choice of the model reflects a tension between different needs. For complexity analysis, one expects a neat relationship between the primitives of the model and the way in which they are effectively implemented. In this respect, random access machines are often taken as the reference model, since their definition closely reflects the von Neumann architecture. The specification of algorithms unfortunately lies at the other end of the spectrum, as one would like them to be as machine-independent as possible. In this case programming languages are the typical model. Functional programming languages, thanks to their higher-order nature, provide very concise and abstract specifications. Their strength is also their weakness: the abstraction from physical machines is pushed to a level where it is no longer clear how to measure the complexity of an algorithm. Is there a way in which such a tension can be solved?

The tools for stating the question formally are provided by complexity theory and by Slot and van Emde Boas' invariance thesis [START_REF] Slot | On tape versus core; an application of space efficient perfect hash functions to the invariance of space[END_REF]:

Reasonable computational models simulate each other with polynomially bounded overhead in time, and constant factor overhead in space.

The weak invariance thesis is the variant where the requirement about space is dropped, and it is the one we will actually work with in this paper. The idea behind the thesis is that for reasonable models the definition of every polynomial or super-polynomial class such as P or EXP does not rely on the chosen model. On the other hand, it is well-known that sub-polynomial classes depend very much on the model, and thus it does not really make sense to pursue a linear rather than polynomial relationship. A first refinement of our question then is: are functional languages invariant with respect to standard models like random access machines or Turing machines? Such an invariance has to be proved via an appropriate measure of time complexity for programs, i.e. a cost model.

The natural answer is to consider the unitary cost model, i.e. take the number of evaluation steps as the cost of the underlying term. However, this is not well-defined. The evaluation of functional programs, indeed, depends very much on the evaluation strategy chosen to implement the language, as the λ-calculus-the reference model for functional languages-is so machine-independent that it does not even come with a deterministic evaluation strategy. And which strategy, if any, gives us the most natural, or canonical cost model (whatever that means)? These questions have received some attention in the last decades. The number of optimal parallel β-steps (in the sense of Lévy [START_REF] Lévy | Réductions correctes et optimales dans le lambda-calcul[END_REF]) to normal form has been shown not to be a reasonable cost model: there exists a family of terms that reduces in a polynomial number of parallel β-steps, but whose complexity is non-elementary [START_REF] Asperti | Parallel beta reduction is not elementary recursive[END_REF][START_REF] Lawall | Optimality and inefficiency: What isn't a cost model of the lambda calculus?[END_REF]. If one considers the number of sequential β-steps (in a given strategy, for a given notion of reduction), the literature offers some partial positive results, all relying on the use of sharing (see below for more details). Some quite general results [START_REF] Avanzini | Closing the gap between runtime complexity and polytime computability[END_REF][START_REF] Lago | On constructor rewrite systems and the lambda calculus[END_REF] have been obtained through graph rewriting, itself a form of sharing, when only first order symbols are considered.

Sharing is indeed a key ingredient, for one of the issues here is due to the representation of terms. The ordinary way of representing terms indeed suffers from the size explosion problem: even for the most restrictive notions of reduction (e.g. Plotkin's weak reduction), there is a family of terms {tn} n∈N such that |tn| is linear in n, tn evaluates to its normal form in n steps, but at i-th step a term of size 2 i is copied, producing a normal form of size exponential in n. Put differently, an evaluation sequence of linear length can possibly produce an output of exponential size. At first sight, then, there is no hope that evaluation lengths may provide an invariant cost model. The idea is that such an impasse can be avoided by sharing common sub-terms along the evaluation process, in order to keep the representation of the output compact, i.e. polynomially related to the number of evaluation steps. But is appropriately managed sharing enough? The literature offers some positive, but partial, answers to this question. The number of steps is indeed known to be an invariant cost model for weak reduction [START_REF] Lago | The weak lambda calculus as a reasonable machine[END_REF][START_REF] Lago | On constructor rewrite systems and the lambda calculus[END_REF] and for head reduction [START_REF] Accattoli | On the invariance of the unitary cost model for head reduction[END_REF].

If the problem at hand consists in computing the normal form of an arbitrary λ-term, however, no positive answer is known. We believe that not knowing whether the λ-calculus in its full generality is a reasonable machine is embarrassing for the λ-calculus community. In addition, this problem is relevant in practice: proof assistants often need to check whether two terms are convertible, itself a problem that can be reduced to the one under consideration.

In this paper, we give a positive answer to the question above, by showing that leftmost-outermost (LO, for short) reduction to normal form indeed induces an invariant cost model. Such an evaluation strategy is standard, in the sense of the standardisation theorem, one of the central theorems in the theory of λ-calculus, first proved by Curry and Feys [START_REF] Curry | Combinatory Logic. Studies in logic and the foundations of mathematics[END_REF]. The relevance of our cost model is given by the fact that LO reduction is an abstract concept from rewriting theory which at first sight is totally unrelated to complexity analysis. In particular, our cost model is completely machineindependent.

Another view on this problem comes in fact from rewriting theory itself. It is common practice to specify the operational semantics of a language via a rewriting system, whose rules always employ some form of substitution, or at least of copying, of subterms. Unfortunately, this practice is very far away from the way languages are implemented. Indeed, actual interpreters perform copying in a very controlled way (see, e.g., [START_REF] Jones | The Implementation of Functional Programming Languages[END_REF][START_REF] Wadsworth | Semantics and pragmatics of the lambda-calculus[END_REF]). This discrepancy induces serious doubts about the relevance of the computational model. Is there any theoretical justification for copy-based models, or more generally for rewriting theory as a modelling tool? In this paper we give a very precise answer, formulated within rewriting theory itself.

As in our previous work [START_REF] Accattoli | On the invariance of the unitary cost model for head reduction[END_REF], we prove our result by means of the linear substitution calculus (see also [START_REF] Accattoli | An abstract factorization theorem for explicit substitutions[END_REF][START_REF] Accattoli | A Nonstandard Standardization Theorem[END_REF]), a simple calculus of explicit substitutions (ES, for short) arising from linear logic and graphical syntaxes and similar to calculi studied by De Bruijn [START_REF] De Bruijn | Generalizing Automath by Means of a Lambda-Typed Lambda Calculus[END_REF], Nederpelt [START_REF] Nederpelt | The fine-structure of lambda calculus[END_REF], and Milner [START_REF] Milner | Local bigraphs and confluence: Two conjectures[END_REF]. A peculiar feature of the linear substitution calculus (LSC) is the use of rewriting rules at a distance, i.e. rules defined by means of contexts, that are used to closely mimic reduction in linear logic proof nets. Such a framework-whose use does not require any knowledge of these areas-allows an easy management of sharing and, in contrast to previous approaches to ES, admits a theory of standardisation and a notion of LO evaluation [START_REF] Accattoli | A Nonstandard Standardization Theorem[END_REF]. The proof of our result indeed is a tour de force based on a fine quantitative study of the relationship between LO derivations for the λ-calculus and a variation over LO derivations for the LSC. Roughly, the latter avoids the size explosion problem while keeping a polynomial relationship with the former.

Let us point out that invariance results usually have two directions, while we here study only one of them (namely that the λcalculus can be efficiently simulated by, say, Turing machines). The missing half is a much simpler problem already solved in [START_REF] Accattoli | On the invariance of the unitary cost model for head reduction[END_REF]: there is an encoding of Turing machines into λ-terms s.t. their execution is simulated by weak head β-reduction with only a linear overhead.

On Invariance and Complexity Analysis. Before proceeding, let us stress some crucial points: 1. ES Are Only a Tool. Although ES are an essential tool for the proof of our result, the result itself is about the usual, pure, λcalculus. In particular, the invariance result can be used without any need to care about ES: we are allowed to measure the complexity of problems by simply bounding the number of LO β-steps taken by any λ-term solving the problem. 2. Complexity Classes in the λ-Calculus. The main consequence of our invariance result is that every polynomial or superpolynomial class, like P of EXP, can be defined using λcalculus (and LO β-reduction) instead of Turing machines. 3. Our Cost Model is Unitary. An important point is that our cost model is unitary, and thus attributes a constant cost to any LO step. One could argue that it is always possible to reduce λ-terms on abstract or concrete machines and take that number of steps as the cost model. First, such a measure of complexity would be very machine-dependent, against the very essence of λ-calculus. Second, these cost models invariably attribute a more-than-constant cost to any β-step, making the measure much harder to use and analyse. It is not evident that a computational model enjoys a unitary invariant cost model. As an example, if multiplication is a primitive operation, random access machines need to be endowed with a logarithmic cost model in order to obtain invariance. The next section explains why the problem at hand is hard, and in particular why iterating our previous results on head reduction [START_REF] Accattoli | On the invariance of the unitary cost model for head reduction[END_REF] does not provide a solution. An extended version of this paper with more details is available [START_REF] Accattoli | Beta reduction is invariant, indeed[END_REF].

Why is The Problem Hard?

In principle, one may wonder why sharing is needed at all, or whether a relatively simple form of sharing suffices. In this section, we will show that sharing is unavoidable and that a new subtle notion of sharing is necessary.

If we stick to explicit representations of terms, in which sharing is not allowed, counterexamples to invariance can be designed in a fairly easy way. Let u be the lambda term yxx and consider the sequence {tn} n∈N of λ-terms defined as t0 = u and tn+1 = (λx.tn)u for every n ∈ N. The term tn has size linear in n, and tn rewrites to its normal form rn in exactly n steps, following the LO reduction order; as an example:

t0 = u = r0; t1 → yuu = yr0r0 = r1; t2 → (λx.t0)(yuu) = (λx.u)r1 → yr1r1 = r2.
For every n, however, rn+1 contains two copies of rn, hence the size of rn is exponential in n. As a consequence, the unitary cost model is not invariant: in a linear number of β-steps we reach an object which cannot even be written down in polynomial time.

The solution the authors proposed in [START_REF] Accattoli | On the invariance of the unitary cost model for head reduction[END_REF] is based on ES, and allows to tame the size explosion problem in a satisfactory way when head reduction suffices. In particular, the head steps above become the following linear head steps (where ES are denoted with t[x u]):

t0 = u = p0; t1 → (yxx)[x u] = u[x u] = p1; t2 → ((λx.t0)u)[x u] = ((λx.u)u)[x u] → u[x u][x u] = p2.
As one can easily verify, the size of pn is linear in n. More generally, linear head reduction (LHR) has the subterm property, i.e. it only duplicates subterms of the initial term. This fact implies that the size of the result and the length of the derivation are linearly related. In other words, the size explosion problem has been solved. Of course one needs to show that 1) the compact results unfold to the expected result (that may be exponentially bigger), and 2) that compact representations can be managed efficiently (typically they can be tested for equality in time polynomial in the size of the compact representation), see [START_REF] Accattoli | On the invariance of the unitary cost model for head reduction[END_REF] or below for more details.

It may seem that one is then forced to use ES to measure complexity. In [START_REF] Accattoli | On the invariance of the unitary cost model for head reduction[END_REF] we also showed that LHR is at most quadratically longer than head reduction, so that the polynomial invariance of LHR lifts to head reduction. This is how we exploit sharing to circumvent the size explosion problem: we are allowed to take the length of the head derivation as a cost model, even if it suffers of the size explosion problem, because the actual implementation is meant to be done via LHR and be only polynomially (actually quadratically) longer.

There is a natural candidate for extending the approach to reduction to normal form: just iterate the (linear) head strategy on the arguments, obtaining the (linear) LO strategy, that does compute normal forms [START_REF] Accattoli | A Nonstandard Standardization Theorem[END_REF]. As we will show, for linear LO derivations the subterm property holds. The size of the output is still under control, being linearly related to the length of the LO derivation. Unfortunately, when computing normal forms this is not enough.

One of the key points in our previous work was that there is a notion of linear head normal form that is a compact representation for head normal forms. The generalisation of such an approach to normal forms has to face a fundamental problem: what is a linear normal form? Indeed, terms with and without ES share the same notion of normal form. Consider again the family of terms {tn} n∈N : if we go on and unfold all substitutions in pn, we end up in rn. Thus, by the subterm property, the linear LO strategy takes an exponential number of steps, and so it cannot be polynomially related to the LO strategy.

Summing up, we need a strategy that 1) implements the LO strategy, 2) has the subterm property and 3) never performs useless substitution steps, i.e. those steps whose role is simply to explicit the normal form, without contributing in any way to β-redexes. The main contribution of this work is the definition of such a linear useful strategy, and the proof that it is indeed polynomially related to both the LO strategy and a concrete implementation model. This is not a trivial task, actually. One may think that it is enough to evaluate a term t in a LO way, stopping as soon as the unfolding u → of the current term u -the term obtained by expanding the ES of u -is a β-normal form. Unfortunately, this simple approach does not work, because the exponential blow-up may be caused by ES lying between two β-redexes, so that proceeding in a LO way would unfold the problematic substitutions anyway.

Our notion of useful step will elaborate on this idea, by computing partial unfoldings, to check if a substitution step contributes or will contribute to some future β-redex. Of course, we will have to show that such tests can be themselves performed in polynomial time, and that the notion of LO useful reduction retains all the good properties of LO reduction.

The Calculus

We assume familiarity with the λ-calculus (see [START_REF] Barendregt | The Lambda Calculus -Its Syntax and Semantics[END_REF]). The language of the linear substitution calculus (LSC for short) is given by the following grammar for terms:

t, u, r, p ::= x | λx.t | tu | t[x u].
The constructor t[x u] is called an explicit substitution (of u for x in t, the usual (implicit) substitution is instead noted t{x u}). Both λx.t and t[x u] bind x in t, and we silently work modulo α-equivalence of these bound variables, e.g. (xy)[y t]{x y} = (yz)[z t]. We use fv(t) for the set of free variables of t.

Contexts. The operational semantics of the LSC is parametric in a notion of (one-hole) context. General contexts are defined by:

C ::= • | λx.C | Ct | tC | C[x t] | t[x C],
and the plugging of a term t into a context C is defined as • t := t, (λx.C) t := λx.(C t ), and so on. As usual, plugging in a context can capture variables, e.g.

(( • y)[y t]) y = (yy)[y t]. The plugging C D of a context D into a context C is defined analogously.
Along most of the paper, however, we will not need such a general notion of context. In fact, our study takes a simpler form if the operational semantics is defined with respect to shallow contexts, defined as (note the absence of the production t[x S]):

S, P, T, V ::= • | λx.S | St | tS | S[x t].
In the following, whenever we refer to a context without further specification it is implicitly assumed that it is a shallow context. A special class of contexts is that of substitution contexts:

L ::= • | L[x t].
Operational Semantics. The (shallow) rewriting rules →dB (dB = β at a distance) and →ls (linear substitution) are given by the closure by (shallow) contexts of the following rules:

L λx.t u →dB L t[x u] ; S x [x u] →ls S u [x u].
The union of →dB and →ls is simply noted →. The rewriting rules are assumed to use on-the-fly α-equivalence to avoid variable capture. For instance,

(λx.t)[y u]y →dB t{y z}[x y][z u]

for z / ∈ fv(t);

(λy.(xy))[x y] →ls (λz.(yz))[x y].
Moreover, in rule ls the context S is assumed to not capture x, so that

(λx.x)[x y] →ls (λx.y)[x y].
The just defined shallow fragment simply ignores garbage collection (that in the LSC can always be postponed [START_REF] Accattoli | An abstract factorization theorem for explicit substitutions[END_REF]) and lacks some of the nice properties of the LSC (obtained simply by replacing shallow contexts by general contexts). Its relevance is the fact that it is the smallest fragment implementing linear LO reduction. The following are examples of shallow steps:

(λx.x)y →dB x[x y]; (xx)[x t] →ls (xt)[x t];
while the following steps are not

t[z (λx.x)y] →dB t[z x[x y]]; x[x y][y t] →ls x[x t][y t].
Taking the external context into account, a substitution step has the following explicit form: P S x [x u] →ls P S u [x u] . We shall often use a compact form, writing T x →ls T u where it is implicitly assumed that T = P S[x u] . We use R and Q as metavariables for redexes. A derivation ρ : t → k u is a finite sequence of reduction steps, sometimes given as R1; . . . ; R k , i.e. as the sequence of reduced redexes. We write |t| for the size of t, |t| [•] for the number of substitutions in t, |ρ| for the length of ρ, and |ρ|dB (resp. |ρ|ls) for the number of dB-steps (resp. ls-steps) in ρ.

(Relative) Unfoldings. The unfolding t → of a term t is the λ-term obtained from t by turning its explicit substitutions into implicit ones:

x

→ := x; (tu) → := t → u → ; (λx.t) → := λx.t → ; (t[x u]) → := t → {x u → }.
We will also need a more general notion, the unfolding t

→ S of t in a context S: t → • := t → ; t → uS := t → S ; t → S[x u] := t → S {x u → }; t → λx.S := t → S ; t → Su := t → S . For instance, (x(yz))[y x][x z] → = z(zz); (xy) → ( • [y x]t)[x
→ := S → {x t → }).
We also write S ≺p t if there is a term u s.t. S u = t, call it the prefix relation. We have the following properties, that only hold because our contexts are shallow (implying that the hole cannot be duplicated during the unfolding).

Lemma 3.1. Let S be a shallow context. Then: 1. S → is a shallow context; 2. S t {x u} = S{x u} t{x u} ; 3. S t → = S → t → S , in particular if S ≺p t then S → ≺p t → .
Given a derivation ρ : t → * u in the LSC, we often consider the β-derivation ρ → : t → → * β u → obtained by projecting ρ via unfolding. Reduction Combinatorics. Given any calculus, a deterministic strategy → for it, and a term t, the expression #→(t) stands for the number of reduction steps necessary to reach the normal form of t along →, or ∞ if t diverges. Similarly, given a natural number n, the expression → n (t) stands for the term u such that t → n u, if n ≤ #→(t), or for the normal form of t otherwise.

The Proof, Made Abstract

Our proof method can be described abstractly. Such an approach both clarifies the structure of the proof and prepares the ground for possible generalisations to, e.g., the call-by-value λ-calculus or calculi with additional features as pattern matching or control operators. We want to show that a certain strategy for the λcalculus provides a unitary and invariant cost model, i.e. that the number of steps is a measure polynomially related to the number of transitions on Turing machines. As explained in the introduction, we pass through an intermediary computational model, a calculus with ES, the linear substitution calculus, playing the role of a very abstract machine for λ-terms.

We are looking for an appropriate strategy X within the LSC which is invariant with respect to both and Turing machines. Then we need two theorems, which together form the main result of the paper: 1. High-Level Implementation:

terminates iff X terminates. Moreover, is implemented by X with only a polynomial overhead. Namely, t k X u iff t h u → with k polynomial in h (our actual bound will be quadratic); 2. Low-Level Implementation: X is implemented on Turing machines with an overhead in time which is polynomial in both k and the size of t.

The high-level part relies on the following notion.

Definition 4.1. Let be a deterministic strategy on λ-terms and X a strategy of the LSC. The pair ( , X ) is a high-level implementation system if whenever t is a λ-term and ρ : t * X u then: .

1. Normal Form: if u is a X -normal
Concretely, the high-level implementation system at work in the paper will take as the LO strategy of the λ-calculus and as X a variant of the linear LO strategy for the LSC. A variant is required because, as we will explain, the linear LO strategy of the LSC does not satisfy the syntactic bound property.

The normal form and projection properties address the qualitative part of the high-level implementation theorem, i.e. the part about termination. The normal form property guarantees that X does not stop prematurely, so that when X terminates cannot keep going. The projection property guarantees that termination of implies termination of X . The two properties actually state a stronger fact:

steps can be identified with the dB-steps of the X strategy. The trace and syntactic bound properties are instead used for the quantitative part of the theorem, i.e. to provide the polynomial bound. The two properties together provide a bound the number of ls-steps in a X derivation with respect to the number of dBsteps, that-by the identification of β and dB redexes-is exactly the length of the associated derivation.

The high-level part can now be proved abstractly.

Theorem 4.2 (High-Level Implementation). Let t be an ordinary λ-term and ( , X ) a high-level implementation system. Then:

1. t is -normalising iff it is X -normalising. 2. If ρ : t * X u then ρ → : t * u → and |ρ| = O(|ρ → | 2 ).
Proof. 1. ⇐) Suppose that t is X -normalisable and let ρ : t * X u a derivation to X -normal form. By the projection property there is a derivation t * u → . By the normal form property u → is a -normal form. ⇒) Suppose that t is -normalisable and let τ : t k u be the derivation to -normal form (unique by determinism of ). Assume, by contradiction, that t is not X -normalisable. Then there is a family of X -derivations ρi : t i X ui with i ∈ N, each one extending the previous one. By the syntactic bound property, X can make only a finite number of ls steps (more generally, →ls is strongly normalising in the LSC). Then the sequence {|ρi|dB} i∈N is non-decreasing and unbounded. By the projection property, the family {ρi} i∈N unfolds to a family of -derivations {ρi → } i∈N of unbounded length (in particular greater than k), absurd. 2. By the projection property, it follows that ρ

→ : t * u → . Moreover, to show |ρ| = O(|ρ → | 2 ) it is enough to show |ρ| = O(|ρ| 2 dB )
. Now, ρ has the shape:

t = r 1 → a 1 dB p 1 → b 1 ls r 2 → a 2 dB p 2 → b 2 ls . . . r k → a k dB p k → b k ls u.
By the syntactic bound property, we obtain bi ≤ |pi| The subterm property-essentially-guarantees that any step has a linear cost in the size of the initial term, the fundamental parameter for complexity; it will be discussed in more detail in Sect. 6. At first sight the selection property is always trivially verified: finding a redex in u takes time linear in |u|. However, our strategy for ES will reduce only redexes satisfying a side-condition whose naïve verification is exponential in |u|. Then one has to be sure that such a computation can be done in polynomial time. In [START_REF] Accattoli | On the invariance of the unitary cost model for head reduction[END_REF], we proved that head reduction and linear head reduction form a high-level implementation system and that linear head reduction is mechanisable, even if we did not use such a terminology, nor were we aware of the presented abstract scheme. In order to extend such a result to normal forms we need to replace head reduction with a normalising strategy (i.e. a strategy reaching the β-normal form, if any).

One candidate for is the LO strategy → LOβ . Such a choice is natural, as → LOβ is normalising, it produces standard derivations, and it is an iteration of head reduction. What is left to do, then, is to find a strategy X for ES, which is both mechanisable and a high-level implementation of → LOβ . Unfortunately, the linear LO strategy, noted →LO and first defined in [START_REF] Accattoli | A Nonstandard Standardization Theorem[END_REF], is mechanisable but the pair (→ LOβ , →LO) is not a high-level implementation system.

In general, mechanisable strategies are not hard to find. As we will show in Sect. 6, the whole class of standard derivations for ES has the subterm property. In particular, the linear strategy →LO-which is standard-enjoys all the other properties but for the syntactic bound property.

Such a problem will be solved by LO useful derivations, to be introduced in Sect. 5, that will be shown to be both mechanisable and a high-level implementation of → LOβ . Useful derivations avoid those substitution steps that only explicit the normal form without contributing to explicit β/dB-redexes (that, by the projection property, can be identified). LO useful derivations will have all the nice properties of LO derivations and moreover will stop on shared, minimal representations of normal forms, solving the problem with linear LO derivations.

Let us point out that our analysis would be vacuous without evidence that useful normal forms are a reasonable representation of λ-terms. In other words, we must be sure that ES do not hide (too much of) the inherent difficulty of reducing λ-terms under the carpet of sharing. In [START_REF] Accattoli | On the invariance of the unitary cost model for head reduction[END_REF], we solved this issue by providing an efficient algorithm for checking the equality of any two LSC terms-thus in particular of useful normal forms-without computing their unfoldings (that otherwise would reintroduce an exponential blowup). Some further discussion can be found in Sect. 10.

Useful Derivations

In this section we define a constrained, optimised notion of reduction, that will be the key to the High-Level Implementation Theorem. The idea is that an optimised step takes place only if it somehow contributes to explicit a β/dB-redex. Let an applicative context be defined by A ::= S Lt , where S and L are a shallow and a substitution context, respectively (note that applicative contexts are not made out of applications only; for instance tλx.( • [y u]r) is an applicative context). Then: Note that useful steps concern future creations of β-redexes and yet their definition circumvents the explicit use of residuals, relying on relative unfoldings only.

Definition
Leftmost-Outermost Useful Derivations. The notion of smallstep evaluation that we will use to implement LO β-reduction is the one of LO useful derivation. We need some preliminary definitions.

Let R be a redex. Its position is defined as follows:

1. If R is a dB-redex S L λx.t u →dB S L t[x u] then its
position is given by the context S surrounding the changing expression; β-redexes are treated as dB-redexes. 2. If R is a ls-redex, expressed in compact form S x →ls S u , then its position is the context S surrounding the variable occurrence to substitute. The left-to-right outside-in order on redexes is expressed as an order on positions, i.e. contexts. Let us warn the reader about a possible source of confusion. The left-to-right outside-in order in the next definition is sometimes simply called left-to-right (or simply left) order. The former terminology is used when terms are seen as trees (where the left-to-right and the outside-in orders are disjoint), while the latter terminology is used when terms are seen as strings (where the left-to-right is a total order). While the study of standardisation for the LSC [START_REF] Accattoli | A Nonstandard Standardization Theorem[END_REF] uses the string approach (and thus only talks about the left-to-right order and the leftmost redex), here some of the proofs (see the long version [START_REF] Accattoli | Beta reduction is invariant, indeed[END_REF]) require a delicate analysis of the relative positions of redexes and so we prefer the more informative tree approach and define the order formally.

Definition 5.2. The following definitions are given with respect to general (not necessarily shallow) contexts, even if apart from the next section we will use them only for shallow contexts. 1. The outside-in order:

1. Root:

( • t)u ≺L (rt) • ; t[x • ]u ≺L t[x r] • .
The next lemma guarantees that we defined a total order. The orders above can be extended from contexts to redexes, in the expected way, e.g. for ≺LO given two redexes R and Q of positions S and P we write R ≺LO Q if S ≺LO P . Now, we can define the notions of derivations we are interested in. Definition 5.4 (Leftmost-Outermost (Useful) Redex). Let t be a term and R a redex of t. R is the leftmost-outermost (resp. leftmost-outermost useful, LOU for short) redex of t if R ≺LO Q for every other redex (resp. useful redex) Q of t. We write t →LO u (resp. t →LOU u) if a step reduces the LO (resp. LOU) redex.

We need to ensure that LOU derivations are mechanisable and form a high-level implementation system when paired with LO derivations. In particular, we will show: 1. the subterm and trace properties, by first showing that they hold for every standard derivation, in Sect. 6, and then showing that LOU derivations are standard, in Sect. 7; 2. the normal form and projection properties, by a careful study of unfoldings and LO/LOU derivations, in Sect. 8; 3. the syntactic bound property, passing through the abstract notion of nested derivation, in Sect. 9; 4. the selection property, by exhibiting a polynomial algorithm to test whether a redex is useful or not, in Sect. 10.

Standard Derivations

We need to show that LOU derivations have the subterm property. It could be done directly. However, we will proceed in an abstract way, by first showing that the subterm property is a property of standard derivations for the LSC, and then showing (in Sect. 7) that LOU derivations are standard. The detour has the purpose of shedding a new light on the notion of standard derivation, a classic concept in rewriting theory. For the sake of readability, we use the concept of residual without formally defining it (see [START_REF] Accattoli | A Nonstandard Standardization Theorem[END_REF] for details).

Definition 6.1 (Standard Derivation).

A derivation ρ : R1; . . . ; Rn is standard if Ri is not the residual of a redex Q ≺LO Rj for every i ∈ {2, . . . , n} and j < i.

The same definition where terms are ordinary λ-terms gives the ordinary notion of standard derivation.

Note that any single reduction step is standard. Then, notice that standard derivations select redexes in a left-to-right and outside-in way, but they are not necessarily LO. For instance, the derivation

((λx.y)y)[y z] →ls ((λx.z)y)[y z] →ls ((λx.z)z)[y z]
is standard even if the LO redex (i.e. the dB-redex on x) is not reduced. The extension of the derivation with ((λx.z)z)[y z] →dB z[x z][y z] is not standard. Last, note that the position of a lsstep is given by the substituted occurrence and not by the ES, that is (xy

)[x u][y t] →ls (xt)[x u][y t] →ls (ut)[x u][y t] is not standard.
In [START_REF] Accattoli | A Nonstandard Standardization Theorem[END_REF] it is showed that in the full LSC standard derivations are complete, i.e. that whenever t → * u there is a standard derivation from t to u. The shallow fragment does not enjoy such a standardisation theorem, as the residuals of a shallow redex need not be shallow. This fact however does not clash with the technical treatment in this paper. The shallow restriction is indeed compatible with standardisation in the sense that: 1. The linear LO strategy is shallow: if the initial term is a λ-term then every redex reduced by the linear LO strategy is shallow (every non-shallow redex R is contained in a substitution, and every substitution is involved in an outer redex Q); 2. ≺LO-ordered shallow derivations are standard: any strategy picking shallow redexes in a left-to-right and outside-in fashion does produce standard derivations (it follows from the easy fact that a shallow redex R cannot turn a non-shallow redex Q s.t.

Q ≺LO R into a shallow redex). Moreover, the only redex swaps we will consider (Lemma 7.1) will produce shallow residuals.

We are now going to show a fundamental property of standard derivations. The subterm property states that at any point of a derivation ρ : t → * u only sub-terms of the initial term t are duplicated. It immediately implies that any rewriting step can be implemented in time polynomial in the size |t| of t. A first consequence is the fact that |u| is linear in the size of the starting term and the number of steps, that we call the no size explosion property.

These properties are based on a technical lemma relying on the notions of box context and box subterm, where a box is the argument of an application or the content of an explicit substitution, corresponding to explicit boxes for promotions in the proof nets representation of λ-terms with ES. Definition 6.2 (Box Context, Box Subterm). Let t be a term. Box contexts (that are not necessarily shallow) are defined by the following grammar, where C is an generic context:

B ::= t • | t[x • ] | C B . A box subterm of t is a term u s.t. t = B u for some box context B.
We are now ready for the lemma stating the fundamental invariant of standard derivations. From the invariant, one easily obtains the subterm property, that in turn implies the no size explosion and the trace properties. Corollary 6.4. Let ρ : t → k u be a standard derivation. 1. Subterm: every →ls-step in ρ duplicates a subterm of t.

No Size Explosion

: |u| ≤ (k + 1) • |t|. 3. Trace: if t is an ordinary λ-term then |u| [•] = |ρ|dB.
The subterm property of standard derivations is specific to evaluation in the LSC, and it is the crucial half of the notion of mechanisable strategy. It allows to see the standardisation theorem as the unveiling of a very abstract machine, hidden inside the calculus itself.

Let us conclude the section with a further invariant of standard derivations. It is not needed for the invariance result, but it sheds some light on the shallow subsystem under study. Let a term be shallow if its substitutions do not contain substitutions. The invariant is that if the initial term is a λ-term then standard shallow derivations involve only shallow terms. This fact is the only point of this section relying on the assumption that reduction is shallow (the standard hypothesis is also necessary, consider

(λx.x)((λy.y)z) →dB (λx.x)(y[y z]) →dB x[x y[y z]]).
Lemma 6.5 (Shallow Invariant). Let t be a λ-term and ρ : t → k u be a standard derivation. Then u is a shallow term.

The Subterm and Trace Properties, via Standard Derivations

While LO derivations are evidently standard, a priori LOU derivations may not be standard, if the reduction of a useful redex R could turn a useless redex Q ≺LO R into a useful redex. Luckily, this is not possible, i.e. uselessness is stable by reduction of ≺LO-majorants, as proved by the next lemma.

Lemma 7.1 (Useless Persistence). Let R : t →ls u be a useless redex and

Q : t → r be a useful redex s.t. R ≺LO Q. The unique residual R ′ of R after Q is shallow and useless.
Using the lemma above and a technical property of standard derivations (the enclave axiom, see [START_REF] Accattoli | A Nonstandard Standardization Theorem[END_REF]) we obtain: Proposition 7.2 (LOU-Derivations Are Standard). Let ρ be a LOU derivation. Then ρ is a standard derivation.

We conclude applying Corollary 6.4: Corollary 7.3 (Subterm and Trace). LOU derivations have the subterm and the trace properties, and only involve shallow terms.

The Normal Form and Projection Properties

For the normal form property it is necessary to show that the position of a redex in an unfolded term t → can be traced back to the position of a useful redex in the original term t. Such a property requires a very detailed and technical study of unfoldings and position, and it is thus omitted (see [START_REF] Accattoli | Beta reduction is invariant, indeed[END_REF]).

By induction on t and using the omitted property we obtain:

Proposition 8.1 (Normal Form). Let t be a LSC term in useful normal form. Then t → is a β-normal form.
The next lemma shows that useful reductions match their intended semantics, in the sense that every useful redex contributes somehow to a β-redex. It is not needed for the invariance result.

Lemma 8.2 (Inverse Normal Form). Let t be a LSC term s.t. t

→ is a β-normal form. Then t is a useful normal form.

For the projection property, we first show that the LO order is stable by unfolding, that in turn requires to show that it is stable by substitution. By induction on t: Lemma 8.3 (≺LO and Substitution). Let t be a λ-term, S ≺p t and P ≺p t. If S{x u} ≺LO P {x u} then S ≺LO P . Lemma 8.4 (≺LO and Unfolding). Let t be a LSC term, S ≺p t and P ≺p t. If S → ≺LO P → then S ≺LO P .

The next lemma deals with the hard part of the projection property. We use → β for β-reduction at top level. Lemma 8.5 (LOU dB-Step Projects on → LOβ ). Let t be a LSC term and R : t = S r →dB S p = u with r →dB p. Then: 1. Projection: R

→ : t → = S → r → S → β S → p → S = u → with r → S → β p → S ; 2. Minimality: if moreover R is the LOU redex in t then R → is the LO β-redex in t → .
The first point is an ordinary projection of reductions. The second one is instead involved, as it requires to prove that if R → is not LO then R is not LOU, i.e. to be able to somehow trace LO redexes back through unfoldings. The proof is by induction on S, that by hypothesis is the position of the LOU redex. The difficult case -not surprisingly -is when S = P [x p], and where Lemma 8.3 is applied. The proof also uses the normal form property, when the position S is on the argument p of an application

rp. Since R is LOU, r is useful-normal. To prove that R → is the LO β redex in (rp) → = r → p →
we use the fact that r → is normal. Projection of derivations now follows as an easy induction: Theorem 8.6 (Projection). Let t be a LSC term and ρ : t → * LOU u. Then there is a LO β-derivation ρ

→ : t → → * β u → s.t. |ρ → | = |ρ|dB.

The Syntactic Bound Property, via Nested Derivations

In this section we show that LOU derivations have the syntactic bound property. Instead of proving this fact directly, we introduce an abstract property, the notion of nested derivation and then prove that 1) nested derivations ensure the syntactic bound property, and 2) LOU derivations are nested. Such an approach helps to understand both LOU derivations and the syntactic bound property.

Definition 9.1 (Nested Derivation). Two ls-steps t →ls u →ls r are nested if the second one substitutes on the subterm substituted by the first one, i.e. if exist S and P s.t. the two steps have the compact form S x →ls S P y →ls S P u . A derivation is nested if any two consecutive substitution steps are nested.

For instance, the first of the following two sequences of steps is nested while the second is not:

(xy)[x yt][y u] →ls ((yt)y)[x yt][y u] →ls ((ut)y)[x yt][y u]; (xy)[x yt][y u] →ls ((yt)y)[x yt][y u] →ls ((yt)u)[x yt][y u].
The idea is that nested derivations ensure the syntactic bound property because no substitution can be used twice in a nested sequence u → k ls r, and so k is necessarily bounded by |u| [•] . Lemma 9.2 (Nested + Subterm = Syntactic Bound). Let t be a λ-term, ρ : t → n u → k ls r be a derivation having the subterm property and whose suffix u → k ls r is nested. Then k ≤ |u| [•] . We are left to show that our small-step implementation of β -LOU derivations -indeed are nested derivations with the subterm property. We already know that they have the subterm property (Corollary 7.3), so we only need to show that they are nested. Using an omitted technical lemma, a case analysis on why a given substitution step is LOU proves: Proposition 9.3. LOU derivations are nested, and so they have the syntactic bound property.

At this point, we proved all the abstract properties implying the high-level implementation theorem.

The Selection Property, or Computing Functions in Compact Form

This section proves the selection property for LOU derivations, which is the missing half of the proof that they are mechanisable, i.e. that they enjoy the low-level implementation theorem. The proof consists in providing a polynomial algorithm for testing the usefulness of a substitution step. The subtlety is that the test has to check whether a term in the form t → S contains a β-redex, or whether it is an abstraction, without explicitly computing t → S (which, of course, takes exponential time in the worst case). If one does not prove that this can be done in time polynomial in (the size of) t and S, then firing each reduction step can cause an exponential blowup! Our algorithm consists in the simultaneous computation of 4 correlated functions on terms in compact form, two of which will provide the answer to our problem. We need some abstract preliminaries about computing functions in compact form.

A function f from n-uples of λ-terms to a set A is said to have arity n, and we write f : n → A in this case. The function f is said to be:

• Efficiently computable if there is a polynomial time algorithm A such that for every n-uple of λ-terms (t1, . . . , tn), the result of A(t1, . . . , tn) is precisely f (t1, . . . , tn). ). An example of function is alpha : 2 → B, which given two λterms t and u, returns true if t and u are α-equivalent and false otherwise. In [START_REF] Accattoli | On the invariance of the unitary cost model for head reduction[END_REF], alpha is shown to be efficiently computable in compact form, via a dynamic programming algorithm B alpha taking in input two LSC terms and computing, for every pair of their subterms, whether the (unfoldings) are α-equivalent or not. Proceeding bottom-up, as usual in dynamic programming, allows to avoid the costly task of computing unfoldings explicitly, which takes exponential time in the worst-case. More details about B alpha can be found in [START_REF] Accattoli | On the invariance of the unitary cost model for head reduction[END_REF].

Each one of the functions of our interest take values in one of the following sets: VARS = the set of finite sets of variables

B = {true, false} T = {var(x) | x is a variable} ∪ {lam, app}
Elements of T represent the nature of a term. The functions are:

• nature : 1 → T, which returns the nature of the input term;

• redex : 1 → B, which returns true if the input term contains a redex and false otherwise; • apvars : 1 → VARS, which returns the set of variables occurring in applicative position in the input term; • freevars : 1 → VARS, which returns the set of free variables occurring in the input term. Note that they all have arity 1 and that showing redex and nature to be efficiently computable in compact form relatively to a context is precisely what is required to prove the efficiency of useful reduction.

The four functions above can all be proved to be efficiently computable (in the three meanings). It is convenient to do so by giving an algorithm computing the product function nature × redex × apvars × freevars : 1 → T × B × VARS × VARS (which we call g) compositionally, on the structure of the input term, because the four function are correlated (for example, tu has a redex, i.e. redex (tu) = true, if t is an abstraction, i.e. if nature(t) = lam). The algorithm computing g on terms is Ag and is defined in Figure 1.

The interesting case in the algorithms for the two compact cases is the one for ES, that makes use of a special notation: given two sets of variables V, W and a variable x, V ⇓x,W is defined to be V if x ∈ W and the empty set ∅ otherwise. The algorithm Bg computing g on LSC terms is defined in Figure 2. The algorithm computing g on pairs in the form (t, S) (where t is a LSC term and S is a shallow context) is defined in Figure 3.

First of all, we need to convince ourselves about the correctness of the proposed algorithms: do they really compute the function g? Actually, the way the algorithms are defined, namely by primitive recursion on the input terms, helps very much here: a simple induction suffices to prove the following: Proposition 10.1. The algorithms Ag,Bg,Cg are all correct, namely for every λ-term t, for every term u and for every context S, it holds that

Ag(t) = g(t); Bg(u) = g(u → ); Cg(u, S) = g(u → S
). The way the algorithms above have been defined also helps while proving that they work in bounded time, e.g., the number of recursive calls triggered by Ag(t) is linear in |t| and each of them takes polynomial time. As a consequence, we can also easily bound the complexity of the three algorithms at hand. Proposition 10.2. The algorithms Ag,Bg,Cg all work in polynomial time. Thus LOU derivations are mechanisable.

Summing Up

The various ingredients from the previous sections can be combined together so as to obtain the following result: Theorem 11.1 (Invariance). There is an algorithm which takes in input a λ-term t and which, in time polynomial in #→ LOβ (t) and |t|, outputs an LSC term u such that u → is the normal form of t.

As we have already mentioned, the algorithm witnessing the invariance of λ-calculus does not produce in output a λ-term, but a compact representation in the form of a term with ES. Theorem 11.1, together with the fact that equality of terms can be checked efficiently in compact form, entail the following formulation of invariance, akin in spirit to, e.g., Statman's Theorem [START_REF] Statman | The typed lambda-calculus is not elementary recursive[END_REF]: Corollary 11.2. There is an algorithm which takes in input two λ-terms t and u and checks whether t and u have the same normal form in time polynomial in #→ LOβ (t), #→ LOβ (u), |t|, and |u|.

If one instantiates Corollary 11.2 to the case in which u is a normal form, one obtains that checking whether the normal form of any term t is equal to u can be done in time polynomial in #→ LOβ (t), |t|, and |u|. This is particularly relevant when the size of u is constant, e.g., when the λ-calculus computes decision problems and the relevant results are truth values.

Please observe that whenever one (or both) of the involved terms are not normalisable, the algorithms above (correctly) diverge.

Discussion

Here we further discuss invariance and some potential optimisations, that, however, are outside the scope of this work (which only deals with asymptotical bounds and is thus foundational in spirit). where Ag(t) = (nt, bt, Vt, Wt);

Ag(tu) = (app, bt ∨ bu ∨ (nt = lam), Vt ∪ Vu ∪ {x | nt = var(x)}, Wt ∪ Wu)
where Ag(t) = (nt, bt, Vt, Wt) and Ag(u) = (nu, bu, Vu, Wu); where Bg(t) = (nt, bt, Vt, Wt);

Bg(tu) = (app, bt ∨ bu ∨ (nt = lam), Vt ∪ Vu ∪ {x | nt = var(x)}, Wt ∪ Wu)
where Bg(t) = (nt, bt, Vt, Wt) and Bg(u) = (nu, bu, Vu, Wu);

Bg(t[x u]) = (n, b, V, W )
where Bg(t) = (nt, bt, Vt, Wt) and Bg(u) = (nu, bu, Vu, Wu) and: Let us stress that the study of invariance is about mechanisability rather than efficiency. One is not looking for the smartest or shortest evaluation strategy. But rather, for one that does not hide the complexity of its implementation in the cleverness of its definition, as it is the case for Lévy's optimal evaluation. Indeed, an optimal derivation can be even shorter then the shortest sequential strategy, but-as shown by Asperti and Mairson [START_REF] Asperti | Parallel beta reduction is not elementary recursive[END_REF]-its definition hides hyper-exponential computations, and consequently optimal derivations do not provide an invariant cost model. The leftmost-outermost strategy, is a sort of maximally unshared normalising strategy, where redexes are duplicated whenever possible (and unneeded redexes are never reduced), somehow dually with respect to optimal derivations. It is exactly this inefficiency that induces the subterm property, the key point for its mechanisability. It is important to not confuse two different levels of sharing: our LOU derivations share subterms, but not computations, while Lévy's optimal derivations do the opposite. By sharing computations, they collapse the complexity of many steps into a single one, making the number of steps an unreliable measure.

nt = var(x) ⇒ n = nu; nt = var(y) ⇒ n = var(y); nt = lam ⇒ n = lam; nt = app ⇒ n = app; b = bt ∨ (bu ∧ x ∈ Wt) ∨ ((nu = lam) ∧ (x ∈ Vu)); V = (Vt -{x}) ∪ Vu ⇓x,W t ∪ {y | nu = var(y) ∧ x ∈ Vt}; W = (Wt -{x}) ∪ Wu ⇓x,W t
Call-by-Value and Call-by-Need. Call-by-name evaluation is in many cases less efficient than call-by-value or call-by-need evaluation. Since we follow the call-by-name policy, the same kind of inefficiency shows up here. However, as already said, invariance is not about absolute efficiency: call-by-name and call-by-value are incomparable -sometimes the former can even be exponentially faster than the latter, sometimes the other way around-but this fact does not forbid both to be invariant, i.e. reasonably mechanisable.

We did not prove call-by-value/need invariance. Nonetheless, we strove to provide an abstract view of both the problem and of the architecture of our solution, having already in mind the adaptation to call-by-value/need λ-calculi. Recently, the first author and Sacerdoti Coen show [START_REF] Accattoli | On the Value of Variables[END_REF] that (in the much simpler weak case) these policies provide an improved high-level implementation theorem, where evaluation in the LSC has a linear overhead, rather than quadratic.

Usefulness. Another source of inefficiency is the fact that at each reduction step we need to check whether the LO redex is useful before firing it, and this potentially amounts to doing a global analysis of the term. One could imagine decorating terms with additional tags in such a way that the check for usefulness becomes local and updating tags is not too costly, so that useful reduction may be implemented more efficiently. In particular, building on the already established relationships between the LSC and abstract machines [START_REF] Accattoli | Distilling Abstract Machines[END_REF], we expect to be able to design an abstract machine implementing LOU evaluation and testing for usefulness in time linear in the size of the starting term.

Conclusions

This work is the last tale in the long quest for an invariant cost model for the λ-calculus. In the last ten years, the authors have been involved in various works in which parsimonious time cost models have been shown to be invariant for more and more general notions of reduction, progressively relaxing the conditions on the use of sharing [START_REF] Accattoli | On the invariance of the unitary cost model for head reduction[END_REF][START_REF] Lago | The weak lambda calculus as a reasonable machine[END_REF][START_REF] Lago | On constructor rewrite systems and the lambda calculus[END_REF]. None of the results in the literature, however, concerns reduction to normal form as instead we do here.

By means of explicit substitutions-our tool for sharing-we provided the first full answer to this long-standing open problem: we proved that the λ-calculus is indeed a reasonable machine, by showing that the length of the leftmost-outermost derivation to normal form is an invariant cost model.

The solution required the development of a whole new toolbox: an abstract deconstruction of the problem, a detailed study of unfoldings, a theory of useful derivations, and a general view of functions efficiently computable in compact form. Along the way, we showed that standard derivations for explicit substitutions enjoy the crucial subterm property. Essentially, it ensures that standard derivations are mechanisable, unveiling a very abstract notion of machine hidden deep inside the λ-calculus itself, and also a surprising perspective on the standardisation theorem, a classic result apparently unrelated to the complexity of evaluation.

Among the downfalls of our results, one can of course mention that proving systems to characterise time complexity classes equal or larger than P can now be done merely by deriving bounds on the number of leftmost-outermost reduction steps to normal form. This could be useful, e.g., in the context of light logics [START_REF] Baillot | Light types for polynomial time computation in lambda calculus[END_REF][START_REF] Coppola | Light logics and the call-by-value lambda calculus[END_REF][START_REF] Gaboardi | A soft type assignment system for λ-calculus[END_REF]. The kind of bounds we obtain here are however more general than those obtained in implicit computational complexity (since we deal with a universal model of computation).

While there is room for finer analyses (e.g. studying call-byvalue or call-by-need evaluation), we consider the understanding of time invariance essentially achieved. However, the study of complexity measures for λ-terms is far from being over. Indeed, the study of space complexity for functional programs has only made its very first steps [START_REF] Lago | Functional programming in sublinear space[END_REF][START_REF] Gaboardi | A logical account of PSPACE[END_REF][START_REF] Schöpp | Stratified Bounded Affine Logic for Logarithmic Space[END_REF], and not much is known about invariant space cost models.

  λz.(zz)] = (λz.(zz))λz.(zz). We extend implicit substitutions and unfoldings to contexts by setting • {x t} := • and • → := • (all other cases are defined as expected, e.g. S[x t]

Theorem

Lemma 5 . 3 (

 53 Totality of ≺LO). If C ≺p t and D ≺p t then either C ≺LO D or D ≺LO C or C = D.

Lemma 6 . 3 (

 63 Standard Derivations Preserve Boxes on Their Right). Let ρ : t0 → k t k → t k+1 be a standard derivation and let S be the position of the last contracted redex, k ≥ 0, and B ≺p t k+1 be a box context s.t. S ≺LO B. Then the box subterm u identified by B (i.e. s.t. t k+1 = B u ) is a box subterm of t0.

  Ag(x) = (var(x), false, ∅, {x}); Ag(λx.t) = (lam, bt, Vt -{x}, Wt -{x})

Figure 1 .

 1 Figure 1. Computing g in explicit form.

Figure 2 .Figure 3 .

 23 Figure 2. Computing g in implicit form.

  Trace: the number |u| [•] of ES in u is exactly the number |ρ|dB of dB-steps in ρ; 4. Syntactic Bound: the length of a sequence of substitution steps from u is bounded by |u| [•]

	form then u →	is a -
	normal form. 2. Projection: ρ → : t * u → and |ρ → | = |ρ|dB.
	3.

  |ρ| = |ρ|dB + |ρ|ls ≤ |ρ|dB + |ρ| 2 dB = O(|ρ| 2 dB ). For the low-level part we rely on the following notion. Definition 4.3. A strategy X on LSC terms is mechanisable if given a derivation ρ : t * X u: 1. Subterm: the terms duplicated along ρ are subterms of t. 2. Selection: the search of the next X redex to reduce in u takes polynomial time in |u|.

	|ρ|ls ≤ k i=1	i j=1 aj ≤ k i=1 |ρ|dB ≤ |ρ| 2 dB .
	Finally,
		i j=1 aj. Then:
		|ρ|ls = k i=1 bi ≤ k i=1	i j=1 aj.
		Note that i j=1 aj ≤ k j=1 aj = |ρ|dB and k ≤ |ρ|dB. So

[•]

. By the trace property we obtain |pi| [•] = i j=1 aj, and so bi ≤

  [START_REF] Accattoli | On the Value of Variables[END_REF].4 (Low-Level Implementation). Let X be a mechanisable strategy. Then there is an algorithm that on input t and k outputs k

X (t), and which works in time polynomial in k and |t|. Proof. By the subterm property, implementing one step takes time polynomial (if not linear) in |t|. An immediate consequence of the subterm property is the no size explosion property, i.e. that |u| ≤ (k +1)•|t|. By the selection property selecting the next redex takes time polynomial in |u|, that by the no size explosion property is polynomial in k and |t|. The composition of polynomials is again a polynomial, and so selecting the redex takes time polynomial in k and |t|. Hence, the reduction can be implemented in polynomial time.

  5.1 (Useful/Useless Steps and Derivations). A useful step is either a dB-step or a ls-step S x →ls S r (in compact form) s.t. r → S : 1. either contains a β-redex, 2. or is an abstraction and S is an applicative context. A useless step is a ls-step that is not useful. A useful derivation (resp. useless derivation) is a derivation whose steps are useful (resp. useless).

	Let us give some examples. The steps
	(tx)[x (λy.y)u] →ls (t((λy.y)u))[x (λy.y)u];
	(xt)[x λy.y] →ls ((λy.y)t)[x λy.y];
	are useful because they move or create a β/dB-redex (first and
	second case of the definition, respectively) while
	(λx.y)[y zz] →ls (λx.(zz))[y zz]
	is useless. However, useful steps are subtler, for instance
	(tx)[x zz][z λy.y] →ls (t(zz))[x zz][z λy.y]
	is useful also if it does not move or create β/dB-redexes, because it
	does so up to relative unfolding, i.e. (zz) → • [z λy.y] = (λy.y)λy.y
	that is a β/dB-redex.

  • ≺O C for every context C = • ; 2. Contextual closure: If C ≺O D then E C ≺O E D for any context E. Note that ≺O can be seen as the prefix relation ≺p on contexts. 3. The left-to-right order: C ≺L D is defined by: 1. Application: If C ≺p t and D ≺p u then Cu ≺L tD; 2. Substitution: If C ≺p t and D ≺p u then C[x u] ≺L t[x D]; 3. Contextual closure: If C ≺L D then E C ≺L E D for any context E. 4. The left-to-right outside-in order: C ≺LO D if C ≺O D or C ≺L D:

	The following are a few examples. For every context C, it holds
	that • ≺L C. Moreover,
	(λx. • )t ≺O (λx.( • [y u])r)t;

•

  Efficiently computable in compact form if there is a polynomial time algorithm A such that for every n-uple of LSC terms (t1, . . . , tn), the result of A(t1, . . . , tn) is precisely

	f (t1 → , . . . , tn → ).
	• Efficiently computable in compact form relatively to
	a context if there is a polynomial time algorithm A
	such that for every n-uple of pairs of LSC terms
	and contexts ((t1, S1), . . . , (tn, Sn)), the result of
	A((t1, S1), . . . , (tn, Sn))) is precisely f (t1 → S 1	, . . . , tn → Sn

Acknowledgments

The second author is supported by the project ANR-12IS02001 "PACE".