central moment analysis for cost accumulators in

33
Central Moment Analysis for Cost Accumulators in Probabilistic Programs Technical Report Di Wang Carnegie Mellon University USA Jan HoïŹ€mann Carnegie Mellon University USA Thomas Reps University of Wisconsin USA Abstract For probabilistic programs, it is usually not possible to au- tomatically derive exact information about their properties, such as the distribution of states at a given program point. Instead, one can attempt to derive approximations, such as upper bounds on tail probabilities. Such bounds can be obtained via concentration inequalities, which rely on the moments of a distribution, such as the expectation (the ïŹrst raw moment) or the variance (the second central moment). Tail bounds obtained using central moments are often tighter than the ones obtained using raw moments, but automati- cally analyzing central moments is more challenging. This paper presents an analysis for probabilistic programs that automatically derives symbolic upper and lower bounds on variances, as well as higher central moments, of cost ac- cumulators. To overcome the challenges of higher-moment analysis, it generalizes analyses for expectations with an algebraic abstraction that simultaneously analyzes diïŹ€erent moments, utilizing relations between them. A key innova- tion is the notion of moment-polymorphic recursion, and a practical derivation system that handles recursive functions. The analysis has been implemented using a template- based technique that reduces the inference of polynomial bounds to linear programming. Experiments with our pro- totype central-moment analyzer show that, despite the ana- lyzer’s upper/lower bounds on various quantities, it obtains tighter tail bounds than an existing system that uses only raw moments, such as expectations. Keywords: Probabilistic programs, central moments, cost analysis, tail bounds 1 Introduction Probabilistic programs [18, 25, 30] can be used to manipulate uncertain quantities modeled by probability distributions and random control ïŹ‚ows. Uncertainty arises naturally in Bayesian networks, which capture statistical dependencies (e.g., for diagnosis of diseases [23]), and cyber-physical sys- tems, which are subject to sensor errors and peripheral dis- turbances (e.g., airborne collision-avoidance systems [29]). PLDI ’21, June 20–25, 2021, Virtual, UK 2021. ACM ISBN 978-1-4503-8391-2/21/06. . . $15.00 hps://doi.org/10.1145/3453483.3454062 Researchers have used probabilistic programs to implement and analyze randomized algorithms [2], cryptographic proto- cols [3], and machine-learning algorithms [16]. A probabilis- tic program propagates uncertainty through a computation, and produces a distribution over results. In general, it is not tractable to compute the result distribu- tions of probabilistic programs automatically and precisely: Composing simple distributions can quickly complicate the result distribution, and randomness in the control ïŹ‚ow can easily lead to state-space explosion. Monte-Carlo simula- tion [36] is a common approach to study the result distribu- tions, but the technique does not provide formal guarantees, and can sometimes be ineïŹƒcient [5]. In this paper, we focus on a speciïŹc yet important kind of uncertain quantity: cost accumulators, which are pro- gram variables that can only be incremented or decremented through the program execution. Examples of cost accumu- lators include termination time [5, 11, 12, 24, 33], rewards in Markov decision processes (MDPs) [35], position infor- mation in control systems [4, 8, 37], and cash ïŹ‚ow during bitcoin mining [43]. Recent work [8, 26, 31, 43] has proposed successful static-analysis approaches that leverage aggregate information of a cost accumulator , such as ’s expected value [ ] (i.e., ’s â€œïŹrst moment”). The intuition why it is beneïŹcial to compute aggregate information—in lieu of distributions—is that aggregate measures like expectations abstract distributions to a single number, while still indi- cating non-trivial properties. Moreover, expectations are transformed by statements in a probabilistic program in a manner similar to the weakest-precondition transformation of formulas in a non-probabilistic program [30]. One important kind of aggregate information is moments. In this paper, we show how to obtain central moments (i.e., [( − [ ]) ] for any ≄ 2), whereas most previous work focused on raw moments (i.e., [ ] for any ≄ 1). Central moments can provide more information about distributions. For example, the variance [ ] (i.e., [( − [ ]) 2 ] , ’s “second central moment”) indicates how can deviate from its mean, the skewness (i.e., [( − [ ]) 3 ] ( [ ]) 3 / 2 , ’s “third standard- ized moment”) indicates how lopsided the distribution of is, and the kurtosis (i.e., [( − [ ]) 4 ] ( [ ]) 2 , ’s “fourth standard- ized moment”) measures the heaviness of the tails of the distribution of . One application of moments is to answer 1 arXiv:2001.10150v3 [cs.PL] 8 Apr 2021

Upload: others

Post on 04-Jan-2022

1 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Central Moment Analysis for Cost Accumulators in

Central Moment Analysis for Cost Accumulators inProbabilistic Programs

Technical Report

Di WangCarnegie Mellon University

USA

Jan HoffmannCarnegie Mellon University

USA

Thomas RepsUniversity of Wisconsin

USA

AbstractFor probabilistic programs, it is usually not possible to au-tomatically derive exact information about their properties,such as the distribution of states at a given program point.Instead, one can attempt to derive approximations, suchas upper bounds on tail probabilities. Such bounds can beobtained via concentration inequalities, which rely on themoments of a distribution, such as the expectation (the firstraw moment) or the variance (the second central moment).Tail bounds obtained using central moments are often tighterthan the ones obtained using raw moments, but automati-cally analyzing central moments is more challenging.

This paper presents an analysis for probabilistic programsthat automatically derives symbolic upper and lower boundson variances, as well as higher central moments, of cost ac-cumulators. To overcome the challenges of higher-momentanalysis, it generalizes analyses for expectations with analgebraic abstraction that simultaneously analyzes differentmoments, utilizing relations between them. A key innova-tion is the notion of moment-polymorphic recursion, and apractical derivation system that handles recursive functions.The analysis has been implemented using a template-

based technique that reduces the inference of polynomialbounds to linear programming. Experiments with our pro-totype central-moment analyzer show that, despite the ana-lyzer’s upper/lower bounds on various quantities, it obtainstighter tail bounds than an existing system that uses onlyraw moments, such as expectations.

Keywords: Probabilistic programs, central moments, costanalysis, tail bounds

1 IntroductionProbabilistic programs [18, 25, 30] can be used to manipulateuncertain quantities modeled by probability distributionsand random control flows. Uncertainty arises naturally inBayesian networks, which capture statistical dependencies(e.g., for diagnosis of diseases [23]), and cyber-physical sys-tems, which are subject to sensor errors and peripheral dis-turbances (e.g., airborne collision-avoidance systems [29]).

PLDI ’21, June 20–25, 2021, Virtual, UK2021. ACM ISBN 978-1-4503-8391-2/21/06. . . $15.00https://doi.org/10.1145/3453483.3454062

Researchers have used probabilistic programs to implementand analyze randomized algorithms [2], cryptographic proto-cols [3], and machine-learning algorithms [16]. A probabilis-tic program propagates uncertainty through a computation,and produces a distribution over results.

In general, it is not tractable to compute the result distribu-tions of probabilistic programs automatically and precisely:Composing simple distributions can quickly complicate theresult distribution, and randomness in the control flow caneasily lead to state-space explosion. Monte-Carlo simula-tion [36] is a common approach to study the result distribu-tions, but the technique does not provide formal guarantees,and can sometimes be inefficient [5].In this paper, we focus on a specific yet important kind

of uncertain quantity: cost accumulators, which are pro-gram variables that can only be incremented or decrementedthrough the program execution. Examples of cost accumu-lators include termination time [5, 11, 12, 24, 33], rewardsin Markov decision processes (MDPs) [35], position infor-mation in control systems [4, 8, 37], and cash flow duringbitcoin mining [43]. Recent work [8, 26, 31, 43] has proposedsuccessful static-analysis approaches that leverage aggregateinformation of a cost accumulator 𝑋 , such as 𝑋 ’s expectedvalue đ”Œ[𝑋 ] (i.e., 𝑋 ’s “first moment”). The intuition why itis beneficial to compute aggregate information—in lieu ofdistributions—is that aggregate measures like expectationsabstract distributions to a single number, while still indi-cating non-trivial properties. Moreover, expectations aretransformed by statements in a probabilistic program in amanner similar to the weakest-precondition transformationof formulas in a non-probabilistic program [30].

One important kind of aggregate information is moments.In this paper, we show how to obtain central moments (i.e.,đ”Œ[(𝑋 âˆ’đ”Œ[𝑋 ])𝑘 ] for any 𝑘 ≄ 2), whereas most previous workfocused on raw moments (i.e., đ”Œ[𝑋𝑘 ] for any 𝑘 ≄ 1). Centralmoments can provide more information about distributions.For example, the variance 𝕍 [𝑋 ] (i.e., đ”Œ[(𝑋 − đ”Œ[𝑋 ])2], 𝑋 ’s“second central moment”) indicates how 𝑋 can deviate fromits mean, the skewness (i.e., đ”Œ[ (đ‘‹âˆ’đ”Œ[𝑋 ])3 ]

(𝕍 [𝑋 ]) 3/2 , 𝑋 ’s “third standard-ized moment”) indicates how lopsided the distribution of 𝑋is, and the kurtosis (i.e., đ”Œ[ (đ‘‹âˆ’đ”Œ[𝑋 ])4 ]

(𝕍 [𝑋 ])2 , 𝑋 ’s “fourth standard-ized moment”) measures the heaviness of the tails of thedistribution of 𝑋 . One application of moments is to answer

1

arX

iv:2

001.

1015

0v3

[cs

.PL

] 8

Apr

202

1

Page 2: Central Moment Analysis for Cost Accumulators in

PLDI ’21, June 20–25, 2021, Virtual, UK Di Wang, Jan Hoffmann, and Thomas Reps

queries about tail bounds, e.g., the assertions about proba-bilities of the form ℙ[𝑋 ≄ 𝑑], via concentration-of-measureinequalities from probability theory [13]. With central mo-ments, we find an opportunity to obtain more precise tailbounds of the form ℙ[𝑋 ≄ 𝑑], and become able to derivebounds on tail probabilities of the form ℙ[|𝑋 − đ”Œ[𝑋 ] | ≄ 𝑑].

Central moments đ”Œ[(𝑋 − đ”Œ[𝑋 ])𝑘 ] can be seen as polyno-mials of raw moments đ”Œ[𝑋 ], · · · ,đ”Œ[𝑋𝑘 ], e.g., the variance𝕍 [𝑋 ] = đ”Œ[(𝑋 âˆ’đ”Œ[𝑋 ])2] can be rewritten as đ”Œ[𝑋 2] âˆ’đ”Œ2 [𝑋 ],where đ”Œđ‘˜ [𝑋 ] denotes (đ”Œ[𝑋 ])𝑘 . To derive bounds on centralmoments, we need both upper and lower bounds on the rawmoments, because of the presence of subtraction. For exam-ple, to upper-bound 𝕍 [𝑋 ], a static analyzer needs to have anupper bound on đ”Œ[𝑋 2] and a lower bound on đ”Œ2 [𝑋 ].In this work, we present and implement the first fully

automatic analysis for deriving symbolic interval boundson higher central moments for cost accumulators in prob-abilistic programs with general recursion and continuousdistributions. One challenge is to support interproceduralreasoning to reuse analysis results for functions. Our solutionmakes use of a “lifting” technique from the natural-language-processing community. That technique derives an algebrafor second moments from an algebra for first moments [27].We generalize the technique to develop moment semirings,and use them to derive a novel frame rule to handle functioncalls with moment-polymorphic recursion (see §2.2).Recent work has successfully automated inference of up-

per [31] or lower bounds [43] on the expected cost of prob-abilistic programs. Kura et al. [26] developed a system toderive upper bounds on higher rawmoments of program run-times. However, even in combination, existing approachescannot solve tasks such as deriving a lower bound on thesecond rawmoment of runtimes, or deriving an upper boundon the variance of accumulators that count live heap cells.Fig. 1(a) summarizes the features of related work on mo-ment inference for probabilistic programs. To the best of ourknowledge, our work is the first moment-analysis tool thatsupports all of the listed programming and analysis features.Fig. 1(b) and (c) compare our work with related work interms of tail-bound analysis on a concrete program (see §5).The bounds are derived for the cost accumulator tick in arandom-walk program that we will present in §2. It can beobserved that for 𝑑 ≄ 20, the most precise tail bound fortick is the one obtained via an upper bound on the variance𝕍 [tick] (tick’s second central moment).

Our work incorporates ideas known from the literature:- Using the expected-potential method (or ranking super-martingales) to derive upper bounds on the expected pro-gram runtimes or monotone costs [10–12, 14, 26, 31].

- Using the Optional Stopping Theorem from probabilitytheory to ensure the soundness of lower-bound inferencefor probabilistic programs [1, 19, 38, 43].

feature [8] [31] [26] [43] this workloop ✓ ✓ ✓ ✓

recursion ✓ ✓continuous distributions ✓ ✓ ✓ ✓non-monotone costs ✓ ✓ ✓higher moments ✓ ✓ ✓interval bounds ✓ ✓ ✓

(a)

[31, 43] [26] this workDerived

đ”Œ[tick] ≀ 2𝑑 + 4 đ”Œ[tick2] ≀ 𝕍 [tick] ≀bound 4𝑑2 + 22𝑑 + 28 22𝑑 + 28

Moment type raw raw centralConcentration Markov Markov Cantelliinequality (degree = 1) (degree = 2)Tail bound ≈ 1

2 ≈ 14

𝑑→∞−−−−−→ 0ℙ[tick ≄ 4𝑑](b)

20 40 60 80 𝑑

0.25

0.5

0.75

[31, 43]

[26]

this workℙ[tick

≄4𝑑]

(c)

Figure 1. (a) Comparison in terms of supporting features.(b) Comparison in terms of moment bounds for the runningexample. (c) Comparison in terms of derived tail bounds.

- Using linear programming (LP) to efficiently automatethe (expected) potential method for (expected) cost analy-sis [20, 21, 42].

The contributions of our work are as follows:‱ We develop moment semirings to compose the momentsfor a cost accumulator from two computations, and toenable interprocedural reasoning about higher moments.

‱ We instantiate moment semirings with the symbolic in-terval domain, use that to develop a derivation systemfor interval bounds on higher central moments for costaccumulators, and automate the derivation via LP solving.

‱ We prove the soundness of our derivation system for pro-grams that satisfy the criterion of our recent extension tothe Optional Stopping Theorem, and develop an algorithmfor checking this criterion automatically.

‱ We implemented our analysis and evaluated it on a broadsuite of benchmarks from the literature. Our experimentalresults show that on a variety of examples, our analyzer isable to use higher central moments to obtain tighter tailbounds on program runtimes than the system of Kura et al.[26], which uses only upper bounds on raw moments.

2

Page 3: Central Moment Analysis for Cost Accumulators in

Central Moment Analysis for Cost Accumulators in Probabilistic Programs PLDI ’21, June 20–25, 2021, Virtual, UK

1 func rdwalk() begin2 { 2(𝑑 − đ‘„) + 4 }3 if đ‘„ < 𝑑 then4 { 2(𝑑 − đ‘„) + 4 }5 𝑡 ∌ uniform(−1, 2);6 { 2(𝑑 − đ‘„ − 𝑡) + 5 }7 đ‘„ B đ‘„ + 𝑡 ;8 { 2(𝑑 − đ‘„) + 5 }9 call rdwalk;10 { 1 }11 tick(1)12 { 0 }13 fi14 end

1 # XID def= {đ‘„, 𝑑, 𝑡}

2 # FID def= {rdwalk}

3 # pre-condition: {𝑑 > 0}4 func main() begin5 đ‘„ B 0;6 call rdwalk7 end

Figure 2. A bounded, biased random walk, implementedusing recursion. The annotations show the derivation of anupper bound on the expected accumulated cost.

2 OverviewIn this section, we demonstrate the expected-potentialmethod for both first-moment analysis (previous work) andhigher central-moment analysis (this work) (§2.1), and dis-cuss the challenges to supporting interprocedural reasoningand to ensuring the soundness of our approach (§2.2).

Example 2.1. The program in Fig. 2 implements a bounded,biased random walk. The main function consists of a singlestatement “call rdwalk” that invokes a recursive function.The variables đ‘„ and 𝑑 represent the current position and theending position of the randomwalk, respectively. We assumethat 𝑑 > 0 holds initially. In each step, the program samplesthe length of the current move from a uniform distributionon the interval [−1, 2]. The statement tick(1) adds one to acost accumulator that counts the number of steps before therandomwalk ends. We denote this accumulator by tick in therest of this section. The program terminates with probabilityone and its expected accumulated cost is bounded by 2𝑑 + 4.

2.1 The Expected-Potential Method forHigher-Moment Analysis

Our approach to higher-moment analysis is inspired by theexpected-potential method [31], which is also known as rank-ing super-martingales [10, 11, 26, 43], for expected-cost boundanalysis of probabilistic programs.The classic potential method of amortized analysis [39]

can be automated to derive symbolic cost bounds for non-probabilistic programs [20, 21]. The basic idea is to definea potential function 𝜙 : ÎŁ → ℝ+ that maps program states𝜎 ∈ ÎŁ to nonnegative numbers, where we assume eachstate 𝜎 contains a cost-accumulator component 𝜎.đ›Œ . If aprogram executes with initial state 𝜎 to final state 𝜎 â€Č, then itholds that 𝜙 (𝜎) ≄ (𝜎 â€Č.đ›Œ − 𝜎.đ›Œ) + 𝜙 (𝜎 â€Č), where (𝜎 â€Č.đ›Œ − 𝜎.đ›Œ)describes the accumulated cost from 𝜎 to 𝜎 â€Č. The potentialmethod also enables compositional reasoning: if a statement

𝑆1 executes from 𝜎 to 𝜎 â€Č and a statement 𝑆2 executes from𝜎 â€Č to 𝜎 â€Čâ€Č, then we have 𝜙 (𝜎) ≄ (𝜎 â€Č.đ›Œ − 𝜎.đ›Œ) + 𝜙 (𝜎 â€Č) and𝜙 (𝜎 â€Č) ≄ (𝜎 â€Čâ€Č.đ›Œ −𝜎 â€Č.đ›Œ) +𝜙 (𝜎 â€Čâ€Č); therefore, we derive 𝜙 (𝜎) ≄(𝜎 â€Čâ€Č.đ›Œ − 𝜎.đ›Œ) + 𝜙 (𝜎 â€Čâ€Č) for the sequential composition 𝑆1; 𝑆2.For non-probabilistic programs, the initial potential providesan upper bound on the accumulated cost.

This approach has been adapted to reason about expectedcosts of probabilistic programs [31, 43]. To derive upperbounds on the expected accumulated cost of a program 𝑆with initial state 𝜎 , one needs to take into consideration thedistribution of all possible executions. More precisely, thepotential function should satisfy the following property:

𝜙 (𝜎) ≄ đ”ŒđœŽâ€Č∌J𝑆K(𝜎) [đ¶ (𝜎, 𝜎 â€Č) + 𝜙 (𝜎 â€Č)], (1)where the notationđ”Œđ‘„âˆŒ` [𝑓 (đ‘„)] represents the expected valueof 𝑓 (đ‘„), where đ‘„ is drawn from the distribution `, J𝑆K(𝜎) isthe distribution over final states of executing 𝑆 from 𝜎 , andđ¶ (𝜎, 𝜎 â€Č) def

= 𝜎 â€Č.đ›Œ − 𝜎.đ›Œ is the execution cost from 𝜎 to 𝜎 â€Č.

Example 2.2. Fig. 2 annotates the rdwalk function fromEx. 2.1 with the derivation of an upper bound on the expectedaccumulated cost. The annotations, taken together, define anexpected-potential function 𝜙 : ÎŁ → ℝ+ where a programstate 𝜎 ∈ ÎŁ consists of a program point and a valuationfor program variables. To justify the upper bound 2(𝑑 âˆ’đ‘„) + 4 for the function rdwalk, one has to show that thepotential right before the tick(1) statement should be atleast 1. This property is established by backward reasoningon the function body:‱ For call rdwalk, we apply the “induction hypothesis” thatthe expected cost of the function rdwalk can be upper-bounded by 2(𝑑 − đ‘„) + 4. Adding the 1 unit of potentialneed by the tick statement, we obtain 2(𝑑 − đ‘„) + 5 as thepre-annotation of the function call.

‱ For đ‘„ B đ‘„ + 𝑡 , we substitute đ‘„ with đ‘„ + 𝑡 in the post-annotation of this statement to obtain the pre-annotation.

‱ For 𝑡 ∌ uniform(−1, 2), because its post-annotation is2(𝑑 − đ‘„ − 𝑡) + 5, we compute its pre-annotation as

đ”Œđ‘ĄâˆŒuniform(−1,2) [2(𝑑 − đ‘„ − 𝑡) + 5]= 2(𝑑 − đ‘„) + 5 − 2 · đ”Œđ‘ĄâˆŒuniform(−1,2) [𝑡]= 2(𝑑 − đ‘„) + 5 − 2 · 1/2 = 2(𝑑 − đ‘„) + 4,

which is exactly the upper bound we want to justify.

Our approach. In this paper, we focus on derivation ofhigher central moments. Observing that a central momentđ”Œ[(𝑋 âˆ’đ”Œ[𝑋 ])𝑘 ] can be rewritten as a polynomial of raw mo-ments đ”Œ[𝑋 ], · · · ,đ”Œ[𝑋𝑘 ], we reduce the problem of boundingcentral moments to reasoning about upper and lower boundson raw moments. For example, the variance can be writtenas 𝕍 [𝑋 ] = đ”Œ[𝑋 2] âˆ’đ”Œ2 [𝑋 ], so it suffices to analyze the upperbound of the second moment đ”Œ[𝑋 2] and the lower boundon the square of the first moment đ”Œ2 [𝑋 ]. For higher cen-tral moments, this approach requires both upper and lowerbounds on higher raw moments. For example, consider thefourth central moment of a nonnegative random variable 𝑋 :

3

Page 4: Central Moment Analysis for Cost Accumulators in

PLDI ’21, June 20–25, 2021, Virtual, UK Di Wang, Jan Hoffmann, and Thomas Reps

1 func rdwalk() begin2 { ⟹1, 2(𝑑 − đ‘„) + 4, 4(𝑑 − đ‘„)2 + 22(𝑑 − đ‘„) + 28⟩ }3 if đ‘„ < 𝑑 then4 { ⟹1, 2(𝑑 − đ‘„) + 4, 4(𝑑 − đ‘„)2 + 22(𝑑 − đ‘„) + 28⟩ }5 𝑡 ∌ uniform(−1, 2);6 { ⟹1, 2(𝑑 − đ‘„ − 𝑡) + 5, 4(𝑑 − đ‘„ − 𝑡)2 + 26(𝑑 − đ‘„ − 𝑡) + 37⟩ }7 đ‘„ B đ‘„ + 𝑡 ;8 { ⟹1, 2(𝑑 − đ‘„) + 5, 4(𝑑 − đ‘„)2 + 26(𝑑 − đ‘„) + 37⟩ }9 call rdwalk;10 { ⟹1, 1, 1⟩ }11 tick(1)12 { ⟹1, 0, 0⟩ }13 fi14 end

Figure 3. Derivation of an upper bound on the first andsecond moment of the accumulated cost.

đ”Œ[(𝑋 âˆ’đ”Œ[𝑋 ])4] = đ”Œ[𝑋 4] −4đ”Œ[𝑋 3]đ”Œ[𝑋 ] +6đ”Œ[𝑋 2]đ”Œ2 [𝑋 ] −3đ”Œ4 [𝑋 ] .Deriving an upper bound on the fourth central moment re-quires lower bounds on the first (đ”Œ[𝑋 ]) and third (đ”Œ[𝑋 3])raw moments.We now sketch the development of moment semirings.

We first consider only the upper bounds on higher mo-ments of nonnegative costs. To do so, we extend the rangeof the expected-potential function 𝜙 to real-valued vectors(ℝ+)𝑚+1, where𝑚 ∈ ℕ is the degree of the target moment.We update the potential inequality (1) as follows:

𝜙 (𝜎) ≄ đ”ŒđœŽâ€Č∌J𝑆K(𝜎) [âˆ’âˆ’âˆ’âˆ’âˆ’âˆ’âˆ’âˆ’âˆ’âˆ’âˆ’âˆ’âˆ’âˆ’âˆ’â†’âŸšđ¶ (𝜎, 𝜎 â€Č)đ‘˜âŸ©0â‰€đ‘˜â‰€đ‘š ⊗ 𝜙 (𝜎 â€Č)], (2)

whereâˆ’âˆ’âˆ’âˆ’âˆ’âˆ’âˆ’âˆ’â†’âŸšđ‘Łđ‘˜âŸ©0â‰€đ‘˜â‰€đ‘š denotes an (𝑚 + 1)-dimensional vector,

the order ≀ on vectors is defined pointwise, and ⊗ is somecomposition operator. Recall that J𝑆K(𝜎) denotes the distri-bution over final states of executing 𝑆 from 𝜎 , and đ¶ (𝜎, 𝜎 â€Č)describes the cost for the execution from 𝜎 to 𝜎 â€Č. Intuitively,for 𝜙 (𝜎) = −−−−−−−−−−−−→⟹𝜙 (𝜎)đ‘˜âŸ©0â‰€đ‘˜â‰€đ‘š and each 𝑘 , the component 𝜙 (𝜎)𝑘is an upper bound on the 𝑘-th moment of the cost for thecomputation starting from 𝜎 . The 0-th moment is the ter-mination probability of the computation, and we assume itis always one for now. We cannot simply define ⊗ as point-wise addition because, for example, (𝑎 + 𝑏)2 ≠ 𝑎2 + 𝑏2 ingeneral. If we think of 𝑏 as the cost for some probabilis-tic computation, and we prepend a constant cost 𝑎 to thecomputation, then by linearity of expectations, we haveđ”Œ[(𝑎 + 𝑏)2] = đ”Œ[𝑎2 + 2𝑎𝑏 + 𝑏2] = 𝑎2 + 2 · 𝑎 · đ”Œ[𝑏] + đ”Œ[𝑏2],i.e., reasoning about the second moment requires us to keeptrack of the first moment. Similarly, we should have𝜙 (𝜎)2 ≄ đ”ŒđœŽâ€Č∌J𝑆K(𝜎) [đ¶ (𝜎, 𝜎 â€Č)2 + 2 Â·đ¶ (𝜎, 𝜎 â€Č) Â·đœ™ (𝜎 â€Č)1 +𝜙 (𝜎 â€Č)2],for the second-moment component, where𝜙 (𝜎 â€Č)1 and𝜙 (𝜎 â€Č)2denote đ”Œ[𝑏] and đ”Œ[𝑏2], respectively. Therefore, the compo-sition operator ⊗ for second-moment analysis (i.e.,𝑚 = 2)should be defined as

⟹1, 𝑟1, 𝑠1⟩ ⊗ ⟹1, 𝑟2, 𝑠2⟩ def= ⟹1, 𝑟1 + 𝑟2, 𝑠1 + 2𝑟1𝑟2 + 𝑠2⟩. (3)

Example 2.3. Fig. 3 annotates the rdwalk function fromEx. 2.1 with the derivation of an upper bound on both thefirst and second moment of the accumulated cost. To justifythe first and second moment of the accumulated cost for thefunction rdwalk, we again perform backward reasoning:‱ For tick(1), it transforms a post-annotation 𝑎 by_𝑎.(⟹1, 1, 1⟩ ⊗ 𝑎); thus, the pre-annotation is ⟹1, 1, 1⟩ ⊗⟹1, 0, 0⟩ = ⟹1, 1, 1⟩.

‱ For call rdwalk, we apply the “induction hypothesis”, i.e.,the upper bound shown on line 2. We use the ⊗ opera-tor to compose the induction hypothesis with the post-annotation of this function call:

⟹1, 2(đ‘‘âˆ’đ‘„)+4, 4(đ‘‘âˆ’đ‘„)2+22(đ‘‘âˆ’đ‘„)+28⟩ ⊗ ⟹1, 1, 1⟩= ⟹1, 2(đ‘‘âˆ’đ‘„)+5, (4(đ‘‘âˆ’đ‘„)2+22(đ‘‘âˆ’đ‘„)+28)+2· (2(đ‘‘âˆ’đ‘„)+4)+1⟩= ⟹1, 2(đ‘‘âˆ’đ‘„)+5, 4(đ‘‘âˆ’đ‘„)2+26(đ‘‘âˆ’đ‘„)+37⟩.

‱ For đ‘„ B đ‘„ + 𝑡 , we substitute đ‘„ with đ‘„ + 𝑡 in the post-annotation of this statement to obtain the pre-annotation.

‱ For 𝑡 ∌ uniform(−1, 2), because the post-annotation in-volves both 𝑡 and 𝑡2, we compute from the definition ofuniform distributions thatđ”Œđ‘ĄâˆŒuniform(−1,2) [𝑡] = 1/2, đ”Œđ‘ĄâˆŒuniform(−1,2) [𝑡2] = 1.

Then the upper bound on the second moment is derivedas follows:đ”Œđ‘ĄâˆŒuniform(−1,2) [4(đ‘‘âˆ’đ‘„âˆ’đ‘Ą)2+26(đ‘‘âˆ’đ‘„âˆ’đ‘Ą)+37]

= (4(đ‘‘âˆ’đ‘„)2+26(đ‘‘âˆ’đ‘„)+37)−(8(đ‘‘âˆ’đ‘„)+26) Â·đ”Œđ‘ĄâˆŒuniform(−1,2) [𝑡]+ 4Â·đ”Œđ‘ĄâˆŒuniform(−1,2) [𝑡2]

= 4(đ‘‘âˆ’đ‘„)2+22(đ‘‘âˆ’đ‘„)+28,which is the same as the desired upper bound on the sec-ond moment of the accumulated cost for the functionrdwalk. (See Fig. 3, line 2.)

We generalize the composition operator ⊗ to momentswith arbitrarily high degrees, via a family of algebraic struc-tures, which we name moment semirings (see §3.2). Thesesemirings are algebraic in the sense that they can be instan-tiated with any partially ordered semiring, not just ℝ+.

Interval bounds. Moment semirings not only provide ageneral method to analyze higher moments, but also enablereasoning about upper and lower bounds on moments simul-taneously. The simultaneous treatment is also essential foranalyzing programs with non-monotone costs (see §3.3).

We instantiate moment semirings with the standard inter-val semiring I = {[𝑎, 𝑏] | 𝑎 ≀ 𝑏}. The algebraic approachallows us to systematically incorporate the interval-valuedbounds, by reinterpreting operations in eq. (3) under I :

⟹[1, 1], [𝑟L1 , 𝑟U1 ], [𝑠L1 , 𝑠U1 ]⟩ ⊗ ⟹[1, 1], [𝑟L2 , 𝑟U2 ], [𝑠L2 , 𝑠U2 ]⟩def= ⟹[1, 1], [𝑟L1 , 𝑟U1 ] +I [𝑟L2 , 𝑟U2 ],

[𝑠L1 , 𝑠U2 ] +I 2 · ( [𝑟L1 , 𝑟U1 ] ·I [𝑟L2 , 𝑟U2 ]) +I [𝑠L2 , 𝑠U2 ]⟩= ⟹[1, 1], [𝑟L1 + 𝑟L2 , 𝑟U1 + 𝑟U2 ], [𝑠L1 + 2 ·min 𝑆 + 𝑠L2 , 𝑠U1 + 2 ·max 𝑆 + 𝑠U2 ]⟩,

4

Page 5: Central Moment Analysis for Cost Accumulators in

Central Moment Analysis for Cost Accumulators in Probabilistic Programs PLDI ’21, June 20–25, 2021, Virtual, UK

where 𝑆 def= {𝑟L1𝑟L2 , 𝑟L1𝑟U2 , 𝑟U1 𝑟L2 , 𝑟U1 𝑟U2 }. We then update the po-

tential inequality eq. (2) as follows:

𝜙 (𝜎) ⊒ đ”ŒđœŽâ€Č∌J𝑆K(𝜎) [−−−−−−−−−−−−−−−−−−−−−−−−−−−→⟹[đ¶ (𝜎, 𝜎 â€Č)𝑘 ,đ¶ (𝜎, 𝜎 â€Č)𝑘 ]⟩0â‰€đ‘˜â‰€đ‘š ⊗ 𝜙 (𝜎 â€Č)],

where the order ⊑ is defined as pointwise interval inclusion.

Example 2.4. Suppose that the interval bound on the firstmoment of the accumulated cost of the rdwalk function fromEx. 2.1 is [2(𝑑 − đ‘„), 2(𝑑 − đ‘„) + 4]. We can now derive theupper bound on the variance 𝕍 [tick] ≀ 22𝑑 + 28 shown inFig. 1(b) (where we substitute đ‘„ with 0 because the mainfunction initializes đ‘„ to 0 on line 5 in Fig. 2):𝕍 [tick] = đ”Œ[tick2] − đ”Œ2 [tick]

≀ (upper bnd. on đ”Œ[tick2])−(lower bnd. on đ”Œ[tick])2= (4𝑑2 + 22𝑑 + 28) − (2𝑑)2 = 22𝑑 + 28.

In §5, we describe how we use moment bounds to derivethe tail bounds shown in Fig. 1(c).

2.2 Two Major ChallengesInterprocedural reasoning. Recall that in the derivation

of Fig. 3, we use the ⊗ operator to compose the upper boundson moments for call rdwalk and its post-annotation ⟹1, 1, 1⟩.However, this approach does not work in general, becausethe post-annotation might be symbolic (e.g., ⟹1, đ‘„, đ‘„2⟩) andthe callee might mutate referenced program variables (e.g., đ‘„ ).One workaround is to derive a pre-annotation for each possi-ble post-annotation of a recursive function, i.e., the momentannotations for a recursive function is polymorphic. Thisworkaround would not be effective for non-tail-recursivefunctions: for example, we need to reason about the rdwalkfunction in Fig. 3 with infinitely many post-annotations⟹1, 0, 0⟩, ⟹1, 1, 1⟩, ⟹1, 2, 4⟩, . . . , i.e., ⟹1, 𝑖, 𝑖2⟩ for all 𝑖 ∈ â„€+.Our solution to moment-polymorphic recursion is to intro-

duce a combination operator ⊕ in a way that if 𝜙1 and 𝜙2 aretwo expected-potential functions, then

𝜙1 (𝜎)⊕𝜙2 (𝜎) â‰„đ”ŒđœŽâ€Č∌J𝑆K(𝜎) [âˆ’âˆ’âˆ’âˆ’âˆ’âˆ’âˆ’âˆ’âˆ’âˆ’âˆ’âˆ’âˆ’âˆ’â†’âŸšđ¶ (𝜎, 𝜎 â€Č)đ‘˜âŸ©0â‰€đ‘˜â‰€đ‘šâŠ—(𝜙1 (𝜎 â€Č)⊕𝜙2 (𝜎 â€Č))] .

We then use the ⊕ operator to derive a frame rule:{ 𝑄1 } 𝑆 { 𝑄 â€Č

1 } { 𝑄2 } 𝑆 { 𝑄 â€Č2 }

{ 𝑄1 ⊕ 𝑄2 } 𝑆 { 𝑄 â€Č1 ⊕ 𝑄 â€Č

2 }We define ⊕ as pointwise addition, i.e., for second moments,

⟹𝑝1, 𝑟1, 𝑠1⟩ ⊕ ⟹𝑝2, 𝑟2, 𝑠2⟩ def= ⟹𝑝1 + 𝑝2, 𝑟1 + 𝑟2, 𝑠1 + 𝑠2⟩, (4)

and because the 0-th-moment (i.e., termination-probability)component is no longer guaranteed to be one, we redefine⊗ to consider the termination probabilities:⟹𝑝1, 𝑟1, 𝑠1âŸ©âŠ—âŸšđ‘2, 𝑟2, 𝑠2⟩ def

= ⟹𝑝1𝑝2, 𝑝2𝑟1+𝑝1𝑟2, 𝑝2𝑠1+2𝑟1𝑟2+𝑝1𝑠2⟩.(5)

