Exponential Automatic Amortized Resource Analysis

Automatic amortized resource analysis (AARA) is a type-based technique for inferring concrete (non-asymptotic) bounds on a program’s resource usage. Existing work on AARA has focused on bounds that are polynomial in the sizes of the inputs. This paper presents and extension of AARA to exponential bounds that preserves the benefits of the technique, such as compositionality and efficient type inference based on linear constraint solving. A key idea is the use of the Stirling numbers of the second kind as the basis of potential functions, which play the same role as the binomial coefficients in polynomial AARA. To formalize the similarities with the existing analyses, the paper presents a general methodology for AARA that is instantiated to the polynomial version, the exponential version, and a combined system with potential functions that are formed by products of Stirling numbers and binomial coefficients. The soundness of exponential AARA is proved with respect to an operational cost semantics and the analysis of representative example programs demonstrates the effectiveness of the new analysis.


Introduction
"Time is money" is a phrase that also applies to executing software, most directly in domains such as on-demand cloud computing and smart contracts where execution comes with a explicit price tag.In such domains, there is an increasing interest in formally analyzing and certifying the precise resource usage of programs.However, the cost of formally verifying properties by hand is an obstacle to even getting projects off the ground.For this reason, it would be desirable if such resource analyses could be performed mostly automatically, with reduced burden on the programmer.
In this paper, we extend automatic amortized resource analysis (AARA) to cover exponential worst-case bounds.AARA is an effective type-based technique for deriving concrete (non-asymptotic) worst-case bounds, in particular for functional languages.It has been introduced by Hofmann and Jost [31] to derive linear bounds on the heap-space usage of strict first-order functional programs with lists.Subsequently, AARA has been extended to programs with recursive types and general resource metrics [34], higher-order functions [33], lazy evaluation [52], parallel evaluation [29], univariate polynomial bounds [27], multivariate polynomial bounds [23,25], session-typed concurrency [13], and side effects [38,46].However, none of the aforementioned works explores exponential bounds.
The idea of AARA is to enrich types with numeric annotations that represent coefficients in a potential function in the sense of amortized analysis [51].Bound inference is reduced to Hindley-Milner type inference extended with linear constraints for the numeric annotations.Advantages of the technique include compositionality, efficient bound inference via off-the-shelf LP solving, and the ability to derive bounds on the high-water mark for non-monotone resources like memory.A powerful innovation leveraged in polynomial AARA is the representation of potential functions as non-negative linear combinations of binomial coefficients.Their combinatorial identities yield simple and local typing rules and support a natural semantic understanding of types and bounds.Moreover, these potential functions are more expressive than non-negative linear-combinations of the standard polynomial basis.
However, polynomial potential is not always enough.Functional languages make it particularly easy to use exponentially many resources just by having two or more recursive calls.The following function subsetSum : int list → int → bool exemplifies this by naively solving the well-known NP-complete problem subset sum.In the worst case, it performs 3 * 2 |nums| − 2 Boolean and arithmetic operations (where |x| gives the length of the list x).Such a function could appear in a program with polynomial resource usage if applied to arguments of logarithmic size.In this case, polynomial AARA would not be able to derive a bound.Section 6 contains a relevant example.
To handle such functions, we introduce an extension to AARA that allows working with potential functions of the form f (n) = b n .This extension ex-ploits the combinatorial properties of Stirling numbers of the second kind [50] in much the same way that AARA currently exploits those of binomial coefficients.Moreover, we allow both multiplicative and additive mixtures of exponential and polynomial potential functions.The techniques used in this process could easily be applied to other potential functions in the future.
The paper first details a generalized AARA type system fit for reuse between polynomial, exponential, and other potential functions.We then instantiate this system with Stirling numbers of the second kind, yielding the first AARA that can infer exponential resource bounds.Finally, we pick out the characteristics that allow for mixing different families of potential functions and maximizing the space they express, and we instantiate the general system with products of exponential and polynomial potential functions.To focus on the main contribution, we develop the system for a simple first-order language with lists in which resource usage is defined with explicit tick expressions.However, we are confident that the results smoothly generalize to more general resource metrics, recursive types, and higher-order functions.As in previous work, we prove the soundness of the analysis with respect to a big-step cost semantics that models the high-water mark of the resource usage.

