a unifying type-theory for higher-order (amortized) cost

28
1 A unifying type-theory for higher-order (amortized) cost analysis VINEET RAJANI โˆ— , Max Planck Institute for Security and Privacy, Germany MARCO GABOARDI, Boston University, USA DEEPAK GARG, Max Planck Institute for Software Systems, Germany JAN HOFFMANN, Carnegie Mellon University, USA This paper presents -amor, a new type-theoretic framework for amortized cost analysis of higher-order functional programs and shows that existing type systems for cost analysis can be embedded in it. -amor introduces a new modal type for representing potentials โ€“ costs that have been accounted for, but not yet incurred, which are central to amortized analysis. Additionally, -amor relies on standard type-theoretic concepts like a๏ฌƒneness, re๏ฌnement types and an indexed cost monad. -amor is proved sound using a rather simple logical relation. We embed two existing type systems for cost analysis in -amor showing that, despite its simplicity, -amor can simulate cost analysis for di๏ฌ€erent evaluation strategies (call-by-name and call- by-value), in di๏ฌ€erent styles (e๏ฌ€ect-based and coe๏ฌ€ect-based), and with or without amortization. One of the embeddings 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 Ho๏ฌ€mann. 2021. A unifying type-theory for higher-order (amortized) cost analysis. Proc. ACM Program. Lang. 1, CONF, Article 1 (January 2021), 28 pages. 1 INTRODUCTION Cost analysis is the static veri๏ฌcation 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 calls or the number of case analyses during the execution of a program. 1 Cost analysis is a well-studied topic. Of interest to us in this paper are type systems for cost analysis, many of which exist [Avanzini and Dal Lago 2017; ร‡iรงek et al. 2017; Crary and Weirich 2000; Dal Lago and Gaboardi 2011; Dal Lago and Petit 2012; Danner et al. 2015; Girard et al. 1992; Handley et al. 2020; Ho๏ฌ€man 2011; Ho๏ฌ€mann et al. 2011, 2017; Ho๏ฌ€mann 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 di๏ฌ€erent evaluation strategies like call-by-value and call-by-name, which result in very di๏ฌ€erent cost models. Second, they vary in how the type system tracks costs. Most commonly and perhaps most intuitively, cost is tracked as an e๏ฌ€ect 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 was mostly done while he was a graduate student at the Max Planck Institute for Software Systems and Saarland University. 1 Cost 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]; Marco Gaboardi, Boston University, USA, [email protected]; Deepak Garg, Max Planck Institute for Software Systems, Saarland Informatics Campus, Germany, [email protected]; Jan Ho๏ฌ€mann, Carnegie Mellon University, USA, jho๏ฌ€[email protected]. Permission to make digital or hard copies of part or all of this work for personal or classroom use is granted without fee provided that copies are not made or distributed for pro๏ฌt or commercial advantage and that copies bear this notice and the full citation on the ๏ฌrst 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-ART1 https://doi.org/ Proc. ACM Program. Lang., Vol. 1, No. CONF, Article 1. Publication date: January 2021.

Upload: others

Post on 08-Feb-2022

1 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: A unifying type-theory for higher-order (amortized) cost

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.

Page 2: A unifying type-theory for higher-order (amortized) cost

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.

Page 3: A unifying type-theory for higher-order (amortized) cost

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.

Page 4: A unifying type-theory for higher-order (amortized) cost

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.

Page 5: A unifying type-theory for higher-order (amortized) cost

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.

Page 6: A unifying type-theory for higher-order (amortized) cost

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.

Page 7: A unifying type-theory for higher-order (amortized) cost

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.

Page 8: A unifying type-theory for higher-order (amortized) cost

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.

Page 9: A unifying type-theory for higher-order (amortized) cost

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.

Page 10: A unifying type-theory for higher-order (amortized) cost

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.

Page 11: A unifying type-theory for higher-order (amortized) cost

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.

Page 12: A unifying type-theory for higher-order (amortized) cost

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.

Page 13: A unifying type-theory for higher-order (amortized) cost

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.

Page 14: A unifying type-theory for higher-order (amortized) cost

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.

Page 15: A unifying type-theory for higher-order (amortized) cost

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.

Page 16: A unifying type-theory for higher-order (amortized) cost

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.

Page 17: A unifying type-theory for higher-order (amortized) cost

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.

Page 18: A unifying type-theory for higher-order (amortized) cost

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.

Page 19: A unifying type-theory for higher-order (amortized) cost

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.

Page 20: A unifying type-theory for higher-order (amortized) cost

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.

Page 21: A unifying type-theory for higher-order (amortized) cost

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.

Page 22: A unifying type-theory for higher-order (amortized) cost

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.

Page 23: A unifying type-theory for higher-order (amortized) cost

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.

Page 24: A unifying type-theory for higher-order (amortized) cost

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.

Page 25: A unifying type-theory for higher-order (amortized) cost

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.

Page 26: A unifying type-theory for higher-order (amortized) cost

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.

Page 27: A unifying type-theory for higher-order (amortized) cost

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.

Page 28: A unifying type-theory for higher-order (amortized) cost

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.