Remark 2.5. As we will show in §3.2, the composition oper-ator ⊗ and combination operator ⊕ form a moment semiring;consequently, we can use algebraic properties of semirings (e.g.,distributivity) to aid higher-moment analysis. For example,a vector ⟹0, 𝑟1, 𝑠1⟩ whose termination-probability component

is zero does not seem to make sense, because moments withrespect to a zero distribution should also be zero. However, bydistributivity, we have

⟹1, 𝑟3, 𝑠3⟩ ⊗ ⟹1, 𝑟1 + 𝑟2, 𝑠1 + 𝑠2⟩= ⟹1, 𝑟3, 𝑠3⟩ ⊗ (⟹0, 𝑟1, 𝑠1⟩ ⊕ ⟹1, 𝑟2, 𝑠2⟩)= (⟹1, 𝑟3, 𝑠3⟩ ⊗ ⟹0, 𝑟1, 𝑠1⟩) ⊕ (⟹1, 𝑟3, 𝑠3⟩ ⊕ ⟹1, 𝑟2, 𝑠2⟩).

If we think of ⟹1, 𝑟1 + 𝑟2, 𝑠1 + 𝑠2⟩ as a post-annotation of acomputation whose moments are bounded by ⟹1, 𝑟3, 𝑠3⟩, theequation above indicates that we can use ⊕ to decompose thepost-annotation into subparts, and then reason about each sub-part separately. This fact inspires us to develop a decompositiontechnique for moment-polymorphic recursion.

Example 2.6. With the ⊕ operator and the frame rule, weonly need to analyze the rdwalk function from Ex. 2.1 withthree post-annotations: ⟹1, 0, 0⟩, ⟹0, 1, 1⟩, and ⟹0, 0, 2⟩, whichform a kind of “elimination sequence.” We construct thissequence in an on-demand manner; the first post-annotationis the identity element ⟹1, 0, 0⟩ of the moment semiring.For post-annotation ⟹1, 0, 0⟩, as shown in Fig. 3, we need

to know the moment bound for rdwalk with the post-annotation ⟹1, 1, 1⟩. Instead of reanalyzing rdwalk with thepost-annotation ⟹1, 1, 1⟩, we use the ⊕ operator to computethe “difference” between it and the previous post-annotation⟹1, 0, 0⟩. Observing that ⟹1, 1, 1⟩ = ⟹1, 0, 0⟩ ⊕ ⟹0, 1, 1⟩, we nowanalyze rdwalk with ⟹0, 1, 1⟩ as the post-annotation:1 call rdwalk; { ⟹0, 1, 3⟩ } # = ⟹1, 1, 1⟩ ⊗ ⟹0, 1, 1⟩2 tick(1) { ⟹0, 1, 1⟩ }Again, because ⟹0, 1, 3⟩ = ⟹0, 1, 1⟩ ⊕ ⟹0, 0, 2⟩, we need to fur-ther analyze rdwalk with ⟹0, 0, 2⟩ as the post-annotation:1 call rdwalk; { ⟹0, 0, 2⟩ } # = ⟹1, 1, 1⟩ ⊗ ⟹0, 0, 2⟩2 tick(1) { ⟹0, 0, 2⟩ }With the post-annotation ⟹0, 0, 2⟩, we can now rea-son monomorphically without analyzing any new post-annotation! We can perform a succession of reasoning stepssimilar to what we have done in Ex. 2.2 to justify the follow-ing bounds (“unwinding” the elimination sequence):‱ {⟹0, 0, 2⟩} rdwalk {⟹0, 0, 2⟩}: Directly by backward reason-ing with the post-annotation ⟹0, 0, 2⟩.

‱ {⟹0, 1, 4(𝑑 − đ‘„) + 9⟩} rdwalk {⟹0, 1, 1⟩}: To analyze the re-cursive call with post-annotation ⟹0, 1, 3⟩, we use the framerule with the post-call-site annotation ⟹0, 0, 2⟩ to derive⟹0, 1, 4(𝑑 − đ‘„) + 11⟩ as the pre-annotation:1 { ⟹0, 1, 4(𝑑 − đ‘„) + 11⟩ } # = ⟹0, 1, 4(𝑑 − đ‘„) + 9⟩ ⊕ ⟹0, 0, 2⟩2 call rdwalk;3 { ⟹0, 1, 3⟩ } # = ⟹0, 1, 1⟩ ⊕ ⟹0, 0, 2⟩

‱ {⟹1, 2(đ‘‘âˆ’đ‘„)+4, 4(đ‘‘âˆ’đ‘„)2+22(đ‘‘âˆ’đ‘„)+28⟩} rdwalk {⟹1, 0, 0⟩}:To analyze the recursive call with post-annotation ⟹1, 1, 1⟩,we use the frame rule with the post-call-site annotation⟹0, 1, 1⟩ to derive ⟹1, 2(𝑑 âˆ’đ‘„) +5, 4(𝑑 âˆ’đ‘„)2 +26(𝑑 âˆ’đ‘„) +37⟩as the pre-annotation:1 { ⟹1, 2(đ‘‘âˆ’đ‘„)+5, 4(đ‘‘âˆ’đ‘„)2+26(đ‘‘âˆ’đ‘„)+37⟩ }2 # = ⟹1, 2(đ‘‘âˆ’đ‘„)+4, 4(đ‘‘âˆ’đ‘„)2+22(đ‘‘âˆ’đ‘„)+28⟩ ⊕ ⟹0, 1, 4(đ‘‘âˆ’đ‘„)+9⟩3 call rdwalk;4 { ⟹1, 1, 1⟩ } # = ⟹1, 0, 0⟩ ⊕ ⟹0, 1, 1⟩

5

Page 6: Central Moment Analysis for Cost Accumulators in

PLDI ’21, June 20–25, 2021, Virtual, UK Di Wang, Jan Hoffmann, and Thomas Reps

1 func geo() begin { ⟹1, 2đ‘„ ⟩ }2 đ‘„ B đ‘„ + 1; { ⟹1, 2đ‘„âˆ’1⟩ }3 # expected-potential method for lower bounds:4 # 2đ‘„âˆ’1 < 1/2 · (2đ‘„ + 1) + 1/2 · 05 if prob(1/2) then { ⟹1, 2đ‘„ + 1⟩ }6 tick(1); { ⟹1, 2đ‘„ ⟩ }7 call geo { ⟹1, 0⟩}8 fi9 end

Figure 4. A purely probabilistic loop with annotations for alower bound on the first moment of the accumulated cost.

In §3.3, we present an automatic inference system for theexpected-potential method that is extended with interval-valued bounds on highermoments, with support for moment-polymorphic recursion.

Soundness of the analysis. Unlike the classic potentialmethod, the expected-potential method is not always soundwhen reasoning about the moments for cost accumulatorsin probabilistic programs.

Counterexample 2.7. Consider the program in Fig. 4 thatdescribes a purely probabilistic loop that exits the loop withprobability 1/2 in each iteration. The expected accumulatedcost of the program should be one [19]. However, the annota-tions in Fig. 4 justify a potential function 2đ‘„ as a lower boundon the expected accumulated cost, no matter what value đ‘„has at the beginning, which is apparently unsound.

Why does the expected-potential method fail in this case?The short answer is that dualization only works for someproblems: upper-bounding the sum of nonnegative ticks isequivalent to lower-bounding the sum of nonpositive ticks;lower-bounding the sum of nonnegative ticks—the issue inFig. 4—is equivalent to upper-bounding the sum of nonposi-tive ticks; however, the two kinds of problems are inherentlydifferent [19]. Intuitively, the classic potential method forbounding the costs of non-probabilistic programs is a par-tial-correctness method, i.e., derived upper/lower bounds aresound if the analyzed program terminates [32]. With proba-bilistic programs, many programs do not terminate definitely,but only almost surely, i.e., they terminate with probabilityone, but have some execution traces that are non-terminating.The programs in Figs. 2 and 4 are both almost-surely termi-nating. For the expected-potential method, the potential at aprogram state can be seen as an average of potentials neededfor all possible computations that continue from the state.If the program state can lead to a non-terminating execu-tion trace, the potential associated with that trace might beproblematic, and as a consequence, the expected-potentialmethod might fail.

Recent research [1, 19, 38, 43] has employed the OptionalStopping Theorem (OST) from probability theory to addressthis soundness issue. The classic OST provides a collection ofsufficient conditions for reasoning about expected gain upon

𝑆 F skip | tick(𝑐) | đ‘„ B 𝐾 | đ‘„ ∌ đ· | call 𝑓 | while 𝐿 do 𝑆 od| if prob(𝑝) then 𝑆1 else 𝑆2 fi | if 𝐿 then 𝑆1 else 𝑆2 fi | 𝑆1; 𝑆2

𝐿 F true | not 𝐿 | 𝐿1 and 𝐿2 | 𝐾1 ≀ 𝐾2𝐾 F đ‘„ | 𝑐 | 𝐾1 + 𝐾2 | 𝐾1 × 𝐾2đ· F uniform(𝑎, 𝑏) | · · ·

Figure 5. Syntax of Appl, where 𝑝 ∈ [0, 1], 𝑎, 𝑏, 𝑐 ∈ ℝ, 𝑎 < 𝑏,đ‘„ ∈ VID is a variable, and 𝑓 ∈ FID is a function identifier.

termination of stochastic processes, where the expected gainat any time is invariant. By constructing a stochastic pro-cess for executions of probabilistic programs and setting theexpected-potential function as the invariant, one can applythe OST to justify the soundness of the expected-potentialfunction. In a companion paper [41], we study and proposean extension to the classic OST with a new sufficient condi-tion that is suitable for reasoning about higher moments; inthis work, we prove the soundness of our central-moment in-ference for programs that satisfy this condition, and developan algorithm to check this condition automatically (see §4).

3 Derivation System for Higher MomentsIn this section, we describe the inference system used byour analysis. We first present a probabilistic programminglanguage (§3.1). We then introduce moment semirings tocompose higher moments for a cost accumulator from twocomputations (§3.2). We use moment semirings to developour derivation system, which is presented as a declarativeprogram logic (§3.3). Finally, we sketch how we reduce theinference of a derivation to LP solving (§3.4).

3.1 A Probabilistic Programming LanguageThis paper uses an imperative arithmetic probabilistic pro-gramming language Appl that supports general recursionand continuous distributions, where program variables arereal-valued. We use the following notational conventions.Natural numbers ℕ exclude 0, i.e., ℕ def

= {1, 2, 3, · · · } ⊆ â„€+ def=

{0, 1, 2, · · · }. The Iverson brackets [·] are defined by [𝜑] = 1if 𝜑 is true and otherwise [𝜑] = 0. We denote updating anexisting binding of đ‘„ in a finite map 𝑓 to 𝑣 by 𝑓 [đ‘„ ↩→ 𝑣]. Wewill also use the following standard notions from probabilitytheory: 𝜎-algebras, measurable spaces, measurable functions,random variables, probability measures, and expectations.Appendix A provides a review of those notions.

Fig. 5 presents the syntax of Appl, where the metavari-ables 𝑆 , 𝐿, 𝐾, and đ· stand for statements, conditions, expres-sions, and distributions, respectively. Each distribution đ· isassociated with a probability measure `đ· ∈ đ”»(ℝ). We writeđ”»(𝑋 ) for the collection of all probability measures on themeasurable space 𝑋 . For example, uniform(𝑎, 𝑏) describesa uniform distribution on the interval [𝑎, 𝑏], and its corre-sponding probability measure is the integration of its densityfunction `uniform(𝑎,𝑏) (𝑂) def

=âˆ«đ‘‚

[đ‘Žâ‰€đ‘„â‰€đ‘ ]𝑏−𝑎 đ‘‘đ‘„ . The statement

6

Page 7: Central Moment Analysis for Cost Accumulators in

Central Moment Analysis for Cost Accumulators in Probabilistic Programs PLDI ’21, June 20–25, 2021, Virtual, UK

â€œđ‘„ ∌ đ·â€ is a random-sampling assignment, which draws fromthe distribution `đ· to obtain a sample value and then assignsit to đ‘„ . The statement “if prob(𝑝) then 𝑆1 else 𝑆2 fi” is aprobabilistic-branching statement, which executes 𝑆1 withprobability 𝑝 , or 𝑆2 with probability (1 − 𝑝).The statement “call 𝑓 ” makes a (possibly recursive) call

to the function with identifier 𝑓 ∈ FID. In this paper, we as-sume that the functions only manipulate states that consistof global program variables. The statement tick(𝑐), where𝑐 ∈ ℝ is a constant, is used to define the cost model. It adds 𝑐to an anonymous global cost accumulator. Note that our im-plementation supports local variables, function parameters,return statements, as well as accumulation of non-constantcosts; the restrictions imposed here are not essential, and areintroduced solely to simplify the presentation.We use a pair ⟹𝒟, 𝑆main⟩ to represent an Appl program,

where 𝒟 is a finite map from function identifiers to theirbodies and 𝑆main is the body of the main function. We presentan operational semantics for Appl in §4.1.

3.2 Moment SemiringsAs discussed in §2.1, we want to design a composition op-eration ⊗ and a combination operation ⊕ to compose andcombine higher moments of accumulated costs such that

𝜙 (𝜎) âŠ’đ”ŒđœŽâ€Č∌J𝑆K(𝜎)[âˆ’âˆ’âˆ’âˆ’âˆ’âˆ’âˆ’âˆ’âˆ’âˆ’âˆ’âˆ’âˆ’âˆ’â†’âŸšđ¶ (𝜎, 𝜎 â€Č)đ‘˜âŸ©0â‰€đ‘˜â‰€đ‘šâŠ—đœ™ (𝜎 â€Č)],

𝜙1 (𝜎)⊕𝜙2 (𝜎) âŠ’đ”ŒđœŽâ€Č∌J𝑆K(𝜎)[âˆ’âˆ’âˆ’âˆ’âˆ’âˆ’âˆ’âˆ’âˆ’âˆ’âˆ’âˆ’âˆ’âˆ’â†’âŸšđ¶ (𝜎, 𝜎 â€Č)đ‘˜âŸ©0â‰€đ‘˜â‰€đ‘šâŠ—(𝜙1 (𝜎 â€Č)⊕𝜙2 (𝜎 â€Č))],

where the expected-potential functions 𝜙, 𝜙1, 𝜙2 map pro-gram states to interval-valued vectors, đ¶ (𝜎, 𝜎 â€Č) is the costfor the computation from 𝜎 to 𝜎 â€Č, and 𝑚 is the degree ofthe target moment. In eqs. (4) and (5), we gave a definitionof ⊗ and ⊕ suitable for first and second moments, respec-tively. In this section, we generalize them to reason aboutupper and lower bounds of higher moments. Our approachis inspired by the work of Li and Eisner [27], which devel-ops a method to “lift” techniques for first moments to thosefor second moments. Instead of restricting the elements ofsemirings to be vectors of numbers, we propose algebraicmoment semirings that can also be instantiated with vectorsof intervals, which we need for the interval-bound analysisthat was demonstrated in §2.1.

Definition 3.1. The𝑚-th order moment semiring M(𝑚)R =

( |R|𝑚+1, ⊕, ⊗, 0, 1) is parametrized by a partially orderedsemiring R = ( |R|, ≀, +, ·, 0, 1), whereâˆ’âˆ’âˆ’âˆ’âˆ’âˆ’âˆ’âˆ’âˆ’â†’âŸšđ‘ąđ‘˜âŸ©0â‰€đ‘˜â‰€đ‘šâŠ•âˆ’âˆ’âˆ’âˆ’âˆ’âˆ’âˆ’âˆ’â†’âŸšđ‘Łđ‘˜âŸ©0â‰€đ‘˜â‰€đ‘š def

=−−−−−−−−−−−−−→⟹𝑱𝑘 + đ‘Łđ‘˜âŸ©0â‰€đ‘˜â‰€đ‘š, (6)

âˆ’âˆ’âˆ’âˆ’âˆ’âˆ’âˆ’âˆ’âˆ’â†’âŸšđ‘ąđ‘˜âŸ©0â‰€đ‘˜â‰€đ‘šâŠ—âˆ’âˆ’âˆ’âˆ’âˆ’âˆ’âˆ’âˆ’â†’âŸšđ‘Łđ‘˜âŸ©0â‰€đ‘˜â‰€đ‘š def=−−−−−−−−−−−−−−−−−−−−−−−−−→⟹∑𝑘

𝑖=0(𝑘𝑖

)×(𝑱𝑖 · 𝑣𝑘−𝑖 )⟩0â‰€đ‘˜â‰€đ‘š, (7)(𝑘𝑖

)is the binomial coefficient; the scalar product 𝑛 × 𝑱 is an

abbreviation for∑𝑛𝑖=1 𝑱, for 𝑛 ∈ â„€+, 𝑱 ∈ R; 0 def

= ⟹0, 0, · · · , 0⟩;and 1 def

= ⟹1, 0, · · · , 0⟩. We define the partial order ⊑ as thepointwise extension of the partial order ≀ onR.

Intuitively, the definition of ⊗ in eq. (7) can be seen asthe multiplication of two moment-generating functions fordistributions with moments

âˆ’âˆ’âˆ’âˆ’âˆ’âˆ’âˆ’âˆ’âˆ’â†’âŸšđ‘ąđ‘˜âŸ©0â‰€đ‘˜â‰€đ‘š andâˆ’âˆ’âˆ’âˆ’âˆ’âˆ’âˆ’âˆ’â†’âŸšđ‘Łđ‘˜âŸ©0â‰€đ‘˜â‰€đ‘š , re-

spectively. We prove a composition property for momentsemirings.

Lemma 3.2. For all 𝑱, 𝑣 ∈ R, it holds that−−−−−−−−−−−−−−→⟹(𝑱 + 𝑣)đ‘˜âŸ©0â‰€đ‘˜â‰€đ‘š =

âˆ’âˆ’âˆ’âˆ’âˆ’âˆ’âˆ’âˆ’âˆ’â†’âŸšđ‘ąđ‘˜âŸ©0â‰€đ‘˜â‰€đ‘š ⊗

âˆ’âˆ’âˆ’âˆ’âˆ’âˆ’âˆ’âˆ’â†’âŸšđ‘Łđ‘˜âŸ©0â‰€đ‘˜â‰€đ‘š,

where 𝑱𝑛 is an abbreviation for∏𝑛𝑖=1 𝑱, for 𝑛 ∈ â„€+, 𝑱 ∈ R.

3.3 Inference RulesWe present the derivation system as a declarative programlogic that uses moment semirings to enable compositionalreasoning and moment-polymorphic recursion.

Interval-valued moment semirings. Our derivationsystem infers upper and lower bounds simultaneously, ratherthan separately, which is essential for non-monotone costs.Consider a program “tick(−1); 𝑆” and suppose that we have⟹1, 2, 5⟩ and ⟹1,−2, 5⟩ as the upper and lower bound on thefirst two moments of the cost for 𝑆 , respectively. If we onlyuse the upper bound, we derive ⟹1,−1, 1⟩ ⊗ ⟹1, 2, 5⟩ = ⟹1, 1, 2⟩,which is not an upper bound on the moments of the costfor the program; if the actual moments of the cost for 𝑆 are⟹1, 0, 5⟩, then the actual moments of the cost for “tick(−1); 𝑆”are ⟹1,−1, 1⟩ ⊗ ⟹1, 0, 5⟩ = ⟹1,−1, 4⟩ ̞≀ ⟹1, 1, 2⟩. Thus, in theanalysis, we instantiate moment semirings with the intervaldomain I . For the program “tick(−1); 𝑆”, its interval-valuedbound on the first two moments is ⟹[1, 1], [−1,−1], [1, 1]⟩ ⊗⟹[1, 1], [−2, 2], [5, 5]⟩ = ⟹[1, 1], [−3, 1], [2, 10]⟩.Template-based expected-potential functions. The ba-

sic approach to automated inference using potential func-tions is to introduce a template for the expected-potentialfunctions. Let us fix𝑚 ∈ ℕ as the degree of the target mo-ment. Because we useM(𝑚)

I -valued expected-potential func-tions whose range is vectors of intervals, the templates arevectors of intervals whose ends are represented symboli-cally. In this paper, we represent the ends of intervals bypolynomials in ℝ[VID] over program variables.More formally, we lift the interval semiring I to a sym-

bolic interval semiring PI by representing the ends of the𝑘-th interval by polynomials in ℝ𝑘𝑑 [VID] up to degree𝑘𝑑 for some fixed 𝑑 ∈ ℕ. Let M(𝑚)

PI be the 𝑚-th ordermoment semiring instantiated with the symbolic intervalsemiring. Then the potential annotation is represented as𝑄 =

−−−−−−−−−−−−−−→⟹[𝐿𝑘 ,𝑈𝑘 ]⟩0â‰€đ‘˜â‰€đ‘š ∈ M(𝑚)PI , where 𝐿𝑘 ’s and𝑈𝑘 ’s are poly-

nomials in ℝ𝑘𝑑 [VID]. 𝑄 defines anM(𝑚)I -valued expected-

potential function 𝜙𝑄 (𝜎) def=−−−−−−−−−−−−−−−−−−−−−→⟹[𝜎 (𝐿𝑘 ), 𝜎 (𝑈𝑘 )]⟩0â‰€đ‘˜â‰€đ‘š , where

𝜎 is a program state, and 𝜎 (𝐿𝑘 ) and 𝜎 (𝑈𝑘 ) are 𝐿𝑘 and 𝑈𝑘evaluated over 𝜎 , respectively.

Inference rules. We formalize our derivation system formoment analysis in a Hoare-logic style. The judgment has

7

Page 8: Central Moment Analysis for Cost Accumulators in

PLDI ’21, June 20–25, 2021, Virtual, UK Di Wang, Jan Hoffmann, and Thomas Reps

(Q-Tick)

𝑄 =−−−−−−−−−−−−−−→⟹[𝑐𝑘 , 𝑐𝑘 ]⟩0â‰€đ‘˜â‰€đ‘š ⊗ 𝑄 â€Č

Δ ⊱ℎ {Γ;𝑄} tick(𝑐) {Γ;𝑄 â€Č}

(Q-Sample)Γ = âˆ€đ‘„ ∈ supp(`đ· ) : Γâ€Č