Language and Cost Semantics
Abstract Syntax To begin, we define an abstract binding tree (ABT, see [20]) underlying a simple strict first-order functional language.Expressions are in letnormal form to simplify the AARA typing rules.For code examples, however, we overlay the ABT with corresponding ML-based syntax.For example, 1::[], [1], and cons(1, nil) all represent the same list.
A program prog is a collection of functions as defined in the following grammar.The symbols lit , binop, and unop refer to standard literal values, binary operations, and unary operations respectively, of basic types (int , bool , etc.).The symbols f , x, and r refer to function identifiers, variables, and rational numbers, respectively.Expressions include function applications, conditionals, and the usual introduction and elimination forms for pairs and lists.They also include two special expressions: tick {r} and share.The former, tick {r}, is used to specify constant resource cost r.We allow r to be negative in the case of resources becoming available instead of being consumed.The latter, share(x 1 ; x 2 , x 3 .e),provides two copies of its argument x 1 for use in e.This is useful because the affine features of the AARA type system do not allow naive variable reuse.In practice, share can be left implicit by automatically preceding every variable usage by share.
To focus on the technical novelties, we keep function identifiers and variables disjoint, that is, the types of variables do not contain arrow types and functions are first-order.Higher-order functions can be handled as in previous AARA literature [25].As a further simplification, we only let functions take one argument; multiple arguments can be simulated with nested pairs.Finally, the language here only supports the inductive types of lists; future work could extend this to more general types as in other AARA literature [38,25,30,28].Operational Cost Semantics To define resource usage, AARA literature uses the operational big-step judgment V ⊢ e ⇓ v | (q, q ′ ) (see e.g.[22]) defined in Figure 1.This judgment means that, under the environment V , the expression e evaluates to the value v under some resource constraints given by the pair q, q ′ .The environment V maps variables to values.The resource constraints are that q is the high-water mark of resource usage, and q − q ′ is the net amount of resources consumed during evaluation.In other words, if one started with exactly as many resources needed to evaluate e, that amount would be q, and the amount of leftover resources after evaluation would be q ′ .It is essential to track both of these values to model resources that might be returned after use, like space.Space usage usually has a positive high-water mark but no net resource consumption, as space could be reused.
The above big-step judgment only formalizes terminating evaluations.To deal with divergence, the additional judgment V ⊢ e ⇓ • | q has been introduced [26].This merely drops the parts of the previous judgment relevant to post-termination, focusing on partial evaluation.It means that some partial evaluation of e uses a high-water mark of q resources.Should it exist, the largest q such that V ⊢ e ⇓ • | q holds would be the high-water mark of resource usage across any partial evaluation of e.The formal definition can be found in Figure 2.

