a unifying type-theory for higher-order (amortized) cost
TRANSCRIPT
1
A unifying type-theory for higher-order (amortized) costanalysis
VINEET RAJANIโ,Max Planck Institute for Security and Privacy, GermanyMARCO GABOARDI, Boston University, USADEEPAK GARG,Max Planck Institute for Software Systems, GermanyJAN HOFFMANN, Carnegie Mellon University, USA
This paper presents _-amor, a new type-theoretic framework for amortized cost analysis of higher-orderfunctional programs and shows that existing type systems for cost analysis can be embedded in it. _-amorintroduces a new modal type for representing potentials โ costs that have been accounted for, but not yetincurred, which are central to amortized analysis. Additionally, _-amor relies on standard type-theoreticconcepts like affineness, refinement types and an indexed cost monad. _-amor is proved sound using a rathersimple logical relation. We embed two existing type systems for cost analysis in _-amor showing that, despiteits simplicity, _-amor can simulate cost analysis for different evaluation strategies (call-by-name and call-by-value), in different styles (effect-based and coeffect-based), and with or without amortization. One of theembeddings also implies that _-amor is relatively complete for all terminating PCF programs.
Additional Key Words and Phrases: amortized cost analysis, type theory, relative completeness
ACM Reference Format:Vineet Rajani, Marco Gaboardi, Deepak Garg, and Jan Hoffmann. 2021. A unifying type-theory for higher-order(amortized) cost analysis. Proc. ACM Program. Lang. 1, CONF, Article 1 (January 2021), 28 pages.
1 INTRODUCTIONCost analysis is the static verification of an upper bound on the evaluation cost of a program,measured in some abstract unit such as the number of reduction steps, the number of function callsor the number of case analyses during the execution of a program.1 Cost analysis is a well-studiedtopic. Of interest to us in this paper are type systems for cost analysis, many of which exist [Avanziniand Dal Lago 2017; รiรงek et al. 2017; Crary andWeirich 2000; Dal Lago and Gaboardi 2011; Dal Lagoand Petit 2012; Danner et al. 2015; Girard et al. 1992; Handley et al. 2020; Hoffman 2011; Hoffmannet al. 2011, 2017; Hoffmann and Hofmann 2010; Hofmann and Jost 2003; Jost et al. 2010, 2009, 2017;Kavvos et al. 2020; Knoth et al. 2019].Despite the common theme of cost analysis, these type systems vary along a number of axes.
First, they cover different evaluation strategies like call-by-value and call-by-name, which result invery different cost models. Second, they vary in how the type system tracks costs. Most commonlyand perhaps most intuitively, cost is tracked as an effect in the type system [Avanzini and Dal LagoโVineet Rajani is now a post-doctoral researcher at the Max Planck Institute for Security and Privacy but this work wasmostly done while he was a graduate student at the Max Planck Institute for Software Systems and Saarland University.1Cost analysis can also be used to establish lower bounds but, here, as in most prior work, the focus is on upper bounds.
Authorsโ addresses: Vineet Rajani, Max Planck Institute for Security and Privacy, Germany, [email protected]; MarcoGaboardi, Boston University, USA, [email protected]; Deepak Garg, Max Planck Institute for Software Systems, SaarlandInformatics Campus, Germany, [email protected]; Jan Hoffmann, Carnegie Mellon University, USA, [email protected].
Permission to make digital or hard copies of part or all of this work for personal or classroom use is granted without feeprovided that copies are not made or distributed for profit or commercial advantage and that copies bear this notice andthe full citation on the first page. Copyrights for third-party components of this work must be honored. For all other uses,contact the owner/author(s).ยฉ 2021 Copyright held by the owner/author(s).2475-1421/2021/1-ART1https://doi.org/
Proc. ACM Program. Lang., Vol. 1, No. CONF, Article 1. Publication date: January 2021.
1:2 Vineet Rajani, Marco Gaboardi, Deepak Garg, and Jan Hoffmann
2017; รiรงek et al. 2017; Crary and Weirich 2000; Danner et al. 2015; Handley et al. 2020; Hoffman2011; Hoffmann et al. 2011, 2017; Hoffmann and Hofmann 2010; Hofmann and Jost 2003; Jost et al.2010, 2009, 2017; Kavvos et al. 2020; Knoth et al. 2019]. To a first approximation, a cost effect can bethought of as an additional output of the program โ a quantitative value representing the cost ofcomputing the usual output. Interestingly, other work [Dal Lago and Gaboardi 2011; Dal Lago andPetit 2012; Girard et al. 1992] treats cost as a coeffect. To the best of our knowledge, coeffect-basedtracking of cost was introduced in Bounded Linear Logic (BLL) [Girard et al. 1992]. The key idea ofcoeffect-based cost analysis is to represent cost as a requirement on the context (a coeffect) insteadof an additional output (an effect). BLL computes the cost of a program by tracking the number oftimes each variable is used (a coeffect) in an affine type system and is limited to polynomial costs.Subsequent work generalizes the idea of coeffect-based cost analysis to not just non-polynomialcosts but also to other quantitative measures besides cost [Brunel et al. 2014; Dal Lago and Gaboardi2011; Dal Lago and Petit 2012; Gaboardi et al. 2016; McDermott and Mycroft 2018; Petricek et al.2014, 2013].
Third, prior work varies in whether it supports only standard (worst-case) cost analysis or alsoamortized cost analysis. Amortized cost analysis is useful for stateful data structures where certainoperation invocations pay a huge cost to change the internal state in a way that reduces the cost ofsubsequent invocations [Tarjan 1985]. In these cases, it is not very useful to compute the worst-casecost of an individual operation; instead one wants to compute an upper bound on the averagecost of a sequence of ๐ operations for large ๐. This is called amortized cost analysis. Standardtextbook examples that rely on amortized cost analysis are a FIFO queue implemented using a pairof functional (LIFO) lists, Fibonacci heaps and the union-find data structure with path compression.
Research question. Given this diversity in type systems for cost analysis, a natural questionis whether one can build a unifying framework that can embed type systems covering differentevaluation strategies, effect- and coeffect-based cost tracking, and amortized cost analysis. Notethat this is unlikely to be a trivial exercise particularly because the effect- and coeffect-based stylesof cost tracking are quite different.In this paper, we answer this question in the affirmative. We build a new type theory, called
_-amor, that is somewhat minimal but can embed type systems covering call-by-value and call-by-name evaluation, effect- and coeffect-based cost tracking, and amortized costs. Hence, _-amor is aunifying framework in the sense described above.
Overview of _-amor. To motivate _-amorโs design, we start by describing the typical structureof amortized cost analysis through the so-called method of potentials [Tarjan 1985]. Suppose wewant to prove that the average cost of running an operation on a data structure is ๐ , when theactual cost of the operation depends on the internal state of the data structure. To do this, wefind a function ๐ that maps the state ๐ of the data structure to a non-negative number, called apotential, and show (using a type-theory like _-amor) that an invocation of the operation thatchanges the data structure from state ๐ ๐ to ๐ ๐+1 has a cost upper-bounded by ๐ (๐ ๐ ) โ ๐ (๐ ๐+1) + ๐ . Itimmediately follows that a sequence of ๐ operations starting in state ๐ 0 with ๐ (๐ 0) = 0 has a totalcost upper-bounded by (๐ (๐ 0) โ ๐ (๐ 1) + ๐) + ... + (๐ (๐ ๐โ1) โ ๐ (๐ ๐) + ๐). This is a telescopic seriesthat equals ๐ (๐ 0) โ ๐ (๐ ๐) + ๐ ยท ๐ , which in turn is upper-bounded by ๐ ยท ๐ since ๐ (๐ 0) = 0 and ๐ (๐ ๐)is non-negative. Hence, the average cost of each operation is no more than ๐ , as required. The value๐ (๐ ) is called the potential associated with the state ๐ . This potential is needed for verification only,i.e., it is ghost state and it does not exist at run time. The type theory is used to prove only that thecost of an individual operation is upper-bounded by ๐ (๐ ๐ ) โ ๐ (๐ ๐+1) + ๐ (the rest is trivial).Based on this intuition, we describe the requirements for a type theory to support amortized
cost analysis, and how _-amor satisfies these requirements.
Proc. ACM Program. Lang., Vol. 1, No. CONF, Article 1. Publication date: January 2021.
A unifying type-theory for higher-order (amortized) cost analysis 1:3
R1) The type theory must include some construct to associate the ghost potential to the type of adata structure. To this end, _-amor introduces a new type constructor written [๐] ๐ , whichascribes a value of type ๐ with associated potential ๐ . Here, ๐ is a non-negative number.
R2) Since the potential ๐ is related to the state ๐ (๐ equals ๐ (๐ )), the type ๐ of the data structuremust reflect its state to sufficient precision, to allow relating ๐ and ๐ meaningfully. _-amoruses standard refinement types [Xi 2007] for this. For instance, ๐ฟ๐ ๐ is the type of lists oflength ๐, and [2๐] (๐ฟ๐ ๐) is the type of lists of length ๐ with associated potential 2๐. Notehow the refinement ๐ relates an aspect of the state, the length of the list, to the potentialassociated with the list.
R3) The type theory must be able to represent execution costs since we need to establish upperbounds on them. As mentioned above, we can choose to represent costs as an effect or as acoeffect. Upfront, _-amor represents cost as an effect: It includes an indexed monadM^ ๐ torepresent a computation of type ๐ and cost ^. However, we show that coeffect-based costanalysis can be simulated in _-amor using a combination of effects and potentials. To the bestof our knowledge, this simulation of coeffect-based analysis in effect-based analysis usingpotentials is a completely new result.
R4) The type theory must prevent duplication of any type that has a potential associated with it,else analysis may not be sound. For example, if a typing derivation duplicates the potential๐ (๐ ๐ ) even once, then the operationโs real cost may be up to 2 ยท ๐ (๐ ๐ ) โ ๐ (๐ ๐+1) + ๐ , and theamortized analysis described earlier breaks completely. Hence, all potential-carrying typesmust be treated affinely in the type theory. Accordingly, _-amor is an affine type theorywith the usual operators of affine logic like โ, & and โ. Duplicable resources are explicitlyrepresented using the standard exponential ! of affine logic. To improve expressiveness, weallow the exponential ! to be indexed, following the dependent sub-exponential of BoundedLinear Logic [Girard et al. 1992].
Overall, _-amor can be seen as a computational _-calculus (in the sense of Moggi [Moggi 1991])equipped with a type system that has the four features mentioned above โ the construct [๐] ๐to associate potential to a type, type refinements, the indexed cost monadM^ ๐ , and affinenesswith an indexed sub-exponential !.2 We give the pure (non-monadic) part of _-amor a call-by-namesemantics with eager evaluation for all pairs and sums. However, as shown by Moggi [1991],simulating call-by-value semantics in a monadic setting is not difficult, a fact we exploit for ourembedding of call-by-value cost analysis in _-amor later._-amor is conceptually very simple. We prove it sound using an elementary logical relation that
extends Pymโs semantics of BI [Pym et al. 2004]. The key novelty in building this relation is thetreatment of potentials, and their interaction with the cost monad (available potential can offsetthe cost in the monad).
_-amor as a unifying framework. We show that _-amor is unifying in the sense described earlierby embedding two state-of-the-art frameworks for (amortized) cost analysis faithfully in _-amor.Our first embedding is that of a core calculus for Resource-aware ML or RAML [Hoffman 2011;Hoffmann and Hofmann 2010], an implemented, widely-used effect-based framework for amortizedcost analysis of ML programs. RAML is call-by-value, so this embedding shows how (amortized)call-by-value cost analysis can be simulated in _-amor.Our second embedding is that of ๐โPCF [Dal Lago and Gaboardi 2011], a coeffect-based type
system for non-amortized cost analysis of call-by-name PCF programs. Unlike the first embedding
2The name โ_-amorโ refers to both the calculus and its type system. The intended use can be disambiguated from thecontext.
Proc. ACM Program. Lang., Vol. 1, No. CONF, Article 1. Publication date: January 2021.
1:4 Vineet Rajani, Marco Gaboardi, Deepak Garg, and Jan Hoffmann
(of RAML), which is relatively easy, this embedding is surprising and difficult, as it simulates๐โPCFโs coeffects using _-amorโs potentials and effects.
Together, the two embeddings cover call-by-value and call-by-name evaluation, effect- andcoeffect-based cost tracking, and amortized cost analysis, thus fulfilling our goal of โunifyingโ.The embedding of ๐โPCF also shows that _-amor is very expressive. Dal Lago and Gaboardi
[2011] showed that๐โPCF is relatively complete for PCF programs, meaning that every PCF programcan be typed with precise cost in it.3 Hence, our embedding of ๐โPCF in _-amor shows that _-amoris also relatively complete for PCF programs.
Added expressiveness relative to prior work on amortized analysis. Besides being a unifying frame-work for cost analysis as just described, _-amor also improves the expressiveness of prior typesystems for amortized cost analysis with the method of potentials. The main line of work here isRAML and its variants [Hoffmann et al. 2011, 2017; Hoffmann and Hofmann 2010; Hofmann andJost 2003; Jost et al. 2010]. However, this line of work fulfills requirements R1 and R4 only partiallyโ potential can be associated only with first-order types and functions are non-affine. As a result,RAML has difficulty in handling the interaction between higher-order values and potential. Tounderstand the issue, consider a Curried function of two arguments, of which the first argumentcarries potential that offsets the cost of executing the function. Suppose that this function is appliedpartially. The resulting closure must not be duplicable because it captures the potential from thefirst argument. However, since RAML (and its many extensions) do not treat functions affinely,they cannot type check such programs. For instance, RAML extensions like [Hoffmann et al. 2017]completely exclude the handling of Curried functions while others like [Jost et al. 2010] can typecheck only those Curried functions where potential is limited to the last argument. In contrast,_-amor, being fully affine, can handle such examples trivially.
Additionally, RAML and its variants do not support polymorphism over costs in full generality,which limits their expressiveness. For example, RAML cannot represent the cost of a standard listfold function precisely when the cost of the binary operator passed as an argument to fold variesdepending on its arguments. In contrast, _-amor has full cost polymorphism and can type such afold function, as we show in Section 3.3.
Organization. To simplify the presentation, we describe _-amor in two stages. First, we describe_-amor without indexing on exponentials (Section 2). This suffices for most examples (Section 3)and the embedding of RAML (Section 4), but not the embedding of ๐โPCF. Then, we introduce thefull _-amor by adding indexed exponentials (Section 5) and show how to embed ๐โPCF (Section 6).We compare to related work in Section 7. The full technical development with proofs of all theoremsand further examples can be found in a technical appendix available from authorโs homepages.
Limitations and scope. Our focus in this paper is on the foundations of a unifying type theoryfor (amortized) cost analysis. An implementation of the type theory is beyond the scope of thispaper, even though in restricted settings like polynomial-time analysis, one could use ideas fromprior work like RAML to implement the type theory efficiently (we are working on such animplementation). Further, we focus only on the cost of non-reusable resources like time. The costof reusable resources like heap space is not within the scope of this paper. Finally, we do notconsider call-by-need evaluation (lazy evaluation with sharing), as in the work of Danielsson[2008]; Jost et al. [2017]; Okasaki [1996]. Call-by-need is very hard to simulate without imperativestate (which we do not include in _-amor) and, additionally, amortized analysis for call-by-needdoes not require affineness. Consequently, unifying work on cost analysis for call-by-need withthat for call-by-value or call-by-name will require a significant amount of further work. We also3The adjective โrelativeโ means relative to having a refinement domain that is sufficiently expressive.
Proc. ACM Program. Lang., Vol. 1, No. CONF, Article 1. Publication date: January 2021.
A unifying type-theory for higher-order (amortized) cost analysis 1:5
Types ๐ ::= 1 | b | ๐1 โธ ๐2 | ๐1 โ ๐2 | ๐1 & ๐2 | ๐1 โ ๐2 | !๐ | [๐] ๐ | M^ ๐ | ๐ฟ๐ ๐๐ผ | โ๐ผ : K.๐ | โ๐ : S.๐ | _๐ก ๐ : S.๐ | ๐ ๐ผ | โ๐ : S.๐ | ๐ถ โ ๐ | ๐ถ&๐
Expressions ๐ ::= ๐ฃ | ๐ฅ | ๐1 ๐2 | โจโจ๐1, ๐2โฉโฉ | letโจโจ๐ฅ,๐ฆโฉโฉ = ๐1 in ๐2 | fix๐ฅ .๐ |โจ๐, ๐โฉ | fst(๐) | snd(๐) | inl(๐) | inr(๐) | case ๐, ๐ฅ .๐,๐ฆ.๐ |let !๐ฅ = ๐1 in ๐2 | ๐ :: ๐ | (match ๐ with |nil โฆโ ๐1 |โ :: ๐ก โฆโ ๐2) | ๐ [] |xlet๐ฅ = ๐1 in ๐2 | clet๐ฅ = ๐1 in ๐2
Values ๐ฃ ::= () | ๐ | _๐ฅ .๐ | โจโจ๐ฃ1, ๐ฃ2โฉโฉ | โจ๐ฃ, ๐ฃโฉ | inl(๐) | inr(๐) | ! ๐ | nil |ฮ.๐ | ret ๐ | bind๐ฅ = ๐1 in ๐2 | โ^ | release๐ฅ = ๐1 in ๐2 | store ๐
Indices ๐ผ , ๐ฝ , ^, ๐, ๐ ::= ๐ | ๐ | ๐ + | ๐ผ + ๐ผ | ๐ผ โ ๐ผ | โ๐<๐ฝ ๐ผ | _๐ ๐ : S.๐ผ | ๐ผ ๐ผConstraints ๐ถ ::= ๐ผ = ๐ผ | ๐ผ < ๐ผ | ๐ถ โง ๐ถSorts S ::= N | R+ | S โ SKinds K ::= ๐๐ฆ๐๐ | S โ K
Fig. 1. _-amorโโs syntax
do not consider call-by-push-value (CBPV) evaluation, although we believe that CBPV is actuallywithin reach โ we would need to give _-amor a CBPV semantics instead of its current monadicsemantics.
Other related work. We wish to note that, besides type systems for cost analysis, there is alsowork on program logics for (amortized) cost analysis [Carbonneaux et al. 2015; Charguรฉraud andPottier 2019; Mรฉvel et al. 2019]. Some of this work, e.g., [Mรฉvel et al. 2019] could be made moreexpressive than our type theory (by adding an expressive language of costs), but this line of workdoes not attempt to unify existing frameworks for cost analysis, which is our primary objectivehere. We could have chosen Mรฉvel et al. [2019] as the base of our work and used that in place of_-amor as the target of our embeddings, but we chose to design _-amor because we believe that ourdesign is considerably less complex. _-amor focuses only on cost analysis. In contrast, Mรฉvel et al.[2019] add cost analysis to a framework for full functional verification, which is really orthogonalto our goal here.
2 _-amorโ (NO SUB-EXPONENTIALS)To simplify the presentation, we first describe _-amorโ, the subset of _-amor that only considersthe standard exponential ! from affine logic, without any indexing (that _-amor supports).
2.1 Syntax and SemanticsThe syntax of _-amorโ is shown in Fig. 1. We describe the various syntactic categories below.
Indices, sorts, kinds and constraints. _-amorโ is a refinement type system. (Static) indices, ร laDML [Xi 2007], are used to track information like list lengths, computation costs and potentials.These indices can be natural or positive real numbers, with support for addition and subtraction.There is also a function to obtain a bounded sum (
โ๐<๐ฝ ๐ผ ) over indices. It basically describes
summation of ๐ผ with ๐ ranging from 0 to ๐ฝ โ 1 inclusive, i.e., ๐ผ [0/๐] + . . . + ๐ผ [๐ฝ โ 1/๐]. Besides this,we also have index-level functions and index-level function application. List lengths are representedusing natural numbers (sort N). Potentials and costs are both represented using non-negative realnumbers (sort R+). _-amorโ also features kinds, denoted by K. ๐๐ฆ๐๐ is the kind of standard affinetypes and S โ K represents a kind family indexed by the sort S. Finally, constraints (denoted by ๐ถ)are predicates (=, <,โง) over indices.
Types. _-amorโ is an affine type system. The most important type is the modal type [๐] ๐ , whichascribes values of type ๐ that have potential ๐ associated with them (as a ghost). We have themultiplicative unit type (denoted 1) and an abstract base type (denoted b) to represent types likeintegers or booleans. Then, there are standard affine types โ affine function spaces (โธ), sums
Proc. ACM Program. Lang., Vol. 1, No. CONF, Article 1. Publication date: January 2021.
1:6 Vineet Rajani, Marco Gaboardi, Deepak Garg, and Jan Hoffmann
Forcing reduction, ๐ โ^ ๐ฃ
๐ โ ๐ฃret ๐ โ0 ๐ฃ
E-return๐1 โ ๐ฃ1 ๐ฃ1 โ^1 ๐ฃ โฒ1 ๐2 [๐ฃ โฒ1/๐ฅ] โ ๐ฃ2 ๐ฃ2 โ^2 ๐ฃ โฒ2
bind๐ฅ = ๐1 in ๐2 โ^1+^2 ๐ฃ โฒ2E-bind
โ^ โ^ ()E-tick
๐1 โ ๐ฃ1 ๐2 [๐ฃ1/๐ฅ] โ ๐ฃ2 ๐ฃ2 โ^ ๐ฃ โฒ2release๐ฅ = ๐1 in ๐2 โ^ ๐ฃ โฒ2
E-release๐ โ ๐ฃ
store ๐ โ0 ๐ฃE-store
Fig. 2. Selected evaluation rules
(โ), pairs (both the multiplicative โ and the additive &) and the exponential (!), which ascribesexpressions that can be duplicated. We include only one representative data type โ the length-refined list type ๐ฟ๐๐ , where the length ๐ is drawn from the language of indices (described earlier).Other data types can be added if needed._-amorโ also has universal quantification over types and indices denoted by โ๐ผ : K.๐ and โ๐ : S.๐ ,
respectively, and existential quantification over indices, denoted โ๐ : S.๐ . Quantification over indicescomes in quite handy for representing variable cost of function arguments (as we exemplify in ourencoding of Church numerals in Section 3.2). The constraint type ๐ถ โ ๐ means that if constraint๐ถ holds then the underlying term has the type ๐ . The dual type ๐ถ&๐ means that the constraint๐ถ holds and the type of the underlying term is ๐ . For instance, the type of non-empty lists canbe written as โ๐. (๐ > 0)&(๐ฟ๐๐). We also have sort-indexed type families, which are type-levelfunctions from sorts to kinds.Finally, _-amorโ has the monadic typeM^ ๐ , which represents computations of cost at most ^.
Technically, M^ ๐ is a graded or indexed monad [Gaboardi et al. 2016]. A non-zero cost can beincurred only by an expression of the monadic type. Following standard convention we call suchexpressions impure, while expressions of all other types are called pure.
Expressions and values. There are term-level constructors for all types (in the kind ๐๐ฆ๐๐) exceptfor the modal type ([๐] ๐). The inhabitants of type [๐] ๐ are exactly those of type ๐ since thepotential is ghost state without a runtime manifestation.
We describe the expression and value forms for some of the types. The term-level constructorsfor the constraint type (๐ถ โ ๐), type and index-level quantification (โ๐ผ : K.๐ , โ๐ : S.๐) are alldenoted ฮ.๐ . (Note that indices, types and constraints do not occur in terms.) We also have a fixpointoperator (fix) which is used to encode recursion.The monadic type M^ ๐ has several term constructors, including the standard monadic unit
(ret ๐) and bind (bind๐ฅ = ๐1 in ๐2). The construct store ๐ stores potential with a term and is theintroduction form of the type [๐] ๐ . Dually, release๐ฅ = ๐1 in ๐2 releases potential stored with ๐1and makes it available to offset the cost of ๐2. Note that store ๐ and release๐ฅ = ๐1 in ๐2 are usefulonly for the type system: they indicate ghost operations, i.e., where potentials should be storedand released, respectively. Operationally, they are uninteresting: store ๐ evaluates exactly like ret ๐ ,while release๐ฅ = ๐1 in ๐2 evaluates exactly like bind๐ฅ = ๐1 in ๐2. Finally, we have a construct forincurring non-zero cost โ the โtickโ construct denoted โ^ . This construct indicates that cost ^ isincurred where it is placed. Programmers place the construct at appropriate points in a program tomodel costs incurred during execution, as in prior work [Danielsson 2008].Operational semantics. _-amorโ is a call-by-name calculus with eager evaluation.4 We use two
evaluation judgments โ pure and forcing. The pure evaluation judgment (๐ โ ๐ฃ) relates an expression๐ to the value ๐ฃ it evaluates to. All monadic expressions are treated as values in the pure evaluation.The rules for pure evaluation are standard so we defer them to the technical appendix. The forcing
4Perhaps somewhat surprisingly, even additive (&) pairs are evaluated eagerly. However, since all effects are confined to amonad, this choice does not matter. ! is lazy as in a standard affine _-calculus.
Proc. ACM Program. Lang., Vol. 1, No. CONF, Article 1. Publication date: January 2021.
A unifying type-theory for higher-order (amortized) cost analysis 1:7
Typing judgment: ฮจ;ฮ;ฮ;ฮฉ; ฮ โข ๐ : ๐
ฮจ;ฮ;ฮ;ฮฉ; ฮ1 โข ๐1 : ๐1 ฮจ;ฮ;ฮ;ฮฉ; ฮ2 โข ๐2 : ๐1ฮจ;ฮ;ฮ;ฮฉ; ฮ1 + ฮ2 โข โจโจ๐1, ๐2โฉโฉ : (๐1 โ ๐2)
T-tensorI
ฮจ;ฮ;ฮ;ฮฉ; ฮ1 โข ๐ : (๐1 โ ๐2) ฮจ;ฮ;ฮ;ฮฉ; ฮ2, ๐ฅ : ๐1, ๐ฆ : ๐2 โข ๐ โฒ : ๐ฮจ;ฮ;ฮ;ฮฉ; ฮ1 + ฮ2 โข letโจโจ๐ฅ,๐ฆโฉโฉ = ๐ in ๐ โฒ : ๐
T-tensorE
ฮจ;ฮ;ฮ;ฮฉ; . โข ๐ : ๐ฮจ;ฮ;ฮ;ฮฉ; . โข !๐ : !๐
T-ExpIฮจ;ฮ;ฮ;ฮฉ; ฮ1 โข ๐ : !๐ ฮจ;ฮ;ฮ;ฮฉ, ๐ฅ : ๐ ; ฮ2 โข ๐ โฒ : ๐ โฒ
ฮจ;ฮ;ฮ;ฮฉ; ฮ1 + ฮ2 โข let !๐ฅ = ๐ in ๐ โฒ : ๐ โฒT-ExpE
ฮจ;ฮ;ฮ;ฮฉ, ๐ฅ : ๐ ; . โข ๐ : ๐ฮจ;ฮ;ฮ;ฮฉ; . โข fix๐ฅ .๐ : ๐
T-fix
ฮจ;ฮ;ฮ;ฮฉ; ฮ โข ๐ : ๐ ฮจ;ฮ;ฮ โข ฮโฒ โ ฮ ฮจ;ฮ;ฮ โข ฮฉโฒ โ ฮฉ ฮจ;ฮ;ฮ โข ๐ <: ๐ โฒ
ฮจ;ฮ;ฮ;ฮฉโฒ; ฮโฒ โข ๐ : ๐ โฒT-weaken
ฮจ;ฮ;ฮ;ฮฉ; ฮ โข ๐ : ๐ฮจ;ฮ;ฮ;ฮฉ; ฮ โข ret ๐ : M 0๐
T-ret
ฮจ;ฮ;ฮ;ฮฉ; ฮ1 โข ๐1 : M^1 ๐1ฮจ;ฮ;ฮ;ฮฉ; ฮ2, ๐ฅ : ๐1 โข ๐2 : M^2 ๐2
ฮ โข ^1 : R+ ฮ โข ^2 : R+
ฮจ;ฮ;ฮ;ฮฉ; ฮ1 + ฮ2 โข bind๐ฅ = ๐1 in ๐2 : M(^1 + ^2) ๐2T-bind
ฮ โข ^ : R+
ฮจ;ฮ;ฮ;ฮฉ; ฮ โข โ^ : M^ 1T-tick
ฮจ;ฮ;ฮ;ฮฉ; ฮ โข ๐ : ๐ ฮ โข ๐ : R+
ฮจ;ฮ;ฮ;ฮฉ; ฮ โข store ๐ : M๐ ( [๐] ๐)T-store
ฮจ;ฮ;ฮ;ฮฉ; ฮ1 โข ๐1 : [๐1] ๐1 ฮจ;ฮ;ฮ;ฮฉ; ฮ2, ๐ฅ : ๐1 โข ๐2 : M(๐1 + ๐2) ๐2ฮ โข ๐1 : R+ ฮ โข ๐2 : R+
ฮจ;ฮ;ฮ;ฮฉ; ฮ1 + ฮ2 โข release๐ฅ = ๐1 in ๐2 : M๐2 ๐2T-release
Fig. 3. Selected typing rules for _-amorโ
evaluation judgment ๐ โ^ ๐ฃ is a relation between terms of typeM^ ๐ and values of type ๐ . ^ is thecost incurred in executing (forcing) ๐ . The rules of this judgment are shown in Fig. 2. E-return statesthat if ๐ reduces to ๐ฃ in the pure reduction, then ret ๐ forces to ๐ฃ with 0 cost. E-store is exactly likeE-return, emphasizing the ghost nature of potential annotations in types. E-bind is the standardmonadic composition of ๐1 with ๐2. The effect (cost) of bind is the sum of the costs of forcing ๐1 and๐2. E-release is similar. โ^ is the only cost-consuming construct in the language. E-tick says that โ^forces to () and it incurs cost ^.
2.2 Type systemThe typing judgment of _-amorโ is written ฮจ;ฮ;ฮ;ฮฉ; ฮ โข ๐ : ๐ . Here, ฮจ is a context mappingtype-level variables to their kinds, ฮ is a context mapping index-level variables to their sorts, ฮ isa context of constraints on the index variables, and ฮฉ and ฮ are the non-affine and affine typingcontexts respectively, both mapping term-level variables to their types. We use the notation ฮ1 + ฮ2to describe the disjoint union of the affine contexts ฮ1 and ฮ2. Selected typing rules are listed inFig. 3, and the full set of rules can be found in the technical appendix.Rules for the affine type constructs of _-amor are standard. T-tensorI is the type rule for intro-
ducing the tensor pair โจโจ๐1, ๐2โฉโฉ โ if ๐1 and ๐2 are typed ๐1 and ๐2 under affine contexts ฮ1 and ฮ2,respectively, then โจโจ๐1, ๐2โฉโฉ is typed (๐1 โ ๐2) under the context (ฮ1 + ฮ2). Dually, T-tensorE is the
Proc. ACM Program. Lang., Vol. 1, No. CONF, Article 1. Publication date: January 2021.
1:8 Vineet Rajani, Marco Gaboardi, Deepak Garg, and Jan Hoffmann
ฮจ;ฮ;ฮ โข ๐ <: ๐ โฒ ฮจ;ฮ;ฮ โข ๐ โฒ โค ๐
ฮจ;ฮ;ฮ โข [๐] ๐ <: [๐ โฒ] ๐ โฒsub-potential
ฮจ;ฮ;ฮ โข ๐ <: ๐ โฒ ฮจ;ฮ;ฮ โข ^ โค ^ โฒ
ฮจ;ฮ;ฮ โข M^ ๐ <: M^ โฒ ๐ โฒsub-monad
ฮ โข ๐ : R+ ฮ โข ๐ โฒ : R+
ฮจ;ฮ;ฮ โข [๐] (๐1 โธ ๐2) <: ( [๐ โฒ] ๐1 โธ [๐ โฒ + ๐] ๐2)sub-potArrow
ฮจ;ฮ;ฮ โข ๐ <: [0] ๐sub-potZero
ฮจ;ฮ, ๐ : S;ฮ โข ๐ <: ๐ โฒ
ฮจ;ฮ;ฮ โข _๐ก ๐ : S.๐ <: _๐ก ๐ : S.๐ โฒsub-familyAbs
ฮ โข ๐ผ : Sฮจ;ฮ;ฮ โข (_๐ก ๐ : S.๐) ๐ผ <: ๐ [๐ผ/๐]
sub-familyApp1
ฮ โข ๐ผ : Sฮจ;ฮ;ฮ โข ๐ [๐ผ/๐] <: (_๐ก ๐ : S.๐) ๐ผ
sub-familyApp2
Fig. 4. Selected subtyping rules
type rule for eliminating the tensor pair โ if expression ๐ has type (๐1 โ ๐2) in the context ฮ1 and acontinuation ๐ โฒ is of type ๐ โฒ in the context ฮ2 plus both elements of the tensor pair (named ๐ฅ and๐ฆ here), then the expression letโจโจ๐ฅ,๐ฆโฉโฉ = ๐ in ๐ โฒ is of type ๐ โฒ under the context (ฮ1 + ฮ2). T-expIascribes !๐ the type !๐ if ๐ can be ascribed the type ๐ under an empty affine context. The subtypingrelation (<:) used in the rule T-weaken is described below, but we skip describing the standarddetails of the auxiliary relation โ, which is described in the technical appendix. T-expE is the rulefor the elimination form of !๐ . The important thing here is that the continuation ๐ โฒ has unboundedaccess to ๐ via the non-affine variable ๐ฅ .
Rules for monadic types are interesting. T-ret types the return of the monad. In the operationalsemantics, ret ๐ takes a well-typed expression and returns it with 0 cost. Hence, its typeM 0๐ alsoincludes 0 cost. T-bind types the monadic bind, which basically sequences two computations. Thecost in the type of the bind is the sum of the costs of the two computations, again mirroring theoperational semantics. T-tick type checks โ^ at typeM^ 1 โ a monad of unit type with cost ^.T-store types store ๐ , which is used to associate potential with the expression ๐ . If ๐ has type ๐ ,
the rule gives store ๐ the typeM^ ( [^] ๐). Intuitively, if ^ units of potential are attached to ๐ , thenthe cost of doing so is ^ units. Finally, T-release is dual to T-store: It uses the potential ๐1 storedwith the first expression ๐1 to reduce the cost of the continuation by the same amount.
Subtyping. Selected subtyping rules are shown in Fig. 4. As mentioned earlier, _-amorโ alsohas type-level functions and applications. Accordingly, we have subtyping rules to convert thetype-level application form ((_๐ก ๐ : S.๐) ๐ผ ) to the substitution form (๐ [๐ผ/๐]) and vice versa. Rulesub-potArrow distributes potential on a function type over the argument and the return value.sub-potZero allows silently casting an expression of type ๐ to type [0] ๐ . This reinforces the ghostnature of potential. The subtyping of the modal type [๐] ๐ is contra-variant in the potential becauseit is sound to throw away potential. The subtyping for the monadic type is covariant in both thetype and the cost (because the cost in the monadic type is an upper bound). There are additional,standard typing rules for sorts and kinds, which we defer to the technical appendix.
Theorem 1 is the soundness of _-amorโ: If ๐ is a closed term which has a statically approximatedcost of ^ units (as specified in the monadic typeM^ ๐) and forcing ๐ actually consumes ^ โฒ units ofcost, then ^ โฒ โค ^. We prove this theorem using a logical relation in Section 2.3.
Theorem 1 (Soundness). โ๐, ๐ฃ, ^, ^ โฒ, ๐ โ ๐๐ฆ๐๐ . โข ๐ : M^ ๐ โง ๐ โ^โฒ ๐ฃ =โ ^ โฒ โค ^
2.3 Model of types and soundnessTo prove the soundness of _-amorโ, we develop a logical-relation model of its types. The model isan extension of Pymโs semantics of BI [Pym et al. 2004] with potentials, the cost monad, and type
Proc. ACM Program. Lang., Vol. 1, No. CONF, Article 1. Publication date: January 2021.
A unifying type-theory for higher-order (amortized) cost analysis 1:9
J1K โ {(๐,๐ , ())}JbK โ {(๐,๐ , ๐ฃ) | ๐ฃ โ JbK}J๐ฟ0๐K โ {(๐,๐ , nil)}J๐ฟ๐ +1๐K โ {(๐,๐ , ๐ฃ :: ๐) |โ๐1, ๐2 .๐1 + ๐2 โค ๐ โง (๐1,๐ , ๐ฃ) โ J๐K โง (๐2,๐ , ๐) โ J๐ฟ๐ ๐K}J๐1 โ ๐2K โ {(๐,๐ , โจโจ๐ฃ1, ๐ฃ2โฉโฉ) | โ๐1, ๐2 .๐1 + ๐2 โค ๐ โง (๐1,๐ , ๐ฃ1) โ J๐1K โง (๐2,๐ , ๐ฃ2) โ J๐2K}J๐1 & ๐2K โ {(๐,๐ , โจ๐ฃ1, ๐ฃ2โฉ) | (๐,๐ , ๐ฃ1) โ J๐1K โง (๐,๐ , ๐ฃ2) โ J๐2K}J๐1 โ ๐2K โ {(๐,๐ , inl(๐ฃ)) | (๐,๐ , ๐ฃ) โ J๐1K} โช {(๐,๐ , inr(๐ฃ)) | (๐,๐ , ๐ฃ) โ J๐2K}J!๐K โ {(๐,๐ , !๐) | (0,๐ , ๐) โ J๐KE }J๐1 โธ ๐2K โ {(๐,๐ , _๐ฅ .๐) | โ๐ โฒ, ๐ โฒ,๐ โฒ<๐ .(๐ โฒ,๐ โฒ, ๐ โฒ) โ J๐1KE =โ (๐ + ๐ โฒ,๐ โฒ, ๐ [๐ โฒ/๐ฅ]) โ J๐2KE }J[๐] ๐K โ {(๐,๐ , ๐ฃ) | โ๐ โฒ.๐ โฒ + ๐ โค ๐ โง (๐ โฒ,๐ , ๐ฃ) โ J๐K}}JM^ ๐K โ {(๐,๐ , ๐ฃ) | โ^ โฒ, ๐ฃ โฒ,๐ โฒ<๐ .๐ฃ โ^โฒ
๐ โฒ ๐ฃโฒ =โ โ๐ โฒ.^ โฒ + ๐ โฒ โค ๐ + ^ โง (๐ โฒ,๐ โ๐ โฒ, ๐ฃ โฒ) โ J๐K}
Jโ๐ผ.๐K โ {(๐,๐ ,ฮ.๐) | โ๐ โฒ,๐ โฒ<๐ .(๐,๐ โฒ, ๐) โ J๐ [๐ โฒ/๐ผ]KE }Jโ๐ .๐K โ {(๐,๐ ,ฮ.๐) | โ๐ผ ,๐ โฒ<๐ .(๐,๐ โฒ, ๐) โ J๐ [๐ผ/๐]KE }J๐ถ โ ๐K โ {(๐,๐ ,ฮ.๐) | . |= ๐ถ =โ (๐,๐ , ๐) โ J๐KE }J๐ถ&๐K โ {(๐,๐ , ๐ฃ) | . |= ๐ถ โง (๐,๐ , ๐ฃ) โ J๐K}Jโ๐ .๐K โ {(๐,๐ , ๐ฃ) | โ๐ โฒ.(๐,๐ , ๐ฃ) โ J๐ [๐ โฒ/๐ ]K}J_๐ก ๐ .๐K โ ๐ where โ๐ผ . ๐ ๐ผ = J๐ [๐ผ/๐]KJ๐ ๐ผK โ J๐K ๐ผ
J๐KE โ {(๐,๐ , ๐) | โ ๐ โฒ<๐, ๐ฃ .๐ โ๐ โฒ ๐ฃ =โ (๐,๐ โ ๐ โฒ, ๐ฃ) โ J๐K}
JฮKE โ {(๐,๐ ,๐พ) | โ๐ : V๐๐๐ โ P๐๐ก๐ .(โ๐ฅ โ dom(ฮ) . (๐ (๐ฅ),๐ ,๐พ (๐ฅ)) โ Jฮ(๐ฅ)KE ) โง (โ๐ฅ โdom(ฮ) ๐ (๐ฅ) โค ๐)}
JฮฉKE โ {(0,๐ , ๐ฟ) | (โ๐ฅ โ dom(ฮฉ) .(0,๐ , ๐ฟ (๐ฅ)) โ J๐KE )}
Fig. 5. Model of _-amorโ types
refinements. We also step-index the model [Ahmed 2004] to break a circularity in its definition,arising from impredicative quantification over types, as in the work of Neis et al. [2011]. Becausewe use step-indices, we also have augmented operational semantics that count the number of rules(denoted ๐ ) used during evaluation. The revised judgments are written ๐ โ๐ ๐ฃ (pure) and ๐ โ^๐ ๐ฃ(forcing). The expected details are in the technical appendix. Note that there is no connectionbetween ๐ and ^ in the forcing judgment โ the former is purely an artifact of our metatheoreticproofs, while the latter is induced by โ constructs in the program. Our use of step-indices, alsowritten ๐ , is standard and readers not familiar with them may simply ignore them. The model(Fig. 5) is defined using four relations: a value relation, an expression relation and substitutionrelations for the affine and non-affine contexts. The first two are mutually recursive, well-foundedin the lexicographic order โจstep index (๐ ), type (๐), value < expressionโฉ.Value relation. The value relation (denoted by J.K) gives an interpretation to _-amorโ types (of
kind ๐๐ฆ๐๐) as sets of triples of the form (๐,๐ , ๐ฃ). Importantly, the potential ๐ is an upper-bound onthe ambient potential required to construct the value ๐ฃ . It must include potential associated withthe (types of) subexpressions of ๐ฃ .We describe interesting cases of this relation. The interpretation for the list type is defined by
a further induction on list size. For a list of size 0 the value relation contains a nil value withany potential (since nil captures no potential). For a list of size ๐ + 1, the value relation is definedinductively on ๐ , similar to the tensor pair, which we describe next. For a tensor (โ) pair, bothcomponents can be used. Therefore, the potential required to construct a tensor pair is at least thesum of the potentials needed to construct the two components. On the other hand, for a with (&)pair, either but not both of the components can be used by the context. So the potential needed fora & pair should be sufficient for each component separately. The type !๐ contains !๐ when ๐ is in ๐ .
Proc. ACM Program. Lang., Vol. 1, No. CONF, Article 1. Publication date: January 2021.
1:10 Vineet Rajani, Marco Gaboardi, Deepak Garg, and Jan Hoffmann
The important aspect here is that the potential associated with ๐ must be 0, otherwise we wouldhave immediate unsoundness due to replication of potential, as described in Section 1.
Next, we explain the interpretation of the arrow type ๐1 โธ ๐2: _๐ฅ.๐ is in this type with potential๐ if for any substitution ๐ โฒ (of type ๐1) that comes with potential ๐ โฒ, the total potential ๐ + ๐ โฒ issufficient for the body ๐ [๐ โฒ/๐ฅ] (of type ๐2).
The step indices๐ play an important role only in the interpretation of the polymorphic type โ๐ผ.๐ .Since the type-level parameter ๐ผ may be substituted by any type, potentially one even larger than ๐ ,the relation would not be well-founded by induction on types alone. Here, we rely on step-indices,noting that substituting ๐ผ with a type consumes at least one step in our operational semantics,so the relation for ๐ (with the substitution) needs to be defined only at a smaller step index. Thisfollows prior work [Neis et al. 2011].Next, we come to the new, interesting types for potential and the cost monad. The potential
type [๐] ๐ contains ๐ฃ with required potential ๐ if ๐ is sufficient to account for ๐ and the potentialrequired for ๐ฃ . (Note that the same value ๐ฃ is in the interpretation of both ๐ and [๐] ๐ .) The gradedmonadic type M^ ๐ contains the (impure) value ๐ฃ with required potential ๐ if ๐ and ^ togethersuffice to cover the cost ^ โฒ of actually forcing ๐ฃ and the potential ๐ โฒ required for the resulting value,i.e., if ๐ + ^ โฅ ^ โฒ + ๐ โฒ. The ambient potential ๐ and the cost ^ on the monad appear together in asum, which explains why the typing rule T-release can offset cost on the monad using potential.
The interpretation of a type family _๐ก ๐ .๐ is a type-level function, as expected. The interpretationof type-level application is an application of such a function. The remaining cases of the valuerelation of Fig. 5 should be self-explanatory.Expression relation. The expression relation, denoted J.KE , maps a type to a set of triples of the
form (๐,๐ , ๐). Its definition is fairly simple and standard: we simply check if the value ๐ฃ obtainedby pure evaluation of ๐ is in the value relation of the same type. The potential does not changeduring pure evaluation, but we adjust the step index correctly.
Substitution relations. Finally, we describe the substitution relations for the affine context (ฮ) andthe non-affine context (ฮฉ). Each relation maps the context to a set of valid substitutions for thecontext, paired with a step index and a potential. The two key points about the interpretation of ฮare: 1) The substitution ๐พ should map each variable to a value of the correct type (semantically), and2) The potential ๐ for the context should be more than the sum of the potentials required for thesubstitutions of each of the variables. The interpretation of the non-affine context ฮฉ is simpler. Itonly demands that the substituted value is in the interpretation of the correct type with 0 potential.Soundness. As is standard for logical-relations models, the main meta-theoretic property is the
โfundamental theoremโ (Theorem 2). The theorem basically says that if ๐ is well-typed in somecontexts at type ๐ , then the application of any substitutions in the semantic interpretation of thecontexts map ๐ into the semantic interpretation of ๐ . The important, interesting aspect of thetheorem in _-amorโ is that the potential needed for ๐ (after substitution) equals the potentialcoming from the context, ๐๐ . This the crux of the soundness of (amortized) cost analysis in _-amorโ.
Theorem 2 (Fundamental theorem for _-amorโ). โฮ,ฮฉ, ฮ, ๐, ๐,๐ , ๐๐ , ๐พ, ๐ฟ, ๐, ].ฮจ;ฮ;ฮ;ฮฉ; ฮ โข ๐ : ๐ โง (๐๐ ,๐ ,๐พ) โ Jฮ ๐]KE โง (0,๐ , ๐ฟ) โ Jฮฉ ๐]KE โง . |= ฮ ] =โ(๐๐ ,๐ , ๐ ๐พ๐ฟ) โ J๐ ๐]KE .
Here, ], ๐ , ๐ฟ and ๐พ denote substitutions for the index context ฮ, the type context ฮจ, the non-affinecontext ฮฉ and the affine context ฮ, respectively. This theorem is proved by induction on the giventyping judgment with a subinduction on the step-index for the case of fix. The technical appendixhas the entire proof.Theorem 1 is a direct corollary of this fundamental theorem. We can derive several additional
corollaries about execution cost directly from this fundamental theorem. For instance, for open
Proc. ACM Program. Lang., Vol. 1, No. CONF, Article 1. Publication date: January 2021.
A unifying type-theory for higher-order (amortized) cost analysis 1:11
terms which only partially use the input potential and save the rest with the result, we can deriveCorollary 3. Here, ๐ is a thunk that expects as input a unit argument, but with some associatedpotential ๐. When applied, ๐ returns a computation (of 0 cost) that forces to a value with a residualpotential ๐โฒ. The corollary says that if the context ฮ provides a potential ๐๐ , then forcing ๐ (withthe substitution ๐พ ) incurs a cost ๐ฝ and produces a value ๐ฃ that requires potential ๐๐ฃ such that๐ฝ โค (๐ + ๐๐ ) โ (๐โฒ + ๐๐ฃ). This expression may look complex, but it is simply a difference of theincoming potentials of ๐ (๐ and ๐๐ ) and the outgoing potentials of ๐ (๐โฒ and ๐๐ฃ). In Section 4, weshow an interesting use of this corollary for deriving an alternate proof of soundness of univariateRAML via its embedding in _-amorโ.Corollary 3. โฮ, ๐, ๐, ๐โฒ, ๐, ๐๐ , ๐พ, ๐ฝ , ๐ฃ๐ก , ๐ฃ ..; .; .; .; ฮ โข ๐ : [๐] 1 โธ M 0 ( [๐โฒ] ๐) โง (๐๐ , _, ๐พ) โ JฮKE โง ๐ () ๐พ โ ๐ฃ๐ก โ๐ฝ ๐ฃ =โโ๐๐ฃ . (๐๐ฃ, _, ๐ฃ) โ J๐K โง ๐ฝ โค (๐ + ๐๐ ) โ (๐โฒ + ๐๐ฃ).
3 EXAMPLESNext, we show three nontrivial examples of cost analysis in _-amorโ. Complete type derivationsfor the examples can be found in the technical appendix. The technical appendix also has additionalexamples that we omit here: Okasakiโs implicit queue in the call-by-name setting, and the standardlist map and append functions.
3.1 Functional queueEager functional FIFO queues are often implemented using two LIFO stacks represented as standardfunctional lists, say ๐1 and ๐2. Enqueue is implemented as a push (cons) on ๐1. Dequeue is implementedas a pop (head) from ๐2 if it is non-empty. However, if ๐2 is empty, then the contents of ๐1 aretransferred, one at a time, to ๐2 and the new ๐2 is popped. The transfer from ๐1 to ๐2 reverses ๐1,thus changing the stackโs LIFO semantics to a queueโs FIFO semantics. We describe the amortizedanalysis of this eager queue in _-amorโ. Our cost model is that every list cons operation incurs aunit cost and no other operation incurs any cost.
Note that the worst-case cost of dequeue is linear in the size of ๐1. However, the amortized costof dequeue is actually constant. This is proved by counting the cost of transferring an elementfrom ๐1 to ๐2 ahead of time โ when that element is enqueued in ๐1. This works because an enqueuedelement can be transferred from ๐1 to ๐2 at most once. Concretely, the enqueue operation has acost (it requires a potential) of 3 units, of which 1 is used by the enqueue operation itself and theremaining 2 are stored as potential with the element in the list ๐1, to be used later in the dequeueoperation if required. This is reflected in the type of enqueue below. The program code for enqueueis straightforward, so we skip it here.๐๐๐ : โ๐,๐. [3] 1 โธ ๐ โธ ๐ฟ๐ ( [2] ๐) โธ ๐ฟ๐๐ โธ M 0 (๐ฟ๐+1 ( [2] ๐) โ ๐ฟ๐๐)Observe how each element of the first list ๐1 in both the input and the output has a potential
2 associated with it. The dequeue operation (denoted by ๐๐ below) is a bit more involved. Theconstraints in the type of dequeue reflect that: a) dequeue can only be performed on a non-emptyqueue, i.e., if๐ + ๐ > 0 and b) the sum of the lengths of the resulting list is only 1 less than thelength of the input lists, i.e., โ๐โฒ, ๐โฒ.((๐โฒ + ๐โฒ + 1) = (๐ + ๐)). The full type and the term for thedequeue operation are described in Listing 1. Dequeue uses a function๐๐๐ฃ๐ , which moves elementsfrom the first list to the second. We skip the description of๐๐๐ฃ๐ . Type-checked terms for ๐๐๐, ๐๐and๐๐๐ฃ๐ are in the technical appendix.๐๐ : โ๐,๐.(๐ + ๐ > 0) โ ๐ฟ๐ ( [2] ๐) โธ ๐ฟ๐๐ โธ M 0 (โ๐โฒ, ๐โฒ.((๐โฒ + ๐โฒ + 1) = (๐ + ๐))&(๐ฟ๐โฒ [2] ๐ โ ๐ฟ๐
โฒ๐))
๐๐ โ ฮ.ฮ.ฮ._ ๐1 ๐2 . match ๐2 with|nil โฆโ bind ๐๐ =๐๐๐ฃ๐ [] [] ๐1 nil in
Proc. ACM Program. Lang., Vol. 1, No. CONF, Article 1. Publication date: January 2021.
1:12 Vineet Rajani, Marco Gaboardi, Deepak Garg, and Jan Hoffmann
match ๐๐ with|nil โฆโ fix๐ฅ .๐ฅ|โ๐ :: ๐ โฒ๐ โฆโ retฮ.โจโจnil, ๐ โฒ๐ โฉโฉ
|โ2 :: ๐ โฒ2 โฆโ retฮ.โจโจ๐1, ๐ โฒ2โฉโฉListing 1. Dequeue operation for eager functional queue in _-amorโ
3.2 Church encodingUnlike our previous example where we had a fixed cost for both enqueue and dequeue, now wedescribe a function whose cost actually varies each time it is applied. To illustrate this point, weencode Church numerals in _-amorโ. Church numerals are a classic example of higher-order termswhere an argument function is applied multiple times to encode various natural numbers. Thismeans that if we want to assign a variable cost (say ๐ถ ๐ for the ๐th instance) to the argumentfunction, we really need to make the type polymorphic in ๐ . This gives rise to a type which usessecond-rank index polymorphism.To recap, Church numerals encode natural numbers as function applications. For example, a
Church zero is defined as _๐ ._๐ฅ .๐ฅ (with zero applications), a Church one as _๐ ._๐ฅ .๐ ๐ฅ (with oneapplication), a Church two as _๐ ._๐ฅ .๐ (๐ ๐ฅ) (with two applications) and so on. To type a Churchnumeral, we must specify a type for ๐ . We assume that we have an N-indexed family of types ๐ผ ,and that ๐ maps ๐ผ ๐ to ๐ผ (๐ + 1) for every ๐ . Then, the ๐th Church numeral, given such a function ๐ ,maps ๐ผ 0 to ๐ผ ๐.
Next, we consider costs. Just for illustration, suppose that we count a unit cost for every functionapplication. We want to encode the precise costs of operations like addition, multiplication andexponentiation in types. Classically, these operations are defined compositionally. For example,addition of๐ and ๐ is defined by applying๐ to the successor function and ๐. This iterates thesuccessor function๐ times over ๐. To type this, the type of ๐ in the Church nats must be generalenough. For this, we use a cost family ๐ถ from N to R+. The cost of applying ๐ depends on theindex of the argument. Then, given such a ๐ , the ๐th Church numeral maps ๐ผ 0 to ๐ผ ๐ with cost((โ๐<๐๐ถ ๐) + ๐), where each ๐ถ ๐ is the cost of using ๐ the ๐th time, and the last ๐ is the cost of the๐ applications in the definition of the ๐th Church numeral. Our type for Church numerals, calledNat below, captures exactly this intuition.
Nat = _๐ก๐.โ๐ผ : Nโ ๐๐ฆ๐๐.โ๐ถ : Nโ R+ .!(โ๐ .((๐ผ ๐ โ [๐ถ ๐] 1) โธ M 0 (๐ผ ( ๐ + 1)))) โธM 0 ((๐ผ 0 โ [(โ๐<๐ ๐ถ ๐) + ๐] 1) โธ M 0 (๐ผ ๐))
Below, we describe a term for the Church one, denoted 1, that has type Nat 1. For notationalsimplification, we define ๐1 โ1 ๐2 โ (bindโ = โ1 in ret(๐1 ๐2)), which applies ๐1 to ๐2 andadditionally incurs a cost of 1 unit.
1 : Nat 11 โ ฮ.ฮ._๐ . ret (_๐ฅ . let !๐๐ข = ๐ in let โจโจ๐ฆ1, ๐ฆ2โฉโฉ = ๐ฅ in releaseโ = ๐ฆ2 in bind๐ = store() in ๐๐ข [] โ1โจโจ๐ฆ1, ๐โฉโฉ)
The term 1 takes the input pair ๐ฅ of type ๐ผ 0 โ [((๐ถ 0) + 1)] 1 and binds its two components to๐ฆ1 and ๐ฆ2. It then releases the potential ((๐ถ 0) + 1) in ๐ฆ2, stores ๐ถ 0 of the released potential in ๐,and applies the input function ๐๐ข to โจโจ๐ฆ1, ๐โฉโฉ, incurring a cost of 1 unit. This incurred cost modelsthe cost of the application (which we want to count). It is offset by the remaining 1 potential thatwas released from ๐ฆ2.
Next, we show the encoding for Church addition. Church addition is defined using a successorfunction (๐ ๐ข๐๐), which is also defined and type-checked in _-amorโ, but whose details we elide
Proc. ACM Program. Lang., Vol. 1, No. CONF, Article 1. Publication date: January 2021.
A unifying type-theory for higher-order (amortized) cost analysis 1:13
here. It is just enough to know that the cost of ๐ ๐ข๐๐ under our cost model is two units.
๐ ๐ข๐๐ : โ๐. [2] 1 โธ M 0 (Nat[๐] โธ M 0 (Nat[๐ + 1]))An encoding of Church addition (๐๐๐) in _-amorโ is shown below. The type of ๐๐๐ takes the
required potential (4โ๐1+2 here) along with two Church naturals (Nat ๐1 andNat ๐2) as argumentsand computes their sum. The potential of (4 โ ๐1 + 2) units corresponds to the precise cost ofperforming the Church addition in our cost model. The whole type is parameterized on ๐1 and ๐2.Ignoring the decorations for monadic operations, ๐๐๐ simply applies ๐1 to ๐ ๐ข๐๐ and ๐2, as expected.
๐๐๐ : โ๐1, ๐2 . [(4 โ ๐1 + 2)] 1 โธ M 0 (Nat ๐1 โธ M 0 (Nat ๐2 โธ M 0 (Nat (๐1 + ๐2))))๐๐๐ โ ฮ.ฮ._๐. ret(_๐1 . ret(_๐2 . releaseโ = ๐ in bind๐ = ๐ธ1 in ๐ธ2))๐ธ1 โ ๐1 [] [] โ1!(ฮ._๐ก . let โจโจ๐ฆ1, ๐ฆ2โฉโฉ = ๐ก in releaseโ = ๐ฆ2 in
bind๐1 = (bind๐2 = store() in (๐ ๐ข๐๐ [] ๐2)) in ๐1 โ1 ๐ฆ1)๐ธ2 โ bind๐ = store() in ๐ โ1โจโจ๐2, ๐โฉโฉ
Listing 2. Encoding of the Church addition in _-amorโ
We have similarly encoded Church multiplication and exponentiation in _-amorโ. We are un-aware of such a general encoding of Church numerals in a monadic cost framework without theuse of potentials.
3.3 List foldAs our final example, we describe the cost analysis of a program that uses higher-order functionswith variable cost and recursion, a combination that prior work like RAML/AARA [Hoffman 2011;Hoffmann et al. 2017] cannot handle. Our example is a slightly modified right-fold function for lists.
Conceptually, the right fold function aggregates (folds) a list by applying an aggregation function(given as an argument) to the elements of a list (starting from the rightmost element) along witha starting value (also given as an argument). We are interested in an encoding where the cost ofapplying the aggregation function varies in each recursion step. To model this kind of variable costwe make use of a cost family ๐ถ : Nโ R+. The intent is that every time the aggregation function isapplied, its cost could be different. In particular, the cost of the ๐th application is ๐ถ (๐ โ ๐) units,where ๐ is the length of the list.
๐ ๐๐๐๐ : โ๐ผ, ๐ฝ, ๐,๐ถ : Nโ R+ .!(โ๐ . [๐ถ ๐] 1 โธ Nat(๐) โธ ๐ผ โธ ๐ฝ โธ M 0 ๐ฝ) โธ !Nat(๐) โธ ๐ฝ โธ ๐ฟ๐๐ผ โธ [โ๐<๐ ๐ถ ๐] 1 โธ M 0 ๐ฝ
๐ ๐๐๐๐ โ fix๐ โฒ.ฮ.ฮ.ฮ.ฮ._๐ ๐ ๐ ๐๐ ๐.
let !๐๐ข = ๐ inlet !๐๐ข = ๐ inmatch ๐๐ with|nil โฆโ ret ๐ |โ :: ๐ก โฆโ release _ = ๐ in
bind ๐ โฒ = store() inbind๐ โฒโฒ = store() inbind ๐ก๐ = ๐ โฒ [] [] [] [] !๐๐ข !(๐๐ข โ 1) ๐ ๐ก ๐ โฒโฒ in(๐๐ข [] ๐ โฒ (๐๐ข โ 1) โ ๐ก๐ )
Listing 3. foldr functionThe type and the term for ๐ ๐๐๐๐ are described in Listing 3. The type of ๐ ๐๐๐๐ is parametric in
๐ผ, ๐ฝ, ๐ and ๐ถ , where ๐ผ is the type of the elements of the list, ๐ฝ is the type of the result, ๐ is thelength of the input list and ๐ถ , as mentioned earlier, is the cost family used to model the varying
Proc. ACM Program. Lang., Vol. 1, No. CONF, Article 1. Publication date: January 2021.
1:14 Vineet Rajani, Marco Gaboardi, Deepak Garg, and Jan Hoffmann
cost of the aggregation function. To obtain a variable cost for each application of the aggregatingfunction, we make use of second-rank index polymorphism like we did for Church numerals. Thismeans that we parameterize the aggregating function with an index which can be instantiatedappropriately to obtain the desired cost. We specify the cost of each application as a potential of(๐ถ ๐) units in a negative position of the aggregating function. To obtain a function whose cost trulydepends on ๐ , we add a parameter of type Nat(๐) (which denotes a singleton type over naturals)to the aggregating function (the first argument of ๐ ๐๐๐๐ ).5 The overall type of the aggregatingfunction has an exponential because we use it multiple times, once in every recursive call. Finally,the total cost (obviously
โ๐<๐๐ถ ๐ units) must be provided as an input potential to ๐ ๐๐๐๐ .
4 EMBEDDING UNIVARIATE RAMLIn this section we describe an embedding of Resource Aware ML (RAML) [Hoffman 2011; Hoffmannand Hofmann 2010] into _-amorโ. RAML is an effect-based type system for amortized analysisof OCaml programs using the method of potentials [Cormen et al. 2009; Tarjan 1985]. The mainmotivation for this embedding is to show that: 1) _-amorโ can also perform effect-based cost analysislike RAML and thus can be used to analyze all examples that have been tried on RAML, 2) _-amorโ,despite being call-by-name in the pure part, can embed RAML which is a call-by-value framework.
We describe an embedding of Univariate RAML [Hoffman 2011; Hoffmann and Hofmann 2010](which subsumes Linear RAML [Hofmann and Jost 2003]) into _-amorโ. We leave embeddingmultivariate RAML [Hoffmann et al. 2011] to future work but anticipate no fundamental difficultiesin doing so.
4.1 A brief primer on Univariate RAMLWe give a brief primer on Univariate RAML [Hoffman 2011; Hoffmann and Hofmann 2010] here.The key feature of Univariate RAML is its ability to encode univariate polynomials in the size of theinput as potential functions. Such functions are expressed as non-negative linear combinations ofbinomial coefficients
(๐๐
), where๐ is the size of the input data structure and ๐ is some natural number.
Vector annotations on the list type ๐ฟ ยฎ๐๐ , for instance, are used as a representation of such univariatepolynomials. The underlying potential on a list of size ๐ and type ๐ฟ ยฎ๐๐ can then be described as๐ ( ยฎ๐, ๐) โ โ
1โค๐โค๐(๐๐
)๐๐ where ยฎ๐ = {๐1 . . . ๐๐ }. The authors of RAML show using the properties of
binomial coefficients, that such a representation is amenable to an inductive characterization ofpolynomials which plays a crucial role in setting up the typing rules of their system. If ยฎ๐ = {๐1 . . . ๐๐ }is the potential vector associated with a list then โณ( ยฎ๐) = {๐1 + ๐2, ๐2 + ๐3, . . . ๐๐โ1 + ๐๐ , ๐๐ } is thepotential vector associated with the tail of that list. Trees follow a treatment similar to lists. Basetypes (unit, bools, ints) have zero potential and the potential of a pair is just the sum of the potentialsof the components. A snippet of the definition of the potential function ฮฆ(๐ : ๐ด) (from Hoffman[2011]) is described below.ฮฆ(๐ : ๐ด) = 0 where ๐ด โ {๐ข๐๐๐ก, ๐๐๐ก, ๐๐๐๐} ฮฆ( [] : ๐ฟ ยฎ๐๐ด) = 0ฮฆ((๐1, ๐2) : (๐ด1, ๐ด2)) = ฮฆ(๐1 : ๐ด1) + ฮฆ(๐2 : ๐ด2) ฮฆ((๐ :: โ) : ๐ฟ ยฎ๐๐ด) = ๐1 + ฮฆ(๐ : ๐ด) + ฮฆ(โ : ๐ฟโณยฎ๐๐ด)
where ยฎ๐ = {๐1 . . . ๐๐ }
A type system is built around this basic idea with a typing judgment of the form ฮฃ; ฮ โข๐๐โฒ ๐๐ : ๐
where ฮ is a typing context mapping free variables to their types, ฮฃ is a context for functionsignatures mapping a function name to a type (this is separate from the typing context becauseRAML only has first-order functions that are declared at the top-level), ๐ and ๐โฒ denote the staticallyapproximated available and remaining potential before and after the execution of ๐๐ , respectively,
5One could choose to represent Nat(๐) as ๐ฟ๐1 in _-amor. We use singleton types just for clarity here.
Proc. ACM Program. Lang., Vol. 1, No. CONF, Article 1. Publication date: January 2021.
A unifying type-theory for higher-order (amortized) cost analysis 1:15
๐1๐/๐โฒโ ๐2 โ ฮฃ(๐ )
ฮฃ;๐ฅ : ๐1 โข๐+๐พ๐๐๐
1๐โฒโ๐พ๐๐๐
2๐ ๐ฅ : ๐2
appยฎ๐ = (๐1, . . . , ๐๐ )
ฮฃ;๐ฅโ : ๐, ๐ฅ๐ก : ๐ฟ (โณ ยฎ๐)๐ โข๐+๐1+๐พ๐๐๐๐
๐ ๐๐๐๐ (๐ฅโ, ๐ฅ๐ก ) : ๐ฟ ยฎ๐๐cons
Fig. 6. Selected type rules of Univariate RAML from Hoffman [2011]
and ๐ is the zero-order type of ๐๐ . Vector annotations are specified on list and tree types (asmentioned above).
Types of first-order functions follow an intuition similar to the typing judgment above. ๐1๐/๐โฒโ๐2
denotes the type of a first-order RAML function which takes an argument of type ๐1 and returns avalue of type ๐2. ๐ units of potential are needed before this function can be applied and ๐โฒ unitsof potential are left after this function has been applied. Intuitively, the cost of the function isupper-bounded by (๐+potential of the input) - (๐โฒ+potential of the result). Fig. 6 describe typingrules for function application and list cons. The app rule type-checks the function applicationwith an input and remaining potential of (๐ + ๐พ๐๐๐1 ) and (๐โฒ โ ๐พ๐๐๐2 )6 units, respectively. RAMLdivides the cost of application into ๐พ๐๐๐1 and ๐พ๐๐๐2 units. Of the available ๐ +๐พ๐๐๐1 units, ๐ units arerequired by the function itself and ๐พ๐๐๐1 units are consumed before the application is performed.Likewise, of the remaining ๐โฒ โ ๐พ๐๐๐2 units, ๐โฒ units are made available from the function and ๐พ๐๐๐2units are consumed after the application is performed. The cons rule requires an input potential of๐ + ๐1 +๐พ๐๐๐๐ units of which ๐1 units are added to the potential of the resulting list and ๐พ๐๐๐๐ unitsare consumed as the cost of performing this operation.
Soundness of the type system is defined by Theorem 4. Soundness is defined for top-level RAMLprograms (formalized later in Definition 6), which basically consist of first-order function definitions(denoted by ๐น ) and the "main" expression ๐ , where execution starts. Stack (denoted by ๐ ) and heap(denoted by ๐ป ) are used to provide bindings for free variables and locations in ๐ .
Theorem 4 (Univariate RAMLโs soundness). โ๐ป,๐ป โฒ,๐ , ฮ, ฮฃ, ๐, ๐, ๐ v, ๐, ๐ โฒ, ๐, ๐โฒ, ๐ก .๐ = ๐น, ๐ is a RAML top-level program and๐ป |= ๐ : ฮ โง ฮฃ, ฮ โข๐
๐โฒ ๐ : ๐ โง๐ ,๐ป โข๐๐โฒ ๐ โ๐ก ๐ v, ๐ป โฒ =โ ๐ โ ๐ โฒ โค (ฮฆ๐ป,๐ (ฮ) + ๐) โ (๐โฒ + ฮฆ๐ป (๐ v : ๐))
4.2 Type-directed translation of Univariate RAML into _-amorโ
As mentioned above, types in Univariate RAML include unit, booleans, integers, lists, trees, pairsand first-order functions. Without loss of generality we introduce two simplifications: a) we abstractRAMLโs bool and int types into an arbitrary base type denoted by b and b) we just choose to workwith the list type only ignoring trees. These simplifications only make the development moreconcise as we do not have to deal with the redundancy of treating similar types again and again.
The translation from Univariate RAML to _-amorโ is type-directed. We describe the type trans-lation function (denoted by L.M) from RAML types to _-amorโ types in Fig. 7.
L๐ข๐๐๐กM = 1LbM = !bL๐ฟ ยฎ๐ ๐M = โ๐ .( [๐ ( ยฎ๐, ๐ )] 1 โ ๐ฟ๐ L๐M)
L(๐1, ๐2)M = (L๐1M โ L๐2M)
L๐1๐/๐โฒโ ๐2M = [๐] 1 โธ L๐1M โธ M 0 ( [๐โฒ] L๐2M)
Fig. 7. Type translation of Univariate RAML
Since RAML allows for full replication of unit and base types, we translate RAMLโs base type, b,into !b of _-amorโ. But translation of the unit type does not need a !, as 1 and !1 are isomorphic in_-amorโ. Unlike the unit and base type of RAML, the list type does have some potential associatedwith it, represented by ยฎ๐. Therefore, we translate RAMLโs list type into a pair type composed ofa modal unit type carrying the required potential and a _-amorโ list type. Since the list type in_-amorโ is refined with size, we add an existential on the pair to quantify the size of the list. The6Every time a subtraction like (๐ผ โ ๐ฝ ) appears, RAML implicitly assumes that there is a side condition (๐ผ โ ๐ฝ ) โฅ 0.
Proc. ACM Program. Lang., Vol. 1, No. CONF, Article 1. Publication date: January 2021.
1:16 Vineet Rajani, Marco Gaboardi, Deepak Garg, and Jan Hoffmann
๐1๐/๐โฒโ ๐2 โ ฮฃ(๐ )
ฮฃ;๐ฅ : ๐1 โข๐+๐พ๐๐๐
1๐โฒโ๐พ๐๐๐
2๐ ๐ฅ : ๐2 { _๐ข.releaseโ = ๐ข in bindโ = โ๐พ
๐๐๐
1 in bind ๐ = store() in ๐ธ1app
๐ธ1 = bind ๐1 = (๐ ๐ ๐ฅ) in release ๐2 = ๐1 in bindโ = โ๐พ๐๐๐
2 in bind ๐3 = store ๐2 in ret ๐3Fig. 8. Expression translation for the app case: Univariate RAML to _-amorโ
potential captured by the unit type must equal the potential associated with the RAML list (thisis represented by the function ๐ ( ยฎ๐, ๐ )). The function ๐ ( ยฎ๐, ๐ ) corresponds to the one that RAMLuses to compute the total potential associated with a list of ๐ elements, which we described above.Note the difference in how potentials are managed in RAML vs how they are managed in thetranslation. In RAML, the potential for an element gets added to the potential of the tail withevery cons operation and, dually only the potential of the head element is consumed in the matchoperation. The translation, however, does not assign potential on a per-element basis. Instead, thetotal potential of the entire list is captured using the ๐ function and the translations of the consand the match expressions work by adding or removing potential from this total. We believe atranslation which works with per element potential is also feasible but we would need an additionalindex to identify the elements of the list in the list data type.We translate a RAML pair type into a tensor (โ) pair. This is in line with how pairs are treated
in RAML (both elements of the pair are available on elimination). Finally, a function type ๐1๐/๐โฒโ๐2
in RAML is translated into the function type [๐] 1 โธ L๐1M โธ M 0 ( [๐โฒ] L๐2M). As in RAML, thetranslated function type also requires a potential of ๐ units for application and a potential of ๐โฒunits remains after the application. The monadic type is required because we cannot release/storepotential without going into the monad. The translation of typing contexts is defined pointwiseusing the type translation function.
We use this type translation function to produce a translation for Univariate RAML expressionsby induction on RAMLโs typing judgment. The translation judgment is ฮฃ; ฮ โข๐
๐โฒ ๐๐ : ๐ { ๐๐ . Itbasicallymeans that awell-typed RAML expression ๐๐ is translated into a _-amorโ expression ๐๐ . Thetranslated expression is of the type [๐] 1 โธ M 0 ( [๐โฒ]L๐M). We only describe the app rule here (Fig. 8).Since we know that the desired term must have the type [๐ + ๐พ๐๐๐1 ] 1 โธ M 0 ( [๐โฒ โ ๐พ๐๐๐2 ]L๐M), thetranslated term is a function which takes an argument, ๐ข, of the desired modal type and releasesthe potential to make it available for consumption. The continuation then consumes ๐พ๐๐๐1 potentialthat leaves ๐ potential remaining for bind ๐ = store() in ๐ธ1. We then store ๐ units of potential withthe unit and use it to perform a function application. We get a result of typeM 0 ( [๐โฒ] L๐2M). Werelease these ๐โฒ units of potential and consume ๐พ๐๐๐2 units from it. This leaves us with a remainingpotential of ๐โฒ โ๐พ๐๐๐2 units. We store this remaining potential with ๐2 and wrap it up in a monad toget the desired type. Translations of other RAML terms (which we do not describe here) follow asimilar approach. The entire translation is intuitive and relies extensively on the ghost operationsstore and release at appropriate places.
We show that the translation is type-preserving by proving that the obtained _-amorโ terms arewell-typed (Theorem 5). The proof of this theorem works by induction on RAMLโs type derivation.
Theorem 5 (Type preservation: Univariate RAML to _-amorโ). If ฮฃ; ฮ โข๐๐โฒ ๐ : ๐ in Univariate RAML
then there exists ๐ โฒ such that ฮฃ; ฮ โข๐๐โฒ ๐ : ๐ { ๐ โฒ and there is a derivation of .; .; .; LฮฃM, LฮM โข ๐ โฒ :
[๐] 1 โธ M 0 ( [๐โฒ]L๐M) in _-amorโ.
As mentioned earlier, RAML only has first-order functions which are defined at the top-level. So,we need to lift this translation to the top-level. Definition 6 defines the top-level RAML programalong with the translation.
Proc. ACM Program. Lang., Vol. 1, No. CONF, Article 1. Publication date: January 2021.
A unifying type-theory for higher-order (amortized) cost analysis 1:17
Definition 6 (Top level RAML program translation). Assume a top-level RAML program๐ โ ๐น, ๐๐๐๐๐ where ๐น โ ๐ 1(๐ฅ) = ๐๐ 1, . . . , ๐ ๐(๐ฅ) = ๐๐ ๐ s.t.ฮฃ, ๐ฅ : ๐๐ 1 โข๐1๐โฒ1 ๐๐ 1 : ๐
โฒ๐ 1 . . . ฮฃ, ๐ฅ : ๐๐ ๐ โข๐๐
๐โฒ๐๐๐ ๐ : ๐ โฒ
๐ ๐and ฮฃ, ฮ โข๐
๐โฒ ๐๐๐๐๐ : ๐
where ฮฃ = ๐ 1 : ๐๐ 1๐1/๐โฒ1โ ๐ โฒ
๐ 1, . . . , ๐ ๐ : ๐๐ ๐๐๐/๐โฒ๐โ ๐ โฒ
๐ ๐.
Then, the translation of ๐ , denoted by ๐ , is defined as (๐น, ๐๐ก ) where๐น = fix๐1._๐ข._๐ฅ .๐๐ก1, . . . , fix๐๐ ._๐ข._๐ฅ .๐๐ก๐ s.t.ฮฃ, ๐ฅ : ๐๐ 1 โข๐1๐โฒ1 ๐๐ 1 : ๐
โฒ๐ 1 { ๐๐ก1 . . . ฮฃ, ๐ฅ : ๐๐ ๐ โข๐๐
๐โฒ๐๐๐ ๐ : ๐ โฒ
๐ ๐{ ๐๐ก๐ and
ฮฃ, ฮ โข๐๐โฒ ๐๐๐๐๐ : ๐ { ๐๐ก .
4.3 Semantic properties of the translationBesides type-preservation, we additionally: 1) prove that our translation preserves semanticsand cost of the source RAML term and 2) re-derive RAMLโs soundness result using _-amorโโsfundamental theorem (Theorem 2) and properties of the translation. This is a sanity check to ensurethat our type translation preserves cost meaningfully (otherwise, we would not be able to recoverRAMLโs soundness theorem in this way).
Semantics and cost preservation is formally stated in Theorem 7, which can be read as follows: if๐๐ is a closed source (RAML) term which translates to a target (_-amorโ) term ๐๐ก and if the sourceexpression evaluates to a value (and a heap๐ป , because RAML uses imperative boxed data structures),then the target term after applying to a unit (because the translation is always a function) canbe evaluated to a value ๐กv๐ via pure (โ) and forcing (โ๐ฝ ) relations such that the source and thetarget values are the same and the cost of evaluation in the target is at least as much as the cost ofevaluation in the source.
Theorem 7 (Semantics and cost preservation). โ๐ป, ๐, ๐ v, ๐, ๐ โฒ, ๐, ๐โฒ..; . โข๐
๐โฒ ๐๐ : b { ๐๐ก โง ., . โข๐๐โฒ ๐ โ ๐ v, ๐ป =โโ๐กv๐ , ๐ฝ .๐๐ก () โ _ โ๐ฝ ๐กv๐ โง ๐ v = ๐กv๐ โง ๐ โ ๐ โฒ โค ๐ฝ
The proof of Theorem 7 is via a cross-language relation between RAML and _-amorโ terms.The relation (described in the technical appendix) is complex because it has to relate RAMLโsimperative data structures (like list which is represented as a chain of pointers in the heap) with_-amorโโs purely functional datastructures. The fundamental theorem of this relation allows us toestablish that the source expression and its translation are related, which implies semantics andcost preservation as required by Theorem 7.
Finally, we re-derive RAMLโs soundness (Theorem 4) in _-amorโ using _-amorโโs fundamentaltheorem and the properties of the translation. To prove this theorem, we obtain a translated termcorresponding to the term ๐ (of Theorem 4) via our translation. Then, using Theorem 7, we showthat the cost of forcing the unit application of the translated term is lower-bounded by ๐ โ ๐ โฒ.After that, we use Corollary 3 to obtain the upper-bound on ๐ โ ๐ โฒ as required in the statement ofTheorem 4.
5 _-amor (WITH SUB-EXPONENTIALS)_-amorโ is quite expressive, but it can only represent one or an unbounded number of copies of aterm. This was evident in the encoding of Church numerals (Section 3.2) and ๐ ๐๐๐๐ (Section 3.3). Inthe Church numeral ๐, the argument function can only be used ๐ times, yet the type requires anunbounded number of copies of the function, since we cannot express โ๐ copiesโ using just !. Asimilar situation reappeared in ๐ ๐๐๐๐ where the aggregating function can only be used ๐ times (oncein every recursion step), but the type requires an unbounded number of copies of this function.
Proc. ACM Program. Lang., Vol. 1, No. CONF, Article 1. Publication date: January 2021.
1:18 Vineet Rajani, Marco Gaboardi, Deepak Garg, and Jan Hoffmann
Indices ๐ผ , ๐ฝ , ๐พ ::= . . . | โโณ ๐ฝ ,๐พ๐ ๐ผ | . . .
Types ๐ ::= . . . | !๐<๐ผ๐ | . . .Non-affine context ฮฉ ::= . | ฮฉ, ๐ฅ :๐<๐ผ ๐for term variables
ฮฉ1 + ฮฉ2 โ
ฮฉ2 ฮฉ1 = .(ฮฉโฒ
1 + ฮฉ2/๐ฅ), ๐ฅ :๐<๐ผ+๐ฝ ๐ ฮฉ1 = ฮฉโฒ1, ๐ฅ :๐<๐ผ ๐ [๐/๐] โง (๐ฅ :๐<๐ฝ ๐ [๐ผ + ๐/๐]) โ ฮฉ2
(ฮฉโฒ1 + ฮฉ2), ๐ฅ :๐<๐ผ ๐ ฮฉ1 = ฮฉโฒ
1, ๐ฅ :๐<๐ผ ๐ โง (๐ฅ :โ โ) โ ฮฉ2โ๐<๐ผ ฮฉ โ
{. ฮฉ = .
(โ๐<๐ผ ฮฉ), ๐ฅ :๐<โ๐<๐ผ ๐ฝ
๐ ฮฉ = ฮฉโฒ, ๐ฅ :๐<๐ฝ ๐ [(โ๐<๐ ๐ฝ [๐/๐] + ๐)/๐]
Fig. 9. Changes to the type system syntax to obtain _-amor from _-amorโ
To overcome this limitation and improve expressiveness, we refine the exponential type !๐of _-amorโ to a dependent sub-exponential !๐<๐๐ , which is morally equivalent to the iteratedtensor ๐ [0/๐] โ ๐ [1/๐] . . . โ ๐ [(๐ โ 1)/๐]. Thus, !๐<๐๐ not only specifies a finite bound ๐ on thenumber of copies of the underlying term, but also provides the ability to give each of them adifferent type (by varying the substitution for ๐). To the best of our knowledge, this dependentsub-exponential was first introduced in Bounded Linear Logic [Girard et al. 1992]. Subsequent workon ๐โPCF [Dal Lago and Gaboardi 2011] and its variant [Dal Lago and Petit 2012] showed how thedependent sub-exponential can be used as part of a coeffect-based cost analysis for PCF programs.In the rest of this section, we extend _-amorโ with the dependent sub-exponential to obtain
the full system _-amor. This allows the representation of bounded replication as explained above.However, unlike the work on ๐โPCF, we do not adopt the coeffect-style of cost analysis in _-amor.Costs are still represented via the cost monad of _-amorโ. We later show in Section 6 how ๐โPCFโscoeffect-based cost analysis can be simulated in _-amor.
5.1 Changes to the types and type systemSyntax. We take the same language that was described in Section 2 but replace the exponential
type with an indexed sub-exponential type. There are no changes to the term syntax or the semanticsof the language. However, we extend the index term language with additional counting constructsthat are described below. The changes to the types and indices are summarized in Fig. 9.
In particular, we include the forest cardinality operatorโโณ ๐ผ ,๐ฝ๐ ๐พ in the index language. This operator,
inspired from ๐โPCF, counts the number of nodes in a forest of trees. Specifically, suppose we havea forest of trees whose nodes are numbered in a pre-order depth-first traversal starting from theroots of the trees (where the trees have been totally ordered in some way). Assume that the numberof children of node ๐ is ๐พ (๐) (๐พ has ๐ free in it). Then, โโณ ๐ผ ,๐ฝ
๐ ๐พ is the total number of nodes in thetrees rooted at the node ๐ผ and its next ๐ฝ โ 1 siblings. The formal definition of the forest cardinalityoperator is shown in Fig. 10. The forest cardinality operator is used to represent the cardinality ofarbitrary recursion trees (note that _-amor inherits the general fixpoint operator from _-amorโ).
โโณ ๐ผ ,0๐ ๐พ = 0
โโณ ๐ผ ,๐ฝ +1๐ ๐พ = โโณ ๐ผ ,๐ฝ๐ ๐พ + (โโณ ๐ผ+1+โโณ๐ผ ,๐ฝ๐ ๐พ,๐พ [๐ผ+โโณ ๐ผ ,๐ฝ
๐ ๐พ/๐]๐ ๐พ)
Fig. 10. Formal definition of forest cardinality from [Dal Lago and Gaboardi 2011]
Typing judgment. The typing judgment of _-amor is the same as that of _-amorโ: ฮจ;ฮ;ฮ;ฮฉ; ฮ โข๐ : ๐ . However, the definition of ฮฉ is now different. The non-affine context ฮฉ now carries elementsof the form ๐ฅ :๐<๐ผ ๐ (Fig. 9). An element of this form means that ๐ผ copies of ๐ฅ are available withtypes ๐ [0/๐] . . . ๐ [(๐ผ โ 1)/๐], mirroring the dependent sub-exponential. The non-affine context also
Proc. ACM Program. Lang., Vol. 1, No. CONF, Article 1. Publication date: January 2021.
A unifying type-theory for higher-order (amortized) cost analysis 1:19
ฮจ;ฮ, ๐;ฮ, ๐ < ๐ผ ;ฮฉ; . โข ๐ : ๐ฮจ;ฮ;ฮ;
โ๐<๐ผ ฮฉ; . โข!๐ :!๐<๐ผ๐
T-subExpI
ฮจ;ฮ;ฮ;ฮฉ1; ฮ1 โข ๐ : (!๐<๐ผ๐) ฮจ;ฮ;ฮ;ฮฉ2, ๐ฅ :๐<๐ผ ๐ ; ฮ2 โข ๐ โฒ : ๐ โฒ
ฮจ;ฮ;ฮ;ฮฉ1 + ฮฉ2; ฮ1 + ฮ2 โข let !๐ฅ = ๐ in ๐ โฒ : ๐ โฒT-subExpE
ฮ,ฮ |= ๐ผ โฅ 1ฮจ;ฮ;ฮ;ฮฉ, ๐ฅ :๐<๐ผ ๐ ; ฮ โข ๐ฅ : ๐ [0/๐]
T-var2
ฮจ;ฮ, ๐;ฮ, ๐ < ๐ฟ;ฮฉ, ๐ฅ :๐<๐ผ ๐ [(๐ + 1 + โโณ๐+1,๐๐
๐ผ )/๐]; . โข ๐ : ๐ ๐ฟ โฅ โโณ 0,1๐๐ผ
ฮจ;ฮ;ฮ;โ๐<๐ฟ ฮฉ; . โข fix ๐ฅ .๐ : ๐ [0/๐]
T-fix
Fig. 11. Changes to the type rules
differs in the definition of the operator +, which was formerly just a disjoint union. Now, thecontexts being added may have common variables and we have to add their multiplicities. Therevised + operation on non-affine contexts is defined in Fig. 9. The figure also defines an iteratedsum operation on contexts,
โ๐<๐ผ ฮฉ.
Typing rules. We only describe the type rules for the sub-exponential, the fixpoint and the use ofnon-affine assumptions as these are the only rules of _-amorโ that change. See Fig. 11. T-subExpIis the rule for the introduction form of the sub-exponential. It says that if an expression ๐ has type๐ in the non-affine context ฮฉ and the constraint ๐ < ๐ผ for a fresh parameter ๐, and ๐ does notuse any any affine resources (indicated by an empty ฮ) then !๐ has type !๐<๐ผ๐ in the non-affinecontext
โ๐<๐ผ ฮฉ. Observe how the multiplicity ๐ผ of resources in the introduced type !๐<๐ผ๐ matches
the multiplicity ๐ผ in the concluding contextโ๐<๐ผ ฮฉ. The dual rule T-subExpE is straightforward:
Eliminating !๐<๐ผ๐ yields the assumption ๐ฅ :๐<๐ผ ๐ in the non-affine context for the continuation. Therule T-var2 specifies when such a hypothesis from the non-affine context can be used. The keyrequirement is that ๐ผ โฅ 1, i.e., the multiplicity of the hypothesis being used should be at least 1.The fixpoint expression (fix๐ฅ .๐) encodes recursion by allowing ๐ to refer to fix๐ฅ .๐ via ๐ฅ . T-fix is
the typing rule for this fixpoint construct. It is a refinement of the corresponding rule in Fig. 3. Therefinements serve two purposes: 1) they make the total number of recursive calls of the fixpointexplicit (this number is denoted ๐ฟ) and 2) they introduce a parameter ๐ that ranges over the differentrecursive calls, enumerated in a pre-order traversal of the recursion tree. The root of the tree (thetop-level call) corresponds to ๐ = 0. The type ๐ is parameterized by ๐. The term ๐ผ (๐) (the term ๐ผ
has ๐ free in it) is the number of children of the ๐th node in the recursion tree. The rule can beunderstood as follows. The first premise of the rule types an arbitrary recursive call correspondingto the node ๐ of the recursion tree. To type the fixpoint body ๐ for this call, the first premise allows๐ผ (๐) copies of the parameter ๐ฅ with appropriate types. These copies correspond to the results ofthe recursive calls below node ๐ (note that (๐ + 1 + โโณ๐+1,๐
๐๐ผ ) is the index of the ๐th child of node ๐
in the recursion tree). The second premise merely says that ๐ฟ must really be an upper bound onthe cardinality of a tree in which the ๐th node has ๐ผ (๐) children. The conclusion of the rule saysthat the entire fixpoint can be typed with ๐ฟ copies of ฮฉ, and the final type is ๐ [0/๐].
Subtyping. We also introduce a new subtyping rule, sub-bSum, which moves potential outside asub-exponential to inside. The rule, shown below, is sound because it does not change the totalpotential. Potentials are anyway ghost, so moving them from one place to another is allowed inour semantic model. Formally, the soundness of this as well as other subtyping rules is captured byLemma 8. ๐ and ] represent the substitutions for the type and index variables respectively.
Proc. ACM Program. Lang., Vol. 1, No. CONF, Article 1. Publication date: January 2021.
1:20 Vineet Rajani, Marco Gaboardi, Deepak Garg, and Jan Hoffmann
ฮจ;ฮ;ฮ โข ([โ๐<๐ผ ๐พ] !๐<๐ผ๐) <: (!๐<๐ผ [๐พ] ๐) sub-bSum
Lemma 8 (Value subtyping lemma). โฮจ,ฮ,ฮ, ๐ โ ๐๐ฆ๐๐, ๐ โฒ, ๐, ].ฮจ;ฮ;ฮ โข ๐ <: ๐ โฒ โง . |= ฮ] =โ J๐ ๐]K โ J๐ โฒ ๐]K
It is noteworthy that sub-bSum is the only subtyping rule in _-amor that specifies how twomodalities interact. In particular, we do not have a rule to make the sub-exponential and the monadinteract (i.e., we do not have what is often called a โdistributive lawโ [Gaboardi et al. 2016]). Wehave not encountered the need for such an interaction. However, studying such an interactioncould be an interesting direction for future work.
5.2 Model of types and soundnessWe only describe the value relation for the sub-exponential here as the remaining cases of thevalue relation are exactly the same as before. (๐,๐ , !๐) is in the value interpretation at type !๐<๐ผ๐iff the potential ๐ suffices for all ๐ผ copies of ๐ at the instantiated types ๐ [๐/๐] for 0 โค ๐ < ๐ผ . Theother change to the model is in the interpretation of ฮฉ. This time we have (๐, ๐ฟ) instead of (0, ๐ฟ) inthe interpretation of ฮฉ such that ๐ is sufficient for all copies of all variables in the context. Bothchanges to the model are described below.
J!๐<๐ผ๐K โ {(๐, !๐) | โ๐0, . . . , ๐๐ผโ1 .๐0 + . . . + ๐๐ผโ1 โค ๐ โง โ0 โค ๐ < ๐ผ .(๐๐ , ๐) โ J๐ [๐/๐]KE }JฮฉKE = {(๐, ๐ฟ) | โ๐ : V๐๐๐ โ I๐๐๐๐๐๐ โ P๐๐ก๐ .
(โ(๐ฅ :๐<๐ผ ๐) โ ฮฉ.โ0 โค ๐ < ๐ผ . (๐ ๐ฅ ๐, ๐ฟ (๐ฅ)) โ J๐ [๐/๐]KE ) โง(โ๐ฅ :๐<๐ผ๐ โฮฉ โ
0โค๐<๐ผ ๐ ๐ฅ ๐) โค ๐}
We formalize the soundness of _-amorโs type system as the following fundamental theorem.Compared to the fundamental theorem of _-amorโ (Theorem 2), this fundamental theorem has anadditional potential ๐๐ that comes from the interpretation of ฮฉ (this potential was 0 in _-amorโ).
Theorem 9 (Fundamental theorem). โฮจ,ฮ,ฮ,ฮฉ, ฮ, ๐, ๐ โ ๐๐ฆ๐๐, ๐๐ , ๐๐, ๐พ, ๐ฟ, ๐, ].ฮจ;ฮ;ฮ;ฮฉ; ฮ โข ๐ : ๐ โง (๐๐ , ๐พ) โ Jฮ ๐]KE โง (๐๐, ๐ฟ) โ Jฮฉ ๐]KE โง . |= ฮ ] =โ(๐๐ + ๐๐, ๐ ๐พ๐ฟ) โ J๐ ๐]KE
The proof of the theorem proceeds in a manner similar to that of Theorem 2, i.e., by inductionon the typing derivation. Now, in the fix case, we additionally induct on the recursion tree. Thisalso requires generalizing the induction hypothesis to account for the potential of the children of anode in the recursion tree. The technical appendix has the entire proof.
6 EMBEDDING ๐โPCFIn this section we describe an embedding of ๐โPCF [Dal Lago and Gaboardi 2011] into _-amor.๐โPCF is a coeffect-based cost-analysis type system (contrast this with RAMLwhich is an effect-basedtype system) which has been shown to be relatively complete for the cost analysis of PCF programs.The objective of this embedding is twofold: It shows that (a) _-amor can simulate coeffect-basedcost analysis like ๐โPCFโs and (b) _-amor is also relatively complete for PCF.
6.1 A brief primer on ๐โPCF๐โPCF [Dal Lago and Gaboardi 2011] is a call-by-name PCF with an affine, refinement type systemfor cost analysis.
Proc. ACM Program. Lang., Vol. 1, No. CONF, Article 1. Publication date: January 2021.
A unifying type-theory for higher-order (amortized) cost analysis 1:21
๐โPCF terms ๐ก ::= ๐ | ๐ (๐ก) | ๐ (๐ก) | ๐ ๐ ๐ง ๐ก ๐กโ๐๐ ๐ข ๐๐๐ ๐ ๐ฃ | _๐ฅ.๐ก | ๐ก๐ข | fix ๐ฅ .๐ก๐โPCF types ๐, ๐ ::= ๐๐๐ก [๐ผ , ๐ฝ ] | ๐ด โธ ๐
๐ด ::= [๐ < ๐ผ ]๐
Fig. 12. ๐โPCFโs syntax of terms and types from [Dal Lago and Gaboardi 2011]
ฮ;ฮ |= ๐ฝ โฅ 0 ฮ;ฮ |= ๐ผ โฅ 1 ฮ;ฮ โข ๐ [0/๐] <: ๐ฮ;ฮ; ฮ, ๐ฅ : [๐ < ๐ผ ]๐ โข๐ฝ ๐ฅ : ๐
varฮ;ฮ; ฮ, ๐ฅ : [๐ < ๐ผ ]๐1 โข๐ฝ ๐ : ๐2
ฮ;ฮ; ฮ โข๐ฝ _๐ฅ.๐ : ( [๐ < ๐ผ ] .๐1) โธ ๐2lam
ฮ;ฮ; ฮ โข๐ฝ ๐1 : ( [๐ < ๐ผ ]๐1) โธ ๐2 ฮ, ๐;ฮ, ๐ < ๐ผ ;ฮ โข๐พ ๐2 : ๐1ฮโฒ โ ฮ โ โ
๐<๐ผ ฮ ๐ป โฅ ๐ผ + ๐ฝ +โ๐<๐ผ ๐พ
ฮ;ฮ; ฮโฒ โข๐ป ๐1 ๐2 : ๐2app
Fig. 13. Selected typing rules of ๐โPCF from [Dal Lago and Gaboardi 2011]
Syntax. Terms and types of ๐โPCF are described in Fig. 12. ๐โPCF uses standard PCF terms butrefines the standard types of PCF for cost analysis. The type of natural numbers is refined withtwo indices, yielding ๐๐๐ก [๐ผ , ๐ฝ ], the type of natural numbers in the range [๐ผ , ๐ฝ ] both inclusive. Thefunction type has the form [๐ < ๐ผ ]๐1โธ๐2 or, more precisely, ( [๐ < ๐ผ ]๐1)โธ๐2. Here, [๐ < ๐ผ ]๐1 isjust ๐โPCFโs notation for the dependent sub-exponential !๐<๐ผ๐ that we introduced in Section 5.Hence, [๐ < ๐ผ ]๐1 โ ๐1 [0/๐] โ . . . โ ๐1 [(๐ผ โ 1)/๐] and the function type [๐ < ๐ผ ]๐1โธ๐2 is morallyequivalent to (๐1 [0/๐] โ . . . โ ๐1 [(๐ผ โ 1)/๐])โธ๐2. This way, the function type explicitly states thenumber of times ๐ผ the argument can be used by the function. This is important for cost analysis inthe type system (explained below). Note that, in ๐โPCF, the sub-exponential can only appear to theleft of an arrow, and in hypothesis.
Type system. The typing judgment of ๐โPCF is ฮ;ฮ; ฮ โข๐ถ ๐๐ : ๐ . Here, ฮ is a context of indexvariables (as in _-amor), ฮ is a set of assumed constraints on index variables (as in _-amor), ฮ is acontext of term variables, and ๐ถ is an upper bound on the cost of evaluation of ๐๐ . The importantparts here are ฮ and ๐ถ . ฮ is actually analogous to the non-affine context ฮฉ of _-amor: It containsvariables with multiplicities and dependencies. An entry in ฮฉ has the form ๐ฅ : [๐ < ๐ผ ]๐ , which isexactly the same as an entry ๐ฅ :๐<๐ผ ๐ in _-amor and means that ๐ผ copies of ๐ฅ are available withtypes ๐ [0/๐], . . . , ๐ [(๐ผ โ 1)/๐]. The cost ๐ถ comes from the language of index terms. Note that in๐โPCF, costs exist only on typing derivations; they are never internalized into types.
We show the important typing rules of ๐โPCF in Fig. 13. While reading these rules, the readershould keep ๐โPCFโs cost model in mind: A unit cost is counted for every use of a variable, whichcorresponds to a unit cost for every variable lookup during execution on a Krivine stack machinefor PCF [Krivine 2007]. Rule (var) allows the use of a hypothesis from ฮ. The variableโs multiplicity๐ผ must be at least 1. Perhaps surprisingly, the cost of this rule ๐ฝ can be 0, even though this rulecorresponds to the use of a variable. The reason is that the cost for all the uses of a variable iscounted together when the variable is bound (i.e., during function application), as we explain later.The rule for function abstraction (lam) is perhaps the most interesting and the defining rule of
๐โPCFโs cost tracking. Here, the cost ๐ฝ of _๐ฅ .๐ is taken to be the same as the cost of the body ๐ .This may sound overly conservative: _๐ฅ .๐ is a value and it evaluates with zero cost, so why countthe cost of the body ๐ in its cost? The reason is that there is no other place to put the cost of thebody. If this were an effect system (which it is not), the bodyโs cost would be internalized intothe functionโs type, and be counted when the function is applied. However, since this is a coeffectsystem, we donโt have this luxury, so the cost of the body is pre-counted when the abstraction iscreated. This is sound because the type system is affine: We cannot apply the abstraction morethan once, so the amount we pre-count here is certainly an upper-bound on the cost that couldarise from this functionโs use in the future. It is this unusual way of counting costs that makes
Proc. ACM Program. Lang., Vol. 1, No. CONF, Article 1. Publication date: January 2021.
1:22 Vineet Rajani, Marco Gaboardi, Deepak Garg, and Jan Hoffmann
๐โPCF substantially different from effect-based systems, and also why potentials are needed forembedding into _-amor (this is explained in Section 6.2).The (app) rule for function application is also interesting. Here, ๐1 is a term of type ( [๐ <
๐ผ ]๐1) โธ ๐2, so the argument ๐2 must be typed parametrically in ๐ for all ๐ < ๐ผ . The total cost ๐ปincludes the cost ๐ฝ of evaluating ๐1, the cost evaluating ๐2 ๐ผ -times (written
โ๐<๐ผ ๐พ ), and, importantly,
an additional cost ๐ผ for the up-to ๐ผ uses of the bound variable in the body of the function afterapplication. This is where the cost for variable use is actually counted. Note how information aboutthe multiplicity ๐ผ of a variable โ the coeffect โ contributes directly to the cost analysis. This justifieswhy ๐โPCF can be classified as a coeffect-based cost tracking system.
Operational semantics. ๐โPCF uses a Krivine machine for PCF, called ๐พ๐๐ถ๐น [Krivine 2007]. Statesof ๐พ๐๐ถ๐น are triples of the form (๐ก, ๐, \ ) where ๐ก is a ๐โPCF term, ๐ is an environment with variablebindings (it maps variables to terms) and \ is stack of closures. A closure (denoted by C) is simply apair consisting of a term and an environment. The left side of Fig. 14 lists some evaluation rules of๐พ๐๐ถ๐น from [Dal Lago and Gaboardi 2011]. For instance, the application triple (๐1 ๐2, ๐, \ ) reducesin one step to ๐1; the argument ๐2, along with the current environment, is pushed to the top ofthe stack for later evaluation. This is exactly how one would expect an evaluation to happen in acall-by-name scheme.
(๐1 ๐2, ๐, \ ) โ (๐1, ๐, (๐2, ๐) .\ )(_๐ฅ .๐, ๐, C.\ ) โ (๐1, C.๐, \ )(๐ฅ, (๐ก0, ๐0) . . . (๐ก๐, ๐๐), \ ) โ (๐ก๐ฅ , ๐๐ฅ , \ )(fix๐ฅ .๐, ๐, \ ) โ (๐, (๐ฅ, (fix๐ฅ .๐, C).๐, \ )
|๐ฅ | = 1|๐ | = 1|_๐ฅ.๐ | = |๐ | + 1|๐1 ๐2 | = |๐1 | + |๐2 | + 1|fix๐ฅ .๐ | = |๐ | + 1
Fig. 14. ๐พ๐๐ถ๐น reduction rules (left) and size function (right) from [Dal Lago and Gaboardi 2011]
Soundness. ๐โPCFโs cost analysis is sound for reductions on the Krivine machine, up to constantfactors. This is formalized in Theorem 10, which says that if the ๐โPCF type system providesa cost bound ๐ผ on term ๐ก (of type ๐๐๐ก ) and ๐ก reduces for ๐ steps on the Krivine machine, then๐ โค |๐ก | โ (๐ผ + 1), where |๐ก | is the size of the term ๐ก , defined on the right side of Fig. 14. The factor |๐ก |arises because ๐โPCF counts only variable uses, while the Krivine machine has other reductions aswell. However, one variable lookup is forced every |๐ก | reductions. (In the statement of the theorem,โ๐ is just shorthand for ๐-step Krivine reduction starting from the initial state (๐ก, ๐, ๐).)Theorem 10 (๐โPCFโs soundness from [Dal Lago and Gaboardi 2011]). โ๐ก, ๐ผ , ๐ฝ , ๐พ .
โข๐ผ ๐ก : ๐๐๐ก [๐ฝ , ๐พ] โง ๐ก โ๐ ๐ =โ ๐ โค |๐ก | โ (๐ผ + 1)
6.2 Type-directed translation of ๐โPCF into _-amorWe now describe our embedding of ๐โPCF in _-amor. The translation of types, denoted LโขM, isshown in the left side of Fig. 15. We abstract the type of naturals and treat them as a generalabstract base type b. In the translation, the annotation [๐ < ๐ผ ] simply changes to !๐<๐ผ as the twohave the same meaning. Further, following Moggiโs embedding of call-by-name in the computa-tional _-calculus [Moggi 1991], the basic skeleton of the translation of a function type ๐ดโธ๐ต is(M 0 L๐ดM)โธ(M 0 L๐ตM). The actual translation of the function type, shown in Fig. 15, additionallyrequires as argument a potential of ๐ผ units (type [๐ผ ] 1), where ๐ผ is the multiplicity of the argu-ment. This pays for the cost of using the argument up to ๐ผ times in the body of the functionand corresponds to the additional ๐ผ cost added to the total in the application rule (app) of ๐โPCF(Section 6.1).
The translation of ๐โPCF contexts, shown on the right side of Fig. 15, maps a ๐โPCF context to anon-affine context (ฮฉ) in _-amor. Again, a variable of type ๐ is mapped to variable of typeM 0L๐Mwith the same multiplicity.
Proc. ACM Program. Lang., Vol. 1, No. CONF, Article 1. Publication date: January 2021.
A unifying type-theory for higher-order (amortized) cost analysis 1:23
L๐M = ๐
L[๐ < ๐ผ ]๐1โธ๐2M = !๐<๐ผ M 0L๐1M โธ [๐ผ ] 1 โธ M 0 L๐2ML.M = .
Lฮ, ๐ฅ : [๐ < ๐ผ ]๐M = LฮM, ๐ฅ :๐<๐ผ M 0 L๐MFig. 15. Type and context translation for ๐โPCF
ฮ;ฮ |= ๐ฝ โฅ 0 ฮ;ฮ |= ๐ผ โฅ 1 ฮ;ฮ โข ๐ [0/๐] <: ๐ฮ;ฮ; ฮ, ๐ฅ : [๐ < ๐ผ ]๐ โข๐ฝ ๐ฅ : ๐ { _๐.releaseโ = ๐ in bindโ = โ1 in ๐ฅ
var
ฮ;ฮ; ฮ, ๐ฅ : [๐ < ๐ผ ]๐1 โข๐ฝ ๐ : ๐2 { ๐๐ก
ฮ;ฮ; ฮ โข๐ฝ _๐ฅ .๐ : ( [๐ < ๐ผ ] .๐1) โธ ๐2 {_๐1 . ret _๐ฆ._๐2 . let !๐ฅ = ๐ฆ in releaseโ = ๐1 in releaseโ = ๐2 in bind๐ = store() in ๐๐ก ๐
lam
ฮ;ฮ; ฮ โข๐ฝ ๐1 : ( [๐ < ๐ผ ]๐1) โธ ๐2 { ๐๐ก1 ฮ, ๐;ฮ, ๐ < ๐ผ ;ฮ โข๐พ ๐2 : ๐1 { ๐๐ก2ฮโฒ โ ฮ โ โ
๐<๐ผ ฮ ๐ป โฅ ๐ฝ + ๐ผ +โ๐<๐ผ ๐พ
ฮ;ฮ; ฮโฒ โข๐ป ๐1 ๐2 : ๐2 { _๐.๐ธ0app
๐ธ0 โ releaseโ = ๐ in ๐ธ1, ๐ธ1 โ bind๐ = store() in ๐ธ2๐ธ2 โ bind๐ = ๐๐ก1 ๐ in ๐ธ3, ๐ธ3 โ bind ๐ = store!() in ๐ธ4๐ธ4 โ bind๐ = store() in ๐ธ5, ๐ธ5 โ ๐ (๐๐๐๐๐๐ !๐๐ก2 ๐) ๐
๐๐๐๐๐๐ : !๐<๐ผ (๐1 โธ ๐2) โธ !๐<๐ผ๐1 โธ !๐<๐ผ๐2๐๐๐๐๐๐ ๐น ๐ โ let ! ๐ = ๐น in let !๐ฅ = ๐ in !(๐ ๐ฅ)
Fig. 16. Expression translation: ๐โPCF to _-amor
Term translation. To explain the translation of terms, we need an auxiliary function on ๐โPCFcontexts, which we write count (ฮ). This function simply adds the multiplicities of all variables in ฮ.
count (.) = 0 count (ฮ, ๐ฅ : [๐ < ๐ผ ]๐) = count (ฮ) + ๐ผThe translation of ๐โPCF terms is type-derivation directed. The translation judgment is of theform ฮ;ฮ; ฮ โข๐ผ ๐๐ : ๐ { ๐๐ where ฮ;ฮ; ฮ โข๐ผ ๐๐ : ๐ is a valid ๐โPCF typing judgment and ๐๐denotes the _-amor translation of the ๐โPCF expression ๐๐ . Importantly, the expected type of ๐๐ is[๐ผ + count (ฮ)] 1 โธ M 0 L๐M. This type means that ๐๐ yields something of typeM 0 L๐M (the expectedtype) after it has been provided (applied to) enough potential. Here, โenoughโ is ๐ผ + count (ฮ) โ๐ผ pays for the cost of evaluation of ๐๐ , as manifest in the given ๐โPCF typing judgment, whilecount (ฮ) pays for the cost of using each variable from the context its multiplicity number of times.With this basic structure of the translation in mind, we present the important term trans-
lation rules in Fig. 16. The translation of a variable ๐ฅ of type ๐ , rule (var), is a term of type[๐ฝ + count (ฮ) + ๐ผ ] 1 โธ M 0 L๐M. This term releases the potential ๐ฝ + count (ฮ) + ๐ผ it receives asargument, then incurs a unit cost (โ1), and then returns the variable ๐ฅ . The unit cost is neededto faithfully represent ๐โPCFโs cost model, which counts a unit cost for every use of a variable.Note that the net remaining cost is 0 because ๐ผ โฅ 1 (premise of the rule), so the released potential๐ฝ + count (ฮ) + ๐ผ is at least as much as the incurred unit cost.The rule for translating functions, rule (lam), handles ๐โPCFโs unusual way of counting costs
of functions. Here, the ๐โPCF context in the premise is ฮ, ๐ฅ : [๐ < ๐ผ ]๐1, so the type of ๐๐ก is[๐ฝ + count (ฮ) + ๐ผ ] 1 โธ M 0 L๐2M in a context that includes ๐ฅ : !๐<๐ผ M 0L๐1M. We wish to construct aterm of type [๐ฝ + count (ฮ)] 1 โธ M 0
(!๐<๐ผ M 0L๐1M โธ [๐ผ ] 1 โธ M 0 L๐2M
). Observe how the types of
๐๐ก and the term we want are very similar: They both have the same total potential ๐ฝ + count (ฮ) + ๐ผin negative positions. As a result, the required term can be constructed quite easily. The exactterm is shown in the conclusion of rule (lam). The rule for function application, rule (app), can beunderstood similarly.
This translation is type-preserving in the sense explained earlier (formalized next).
Proc. ACM Program. Lang., Vol. 1, No. CONF, Article 1. Publication date: January 2021.
1:24 Vineet Rajani, Marco Gaboardi, Deepak Garg, and Jan Hoffmann
Theorem 11 (Type preservation: ๐โPCF to _-amor). If ฮ;ฮ; ฮ โข๐ผ ๐ : ๐ in ๐โPCF then there exists ๐ โฒ
such that ฮ;ฮ; ฮ โข๐ผ ๐ : ๐ { ๐ โฒ and there is a derivation of โข;ฮ;ฮ; LฮM; โข โข ๐ โฒ : [๐ผ + count (ฮ)] 1 โธM 0 L๐M in _-amor.
Remark about the importance of potentials. The astute reader has probably noticed that, in thistranslation, we represent costs using potentials in negative positions, not monad indices (the monadindex is 0 everywhere in the translation!). Could we have used the monad index instead, and notneeded potentials at all? The answer is no: Trying to embed ๐โPCF using the monad alone breaksthe well-typedness of the translation of functions in the (lam) rule of Fig. 16. In the following, weexplain this further.
Suppose we were to do away with potentials. Then, in the term translation, a๐โPCF term ๐๐ typedas ฮ;ฮ; ฮ โข๐ผ ๐๐ : ๐ would no longer translate to a term of type [๐ผ + count (ฮ)] 1 โธ M 0 L๐M, butwould instead translate to a term of the โequivalentโ monadic typeM(๐ผ + count (ฮ)) L๐M. Further, the๐โPCF function type [๐ < ๐ผ ]๐1โธ๐2 would no longer translate to !๐<๐ผ M 0L๐1M โธ [๐ผ ] 1 โธ M 0 L๐2M,but would instead translate to !๐<๐ผ M 0L๐1M โธ M ๐ผ L๐2M.
Then, in the (lam) case, from the premise wewould have a term ๐๐ก of typeM(๐ฝ + count (ฮ) + ๐ผ ) L๐2Min a context that includes ๐ฅ : !๐<๐ผ M 0L๐1M. We would want to construct a term of typeM(๐ฝ + count (ฮ))
(!๐<๐ผ M 0L๐1M โธ M ๐ผ L๐2M
). However, such a term does not exist in _-amor or any
calculus with indexed/gradedmonads that we know of. To understand this, letโs abstract ๐ฝ +count (ฮ)to ๐พ , !๐<๐ผ M 0L๐1M to ๐ , and L๐2M to ๐ . Then, we are asking that the type (๐ โธ M(๐พ + ๐ผ ) ๐) โธM๐พ (๐ โธ M ๐ผ ๐) be inhabited. It should be easy to see that this type goes well beyond standardproperties of graded monads. Adding a primitive term of this type would be sound, but quitedissatisfying.
To summarize, it seems that potentials are somewhat fundamental to embedding a coeffect-basedcost analysis (as in ๐โPCF) in a monadic or effect-based system like _-amor. Whether and how thisgeneralizes to quantitative properties beyond cost remains an open question.
Semantic properties. We proved above that our translation is type preserving. In addition, weprove that it preserves reduction costs up to constant factors. This requires us to relate reductionof a ๐โPCF term ๐ in the Krivine machine to the reduction of its translation in _-amor. This istechnically tedious and, due to lack of space here, we cover it in full detail only in our technicalappendix. Briefly, we define a type-preserving โdecompilationโ of Krivine states to ๐โPCF terms. Bycomposing this with the translation of ๐โPCF terms to _-amor, we get a translation from Krivinestates to _-amor. We then show the following result about this translation (formalized as Lemma52 in the appendix): A Krivine reduction of ๐ steps can be simulated by its _-amor translation in๐ steps where ๐ โค (๐ + 1) |๐ก | and |๐ก | is the size of the initial Krivine state. This result immediatelyallows us to re-derive ๐โPCFโs soundness theorem from that of _-amor.
Completeness. Finally, we note that Dal Lago and Gaboardi [2011] proved that ๐โPCF is relativelycomplete for PCF programs: Every simply typed, closed PCF program of type ๐๐๐ก that reduces in ๐steps to a value can be typed with cost ๐ in ๐โPCF. Since our translation from ๐โPCF to _-amortrivially preserves cost annotations for closed terms, this means that _-amor can also type (thetranslation of) a PCF term that reduces in ๐ steps with cost ๐ . Hence, _-amor is also relativelycomplete for PCF.
7 RELATEDWORKThe literature on cost analysis is very vast; we summarize and compare to only a representativesubset of the literature, covering several prominent styles of cost analysis.
Proc. ACM Program. Lang., Vol. 1, No. CONF, Article 1. Publication date: January 2021.
A unifying type-theory for higher-order (amortized) cost analysis 1:25
Type and effect systems. Several type and effect system have been proposed for amortized analysisusing the method of potentials. Early approaches [Hofmann and Jost 2003; Jost et al. 2009] allowthe potential associated with a value to only be a linear function of the valueโs size. UnivariateRAML [Hoffmann and Hofmann 2010] generalizes this to polynomial potentials. MultivariateRAML [Hoffmann et al. 2011] is a further generalizationwhere a single potential, that is a polynomialof the sizes of several input variables, can be associated to all of them together. These approacheswork with an assumption that functions are freely duplicable and not under the purview ofaffineness. As a result, potential captured in partially applied functions renders these approachesunsound. This is prevented by simply disallowing closures with captured potential in the typesystem. In contrast, _-amor can effortlessly handle closures that capture potential since the typesystem is fully affine. We already showed how to embed Univariate RAML in _-amor in Section 4.We believe that the embedding can be extended to Multivariate RAML with some effort.
Later work tries to address some of the limitations of RAML. For instance, [Jost et al. 2010]extends RAML with limited support for closures and higher-order functions. In particular, [Jost et al.2010] can handle Curried functions only when the potential is associated with the last argument.More recently, the type system of Knoth et al. [2019] works by capturing the number of times apartially applied function can be used, thereby supporting closures which have enough potentialfor the allowed number of copies. All of these approaches are somewhat ad hoc in nature and donot fix the root cause in the type theory, which is the lack of affineness. In contrast, _-amor, beingfully affine, does not have such limitations.
Some prior work such as the unary fragment of รiรงek et al. [2017] uses effect-based type systemsfor non-amortized cost analysis. Handley et al. [2020] show how to perform (non-amortized) costanalysis using refinement types of Liquid Haskell. A significant line of work tracing lineage back toat least Crary and Weirich [2000] uses sized types and cost represented in a writer monad for costanalysis. More recently, Danner et al. [2015] show how to extend this idea to extract sound costrecurrences from programs. These recurrences can be solved to establish cost bounds. However,none of this work supports potentials or amortized analysis. Conceptually, it is simpler than thepreviously-mentioned work on amortized cost analysis (it corresponds to RAML functions wherethe input and output potentials are both 0). We believe that all systems mentioned in this paragraphso far can be embedded in _-amor. Kavvos et al. [2020] extend the work of Danner et al. [2015] tothe call-by-push-value (CBPV) setting. While we have not considered CBPV here, we believe thatchanging _-amorโs semantics from monadic to CBPV is feasible and not particularly difficult.Cost analysis using program logics. As an alternative to type systems, a growing line of work
uses variants of Hoare logic for amortized cost analysis [Carbonneaux et al. 2015; Charguรฉraudand Pottier 2019; Mรฉvel et al. 2019]. The common idea is to represent the potential before andafter the execution of a code segment as ghost state in the pre- and post-condition of the segment,respectively. Conceptually, this idea is not very different from how we encode potentials usingour [๐] ๐ construct in the inputs and outputs of functions (e.g., in embedding RAML in Section 4).However, unlike _-amor, prior work on program logics for cost analysis shows neither embeddingsof existing frameworks, nor any (relative) completeness result. Haslbeck and Nipkow [2018] presentcompleteness results for some Hoare logics but only in the context of a simple imperative language.The real challenge for completeness is handling of higher-order functions with arbitrary recursion,which _-amor covers. [Mรฉvel et al. 2019] introduce a new concept called time receipts, which areuseful for lower-bound analysis, something that _-amor does not support yet, but that we areextending _-amor to.
Cost analysis of lazy programs. Some prior work [Danielsson 2008; Jost et al. 2017; Madhavan et al.2017] develops methods for cost analysis of lazy programs (call-by-need evaluation). It turns out thataffineness is not required for amortized cost analysis of lazy programs, as an expression is evaluated
Proc. ACM Program. Lang., Vol. 1, No. CONF, Article 1. Publication date: January 2021.
1:26 Vineet Rajani, Marco Gaboardi, Deepak Garg, and Jan Hoffmann
only once due to memoization even if it is used several times. As mentioned in Section 1, thesemantics of call-by-need is fundamentally incompatible with the semantics of _-amor, so we didnot consider call-by-need in this paper. However, at the level of types, there is considerable similaritybetween Danielsson [2008] and our work. For example, Danielsson [2008] uses an indexed monad totrack costs as we do. Interestingly, he does not have a separate type-theoretic construct to associatepotential. Instead, he introduces a primitive coercion โpayโ of typeM(^1 + ^2) ๐ โธ M^1 (M^2 ๐),which, in a way, represents paying ^1 part of the cost ^1 + ^2 using potential from the outside.An interesting question is whether, assuming a coercion of this type, we could do away withpotentials altogether and still embed ๐โPCF. It seems that the answer is no. As we noted at the endof Section 6.2, the axiom about indexed monads we really need in order to embed ๐โPCF withoutpotentials is (๐ โธ M(^1 + ^2) ๐) โธ M^1 (๐ โธ M^2 ๐), and this is stronger than โpayโ.Coeffect-based cost analysis. ๐โPCF [Dal Lago and Gaboardi 2011] and ๐โPCFv [Dal Lago and
Petit 2012] are coeffect-based type systems for non-amortized cost analysis of PCF programs in thecall-by-name and call-by-value settings, respectively. Both systems count the number of variablelookups during execution on an abstract machine (the Krivine machine for call-by-name and theCEK machine for call-by-value [Felleisen and Friedman 1987; Krivine 2007]). This is easily doneby tracking (as a coeffect) the number of uses of each variable in an affine type system with adependent sub-exponential borrowed from Bounded Linear Logic (BLL) [Girard et al. 1992] (_-amoralso borrows the same dependent sub-exponential, but does not use coeffects for tracking cost). Acommon limitation of ๐โPCF and ๐โPCFv is that they cannot internalize the cost of a program intoits type; instead the cost is a function of the typing derivation. We showed in Section 6 that _-amorcan embed ๐โPCF and internalize its costs into types. Hence, _-amor advances beyond ๐โPCF. Weexpect that _-amor can also embed ๐โPCFv, but have not tried this embedding yet.Atkey [2018] presents Quantitative Type Theory (QTT), which is a dependent type theory
with coeffects. QTT and _-amor are very different in their goals. QTT is focused on studyingthe interaction between dependent types and coeffects. On the other hand, _-amor is based oneffects; we only show how to simulate coeffect-based cost analysis. Technically, QTT only considersnon-dependent coeffects, as in ๐ฅ :๐ ๐ (๐ copies of the same ๐) while _-amor includes coeffects withparameterized linear dependencies from the dependent sub-exponential, as in ๐ฅ :๐<๐ ๐ (๐ differentinstances of ๐).
8 CONCLUSIONWe developed _-amor with the broader goal of unifying existing type systems for cost analysis._-amor introduces a new modal type constructor to represent potential at the level of types anduses affine types with a dependent sub-exponential. Through two embeddings, we have shownthat _-amor can simulate cost analysis for different evaluation strategies (call-by-name and call-by-value), in different styles (effect-based and coeffect-based), and with or without amortization.
Acknowledgments. We thank our anonymous reviewers and our shepherd David Van Horn fortheir feedback. Vineet Rajani conducted most of this research while he was a graduate studentat the Max Planck Institute for Software Systems and Saarland University. Marco Gaboardi wassupported by the National Science Foundation under awards 1718220 and 1845803. Jan Hoffmannwas supported by DARPA under AA Contract FA8750-18-C-0092 and by the National ScienceFoundation under SaTC Award 1801369, CAREER Award 1845514, and SHF Awards 1812876 and2007784. Any opinions, findings, and conclusions contained in this document are those of theauthors and do not necessarily reflect the views of the sponsoring organizations.
Proc. ACM Program. Lang., Vol. 1, No. CONF, Article 1. Publication date: January 2021.
A unifying type-theory for higher-order (amortized) cost analysis 1:27
REFERENCESAmal Jamil Ahmed. 2004. Semantics of types for mutable state. Ph.D. Dissertation. Princeton university.Robert Atkey. 2018. Syntax and Semantics of Quantitative Type Theory. In Annual ACM/IEEE Symposium on Logic in
Computer Science.Martin Avanzini and Ugo Dal Lago. 2017. Automating Sized-type Inference for Complexity Analysis. Proc. ACM Program.
Lang. 1, ICFP (2017).Aloรฏs Brunel, Marco Gaboardi, Damiano Mazza, and Steve Zdancewic. 2014. A Core Quantitative Coeffect Calculus. In
Proceedings of the European Symposium on Programming Languages and Systems.Quentin Carbonneaux, Jan Hoffmann, and Zhong Shao. 2015. Compositional certified resource bounds. In Proceedings of the
ACM SIGPLAN Conference on Programming Language Design and Implementation.Arthur Charguรฉraud and Franรงois Pottier. 2019. Verifying the Correctness and Amortized Complexity of a Union-Find
Implementation in Separation Logic with Time Credits. J. Autom. Reasoning 62, 3 (2019).Ezgi รiรงek, Gilles Barthe, Marco Gaboardi, Deepak Garg, and Jan Hoffmann. 2017. Relational cost analysis. In Proceedings of
the ACM SIGPLAN-SIGACT Symposium on Principles of Programming Languages.Thomas H. Cormen, Charles E. Leiserson, Ronald L. Rivest, and Clifford Stein. 2009. Introduction to Algorithms, 3rd Edition.
MIT Press.Karl Crary and Stephanie Weirich. 2000. Resource Bound Certification. In Proceedings of the ACM SIGPLAN-SIGACT
Symposium on Principles of Programming Languages.Ugo Dal Lago and Marco Gaboardi. 2011. Linear Dependent Types and Relative Completeness. Logical Methods in Computer
Science 8, 4 (2011).Ugo Dal Lago and Barbara Petit. 2012. Linear Dependent Types in a Call-by-value Scenario. Science of Computer Programming
84 (2012).Nils Anders Danielsson. 2008. Lightweight Semiformal Time Complexity Analysis for Purely Functional Data Structures. In
Proceedings of the ACM SIGPLAN-SIGACT Symposium on Principles of Programming Languages.Norman Danner, Daniel R. Licata, and Ramyaa. 2015. Denotational cost semantics for functional languages with inductive
types. In Proceedings of the ACM SIGPLAN International Conference on Functional Programming. 140โ151.Matthias Felleisen and Daniel P. Friedman. 1987. Control operators, the SECD-machine, and the _-calculus. In Proceedings
of the IFIP Working Conference on Formal Description of Programming Concepts.Marco Gaboardi, Shin-ya Katsumata, Dominic Orchard, Flavien Breuvart, and Tarmo Uustalu. 2016. Combining Effects and
Coeffects via Grading. In Proceedings of the ACM SIGPLAN International Conference on Functional Programming.Jean-Yves Girard, Andre Scedrov, and Philip J. Scott. 1992. Bounded linear logic: a modular approach to polynomial-time
computability. Theoretical Computer Science 97, 1 (1992).Martin A. T. Handley, Niki Vazou, and Graham Hutton. 2020. Liquidate your assets: reasoning about resource usage in
liquid Haskell. Proc. ACM Program. Lang. 4, POPL (2020).Maximilian P. L. Haslbeck and Tobias Nipkow. 2018. Hoare Logics for Time Bounds - A Study in Meta Theory. In Tools and
Algorithms for the Construction and Analysis of Systems, Vol. 10805.Jan Hoffman. 2011. Types with Potential: Polynomial Resource Bounds via Automatic Amortized Analysis. Ph.D. Dissertation.
Ludwig-Maximilians-Universitรคt Mรผnchen.Jan Hoffmann, Klaus Aehlig, and Martin Hofmann. 2011. Multivariate Amortized Resource Analysis. In Proceedings of the
ACM SIGPLAN-SIGACT Symposium on Principles of Programming Languages.Jan Hoffmann, Ankush Das, and Shu-Chun Weng. 2017. Towards Automatic Resource Bound Analysis for OCaml. In
Proceedings of the ACM SIGPLAN Symposium on Principles of Programming Languages.Jan Hoffmann and Martin Hofmann. 2010. Amortized Resource Analysis with Polynomial Potential: A Static Inference of
Polynomial Bounds for Functional Programs. In Proceedings of the European Conference on Programming Languages andSystems.
Martin Hofmann and Steffen Jost. 2003. Static Prediction of Heap Space Usage for First-order Functional Programs. InProceedings of the ACM SIGPLAN-SIGACT Symposium on Principles of Programming Languages.
Steffen Jost, Kevin Hammond, Hans-Wolfgang Loidl, and Martin Hofmann. 2010. Static Determination of QuantitativeResource Usage for Higher-order Programs. In Proceedings of the ACM SIGPLAN-SIGACT Symposium on Principles ofProgramming Languages.
Steffen Jost, Hans-Wolfgang Loidl, Kevin Hammond, Norman Scaife, and Martin Hofmann. 2009. "Carbon Credits" forResource-Bounded Computations Using Amortised Analysis. In Proceedings of Formal Methods.
Steffen Jost, Pedro Vasconcelos, Mรกrio Florido, and Kevin Hammond. 2017. Type-Based Cost Analysis for Lazy FunctionalLanguages. J. Autom. Reason. 59, 1 (2017).
G. A. Kavvos, Edward Morehouse, Daniel R. Licata, and Norman Danner. 2020. Recurrence extraction for functionalprograms through call-by-push-value. Proc. ACM Program. Lang. 4, POPL (2020).
Proc. ACM Program. Lang., Vol. 1, No. CONF, Article 1. Publication date: January 2021.
1:28 Vineet Rajani, Marco Gaboardi, Deepak Garg, and Jan Hoffmann
Tristan Knoth, Di Wang, Nadia Polikarpova, and Jan Hoffmann. 2019. Resource-guided program synthesis. In Proceedings ofthe ACM SIGPLAN Conference on Programming Language Design and Implementation.
Jean-Louis Krivine. 2007. A Call-by-name Lambda-calculus Machine. Higher Order Symbolic Computation 20, 3 (2007).Ravichandhran Madhavan, Sumith Kulal, and Viktor Kuncak. 2017. Contract-based Resource Verification for Higher-order
Functions with Memoization. In Proceedings of the ACM SIGPLAN Symposium on Principles of Programming Languages.Dylan McDermott and Alan Mycroft. 2018. Call-by-need effects via coeffects. Open Comput. Sci. 8, 1 (2018).Glen Mรฉvel, Jacques-Henri Jourdan, and Franรงois Pottier. 2019. Time credits and time receipts in Iris. In European Symposium
on Programming.Eugenio Moggi. 1991. Notions of Computation and Monads. Information and Computation 93, 1 (1991).Georg Neis, Derek Dreyer, and Andreas Rossberg. 2011. Non-parametric parametricity. J. Funct. Program. 21, 4-5 (2011).Chris Okasaki. 1996. Purely Functional Data Structures. Ph.D. Dissertation. Carnegie Mellon University.Tomas Petricek, Dominic Orchard, and Alan Mycroft. 2014. Coeffects: A Calculus of Context-dependent Computation. In
Proceedings of the ACM SIGPLAN International Conference on Functional Programming.Tomas Petricek, Dominic A. Orchard, and Alan Mycroft. 2013. Coeffects: Unified Static Analysis of Context-Dependence. In
Automata, Languages, and Programming - International Colloquium.David J. Pym, Peter W. OโHearn, and Hongseok Yang. 2004. Possible worlds and resources: the semantics of BI. Theoretical
Computer Science 315, 1 (2004).Robert E. Tarjan. 1985. Amortized computational complexity. SIAM J. Algebraic Discrete Methods 6, 2 (1985).Hongwei Xi. 2007. Dependent ML An approach to practical programming with dependent types. J. Funct. Program. 17, 2
(2007).
Proc. ACM Program. Lang., Vol. 1, No. CONF, Article 1. Publication date: January 2021.