𝑄 = đ”Œđ‘„âˆŒ`đ· [𝑄 â€Č]Δ ⊱ℎ {Γ;𝑄} đ‘„ ∌ đ· {Γâ€Č;𝑄 â€Č}

(Q-Loop)Δ ⊱ℎ {Γ ∧ 𝐿;𝑄} 𝑆 {Γ;𝑄}

Δ⊱ℎ {Γ;𝑄} while 𝐿 do 𝑆 od {Γ∧¬𝐿;𝑄}

(Q-Call-Mono)(Γ;𝑄, Γâ€Č;𝑄 â€Č) ∈ Δ𝑚 (𝑓 )

Δ⊱𝑚 {Γ;𝑄} call 𝑓 {Γâ€Č;𝑄 â€Č}(Q-Seq)

Δ ⊱ℎ {Γ;𝑄} 𝑆1 {Γâ€Č;𝑄 â€Č}Δ ⊱ℎ {Γâ€Č;𝑄 â€Č} 𝑆2 {Γâ€Čâ€Č;𝑄 â€Čâ€Č}Δ ⊱ℎ {Γ;𝑄} 𝑆1; 𝑆2 {Γâ€Čâ€Č;𝑄 â€Čâ€Č}

(Q-Call-Poly)ℎ < 𝑚 Δℎ (𝑓 ) = (Γ;𝑄1, Γ

â€Č;𝑄 â€Č1)

Δ ⊱ℎ+1 {Γ;𝑄2} 𝒟(𝑓 ) {Γâ€Č;𝑄 â€Č2}

Δ ⊱ℎ {Γ;𝑄1 ⊕ 𝑄2} call 𝑓 {Γâ€Č;𝑄 â€Č1 ⊕ 𝑄 â€Č

2}(Q-Prob)Δ ⊱ℎ {Γ;𝑄1} 𝑆1 {Γâ€Č;𝑄 â€Č} Δ ⊱ℎ {Γ;𝑄2} 𝑆2 {Γâ€Č;𝑄 â€Č}𝑄 = 𝑃 ⊕ 𝑅 𝑃 = ⟹[𝑝, 𝑝], [0, 0], · · · , [0, 0]⟩ ⊗ 𝑄1

𝑅 = ⟹[1 − 𝑝, 1 − 𝑝], [0, 0], · · · , [0, 0]⟩ ⊗ 𝑄2

Δ ⊱ℎ {Γ;𝑄} if prob(𝑝) then 𝑆1 else 𝑆2 fi {Γâ€Č;𝑄 â€Č}(Q-Weaken)Δ⊱ℎ {Γ0;𝑄0} 𝑆 {Γâ€Č0 ;𝑄 â€Č

0} Γ |= Γ0 Γâ€Č0 |= Γâ€Č Γ |= 𝑄 ⊒ 𝑄0 Γâ€Č0 |= 𝑄 â€Č0 ⊒ 𝑄 â€Č

Δ ⊱ℎ {Γ;𝑄} 𝑆 {Γâ€Č;𝑄 â€Č}

Figure 6. Selected inference rules of the derivation system.

the form Δ ⊱ℎ {Γ;𝑄} 𝑆 {Γâ€Č;𝑄 â€Č}, where 𝑆 is a statement,{Γ;𝑄} is a precondition, {Γâ€Č;𝑄 â€Č} is a postcondition, Δ =

âŸšÎ”đ‘˜âŸ©0â‰€đ‘˜â‰€đ‘š is a context of function specifications, andℎ ∈ â„€+

specifies some restrictions put on 𝑄,𝑄 â€Č that we will explainlater. The logical context Γ : (VID→ℝ)→{⊀,⊄} is a predi-cate that describes reachable states at a program point. Thepotential annotation 𝑄 ∈ M(𝑚)

PI specifies a map from pro-gram states to the moment semiring that is used to defineinterval-valued expected-potential functions. The semanticsof the triple {·;𝑄} 𝑆 {·;𝑄 â€Č} is that if the rest of the computa-tion after executing 𝑆 has its moments of the accumulatedcost bounded by 𝜙𝑄â€Č , then the whole computation has itsmoments of the accumulated cost bounded by𝜙𝑄 . The param-eter ℎ restricts all 𝑖-th-moment components in 𝑄,𝑄 â€Č, suchthat 𝑖 < ℎ, to be [0, 0]. We call such potential annotationsℎ-restricted; this construction is motivated by an observationfrom Ex. 2.6, where we illustrated the benefits of carryingout interprocedural analysis using an “elimination sequence”of annotations for recursive function calls, where the succes-sive annotations have a greater number of zeros, filling fromthe left. Function specifications are valid pairs of pre- andpost-conditions for all declared functions in a program. Foreach 𝑘 , such that 0â‰€đ‘˜ â‰€đ‘š, and each function 𝑓 , a valid spec-ification (Γ;𝑄, Γâ€Č;𝑄 â€Č) ∈ Δ𝑘 (𝑓 ) is justified by the judgmentΔ ⊱𝑘 {Γ;𝑄}𝒟(𝑓 ) {Γâ€Č;𝑄 â€Č}, where𝒟(𝑓 ) is the function bodyof 𝑓 , and𝑄,𝑄 â€Č are 𝑘-restricted. To perform context-sensitiveanalysis, a function can have multiple specifications.Fig. 6 presents some of the inference rules. The rule (Q-

Tick) is the only rule that deals with costs in a program. To

accumulate the moments of the cost, we use the ⊗ opera-tion in the moment semiring M(𝑚)

PI . The rule (Q-Sample)accounts for sampling statements. Because â€œđ‘„ âˆŒđ·â€ randomlyassigns a value to đ‘„ in the support of distributionđ· , we quan-tify đ‘„ out universally from the logical context. To compute𝑄 = đ”Œđ‘„âˆŒ`đ· [𝑄 â€Č], where đ‘„ is drawn from distribution đ· , weassume the moments for đ· are well-defined and computable,and substitute đ‘„đ‘– , 𝑖 ∈ ℕ with the corresponding moments in𝑄 â€Č. We make this assumption because every component of𝑄 â€Č is a polynomial over program variables. For example, ifđ· = uniform(−1, 2), we know the following factsđ”Œđ‘„âˆŒ`đ· [đ‘„0]=1,đ”Œđ‘„âˆŒ`đ· [đ‘„1]= 1/2,đ”Œđ‘„âˆŒ`đ· [đ‘„2]=1,đ”Œđ‘„âˆŒ`đ· [đ‘„3]= 5/4.Then for 𝑄 â€Č = ⟹[1, 1], [1 + đ‘„2, đ‘„đ‘Š2 + đ‘„3𝑩]⟩, by the linearity ofexpectations, we compute 𝑄 = đ”Œđ‘„âˆŒ`đ· [𝑄 â€Č] as follows:đ”Œđ‘„âˆŒ`đ· [𝑄 â€Č] = ⟹[1, 1], [đ”Œđ‘„âˆŒ`đ· [1 + đ‘„2],đ”Œđ‘„âˆŒ`đ· [đ‘„đ‘Š2 + đ‘„3𝑩]⟩

= ⟹[1, 1], [1 + đ”Œđ‘„âˆŒ`đ· [đ‘„2], 𝑩2đ”Œđ‘„âˆŒ`đ· [đ‘„] + đ‘Šđ”Œđ‘„âˆŒ`đ· [đ‘„3]]⟩= ⟹[1, 1], [2, 1/2 · 𝑩2 + 5/4 · 𝑩]⟩.

The other probabilistic rule (Q-Prob) deals with probabilisticbranching. Intuitively, if the moments of the execution of 𝑆1and 𝑆2 are 𝑞1 and 𝑞2, respectively, and those of the accumu-lated cost of the computation after the branch statement isbounded by 𝜙𝑄â€Č , then the moments for the whole computa-tion should be bounded by a “weighted average” of (𝑞1⊗𝜙𝑄â€Č)and (𝑞2 ⊗ 𝜙𝑄â€Č), with respect to the branching probability 𝑝 .We implement the weighted average by the combinationoperator ⊕ applied to ⟹[𝑝, 𝑝], [0, 0], · · · , [0, 0]⟩ ⊗ 𝑞1 ⊗ 𝜙𝑄â€Č

and ⟹[1 − 𝑝, 1 − 𝑝], [0, 0], · · · , [0, 0]⟩ ⊗ 𝑞2 ⊗ 𝜙𝑄â€Č , because the0-th moments denote probabilities.

The rules (Q-Call-Poly) and (Q-Call-Mono) handle func-tion calls. Recall that in Ex. 2.6, we use the ⊕ operator tocombine multiple potential functions for a function to reasonabout recursive function calls. The restriction parameter ℎis used to ensure that the derivation system only needs toreason about finitely many post-annotations for each callsite. In rule (Q-Call-Poly), whereℎ is smaller than the targetmoment𝑚, we fetch the pre- and post-condition 𝑄1, 𝑄

â€Č1 for

the function 𝑓 from the specification context Δℎ . We thencombine it with a frame of (ℎ + 1)-restricted potential an-notations 𝑄2, 𝑄

â€Č2 for the function 𝑓 . The frame is used to

account for the interval bounds on the moments for the com-putation after the function call for most non-tail-recursiveprograms. When ℎ reaches the target moment𝑚, we use therule (Q-Call-Mono) to reason moment-monomorphically,because setting ℎ to𝑚 + 1 implies that the frame can onlybe ⟹[0, 0], [0, 0], · · · , [0, 0]⟩.

The structural rule (Q-Weaken) is used to strengthen thepre-condition and relax the post-condition. The entailmentrelation Γ |= Γâ€Č states that the logical implication Γ =⇒ Γâ€Č

is valid. In terms of the bounds on higher moments for costaccumulators, if the triple {·;𝑄} 𝑆 {·;𝑄 â€Č} is valid, then wecan safely widen the intervals in the pre-condition 𝑄 andnarrow the intervals in the post-condition 𝑄 â€Č.

8

Page 9: Central Moment Analysis for Cost Accumulators in

Central Moment Analysis for Cost Accumulators in Probabilistic Programs PLDI ’21, June 20–25, 2021, Virtual, UK

1 func rdwalk() begin2 { đ‘„ <𝑑+2; ⟹[1, 1], [2(đ‘‘âˆ’đ‘„), 2(đ‘‘âˆ’đ‘„)+4],3 [4(đ‘‘âˆ’đ‘„)2 + 6(đ‘‘âˆ’đ‘„)−4, 4(đ‘‘âˆ’đ‘„)2+22(đ‘‘âˆ’đ‘„)+28]⟩ }4 if đ‘„ < 𝑑 then5 { đ‘„ <𝑑 ; ⟹[1, 1], [2(đ‘‘âˆ’đ‘„), 2(đ‘‘âˆ’đ‘„)+4],6 [4(đ‘‘âˆ’đ‘„)2+6(đ‘‘âˆ’đ‘„)−4, 4(đ‘‘âˆ’đ‘„)2+22(đ‘‘âˆ’đ‘„)+28]⟩ }7 𝑡 ∌ uniform(−1, 2);8 { đ‘„ <𝑑∧𝑡 ≀ 2; ⟹[1, 1], [2(đ‘‘âˆ’đ‘„âˆ’đ‘Ą)+1, 2(đ‘‘âˆ’đ‘„âˆ’đ‘Ą)+5],9 [4(đ‘‘âˆ’đ‘„âˆ’đ‘Ą)2 + 10(đ‘‘âˆ’đ‘„âˆ’đ‘Ą)−3, 4(đ‘‘âˆ’đ‘„âˆ’đ‘Ą)2+26(đ‘‘âˆ’đ‘„âˆ’đ‘Ą)+37)]⟩ }10 đ‘„ B đ‘„ + 𝑡 ;11 { đ‘„ <𝑑+2; ⟹[1, 1], [2(đ‘‘âˆ’đ‘„)+1, 2(đ‘‘âˆ’đ‘„)+5],12 [4(đ‘‘âˆ’đ‘„)2+10(đ‘‘âˆ’đ‘„)−3, 4(đ‘‘âˆ’đ‘„)2+26(đ‘‘âˆ’đ‘„)+37]⟩ }13 call rdwalk;14 { ⊀; ⟹[1, 1], [1, 1], [1, 1]⟩ }15 tick(1)16 { ⊀; ⟹[1, 1], [0, 0], [0, 0]⟩ }17 fi18 end

Figure 7. The rdwalk function with annotations for theinterval-bounds on the first and second moments.

Example 3.3. Fig. 7 presents the logical context and thecomplete potential annotation for the first and second mo-ments for the cost accumulator tick of the rdwalk functionfrom Ex. 2.1. Similar to the reasoning in Ex. 2.6, we can jus-tify the derivation using moment-polymorphic recursionand the moment bounds for rdwalk with post-annotations⟹[0, 0], [1, 1], [1, 1]⟩ and ⟹[0, 0], [0, 0], [2, 2]⟩.

3.4 Automatic Linear-Constraint GenerationWe adapt existing techniques [9, 31] to automate our in-ference system by (i) using an abstract interpreter to inferlogical contexts, (ii) generating templates and linear con-straints by inductively applying the derivation rules to theanalyzed program, and (iii) employing an off-the-shelf LPsolver to discharge the linear constraints. During the genera-tion phase, the coefficients of monomials in the polynomialsfrom the ends of the intervals in every qualitative context𝑄 ∈ M(𝑚)

PI are recorded as symbolic names, and the inequal-ities among those coefficients—derived from the inferencerules in Fig. 6—are emitted to the LP solver.

Generating linear constraints. Fig. 8 demonstrates thegeneration process for some of the bounds in Fig. 3. Let đ”đ‘˜ bea vector of monomials over program variables VID of degreeup to 𝑘 . Then a polynomial

âˆ‘đ‘âˆˆđ”đ‘˜ 𝑞𝑏 · 𝑏, where 𝑞𝑏 ∈ ℝ for

all 𝑏 ∈ đ”đ‘˜ , can be represented as a vector of its coefficients(𝑞𝑏)đ‘âˆˆđ”đ‘˜ . We denote coefficient vectors by uppercase letters,while we use lowercase letters as names of the coefficients.We also assume that the degree of the polynomials for the𝑘-th moments is up to 𝑘 .

For (Q-Tick), we generate constraints that correspond tothe composition operation ⊗ of the moment semiring. For

example, the second-moment component should satisfyâˆ‘ïžđ‘âˆˆđ”2

𝑡𝑡𝑘𝑏

· 𝑏 = the second-moment component of((1, 1, 1)

⊗ (âˆ‘ïžđ‘âˆˆđ”0

𝑱𝑡𝑘𝑏

· 𝑏,âˆ‘ïžđ‘âˆˆđ”1

𝑣𝑡𝑘𝑏

· 𝑏,âˆ‘ïžđ‘âˆˆđ”2

đ‘€đ‘Ąđ‘˜đ‘

· 𝑏))=

âˆ‘ïžđ‘âˆˆđ”2

đ‘€đ‘Ąđ‘˜đ‘

· 𝑏 + 2 Â·âˆ‘ïžđ‘âˆˆđ”1

𝑣𝑡𝑘𝑏

· 𝑏 +âˆ‘ïžđ‘âˆˆđ”0

𝑱𝑡𝑘𝑏

· 𝑏.

Then we extract 𝑡𝑡𝑘1 = đ‘€đ‘Ąđ‘˜1 + 2𝑣𝑡𝑘1 + 𝑱𝑡𝑘1 for 𝑏 = 1, andđ‘Ąđ‘Ąđ‘˜đ‘„ = đ‘€đ‘Ąđ‘˜đ‘„ + 2đ‘Łđ‘Ąđ‘˜đ‘„ for 𝑏 = đ‘„ , etc. For (Q-Sample), we generateconstraints to perform “partial evaluation” on the polynomi-als by substituting 𝑟 with the moments of uniform(−1, 2).As we discussed in §3.3, let đ· denote uniform(−1, 2), thenđ”Œđ‘ŸâˆŒđ· [đ‘€đ‘ đ‘Žđ‘Ÿ · 𝑟 ] = đ‘€đ‘ đ‘Žđ‘Ÿ · đ”Œđ‘ŸâˆŒđ· [𝑟 ] = 1/2 Â·đ‘€đ‘ đ‘Žđ‘Ÿ , đ”Œđ‘ŸâˆŒđ· [đ‘€đ‘ đ‘Žđ‘Ÿ 2 · 𝑟 2] =đ‘€đ‘ đ‘Žđ‘Ÿ 2

· đ”Œđ‘ŸâˆŒđ· [𝑟 2] = 1 · đ‘€đ‘ đ‘Žđ‘Ÿ 2. Then we generate a constraint

𝑡𝑠𝑎1 = đ‘€đ‘ đ‘Ž1 + 1/2 Â·đ‘€đ‘ đ‘Žđ‘Ÿ + 1 Â·đ‘€đ‘ đ‘Žđ‘Ÿ 2

for 𝑡𝑠𝑎1 .The loop rule (Q-Loop) involves constructing loop in-

variants 𝑄 , which is in general a non-trivial problem forautomated static analysis. Instead of computing the loopinvariant 𝑄 explicitly, our system represents 𝑄 directly asa template with unknown coefficients, then uses 𝑄 as thepost-annotation to analyze the loop body and obtain a pre-annotation, and finally generates linear constraints that in-dicate the pre-annotation equals to 𝑄 .The structural rule (Q-Weaken) can be applied at any

point during the derivation. In our implementation, we applyit where the control flow has a branch, because differentbranches might have different costs. To handle the judgmentΓ |= 𝑄 ⊒ 𝑄 â€Č, i.e., to generate constraints that ensure oneinterval is always contained in another interval, where theends of the intervals are polynomials, we adapt the idea ofrewrite functions [9, 31]. Intuitively, to ensure that [𝐿1,𝑈1] ⊒[𝐿2,𝑈2], i.e., 𝐿1 ≀ 𝐿2 and𝑈2 ≀ 𝑈1, under the logical contextΓ, we generate constraints indicating that there exist twopolynomials𝑇1,𝑇2 that are always nonnegative under Γ, suchthat 𝐿1 = 𝐿2+𝑇1 and𝑈1 = 𝑈2−𝑇2. Here,𝑇1 and𝑇2 are like slackvariables, except that because all quantities are polynomials,they are too (i.e., slack polynomials). In our implementation,Γ is a set of linear constraints over program variables ofthe form E ≄ 0 , then we can represent 𝑇1,𝑇2 by conicalcombinations (i.e., linear combinations with nonnegativescalars) of expressions E in Γ.

Solving linear constraints. The LP solver not only findsassignments to the coefficients that satisfy the constraints, itcan also optimize a linear objective function. In our central-moment analysis, we construct an objective function thattries to minimize imprecision. For example, let us considerupper bounds on the variance. We randomly pick a con-crete valuation of program variables that satisfies the pre-condition (e.g., 𝑑 > 0 in Fig. 2), and then substitute programvariables with the concrete valuation in the polynomial forthe upper bound on the variance (obtained from bounds on

9

Page 10: Central Moment Analysis for Cost Accumulators in

PLDI ’21, June 20–25, 2021, Virtual, UK Di Wang, Jan Hoffmann, and Thomas Reps

𝑝𝑡𝑘1 = 𝑱𝑡𝑘1 𝑞𝑡𝑘1 = 𝑱𝑡𝑘1 + 𝑣𝑡𝑘1 đ‘žđ‘Ąđ‘˜đ‘„ = đ‘Łđ‘Ąđ‘˜đ‘„ 𝑞𝑡𝑘𝑁 = 𝑣𝑡𝑘𝑁 𝑞𝑡𝑘𝑟 = 𝑣𝑡𝑘𝑟𝑡𝑡𝑘1 = đ‘€đ‘Ąđ‘˜1 + 2𝑣𝑡𝑘1 + 𝑱𝑡𝑘1 đ‘Ąđ‘Ąđ‘˜đ‘„ = đ‘€đ‘Ąđ‘˜đ‘„ + 2đ‘Łđ‘Ąđ‘˜đ‘„ 𝑡𝑡𝑘

đ‘„2= đ‘€đ‘Ąđ‘˜

đ‘„2· · ·

Δ ⊱ {⊀; (𝑃𝑡𝑘 , 𝑄𝑡𝑘 ,𝑇 𝑡𝑘 )} tick(1) {⊀; (𝑈 𝑡𝑘 ,𝑉 𝑡𝑘 ,𝑊 𝑡𝑘 )}(Q-Tick)

𝑝𝑠𝑎1 = 𝑱𝑠𝑎1 𝑞𝑠𝑎1 = 𝑣𝑠𝑎1 + 1/2 · 𝑣𝑠𝑎𝑟 đ‘žđ‘ đ‘Žđ‘„ = đ‘Łđ‘ đ‘Žđ‘„ 𝑞𝑠𝑎𝑁 = 𝑣𝑠𝑎𝑁 𝑞𝑠𝑎𝑟 = 0𝑡𝑠𝑎1 = đ‘€đ‘ đ‘Ž1 + 1/2 Â·đ‘€đ‘ đ‘Žđ‘Ÿ + 1 Â·đ‘€đ‘ đ‘Ž

𝑟 2đ‘Ąđ‘ đ‘Žđ‘„ = đ‘€đ‘ đ‘Žđ‘„ + 1/2 Â·đ‘€đ‘ đ‘Žđ‘Ÿ Â·đ‘„ 𝑡𝑠𝑎

đ‘„2= đ‘€đ‘ đ‘Ž

đ‘„2· · ·

Δ ⊱ {đ‘„ < 𝑁 ; (𝑃𝑠𝑎, 𝑄𝑠𝑎,𝑇 𝑠𝑎)} 𝑟 ∌ uniform(−1, 2) {đ‘„ < 𝑁 ∧ 𝑟 ≀ 2; (𝑈 𝑠𝑎,𝑉 𝑠𝑎,𝑊 𝑠𝑎)} (Q-Sample)

Figure 8. Generate linear constraints, guided by inference rules.

the raw moments). The resulting linear combination of coef-ficients, which we set as the objective function, stands for thevariance under the concrete valuation. Thus, minimizing theobjective function produces the most precise upper boundon the variance under the specific concrete valuation. Also,we can extract a symbolic upper bound on the variance usingthe assignments to the coefficients. Because the derivation ofthe bounds only uses the given pre-condition, the symbolicbounds apply to all valuations that satisfy the pre-condition.

4 Soundness of Higher-Moment AnalysisIn this section, we study the soundness of our derivation sys-tem for higher-moment analysis. We first present a Markov-chain semantics for the probabilistic programming languageAppl to reason about how stepwise costs contribute to theglobal accumulated cost (§4.1). We then formulate higher-moment analysis with respect to the semantics and provethe soundness of our derivation system for higher-momentanalysis based on a recent extension to the Optional StoppingTheorem (§4.2). Finally, we sketch the algorithmic approachfor ensuring the soundness of our analysis (§4.3).

4.1 A Markov-Chain SemanticsOperational semantics. We start with a small-step op-

erational semantics with continuations, which we will uselater to construct the Markov-chain semantics. We follow adistribution-based approach [7, 25] to define an operationalcost semantics for Appl. Full details of the semantics areincluded in Appendix B. A program configuration 𝜎 ∈ ÎŁ isa quadruple âŸšđ›Ÿ, 𝑆, đŸ, đ›ŒâŸ© where đ›Ÿ : VID → ℝ is a programstate that maps variables to values, 𝑆 is the statement beingexecuted, đŸ is a continuation that describes what remains tobe done after the execution of 𝑆 , and đ›Œ ∈ ℝ is the global costaccumulator. An execution of an Appl program ⟹𝒟, 𝑆main⟩is initialized with ⟹__.0, 𝑆main,Kstop, 0⟩, and the termina-tion configurations have the form ⟹_, skip,Kstop, _⟩, whereKstop is an empty continuation.

Different from a standard semantics where each programconfiguration steps to at most one new configuration, aprobabilistic semantics may pick several different new con-figurations. The evaluation relation for Appl has the form𝜎 ↩→ ` where ` ∈ đ”»(ÎŁ) is a probability measure over con-figurations. Below are two example rules. The rule (E-Prob)

constructs a distribution whose support has exactly twoelements, which stand for the two branches of the proba-bilistic choice. We write 𝛿 (𝜎) for the Dirac measure at 𝜎 ,defined as _𝐮.[𝜎 ∈ 𝐮] where 𝐮 is a measurable subset ofÎŁ. We also write 𝑝 · `1 + (1 − 𝑝) · `2 for a convex combi-nation of measures `1 and `2 where 𝑝 ∈ [0, 1], defined as_𝐮.𝑝 · `1 (𝐮) + (1 − 𝑝) · `2 (𝐮). The rule (E-Sample) “pushes”the probability distribution of đ· to a distribution over post-sampling program configurations.

𝑆 = if prob(𝑝) then 𝑆1 else 𝑆2 fiâŸšđ›Ÿ, 𝑆, đŸ, đ›ŒâŸ© ↩→ 𝑝 · 𝛿 (âŸšđ›Ÿ, 𝑆1, đŸ, đ›ŒâŸ©) + (1 − 𝑝) · 𝛿 (âŸšđ›Ÿ, 𝑆2, đŸ, đ›ŒâŸ©)

(E-Prob)

âŸšđ›Ÿ, đ‘„ âˆŒđ·,đŸ, đ›ŒâŸ© ↩→ _𝐮.`đ· ({𝑟 | âŸšđ›Ÿ [đ‘„ ↩→𝑟 ], skip, đŸ, đ›ŒâŸ© ∈ 𝐮}) (E-Sample)

Example 4.1. Suppose that a random sampling statementis being executed, i.e., the current configuration is

⟹{𝑡 ↩→ 𝑡0}, (𝑡 ∌ uniform(−1, 2)), đŸ0, đ›Œ0⟩.The probability measure for the uniform distribution is_𝑂.

âˆ«đ‘‚

[−1â‰€đ‘„â‰€2]3 đ‘‘đ‘„ . Thus, by the rule (E-Sample), we derive

the post-sampling probability measure over configurations:_𝐮.

∫ℝ[⟹{𝑡 ↩→ 𝑟 }, skip, đŸ0, đ›Œ0⟩ ∈ 𝐮] · [−1â‰€đ‘Ÿ ≀2]

3 𝑑𝑟 .

A Markov-chain semantics. In this work, we harnessMarkov-chain-based reasoning [24, 33] to develop a Markov-chain cost semantics for Appl, based on the evaluation rela-tion 𝜎 ↩→ `. An advantage of this approach is that it allowsus to study how the cost of every single evaluation step con-tributes to the accumulated cost at the exit of the program.Details of this semantics are included in Appendix C.Let (Ω,F ,ℙ) be the probability space where Ω def

= ÎŁâ„€+ isthe set of all infinite traces over program configurations,F isa 𝜎-algebra on Ω, and ℙ is a probability measure on (Ω,F)obtained by the evaluation relation 𝜎 ↩→ ` and the initial con-figuration ⟹__.0, 𝑆main,Kstop, 0⟩. Intuitively, ℙ specifies theprobability distribution over all possible executions of a prob-abilistic program. The probability of an assertion \ with re-spect to ℙ, written ℙ[\ ], is defined as ℙ({𝜔 | \ (𝜔) is true}).To formulate the accumulated cost at the exit of the pro-

gram, we define the stopping time 𝑇 : Ω → â„€+ âˆȘ {∞} of aprobabilistic program as a random variable on the probabilityspace (Ω,F ,ℙ) of program traces:

𝑇 (𝜔) def= inf{𝑛 ∈ â„€+ | 𝜔𝑛 = ⟹_, skip,Kstop, _⟩},

10

Page 11: Central Moment Analysis for Cost Accumulators in

Central Moment Analysis for Cost Accumulators in Probabilistic Programs PLDI ’21, June 20–25, 2021, Virtual, UK

i.e.,𝑇 (𝜔) is the number of evaluation steps before the trace𝜔reaches some termination configuration ⟹_, skip,Kstop, _⟩.We define the accumulated cost 𝐮𝑇 : Ω → ℝ with respect tothe stopping time 𝑇 as

𝐮𝑇 (𝜔) def= 𝐮𝑇 (𝜔) (𝜔),

where 𝐮𝑛 : Ω → ℝ captures the accumulated cost at the𝑛-th evaluation step for 𝑛 ∈ â„€+, which is defined as

𝐮𝑛 (𝜔) def= đ›Œđ‘› where 𝜔𝑛 = ⟹_, _, _, đ›Œđ‘›âŸ©.

The𝑚-th moment of the accumulated cost is given by theexpectation đ”Œ[𝐮𝑚

𝑇] with respect to ℙ.

4.2 Soundness of the Derivation SystemProofs for this section are included in Appendices D to F.

The expected-potential method for moment analysis.We fix a degree 𝑚 ∈ ℕ and let M(𝑚)

I be the 𝑚-th ordermoment semiring instantiated with the interval semiring I .We now defineM(𝑚)

I -valued expected-potential functions.

Definition 4.2. A measurable map 𝜙 : Σ → M(𝑚)I is said

to be an expected-potential function if(i) 𝜙 (𝜎) = 1 if 𝜎 = ⟹_, skip,Kstop, _⟩, and(ii) 𝜙 (𝜎) ⊒ đ”ŒđœŽâ€Č∌↊→(𝜎) [

−−−−−−−−−−−−−−−−−−−−−−−−−−−−→([(đ›Œ â€Č − đ›Œ)𝑘 , (đ›Œ â€Č − đ›Œ)𝑘 ])0â‰€đ‘˜â‰€đ‘š ⊗ 𝜙 (𝜎 â€Č)]where 𝜎 = ⟹_, _, _, đ›ŒâŸ©, 𝜎 â€Č = ⟹_, _, _, đ›Œ â€Č⟩ for all 𝜎 ∈ ÎŁ.

Intuitively, 𝜙 (𝜎) is an interval bound on the moments forthe accumulated cost of the computation that continues fromthe configuration 𝜎 . We define Ω𝑛 and Y𝑛 , where 𝑛 ∈ â„€+, tobeM(𝑚)

I -valued random variables on the probability space(Ω,F ,ℙ) of the Markov-chain semantics as

Ω𝑛 (𝜔) def= 𝜙 (𝜔𝑛),Y𝑛 (𝜔) def

=−−−−−−−−−−−−−−−−−−−−−−−−→⟹[𝐮𝑛 (𝜔)𝑘 , 𝐮𝑛 (𝜔)𝑘 ]⟩0â‰€đ‘˜â‰€đ‘šâŠ—ÎŠđ‘› (𝜔).

In the definition of Y𝑛 , we use ⊗ to compose the powers ofthe accumulated cost at step 𝑛 and the expected potentialfunction that stands for the moments of the accumulatedcost for the rest of the computation.

Lemma 4.3. By the properties of potential functions, we canprove that đ”Œ[Y𝑛+1 | Y𝑛] ⊑ Y𝑛 almost surely, for all 𝑛 ∈ â„€+.

We call {Y𝑛}đ‘›âˆˆâ„€+ a moment invariant. Our goal is toestablish that đ”Œ[Y𝑇 ] ⊑ đ”Œ[Y0], i.e., the initial interval-valued potential đ”Œ[Y0] = đ”Œ[1 ⊗ Ί0] = đ”Œ[Ί0] bracketsthe higher moments of the accumulated cost đ”Œ[Y𝑇 ] =

đ”Œ[−−−−−−−−−−−−−−−→⟹[𝐮𝑘

𝑇, 𝐮𝑘

𝑇]⟩0â‰€đ‘˜â‰€đ‘š ⊗ 1] =

−−−−−−−−−−−−−−−−−−−−−−→⟹[đ”Œ[𝐮𝑘

𝑇],đ”Œ[𝐮𝑘

𝑇]]⟩0â‰€đ‘˜â‰€đ‘š .

Soundness. The soundness of the derivation system isproved with respect to the Markov-chain semantics. Let∄−−−−−−−−−−−−−−→⟚[𝑎𝑘 , 𝑏𝑘 ]⟩0â‰€đ‘˜â‰€đ‘š ∄∞ def

= max0â‰€đ‘˜â‰€đ‘š{max{|𝑎𝑘 |, |𝑏𝑘 |}}.Theorem 4.4. Let ⟹𝒟, 𝑆main⟩ be a probabilistic program. Sup-pose Δ ⊱ {Γ;𝑄} 𝑆main {Γâ€Č; 1}, where 𝑄 ∈ M(𝑚)

PI and theends of the 𝑘-th interval in 𝑄 are polynomials in ℝ𝑘𝑑 [VID].Let {Y𝑛}đ‘›âˆˆâ„€+ be the moment invariant extracted from the

Markov-chain semantics with respect to the derivation ofΔ ⊱ {Γ;𝑄} 𝑆main {Γâ€Č; 1}. If the following conditions hold:(i) đ”Œ[𝑇𝑚𝑑 ] < ∞, and(ii) there exists đ¶ ≄ 0 such that for all 𝑛 ∈ â„€+, ∄Y𝑛 ∄∞ ≀

đ¶ · (𝑛 + 1)𝑚𝑑 almost surely,

Then−−−−−−−−−−−−−−−−−−−−−−→⟹[đ”Œ[𝐮𝑘

𝑇],đ”Œ[𝐮𝑘

𝑇]]⟩0â‰€đ‘˜â‰€đ‘š ⊑ 𝜙𝑄 (__.0).

The intuitive meaning of−−−−−−−−−−−−−−−−−−−−−−→⟹[đ”Œ[𝐮𝑘

𝑇],đ”Œ[𝐮𝑘

𝑇]]⟩0â‰€đ‘˜â‰€đ‘š ⊑

𝜙𝑄 (__.0) is that the moment đ”Œ[𝐮𝑘𝑇] of the accumulated cost

upon program termination is bounded by the interval in the𝑘 th-moment component of 𝜙𝑄 (__.0), where 𝑄 is the quanti-tative context and __.0 is the initial state.

As we discussed in §2.2 and Ex. 2.7, the expected-potentialmethod is not always sound for deriving bounds on highermoments for cost accumulators in probabilistic programs.The extra conditions Thm. 4.4(i) and (ii) impose constraintson the analyzed program and the expected-potential func-tion, which allow us to reduce the soundness to the optionalstopping problem from probability theory.

Optional stopping. Let us represent the moment invari-ant {Y𝑛}đ‘›âˆˆâ„€+ as

{⟹[𝐿 (0)𝑛 ,𝑈 (0)𝑛 ], [𝐿 (1)𝑛 ,𝑈 (1)

𝑛 ], · · · , [𝐿 (𝑚)𝑛 ,𝑈 (𝑚)

𝑛 ]⟩}đ‘›âˆˆâ„€+ ,

where 𝐿 (𝑘)𝑛 ,𝑈 (𝑘)𝑛 : Ω → ℝ are real-valued random vari-

ables on the probability space (Ω,F ,ℙ) of the Markov-chainsemantics, for 𝑛 ∈ â„€+, 0 ≀ 𝑘 ≀ 𝑚. We then have the obser-vations below as direct corollaries of Lem. 4.3:‱ For any 𝑘 , the sequence {𝑈 (𝑘)

𝑛 }đ‘›âˆˆâ„€+ satisfies đ”Œ[𝑈 (𝑘)𝑛+1 |

𝑈 (𝑘)𝑛 ] ≀ 𝑈 (𝑘)

𝑛 almost surely, for all 𝑛 ∈ â„€+, and we wantto find sufficient conditions for đ”Œ[𝑈 (𝑘)

𝑇] ≀ đ”Œ[𝑈 (𝑘)

0 ].‱ For any 𝑘 , the sequence {𝐿 (𝑘)𝑛 }đ‘›âˆˆâ„€+ satisfies đ”Œ[𝐿 (𝑘)

𝑛+1 |𝐿 (𝑘)𝑛 ] ≄ 𝐿 (𝑘)𝑛 almost surely, for all 𝑛 ∈ â„€+, and we want tofind sufficient conditions for đ”Œ[𝐿 (𝑘)

𝑇] ≄ đ”Œ[𝐿 (𝑘)0 ].

These kinds of questions can be reduced to optional stop-ping problem from probability theory. Recent research [1,19, 38, 43] has used the Optional Stopping Theorem (OST)from probability theory to establish sufficient conditions forthe soundness for analysis of probabilistic programs. How-ever, the classic OST turns out to be not suitable for higher-moment analysis. We extend OST with a new sufficient con-dition that allows us to prove Thm. 4.4. We discuss the detailsof our extended OST in a companion paper [41]; in this work,we focus on the derivation system for central moments. Wealso include a brief discussion on OST in Appendix E.

4.3 An Algorithm for Checking Soundness CriteriaTermination Analysis. We reuse our system for auto-

matically deriving higher moments, which we developed in§3.3 and §3.4, for checking if đ”Œ[𝑇𝑚𝑑 ] < ∞ (Thm. 4.4(i)). Toreason about termination time, we assume that every pro-gram statement increments the cost accumulator by one. For

11

Page 12: Central Moment Analysis for Cost Accumulators in

PLDI ’21, June 20–25, 2021, Virtual, UK Di Wang, Jan Hoffmann, and Thomas Reps

example, the inference rule (Q-Sample) becomesΓ = âˆ€đ‘„ ∈ supp(`đ· ) : Γâ€Č 𝑄 = ⟹1, 1, · · · , 1⟩ ⊗ đ”Œđ‘„âˆŒ`đ· [𝑄 â€Č]

Δ ⊱ {Γ;𝑄} đ‘„ ∌ đ· {Γâ€Č;𝑄 â€Č}However, we cannot apply Thm. 4.4 for the soundness of thetermination-time analysis, because that would introduce acircular dependence. Instead, we use a different proof tech-nique to reason about đ”Œ[𝑇𝑚𝑑 ], taking into account themono-tonicity of the runtimes. Because upper-bound analysis ofhigher moments of runtimes has been studied by Kura et al.[26], we skip the details, but include them in Appendix G.

Boundedness of âˆ„đ‘Œđ‘› ∄∞, 𝑛 ∈ â„€+. To ensure that the con-dition in Thm. 4.4(ii) holds, we check if the analyzed programsatisfies the bounded-update property: every (deterministicor probabilistic) assignment to a program variable updatesthe variable with a change bounded by a constant đ¶ almostsurely. Then the absolute value of every program variable atevaluation step 𝑛 can be bounded by đ¶ · 𝑛 = 𝑂 (𝑛). Thus, apolynomial up to degree ℓ ∈ ℕ over program variables can bebounded by𝑂 (𝑛ℓ ) at evaluation step 𝑛. As observed byWanget al. [43], bounded updates are common in practice.

5 Tail-Bound AnalysisOne application of our central-moment analysis is to boundthe probability that the accumulated cost deviates from somegiven quantity. In this section, we sketch how we producethe tail bounds shown in Fig. 1(c).There are a lot of concentration-of-measure inequalities

in probability theory [13]. Among those, one of the mostimportant is Markov’s inequality:

Proposition 5.1. If 𝑋 is a nonnegative random variable and𝑎 > 0, then ℙ[𝑋 ≄ 𝑎] ≀ đ”Œ[𝑋𝑘 ]

𝑎𝑘for any 𝑘 ∈ ℕ.

Recall that Fig. 1(b) presents upper bounds on the rawmoments đ”Œ[tick] ≀ 2𝑑 + 4 and đ”Œ[tick2] ≀ 4𝑑2 + 22𝑑 + 28for the cost accumulator tick. With Markov’s inequality, wederive the following tail bounds:

ℙ[tick ≄ 4𝑑] ≀ đ”Œ[tick]4𝑑 ≀ 2𝑑 + 4

4𝑑𝑑→∞−−−−−→ 1

2 , (8)

ℙ[tick ≄ 4𝑑] ≀ đ”Œ[tick2](4𝑑)2 ≀ 4𝑑2 + 22𝑑 + 28

16𝑑2𝑑→∞−−−−−→ 1

4 . (9)

Note that (9) provides an asymptotically more precise boundon ℙ[tick ≄ 4𝑑] than (8) does, when 𝑑 approaches infinity.

Central-moment analysis can obtain an even more precisetail bound. As presented in Fig. 1(b), our analysis derives𝕍 [tick] ≀ 22𝑑 + 28 for the variance of tick. We can nowemploy concentration inequalities that involve variances ofrandom variables. Recall Cantelli’s inequality:

Proposition 5.2. If 𝑋 is a random variable and 𝑎 > 0, thenℙ[đ‘‹âˆ’đ”Œ[𝑋 ] â‰„đ‘Ž] ≀ 𝕍 [𝑋 ]

𝕍 [𝑋 ]+𝑎2 and ℙ[đ‘‹âˆ’đ”Œ[𝑋 ] â‰€âˆ’đ‘Ž] ≀ 𝕍 [𝑋 ]𝕍 [𝑋 ]+𝑎2 .

With Cantelli’s inequality, we obtain the following tailbound, where we assume 𝑑 ≄ 2:

ℙ[tick ≄ 4𝑑] = ℙ[tick − (2𝑑 + 4) ≄ 2𝑑 − 4]

≀ ℙ[tick − đ”Œ[tick] ≄ 2𝑑 − 4] ≀ 𝕍 [tick]𝕍 [tick] + (2𝑑 − 4)2

= 1 − (2𝑑 − 4)2𝕍 [tick] + (2𝑑 − 4)2 ≀ 22𝑑 + 28

4𝑑2 + 6𝑑 + 44𝑑→∞−−−−−→ 0.

(10)

For all 𝑑 ≄ 15, (10) gives a more precise bound than both(8) and (9). It is also clear from Fig. 1(c), where we plot thethree tail bounds (8), (9), and (10), that the asymptoticallymost precise bound is the one obtained via variances.

In general, for higher central moments, we employ Cheby-shev’s inequality to derive tail bounds:

Proposition 5.3. If 𝑋 is a random variable and 𝑎 > 0, thenℙ[|𝑋 − đ”Œ[𝑋 ] | ≄ 𝑎] ≀ đ”Œ[ (đ‘‹âˆ’đ”Œ[𝑋 ])2𝑘 ]

𝑎2𝑘for any 𝑘 ∈ ℕ.

In our experiments, we use Chebyshev’s inequality toderive tail bounds from the fourth central moments. We willshow in Fig. 9 that these tail bounds can be more precisethan those obtained from both raw moments and variances.

6 Implementation and ExperimentsImplementation. Our tool is implemented in OCaml, and

consists of about 5,300 LOC. The tool works on imperativearithmetic probabilistic programs using a CFG-based IR [40].The language supports recursive functions, continuous dis-tributions, unstructured control-flow, and local variables. Toinfer the bounds on the central moments for a cost accumu-lator in a program, the user needs to specify the order ofthe analyzed moment, and a maximal degree for the poly-nomials to be used in potential-function templates. UsingAPRON [22], we implemented an interprocedural numericanalysis to infer the logical contexts used in the derivation.We use the off-the-shelf solver Gurobi [28] for LP solving.

Evaluation setup. We evaluated our tool to answer thefollowing three research questions:1. How does the raw-moment inference part of our tool

compare to existing techniques for expected-cost boundanalysis [31, 43]?

2. How does our tool compare to the state of the art in tail-probability analysis (which is based only on higher rawmoments [26])?

3. How scalable is our tool? Can it analyze programs withmany recursive functions?For the first question, we collected a broad suite of chal-

lenging examples from related work [26, 31, 43] with dif-ferent loop and recursion patterns, as well as probabilisticbranching, discrete sampling, and continuous sampling. Ourtool achieved comparable precision and efficiency with theprior work on expected-cost bound analysis [31, 43]. Thedetails are included in Appendix H.

12

Page 13: Central Moment Analysis for Cost Accumulators in

Central Moment Analysis for Cost Accumulators in Probabilistic Programs PLDI ’21, June 20–25, 2021, Virtual, UK

For the second question, we evaluated our tool on the com-plete benchmarks from Kura el al. [26]. We also conducted acase study of a timing-attack analysis for a program providedby DARPA during engagements of the STAC program [15],where central moments are more useful than raw momentsto bound the success probability of an attacker. We includethe case study in Appendix I.

For the third question, we conducted case studies on twosets of synthetic benchmark programs:‱ coupon-collector programs with𝑁 coupons (𝑁 ∈ [1, 103]),where each program is implemented as a set of tail-recursive functions, each of which represents a state ofcoupon collection, i.e., the number of coupons collectedso far; and

‱ random-walk programs with 𝑁 consecutive one-dimensional random walks (𝑁 ∈ [1, 103]), each of whichstarts at a position that equals the number of steps takenby the previous random walk to reach the ending position(the origin). Each program is implemented as a set ofnon-tail-recursive functions, each of which represents arandom walk. The random walks in the same programcan have different transition probabilities.

The largest synthetic program has nearly 16,000 LOC. Wethen ran our tool to derive an upper bound on the fourth(resp., second) central moment of the runtime for eachcoupon-collector (resp., random-walk) program.The experiments were performed on a machine with an

Intel Core i7 3.6GHz processor and 16GB of RAM undermacOS Catalina 10.15.7.

Results. Some of the evaluation results to answer the sec-ond research question are presented in Tab. 1. The programs(1-1) and (1-2) are coupon-collector problems with a totalof two and four coupons, respectively. The programs (2-1)and (2-2) are one-dimensional random walks with integer-valued and real-valued coordinates, respectively. We omitthree other programs here but include the full results in Ap-pendix H. The table contains the inferred upper bounds onthe moments for runtimes of these programs, and the run-ning times of the analyses. We compared our results withKura et al.’s inference tool for raw moments [26]. Our toolis as precise as, and sometimes more precise than the priorwork on all the benchmark programs. Meanwhile, our tool isable to infer an upper bound on the raw moments of degreeup to four on all the benchmarks, while the prior work re-ports failure on some higher moments for the random-walkprograms. In terms of efficiency, our tool completed eachexample in less than 10 seconds, while the prior work tookmore than a fewminutes on some programs. One reason whyour tool is more efficient is that we always reduce higher-moment inference with non-linear polynomial templates toefficient LP solving, but the prior work requires semidefiniteprogramming (SDP) for polynomial templates.

Table 1. Upper bounds on the raw/central moments of run-times, with comparison to Kura et al. [26]. “T/O” stands fortimeout after 30 minutes. “N/A” means that the tool is notapplicable. “-” indicates that the tool fails to infer a bound.Entries with more precise results or less analysis time aremarked in bold. Full results are included in Appendix H.

program moment this work Kura et al. [26]upper bnd. time (sec) upper bnd. time (sec)

(1-1)

2nd raw 201 0.019 201 0.0153rd raw 3829 0.019 3829 0.0204th raw 90705 0.023 90705 0.027

2nd central 32 0.029 N/A N/A4th central 9728 0.058 N/A N/A

(1-2)

2nd raw 2357 1.068 3124 0.0373rd raw 148847 1.512 171932 0.0624th raw 11285725 1.914 12049876 0.096

2nd central 362 3.346 N/A N/A4th central 955973 9.801 N/A N/A

(2-1)

2nd raw 2320 0.016 2320 11.3803rd raw 691520 0.018 - 16.0564th raw 340107520 0.021 - 23.414

2nd central 1920 0.026 N/A N/A4th central 289873920 0.049 N/A N/A

(2-2)

2nd raw 8375 0.022 8375 38.4633rd raw 1362813 0.028 - 73.4084th raw 306105209 0.035 - 141.072

2nd central 5875 0.029 N/A N/A4th central 447053126 0.086 N/A N/A

Besides raw moments, our tool is also capable of inferringupper bounds on the central moments of runtimes for thebenchmarks. To evaluate the quality of the inferred centralmoments, Fig. 9 plots the upper bounds of tail probabilitieson runtimes 𝑇 obtained by Kura et al. [26], and those byour central-moment analysis. Specifically, the prior workuses Markov’s inequality (Prop. 5.1), while we are also ableto apply Cantelli’s and Chebyshev’s inequality (Props. 5.2and 5.3) with central moments. Our tool outperforms theprior work on programs (1-1) and (1-2), and derives bettertail bounds when 𝑑 is large on program (2-2), while it obtainssimilar curves on program (2-1).

Scalability. In Fig. 10, we demonstrate the running timesof our tool on the two sets of synthetic benchmark programs;Fig. 10a plots the analysis times for coupon-collector pro-grams as a function of the independent variable 𝑁 (the totalnumber of coupons), and Fig. 10b plots the analysis times forrandom-walk programs as a function of 𝑁 (the total numberof random walks). The evaluation statistics show that ourtool achieves good scalability in both case studies: the run-time is almost a linear function of the program size, whichis the number of recursive functions for both case studies.Two reasons why our tool is scalable on the two sets of pro-grams are (i) our analysis is compositional and uses functionsummaries to analyze function calls, and (ii) for a fixed set of

13

Page 14: Central Moment Analysis for Cost Accumulators in

PLDI ’21, June 20–25, 2021, Virtual, UK Di Wang, Jan Hoffmann, and Thomas Reps

20 30 40 𝑑0

0.1

0.2

tailprobability

ℙ[𝑇

â‰„đ‘‘]

(1-1)

by raw moments [26]by 2nd central momentby 4th central moment

100 150 𝑑0

0.1

0.2

(1-2)

by raw moments [26]by 2nd central momentby 4th central moment

200 400 𝑑0

0.1

0.2

tailprobability

ℙ[𝑇

â‰„đ‘‘]

(2-1)

by raw moments [26]by 2nd central momentby 4th central moment

200 400 𝑑0

0.1

0.2

(2-2)

by raw moments [26]by 2nd central momentby 4th central moment

Figure 9. Upper bound of the tail probability ℙ[𝑇 ≄ 𝑑] as afunction of 𝑑 , with comparison to Kura et al. [26]. Each grayline is the minimum of tail bounds obtained from the rawmoments of degree up to four inferred by Kura et al. [26].Green lines and red lines are the tail bounds given by 2ndand 4th central moments inferred by our tool, respectively.We include the plots for other programs in Appendix H.

0 200 400 600 800 1000N

0

2000

4000

6000

8000

runn

ing

time

(ms)

(a) Coupon Collector

0 200 400 600 800 1000N

0

500

1000

1500

2000

2500

3000

3500

runn

ing

time

(ms)

(b) Random Walk

Figure 10. Running times of our tool on two sets of syntheticbenchmark programs. Each figure plots the runtimes as afunction of the size of the analyzed program.

templates and a fixed diameter of the call graph, the numberof linear constraints generated by our tool grows linearlywith the size of the program, and the LP solvers availablenowadays can handle large problem instances efficiently.

Discussion. Higher central moments can also providemore information about the shape of a distribution, e.g., theskewness (i.e., đ”Œ[ (đ‘‡âˆ’đ”Œ[𝑇 ])

3 ](𝕍 [𝑇 ]) 3/2 ) indicates how lopsided the distri-

bution of 𝑇 is, and the kurtosis (i.e., đ”Œ[ (đ‘‡âˆ’đ”Œ[𝑇 ])4 ](𝕍 [𝑇 ])2 ) measures

the heaviness of the tails of the distribution of 𝑇 . We usedour tool to analyze two variants of the random-walk pro-gram (2-1). The two random walks have different transitionprobabilities and step length, but they have the same ex-pected runtime đ”Œ[𝑇 ]. Tab. 2 presents the skewness and kur-tosis derived from the moment bounds inferred by our tool.

100 200 300 400 5000

2

4

·10−3

density

estim

ation

rdwalk-1rdwalk-2đ”Œ[𝑇 ]

600 700 800 900 𝑇0

1

2

·10−4rdwalk-1rdwalk-2

Figure 11. Density estimation for the runtime𝑇 of two vari-ants rdwalk-1 and rdwalk-2 of (2-1).

Table 2. Skewness & kurtosis.program skewness kurtosisrdwalk-1 2.1362 10.5633rdwalk-2 2.9635 17.5823

A positive skew indicatesthat the mass of the distribu-tion is concentrated on theleft, and larger skew meansthe concentration is more left. A larger kurtosis, on theother hand, indicates that the distribution has fatter tails.Therefore, as the derived skewness and kurtosis indicate, thedistribution of the runtime 𝑇 for rdwalk-2 should be moreleft-leaning and have fatter tails than the distribution forrdwalk-1. Density estimates for the runtime 𝑇 , obtained bysimulation, are shown in Fig. 11.Our tool can also derive symbolic bounds on higher mo-

ments. The table below presents the inferred upper boundson the variances for the random-walk benchmarks, wherewe replace the concrete inputs with symbolic pre-conditions.

program pre-condition upper bnd. on the variance(2-1) đ‘„ ≄ 0 1920đ‘„(2-2) đ‘„ ≄ 0 2166.6667đ‘„ + 1541.6667

7 ConclusionWe have presented an automatic central-moment analysisfor probabilistic programs that support general recursionand continuous distributions, by deriving symbolic intervalbounds on higher raw moments for cost accumulators. Wehave proposed moment semirings for compositional reason-ing and moment-polymorphic recursion for interproceduralanalysis. We have proved soundness of our technique usinga recent extension to the Optional Stopping Theorem. Theeffectiveness of our technique has been demonstrated withour prototype implementation and the analysis of a broadsuite of benchmarks, as well as a tail-bound analysis.In the future, we plan to go beyond arithmetic programs

and add support for more datatypes, e.g., Booleans and lists.We will also work on other kinds of uncertain quantitiesfor probabilistic programs. Another research direction is toapply our analysis to higher-order functional programs.

AcknowledgmentsThis article is based on research supported, in part, by a giftfrom Rajiv and Ritu Batra; by ONR under grants N00014-17-1-2889 and N00014-19-1-2318; by DARPA under AA con-tract FA8750-18-C0092; and by the NSF under SaTC award1801369, SHF awards 1812876 and 2007784, and CAREERaward 1845514. Any opinions, findings, and conclusions orrecommendations expressed in this publication are those of

14

Page 15: Central Moment Analysis for Cost Accumulators in

Central Moment Analysis for Cost Accumulators in Probabilistic Programs PLDI ’21, June 20–25, 2021, Virtual, UK

the authors, and do not necessarily reflect the views of thesponsoring agencies.

We thank Jason Breck and John Cyphert for formulating,and providing us with an initial formalization of the problemof timing-attack analysis.

References[1] Gilles Barthe, Thomas Espitau, Luis MarĂ­a Ferrer Fioriti, and Justin Hsu.

2016. Synthesizing Probabilistic Invariants via Doob’s Decomposition.In Computer Aided Verif. (CAV’16). https://doi.org/10.1007/978-3-319-41528-4_3

[2] Gilles Barthe, Thomas Espitau, Marco Gaboardi, Benjamin GrĂ©goire,Justin Hsu, and Pierre-Yves Strub. 2018. An Assertion-Based ProgramLogic for Probabilistic Programs. In European Symp. on Programming(ESOP’18). https://doi.org/10.1007/978-3-319-89884-1_5

[3] Gilles Barthe, Benjamin GrĂ©goire, and Santiago Zanella BĂ©guelin. 2009.Formal Certification of Code-Based Cryptographic Proofs. In Princ. ofProg. Lang. (POPL’09). https://doi.org/10.1145/1594834.1480894

[4] Ezio Bartocci, Laura Kovács, and Miroslav Stankovič. 2019. AutomaticGeneration of Moment-Based Invariants for Prob-Solvable Loops. InAutomated Tech. for Verif. and Analysis (ATVA’19). https://doi.org/10.1007/978-3-030-31784-3_15

[5] Kevin Batz, Benjamin Lucien Kaminski, Joost-Pieter Katoen, andChristoph Matheja. 2018. How long, O Bayesian network, will Isample thee?. In European Symp. on Programming (ESOP’18). https://doi.org/10.1007/978-3-319-89884-1_7

[6] Patrick Billingsley. 2012. Probability and Measure. John Wiley & Sons,Inc.

[7] Johannes Borgström, Ugo Dal Lago, Andrew D. Gordon, and MarcinSzymczak. 2016. A Lambda-Calculus Foundation for UniversalProbabilistic Programming. In Int. Conf. on Functional Programming(ICFP’16). https://doi.org/10.1145/2951913.2951942

[8] Olivier Bouissou, Eric Goubault, Sylvie Putot, Aleksandar Chakarov,and Sriram Sankaranarayanan. 2016. Uncertainty Propagation UsingProbabilistic Affine Forms and Concentration of Measure Inequalities.In Tools and Algs. for the Construct. and Anal. of Syst. (TACAS’16).https://doi.org/10.1007/978-3-662-49674-9_13

[9] Quentin Carbonneaux, Jan Hoffmann, Thomas Reps, and Zhong Shao.2017. Automated Resource Analysis with Coq Proof Objects. In Com-puter Aided Verif. (CAV’17). https://doi.org/10.1007/978-3-319-63390-9_4

[10] Aleksandar Chakarov and Sriram Sankaranarayanan. 2013. Proba-bilistic Program Analysis with Martingales. In Computer Aided Verif.(CAV’13). https://doi.org/10.1007/978-3-642-39799-8_34

[11] Krishnendu Chatterjee, Hongfei Fu, and Amir Kafshdar Goharshady.2016. Termination Analysis of Probabilistic Programs Through Posi-tivstellensatz’s. In Computer Aided Verif. (CAV’16). https://doi.org/10.1007/978-3-319-41528-4_1

[12] Krishnendu Chatterjee, Hongfei Fu, Petr NovotnĂœ, and RouzbehHasheminezhad. 2016. Algorithmic Analysis of Qualitative and Quanti-tative Termination Problems for Affine Probabilistic Programs. In Princ.of Prog. Lang. (POPL’16). https://doi.org/10.1145/2837614.2837639

[13] Devdatt P. Dubhashi and Alessandro Panconesi. 2009. Concentrationof Measure for the Analysis of Randomized Algorithms. CambridgeUniversity Press. https://doi.org/10.1017/CBO9780511581274

[14] Luis María Ferrer Fioriti and Holger Hermanns. 2015. Probabilistic Ter-mination: Soundness, Completeness, and Compositionality. In Princ.of Prog. Lang. (POPL’15). https://doi.org/10.1145/2676726.2677001

[15] Dustin Fraze. 2015. Space/Time Analysis for Cybersecurity (STAC).Available on https://www.darpa.mil/program/space-time-analysis-for-cybersecurity.

[16] Zoubin Ghahramani. 2015. Probabilistic machine learning and arti-ficial intelligence. Nature 521 (May 2015). https://doi.org/10.1038/

nature14541[17] MichĂšle Giry. 1982. A Categorical Approach to Probability Theory. In

Categorical Aspects of Topology and Analysis. https://doi.org/10.1007/BFb0092872

[18] Andrew D. Gordon, Thomas A. Henzinger, Aditya V. Nori, and Sri-ram K. Rajamani. 2014. Probabilistic Programming. In Future of Softw.Eng. (FOSE’14). https://doi.org/10.1145/2593882.2593900

[19] Marcel Hark, Benjamin Lucien Kaminski, JĂŒrgen Giesl, and Joost-PieterKatoen. 2019. Aiming Low Is Harder: Induction for Lower Bounds inProbabilistic Program Verification. Proc. ACM Program. Lang. 4, POPL(December 2019). https://doi.org/10.1145/3371105

[20] Jan Hoffmann and Martin Hofmann. 2010. Amortized Resource Anal-ysis with Polymorphic Recursion and Partial Big-Step OperationalSemantics. In Asian Symp. on Prog. Lang. and Systems (APLAS’10).https://doi.org/10.1007/978-3-642-17164-2_13

[21] Martin Hofmann and Steffen Jost. 2003. Static Prediction of HeapSpace Usage for First-Order Functional Programs. In Princ. of Prog.Lang. (POPL’03). https://doi.org/10.1145/604131.604148

[22] Bertrand Jeannet and Antoine MinĂ©. 2009. Apron: A Library of Nu-merical Abstract Domains for Static Analysis. In Computer Aided Verif.(CAV’09). https://doi.org/10.1007/978-3-642-02658-4_52

[23] Xia Jiang and Gregory F. Cooper. 2010. A Bayesian spatio-temporalmethod for disease outbreak detection. J. American Medical InformaticsAssociation 17, 4 (July 2010). https://doi.org/10.1136/jamia.2009.000356

[24] Benjamin Lucien Kaminski, Joost-Pieter Katoen, Christoph Matheja,and Federico Olmedo. 2016. Weakest Precondition Reasoning forExpected Run–Times of Probabilistic Programs. In European Symp.on Programming (ESOP’16). https://doi.org/10.1007/978-3-662-49498-1_15

[25] Dexter Kozen. 1981. Semantics of Probabilistic Programs. J. Comput.Syst. Sci. 22, 3 (June 1981). https://doi.org/10.1016/0022-0000(81)90036-2

[26] Satoshi Kura, Natsuki Urabe, and Ichiro Hasuo. 2019. Tail Probabilityfor Randomized Program Runtimes via Martingales for Higher Mo-ments. In Tools and Algs. for the Construct. and Anal. of Syst. (TACAS’19).https://doi.org/10.1007/978-3-030-17465-1_8

[27] Zhifei Li and Jason Eisner. 2009. First- and Second-Order ExpectationSemirings with Applications to Minimum-Risk Training on Trans-lation Forests. In Empirical Methods in Natural Language Processing(EMNLP’09). https://dl.acm.org/doi/10.5555/1699510.1699517

[28] Gurobi Optimization LLC. 2020. Gurobi Optimizer Reference Manual.Available on https://www.gurobi.com.

[29] Guido Manfredi and Yannick Jestin. 2016. An Introduction to ACASXu and the Challenges Ahead. In Digital Avionics Systems Conference(DASC’16). https://doi.org/10.1109/DASC.2016.7778055

[30] Annabelle K. McIver and Carroll C. Morgan. 2005. Abstraction, Refine-ment and Proof for Probabilistic Systems. Springer Science+BusinessMedia, Inc. https://doi.org/10.1007/b138392

[31] Van Chan Ngo, Quentin Carbonneaux, and Jan Hoffmann. 2018.Bounded Expectations: Resource Analysis for Probabilistic Programs.In Prog. Lang. Design and Impl. (PLDI’18). https://doi.org/10.1145/3192366.3192394

[32] Van Chan Ngo, Mario Dehesa-Azuara, Matthew Fredrikson, and JanHoffmann. 2017. Verifying and Synthesizing Constant-ResourceImplementations with Types. In Symp. on Sec. and Privacy (SP’17).https://doi.org/10.1109/SP.2017.53

[33] Federico Olmedo, Benjamin Lucien Kaminski, Joost-Pieter Katoen, andChristoph Matheja. 2016. Reasoning about Recursive ProbabilisticPrograms. In Logic in Computer Science (LICS’16). https://doi.org/10.1145/2933575.2935317

[34] Prakash Panangaden. 1999. The Category of Markov Kernels. Electr.Notes Theor. Comp. Sci. 22 (1999). https://doi.org/10.1016/S1571-0661(05)80602-4

[35] Martin L. Puterman. 1994.Markov Decision Processes: Discrete StochasticDynamic Programming. John Wiley & Sons, Inc. https://dl.acm.org/

15

Page 16: Central Moment Analysis for Cost Accumulators in

PLDI ’21, June 20–25, 2021, Virtual, UK Di Wang, Jan Hoffmann, and Thomas Reps

doi/book/10.5555/528623[36] Reuven Y. Rubinstein and Dirk P. Kroese. 2016. Simulation and the

Monte Carlo Method. John Wiley & Sons, Inc. https://doi.org/10.1002/9781118631980

[37] Sriram Sankaranarayanan, Aleksandar Chakarov, and Sumit Gulwani.2013. Static Analysis for Probabilistic Programs: Inferring WholeProgram Properties from Finitely Many Paths. In Prog. Lang. Designand Impl. (PLDI’13). https://doi.org/10.1145/2491956.2462179

[38] Anne Schreuder and Luke Ong. 2019. Polynomial Probabilistic Invari-ants and the Optional Stopping Theorem. https://arxiv.org/abs/1910.12634

[39] Robert Endre Tarjan. 1985. Amortized Computational Complexity.SIAM J. Algebraic Discrete Methods 6, 2 (August 1985). https://doi.org/10.1137/0606031

[40] Di Wang, Jan Hoffmann, and Thomas Reps. 2018. PMAF: An AlgebraicFramework for Static Analysis of Probabilistic Programs. In Prog. Lang.Design and Impl. (PLDI’18). https://doi.org/10.1145/3192366.3192408

[41] Di Wang, Jan Hoffmann, and Thomas Reps. 2021. Expected-CostAnalysis for Probabilistic Programs and Semantics-Level Adaption ofOptional Stopping Theorems. https://arxiv.org/abs/2103.16105

[42] Di Wang, David M. Kahn, and Jan Hoffmann. 2020. Raising Expec-tations: Automating Expected Cost Analysis with Types. Proc. ACMProgram. Lang. 4, ICFP (August 2020). https://doi.org/10.1145/3408992

[43] Peixin Wang, Hongfei Fu, Amir Kafshdar Goharshady, KrishnenduChatterjee, Xudong Qin, and Wenjun Shi. 2019. Cost Analysis ofNondeterministic Probabilistic Programs. In Prog. Lang. Design andImpl. (PLDI’19). https://doi.org/10.1145/3314221.3314581

[44] David Williams. 1991. Probability with Martingales. Cambridge Uni-versity Press. https://doi.org/10.1017/CBO9780511813658

16

Page 17: Central Moment Analysis for Cost Accumulators in

Central Moment Analysis for Cost Accumulators in Probabilistic Programs PLDI ’21, June 20–25, 2021, Virtual, UK

A Preliminaries on Measure TheoryInterested readers can refer to textbooks and notes in theliterature [6, 44] for more details.

A.1 BasicsA measurable space is a pair (𝑆,S), where 𝑆 is a nonemptyset, and S is a 𝜎-algebra on 𝑆 , i.e., a family of subsets of 𝑆 thatcontains ∅ and is closed under complement and countableunions. The smallest 𝜎-algebra that contains a family A ofsubsets of 𝑆 is said to be generated by A, denoted by 𝜎 (A).Every topological space (𝑆, 𝜏) admits a Borel 𝜎-algebra, givenby 𝜎 (𝜏). This gives canonical 𝜎-algebras on ℝ, ℚ, ℕ, etc. Afunction 𝑓 : 𝑆 → 𝑇 , where (𝑆,S) and (𝑇, T ) are measur-able spaces, is said to be (S, T )-measurable, if 𝑓 −1 (đ”) ∈ Sfor each đ” ∈ T . If 𝑇 = ℝ, we tacitly assume that the Borel𝜎-algebra is defined on 𝑇 , and we simply call 𝑓 measurable,or a random variable. Measurable functions form a vectorspace, and products, maxima, and limiting operations pre-serve measurability.A measure ` on a measurable space (𝑆,S) is a mapping

from S to [0,∞] such that (i) ` (∅) = 0, and (ii) for allpairwise-disjoint {𝐮𝑛}đ‘›âˆˆâ„€+ in S , it holds that ` (â‹ƒđ‘›âˆˆâ„€+ 𝐮𝑖 ) =âˆ‘đ‘›âˆˆâ„€+ ` (𝐮𝑖 ). The triple (𝑆,S, `) is called a measure space. A

measure ` is called a probability measure, if ` (𝑆) = 1. Wedenote the collection of probability measures on (𝑆,S) byđ”»(𝑆,S). For each đ‘„ ∈ 𝑆 , the Dirac measure 𝛿 (đ‘„) is definedas _𝐮.[đ‘„ ∈ 𝐮]. For measures ` and a , we write ` + a for themeasure _𝐮.` (𝐮) +a (𝐮). For measure ` and scalar 𝑐 ≄ 0, wewrite 𝑐 · ` for the measure _𝐮.𝑐 · ` (𝐮).

The integral of a measurable function 𝑓 on 𝐮 ∈ S with re-spect to ameasure ` on (𝑆,S) is defined following Lebesgue’stheory and is denoted by ` (𝑓 ;𝐮),

âˆ«đŽđ‘“ 𝑑`, or

âˆ«đŽđ‘“ (đ‘„)` (đ‘‘đ‘„). If

` is a probability measure, we call the integral as the expec-tation of 𝑓 , written đ”Œđ‘„âˆŒ` [𝑓 ;𝐮], or simply đ”Œ[𝑓 ;𝐮] when thescope is clear in the context. If 𝐮 = 𝑆 , we tacitly omit 𝐮 fromthe notations. For each𝐮 ∈ S , it holds that ` (𝑓 ;𝐮) = ` (𝑓 I𝐮),where I𝐮 is the indicator function for 𝐮. We denote the col-lection of integrable functions on (𝑆,S, `) by L1 (𝑆,S, `).

A kernel from a measurable space (𝑆,S) to another (𝑇, T )is a mapping from 𝑆×T to [0,∞] such that: (i) for each đ‘„ ∈ 𝑆 ,the function _đ”.^ (đ‘„, đ”) is a measure on (𝑇, T ), and (ii) foreach đ” ∈ T , the function _đ‘„.^ (đ‘„, đ”) is measurable. We write^ : (𝑆,S) ⇝ (𝑇, T ) to declare that ^ is a kernel from (𝑆,S)to (𝑇, T ). Intuitively, kernels describe measure transformersfrom one measurable space to another. A kernel ^ is calleda probability kernel, if ^ (đ‘„,𝑇 ) = 1 for all đ‘„ ∈ 𝑆 . We denotethe collection of probability kernels from (𝑆,S) to (𝑇, T ) by𝕂((𝑆,S), (𝑇, T )). If the two measurable spaces coincide, wesimply write𝕂(𝑆,S). We can “push-forward” a measure ` on(𝑆,S) to a measure on (𝑇, T ) through a kernel ^ : (𝑆,S) ⇝(𝑇, T ) by integration:1 ` ≫= ^

def= _đ”.

âˆ«đ‘†^ (đ‘„, đ”)` (đ‘‘đ‘„).

1We use amonad bind notation ≫= here. Indeed, the category of measurablespaces admits a monad with sub-probability measures [17, 34].

A.2 Product MeasuresThe product of two measurable spaces (𝑆,S) and (𝑇, T ) isdefined as (𝑆,S)⊗ (𝑇, T ) def

= (𝑆×𝑇,S⊗T ), where S⊗T is thesmallest 𝜎-algebra that makes coordinate maps measurable,i.e., 𝜎 ({𝜋−1

1 (𝐮) | 𝐮 ∈ S} âˆȘ {𝜋−12 (đ”) | đ” ∈ T })), where 𝜋𝑖 is

the 𝑖-the coordinate map. If `1 and `2 are two probabilitymeasures on (𝑆,S) and (𝑇, T ), respectively, then there existsa unique probability measure ` on (𝑆,S)⊗ (𝑇, T ), by Fubini’stheorem, called the product measure of `1 and `2, written`1 ⊗ `2, such that ` (đŽĂ—đ”) = `1 (𝐮) · `2 (đ”) for all 𝐮 ∈ S andđ” ∈ T .If ` is a probability measure on (𝑆,S) and ^ : (𝑆,S) ⇝

(𝑇, T ) is a probability kernel, then we can construct the aprobability measure on (𝑆,S) ⊗ (𝑇, T ) that captures all tran-sitions from ` through ^: ` ⊗^ def

= _(𝐮, đ”).âˆ«đŽ^ (đ‘„, đ”)` (đ‘‘đ‘„). If

` is a probability measure on (𝑆0,S0) and ^𝑖 : (𝑆𝑖−1,S𝑖−1) ⇝(𝑆𝑖 ,S𝑖 ) is a probability kernel for 𝑖 = 1, · · · , 𝑛,where 𝑛 ∈ â„€+,then we can construct a probability measure on

⊗𝑛

𝑖=0 (𝑆𝑖 ,S𝑖 ),i.e., the space of sequences of 𝑛 transitions by iteratively ap-plying the kernels to `:

` ⊗⊗0

𝑖=1^𝑖

def= `,

` ⊗⊗𝑘+1

𝑖=1^𝑖

def= (` ⊗

⊗𝑘

𝑖=1^𝑖 ) ⊗ ^𝑘+1, 0 ≀ 𝑘 < 𝑛.

Let (𝑆𝑖 ,S𝑖 ), 𝑖 ∈ I be a family of measurable spaces. Theirproduct, denoted by

⊗𝑖∈I (𝑆𝑖 ,S𝑖 ) = (∏𝑖∈I 𝑆𝑖 ,

⊗𝑖∈I S𝑖 ), is

the product space with the smallest 𝜎-algebra such that foreach 𝑖 ∈ I , the coordinatemap 𝜋𝑖 is measurable. The theorembelow is widely used to construct a probability measures onan infinite product via probability kernels.

Proposition A.1 (Ionescu-Tulcea). Let (𝑆𝑖 ,S𝑖 ), 𝑖 ∈ â„€+ be asequence of measurable spaces. Let `0 be a probability measureon (𝑆0,S0). For each 𝑖 ∈ ℕ, let ^𝑖 :

⊗𝑖−1𝑘=0 (𝑆𝑘 ,S𝑘 ) ⇝ (𝑆𝑖 ,S𝑖 )

be a probability kernel. Then there exists a sequence of proba-

bility measures `𝑖def= `0 ⊗

⊗𝑖

𝑘=1 ^𝑘 , 𝑖 ∈ â„€+, and there existsa uniquely defined probability measure ` on

⊗∞𝑘=0 (𝑆𝑘 ,S𝑘 )

such that `𝑖 (𝐮) = ` (𝐮 × ∏∞𝑘=𝑖+1 𝑆𝑘 ) for all 𝑖 ∈ â„€+ and

𝐮 ∈ ⊗𝑖

𝑘=0 S𝑖 .

A.3 Conditional ExpectationsLet (Ω,F ,ℙ) be a measure space, where ℙ is a probabilitymeasure. We write L1 for L1 (Ω,F ,ℙ). Let 𝑋 ∈ L1 be anintegrable random variable, and G ⊆ F be a sub-𝜎-algebraof F . Then there exists a random variable 𝑌 ∈ L1 such that:(i) 𝑌 is G-measurable, and (ii) for each đș ∈ G, it holds thatđ”Œ[𝑌 ;đș] = đ”Œ[𝑋 ;đș]. Such a random variable 𝑌 is said to bea version of conditional expectation đ”Œ[𝑋 | G] of 𝑋 givenG. Conditional expectations admit almost-sure uniqueness.On the other hand, if 𝑋 is a nonnegative random variable,the conditional expectation đ”Œ[𝑋 | G] is also defined andalmost-surely unique.

17

Page 18: Central Moment Analysis for Cost Accumulators in

PLDI ’21, June 20–25, 2021, Virtual, UK Di Wang, Jan Hoffmann, and Thomas Reps

Intuitively, for some 𝜔 ∈ Ω, đ”Œ[𝑋 | G] (𝜔) is the expecta-tion of𝑋 given the set of values𝑍 (𝜔) for everyG-measurablerandom variable 𝑍 . For example, if G = {∅,Ω}, which con-tains no information, then đ”Œ[𝑋 | G] (𝜔) = đ”Œ[𝑋 ]. If G = F ,which contains full information, then đ”Œ[𝑋 | G] (𝜔) = 𝑋 (𝜔).

We review some useful properties of conditional expecta-tions below.

Proposition A.2. Let 𝑋 ∈ L1, and G,H be sub-𝜎-algebrasof F .(1) If 𝑌 is any version of đ”Œ[𝑋 | G], then đ”Œ[𝑌 ] = đ”Œ[𝑋 ].(2) If 𝑋 is G-measurable, then đ”Œ[𝑋 | G] = 𝑋 , a.s.(3) IfH is a sub-𝜎-algebra of G, then đ”Œ[đ”Œ(𝑋 | G) | H] = đ”Œ[𝑋 |

H], a.s.(4) If 𝑍 is G-measurable and 𝑍 · 𝑋 ∈ L1, then đ”Œ[𝑍 · 𝑋 | G] =

𝑍 · đ”Œ[𝑋 | G], a.s. On the other hand, if 𝑋 is a nonnega-tive random variable and 𝑍 is a nonnegative G-measurablerandom variable, then the property also holds, with the un-derstanding that both sides might be infinite.

A.4 Convergence TheoremsWe review two important convergence theorems for seriesof random variables.

Proposition A.3 (Monotone convergence theorem). If{𝑓𝑛}đ‘›âˆˆâ„€+ is a non-decreasing sequence of nonnegativemeasurable functions on a measure space (𝑆,S, `), and{𝑓𝑛}đ‘›âˆˆâ„€+ converges to 𝑓 pointwise, then 𝑓 is measurable andlim𝑛→∞ ` (𝑓𝑛) = ` (𝑓 ) ≀ ∞.

Further, the theorem still holds if 𝑓 is chosen as a measurablefunction and “{𝑓𝑛}đ‘›âˆˆâ„€+ converges to 𝑓 pointwise” holds almosteverywhere, rather than everywhere.

Proposition A.4 (Dominated convergence theorem). If{𝑓𝑛}đ‘›âˆˆâ„€+ is a sequence of measurable functions on a mea-sure space (𝑆,S, `), {𝑓𝑛}đ‘›âˆˆâ„€+ converges to 𝑓 pointwise, and{𝑓𝑛}đ‘›âˆˆâ„€+ is dominated by a nonnegative integrable function 𝑔(i.e., |𝑓𝑛 (đ‘„) | ≀ 𝑔(đ‘„) for all 𝑛 ∈ â„€+, đ‘„ ∈ 𝑆), then 𝑓 is integrableand lim𝑛→∞ ` (𝑓𝑛) = ` (𝑓 ).Further, the theorem still holds if 𝑓 is chosen as a measur-

able function and “{𝑓𝑛}đ‘›âˆˆâ„€+ converges to 𝑓 pointwise and isdominated by 𝑔” holds almost everywhere.

B Operational Cost SemanticsWe follow a distribution-based approach [7, 25] to definereduction rules for the semantics of Appl. A probabilisticsemantics steps a program configuration to a probabilitydistribution on configurations. To formally describe thesedistributions, we need to construct a measurable space ofprogram configurations. Our approach is to construct a mea-surable space for each of the four components of configura-tions, and then use their product measurable space as thesemantic domain.

‱ Valuations đ›Ÿ : VID → ℝ are finite real-valued maps,so we define (𝑉 ,V) def

= (ℝVID,B(ℝVID)) as the canonicalstructure on a finite-dimensional space.

‱ The executing statement 𝑆 can contain real numbers, sowe need to “lift” the Borel 𝜎-algebra on ℝ to programstatements. Intuitively, statements with exactly the samestructure can be treated as vectors of parameters that cor-respond to their real-valued components. Formally, weachieve this by constructing a metric space on statementsand then extracting a Borel 𝜎-algebra from the metricspace. Fig. 12 presents a recursively defined metric 𝑑𝑆 onstatements, as well as metrics 𝑑𝐾 , 𝑑𝐿 , and đ‘‘đ· on expres-sions, conditions, and distributions, respectively, as theyare required by 𝑑𝑆 . We denote the result measurable spaceby (𝑆,S).

‱ A continuation đŸ is either an empty continuation Kstop,a loop continuation Kloop 𝐿 𝑆 đŸ , or a sequence contin-uation Kseq 𝑆 đŸ . Similarly, we construct a measurablespace (đŸ,K) on continuations by extracting from a met-ric space. Fig. 12 shows the definition of a metric đ‘‘đŸ oncontinuations.

‱ The cost accumulator đ›Œ ∈ ℝ is a real number, so we define(𝑊,W) def

= (ℝ,B(ℝ)) as the canonical measurable spaceon ℝ.

Then the semantic domain is defined as the product mea-surable space of the four components: (ÎŁ,O) def

= (𝑉 ,V) ⊗(𝑆,S) ⊗ (đŸ,K) ⊗ (𝑊,W).Fig. 13 presents the rules of the evaluation relation 𝜎 ↩→ `

for Appl where 𝜎 is a configuration and ` is a probabilitydistribution on configurations. Note that inAppl, expressions𝐾 and conditions 𝐿 are deterministic, so we define a standardbig-step evaluation relation for them, written đ›Ÿ ⊱ 𝐾 ⇓ 𝑟 andđ›Ÿ ⊱ 𝐿 ⇓ 𝑏, where đ›Ÿ is a valuation, 𝑟 ∈ ℝ, and 𝑏 ∈ {⊀,⊄}.Most of the rules in Fig. 13, except (E-Sample) and (E-Prob),are also deterministic as they step to a Dirac measure.The evaluation relation ↩→ can be interpreted as a distri-

bution transformer. Indeed, ↩→ can be seen as a probabilitykernel.

Lemma B.1. Let đ›Ÿ : VID → ℝ be a valuation.‱ Let 𝐾 be an expression. Then there exists a unique 𝑟 ∈ ℝ

such that đ›Ÿ ⊱ 𝐾 ⇓ 𝑟 .‱ Let 𝐿 be a condition. Then there exists a unique 𝑏 ∈ {⊀,⊄}such that đ›Ÿ ⊱ 𝐿 ⇓ 𝑏.

Proof. By induction on the structure of 𝐾 and 𝐿. □

Lemma B.2. For every configuration 𝜎 ∈ ÎŁ, there exists aunique ` ∈ đ”»(ÎŁ,O) such that 𝜎 ↩→ `.

Proof. Let 𝜎 = âŸšđ›Ÿ, 𝑆, đŸ, đ›ŒâŸ©. Then by case analysis on the struc-ture of 𝑆 , followed by a case analysis on the structure of đŸ if𝑆 = skip. The rest of the proof appeals to Lem. B.1. □

Theorem B.3. The evaluation relation ↩→ defines a probabil-ity kernel on program configurations.

18

Page 19: Central Moment Analysis for Cost Accumulators in

Central Moment Analysis for Cost Accumulators in Probabilistic Programs PLDI ’21, June 20–25, 2021, Virtual, UK

𝑑𝐾 (đ‘„, đ‘„) def= 0

𝑑𝐾 (𝑐1, 𝑐2) def= |𝑐1 − 𝑐2 |

𝑑𝐾 (𝐾11 + 𝐾12, 𝐾21 + 𝐾22) def= 𝑑𝐾 (𝐾11, 𝐾21) + 𝑑𝐾 (𝐾12, 𝐾22)

𝑑𝐾 (𝐾11 × 𝐾12, 𝐾21 × 𝐾22) def= 𝑑𝐾 (𝐾11, 𝐾21) + 𝑑𝐾 (𝐾12, 𝐾22)

𝑑𝐾 (𝐾1, 𝐾2) def= ∞ otherwise

𝑑𝐿 (true, true) def= 0

𝑑𝐿 (not 𝐿1,not 𝐿2) def= 𝑑𝐿 (𝐿1, 𝐿2)

𝑑𝐿 (𝐿11 and 𝐿12, 𝐿21 and 𝐿22) def= 𝑑𝐿 (𝐿11, 𝐿21) + 𝑑𝐿 (𝐿12, 𝐿22)

𝑑𝐿 (𝐾11 ≀ 𝐾12, 𝐾21 ≀ 𝐾22) def= 𝑑𝐾 (𝐾11, 𝐾21) + 𝑑𝐾 (𝐾12, 𝐾22)

𝑑𝐿 (𝐿1, 𝐿2) def= ∞ otherwise

đ‘‘đ· (uniform(𝑎1, 𝑏1),uniform(𝑎2, 𝑏2)) def= |𝑎1 − 𝑎2 | + |𝑏1 − 𝑏2 |

đ‘‘đ· (đ·1, đ·2) def= ∞ otherwise

𝑑𝑆 (skip, skip) def= 0

𝑑𝑆 (tick(𝑐1), tick(𝑐2)) def= |𝑐1 − 𝑐2 |

𝑑𝑆 (đ‘„ B 𝐾1, đ‘„ B 𝐾2) def= 𝑑𝐾 (𝐾1, 𝐾2)

𝑑𝑆 (đ‘„ ∌ đ·1, đ‘„ ∌ đ·2) def= đ‘‘đ· (đ·1, đ·2)

𝑑𝑆 (call 𝑓 , call 𝑓 ) def= 0

𝑑𝑆 (if prob(𝑝1) then 𝑆11 else 𝑆12 fi, if prob(𝑝2) then 𝑆21 else 𝑆22 fi) def= |𝑝1 − 𝑝2 | + 𝑑𝑆 (𝑆11, 𝑆21) + 𝑑𝑆 (𝑆12, 𝑆22)

𝑑𝑆 (if 𝐿1 then 𝑆11 else 𝑆12 fi, if 𝐿2 then 𝑆21 else 𝑆22 fi) def= 𝑑𝐿 (𝐿1, 𝐿2) + 𝑑𝑆 (𝑆11, 𝑆21) + 𝑑𝑆 (𝑆12, 𝑆22)

𝑑𝑆 (while 𝐿1 do 𝑆1 od,while 𝐿2 do 𝑆2 od) def= 𝑑𝐿 (𝐿1, 𝐿2) + 𝑑𝑆 (𝑆1, 𝑆2)

𝑑𝑆 (𝑆11; 𝑆12, 𝑆21; 𝑆22) def= 𝑑𝑆 (𝑆11, 𝑆21) + 𝑑𝑆 (𝑆12, 𝑆22)

𝑑𝑆 (𝑆1, 𝑆2) def= ∞ otherwise

đ‘‘đŸ (Kstop,Kstop) def= 0

đ‘‘đŸ (Kloop 𝐿1 𝑆1 đŸ1,Kloop 𝐿2 𝑆2 đŸ2) def= 𝑑𝐿 (𝐿1, 𝐿2) + 𝑑𝑆 (𝑆1, 𝑆2) + đ‘‘đŸ (đŸ1, đŸ2)

đ‘‘đŸ (Kseq 𝑆1 đŸ1,Kseq 𝑆2 đŸ2) def= 𝑑𝑆 (𝑆1, 𝑆2) + đ‘‘đŸ (đŸ1, đŸ2)

đ‘‘đŸ (đŸ1, đŸ2) def= ∞ otherwise

Figure 12.Metrics for expressions, conditions, distributions, statements, and continuations.

Proof. Lem. B.2 tells us that ↩→ can be seen as a function ˆ↩→defined as follows:

ˆ↩→(𝜎,𝐮) def= ` (𝐮) where 𝜎 ↩→ `.

It is clear that _𝐮. ˆ↩→(𝜎,𝐮) is a probability measure. On theother hand, to show that _𝜎. ˆ↩→(𝜎,𝐮) is measurable for any𝐮 ∈ O, we need to prove that for đ” ∈ B(ℝ), it holds thatđ’Ș(𝐮, đ”) def

= (_𝜎. ˆ↩→(𝜎,𝐮))−1 (đ”) ∈ O.We introduce skeletons of programs to separate real num-

bers and discrete structures.𝑆 F skip | tick(□ℓ ) | đ‘„ B 𝐾 | đ‘„ ∌ ïżœÌ‚ïżœ | call 𝑓| if prob(□ℓ ) then 𝑆1 else 𝑆2 fi | if ïżœÌ‚ïżœ then 𝑆1 else 𝑆2 fi

| while ïżœÌ‚ïżœ do 𝑆 od | 𝑆1; 𝑆2ïżœÌ‚ïżœ F true | not ïżœÌ‚ïżœ | ïżœÌ‚ïżœ1 and ïżœÌ‚ïżœ2 | 𝐾1 ≀ 𝐾2

𝐾 F đ‘„ | □ℓ | 𝐾1 + 𝐾2 | 𝐾1 × 𝐾2

ïżœÌ‚ïżœ F uniform(□ℓ𝑎 ,□ℓ𝑏 )ïżœÌ‚ïżœ F Kstop | Kloop ïżœÌ‚ïżœ 𝑆 ïżœÌ‚ïżœ | Kseq 𝑆 ïżœÌ‚ïżœThe holes □ℓ are placeholders for real numbers parameterizedby locations ℓ ∈ LOC. We assume that the holes in a programstructure are always pairwise distinct. Let [ : LOC → ℝ

be a map from holes to real numbers and [ (𝑆) (resp., [ (ïżœÌ‚ïżœ),[ (𝐾), [ (ïżœÌ‚ïżœ), [ (ïżœÌ‚ïżœ)) be the instantiation of a statement (resp.,condition, expression, distribution, continuation) skeleton bysubstituting [ (ℓ) for □ℓ . One important property of skeletonsis that the “distance” between any concretizations of twodifferent skeletons is always infinity with respect to themetrics in Fig. 12.

Observe thatđ’Ș(𝐮, đ”) =

â‹ƒÌ‚đ‘†,ïżœÌ‚ïżœ

đ’Ș(𝐮, đ”) ∩ {âŸšđ›Ÿ, [ (𝑆), [ (ïżœÌ‚ïżœ), đ›ŒâŸ© | any đ›Ÿ, đ›Œ, [}

19

Page 20: Central Moment Analysis for Cost Accumulators in

PLDI ’21, June 20–25, 2021, Virtual, UK Di Wang, Jan Hoffmann, and Thomas Reps

đ›Ÿ ⊱ 𝐾 ⇓ 𝑟 “the expression 𝐾 evaluates to a real value 𝑟 under the valuation đ›Ÿâ€(E-Var)đ›Ÿ (đ‘„) = đ‘Ÿđ›Ÿ ⊱ đ‘„ ⇓ 𝑟

(E-Const)

đ›Ÿ ⊱ 𝑐 ⇓ 𝑐

(E-Add)đ›Ÿ ⊱ 𝐾1 ⇓ 𝑟1 đ›Ÿ ⊱ 𝐾2 ⇓ 𝑟2 𝑟 = 𝑟1 + 𝑟2

đ›Ÿ ⊱ 𝐾1 + 𝐾2 ⇓ 𝑟

(E-Mul)đ›Ÿ ⊱ 𝐾1 ⇓ 𝑟1 đ›Ÿ ⊱ 𝐾2 ⇓ 𝑟2 𝑟 = 𝑟1 · 𝑟2

đ›Ÿ ⊱ 𝐾1 × 𝐾2 ⇓ đ‘Ÿđ›Ÿ ⊱ 𝐿 ⇓ 𝑏 “the condition 𝐿 evaluates to a Boolean value 𝑏 under the valuation đ›Ÿâ€

(E-Top)

đ›Ÿ ⊱ true ⇓ ⊀

(E-Neg)đ›Ÿ ⊱ 𝐿 ⇓ 𝑏

đ›Ÿ ⊱ not 𝐿 ⇓ ¬𝑏

(E-Conj)đ›Ÿ ⊱ 𝐿1 ⇓ 𝑏1 đ›Ÿ ⊱ 𝐿2 ⇓ 𝑏2đ›Ÿ ⊱ 𝐿1 and 𝐿2 ⇓ 𝑏1 ∧ 𝑏2

(E-Le)đ›Ÿ ⊱ 𝐾1 ⇓ 𝑟1 đ›Ÿ ⊱ 𝐾2 ⇓ 𝑟2đ›Ÿ ⊱ 𝐾1 ≀ 𝐾2 ⇓ [𝑟1 ≀ 𝑟2]

âŸšđ›Ÿ, 𝑆, đŸ, đ›ŒâŸ© ↩→ ` “the configuration âŸšđ›Ÿ, 𝑆, đŸ, đ›ŒâŸ© steps to a probability distribution ` on âŸšđ›Ÿ â€Č, 𝑆 â€Č, đŸ â€Č, đ›Œ â€Č⟩’s”

(E-Skip-Stop)

âŸšđ›Ÿ, skip,Kstop, đ›ŒâŸ© ↩→ 𝛿 (âŸšđ›Ÿ, skip,Kstop, đ›ŒâŸ©)

(E-Skip-Loop)đ›Ÿ ⊱ 𝐿 ⇓ 𝑏

âŸšđ›Ÿ, skip,Kloop 𝑆 𝐿 đŸ, đ›ŒâŸ© ↩→ [𝑏] · 𝛿 (âŸšđ›Ÿ, 𝑆,Kloop 𝑆 𝐿 đŸ, đ›ŒâŸ©) + [¬𝑏] · 𝛿 (âŸšđ›Ÿ, skip, đŸ, đ›ŒâŸ©)(E-Skip-Seq)

âŸšđ›Ÿ, skip,Kseq 𝑆 đŸ, đ›ŒâŸ© ↩→ 𝛿 (âŸšđ›Ÿ, 𝑆, đŸ, đ›ŒâŸ©)(E-Tick)

âŸšđ›Ÿ, tick(𝑐), đŸ, đ›ŒâŸ© ↩→ 𝛿 (âŸšđ›Ÿ, skip, đŸ, đ›Œ + đ‘âŸ©)(E-Assign)

đ›Ÿ ⊱ 𝐾 ⇓ đ‘ŸâŸšđ›Ÿ, đ‘„ B 𝐾, đŸ, đ›ŒâŸ© ↩→ 𝛿 (âŸšđ›Ÿ [đ‘„ ↩→ 𝑟 ], skip, đŸ, đ›ŒâŸ©)

(E-Sample)

âŸšđ›Ÿ, đ‘„ ∌ đ·,đŸ, đ›ŒâŸ© ↩→ `đ· ≫= _𝑟 .𝛿 (âŸšđ›Ÿ [đ‘„ ↩→ 𝑟 ], skip, đŸ, đ›ŒâŸ©)(E-Call)

âŸšđ›Ÿ, call 𝑓 , đŸ, đ›ŒâŸ© ↩→ 𝛿 (âŸšđ›Ÿ,𝒟(𝑓 ), đŸ, đ›ŒâŸ©)(E-Prob)

âŸšđ›Ÿ, if prob(𝑝) then 𝑆1 else 𝑆2 fi, đŸ, đ›ŒâŸ© ↩→ 𝑝 · 𝛿 (âŸšđ›Ÿ, 𝑆1, đŸ, đ›ŒâŸ©) + (1 − 𝑝) · 𝛿 (âŸšđ›Ÿ, 𝑆2, đŸ, đ›ŒâŸ©)(E-Cond)

đ›Ÿ ⊱ 𝐿 ⇓ đ‘âŸšđ›Ÿ, if 𝐿 then 𝑆1 else 𝑆2 fi, đŸ, đ›ŒâŸ© ↩→ [𝑏] · 𝛿 (âŸšđ›Ÿ, 𝑆1, đŸ, đ›ŒâŸ©) + [¬𝑏] · 𝛿 (âŸšđ›Ÿ, 𝑆2, đŸ, đ›ŒâŸ©)

(E-Loop)

âŸšđ›Ÿ,while 𝐿 do 𝑆 od, đŸ, đ›ŒâŸ© ↩→ 𝛿 (âŸšđ›Ÿ, skip,Kloop 𝐿 𝑆 đŸ, đ›ŒâŸ©)(E-Seq)

âŸšđ›Ÿ, 𝑆1; 𝑆2, đŸ, đ›ŒâŸ© ↩→ 𝛿 (âŸšđ›Ÿ, 𝑆1,Kseq 𝑆2 đŸ, đ›ŒâŸ©)

Figure 13. Rules of the operational semantics of Appl.

and that 𝑆, ïżœÌ‚ïżœ are countable families of statement and contin-uation skeletons. Thus it suffices to prove that every set inthe union, which we denote by đ’Ș(𝐮, đ”) ∩ 𝒞(𝑆, ïżœÌ‚ïżœ) later inthe proof, is measurable. Note that 𝒞(𝑆, ïżœÌ‚ïżœ) itself is indeedmeasurable. Further, the skeletons 𝑆 and ïżœÌ‚ïżœ are able to deter-mine the evaluation rule for all concretized configurations.Thus we can proceed by a case analysis on the evaluationrules.To aid the case analysis, we define a deterministic eval-

uation relation det↩−−→ by getting rid of the 𝛿 (·) notations inthe rules in Fig. 13 except probabilistic ones (E-Sample) and(E-Prob). Obviously, det↩−−→ can be interpreted as a measurablefunction on configurations.‱ If the evaluation rule is deterministic, then we have

đ’Ș(𝐮, đ”) âˆ©đ’ž(𝑆, ïżœÌ‚ïżœ)= {𝜎 | 𝜎 ↩→ `, ` (𝐮) ∈ đ”} âˆ©đ’ž(𝑆, ïżœÌ‚ïżœ)= {𝜎 | 𝜎 det↩−−→ 𝜎 â€Č, [𝜎 â€Č ∈ 𝐮] ∈ đ”} âˆ©đ’ž(𝑆, ïżœÌ‚ïżœ)

=

𝒞(𝑆, ïżœÌ‚ïżœ) if {0, 1} ⊆ đ”

det↩−−→−1(𝐮) âˆ©đ’ž(𝑆, ïżœÌ‚ïżœ) if 1 ∈ đ” and 0 ∉ đ”

det↩−−→−1(𝐮𝑐 ) âˆ©đ’ž(𝑆, ïżœÌ‚ïżœ) if 0 ∈ đ” and 1 ∉ đ”

∅ if {0, 1} ∩ đ” = ∅.The sets in all the cases are measurable, so is the setđ’Ș(𝐮, đ”) âˆ©đ’ž(𝑆, ïżœÌ‚ïżœ).

‱ (E-Prob): Consider đ” with the form (−∞, 𝑡] with 𝑡 ∈ ℝ. If𝑡 ≄ 1, then đ’Ș(𝐮, đ”) = ÎŁ. Otherwise, let us assume 𝑡 < 1.Let 𝑆 = if prob(□) then 𝑆1 else 𝑆2 fi. Then we haveđ’Ș(𝐮, đ”) âˆ©đ’ž(𝑆, ïżœÌ‚ïżœ)

= {𝜎 | 𝜎 ↩→ `, ` (𝐮) ∈ đ”} âˆ©đ’ž(𝑆, ïżœÌ‚ïżœ)= {𝜎 | 𝜎 ↩→ 𝑝 · 𝛿 (𝜎1) + (1 − 𝑝) · 𝛿 (𝜎2),

𝑝 · [𝜎1 ∈ 𝐮] + (1 − 𝑝) · [𝜎2 ∈ 𝐮] ∈ đ”} âˆ©đ’ž(𝑆, ïżœÌ‚ïżœ)= 𝒞(𝑆, ïżœÌ‚ïżœ) ∩ {âŸšđ›Ÿ, if prob(𝑝) then 𝑆1 else 𝑆2 fi, đŸ, đ›ŒâŸ© |

𝑝 · [âŸšđ›Ÿ, 𝑆1, đŸ, đ›ŒâŸ© ∈ 𝐮] + (1 − 𝑝) · [âŸšđ›Ÿ, 𝑆2, đŸ, đ›ŒâŸ© ∈ 𝐮] ≀ 𝑡}= 𝒞(𝑆, ïżœÌ‚ïżœ) ∩{âŸšđ›Ÿ, if prob(𝑝) then 𝑆1 else 𝑆2 fi, đŸ, đ›ŒâŸ© |20

Page 21: Central Moment Analysis for Cost Accumulators in

Central Moment Analysis for Cost Accumulators in Probabilistic Programs PLDI ’21, June 20–25, 2021, Virtual, UK

(âŸšđ›Ÿ, 𝑆1, đŸ, đ›ŒâŸ© ∈ 𝐮, âŸšđ›Ÿ, 𝑆2, đŸ, đ›ŒâŸ© ∉ 𝐮, 𝑝 ≀ 𝑡) √(âŸšđ›Ÿ, 𝑆2, đŸ, đ›ŒâŸ© ∈ 𝐮, âŸšđ›Ÿ, 𝑆1, đŸ, đ›ŒâŸ© ∉ 𝐮, 1 − 𝑝 ≀ 𝑡)}.

The set above is measurable because 𝐮 and 𝐮𝑐 are mea-surable, as well as {𝑝 ≀ 𝑡} and {𝑝 ≄ 1− 𝑡} are measurablein ℝ.

‱ (E-Sample): Consider đ” with the form (−∞, 𝑡] with 𝑡 ∈ ℝ.Similar to the previous case, we assume that 𝑡 < 1. Let𝑆 = đ‘„ ∌ uniform(□ℓ𝑎 ,□ℓ𝑏 ), without loss of generality.Then we haveđ’Ș(𝐮, đ”) âˆ©đ’ž(𝑆, ïżœÌ‚ïżœ)

= {𝜎 | 𝜎 ↩→ `, ` (𝐮) ∈ đ”} âˆ©đ’ž(𝑆, ïżœÌ‚ïżœ)

= {𝜎 | 𝜎 ↩→ `đ· ≫= ^𝜎 ,

∫^𝜎 (𝑟 ) (𝐮)`đ· (𝑑𝑟 ) ≀ 𝑡} âˆ©đ’ž(𝑆, ïżœÌ‚ïżœ)

= 𝒞(𝑆, ïżœÌ‚ïżœ) ∩ {𝜎 | 𝜎 ↩→ `đ· ≫= ^𝜎 ,

`đ· ({𝑟 | âŸšđ›Ÿ [đ‘„ ↩→ 𝑟 ], skip, đŸ, đ›ŒâŸ© ∈ 𝐮}) ≀ 𝑡}= 𝒞(𝑆, ïżœÌ‚ïżœ) ∩ {âŸšđ›Ÿ, đ‘„ ∌ uniform(𝑎, 𝑏), đŸ, đ›ŒâŸ© | 𝑎 < 𝑏,

`uniform(𝑎,𝑏) ({𝑟 | âŸšđ›Ÿ [đ‘„ ↩→ 𝑟 ], skip, đŸ, đ›ŒâŸ© ∈ 𝐮}) ≀ 𝑡},where ^ âŸšđ›Ÿ,𝑆,đŸ,đ›Œ ⟩

def= _𝑟 .𝛿 (âŸšđ›Ÿ [đ‘„ ↩→ 𝑟 ], skip, đŸ, đ›ŒâŸ©). For fixed

đ›Ÿ, đŸ, đ›Œ , the set {𝑟 | âŸšđ›Ÿ [đ‘„ ↩→ 𝑟 ], skip, đŸ, đ›ŒâŸ© ∈ 𝐮} is measur-able in ℝ. For the distributions considered in this paper,there is a sub-probability kernel ^đ· : ℝar(đ·) ⇝ ℝ. Forexample, ^uniform (𝑎, 𝑏) is defined to be `uniform(𝑎,𝑏) if 𝑎 <

𝑏, or 0 otherwise. Therefore, _(𝑎, 𝑏) .^uniform (𝑎, 𝑏) ({𝑟 |âŸšđ›Ÿ [đ‘„ ↩→ 𝑟 ], skip, đŸ, đ›ŒâŸ© ∈ 𝐮}) is measurable, and its in-version on (−∞, 𝑡] is a measurable set on distributionparameters (𝑎, 𝑏). Hence the set above is measurable.

□

C Trace-Based Cost SemanticsTo reason about moments of the accumulated cost, wefollow the Markov-chain-based reasoning [24, 33] to de-velop a trace-based cost semantics for Appl. Let (Ω,F) def

=⊗∞𝑛=0 (Σ,O) be a measurable space of infinite traces on

program configurations. Let {F𝑛}đ‘›âˆˆâ„€+ be a filtration, i.e.,an increasing sequence F0 ⊆ F1 ⊆ · · · ⊆ F of sub-𝜎-algebras in F , generated by coordinate maps 𝑋𝑛 (𝜔) def

= 𝜔𝑛

for 𝑛 ∈ â„€+. Let ⟹𝒟, 𝑆main⟩ be an Appl program. Let `0def=

𝛿 (⟹__.0, 𝑆main,Kstop, 0⟩) be the initial distribution. Let ℙbe the probability measure on infinite traces induced byProp. A.1 and Thm. B.3. Then (Ω,F ,ℙ) forms a probabilityspace on infinite traces of the program.

D Trace-Based Reasoning on ExpectationsRecall that we define a stopping time 𝑇 : Ω → â„€+ âˆȘ {∞} as

𝑇 (𝜔) def= inf{𝑛 ∈ â„€+ | 𝜔𝑛 = ⟹_, skip,Kstop, _⟩},

and random variables {𝐮𝑛}đ‘›âˆˆâ„€+ , {Ω𝑛}đ‘›âˆˆâ„€+ , {𝑌𝑛}đ‘›âˆˆâ„€+ as𝐮𝑛 (𝜔) def

= đ›Œđ‘›,Ω𝑛 (𝜔) def= 𝜙 (𝜔𝑛), 𝑌𝑛 (𝜔) def

= 𝐮𝑛 (𝜔) + Ω𝑛 (𝜔),where 𝜔𝑛 = ⟹_, _, _, đ›Œđ‘›âŸ©, and 𝜙 : ÎŁ → ℝ is an expected-potential function for expected-cost bound analysis. Taking

the stopping time into consideration, we define the stoppedversion for these random variables as 𝐮𝑇 (𝜔) def

= 𝐮𝑇 (𝜔) (𝜔),Ω𝑇 (𝜔) def

= Ω𝑇 (𝜔) (𝜔), 𝑌𝑇 (𝜔) def= 𝑌𝑇 (𝜔) (𝜔). Note that𝐮𝑇 ,Ω𝑇 , 𝑌𝑇

are not guaranteed to be well-defined everywhere.

Lemma D.1. If ℙ[𝑇 < ∞] = 1, i.e., the program termi-nates almost surely, then 𝐮𝑇 is well-defined almost surely andℙ[lim𝑛→∞𝐮𝑛 = 𝐮𝑇 ] = 1. Further, if {𝐮𝑛}đ‘›âˆˆâ„€+ is pointwisenon-decreasing, then lim𝑛→∞ đ”Œ[𝐮𝑛] = đ”Œ[𝐮𝑇 ].Proof. By the property of the operational semantics, for 𝜔 ∈Ω such that 𝑇 (𝜔) < ∞, we have 𝐮𝑛 (𝜔) = 𝐮𝑇 (𝜔) for all𝑛 ≄ 𝑇 (𝜔). Then we have

ℙ[ lim𝑛→∞𝐮𝑛 = 𝐮𝑇 ]

= ℙ({𝜔 | lim𝑛→∞𝐮𝑛 (𝜔) = 𝐮𝑇 (𝜔)})

≄ ℙ({𝜔 | lim𝑛→∞𝐮𝑛 (𝜔) = 𝐮𝑇 (𝜔) ∧𝑇 (𝜔) < ∞})

= ℙ({𝜔 | 𝐮𝑇 (𝜔) (𝜔) = 𝐮𝑇 (𝜔) ∧𝑇 (𝜔) < ∞})= ℙ({𝜔 | 𝑇 (𝜔) < ∞})= 1.

Now let us assume that {𝐮𝑛}đ‘›âˆˆâ„€+ is pointwise non-decreasing. By the property of the operational semantics, weknow that 𝐮0 = 0. Therefore, 𝐮𝑛’s are nonnegative randomvariables, and their expectations đ”Œ[𝐮𝑛]’s are well-defined.By Prop. A.3, we have lim𝑛→∞ đ”Œ[𝐮𝑛] = đ”Œ[lim𝑛→∞𝐮𝑛]. Wethen conclude by the fact that lim𝑛→∞𝐮𝑛 = 𝐮𝑇 , a.s., whichwe just proved. □

We reformulate the martingale property using the filtra-tion {F𝑛}đ‘›âˆˆâ„€+ .

Lemma D.2. For all 𝑛 ∈ â„€+, it holds thatđ”Œ[𝑌𝑛+1 | F𝑛] = 𝑌𝑛, a.s.,

i.e., the expectation of 𝑌𝑛 conditioned on the execution historyis an invariant for 𝑛 ∈ â„€+.

Proof. We say that a sequence of random variables {𝑋𝑛}đ‘›âˆˆâ„€+

is adapted to a filtration {F𝑛}đ‘›âˆˆâ„€+ if for each 𝑛 ∈ â„€+, 𝑋𝑛 isF𝑛-measurable. Then {Ω𝑛}đ‘›âˆˆâ„€+ and {𝐮𝑛}đ‘›âˆˆâ„€+ are adaptedto the coordinate-generated filtration {F𝑛}đ‘›âˆˆâ„€+ as Ω𝑛 (𝜔)and 𝐮𝑛 (𝜔) depend on 𝜔𝑛 . Then we have

đ”Œ[𝑌𝑛+1 | F𝑛] (𝜔)= đ”Œ[𝐮𝑛+1 + Ω𝑛+1 | F𝑛] (𝜔)= đ”Œ[(𝐮𝑛+1 −𝐮𝑛) + Ω𝑛+1 +𝐮𝑛 | F𝑛] (𝜔)= đ”Œ[(𝐮𝑛+1 −𝐮𝑛) + Ω𝑛+1 | F𝑛] (𝜔) +𝐮𝑛 (𝜔)= đ”Œ[(đ›Œđ‘›+1 − đ›Œđ‘›) + 𝜙 (𝜔𝑛+1) | F𝑛] +𝐮𝑛 (𝜔)= đ”ŒđœŽâ€Č∌↊→(𝜔𝑛) [(đ›Œ â€Č − đ›Œđ‘›) + 𝜙 (𝜎 â€Č)] +𝐮𝑛 (𝜔)= 𝜙 (𝜔𝑛) +𝐮𝑛 (𝜔)= Ω𝑛 (𝜔) +𝐮𝑛 (𝜔)= 𝑌𝑛 (𝜔), a.s.

21

Page 22: Central Moment Analysis for Cost Accumulators in

PLDI ’21, June 20–25, 2021, Virtual, UK Di Wang, Jan Hoffmann, and Thomas Reps

Furthermore, we have the following corollary:đ”Œ[𝑌𝑛+1] = đ”Œ[đ”Œ(𝑌𝑛+1 | F𝑛)] = đ”Œ[𝑌𝑛],

for each 𝑛 ∈ â„€+, thus đ”Œ[𝑌𝑛] = đ”Œ[𝑌0] for all 𝑛 ∈ â„€+. □

Now we can prove soundness of the extended OST.

Theorem D.3. If đ”Œ[|𝑌𝑛 |] < ∞ for all 𝑛 ∈ â„€+, then đ”Œ[𝑌𝑇 ]exists and đ”Œ[𝑌𝑇 ] = đ”Œ[𝑌0] in the following situation:There exist ℓ ∈ ℕ and đ¶ ≄ 0 such that đ”Œ[𝑇 ℓ ] < ∞ and for

all 𝑛 ∈ â„€+, |𝑌𝑛 | ≀ đ¶ · (𝑛 + 1)ℓ almost surely.

Proof. By đ”Œ[𝑇 ℓ ] < ∞ where ℓ ≄ 1, we know that ℙ[𝑇 <

∞] = 1. Then similar to the proof of Lem. D.1, we know that𝑌𝑇 is well-defined almost surely and ℙ[lim𝑛→∞ 𝑌𝑛 = 𝑌𝑇 ] = 1.On the other hand, we have|𝑌𝑛 | = |𝑌min(𝑇,𝑛) | ≀ đ¶ · (min(𝑇, 𝑛) + 1)ℓ ≀ đ¶ · (𝑇 + 1)ℓ , a.s.Recall thatđ”Œ[𝑇 ℓ ] < ∞. Thenđ”Œ[(𝑇+1)ℓ ] = đ”Œ[𝑇 ℓ+𝑂 (𝑇 ℓ−1)] <∞. By Prop. A.4, with the function 𝑔 set to _𝜔.đ¶ · (𝑇 (𝜔) +1)ℓ ,we know that lim𝑛→∞ đ”Œ[𝑌𝑛] = đ”Œ[𝑌𝑇 ]. By Lem. D.2, we haveđ”Œ[𝑌𝑛] = đ”Œ[𝑌0] for all 𝑛 ∈ â„€+ thus we conclude that đ”Œ[𝑌0] =đ”Œ[𝑌𝑇 ]. □

E Trace-Based Reasoning on MomentsWe start with the fundamental composition property formoment semirings.

Lemma (Lem. 3.2). For all 𝑱, 𝑣 ∈ R, it holds that⟹1, (𝑱 + 𝑣), (𝑱 + 𝑣)2, · · · , (𝑱 + 𝑣)đ‘šâŸ©

= ⟹1, 𝑱,𝑱2, · · · , 𝑱𝑚) ⊗ (1, 𝑣, 𝑣2, · · · , đ‘Łđ‘šâŸ©,where 𝑱𝑛 is an abbreviation for

∏𝑛𝑖=1 𝑱 for 𝑛 ∈ â„€+, 𝑱 ∈ R.

Proof. Observe that

đ‘…đ»đ‘†đ‘˜ =

đ‘˜âˆ‘ïžđ‘–=0

(𝑘

𝑖

)× (𝑱𝑖 · 𝑣𝑘−𝑖 ).

We prove by induction on 𝑘 that (𝑱 + 𝑣)𝑘 = đ‘…đ»đ‘†đ‘˜ .‱ 𝑘 = 0: Then (𝑱 + 𝑣)0 = 1. On the other hand, we have

đ‘…đ»đ‘†0 =

(00

)× (𝑱0 · 𝑣0) = 1 × (1 · 1) = 1.

‱ Suppose that (𝑱 + 𝑣)𝑘 = đ‘…đ»đ‘†đ‘˜ . Then(𝑱 + 𝑣)𝑘+1

= (𝑱 + 𝑣) · (𝑱 + 𝑣)𝑘

= (𝑱 + 𝑣) Â·đ‘˜âˆ‘ïžđ‘–=0

(𝑘

𝑖

)× (𝑱𝑖 · 𝑣𝑘−𝑖 )

=

đ‘˜âˆ‘ïžđ‘–=0

(𝑘

𝑖

)× (𝑱𝑖+1 · 𝑣𝑘−𝑖 ) +

đ‘˜âˆ‘ïžđ‘–=0

(𝑘

𝑖

)× (𝑱𝑖 · 𝑣𝑘−𝑖+1)

=

𝑘+1âˆ‘ïžđ‘–=1

(𝑘

𝑖 − 1

)× (𝑱𝑖 · 𝑣𝑘−𝑖+1) +

đ‘˜âˆ‘ïžđ‘–=0

(𝑘

𝑖

)× (𝑱𝑖 · 𝑣𝑘−𝑖+1)

=

𝑘+1âˆ‘ïžđ‘–=0

((𝑘

𝑖 − 1

)+(𝑘

𝑖

)) × (𝑱𝑖 · 𝑣𝑘−𝑖+1)

=

𝑘+1âˆ‘ïžđ‘–=0

(𝑘 + 1𝑖

)× (𝑱𝑘 · 𝑣𝑘−𝑖+1)

= đ‘…đ»đ‘†đ‘˜+1.□

We also show that ⊗ and ⊕ are monotone if the operationsof the underlying semiring are monotone.

Lemma E.1. LetR = ( |R|, ≀, +, ·, 0, 1) be a partially orderedsemiring. If + and · are monotone with respect to ≀, then ⊗and ⊕ in the moment semiring M(𝑚)

R are also monotone withrespect to ⊑.Proof. It is straightforward to show ⊕ is monotone. Forthe rest of the proof, without loss of generality, we showthat

âˆ’âˆ’âˆ’âˆ’âˆ’âˆ’âˆ’âˆ’âˆ’â†’âŸšđ‘ąđ‘˜âŸ©0â‰€đ‘˜â‰€đ‘š ⊗ âˆ’âˆ’âˆ’âˆ’âˆ’âˆ’âˆ’âˆ’â†’âŸšđ‘Łđ‘˜âŸ©0â‰€đ‘˜â‰€đ‘š ⊑ âˆ’âˆ’âˆ’âˆ’âˆ’âˆ’âˆ’âˆ’âˆ’â†’âŸšđ‘ąđ‘˜âŸ©0â‰€đ‘˜â‰€đ‘š ⊗ âˆ’âˆ’âˆ’âˆ’âˆ’âˆ’âˆ’âˆ’âˆ’â†’âŸšđ‘€đ‘˜âŸ©0â‰€đ‘˜â‰€đ‘š ifâˆ’âˆ’âˆ’âˆ’âˆ’âˆ’âˆ’âˆ’â†’âŸšđ‘Łđ‘˜âŸ©0â‰€đ‘˜â‰€đ‘š ⊑ âˆ’âˆ’âˆ’âˆ’âˆ’âˆ’âˆ’âˆ’âˆ’â†’âŸšđ‘€đ‘˜âŸ©0â‰€đ‘˜â‰€đ‘š . By the definition of ⊑, we knowthat 𝑣𝑘 ≀ đ‘€đ‘˜ for all 𝑘 = 0, 1, · · · ,𝑚. Then for each 𝑘 , we have

(âˆ’âˆ’âˆ’âˆ’âˆ’âˆ’âˆ’âˆ’âˆ’â†’âŸšđ‘ąđ‘˜âŸ©0â‰€đ‘˜â‰€đ‘š ⊗ âˆ’âˆ’âˆ’âˆ’âˆ’âˆ’âˆ’âˆ’â†’âŸšđ‘Łđ‘˜âŸ©0â‰€đ‘˜â‰€đ‘š)𝑘

=

đ‘˜âˆ‘ïžđ‘–=0

(𝑘

𝑖

)× (𝑱𝑖 · 𝑣𝑘−𝑖 )

â‰€đ‘˜âˆ‘ïžđ‘–=0

(𝑘

𝑖

)× (𝑱𝑖 Â·đ‘€đ‘˜âˆ’đ‘– )

= (âˆ’âˆ’âˆ’âˆ’âˆ’âˆ’âˆ’âˆ’âˆ’â†’âŸšđ‘ąđ‘˜âŸ©0â‰€đ‘˜â‰€đ‘š ⊗ âˆ’âˆ’âˆ’âˆ’âˆ’âˆ’âˆ’âˆ’âˆ’â†’âŸšđ‘€đ‘˜âŸ©0â‰€đ‘˜â‰€đ‘š)𝑘 .Then we conclude by the definition of ⊑. □

As we allow potential functions to be interval-valued, weshow that the interval semiring I satisfies the monotonicityrequired in Lem. E.1.

Lemma E.2. The operations +I and ·I are monotone withrespect to ≀I .

Proof. It is straightforward to show +I is monotone. For therest of the proof, it suffices to show that [𝑎, 𝑏] ·I [𝑐, 𝑑] ≀I[𝑎â€Č, 𝑏 â€Č] ·I [𝑐, 𝑑] if [𝑎, 𝑏] ≀I [𝑎â€Č, 𝑏 â€Č], i.e., [𝑎, 𝑏] ⊆ [𝑎â€Č, 𝑏 â€Č] or𝑎 ≄ 𝑎â€Č, 𝑏 ≀ 𝑏 â€Č.

We claim that min 𝑆𝑎,𝑏,𝑐,𝑑 ≄ min 𝑆𝑎â€Č,𝑏â€Č,𝑐,𝑑 , i.e.,min{𝑎𝑐, 𝑎𝑑, 𝑏𝑐, 𝑏𝑑} ≄ min{𝑎â€Č𝑐, 𝑎â€Č𝑑,𝑏 â€Č𝑐, 𝑏 â€Č𝑑}.‱ If 0 ≀ 𝑐 ≀ 𝑑 : Then 𝑎𝑐 ≀ 𝑏𝑐 , 𝑎𝑑 ≀ 𝑏𝑑 , 𝑎â€Č𝑐 ≀ 𝑏 â€Č𝑐 , 𝑎â€Č𝑑 ≀ 𝑏 â€Č𝑑 .It then suffices to show that min{𝑎𝑐, 𝑎𝑑} ≄ min{𝑎â€Č𝑐, 𝑎â€Č𝑑}.Because 𝑑 ≄ 𝑐 ≄ 0 and 𝑎 ≄ 𝑎â€Č, we conclude that 𝑎𝑐 ≄ 𝑎â€Č𝑐and 𝑎𝑑 ≄ 𝑎â€Č𝑑 .

‱ If 𝑐 < 0 ≀ 𝑑 : Then 𝑎𝑐 ≄ 𝑏𝑐 , 𝑎𝑑 ≀ 𝑏𝑑 , 𝑎â€Č𝑐 ≄ 𝑏 â€Č𝑐 , 𝑎â€Č𝑑 ≄ 𝑏 â€Č𝑑 .It then suffices to show that min{𝑏𝑐, 𝑎𝑑} ≄ min{𝑏 â€Č𝑐, 𝑎â€Č𝑑}.Because 𝑑 ≄ 0 > 𝑐 and 𝑎 ≄ 𝑎â€Č, 𝑏 ≀ 𝑏 â€Č, we conclude that𝑏𝑐 ≄ 𝑏 â€Č𝑐 and 𝑎𝑑 ≀ 𝑎â€Č𝑑 .

‱ If 𝑐 ≀ 𝑑 < 0: Then 𝑎𝑐 ≄ 𝑏𝑐 , 𝑎𝑑 ≄ 𝑏𝑑 , 𝑎â€Č𝑐 ≄ 𝑏 â€Č𝑐 , 𝑎â€Č𝑑 ≄ 𝑏 â€Č𝑑 .It then suffices to show that min{𝑏𝑐, 𝑏𝑑} ≄ min{𝑏 â€Č𝑐, 𝑏 â€Č𝑑}.Because 0 > 𝑑 ≄ 𝑐 and 𝑏 ≀ 𝑏 â€Č, we conclude that 𝑏𝑐 ≄ 𝑏 â€Č𝑐and 𝑏𝑑 ≄ 𝑏 â€Č𝑑 .In a similar way, we can also prove that max 𝑆𝑎,𝑏,𝑐,𝑑 ≀

max 𝑆𝑎â€Č,𝑏â€Č,𝑐,𝑑 . Therefore, we show that ·I is monotone. □22

Page 23: Central Moment Analysis for Cost Accumulators in

Central Moment Analysis for Cost Accumulators in Probabilistic Programs PLDI ’21, June 20–25, 2021, Virtual, UK

Lemma E.3. If {[𝑎𝑛, 𝑏𝑛]}đ‘›âˆˆâ„€+ is a montone sequence in I ,i.e., [𝑎0, 𝑏0] ≀I [𝑎1, 𝑏1] ≀I · · · ≀I [𝑎𝑛, 𝑏𝑛] ≀I · · · , and[𝑎𝑛, 𝑏𝑛] ≀I [𝑐, 𝑑] for all 𝑛 ∈ â„€+. Let [𝑎, 𝑏] = lim𝑛→∞ [𝑎𝑛, 𝑏𝑛](the limit is well-defined by the monotone convergence theoremfor series). Then [𝑎, 𝑏] ≀I [𝑐, 𝑑].Proof. By the definition of ≀I , we know that {𝑎𝑛}đ‘›âˆˆâ„€+ is non-increasing and {𝑏𝑛}đ‘›âˆˆâ„€+ is non-decreasing. Because 𝑎𝑛 ≄ 𝑐for all 𝑛 ∈ â„€+, we conclude that lim𝑛→∞ 𝑎𝑛 ≄ 𝑐 . Because𝑏𝑛 ≀ 𝑑 for all 𝑛 ∈ â„€+, we conclude that lim𝑛→∞ 𝑏𝑛 ≀ 𝑑 . Thuswe conclude that [𝑎, 𝑏] ≀I [𝑐, 𝑑]. □

Recall that we extend the notions of 𝐮𝑛,Ω𝑛, 𝑌𝑛 with inter-vals for higher moments as follows:

𝐮𝑛 (𝜔) def=−−−−−−−−−−−−−−→⟹[đ›Œđ‘˜đ‘› , đ›Œđ‘˜đ‘› ]⟩0â‰€đ‘˜â‰€đ‘š where 𝜔𝑛 = ⟹_, _, _, đ›Œđ‘›âŸ©,

Ω𝑛 (𝜔) def= 𝜙 (𝜔𝑛),

𝑌𝑛 (𝜔) def= 𝐮𝑛 (𝜔) ⊗ Ω𝑛 (𝜔).

Note that in the definition of 𝑌𝑛 , we use ⊗ to compose thepowers of the accumulated cost at step 𝑛 and the potentialfunction that stands for the moments of the accumulatedcost of the rest of the computation.We now extend some of the previous results on first mo-

ments to higher moments with intervals.

Lemma E.4. Let 𝑋,𝑌 : Ω → M(𝑚)I be integrable. Then

đ”Œ[𝑋 ⊕ 𝑌 ] = đ”Œ[𝑋 ] ⊕ đ”Œ[𝑌 ].Proof. Appeal to linearity of expectations and the fact that⊕ is the pointwise extension of +I , as well as +I is thepointwise extension of numeric addition. □

Lemma E.5. If 𝑋 : Ω → M(𝑚)I is G-measurable and

bounded, a.s., as well as 𝑋 (𝜔) = −−−−−−−−−−−−−−−−−−−−−→⟹[𝑎𝑘 (𝜔), 𝑎𝑘 (𝜔)]⟩0â‰€đ‘˜â‰€đ‘š forall 𝜔 ∈ Ω, then đ”Œ[𝑋 ⊗ 𝑌 | G] = 𝑋 ⊗ đ”Œ[𝑌 | G] almost surely.

Proof. Fix 𝜔 ∈ Ω. Let 𝑌 (𝜔) = −−−−−−−−−−−−−−−−−−−−−→⟹[𝑏𝑘 (𝜔), 𝑐𝑘 (𝜔)]⟩0â‰€đ‘˜â‰€đ‘š . Thenwe have

đ”Œ[𝑋 ⊗ 𝑌 | G] (𝜔)= đ”Œ[−−−−−−−−−−−−−−→⟹[𝑎𝑘 , 𝑎𝑘 ]⟩0â‰€đ‘˜â‰€đ‘š ⊗ −−−−−−−−−−−−−→⟹[𝑏𝑘 , 𝑐𝑘 ]⟩0â‰€đ‘˜â‰€đ‘š | G] (𝜔)

= đ”Œ[−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−→⟹∑I

𝑘𝑖=0

(𝑘𝑖

) ×I ( [𝑎𝑖 , 𝑎𝑖 ] ·I [𝑏𝑘−𝑖 , 𝑐𝑘−𝑖 ])⟩0â‰€đ‘˜â‰€đ‘š | G] (𝜔)

=âˆ’âˆ’âˆ’âˆ’âˆ’âˆ’âˆ’âˆ’âˆ’âˆ’âˆ’âˆ’âˆ’âˆ’âˆ’âˆ’âˆ’âˆ’âˆ’âˆ’âˆ’âˆ’âˆ’âˆ’âˆ’âˆ’âˆ’âˆ’âˆ’âˆ’âˆ’âˆ’âˆ’âˆ’âˆ’âˆ’âˆ’âˆ’âˆ’âˆ’âˆ’âˆ’âˆ’âˆ’âˆ’âˆ’âˆ’âˆ’âˆ’âˆ’âˆ’âˆ’â†’âŸšđ”Œ[∑I

𝑘𝑖=0

(𝑘𝑖

) ×I ( [𝑎𝑖 , 𝑎𝑖 ] ·I [𝑏𝑘−𝑖 , 𝑐𝑘−𝑖 ]) | G] (𝜔)⟩0â‰€đ‘˜â‰€đ‘š=−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−→⟹∑I

𝑘𝑖=0

(𝑘𝑖

) ×I đ”Œ[[𝑎𝑖 , 𝑎𝑖 ] ·I [𝑏𝑘−𝑖 , 𝑐𝑘−𝑖 ] | G] (𝜔)⟩0â‰€đ‘˜â‰€đ‘š .On the other hand, we have𝑋 (𝜔) ⊗ đ”Œ[𝑌 | G] (𝜔)

= ⟹𝑋0 (𝜔), · · · , 𝑋𝑚 (𝜔)⟩ ⊗ đ”Œ[⟹𝑌0, · · · , đ‘Œđ‘šâŸ© | G] (𝜔)= ⟹𝑋0 (𝜔), · · · , 𝑋𝑚 (𝜔)⟩ ⊗ âŸšđ”Œ[𝑌0 | G] (𝜔), · · · ,đ”Œ[𝑌𝑚 | G] (𝜔)⟩

=−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−→⟹∑I

𝑘𝑖=0

(𝑘𝑖

) ×I (𝑋𝑖 (𝜔) ·I đ”Œ[𝑌𝑘−𝑖 | G] (𝜔))⟩0â‰€đ‘˜â‰€đ‘š

=−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−→⟹∑I

𝑘𝑖=0

(𝑘𝑖

)×I ( [𝑎𝑖 (𝜔), 𝑎𝑖 (𝜔)] ·Iđ”Œ[[𝑏𝑘−𝑖 , 𝑐𝑘−𝑖 ] | G] (𝜔))⟩0â‰€đ‘˜â‰€đ‘š .Thus, it suffices to show that for each 𝑖 , đ”Œ[[𝑎𝑖 , 𝑎𝑖 ] ·I[𝑏𝑘−𝑖 , 𝑐𝑘−𝑖 ] | G] = [𝑎𝑖 , 𝑎𝑖 ] ·I đ”Œ[[𝑏𝑘−𝑖 , 𝑐𝑘−𝑖 ] | G] almost surely.For 𝜔 such that 𝑎𝑖 (𝜔) ≄ 0:đ”Œ[[𝑎𝑖 , 𝑎𝑖 ] ·I [𝑏𝑘−𝑖 , 𝑐𝑘−𝑖 ] | G] (𝜔)

= đ”Œ[[𝑎𝑖𝑏𝑘−𝑖 , 𝑎𝑖𝑐𝑘−𝑖 ] | G] (𝜔)= [đ”Œ[𝑎𝑖𝑏𝑘−𝑖 | G] (𝜔),đ”Œ[𝑎𝑖𝑐𝑘−𝑖 | G] (𝜔)]= [𝑎𝑖 (𝜔) · đ”Œ[𝑏𝑘−𝑖 | G] (𝜔), 𝑎𝑖 (𝜔) · đ”Œ[𝑐𝑘−𝑖 | G] (𝜔)], 𝑎.𝑠 .,= [𝑎𝑖 (𝜔), 𝑎𝑖 (𝜔)] ·I đ”Œ[[𝑏𝑘−𝑖 , 𝑐𝑘−𝑖 ] | G] (𝜔).

For 𝜔 such that 𝑎𝑖 (𝜔) < 0:đ”Œ[[𝑎𝑖 , 𝑎𝑖 ] ·I [𝑏𝑘−𝑖 , 𝑐𝑘−𝑖 ] | G] (𝜔)

= đ”Œ[[𝑎𝑖𝑐𝑘−𝑖 , 𝑎𝑖𝑏𝑘−𝑖 ] | G] (𝜔)= [đ”Œ[𝑎𝑖𝑐𝑘−𝑖 | G] (𝜔),đ”Œ[𝑎𝑖𝑏𝑘−𝑖 | G] (𝜔)]= [𝑎𝑖 (𝜔) · đ”Œ[𝑐𝑘−𝑖 | G] (𝜔), 𝑎𝑖 (𝜔) · đ”Œ[𝑏𝑘−𝑖 | G] (𝜔)], 𝑎.𝑠 .,= [𝑎𝑖 (𝜔), 𝑎𝑖 (𝜔)] ·I đ”Œ[[𝑏𝑘−𝑖 , 𝑐𝑘−𝑖 ] | G] (𝜔).

□

Lemma (Lem. 4.3). For all 𝑛 ∈ â„€+, it holds thatđ”Œ[𝑌𝑛+1 | F𝑛] ⊑ 𝑌𝑛, a.s.

Proof. Similar to the proof of Lem. D.2, we know that{𝐮𝑛}đ‘›âˆˆâ„€+ and {Ω𝑛}đ‘›âˆˆâ„€+ are adapted to {F𝑛}đ‘›âˆˆâ„€+ . By theproperty of the operational semantics, we know thatđ›Œđ‘› (𝜔) ≀ đ¶ · 𝑛 almost surely for some đ¶ ≄ 0. Then usingLem. E.5, we haveđ”Œ[𝑌𝑛+1 | F𝑛] (𝜔)

= đ”Œ[𝐮𝑛+1 ⊗ Ω𝑛+1 | F𝑛] (𝜔)

= đ”Œ[𝐮𝑛⊗−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−→⟹[(đ›Œđ‘›+1âˆ’đ›Œđ‘›)𝑘 , (đ›Œđ‘›+1âˆ’đ›Œđ‘›)𝑘 ]⟩0â‰€đ‘˜â‰€đ‘šâŠ—ÎŠđ‘›+1 | F𝑛] (𝜔)

= 𝐮𝑛 (𝜔)âŠ—đ”Œ[−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−→⟹[(đ›Œđ‘›+1âˆ’đ›Œđ‘›)𝑘 , (đ›Œđ‘›+1âˆ’đ›Œđ‘›)𝑘 ]⟩0â‰€đ‘˜â‰€đ‘šâŠ—ÎŠđ‘›+1 |F𝑛] (𝜔),

𝑎.𝑠 .,

= 𝐮𝑛 (𝜔)âŠ—đ”Œ[−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−→⟹[(đ›Œđ‘›+1âˆ’đ›Œđ‘›)𝑘 , (đ›Œđ‘›+1âˆ’đ›Œđ‘›)𝑘 ]⟩0â‰€đ‘˜â‰€đ‘šâŠ—đœ™ (𝜔𝑛+1) |F𝑛] .

Recall the property of the expected-potential function 𝜙 inDefn. 4.2. Then by Lem. E.1 with Lem. E.2, we have

đ”Œ[𝑌𝑛+1 | F𝑛] (𝜔)⊑ 𝐮𝑛 (𝜔) ⊗ 𝜙 (𝜔𝑛), 𝑎.𝑠 .,= 𝐮𝑛 (𝜔) ⊗ Ω𝑛 (𝜔)= 𝑌𝑛 .

As a corollary, we have đ”Œ[𝑌𝑛] ⊑ đ”Œ[𝑌0] for all 𝑛 ∈ â„€+. □

Now we prove the following extension of OSTto deal with interval-valued potential functions. Let∄−−−−−−−−−−−−−−→⟚[𝑎𝑘 , 𝑏𝑘 ]⟩0â‰€đ‘˜â‰€đ‘š ∄∞ def

= max0â‰€đ‘˜â‰€đ‘š{max{|𝑎𝑘 |, |𝑏𝑘 |}}.Theorem E.6. If đ”Œ[âˆ„đ‘Œđ‘› ∄∞] < ∞ for all 𝑛 ∈ â„€+, then đ”Œ[𝑌𝑇 ]exists and đ”Œ[𝑌𝑇 ] ⊑ đ”Œ[𝑌0] in the following situation:There exist ℓ ∈ ℕ and đ¶ ≄ 0 such that đ”Œ[𝑇 ℓ ] < ∞ and for

all 𝑛 ∈ â„€+, âˆ„đ‘Œđ‘› ∄∞ ≀ đ¶ · (𝑛 + 1)ℓ almost surely.

23

Page 24: Central Moment Analysis for Cost Accumulators in

PLDI ’21, June 20–25, 2021, Virtual, UK Di Wang, Jan Hoffmann, and Thomas Reps

Proof. By đ”Œ[𝑇 ℓ ] < ∞ where ℓ ≄ 1, we know that ℙ[𝑇 <

∞] = 1. Then similar to the proof of Lem. D.1, we know that𝑌𝑇 is well-defined almost surely and ℙ[lim𝑛→∞ 𝑌𝑛 = 𝑌𝑇 ] = 1.On the other hand, 𝑌𝑛 (𝜔) can be treated as a vector of realnumbers. Let 𝑎𝑛 : Ω → ℝ be a real-valued componentin 𝑌𝑛 . Because đ”Œ[âˆ„đ‘Œđ‘› ∄∞] < ∞ and âˆ„đ‘Œđ‘› ∄∞ ≀ đ¶ · (𝑛 + 1)ℓalmost surely, we know that đ”Œ[|𝑎𝑛 |] ≀ đ”Œ[âˆ„đ‘Œđ‘› ∄∞] < ∞ and|𝑎𝑛 | ≀ âˆ„đ‘Œđ‘› ∄∞ ≀ đ¶ · (𝑛 + 1)ℓ almost surely. Therefore,|𝑎𝑛 | = |𝑎min(𝑇,𝑛) | ≀ đ¶ · (min(𝑇, 𝑛) + 1)ℓ ≀ đ¶ · (𝑇 + 1)ℓ , a.s.Recall that đ”Œ[𝑇 ℓ ] < ∞. Then đ”Œ[(𝑇 + 1)ℓ ] = đ”Œ[𝑇 ℓ +𝑂 (𝑇 ℓ−1)] < ∞. By Prop. A.4, with the function 𝑔 set to_𝜔.đ¶ · (𝑇 (𝜔) + 1)ℓ , we know that lim𝑛→∞ đ”Œ[𝑎𝑛] = đ”Œ[𝑎𝑇 ].Because 𝑎𝑛 is an arbitrary real-valued component in 𝑌𝑛 , weknow that lim𝑛→∞ đ”Œ[𝑌𝑛] = đ”Œ[𝑌𝑇 ]. By Lem. 4.3, we knowthat đ”Œ[𝑌𝑛] ⊑ đ”Œ[𝑌0] for all 𝑛 ∈ â„€+. By Lem. E.3, we concludethat lim𝑛→∞ đ”Œ[𝑌𝑛] ⊑ đ”Œ[𝑌0], i.e., đ”Œ[𝑌𝑇 ] ⊑ đ”Œ[𝑌0]. □

Discussion. The classic OST from probability theory hasbeen used to reason about probabilistic programs:Proposition E.7. Let {𝑋𝑛}đ‘›âˆˆâ„€+ be a super-martingale (resp.,sub-martingale). If đ”Œ[|𝑋𝑛 |] < ∞ for all 𝑛 ∈ â„€+, then đ”Œ[𝑋𝑇 ]exists and đ”Œ[𝑋𝑇 ] ≀ đ”Œ[𝑋0] (resp., đ”Œ[𝑋𝑇 ] ≄ đ”Œ[𝑋0]) in each ofthe following situations:(a) 𝑇 is almost-surely bounded;(b) đ”Œ[𝑇 ] <∞ and for some đ¶ ≄ 0, đ”Œ[|𝑋𝑛+1 − 𝑋𝑛 | | 𝑋𝑛] ≀ đ¶

almost surely, for all 𝑛 ∈ â„€+;(c) for some đ¶ ≄ 0, |𝑋𝑛 | ≀ đ¶ almost surely, for all 𝑛 ∈ â„€+.

Note that from item (a) to item (c) in Prop. E.7, the con-straint on the stopping time 𝑇 is getting weaker while theconstraint on {𝑋𝑛}đ‘›âˆˆâ„€+ is getting stronger. However, the clas-sic OST is not suitable for higher-moment analysis: item (a)cannot handle almost-surely terminating programs; item (b)relies on bounded difference, which is too restrictive, becausethe change in second moments can be non-constant even ifthe change in first moments can be bounded by a universalconstant; and item (c) basically requires the accumulatedcost can be bounded by a universal constant, which is alsonot practical.Counterexample E.8. Recall the random-walk program inFig. 3. Below, we present the pre- and post-condition of therandom-sampling statement:

1 { ⟹1, 2(đ‘‘âˆ’đ‘„)+4, 4(đ‘‘âˆ’đ‘„)2+22(đ‘‘âˆ’đ‘„)+28⟩ }2 𝑡 ∌ uniform(−1, 2);3 { ⟹1, 2(đ‘‘âˆ’đ‘„âˆ’đ‘Ą)+5, 4(đ‘‘âˆ’đ‘„âˆ’đ‘Ą)2+26(đ‘‘âˆ’đ‘„âˆ’đ‘Ą)+37⟩ }

Suppose that we want to apply the classic OST to ensuringthe second-moment component is sound. Let us use Ίâ€Č

𝑛 and𝑌 â€Č𝑛 to denote the over-approximations of the second momentin Ω𝑛 and 𝑌𝑛 , respectively. It has been shown that for sucha random walk, the stopping time 𝑇 is not bounded butđ”Œ[𝑇 ] < ∞ [26, 31]. Thus, the applicable OST criterion couldbe Prop. E.7(b), which requires someđ¶ > 0 such that |𝑌 â€Č

𝑛+1 −𝑌 â€Č𝑛 | ≀ đ¶ for all 𝑛 ∈ â„€+.

Consider the case where the 𝑛-th evaluation step of a trace𝜔 executes the random-sampling statement. Let đ›Œđ‘œ

def= 𝐮𝑛 (𝜔).

By the definition that 𝑌𝑛 (𝜔) def=−−−−−−−−−−−−−−→([đ›Œđ‘˜đ‘œ , đ›Œđ‘˜đ‘œ ])0â‰€đ‘˜â‰€đ‘š ⊗Ω𝑛 (𝜔), we

have2

𝑌 â€Č𝑛 (𝜔)

= đ›Œ2𝑜+2 · đ›Œđ‘œ · (2(đ‘‘âˆ’đ‘„)+4)+(4(đ‘‘âˆ’đ‘„)2+22(đ‘‘âˆ’đ‘„)+28),𝑌 â€Č𝑛+1 (𝜔)

= đ›Œ2𝑜+2Â·đ›Œđ‘œ · (2(đ‘‘âˆ’đ‘„âˆ’đ‘Ą)+5)+(4(đ‘‘âˆ’đ‘„âˆ’đ‘Ą)2+26(đ‘‘âˆ’đ‘„âˆ’đ‘Ą)+37),thus

𝑌 â€Č𝑛+1 (𝜔)−𝑌 â€Č

𝑛 (𝜔)= 2Â·đ›Œđ‘œ · (−2𝑡+1)+(−4𝑡 (2𝑑−2đ‘„âˆ’đ‘Ą)+4(đ‘‘âˆ’đ‘„)−26𝑡+9),

where 𝑡 ∈ [−1, 2], 𝑑 is a constant, but đ›Œđ‘œ and đ‘„ are generallyunbounded, thus |𝑌 â€Č

𝑛+1−𝑌 â€Č𝑛 | cannot be bounded by a constant.

F Soundness of Bound InferenceFig. 14 presents the inference rules of the derivation system.Note that we prove the soundness on a more declarativesystemwhere the two rules for function calls are merged intoone. The validity of a context Δ for function specificationsis then established by the validity of all specifications in Δ,denoted by ⊱ Δ.In addition to rules of the judgments for statements and

function specifications, we also include rules for continu-ations and configurations that are used in the operationalsemantics. A continuation đŸ is valid with a pre-condition{Γ;𝑄}, written Δ ⊱ {Γ;𝑄} đŸ , if 𝜙𝑄 describes a boundon the moments of the accumulated cost of the compu-tation represented by đŸ on the condition that the valua-tion before đŸ satisfies Γ. Validity for configurations, writtenΔ ⊱ {Γ;𝑄} âŸšđ›Ÿ, 𝑆, đŸ, đ›ŒâŸ©, is established by validity of the state-ment 𝑆 and the continuation đŸ , as well as the requirementthat the valuation đ›Ÿ satisfies the pre-condition Γ. Here 𝜙𝑄also describes an interval bound on the moments of the ac-cumulated cost of the computation that continues from theconfiguration âŸšđ›Ÿ, 𝑆, đŸ, đ›ŒâŸ©.The rule (Q-Weaken) and (QK-Weaken) are used to

strengthen the pre-condition and relax the post-condition.In terms of the bounds on moments of the accumulated cost,if the triple {·;𝑄} 𝑆 {·;𝑄 â€Č} is valid, then we can safely widenthe intervals in the pre-condition𝑄 and narrow the intervalsin the post-condition𝑄 â€Č. To handle the judgment Γ |= 𝑄 ⊒ 𝑄 â€Č

in constraint generation, we adapt the idea of rewrite func-tions [9, 31]. Intuitively, to ensure that [𝐿1,𝑈1] ⊒PI [𝐿2,𝑈2],i.e., 𝐿1 ≀ 𝐿2 and 𝑈2 ≀ 𝑈1, under the logical context Γ, wegenerate constraints indicating that there exist two polyno-mials 𝑇1,𝑇2 that are always nonnegative under Γ, such that𝐿1 = 𝐿2 + 𝑇1 and 𝑈1 = 𝑈2 − 𝑇2. In our implementation, Γis a set of linear constraints over program variables of the2When we use a program variable đ‘„ in 𝑌𝑛 (𝜔) , it represents đ›Ÿ (đ‘„) where𝜔𝑛 = âŸšđ›Ÿ, _, _, _⟩.

24

Page 25: Central Moment Analysis for Cost Accumulators in

Central Moment Analysis for Cost Accumulators in Probabilistic Programs PLDI ’21, June 20–25, 2021, Virtual, UK

(Valid-Ctx)∀𝑓 ∈ dom(Δ) : ∀(Γ;𝑄, Γ;𝑄 â€Č) ∈ Δ(𝑓 ) : Δ ⊱ {Γ;𝑄} 𝒟(𝑓 ) {Γâ€Č;𝑄 â€Č}

⊱ Δ

(Q-Skip)

Δ ⊱ {Γ;𝑄} skip {Γ;𝑄}

(Q-Tick)

𝑄 =−−−−−−−−−−−−−−→⟹[𝑐𝑘 , 𝑐𝑘 ]⟩0â‰€đ‘˜â‰€đ‘š ⊗ 𝑄 â€Č

Δ ⊱ {Γ;𝑄} tick(𝑐) {Γ;𝑄 â€Č}(Q-Assign)Γ = [𝐾/đ‘„]Γâ€Č 𝑄 = [𝐾/đ‘„]𝑄 â€Č

Δ ⊱ {Γ;𝑄} đ‘„ B 𝐾 {Γâ€Č;𝑄 â€Č}

(Q-Sample)Γ = âˆ€đ‘„ ∈ supp(`đ· ) : Γâ€Č 𝑄 = đ”Œđ‘„âˆŒ`đ· [𝑄 â€Č]

Δ ⊱ {Γ;𝑄} đ‘„ ∌ đ· {Γâ€Č;𝑄 â€Č}

(Q-Loop)Δ ⊱ {Γ ∧ 𝐿;𝑄} 𝑆1 {Γ;𝑄}

Δ ⊱ {Γ;𝑄} while 𝐿 do 𝑆1 od {Γ ∧ ¬𝐿;𝑄}(Q-Cond)

Δ ⊱ {Γ ∧ 𝐿;𝑄} 𝑆1 {Γâ€Č;𝑄 â€Č}Δ ⊱ {Γ ∧ ¬𝐿;𝑄} 𝑆2 {Γâ€Č;𝑄 â€Č}

Δ ⊱ {Γ;𝑄} if 𝐿 then 𝑆1 else 𝑆2 fi {Γâ€Č;𝑄 â€Č}

(Q-Seq)Δ ⊱ {Γ;𝑄} 𝑆1 {Γâ€Č;𝑄 â€Č}

Δ ⊱ {Γâ€Č;𝑄 â€Č} 𝑆2 {Γâ€Čâ€Č;𝑄 â€Čâ€Č}Δ ⊱ {Γ;𝑄} 𝑆1; 𝑆2 {Γâ€Čâ€Č;𝑄 â€Čâ€Č}

(Q-Call)∀𝑖 : (Γ;𝑄𝑖 , Γâ€Č;𝑄 â€Č

𝑖 ) ∈ Δ(𝑓 )Δ ⊱ {Γ;⊕𝑖 𝑄𝑖 } call 𝑓 {Γâ€Č;⊕𝑖 𝑄

â€Č𝑖}

(Q-Prob)Δ ⊱ {Γ;𝑄1} 𝑆1 {Γâ€Č;𝑄 â€Č} Δ ⊱ {Γ;𝑄2} 𝑆2 {Γâ€Č;𝑄 â€Č}

𝑄 = 𝑃 ⊕ 𝑅 𝑃 = ⟹[𝑝, 𝑝], [0, 0], · · · , [0, 0]⟩ ⊗ 𝑄1 𝑅 = ⟹[1 − 𝑝, 1 − 𝑝], [0, 0], · · · , [0, 0]⟩ ⊗ 𝑄2

Δ ⊱ {Γ;𝑄} if prob(𝑝) then 𝑆1 else 𝑆2 fi {Γâ€Č;𝑄 â€Č}(Q-Weaken)Δ ⊱ {Γ0;𝑄0} 𝑆 {Γâ€Č0 ;𝑄 â€Č

0} Γ |= Γ0 Γâ€Č0 |= Γâ€Č Γ |= 𝑄 ⊒ 𝑄0 Γâ€Č0 |= 𝑄 â€Č0 ⊒ 𝑄 â€Č

Δ ⊱ {Γ;𝑄} 𝑆 {Γâ€Č;𝑄 â€Č}(Valid-Cfg)đ›Ÿ |= Γ Δ ⊱ {Γ;𝑄} 𝑆 {Γâ€Č;𝑄 â€Č} Δ ⊱ {Γâ€Č;𝑄 â€Č} đŸ

Δ ⊱ {Γ;𝑄} âŸšđ›Ÿ, 𝑆, đŸ, đ›ŒâŸ©(QK-Stop)

Δ ⊱ {Γ;𝑄} Kstop

(QK-Loop)Δ ⊱ {Γ ∧ 𝐿;𝑄} 𝑆 {Γ;𝑄} Δ ⊱ {Γ ∧ ¬𝐿;𝑄} đŸ

Δ ⊱ {Γ;𝑄} Kloop 𝐿 𝑆 đŸ(QK-Seq)Δ ⊱ {Γ;𝑄} 𝑆 {Γâ€Č;𝑄 â€Č} Δ ⊱ {Γâ€Č;𝑄 â€Č} đŸ

Δ ⊱ {Γ;𝑄} Kseq 𝑆 đŸ

(QK-Weaken)Δ ⊱ {Γâ€Č;𝑄 â€Č} đŸ Γ |= Γâ€Č Γ |= 𝑄 ⊒ 𝑄 â€Č

Δ ⊱ {Γ;𝑄} đŸ

Figure 14. Inference rules of the derivation system.

form E ≄ 0 , then we can represent 𝑇1,𝑇2 by conical combi-nations (i.e., linear combinations with nonnegative scalars)of expressions E in Γ.To reduce the soundness proof to the extended OST

for interval-valued bounds, we construct an annotatedtransition kernel from validity judgements ⊱ Δ and Δ ⊱{Γ;𝑄} 𝑆main {Γâ€Č;𝑄 â€Č}.Lemma F.1. Suppose ⊱ Δ and Δ ⊱ {Γ;𝑄} 𝑆main {Γâ€Č;𝑄 â€Č}.An annotated program configuration has the form⟹Γ, 𝑄,đ›Ÿ, 𝑆, đŸ, đ›ŒâŸ© such that Δ ⊱ {Γ;𝑄} âŸšđ›Ÿ, 𝑆, đŸ, đ›ŒâŸ©. Thenthere exists a probability kernel ^ over annotated programconfigurations such that:

For all 𝜎 = ⟹Γ, 𝑄,đ›Ÿ, 𝑆, đŸ, đ›ŒâŸ© ∈ dom(^), it holds that(i) ^ is the same as the evaluation relation ↩→ if the annotations

are omitted, i.e.,^ (𝜎)≫=_⟹_, _, đ›Ÿ â€Č, 𝑆 â€Č, đŸ â€Č, đ›Œ â€Č⟩.𝛿 (âŸšđ›Ÿ â€Č, 𝑆 â€Č, đŸ â€Č, đ›Œ â€Č⟩) = ↩→(âŸšđ›Ÿ, 𝑆, đŸ, đ›ŒâŸ©),

and(ii) 𝜙𝑄 (đ›Ÿ) ⊒ đ”ŒđœŽâ€Č∌^ (𝜎) [

−−−−−−−−−−−−−−−−−−−−−−−−−−→⟹[(đ›Œ â€Čâˆ’đ›Œ)𝑘 , (đ›Œ â€Čâˆ’đ›Œ)𝑘 ]⟩0â‰€đ‘˜â‰€đ‘šâŠ—đœ™đ‘„â€Č (đ›Ÿ â€Č)]

where 𝜎 â€Č = ⟹_, 𝑄 â€Č, đ›Ÿ â€Č, _, _, đ›Œ â€Č⟩.Before proving the soundness, we show that the derivation

system for bound inference admits a relaxation rule.

Lemma F.2. Suppose that ⊱ Δ. If Δ ⊱ {Γ;𝑄1} 𝑆 {Γâ€Č;𝑄 â€Č1}

and Δ ⊱ {Γ;𝑄2} 𝑆 {Γâ€Č;𝑄 â€Č2}, then the judgment Δ ⊱ {Γ;𝑄1 ⊕

𝑄2} 𝑆 {Γâ€Č;𝑄 â€Č1 ⊕ 𝑄 â€Č

2} is derivable.Proof. By nested induction on the derivation of the judg-ment Δ ⊱ {Γ;𝑄1} 𝑆 {Γâ€Č;𝑄 â€Č

1}, followed by inversion onΔ ⊱ {Γ;𝑄2} 𝑆 {Γâ€Č;𝑄 â€Č

2}. We assume the derivations havethe same shape and the same logical contexts; in practice, wecan ensure this by explicitly inserting weakening positions,e.g., all the branching points, and by doing a first pass toobtain logical contexts.

‱(Q-Skip)

Δ ⊱ {Γ;𝑄1} skip {Γ;𝑄1}By inversion, we have 𝑄2 = 𝑄

â€Č2. By (Q-Skip), we immedi-

ately have Δ ⊱ {Γ;𝑄1 ⊕ 𝑄2} skip {Γ;𝑄1 ⊕ 𝑄2}.

‱

(Q-Tick)

𝑄1 =−−−−−−−−−−−−−−→⟹[𝑐𝑘 , 𝑐𝑘 ]⟩0â‰€đ‘˜â‰€đ‘š ⊗ 𝑄 â€Č

1Δ ⊱ {Γ;𝑄1} tick(𝑐) {Γ;𝑄 â€Č

1}By inversion, we have 𝑄2 =

−−−−−−−−−−−−−→⟹[𝑐𝑘 , 𝑐𝑘 ]⟩0â‰€đ‘˜â‰€đ‘š ⊗ 𝑄 â€Č

2. By

distributivity, we have−−−−−−−−−−−−−→⟹[𝑐𝑘 , 𝑐𝑘 ]⟩0â‰€đ‘˜â‰€đ‘š ⊗ (𝑄 â€Č

1 ⊕ 𝑄 â€Č2) =

(−−−−−−−−−−−−−→⟹[𝑐𝑘 , 𝑐𝑘 ]⟩0â‰€đ‘˜â‰€đ‘š ⊗𝑄 â€Č

1) ⊕ (−−−−−−−−−−−−−→⟹[𝑐𝑘 , 𝑐𝑘 ]⟩0â‰€đ‘˜â‰€đ‘š ⊕𝑄 â€Č

2) = 𝑄1⊕𝑄2.Then we conclude by (Q-Tick).

25

Page 26: Central Moment Analysis for Cost Accumulators in

PLDI ’21, June 20–25, 2021, Virtual, UK Di Wang, Jan Hoffmann, and Thomas Reps

‱

(Q-Assign)Γ = [𝐾/đ‘„]Γâ€Č 𝑄1 = [𝐾/đ‘„]𝑄 â€Č

1Δ ⊱ {Γ;𝑄1} đ‘„ B 𝐾 {Γâ€Č;𝑄 â€Č

1}By inversion, we have 𝑄2 = [𝐾/đ‘„]𝑄 â€Č

2. Then we know that[𝐾/đ‘„] (𝑄 â€Č

1 ⊕ 𝑄 â€Č2) = [𝐾/đ‘„]𝑄 â€Č

1 ⊕ [𝐾/đ‘„]𝑄 â€Č2 = 𝑄1 ⊕ 𝑄2. Then

we conclude by (Q-Assign).

‱

(Q-Sample)Γ = âˆ€đ‘„ ∈ supp(`đ· ) : Γâ€Č 𝑄1 = đ”Œđ‘„âˆŒ`đ· [𝑄 â€Č

1]Δ ⊱ {Γ;𝑄1} đ‘„ ∌ đ· {Γâ€Č;𝑄 â€Č

1}By inversion, we have 𝑄2 = đ”Œđ‘„âˆŒ`đ· [𝑄 â€Č

2]. By Lem. E.4, weknow that đ”Œđ‘„âˆŒ`đ· [𝑄 â€Č

1 ⊕ 𝑄 â€Č2] = đ”Œđ‘„âˆŒ`đ· [𝑄 â€Č

1] ⊕ đ”Œđ‘„âˆŒ`đ· [𝑄 â€Č2] =

𝑄1 ⊕ 𝑄2. Then we conclude by (Q-Sample).

‱

(Q-Call)∀𝑖 : (Γ;𝑄1𝑖 , Γ

â€Č;𝑄 â€Č1𝑖 ) ∈ Δ(𝑓 )

Δ ⊱ {Γ;⊕𝑖 𝑄1𝑖 } call 𝑓 {Γâ€Č;⊕𝑖 𝑄â€Č1𝑖 }

By inversion, 𝑄2 =⊕

𝑗 𝑄2𝑗 and 𝑄 â€Č2 =

⊕𝑗 𝑄

â€Č2𝑗 where

(Γ;𝑄2𝑗 , Γâ€Č;𝑄 â€Č

2𝑗 ) ∈ Δ(𝑓 ) for each 𝑗 . Then by (Q-Call), wehave Δ ⊱ {Γ;⊕𝑖 𝑄1𝑖 ⊕

⊕𝑗 𝑄2𝑗 } call 𝑓 {Γâ€Č;⊕𝑖 𝑄

â€Č1𝑖 ⊕⊕

𝑗 𝑄â€Č2𝑗 }.

‱

(Q-Prob)Δ ⊱ {Γ;𝑄11} 𝑆1 {Γâ€Č;𝑄 â€Č

1} Δ ⊱ {Γ;𝑄12} 𝑆2 {Γâ€Č;𝑄 â€Č1}

𝑄1 = 𝑃1 ⊕ 𝑅1 𝑃1 = ⟹[𝑝, 𝑝], [0, 0], · · · , [0, 0]⟩ ⊗ 𝑄11𝑅1 = ⟹[1 − 𝑝, 1 − 𝑝], [0, 0], · · · , [0, 0]⟩ ⊗ 𝑄12

Δ ⊱ {Γ;𝑄1} if prob(𝑝) then 𝑆1 else 𝑆2 fi {Γâ€Č;𝑄 â€Č1}

By inversion, we know that𝑄2 = 𝑃2⊕𝑅2 for some𝑄21, 𝑄22such thatΔ ⊱ {Γ;𝑄21} 𝑆1 {Γâ€Č;𝑄 â€Č

2},Δ ⊱ {Γ;𝑄22} 𝑆2 {Γâ€Č;𝑄 â€Č2},

𝑃2 = ⟹[𝑝, 𝑝], [0, 0], · · · , [0, 0]⟩ ⊗𝑄21, and 𝑅2 = ⟹[1− 𝑝, 1−𝑝], [0, 0], · · · , [0, 0]⟩ ⊗ 𝑄22. By induction hypothesis, wehave Δ ⊱ {Γ;𝑄11⊕𝑄21} 𝑆1 {Γâ€Č;𝑄 â€Č

1⊕𝑄 â€Č2} and Δ ⊱ {Γ;𝑄12⊕

𝑄22} 𝑆2 {Γâ€Č;𝑄 â€Č1 ⊕ 𝑄 â€Č

2}. Then⟹[𝑝, 𝑝], · · · , [0, 0]⟩ ⊗ (𝑄11 ⊕ 𝑄21)

= (⟹[𝑝, 𝑝], · · · , [0, 0]⟩ ⊗ 𝑄11) ⊕ (⟹[𝑝, 𝑝], · · · , [0, 0]⟩ ⊕ 𝑄21)= 𝑃1 ⊕ 𝑃2,⟹[1 − 𝑝, 1 − 𝑝], · · · , [0, 0]⟩ ⊗ (𝑄12 ⊕ 𝑄22)

= (⟹[1−𝑝,1−𝑝],· · ·,[0, 0]âŸ©âŠ—đ‘„12)⊕ (⟹[1−𝑝,1−𝑝],· · ·,[0, 0]âŸ©âŠ—đ‘„22)= 𝑅1 ⊕ 𝑅2,(𝑃1 ⊕ 𝑃2) ⊕ (𝑅1 ⊕ 𝑅2)

= (𝑃1 ⊕ 𝑅1) ⊕ (𝑃2 ⊕ 𝑅2)= 𝑄1 ⊕ 𝑄2.

Thus we conclude by (Q-Prob).

‱

(Q-Cond)Δ ⊱ {Γ ∧ 𝐿;𝑄1} 𝑆1 {Γâ€Č;𝑄 â€Č

1} Δ ⊱ {Γ ∧ ¬𝐿;𝑄1} 𝑆2 {Γâ€Č;𝑄 â€Č1}

Δ ⊱ {Γ;𝑄1} if 𝐿 then 𝑆1 else 𝑆2 fi {Γâ€Č;𝑄 â€Č1}

By inversion, we have Δ ⊱ {Γ ∧ 𝐿;𝑄2} 𝑆1 {Γâ€Č;𝑄 â€Č2}, and

Δ ⊱ {Γ ∧ ¬𝐿;𝑄2} 𝑆2 {Γâ€Č;𝑄 â€Č2}. By induction hypothesis,

we have Δ ⊱ {Γ ∧ 𝐿;𝑄1 ⊕ 𝑄2} 𝑆1 {Γâ€Č;𝑄 â€Č1 ⊕ 𝑄 â€Č

2} and Δ ⊱{Γ ∧ ¬𝐿;𝑄1 ⊕𝑄2} 𝑆2 {Γâ€Č;𝑄 â€Č

1 ⊕𝑄 â€Č2}. Then we conclude by

(Q-Cond).

‱

(Q-Loop)Δ ⊱ {Γ ∧ 𝐿;𝑄1} 𝑆 {Γ;𝑄1}

Δ ⊱ {Γ;𝑄1} while 𝐿 do 𝑆 od {Γ ∧ ¬𝐿;𝑄1}

By inversion, we have Δ ⊱ {Γ∧𝐿;𝑄2} 𝑆 {Γ;𝑄2}. By induc-tion hypothesis, we haveΔ ⊱ {Γ∧𝐿;𝑄1⊕𝑄2} 𝑆 {Γ;𝑄1⊕𝑄2}.Then we conclude by (Q-Loop).

‱

(Q-Seq)Δ ⊱ {Γ;𝑄1} 𝑆1 {Γâ€Čâ€Č;𝑄 â€Čâ€Č

1 } Δ ⊱ {Γâ€Čâ€Č;𝑄 â€Čâ€Č1 } 𝑆2 {Γâ€Č;𝑄 â€Č

1}Δ ⊱ {Γ;𝑄1} 𝑆1; 𝑆2 {Γâ€Č;𝑄 â€Č

1}By inversion, there exists 𝑄 â€Čâ€Č

2 such that Δ ⊱{Γ;𝑄2} 𝑆1 {Γâ€Čâ€Č;𝑄 â€Čâ€Č

2 }, and Δ ⊱ {Γâ€Čâ€Č;𝑄 â€Čâ€Č2 } 𝑆2 {Γâ€Č;𝑄 â€Č

2}. By in-duction hypothesis, we have Δ ⊱ {Γ;𝑄1⊕𝑄2} 𝑆1 {Γâ€Čâ€Č;𝑄 â€Čâ€Č

1 ⊕𝑄 â€Čâ€Č2 } and Δ ⊱ {Γâ€Čâ€Č;𝑄 â€Čâ€Č

1 ⊕ 𝑄 â€Čâ€Č2 } 𝑆2 {Γâ€Č;𝑄 â€Č

1 ⊕ 𝑄 â€Č2}. Then we

conclude by (Q-Seq).

‱

(Q-Weaken)Δ ⊱ {Γ0;𝑄0} 𝑆 {Γâ€Č0 ;𝑄 â€Č

0}Γ |= Γ0 Γâ€Č0 |= Γâ€Č Γ |= 𝑄1 ⊒ 𝑄0 Γâ€Č0 |= 𝑄 â€Č

0 ⊒ 𝑄 â€Č1

Δ ⊱ {Γ;𝑄1} 𝑆 {Γâ€Č;𝑄 â€Č1}

By inversion, there exist 𝑄3, 𝑄â€Č3 such that Γ |= 𝑄2 ⊒ 𝑄3,

Γâ€Č0 |= 𝑄 â€Č3 ⊒ 𝑄 â€Č

2, and Δ ⊱ {Γ0;𝑄3} 𝑆 {Γâ€Č0 ;𝑄 â€Č3}. By induction

hypothesis, we have Δ ⊱ {Γ0;𝑄0 ⊕𝑄3} 𝑆 {Γâ€Č0 ;𝑄 â€Č0 ⊕𝑄 â€Č

3}. Toapply (Q-Weaken), we need to show that Γ |= 𝑄1 ⊕ 𝑄2 ⊒𝑄0 ⊕ 𝑄3 and Γâ€Č0 |= 𝑄 â€Č

0 ⊕ 𝑄 â€Č3 ⊒ 𝑄 â€Č

1 ⊕ 𝑄 â€Č2. Then appeal to

Lemmas E.1 and E.2.□

Now we can construct the annotated transition kernel toreduce the soundness proof to OST.

Proof of Lem. F.1. Let a def= ↩→(âŸšđ›Ÿ, 𝑆, đŸ, đ›ŒâŸ©). By inversion

on Δ ⊱ {Γ;𝑄} âŸšđ›Ÿ, 𝑆, đŸ, đ›ŒâŸ©, we know that đ›Ÿ |= Γ, Δ ⊱{Γ;𝑄} 𝑆 {Γâ€Č;𝑄 â€Č}, and Δ ⊱ {Γâ€Č;𝑄 â€Č} đŸ for some Γâ€Č, 𝑄 â€Č. Weconstruct a probability measure ` as ^ (⟹Γ, 𝑄,đ›Ÿ, 𝑆, đŸ, đ›ŒâŸ©) byinduction on the derivation of Δ ⊱ {Γ;𝑄} 𝑆 {Γâ€Č;𝑄 â€Č}.

‱(Q-Skip)

Δ ⊱ {Γ;𝑄} skip {Γ;𝑄}By induction on the derivation of Δ ⊱ {Γ;𝑄} đŸ .

–

(QK-Stop)

Δ ⊱ {Γ;𝑄} KstopWe have a = 𝛿 (âŸšđ›Ÿ, skip,Kstop, đ›ŒâŸ©). Then we set ` =

𝛿 (⟹Γ, 𝑄,đ›Ÿ, skip,Kstop, đ›ŒâŸ©). It is clear that 𝜙𝑄 (đ›Ÿ) = 1 ⊗𝜙𝑄 (đ›Ÿ).

–

(QK-Loop)Δ ⊱ {Γ ∧ 𝐿;𝑄} 𝑆 {Γ;𝑄} Δ ⊱ {Γ ∧ ¬𝐿;𝑄} đŸ

Δ ⊱ {Γ;𝑄} Kloop 𝐿 𝑆 đŸLet 𝑏 ∈ {⊄,⊀} be such that đ›Ÿ ⊱ 𝐿 ⇓ 𝑏.If 𝑏 = ⊀, then a = 𝛿 (âŸšđ›Ÿ, 𝑆,Kloop 𝐿 𝑆 đŸ, đ›ŒâŸ©). We set` = 𝛿 (⟹Γ ∧ 𝐿,𝑄,đ›Ÿ, 𝑆,Kloop 𝐿 𝑆 đŸ, đ›ŒâŸ©). In this case, weknow that đ›Ÿ |= Γ ∧ 𝐿. By the premise, we know thatΔ ⊱ {Γ ∧ 𝐿;𝑄} 𝑆 {Γ;𝑄}. It then remains to show thatΔ ⊱ {Γ;𝑄} Kloop 𝐿 𝑆 đŸ . By (QK-Loop), it suffices toshow that Δ ⊱ {Γ∧𝐿;𝑄} 𝑆 {Γ;𝑄} and Δ ⊱ {Γ∧¬𝐿;𝑄}đŸ .Then appeal to the premise.If 𝑏 = ⊄, then ` = 𝛿 (âŸšđ›Ÿ, skip, đŸ, đ›ŒâŸ©). We set ` = 𝛿 (⟹Γ ∧¬𝐿,𝑄,đ›Ÿ, skip, đŸ, đ›ŒâŸ©). In this case, we know that đ›Ÿ |=Γ∧¬𝐿. By (Q-Skip), we have Δ ⊱ {Γ∧¬𝐿;𝑄} skip {Γ∧

26

Page 27: Central Moment Analysis for Cost Accumulators in

Central Moment Analysis for Cost Accumulators in Probabilistic Programs PLDI ’21, June 20–25, 2021, Virtual, UK

¬𝐿;𝑄}. It then remains to show that Δ ⊱ {Γ ∧¬𝐿;𝑄} đŸ .Then appeal to the premise.In both cases, đ›Ÿ and𝑄 do not change, thus we concludethat 𝜙𝑄 (đ›Ÿ) = 1 ⊗ 𝜙𝑄 (đ›Ÿ).

–

(QK-Seq)Δ ⊱ {Γ;𝑄} 𝑆 {Γâ€Č;𝑄 â€Č} Δ ⊱ {Γâ€Č;𝑄 â€Č} đŸ

Δ ⊱ {Γ;𝑄} Kseq 𝑆 đŸWe have a = 𝛿 (âŸšđ›Ÿ, 𝑆, đŸ, đ›ŒâŸ©). Then we set ` =

𝛿 (⟹Γ, 𝑄,đ›Ÿ, 𝑆, đŸ, đ›ŒâŸ©). By the premise, we know that Δ ⊱{Γ;𝑄} 𝑆 {Γâ€Č;𝑄 â€Č} and Δ ⊱ {Γâ€Č;𝑄 â€Č} đŸ . Because đ›Ÿ and 𝑄do not change, we conclude that 𝜙𝑄 (đ›Ÿ) = 1 ⊗ 𝜙𝑄 (đ›Ÿ).

–

(QK-Weaken)Δ ⊱ {Γâ€Č;𝑄 â€Č} đŸ Γ |= Γâ€Č Γ |= 𝑄 ⊒ 𝑄 â€Č

Δ ⊱ {Γ;𝑄} đŸBecause đ›Ÿ |= Γ and Γ |= Γâ€Č, we know thatđ›Ÿ |= Γâ€Č. Let ` â€Č be obtained from the inductionhypothesis on Δ ⊱ {Γâ€Č;𝑄 â€Č} đŸ . Then 𝜙𝑄â€Č (đ›Ÿ) âŠ’đ”ŒđœŽâ€Č∌`â€Č [

−−−−−−−−−−−−−−−−−−−−−−−−−−−−→⟹[(đ›Œ â€Č − đ›Œ)𝑘 , (đ›Œ â€Č − đ›Œ)𝑘 ]⟩0â‰€đ‘˜â‰€đ‘šâŠ—đœ™đ‘„â€Čâ€Č (đ›Ÿ â€Č)], where

𝜎 â€Č = ⟹_, 𝑄 â€Čâ€Č, đ›Ÿ â€Č, _, _, đ›Œ â€Č⟩. We set ` = ` â€Č. Because Γ |= 𝑄 ⊒𝑄 â€Č and đ›Ÿ |= Γ, we conclude that 𝜙𝑄 (đ›Ÿ) ⊒ 𝜙𝑄â€Č (đ›Ÿ).

‱

(Q-Tick)

𝑄 =−−−−−−−−−−−−−−→⟹[𝑐𝑘 , 𝑐𝑘 ]⟩0â‰€đ‘˜â‰€đ‘š ⊗ 𝑄 â€Č

Δ ⊱ {Γ;𝑄} tick(𝑐) {Γ;𝑄 â€Č}We have a = 𝛿 (âŸšđ›Ÿ, skip, đŸ, đ›Œ + đ‘âŸ©). Then we set ` =

𝛿 (⟹Γ, 𝑄 â€Č, đ›Ÿ, skip, đŸ, đ›Œ + đ‘âŸ©). By (Q-Skip), we have Δ ⊱{Γ;𝑄 â€Č} skip {Γ;𝑄 â€Č}. Then by the assumption, we haveΔ ⊱ {Γ;𝑄 â€Č} đŸ . It remains to show that 𝜙𝑄 (đ›Ÿ) ⊒−−−−−−−−−−−−−→⟹[𝑐𝑘 , 𝑐𝑘 ]⟩0â‰€đ‘˜â‰€đ‘š ⊗ 𝜙𝑄â€Č (đ›Ÿ). Indeed, we have 𝜙𝑄 (đ›Ÿ) =−−−−−−−−−−−−−→⟹[𝑐𝑘 , 𝑐𝑘 ]⟩0â‰€đ‘˜â‰€đ‘š ⊗ 𝜙𝑄â€Č (đ›Ÿ) by the premise.

‱

(Q-Assign)Γ = [𝐾/đ‘„]Γâ€Č 𝑄 = [𝐾/đ‘„]𝑄 â€Č

Δ ⊱ {Γ;𝑄} đ‘„ B 𝐾 {Γâ€Č;𝑄 â€Č}Let 𝑟 ∈ ℝ be such that đ›Ÿ ⊱ 𝐾 ⇓ 𝑟 . We have a = 𝛿 (âŸšđ›Ÿ [đ‘„ ↩→𝑟 ], skip, đŸ, đ›ŒâŸ©). Then we set ` = 𝛿 (⟹Γâ€Č, 𝑄 â€Č, đ›Ÿ [đ‘„ ↩→𝑟 ], skip, đŸ, đ›ŒâŸ©). Because đ›Ÿ ⊱ Γ, i.e., đ›Ÿ ⊱ [𝐾/đ‘„]Γâ€Č, weknow that đ›Ÿ [đ‘„ ↩→ 𝑟 ] ⊱ Γâ€Č. By (Q-Skip), we have Δ ⊱{Γâ€Č;𝑄 â€Č} skip {Γâ€Č;𝑄 â€Č}. Then by the assumption, we haveΔ ⊱ {Γâ€Č;𝑄 â€Č} đŸ . It remains to show that 𝜙𝑄 (đ›Ÿ) = 1 ⊗𝜙𝑄â€Č (đ›Ÿ [đ‘„ ↩→ 𝑟 ]). By the premise, we have 𝑄 = [𝐾/đ‘„]𝑄 â€Č,thus 𝜙𝑄 (đ›Ÿ) = 𝜙 [𝐾/đ‘„ ]𝑄â€Č (đ›Ÿ) = 𝜙𝑄â€Č (đ›Ÿ [đ‘„ ↩→ 𝑟 ]).

‱

(Q-Sample)Γ = âˆ€đ‘„ ∈ supp(`đ· ) : Γâ€Č 𝑄 = đ”Œđ‘„âˆŒ`đ· [𝑄 â€Č]

Δ ⊱ {Γ;𝑄} đ‘„ ∌ đ· {Γâ€Č;𝑄 â€Č}We have a = `đ· ≫= _𝑟 .𝛿 (âŸšđ›Ÿ [đ‘„ ↩→ 𝑟 ], skip, đŸ, đ›ŒâŸ©). Thenwe set ` = `đ· ≫= _𝑟 .𝛿 (⟹Γâ€Č, 𝑄 â€Č, đ›Ÿ [đ‘„ ↩→ 𝑟 ], skip, đŸ, đ›ŒâŸ©). Forall 𝑟 ∈ supp(`đ· ), because đ›Ÿ |= âˆ€đ‘„ ∈ supp(`đ· ) : Γâ€Č, weknow that đ›Ÿ [đ‘„ ↩→ 𝑟 ] |= Γâ€Č. By (Q-Skip), we have Δ ⊱{Γâ€Č;𝑄 â€Č} skip {Γâ€Č;𝑄 â€Č}. Then by the assumption, we haveΔ ⊱ {Γâ€Č;𝑄 â€Č}đŸ . It remains to show that𝜙𝑄 (đ›Ÿ) ⊒ đ”Œđ‘ŸâˆŒ`đ· [1⊗𝜙𝑄â€Č (đ›Ÿ [đ‘„ ↩→ 𝑟 ])]. Indeed, because 𝑄 = đ”Œđ‘„âˆŒ`đ· [𝑄 â€Č], weknow that 𝜙𝑄 (đ›Ÿ) = đœ™đ”Œđ‘„âˆŒ`đ· [𝑄â€Č ] (đ›Ÿ) = đ”Œđ‘ŸâˆŒ`đ· [𝜙𝑄â€Č (đ›Ÿ [đ‘„ ↩→𝑟 ])].

‱

(Q-Call)∀𝑖 : (Γ;𝑄𝑖 , Γâ€Č;𝑄 â€Č

𝑖 ) ∈ Δ(𝑓 )Δ ⊱ {Γ;⊕𝑖 𝑄𝑖 } call 𝑓 {Γâ€Č;⊕𝑖 𝑄

â€Č𝑖}

We have a = 𝛿 (âŸšđ›Ÿ,𝒟(𝑓 ), đŸ, đ›ŒâŸ©). Then we set ` =

𝛿 (⟹Γ,⊕𝑖 𝑄𝑖 , đ›Ÿ,𝒟(𝑓 ), đŸ, đ›ŒâŸ©). By the premise, we knowthat Δ ⊱ {Γ;𝑄𝑖 } 𝒟(𝑓 ) {Γâ€Č;𝑄 â€Č

𝑖 } for each 𝑖 . ByLem. F.2 and simple induction, we know that Δ ⊱{Γ;⊕𝑖 𝑄𝑖 } 𝒟(𝑓 ) {Γâ€Č;⊕𝑖 𝑄

â€Č𝑖 }. Because đ›Ÿ and

⊕𝑖 𝑄𝑖 do

not change, we conclude that 𝜙⊕𝑖 𝑄𝑖

(đ›Ÿ) = 1 ⊗ 𝜙⊕𝑖 𝑄𝑖

(đ›Ÿ).

‱

(Q-Prob)Δ ⊱ {Γ;𝑄1} 𝑆1 {Γâ€Č;𝑄 â€Č} Δ ⊱ {Γ;𝑄2} 𝑆2 {Γâ€Č;𝑄 â€Č}𝑄 = 𝑃 ⊕ 𝑅 𝑃 = ⟹[𝑝, 𝑝], [0, 0], · · · , [0, 0]⟩ ⊗ 𝑄1

𝑅 = ⟹[1 − 𝑝, 1 − 𝑝], [0, 0], · · · , [0, 0]⟩ ⊗ 𝑄2

Δ ⊱ {Γ;𝑄} if prob(𝑝) then 𝑆1 else 𝑆2 fi {Γâ€Č;𝑄 â€Č}We have a = 𝑝 · 𝛿 (âŸšđ›Ÿ, 𝑆1, đŸ, đ›ŒâŸ©) + (1 − 𝑝) · 𝛿 (âŸšđ›Ÿ, 𝑆2, đŸ, đ›ŒâŸ©).Then we set ` = 𝑝 · 𝛿 (⟹Γ, 𝑄1, đ›Ÿ, 𝑆1, đŸ, đ›ŒâŸ© + (1 −𝑝) · 𝛿 (⟹Γ, 𝑄2, đ›Ÿ, 𝑆2, đŸ, đ›ŒâŸ©). From the assumption and thepremise, we know that đ›Ÿ |= Γ, Δ ⊱ {Γâ€Č;𝑄 â€Č} đŸ , andΔ ⊱ {Γ;𝑄1} 𝑆1 {Γâ€Č;𝑄 â€Č}, Δ ⊱ {Γ;𝑄2} 𝑆2 {Γâ€Č;𝑄 â€Č}. It remainsto show that𝜙𝑄 (đ›Ÿ)𝑘 ≄I (𝑝 Â·đœ™đ‘„1 (đ›Ÿ)𝑘 )+I ((1−𝑝) Â·đœ™đ‘„2 (đ›Ÿ)𝑘 ),where the scalar product 𝑝 · [𝑎, 𝑏] def

= [𝑝𝑎, 𝑝𝑏] for 𝑝 ≄ 0. Onthe other hand, from the premise, we have𝑄𝑘 = 𝑃𝑘 +PI 𝑅𝑘and 𝑃𝑘 = (( [𝑝, 𝑝], · · · , [0, 0]) ⊗𝑄1)𝑘 =

(𝑘0) ×PI ( [𝑝, 𝑝] ·PI

(𝑄1)𝑘 ) = 𝑝 · (𝑄1)𝑘 , as well as𝑅𝑘 = (1−𝑝) · (𝑄2)𝑘 . Therefore,we have 𝜙𝑄 (đ›Ÿ)𝑘 = 𝜙−−−−−−−−−−−−−−−−−→⟹𝑃𝑘+PI𝑅𝑘 ⟩0â‰€đ‘˜â‰€đ‘š

(đ›Ÿ)𝑘 = 𝑝 · 𝜙𝑄1 (đ›Ÿ)𝑘 +I(1 − 𝑝) · 𝜙𝑄2 (đ›Ÿ)𝑘 .

‱

(Q-Cond)Δ ⊱ {Γ ∧ 𝐿;𝑄} 𝑆1 {Γâ€Č;𝑄 â€Č} Δ ⊱ {Γ ∧ ¬𝐿;𝑄} 𝑆2 {Γâ€Č;𝑄 â€Č}

Δ ⊱ {Γ;𝑄} if 𝐿 then 𝑆1 else 𝑆2 fi {Γâ€Č;𝑄 â€Č}Let 𝑏 ∈ {⊀,⊄} be such that đ›Ÿ ⊱ 𝐿 ⇓ 𝑏.If 𝑏 = ⊀, then a = 𝛿 (âŸšđ›Ÿ, 𝑆1, đŸ, đ›ŒâŸ©). We set ` = 𝛿 (⟹Γ ∧𝐿,𝑄,đ›Ÿ, 𝑆1, đŸ, đ›ŒâŸ©). In this case, we know that đ›Ÿ |= Γ ∧ 𝐿.By the premise and the assumption, we know that Δ ⊱{Γ ∧ 𝐿;𝑄} 𝑆1 {Γâ€Č;𝑄 â€Č} and Δ ⊱ {Γâ€Č;𝑄 â€Č} đŸ .If 𝑏 = ⊄, then a = 𝛿 (âŸšđ›Ÿ, 𝑆2, đŸ, đ›ŒâŸ©). We set ` = 𝛿 (⟹Γ ∧¬𝐿,𝑄,đ›Ÿ, 𝑆2, đŸ, đ›ŒâŸ©). In this case, we know that đ›Ÿ |= Γ ∧ ¬𝐿.By the premise and the assumption, we know that Δ ⊱{Γ ∧ ¬𝐿;𝑄} 𝑆2 {Γâ€Č;𝑄 â€Č} and Δ ⊱ {Γâ€Č;𝑄 â€Č} đŸ .In both cases, đ›Ÿ and 𝑄 do not change, thus we concludethat 𝜙𝑄 (đ›Ÿ) = 1 ⊗ 𝜙𝑄 (đ›Ÿ).

‱

(Q-Loop)Δ ⊱ {Γ ∧ 𝐿;𝑄} 𝑆 {Γ;𝑄}

Δ ⊱ {Γ;𝑄} while 𝐿 do 𝑆1 od {Γ ∧ ¬𝐿;𝑄}We have a = 𝛿 (âŸšđ›Ÿ, skip,Kloop 𝐿 𝑆 đŸ, đ›ŒâŸ©). Then we set` = 𝛿 (⟹Γ, 𝑄,đ›Ÿ, skip,Kloop 𝐿 𝑆 đŸ, đ›ŒâŸ©). By (Q-Skip), wehave Δ ⊱ {Γ;𝑄} skip {Γ;𝑄}. Then by the assumptionΔ ⊱ {Γ ∧ ¬𝐿;𝑄} đŸ and the premise, we know that Δ ⊱{Γ;𝑄} Kloop 𝐿 𝑆 đŸ by (QK-Loop). Because đ›Ÿ and 𝑄 donot change, we conclude that 𝜙𝑄 (đ›Ÿ) = 1 ⊗ 𝜙𝑄 (đ›Ÿ).

‱

(Q-Seq)Δ ⊱ {Γ;𝑄} 𝑆1 {Γâ€Č;𝑄 â€Č} Δ ⊱ {Γâ€Č;𝑄 â€Č} 𝑆2 {Γâ€Čâ€Č;𝑄 â€Čâ€Č}

Δ ⊱ {Γ;𝑄} 𝑆1; 𝑆2 {Γâ€Čâ€Č;𝑄 â€Čâ€Č}

27

Page 28: Central Moment Analysis for Cost Accumulators in

PLDI ’21, June 20–25, 2021, Virtual, UK Di Wang, Jan Hoffmann, and Thomas Reps

We have a = 𝛿 (âŸšđ›Ÿ, 𝑆1,Kseq 𝑆2 đŸ, đ›ŒâŸ©). Then we set` = 𝛿 (⟹Γ, 𝑄,đ›Ÿ, 𝑆1,Kseq 𝑆2 đŸ, đ›ŒâŸ©). By the first premise,we have Δ ⊱ {Γ;𝑄} 𝑆1 {Γâ€Č;𝑄 â€Č}. By the assumptionΔ ⊱ {Γâ€Čâ€Č;𝑄 â€Čâ€Č} đŸ and the second premise, we know thatΔ ⊱ {Γâ€Č;𝑄 â€Č} Kseq 𝑆2 đŸ by (QK-Seq). Because đ›Ÿ and 𝑄 donot change, we conclude that 𝜙𝑄 (đ›Ÿ) = 1 ⊗ 𝜙𝑄 (đ›Ÿ).

‱

(Q-Weaken)Δ ⊱ {Γ0;𝑄0} 𝑆 {Γâ€Č0 ;𝑄 â€Č

0}Γ |= Γ0 Γâ€Č0 |= Γâ€Č Γ |= 𝑄 ⊒ 𝑄0 Γâ€Č0 |= 𝑄 â€Č

0 ⊒ 𝑄 â€Č

Δ ⊱ {Γ;𝑄} 𝑆 {Γâ€Č;𝑄 â€Č}By đ›Ÿ |= Γ and Γ |= Γ0, we know that đ›Ÿ |= Γ0. By theassumption Δ ⊱ {Γâ€Č;𝑄 â€Č} đŸ and the premise Γâ€Č0 |= Γâ€Č,Γâ€Č0 |= 𝑄 â€Č

0 ⊒ 𝑄 â€Č, we derive Δ ⊱ {Γâ€Č0 ;𝑄 â€Č0} đŸ by (QK-

Weaken). Thus let `0 be obtained by the induction hy-pothesis on Δ ⊱ {Γ0;𝑄0} 𝑆 {Γâ€Č0 ;𝑄 â€Č

0}. Then 𝜙𝑄0 (đ›Ÿ) âŠ’đ”ŒđœŽâ€Č∌`0 [

−−−−−−−−−−−−−−−−−−−−−−−−−−−−→⟹[(đ›Œ â€Č − đ›Œ)𝑘 , (đ›Œ â€Č − đ›Œ)𝑘 ]⟩0â‰€đ‘˜â‰€đ‘š ⊗ 𝜙𝑄â€Čâ€Č (đ›Ÿ â€Č)], where

𝜎 â€Č = ⟹_, 𝑄 â€Čâ€Č, đ›Ÿ â€Č, _, _, đ›Œ â€Č⟩. We set ` = `0. By the premiseΓ |= 𝑄 ⊒ 𝑄0 and đ›Ÿ |= Γ, we conclude that 𝜙𝑄 (đ›Ÿ) ⊒ 𝜙𝑄0 (đ›Ÿ).

□

Therefore, we can use the annotated kernel ^ above tore-construct the trace-based moment semantics in appen-dix C. Then we can define the potential function on an-notated program configurations as 𝜙 (𝜎) def

= 𝜙𝑄 (đ›Ÿ) where𝜎 = ⟹_, 𝑄,đ›Ÿ, _, _, _⟩.

The next step is to apply the extended OST for intervalbounds (Thm. E.6). Recall that the theorem requires that forsome ℓ ∈ ℕ and đ¶ ≄ 0, âˆ„đ‘Œđ‘› ∄∞ ≀ đ¶ · (𝑛 + 1)ℓ almost surelyfor all 𝑛 ∈ â„€+. One sufficient condition for the requirementis to assume the bounded-update property, i.e., every (deter-ministic or probabilistic) assignment to a program variableupdates the variable with a bounded change. As observedbyWang et al. [43], bounded updates are common in practice.We formulate the idea as follows.Lemma F.3. If there exists đ¶0 ≄ 0 such that for all 𝑛 ∈ â„€+

and đ‘„ ∈ VID, it holds that ℙ[|đ›Ÿđ‘›+1 (đ‘„) − đ›Ÿđ‘› (đ‘„) | ≀ đ¶0] = 1where 𝜔 is an infinite trace, 𝜔𝑛 = âŸšđ›Ÿđ‘›, _, _, _⟩, and 𝜔𝑛+1 =

âŸšđ›Ÿđ‘›+1, _, _, _⟩, then there exists đ¶ ≄ 0 such that for all 𝑛 ∈ â„€+,âˆ„đ‘Œđ‘› ∄∞ ≀ đ¶ · (𝑛 + 1)𝑚𝑑 almost surely.

Proof. Let đ¶1 ≄ 0 be such that for all tick(𝑐) statements inthe program, |𝑐 | ≀ đ¶1. Then for all 𝜔 , if 𝜔𝑛 = ⟹_, _, _, đ›Œđ‘›âŸ©,then |đ›Œđ‘› | ≀ 𝑛 · đ¶1. On the other hand, we know thatℙ[|đ›Ÿđ‘› (đ‘„) − đ›Ÿ0 (đ‘„) | ≀ đ¶0 · 𝑛] = 1 for any variable đ‘„ . As weassume all the program variables are initialized to zero, weknow that ℙ[|đ›Ÿđ‘› (đ‘„) | ≀ đ¶0 · 𝑛] = 1. From the construction inthe proof of Lem. F.1, we know that all the templates usedto define the interval-valued potential function should havealmost surely bounded coefficients. Let đ¶2 ≄ 0 be such abound. Also, the 𝑘-th component in a template is a poly-nomial in ℝ𝑘𝑑 [VID]. Therefore, Ω𝑛 (𝜔) = 𝜙 (𝜔𝑛) = 𝜙𝑄𝑛

(đ›Ÿđ‘›),and

|𝜙𝑄𝑛(đ›Ÿđ‘›)𝑘 | ≀

đ‘˜đ‘‘âˆ‘ïžđ‘–=0

đ¶2 · |VID|𝑖 · |đ¶0 · 𝑛 |𝑖 ≀ đ¶3 · (𝑛 + 1)𝑘𝑑 , a.s.,

for some sufficiently large constant đ¶3. Thus| (𝑌𝑛)𝑘 | = | (𝐮𝑛 ⊗ Ω𝑛)𝑘 |

= |đ‘˜âˆ‘ïžI

𝑖=0

(𝑘

𝑖

)×I ((𝐮𝑛)𝑖 ·I (Ω𝑛)𝑘−𝑖 ) |

â‰€đ‘˜âˆ‘ïžđ‘–=0

(𝑘

𝑖

)· (𝑛 Â·đ¶1)𝑖 · (đ¶3 · (𝑛 + 1)) (𝑘−𝑖)𝑑

≀ đ¶4 · (𝑛 + 1)𝑘𝑑 , a.s.,for some sufficiently large constant đ¶4. Therefore âˆ„đ‘Œđ‘› ∄∞ â‰€đ¶5 · (𝑛+1)𝑚𝑑 , a.s., for some sufficiently large constantđ¶5. □

Now we prove the soundness of bound inference.

Theorem (Thm. 4.4). Suppose Δ ⊱ {Γ;𝑄} 𝑆main {Γâ€Č; 1} and⊱ Δ. Then đ”Œ[𝐮𝑇 ] ⊑ 𝜙𝑄 (__.0), i.e., the moments đ”Œ[𝐮𝑇 ] of theaccumulated cost upon program termination are bounded byintervals in 𝜙𝑄 (__.0) where 𝑄 is the quantitative context and__.0 is the initial valuation, if both of the following propertieshold:(i) đ”Œ[𝑇𝑚𝑑 ] < ∞, and(ii) there exists đ¶0 ≄ 0 such that for all 𝑛 ∈ â„€+ and đ‘„ ∈ VID,

it holds almost surely that |đ›Ÿđ‘›+1 (đ‘„) − đ›Ÿđ‘› (đ‘„) | ≀ đ¶0 whereâŸšđ›Ÿđ‘›, _, _, _⟩ = 𝜔𝑛 and âŸšđ›Ÿđ‘›+1, _, _, _⟩ = 𝜔𝑛+1 of an infinitetrace 𝜔 .

Proof. By Lem. F.3, there exists đ¶ ≄ 0 such that âˆ„đ‘Œđ‘› ∄∞ ≀ đ¶ ·(𝑛+1)𝑚𝑑 almost surely for all 𝑛 ∈ â„€+. By the assumption, wealso know that đ”Œ[𝑇𝑚𝑑 ] < ∞. Thus by Thm. E.6, we concludethat đ”Œ[𝑌𝑇 ] ⊑ đ”Œ[𝑌0], i.e., đ”Œ[𝐮𝑇 ] ⊑ đ”Œ[Ί0] = 𝜙𝑄 (__.0). □

G Termination AnalysisIn this section, we develop a technique to reason about upperbounds on higher moments đ”Œ[𝑇𝑚] of the stopping time 𝑇 .We adapt the idea of expected-potential functions, but relyon a simpler convergence proof. In this section, we assumeR = ( [0,∞], ≀, +, ·, 0, 1) to be a partially ordered semiringon extended nonnegative real numbers.

Definition G.1. A map 𝜓 : Σ → M(𝑚)R is said to be an

expected-potential function for upper bounds on stoppingtime if(i) 𝜓 (𝜎)0 = 1 for all 𝜎 ∈ ÎŁ,(ii) 𝜓 (𝜎) = 1 if 𝜎 = ⟹_, skip,Kstop, _⟩, and(iii) 𝜓 (𝜎) ⊒ đ”ŒđœŽâ€Č∌↊→(𝜎) [⟹1, 1, · · · , 1⟩ ⊗ 𝜓 (𝜎 â€Č)] for all non-

terminating configuration 𝜎 ∈ Σ.

Intuitively, 𝜓 (𝜎) is an upper bound on the moments ofthe evaluation steps upon termination for the computationthat continues from the configuration 𝜎 . We define𝐮𝑛 and ι𝑛where𝑛 ∈ â„€+ to be random variables on the probability space(Ω,F ,ℙ) of the trace semantics as 𝐮𝑛 (𝜔) def

=âˆ’âˆ’âˆ’âˆ’âˆ’âˆ’âˆ’âˆ’âˆ’â†’âŸšđ‘›đ‘˜âŸ©0â‰€đ‘˜â‰€đ‘š and

ι𝑛 (𝜔) def= 𝜓 (𝜔𝑛). Then we define 𝐮𝑇 (𝜔) def

= 𝐮𝑇 (𝜔) (𝜔). Notethat 𝐮𝑇 =

âˆ’âˆ’âˆ’âˆ’âˆ’âˆ’âˆ’âˆ’âˆ’â†’âŸšđ‘‡đ‘˜âŸ©0â‰€đ‘˜â‰€đ‘š .

28

Page 29: Central Moment Analysis for Cost Accumulators in

Central Moment Analysis for Cost Accumulators in Probabilistic Programs PLDI ’21, June 20–25, 2021, Virtual, UK

Table 4. Upper bounds on the raw/central moments of run-times, with comparison to Kura et al. [26]. “T/O” stands fortimeout after 30 minutes. “N/A” means that the tool is notapplicable. “-” indicates that the tool fails to infer a bound.Entries with more precise results or less analysis time aremarked in bold.

program moment this work Kura et al. [26]upper bnd. time (sec) upper bnd. time (sec)

(1-1)

2nd raw 201 0.019 201 0.0153rd raw 3829 0.019 3829 0.0204th raw 90705 0.023 90705 0.027

2nd central 32 0.029 N/A N/A4th central 9728 0.058 N/A N/A

(1-2)

2nd raw 2357 1.068 3124 0.0373rd raw 148847 1.512 171932 0.0624th raw 11285725 1.914 12049876 0.096

2nd central 362 3.346 N/A N/A4th central 955973 9.801 N/A N/A

(2-1)

2nd raw 2320 0.016 2320 11.3803rd raw 691520 0.018 - 16.0564th raw 340107520 0.021 - 23.414

2nd central 1920 0.026 N/A N/A4th central 289873920 0.049 N/A N/A

(2-2)

2nd raw 8375 0.022 8375 38.4633rd raw 1362813 0.028 - 73.4084th raw 306105209 0.035 - 141.072

2nd central 5875 0.029 N/A N/A4th central 447053126 0.086 N/A N/A

(2-3)

2nd raw 3675 0.039 6710 48.6623rd raw 618584 0.049 19567045 0.0394th raw 164423336 0.055 - T/O

2nd central 3048 0.053 N/A N/A4th central 196748763 0.123 N/A N/A

(2-4)

2nd raw 6625 0.035 10944 216.3523rd raw 742825 0.048 - 453.4354th raw 101441320 0.072 - 964.579

2nd central 6624 0.051 N/A N/A4th central 313269063 0.215 N/A N/A

(2-5)

2nd raw 21060 0.045 - 216.6053rd raw 9860940 0.063 - 467.5774th raw 7298339760 0.101 - 1133.947

2nd central 20160 0.068 N/A N/A4th central 8044220161 0.271 N/A N/A

Table 3. Upper bounds of the expectations of runtimes, withcomparison to Kura et al. [26].program upper bound by our tool upper bound by Kura et al. [26](1-1) 13 (deg 2, 0.106s) 13 (deg 1, 0.011s)(1-2) 44.6667 (deg 4, 0.417s) 68 (deg 1, 0.020s)(2-1) 20 (deg 1, 0.096s) 20 (deg 1, 0.011s)(2-2) 75 (deg 1, 0.116s) 75 (deg 1, 0.015s)(2-3) 42 (deg 1, 0.260s) 62 (deg 1, 0.017s)(2-4) 73 (deg 1, 0.143s) 96 (deg 1, 0.013s)(2-5) 90 (deg 1, 0.244s) 90 (deg 1, 0.017s)

We now show that a valid potential function for stoppingtime always gives a sound upper bound.

Theorem G.2. đ”Œ[𝐮𝑇 ] ⊑ đ”Œ[Κ0].Proof. Let đ¶đ‘› (𝜔) def

= ⟹1, 1, · · · , 1⟩ if 𝑛 < 𝑇 (𝜔), otherwiseđ¶đ‘› (𝜔) def

= ⟹1, 0, · · · , 0⟩. Then 𝐮𝑇 =⊗∞

𝑖=0đ¶đ‘– . By Prop. A.3,we know that đ”Œ[𝐮𝑇 ] = lim𝑛→∞ đ”Œ[⊗𝑛

𝑖=0đ¶đ‘– ]. Thus it sufficesto show that for all 𝑛 ∈ â„€+, đ”Œ[⊗𝑛

𝑖=0đ¶đ‘– ] ⊑ đ”Œ[Κ0].Observe that {đ¶đ‘›}đ‘›âˆˆâ„€+ is adapted to {F𝑛}đ‘›âˆˆâ„€+ , because

the event {𝑇 ≀ 𝑛} is F𝑛-measurable. Then we haveđ”Œ[

⊗𝑛

𝑖=0đ¶đ‘– ⊗ ι𝑛+1 | F𝑛]

=⊗𝑛−1

𝑖=0đ¶đ‘– ⊗ đ”Œ[đ¶đ‘› ⊗ ι𝑛+1 | F𝑛], 𝑎.𝑠 .,

⊑⊗𝑛−1

𝑖=0đ¶đ‘– ⊗ ι𝑛 .

Therefore, đ”Œ[⊗𝑛

𝑖=0đ¶đ‘– ⊗ ι𝑛+1] ⊑ đ”Œ[Κ0] for all 𝑛 ∈ â„€+ by asimple induction. Because ι𝑛+1 ⊒ 1, we conclude that

đ”Œ[⊗𝑛

𝑖=0đ¶đ‘– ] ⊑ đ”Œ[

⊗𝑛

𝑖=0đ¶đ‘– ⊗ ι𝑛+1] ⊑ đ”Œ[Κ0] .

□

H Experimental EvaluationTab. 4 and Fig. 15 are the complete versions of the statisticsin Tab. 1 and the plots in Fig. 9, respectively.Tab. 3 compares our tool with Kura et al. [26] on their

benchmarks for upper bounds on the first moments of run-times. The programs (1-1) and (1-2) are coupon-collectorproblems with a total of two and four coupons, respectively.The other five are variants of random walks. The first threeare one-dimensional random walks: (2-1) is integer-valued,(2-2) is real-valued with continuous sampling, and (2-3) ex-hibits adversarial nondeterminism. The programs (2-4) and(2-5) are two-dimensional random walks. In the parenthesesafter the bounds, we record the degree of polynomials for thetemplates and the running time of the analysis. All analysescompleted is less than half a second, and our tool can derivebetter bounds than the compared tool [26].

Tab. 5 compares our tool with Absynth by Ngo et al. [31]on their benchmarks for upper bounds on the first momentsof monotone cost accumulators. Both tools are able to infersymbolic polynomial bounds. Absynth uses a finer-grainedset of base functions, and it supports bounds of the form| [đ‘„,𝑩] |, which is defined as max(0, 𝑩 − đ‘„). Our tool is as pre-cise as, and sometimes more precise than Absynth,3 but it isless efficient thanAbsynth. One reason for this could be thatwe use a full-fledged numerical abstract domain to derivethe logical contexts, while Absynth employs less powerfulbut more efficient heuristics to do that step. Nevertheless, allthe analyses completed in less than one second.Tab. 6 compares our tool with Wang et al. [43] on their

benchmarks for lower and upper bounds on the first mo-ments of accumulated costs. All the benchmark programs3We compared the precision by first simplifying the bounds derived byAbsynth using the pre-conditions shown in Tab. 5, and then comparingthe polynomials.

29

Page 30: Central Moment Analysis for Cost Accumulators in

PLDI ’21, June 20–25, 2021, Virtual, UK Di Wang, Jan Hoffmann, and Thomas Reps

20 30 40 500

0.1

0.2

𝑑

tailprobability

ℙ[𝑇

â‰„đ‘‘]

(1-1)

by raw moments [26]by 2nd central momentby 4th central moment

100 150 2000

0.1

0.2

𝑑

tailprobability

ℙ[𝑇

â‰„đ‘‘]

(1-2)

by raw moments [26]by 2nd central momentby 4th central moment

200 400 6000

0.1

0.2

𝑑

tailprobability

ℙ[𝑇

â‰„đ‘‘]

(2-1)

by raw moments [26]by 2nd central momentby 4th central moment

200 400 6000

0.1

0.2

𝑑

tailprobability

ℙ[𝑇

â‰„đ‘‘]

(2-2)

by raw moments [26]by 2nd central momentby 4th central moment

200 400 6000

0.1

0.2

𝑑

tailprobability

ℙ[𝑇

â‰„đ‘‘]

(2-3)

by raw moments [26]by 2nd central momentby 4th central moment

200 400 6000

0.1

0.2

𝑑

tailprobability

ℙ[𝑇

â‰„đ‘‘]

(2-4)

by raw moments [26]by 2nd central momentby 4th central moment

400 700 1,000 1,3000

0.1

0.2

𝑑

tailprobability

ℙ[𝑇

â‰„đ‘‘]

(2-5)

by raw moments [26]by 2nd central momentby 4th central moment

Figure 15. Upper bound of the tail probability ℙ[𝑇 ≄ 𝑑] as a function of 𝑑 , with comparison to Kura et al. [26]. Each gray lineis the minimum of tail bounds obtained from the raw moments of degree up to four inferred by Kura et al. [26]. Green linesand red lines are the tail bounds given by 2nd and 4th central moments inferred by our tool, respectively.

satisfy the bounded-update property. To ensure soundness,our tool has to perform an extra termination check requiredby Thm. 4.4. Our tool derives similar symbolic lower andupper bounds, compared to the results of Wang et al. [43].Meanwhile, our tool is more efficient than the compared tool.One source of slowdown for Wang et al. [43] could be thatthey use Matlab as the LP backend, and the initialization ofMatlab has significant overhead.

I Case Study: Timing-Attack AnalysisWe motivate our work on central-moment analysis using aprobabilistic program with a timing-leak vulnerability, anddemonstrate how the results from an analysis can be used tobound the success rate of an attack program that attemptsto exploit the vulnerability. The program is extracted andmodified from a web application provided by DARPA duringengagements as part of the STAC program [15]. In essence,the program models a password checker that compares aninput guess with an internally stored password secret, rep-resented as two 𝑁 -bit vectors. The program in Fig. 16(a) isthe interface of the checker, and Fig. 16(b) is the comparisonfunction compare, which carries out most of the computa-tion. The statements of the form “tick(·)” represent a costmodel for the running time of compare, which is assumedto be observable by the attacker. compare iterates over thebits from high-index to low-index, and the running time ex-pended during the processing of bit 𝑖 depends on the currentcomparison result (stored in cmp), as well as on the valuesof the 𝑖-th bits of guess and secret. Because the running timeof compare might leak information about the relationshipbetween guess and secret, compare introduces some random

delays to add noise to its running time. However, we willsee shortly that such a countermeasure does not protect theprogram from a timing attack.We now show how the moments of the running time of

compare—the kind of information provided by our central-moment analysis (§3)—are useful for analyzing the successprobability of the attack program given in Fig. 16(c). Let 𝑇be the random variable for the running time of compare.A standard timing attack for such programs is to guess thebits of secret successively. The idea is the following: Sup-pose that we have successfully obtained the bits secret [𝑖 + 1]through secret [𝑁 ]; we now guess that the next bit, secret [𝑖],is 1 and set guess[𝑖] B 1. Theoretically, if the following twoconditional expectations

đ”Œ[𝑇1] def= đ”Œ[𝑇 | ∧𝑁

𝑗=𝑖+1 (secret [ 𝑗] = guess[ 𝑗])∧ (secret [𝑖] = 1 ∧ guess[𝑖] = 1)]

(11)

đ”Œ[𝑇0] def= đ”Œ[𝑇 | ∧𝑁

𝑗=𝑖+1 (secret [ 𝑗] = guess[ 𝑗])∧ (secret [𝑖] = 0 ∧ guess[𝑖] = 1)]

(12)

have a significant difference, then there is an opportunityto check our guess by running the program multiple times,using the average running time as an estimate of đ”Œ[𝑇 ], andchoosing the value of guess[𝑖] according to whichever of (11)and (12) is closest to our estimate. However, if the differencebetween đ”Œ[𝑇1] and đ”Œ[𝑇0] is not significant enough, or theprogram produces a large amount of noise in its runningtime, the attack might not be realizable in practice. To deter-mine whether the timing difference represents an exploitablevulnerability, we need to reason about the attack program’ssuccess rate.

30

Page 31: Central Moment Analysis for Cost Accumulators in

Central Moment Analysis for Cost Accumulators in Probabilistic Programs PLDI ’21, June 20–25, 2021, Virtual, UK

Table 5. Upper bounds of the expectations of monotone costs, with comparison to Ngo et al. [31].program pre-condition upper bound by our tool upper bound by Absynth [31]2drdwalk 𝑑 < 𝑛 2(𝑛 − 𝑑 + 1) (deg 1, 0.269s) 2| [𝑑, 𝑛 + 1] | (deg 1, 0.170s)C4B_t09 đ‘„ > 0 17đ‘„ (deg 1, 0.061s) 17| [0, đ‘„] | (deg 1, 0.014s)C4B_t13 đ‘„ > 0 ∧ 𝑩 > 0 1.25đ‘„ + 𝑩 (deg 1, 0.060s) 1.25| [0, đ‘„] | + | [0, 𝑩] | (deg 1, 0.008s)C4B_t15 đ‘„ > 𝑩 ∧ 𝑩 > 0 1.1667đ‘„ (deg 1, 0.072s) 1.3333| [0, đ‘„] | (deg 1, 0.014s)C4B_t19 𝑖 > 100 ∧ 𝑘 > 0 𝑘 + 2𝑖 − 49 (deg 1, 0.059s) | [0, 51 + 𝑖 + 𝑘] | + 2| [0, 𝑖] | (deg 1, 0.010s)C4B_t30 đ‘„ > 0 ∧ 𝑩 > 0 0.5đ‘„ + 0.5𝑩 + 2 (deg 1, 0.044s) 0.5| [0, đ‘„ + 2] | + 0.5| [0, 𝑩 + 2] | (deg 1, 0.007s)C4B_t61 𝑙 ≄ 8 1.4286𝑙 (deg 1, 0.046s) | [0, 𝑙] | + 0.5| [1, 𝑙] | (deg 1, 0.007s)

bayesian_network 𝑛 > 0 4𝑛 (deg 1, 0.264s) 4| [0, 𝑛] | (deg 1, 0.057s)ber đ‘„ < 𝑛 2(𝑛 − đ‘„) (deg 1, 0.035s) 2| [đ‘„, 𝑛] | (deg 1, 0.004s)bin 𝑛 > 0 0.2(𝑛 + 9) (deg 1, 0.036s) 0.2| [0, 𝑛 + 9] | (deg 1, 0.030s)

condand 𝑛 > 0 ∧𝑚 > 0 2𝑚 (deg 1, 0.042s) 2| [0,𝑚] | (deg 1, 0.004s)cooling 𝑚𝑡 > 𝑠𝑡 ∧ 𝑡 > 0 𝑚𝑡 − 𝑠𝑡 + 0.42𝑡 + 2.1 (deg 1, 0.071s) 0.42| [0, 𝑡 + 5] | + | [𝑠𝑡,𝑚𝑡] | (deg 1, 0.017s)coupon ⊀ 11.6667 (deg 4, 0.066s) 15 (deg 1, 0.016s)

cowboy_duel ⊀ 1.2 (deg 1, 0.030s) 1.2 (deg 1, 0.004s)cowboy_duel_3way ⊀ 2.0833 (deg 1, 0.110s) 2.0833 (deg 1, 0.142s)

fcall đ‘„ < 𝑛 2(𝑛 − đ‘„) (deg 1, 0.053s) 2| [đ‘„, 𝑛] | (deg 1, 0.004s)filling_vol 𝑣𝑜𝑙𝑇 đč > 0 0.6667𝑣𝑜𝑙𝑇 đč + 7 (deg 1, 0.082s) 0.3333| [0, 𝑣𝑜𝑙𝑇 đč + 10] | + 0.3333| [0, 𝑣𝑜𝑙𝑇 đč + 11] | (deg 1, 0.079s)

geo ⊀ 5 (deg 1, 0.061s) 5 (deg 1, 0.003s)hyper đ‘„ < 𝑛 5(𝑛 − đ‘„) (deg 1, 0.035s) 5| [đ‘„, 𝑛] | (deg 1, 0.005s)linear01 đ‘„ > 2 0.6đ‘„ (deg 1, 0.034s) 0.6| [0, đ‘„] | (deg 1, 0.008s)prdwalk đ‘„ < 𝑛 1.1429(𝑛 − đ‘„ + 4) (deg 1, 0.037s) 1.1429| [đ‘„, 𝑛 + 4] | (deg 1, 0.011s)prnes 𝑩 > 0 ∧ 𝑛 < 0 −68.4795𝑛 + 0.0526(𝑩 − 1) (deg 1, 0.071s) 68.4795| [𝑛, 0] | + 0.0526| [0, 𝑩] | (deg 1, 0.016s)prseq 𝑩 > 9 ∧ đ‘„ − 𝑩 > 2 1.65đ‘„ − 1.5𝑩 (deg 1, 0.061s) 1.65| [𝑩, đ‘„] | + 0.15| [0, 𝑩] | (deg 1, 0.011s)prspeed đ‘„ + 1 < 𝑛 ∧ 𝑩 < 𝑚 2(𝑚 − 𝑩) + 0.6667(𝑛 − đ‘„) (deg 1, 0.065s) 2| [𝑩,𝑚] | + 0.6667| [đ‘„, 𝑛] | (deg 1, 0.010s)race ℎ < 𝑡 0.6667(𝑡 − ℎ + 9) (deg 1, 0.039s) 0.6667| [ℎ, 𝑡 + 9] | (deg 1, 0.027s)rdseql đ‘„ > 0 ∧ 𝑩 > 0 2.25đ‘„ + 𝑩 (deg 1, 0.059s) 2.25| [0, đ‘„] | + | [0, 𝑩] | (deg 1, 0.008s)rdspeed đ‘„ + 1 < 𝑛 ∧ 𝑩 < 𝑚 2(𝑚 − 𝑩) + 0.6667(𝑛 − đ‘„) (deg 1, 0.062s) 2| [𝑩,𝑚] | + 0.6667| [đ‘„, 𝑛] | (deg 1, 0.010s)rdwalk đ‘„ < 𝑛 2(𝑛 − đ‘„ + 1) (deg 1, 0.035s) 2| [đ‘„, 𝑛 + 1] | (deg 1, 0.004s)

rejection_sampling 𝑛 > 0 2𝑛 (deg 2, 0.071s) 2| [0, 𝑛] | (deg 1, 0.350s)rfind_lv ⊀ 2 (deg 1, 0.033s) 2 (deg 1, 0.004s)rfind_mc 𝑘 > 0 2 (deg 1, 0.047s) | [0, 𝑘] | (deg 1, 0.007s)robot 𝑛 > 0 0.2778(𝑛 + 7) (deg 1, 0.047s) 0.3846| [0, 𝑛 + 6] | (deg 1, 0.017s)roulette 𝑛 < 10 −4.9333𝑛 + 98.6667 (deg 1, 0.057s) 4.9333| [𝑛, 20] | (deg 1, 0.073s)sprdwalk đ‘„ < 𝑛 2(𝑛 − đ‘„) (deg 1, 0.036s) 2| [đ‘„, 𝑛] | (deg 1, 0.004s)

trapped_miner 𝑛 > 0 7.5𝑛 (deg 1, 0.081s) 7.5| [0, 𝑛] | (deg 1, 0.015s)complex 𝑩,đ‘€, 𝑛,𝑚 > 0 4𝑚𝑛 + 2𝑛 +đ‘€ + 0.6667(𝑩 + 1) (deg 2, 0.142s) (4| [0,𝑚] | + 2) | [0, 𝑛] | + (| [0,đ‘€] | + 0.3333| [0, 𝑩] |) | [0, 𝑩 + 1] | + 0.6667 (deg 2, 0.451s)multirace 𝑛 > 0 ∧𝑚 > 0 2𝑚𝑛 + 4𝑛 (deg 2, 0.080s) 2| [0,𝑚] | | [0, 𝑛] | + 4| [0, 𝑛] | (deg 2, 0.692s)pol04 đ‘„ > 0 4.5đ‘„2 + 10.5đ‘„ (deg 2, 0.052s) 1.5| [0, đ‘„] |2 + 3| [1, đ‘„] | | [0, đ‘„] | + 10.5| [0, đ‘„] | (deg 2, 0.101s)pol05 đ‘„ > 0 0.6667(đ‘„2 + 3đ‘„) (deg 2, 0.059s) 2| [0, đ‘„ + 1] | | [0, đ‘„] | (deg 2, 0.133s)pol07 𝑛 > 1 1.5𝑛2 − 4.5𝑛 + 3 (deg 2, 0.057s) 1.5| [1, 𝑛] | | [2, 𝑛] | (deg 2, 0.203s)rdbub 𝑛 > 0 3𝑛2 (deg 2, 0.055s) 3| [0, 𝑛] |2 (deg 2, 0.030s)

recursive 𝑎𝑟𝑔𝑙 < 𝑎𝑟𝑔ℎ 0.25(𝑎𝑟𝑔ℎ − 𝑎𝑟𝑔𝑙 )2 + 1.75(𝑎𝑟𝑔ℎ − 𝑎𝑟𝑔𝑙 ) (deg 2, 0.313s) 0.25| [𝑎𝑟𝑔𝑙 , 𝑎𝑟𝑔ℎ] |2 + 1.75| [𝑎𝑟𝑔𝑙 , 𝑎𝑟𝑔ℎ] | (deg 2, 0.398s)trader 𝑚𝑃 > 0 ∧ 𝑠𝑃 > 𝑚𝑃 1.5(𝑠𝑃2 −𝑚𝑃2 + 𝑠𝑃 −𝑚𝑃) (deg 2, 0.073s) 1.5| [𝑚𝑃, 𝑠𝑃] |2 + 3| [𝑚𝑃, 𝑠𝑃] | | [0,𝑚𝑃] | + 1.5| [𝑚𝑃, 𝑠𝑃] | (deg 2, 0.192s)

Table 6. Upper and lower bounds of the expectation of (possibly) non-monotone costs, with comparison to Wang et al. [43].program pre-cond. termination bounds by our tool bounds by Wang et al. [43]Bitcoin

đ‘„ ≄ 1 đ”Œ[𝑇 ] < ∞ ub. −1.475đ‘„ (deg 1, 0.078s) −1.475đ‘„ + 1.475 (deg 2, 3.705s)Mining (deg 1, 0.080s) lb. −1.5đ‘„ (deg 1, 0.088s) −1.5đ‘„ (deg 2, 3.485s)Bitcoin

𝑩 ≄ 0 đ”Œ[𝑇 2] < ∞ ub. −7.375𝑩2 − 66.375𝑩 (deg 2, 0.127s) −7.375𝑩2 − 41.625𝑩 + 49 (deg 2, 5.936s)Mining Pool (deg 4, 0.168s) lb. −7.5𝑩2 − 67.5𝑩 (deg 2, 0.134s) −7.5𝑩2 − 67.5𝑩 (deg 2, 6.157s)Queueing

𝑛 > 0 đ”Œ[𝑇 2] < ∞ ub. 0.0531𝑛 (deg 2, 0.134s) 0.0492𝑛 (deg 3, 69.669s)Network (deg 2, 0.208s) lb. 0.028𝑛 (deg 2, 0.139s) 0.0384𝑛 (deg 3, 68.849s)Running

đ‘„ ≄ 0 đ”Œ[𝑇 2] < ∞ ub. 0.3333(đ‘„2 + đ‘„) (deg 2, 0.063s) 0.3333đ‘„2 + 0.3333đ‘„ (deg 2, 3.766s)Example (deg 2, 0.064s) lb. 0.3333(đ‘„2 + đ‘„) (deg 2, 0.059s) 0.3333đ‘„2 + 0.3333đ‘„ − 0.6667 (deg 2, 3.555s)Nested

𝑖 ≄ 0 đ”Œ[𝑇 2] < ∞ ub. 0.3333𝑖2 + 𝑖 (deg 2, 0.117s) 0.3333𝑖2 + 𝑖 (deg 2, 28.398s)Loop (deg 4, 0.127s) lb. 0.3333𝑖2 + 𝑖 (deg 2, 0.115s) 0.3333𝑖2 − 𝑖 (deg 2, 7.299s)

Randomđ‘„ ≀ 𝑛 đ”Œ[𝑇 ] < ∞ ub. 2.5đ‘„ − 2.5𝑛 − 2.5 (deg 1, 0.064s) 2.5đ‘„ − 2.5𝑛 (deg 2, 4.536s)

Walk (deg 1, 0.063s) lb. 2.5đ‘„ − 2.5𝑛 − 2.5 (deg 1, 0.068s) 2.5đ‘„ − 2.5𝑛 − 2.5 (deg 2, 4.512s)

2D Robot đ‘„ ≄ 𝑩 đ”Œ[𝑇 2] < ∞ ub. 1.7280(đ‘„ − 𝑩)2 + 31.4539(đ‘„ − 𝑩) + 126.5167 (deg 2, 0.132s) 1.7280(đ‘„ − 𝑩)2 + 31.4539(đ‘„ − 𝑩) + 126.5167 (deg 2, 7.133s)(deg 2, 0.145s) lb. 1.7280(đ‘„ − 𝑩)2 + 31.4539(đ‘„ − 𝑩) + 29.7259 (deg 2, 0.121s) 1.7280(đ‘„ − 𝑩)2 + 31.4539(đ‘„ − 𝑩) (deg 2, 7.040s)

Good 𝑑 ≀ 30 ∧ đ”Œ[𝑇 2] < ∞ ub. −0.5𝑛 − 3.6667𝑑 + 117.3333 (deg 2, 0.093s) 0.0067𝑑𝑛 − 0.7𝑛 − 3.8035𝑑 + 0.0022𝑑2 + 119.4351 (deg 2, 5.272s)Discount 𝑛 ≄ 1 (deg 2, 0.093s) lb. −0.005𝑛2 − 0.5𝑛 (deg 2, 0.092s) 0.0067𝑑𝑛 − 0.7133𝑛 − 3.8123𝑑 + 0.0022𝑑2 + 112.3704 (deg 2, 5.323s)Pollutant

𝑛 ≄ 0 đ”Œ[𝑇 2] < ∞ ub. −0.2𝑛2 + 50.2𝑛 (deg 2, 0.092s) −0.2𝑛2 + 50.2𝑛 (deg 2, 5.851s)Disposal (deg 2, 0.091s) lb. −0.2𝑛2 + 50.2𝑛 − 435.6 (deg 2, 0.094s) −0.2𝑛2 + 50.2𝑛 − 482 (deg 2, 5.215s)Species 𝑎 ≄ 5 ∧ đ”Œ[𝑇 2] < ∞ ub. 40𝑎𝑏 − 180𝑎 − 180𝑏 + 810 (deg 2, 0.045s) 40𝑎𝑏 − 180𝑎 − 180𝑏 + 810 (deg 3, 5.545s)Fight 𝑏 ≄ 5 (deg 2, 0.042s) lb. N/A N/A

31

Page 32: Central Moment Analysis for Cost Accumulators in

PLDI ’21, June 20–25, 2021, Virtual, UK Di Wang, Jan Hoffmann, and Thomas Reps

1 func compare(guess, secret) begin2 𝑖 B 𝑁 ; cmp B 0;3 while 𝑖 > 0 do4 tick(2);5 while 𝑖 > 0 do6 if prob(0.5) then break fi7 tick(5);8 if cmp > 0 ∹ (cmp = 0 ∧ guess[𝑖] > secret [𝑖])9 then cmp B 110 else11 tick(5);12 if cmp < 0 ∹ (cmp = 0 ∧ guess[𝑖] < secret [𝑖])13 then cmp B −114 fi15 fi16 tick(1);17 𝑖 B 𝑖 − 118 od19 od;20 return cmp21 end

(b)

1 func check(guess) begin2 cmp B compare(guess, secret);3 if cmp = 0 then4 login()5 fi6 end

(a)

1 guess B Âź0;2 𝑖 B 𝑁 ;3 while 𝑖 > 0 do4 next B guess;5 next [𝑖] B 1;6 est B estimateTime(đŸ, check(next));7 if est ≀ 13𝑁 − 1.5𝑖 then8 guess[𝑖] B 09 else10 guess[𝑖] B 111 fi;12 𝑖 B 𝑖 − 113 od

(c)

Figure 16. (a) The interface of the password checker. (b) A function that compares two bit vectors, adding some randomnoise. (c) An attack program that attempts to exploit the timing properties of compare to find the value of the password storedin secret.

Toward this end, we can analyze the failure probabilityfor setting guess[𝑖] incorrectly, which happens when, dueto an unfortunate fluctuation, the running-time estimate estis closer to one of đ”Œ[𝑇1] and đ”Œ[𝑇0], but the truth is actuallythe other. For instance, suppose that đ”Œ[𝑇0] < đ”Œ[𝑇1] and𝑒𝑠𝑡 < đ”Œ[𝑇0 ]+đ”Œ[𝑇1 ]

2 ; the attack program would pick 𝑇0 as thetruth, and set guess[𝑖] to 0. If such a choice is incorrect, thenthe actual distribution of est on the 𝑖-th round of the attackprogram satisfies đ”Œ[est] = đ”Œ[𝑇1], and the probability of thisfailure event is

ℙ

[est <

đ”Œ[𝑇0] + đ”Œ[𝑇1]2

]= ℙ

[est − đ”Œ[𝑇1] < đ”Œ[𝑇0] − đ”Œ[𝑇1]

2

]= ℙ

[est − đ”Œ[est] < đ”Œ[𝑇0] − đ”Œ[𝑇1]

2

]under the condition given by the conjunction in (11). Thisformula has exactly the same shape as a tail probability,whichmakes it possible to utilizemoments and concentration-of-measure inequalities [13] to bound the probability.

The attack program is parameterized by đŸ > 0, which rep-resents the number of trials it performs for each bit positionto obtain an estimate of the running time. Assume that wehave applied our central-moment-analysis technique (§3),and obtained the following inequalities on the mean (i.e., thefirst moment), the second moment, and the variance (i.e., the

second central moment) of the quantities (11) and (12).đ”Œ[𝑇1] ≄ 13𝑁, đ”Œ[𝑇1] ≀ 15𝑁,𝕍 [𝑇1] ≀ 26𝑁 2 + 42𝑁, (13)đ”Œ[𝑇0] ≄ 13𝑁 − 5𝑖, đ”Œ[𝑇0] ≀ 13𝑁 − 3𝑖,𝕍 [𝑇0] ≀ 8𝑁 − 36𝑖2 + 52𝑁𝑖 + 24𝑖 . (14)

To bound the probability that the attack program makes anincorrect guess for the 𝑖-th bit, we do case analysis:‱ Suppose that secret [𝑖] = 1, but the attack program assignsguess[𝑖] B 0. The truth—with respect to the actual distri-bution of the running time𝑇 of compare for the 𝑖-th bit—isthatđ”Œ[est] = đ”Œ[𝑇1], but the attack program in Fig. 16(c) ex-ecutes the then-branch of the conditional statement. Thus,our task reduces to that of bounding ℙ[est < 13𝑁 − 1.5𝑖].The estimate est is the average of đŸ i.i.d. random variablesdrawn from a distribution with mean đ”Œ[𝑇1] and variance𝕍 [𝑇1]. We derive the following, using the inequalities from(13):

đ”Œ[est] = đ”Œ[𝑇1] ≄ 13𝑁,

𝕍 [est] = 𝕍 [𝑇1]đŸ

≀ 26𝑁 2 + 42đ‘đŸ

. (15)We now use Cantelli’s inequality.

Proposition (Cantelli’s inequality). If𝑋 is a random vari-able and 𝑎 > 0, then we haveℙ[𝑋 âˆ’đ”Œ[𝑋 ] ≀ −𝑎] ≀ 𝕍 [𝑋 ]

𝕍 [𝑋 ]+𝑎2and ℙ[𝑋 − đ”Œ[𝑋 ] ≄ 𝑎] ≀ 𝕍 [𝑋 ]

𝕍 [𝑋 ]+𝑎2 .

32

Page 33: Central Moment Analysis for Cost Accumulators in

Central Moment Analysis for Cost Accumulators in Probabilistic Programs PLDI ’21, June 20–25, 2021, Virtual, UK

We are now able to derive an upper bound on ℙ[est <

13𝑁 − 1.5𝑖] as follows:ℙ[est ≀ 13𝑁 − 1.5𝑖]

= ℙ[est − 13𝑁 ≀ −1.5𝑖]≀ ℙ[est − đ”Œ[est] ≀ −1.5𝑖] † đ”Œ[est] ≄ 13𝑁 by (15) †

≀ 𝕍 [est]𝕍 [est] + (1.5𝑖)2 † Cantelli’s inequality †

=26𝑁 2 + 42𝑁

26𝑁 2 + 42𝑁 + 2.25đŸđ‘–2 .‱ The other case, in which secret [𝑖] = 0 but the attack pro-gram chooses to set guess[𝑖] B 1, can be analyzed in asimilar way to the previous case, and the bound obtainedis the following:

ℙ[est > 13𝑁 − 1.5𝑖]≀ ℙ[est ≄ 13𝑁 − 1.5𝑖]

≀ 8𝑁 − 36𝑖2 + 52𝑁𝑖 + 24𝑖8𝑁 − 36𝑖2 + 52𝑁𝑖 + 24𝑖 + 2.25đŸđ‘–2 .

Let đč 𝑖1 and đč 𝑖0, respectively, denote the two upper bounds onthe failure probabilities for the 𝑖-th bit.

For the attack program to succeed, it has to succeed forall bits. If the number of bits is 𝑁 = 32, and in each iterationthe number of trials that the attack program uses to estimatethe running time is đŸ = 104, we derive a lower bound on thesuccess rate of the attack program from the upper boundson the failure probabilities derived above:

ℙ[Success] ≄32∏𝑖=1

(1 −max(đč 𝑖1, đč 𝑖0)) ≄ 0.219413,

which is low, but not insignificant. However, the somewhatlow probability is caused by a property of compare: if guessand secret share a very long prefix, then the running-timebehavior on different values of 𝑔𝑱𝑒𝑠𝑠 becomes indistinguish-able. However, if instead we bound the success rate for allbut the last six bits, we obtain:ℙ[Success for all but the last six bits] ≄ 0.830561,

which is a much higher probability! The attack programcan enumerate all the possibilities to resolve the last sixbits. (Moreover, by brute-forcing the last six bits, a total of10,000× 26+ 64 = 260,064 calls to check would be performed,rather than 320,000.)Overall, our analysis concludes that the check and com-

pare procedures in Fig. 16 are vulnerable to a timing attack.

33