Automatic Amortized Resource Analysis
Here we lay out a generalized version of the AARA system with the potential functions abstracted.Existing AARA literature is specialized to polynomial functions (see e.g.[27]).This existing polynomial system may be obtained as an instantiation, as may the exponential system that we introduce in Section 4.
AARA uses the potential (or physicist's) method to account for resource use, as is commonly used in amortized analyses.The potential method uses the physical analogy of converting between potential and actual energy that can be used to perform work.Whereas a physicist might find potential in the chemical bonds of a fuel, however, AARA places it in the constructors of lists.
To prime intuition with an example, consider paying a resource for each :: operation performed in the following code.It performs snoc, which is like cons but adds onto the back of the list rather than the front.The resource consumption of snoc x xs as defined by the t ick expressions is 1 + |xs|.Using the potential method, we can justify this bound as follows.q = max(r, 0) q ′ = max(−r, 0) If 1 resource is initially available, then the base case of the empty list can be paid for.If there is 1 stored per element of the list then 1 resource is released in the cons case of the pattern match.This suffices to pay for the additional :: operation.The remaining potential on xs can be assigned to tl for the recursive call.One can sum these costs to infer that the initial potential 1 + |xs| covers the cost of all the :: operations.The AARA type system could describe this with the typing L 1 (Z) for x s (describing the linear potential in the superscript) and → L 0 (Z) for snoc (describing the initial/remaining resources above the arrow).Another valid type is Z × L 2 (Z) , which could be used in a context where the result of snoc must be used to pay for additional cost.Types The AARA system laid out here supports the types given below.The symbol F gives the types of functions, where q and q ′ are non-negative rationals.The symbol S gives the remaining non-function types, where basic stands for the basic types like int or unit, and the resource annotation P is an indexed family of rationals representing the coefficients in a linear combination of basic potential functions.V The typing rules for these types are given in Figure 3 and explained in the following sections.The values of these types are the usual values.
Potential To understand typing rules, it is necessary to define potential.The following potential constructs are generalized from polynomial AARA work [27].
As mentioned, P = (p i ) i∈I is in Q I as an indexed family of rationals.Each entry represents a coefficient in a linear combination of basic potential functions.This linearity makes it natural to overload the type of P as a vector or matrix of rationals, so it is treated as such whenever the context is appropriate.Finally, let those basic potential functions be fixed as some family (f i ) i∈I , where f i (0) = 0.
We define the potential represented with P using the function φ where The function φ yields the total potential on a list (excluding the potential of its elements) as a function of the list's size n and its potential annotation P .
We can then relate resource potential between different sizes of list with the shift operator ⊳ : Q I → Q I and constant difference operator δ : Q I → Q.These functions need only satisfy the following property equation.
Though we leave open the explicit definition of these functions for generality, we only later work with instances of them that are linear operators, such that Equation 1 denotes a linear recurrence.Such a refinement leaves ⊳P and δ(P ) linear functions of P .These functions come in handy for understanding the stored potential in a value of a given type, defined by the potential function Φ as follows.
We often need to measure the potential across an entire evaluation context of typed values V : Γ given by a typing context Γ and variable bindings V .We do so by extending the definition of potential Φ as follows.
Finally, we can use these definitions to obtain a closed-form expression for the potential over an entire list (including its elements) with the following: We induct over the structure of the list l.
For the empty list of length 0: i=1 Φ(a i : A) We can apply Lemma 1 to the previously defined function snoc to see the change in potential between input and output.This difference in potential should bound the resources consumed.For this case, the basic potential functions (f i ) only need contain λn.n, and we can let ⊳(p) = p = δ((p)).Letting y be the result of snoc x xs, the type Z × L 1 (Z) This is exactly the amount of resources consumed, so the bound is tight.In this work we only consider so-called univariate potential, wherein every term in the potential sum is dependent on the length of only one input list.However, different univariate potential summands may depend on different inputs, and thus univariate potential may still be multivariate.The term multivariate potential refers to using more general multivariate functions for potential.There is existent work on multivariate potential using polynomial functions [24].We expect that the work here extends to multivariate potential similarly.
Typing Rules The typing rules in Figure 3 use the judgment Σ; Γ q q ′ e : A. In this typing judgment, Γ maps variables to types, while Σ maps function labels to sets of types.This judgment holds when, in the typing environment given by Σ and Γ , the expression e is of type A, subject to the constraints that q and q ′ are the amount of available resources before and after some evaluation of e. Unlike the judgment V ⊢ e ⇓ v | (q, q ′ ), these values need not be tight.
By expressing available resources on the turnstile, and potential resources in the types given by Σ, Γ , and A, the type system is set up to formalize the reasoning of the potential method.Theorem 1 shows that it is sound with respect to the operational semantics of Section 2.
Many typing rules preserve the total resource potential they are given, consuming none of it themselves.They therefore usually either have no explicit interaction with potential (e.g.Lit ) or pass around exactly what they are given (e.g.Let).All basic rules in the first block of Figure 3 fit this characterization.
The typing rules concerning functions in second block of Figure 3 are the only to make use of Σ.For each function f defined in prog via f unc{f }(x.e),Σ(f ) refers to the set of types that its body e could be given.That we allow for sets of types is important because recursive calls to a function may not always make use of a type with the same resource annotations; this is called resourcepolymorphic recursion.Despite these rules capturing the intuition behind typing resource-polymorphic recursion, they are not used in existing implementation, as they lead to infinite type derivations.Nonetheless there exists an effective way to type resource-polymorphic recursion with a finite derivation; see [26].In the examples provided in this article, it usually suffices to consider only resourcemonomorphic recursion, wherein inner and outer calls use the same annotation.
All of the rules discussed so far are simply those of existing AARA literature with their parameter for operation cost set to 0 (see e.g.[27]).This does not change their generality, as such constant cost can (and could already in prior work) be simulated using tick .Similarly, non-constant costs could be simulated by running helper functions using tick the appropriate number of times.
The remaining rules cover sharing, subtype-weakening, and the rules concerning lists.Weakening, though not listed, is also allowed.
Sharing is a form of contraction.By sharing, the rest of the typing rules can become affine, allowing only single usages of a given variable.Intuitively, sharing is meant to prevent duplicating potential across multiple usages of a variable, and instead split the potential across them.The rules for the sharing judgment, indicating how to split potential, can be found in Figure 4.Note that the rule ShareList adds indexed collections of rationals; this should be interpreted pointwise, as if the addends were vectors or matrices.
Subtype-weakening is a form of subtyping based on potential.It discards potential on a list, weakening the upper bound on resources it represents.This rule follows all usual subtyping rules, as well as Subtype from Figure 4. Relaxing behaves similarly, but loosens the bounds on the available resources instead.
The intuition for the rules concerning lists in the last block of Figure 3 is that total resources should be conserved between constructions and destructions.Because δ(P ) expresses the difference in potential, it is exactly how many resource units are released after a pattern match on a list of type L P (A).For the same reason, it is also how many need to be stored when reversing the process and putting an element on a list of type L ⊳P (A).Finally, when a list is empty, it has no room to store potential.Every potential function f i maps 0 to 0, so an empty list can safely be assigned any scalar of zero potential.

Soundness
The soundness of the type system is expressed with the following theorem.It states that the evaluation of an expression e does not require more resources than initially present, and (should evaluation terminate) it leaves at least as many resource as dictated.The proof is a straightforward generalization of the version from [27], but we nonetheless reproduce the proof below.
Theorem 1.Let Σ; Γ q q ′ e : B and V provide the variable bindings for Γ Proof.Assume V binds Γ 's variables and perform nested induction on the type derivation and operational judgment for an expression in let-normal form.We show the induction below only for the terminating operational judgment cases, but the partial-evaluation cases are nearly identical.
(Base Non-Cons) Suppose the last rule applied in the typing derivation is any non-Cons base case, i.e., Lit , Var , Unop, Binop, Pair , Nil , or Tick .Then assume the appropriate terminating operational judgment rule applies.In such a case, one finds p ≤ q, p ′ ≥ q ′ , and Φ(v : B) = Φ(V : Γ ).This and the non-negativity of potential are sufficient to satisfy the desired inequalities.
(Base Cons) Suppose the last rule is Cons, so q = δ(P ) and q ′ = 0. Assume the Cons operational judgment applies, so that p = p ′ = 0. Note Φ(v h :: v t : L P ) is equal to δ(P ) + Φ(v h : A) + Φ(v t : L P (A)) by definition.This identity and the non-negativity of potential satisfy the desired inequalities.
(Step Implicit Inequalities) Suppose the last rule is one of SubWeakL, SubWeakR, Relax , or substructural weakening, and assume some operational judgment applies.Each typing requires a similar typing judgment as a premiss.Further, none changes any values, so the same operational judgment still applies.Thus, the inductive hypothesis applies, and gives almost the inequalities we need.Each case provides the inequalities needed to finish.For subtype-weakening, it is sufficient note that C <: D entails Φ(v : C) ≥ Φ(v : D), since C is pointwise greater-then-or-equal to D. For relax , the premisses of the relax rule directly include the inequalities needed to complete the case.And we can complete the substructural weakening case by noting that the non-negativity of potential entails Φ(V : Γ, v : A) ≥ Φ(V : Γ ).
(Step Let) Suppose the last rule is Let, and suppose its operational judgment applies.The premisses of the typing rule require that Σ; Γ 1 q r e 1 : A and Σ; Γ 2 , x : A r q ′ e 2 : B. The premisses of the operational judgment require that , where p = s + max(t − s ′ , 0) and p ′ = t ′ + max(s ′ − t, 0).Applying the inductive hypothesis to these premiss pairs and adding the resulting inequalities cancels terms to complete the case.
(Step Cond) Suppose the last rule is Cond , and that either of the CondT or CondF operational judgments apply.In either case, applying the inductive hypothesis to its premiss and the premiss of Cond gives the needed inequalities.
(Step PMat) Suppose that the last rule applied is PMat , so that Γ = Γ ′ , x : A 1 × A 2 .This rule would require as a premiss that Σ; Γ ′ , x 1 : A 1 , x 2 : A 2 q q ′ e ′ : B, for e ′ the body of the match statement e.Suppose the PMat operational judgment applies.This judgment requires as a premiss that , where the value of x is (v 1 , v 2 ).Applying the inductive hypothesis to these premisses followed by the definitional identity Φ((v 1 , v 2 ) : (Step App) Suppose the last rule is App.Note that this rule requires Fun as a premiss, which in turn requires Σ; x : A q q ′ e ′ : B where e ′ is the body of the function being applied.If the App operational judgment applies, its premiss would require V [x ′ → V (x)] ⊢ e ⇓ v | (p, p ′ ).Although e ′ might not be a smaller expression than e, the operational judgment derivation still shrinks.This means the inductive hypothesis applies, and it gives the exact inequalities needed.
Type Inference Type inference for the Hindley-Milner part of the type system is decidable [21,41].The only new barrier for automating inference in AARA is obtaining witnesses for all the coefficients in each annotation P in a derivation.
Each typing rule naturally gives a set of linear constraints on the entries of P .If the relation given by ⊳ and δ can likewise be expressed with linear constraints, then all such constraints are linear.So long as |P | is finite, this forms a linear program.A linear program solver can then find minimal witnesses efficiently.
Existing AARA literature (see e.g.[27]), however, uses binomial coefficients as the basis functions for P , of which there are infinitely many.This nonetheless works because only a particular finite prefix of their set, − 1 , . . ., − k , are used as a basis in a given analysis.Each such prefix basis also yields the same locallydefinable shift operation: the linear equality ⊳p i = p i + p i+1 , where p k is the coefficient of − k and is 0 if the function is outside the prefix.As this is a linear relation, and each prefix is finite, inference can be performed via linear program.The prefix bases of binomial coefficients thereby form an infinite family of finite bases, each of which allows automated inference of resource polynomials up to a fixed degree in the AARA system.
As a caveat, not all programs use resources in a manner compatible with the AARA system.Indeed, it is undecidable whether or not a program uses e.g.polynomial amounts of resources, as this could solve the halting problem.

Stirling numbers of the second kind
n count the number of ways to form a k-partition of a set of n elements.These can be used to express exponential potential functions similarly to how binomial coefficients can express polynomial ones.In particular, we make use of Stirling numbers with arguments n, k offset by 1, n+1 k+1 , so that φ(n, P ) = i p i • n+1 i+1 .While other bases could also express exponential potential, these offset Stirling numbers have a few particularly desirable properties, which are described in this section.Simple Shift Operation Like binomial coefficients, the prefixes of the basis of the offset Stirling numbers of the second kind form an infinite family of finite bases, each of which allows automated inference in the AARA system.However, these potential functions are exponential rather than polynomial.
Stirling numbers of the second kind satisfy the recurrence n+1 k+1 = (k + 1) n k+1 + n k .This recurrence allows the ⊳ operation to have the same local definition for every annotation entry in every prefix basis: ⊳p i = (i + 1)p i + p i+1 , where p k is the coefficient of n+1 k+1 , and is 0 if the function index is outside the chosen prefix.Given this definition for ⊳ and letting δ(P ) = p 0 , we find p 0 + i ⊳p i n+1 i+1 = i p i n+2 i+1 , satisfying Equation 1.This shift operation yields a linear relation, as the coefficient of a given p i is a constant scalar.Thus, exactly like when using binomial coefficients, inference is automatable via linear programming.Certain other exponential bases, like Gaussian binomial coefficients, could be similarly automated.
), the offset Stirling numbers of the second kind can form a linear basis for the space of sums of exponential functions.Each function λn.b n with b ≥ 1 can be expressed as a linear combination of the functions λn.n+1 k+1 .The function λn.n+1 k+1 is also non-negative for natural n, and non-decreasing with respect to n.These are two natural properties to require of basic potential functions, since amortized analysis requires non-negative resources, and larger inputs should not usually become cheaper to process.Further, the properties are preserved by non-negative linear (i.e.conical) combination, and by ⊳ when defined with a non-negative linear recurrence -the combinations given by P and ⊳P always satisfy the two potential function properties.
Ensuring these properties for more general potential functions requires determining if such a function on a natural domain is always non-negative.This is non-trivial.In the existing literature on multivariate polynomials, we find this is undecidable in the worst case [40].However, restricting to non-negative linear (that is, conical ) combinations of non-negative, non-decreasing functionsas we have done here -gives simple linear constraints that ensure both desired properties.For finite bases, this is easily handled via linear programming.
When considering expressivity in this conical combination model of potential functions, one finds some otherwise-valid potential functions are not be expressible in the conical space given by the offset Stirling number functions.Nonetheless, Stirling number functions are a maximally expressive basis; it is not possible to express additional potential functions using a different basis without losing expressibility elsewhere.Notably, the standard exponential basis is not maximal in this sense.The formal statement of such maximal expressivity is generalized in the theorem below.Any finite, sequential subset of the offset Stirling number functions satisfy the prerequisites of this theorem, as do the binomial coefficient functions and other well-known functions like the Gaussian polynomials.Theorem 2. Let {f i } be a finite set of linearly independent functions on the naturals that are non-negative and non-decreasing.Let f i (n) be 0 until n ≥ i, and let i ≤ j imply that O(f i ) ⊆ O(f j ), with asymptotic equality only when i = j.Let L be the linear span (collection of linear combinations) of {f i }, and let C be its conical span (collection of conical combinations).
There does not exist another linearly independent basis {g i } with linear span L and conical span D C such that each function in {g i } is non-negative and non-decreasing.That is, {f i } has a maximally expressive conical span.
Proof.Suppose there is such a basis {g i }.We express each basis {f i } and {g i } with linear combinations of the other, and derive a contradiction.
If there is any function in the conical span D of {g i } that is not in C, then this is the case for some basis function g k .Because g k ∈ L, it can be written as a linear combination of {f i }; let i α i f i = g k .Because g k ∈ C, there is at least one coefficient α i < 0; let it be α m .In case there are multiple candidate elements g k , pick g k to be the basis function such that this index m is minimized.
We then see that for i > m is 0. This yields two observations: First, m < k, as otherwise the fastest-growing term of g k would be negative, but g k is never negative.Second, the term α m f m (m) is negative, yet g k ≥ 0, so it must be that i<m α i f i (m) > 0. Thus there exists a coefficient α p > 0 where p < m.Now we look at representing {f i } with {g i }.Because the conical span D contains C, it can represent each f i as a conical combination.Notably, a given f i cannot be represented only with functions outside of Ω(f i ), nor any function outside of O(f i ), due to growth rates.There is therefore at least one function in {g i } that is Θ(f i ), for each i.Since the linear span of these corresponding g i already has the same (finite) dimension as L, any additional functions would not be linearly independent.Due to this, we can say g i ∈ Θ(f i ) uniquely for each i.
Take f k in particular as a conical combination of {g i }.We now consider replacing each element of {g i } in that conical combination with its equivalent linear combination of elements of {f i }.Because of the above correspondence of growth rates, there must be a positive coefficient for g k .Because g k has positive weight α p on f p where p < m < k, another basis function g i in the conical combination must have negative weight on f p to cancel it out in their linear combination.However, g k was picked such that it had the lowest index m with negative weight across all {g i }; it is contradictory for there to be such a p < m.

Natural Semantics The values of n+1
k+1 count the number of ways to pick k nonempty disjoint subsets of n elements.Many programs with exponential resource use iterate over collections of subsets, so these numbers naturally arise.
Recall the naive solution to subset sum from the introduction.The algorithm iterates through all the subsets of numbers in the input list.When considering Fagin's descriptive complexity result that NP problems are precisely those expressible in existential second order logic [14], it becomes clear that naive solutions to any NP-complete problem fit this characterization: naively brute-forcing through second order terms to find an existential witness is just iterating through tuples of subsets.
Example Consider the naive solution to subset sum from the introduction.One can verify that the number of Boolean and arithmetic operations used on an input of size n is 3 * 2 n − 2 by induction.We find the same bound here by preceding each such operation with an explicit tick {1} operation.Thee AARA type system then verifies that the type of subsetSum is Here is the code again, with type annotations on each line tracking the amount of n+1 2 potential on lists, and comments tracking available constant potential.For clarity, the code is re-written in a let-normal form, and sharing locations are marked.The indicated values yield witnesses for the AARA typing rules, so we know via soundness that the difference between initial and ending potential gives an upper bound on how many operations were used.That difference is 1+3 * n+1 2 = 3 * 2 n − 2, where n is the size of nums, exactly the amount used.
Exponential terms with higher bases than 2 can come into play with more recursive calls, like in the code below enumerating the 3 n ways to put n labelled balls into 3 labelled bins.→ L 0,0 (L 0,0 (Z) × L 0,0 (Z) × L 0,0 (Z)) to ballBins3, where the superscript tracks n+1

Mixed Potential
It is possible to combine the existing polynomial potential functions with these new exponential potential functions to not only conservatively extend both, but further represent potentials functions with their products.This space represents functions in Θ(n k (b + 1) n ) for naturals k, b, and does so with terms of the form n k n+1 b+1 so that φ(n, P ) = b,k p b,k • n k n+1 b+1 .Note that for k or b equal to 0, the potential functions here reduce to the offset Stirling numbers or binomial coefficients, respectively.
The methods used to combine these potential functions here can easily be generalized to combine any two suitable sets.
Simple Shift Operation It is straightforward to find a linear recurrence for these products by distributing over their linear recurrences.
Expressivity The product of non-negative, non-decreasing functions is still nonnegative and non-decreasing, so products of valid potential functions are still valid.Soundness is preserved by letting p 0 be shorthand for the new constant function coefficient p 0,0 wherever it is used in Theorem 1.Moreover, maximality of expressivity is preserved, simply by giving index pairs the ordering relation (i 1 , i 2 ) ≤ (j 1 , j 2 ) ⇐⇒ i 1 ≤ j 1 ∧ i 2 ≤ j 2 and applying Theorem 2.
Example Consider bounding the number of Boolean and arithmetic operations in a variation of subset sum: single-use subset sum.Here the input may contain duplicate numbers that should be ignored, so as to treat the input as a true set.This is a trivial change to the mathematical problem, but one that real code might have to deal with, depending on the implementation of sets.
The code can be changed to handle this by removing all later duplicates of each number it reaches, so that later recursive calls will never see the number One can prove by induction that at most 4 * 2 n − n − 3 Boolean or arithmetic operations are required.Although this can be bounded with only exponential functions, the purely exponential potential system cannot reason about the exact (linear) cost associated with remove, and overestimates the bound to be in θ( 3 n ).This mixed system can provide a better (though still loose) bound of n2 n + 2 * 2 n − n − 1, giving a type of L 0,2,1 (Z) × Z 1/0 → bool to subSum1.After showing this derivation, we will show how to find the exact bound with AARA.
The following is the single-use subset sum code, with comments on each line tracking the amount of available resources on each line.For clarity, we indicate sharing and subtype-weakening locations.Demotion There is one special exception to the non-negativity of potential annotations that may be added due to the particular nature of the relation between binomial coefficients and Stirling numbers.It represents the concept of demoting exponential potential into polynomial potential.
The relevant relation is n+1 This allows a unit of n+1 2 potential to account for one unit each of all non-constant binomial coefficient potentials.We can express this with the following additional subtyping rule.In this rule we interpret the 2-dimensional indexing of the potential annotation as a matrix, and we let − → p refer to the vector of potential entries at index coordinates 0, i for i ≥ 1.
Theorem 3. The demotion rule is sound.
Proof.We need only show that C <: D implies Φ(v : D) ≤ Φ(v : C) for unchanged values v.The rest of soundness then follows as in Theorem 1.To do so, it is sufficient to show for l = [a 1 , . . ., a n ] we have Φ(a : L Q (A)) ≤ Φ(a : L P (A)).Without loss of generality, we need only consider where R = 0.

Φ(l : L
As a corollary, this allows us to loosen the constraint that every annotation P contains only non-negative rationals.In particular, it is no longer required that ∀i.p 0,i ≥ 0. Instead, we require that ∀i.p 0,i + p 1,0 ≥ 0. Each unit of n+1 2 potential may "pay" for one unit of deficit from each polynomial potential function.Because this is still a linear constraint, type inference remains automatable.
Using Demote, tighter bounds can be obtained.Consider the single-use subset sum solution from the previous section.Here it is again below, but this time allowing the linear potential to be paid for by n+1

Exponentials, Polynomials, and Logarithms
The addition of exponential potential also allows for the inference of previously nonderivable polynomial-resource types for certain programs.One such way this can happen is by compacting the potential of a list into a new list logarithmic in size to the first.Performing exponential-cost operations, such as subsetSum, on a list of logarithmic size only has linear cost in total.
In the code below, log takes a list x of length n and returns a list of length roughly log 2 (n).If x begins with one unit of linear potential, the type system assigns the output of log one unit of base-2 exponential (2 n − 1) potential.We show in the code below with types of the form L a,b , where a is the linear potential, and b is the base-2 exponential potential.This lets us find that half can have type L 1,0 (Z) 0/0 → L 2,0 (Z) and l og has type L 1,0 (Z) 0/0 → L 0,1 (Z).The typing of l og shows the conversion from linear to exponential potential.Typing l og above requires resource-polymorphic recursion.However, this can be justified by noting that the above can be thought of to show half has type L a,0 (Z) 0/0 → L 2a,0 (Z) and l og has type L a,0 (Z) 0/0 → L 0,a (Z) for any a ≥ 0. Coincidentally, l og conversion of linear to exponential potential certifies that the output list's size can be bounded by a logarithm of the input's size.Nonetheless, logarithmic potential is not directly compatible with the approach this work takes.Sublinear functions have negative second derivatives, and this yields negative annotation entries under ⊳ applications.This may not be insurmountable, as the demotion rule showed here, but new ideas are needed overall.Logarithmic potential has been explored in [32], though the approach there departs from the automatable AARA framework of linear constraint solving.

Conclusion and Future Work
Using Stirling numbers of the second kind allows for the automated inference of exponential resource usages via Automatic Amortized Resource Analysis.This may be combined with the existing polynomial system, allowing mixtures of polynomial and exponential functions to be inferred.Under this system, more kinds of programs can now be automatically analyzed, in particular those making use of multiple recursive calls, or logarithmically-sized lists.Finally, the framework put in place to accomplish this separates the concerns of the type system and potential functions, paving the way to allow modular addition of different potential functions.Future work could extend the work here to cover additional language features supported in polynomial AARA literature, like trees [22].

2 , and n n+1 2 potential
again.It is easy to create a function remove of type Z × L a+1,b,c (Z) d/d → L a,b,c (Z) to do this for any a, b, c, d, where the superscript values represent linear, n+1