Introduction
Incremental computation seeks to efficiently maintain a program’s outputs as its inputs evolve, without paying the cost of recomputing from scratch. A standard way to achieve this goal is to derive update logic that explains how the output should change as new data arrive [39 , 51 ] . This paradigm is central to modern data-processing frameworks [15 , 17 , 42 , 44 ] , which commonly support stateful computations by repeatedly applying such update logic to a maintained state as new records arrive. In this way, the system incrementally accumulates the effect of the input stream, using the evolving state as a concise summary of prior computation.
Yet insertion is only part of the story. In many practical workloads, systems must also undo the effect of previously incorporated inputs. For example, records may age out of sliding windows, stale
Authors’ Contact Information: Ziteng Wang, University of Texas at Austin, Austin, USA, ziteng@utexas.edu; Linus Zheng, University of Texas at Austin, Austin, USA, linus@utexas.edu; Dixin Tang, University of Texas at Austin, Austin, USA, dixin@utexas.edu; Işıl Dillig, University of Texas at Austin, Austin, USA, isil@cs.utexas.edu.
This work is licensed under a Creative Commons Attribution 4.0 International License.
© 2027 Copyright held by the owner/author(s).
ACM 2475-1421/2027/1-ART30
https://doi.org/10.1145/3871732
Proc. ACM Program. Lang., Vol. 11, No. POPL, Article 30. Publication date: January 2027. outputs may need to be withdrawn, and continuously maintained views must reflect deletions as well as additions. For this reason, modern data-processing frameworks such as Apache Flink [17 ] and Materialize [41 ] also support change-oriented maintenance, making it (in principle) possible to process deletions and modifications incrementally rather than by recomputing from scratch.
In practice, however, interfaces for incremental deletion are harder to use than their insertion-only counterparts. For example, while Flink provides a retract interface for handling deletions, developers must implement the retraction logic themselves and maintain any necessary auxiliary state. This can be challenging both semantically and algorithmically. Semantically, retracting a past contribution is harder because the maintained state may already have discarded the information needed to faithfully reverse its effect. Algorithmically, deletion support often demands auxiliary state and carefully orchestrated rollback logic, making such operators significantly harder to design and implement. As a result, although modern frameworks facilitate incremental deletion in principle, exploiting this capability effectively remains challenging in practice.
Motivated by this observation, this paper addresses the problem of automatically transforming an insertion-only computation into one that also supports incremental deletions . We study this problem in the setting of fold-based computations over input streams, which underlie many operators in modern data-processing systems. Such a setting is compelling for two reasons. First, it is broad: many practically relevant operators can be viewed as repeated updates to a maintained summary. Second, it exposes the core difficulty: because the past influences future behavior only through a summary , the ability to retract prior inputs depends entirely on what the computation has chosen to preserve. Hence, this setting isolates the central tension behind incremental deletion.
At a high level, the core contribution of this paper is a method for synthesizing incremental deletion logic for fold-based computations. The method takes as input an insertion-only computation together with a retraction specification describing which deletions must be supported. Such a specification is necessary because deletion support is inherently partial: a computation may be able to retract some prior inputs incrementally, but not others. Given these inputs, our method determines what additional information, if any, must be retained to support the required deletions, synthesizes the lifted update logic needed to maintain that information, and automatically constructs the corresponding retraction function. The resulting computation is then formally verified to be semantically equivalent to the original insertion-only computation.
From a programming languages perspective, this paper makes two main technical contributions. The first contribution is a semantic account of incremental deletion and a new verification methodology for proving equivalence between the insertion-only version and the corresponding retractable one. This equivalence checking problem is inherently challenging because (1) the retractable computation may maintain a different internal summary; (2) executions may contain arbitrary interleavings of insertions and deletions, and (3) correctness depends on which deletions must be handled incrementally. Our proposed verification methodology addresses all of these challenges by reducing a difficult global reasoning problem to local proof obligations over inductive relational invariants. The result is a sound and practically effective method for establishing semantic equivalence despite differences in internal representation and execution history.
Our second technical contribution is a synthesis framework for correct-by-construction retractable computations. The central difficulty is that the space of correct solutions is constrained simultaneously by the choice of retained summary information and by how deletion is performed over that representation. Our approach addresses this tension by jointly searching for auxiliary summary state and a lifting invariant that semantically relates the original fold to its enriched counterpart. Once the required auxiliary state is identified, our method derives the retraction logic by inferring a relational invariant as a local specification and then decomposing the implementation into semantic cases, each capturing a different way the removed element can affect the maintained summary. We implemented the proposed approach in a new tool called Rewind and evaluated it on 94 benchmarks covering real-world aggregations written in the Flink framework [17 ] . Rewind successfully synthesized correct retraction logic for all 94 scenarios, including cases where the original manually-written logic is wrong. We also conducted ablation studies demonstrating that all of our proposed ingredients are crucial for the practicality of Rewind. Finally, our performance results show that the synthesized incremental deletions substantially outperform recomputation.
To summarize, this paper makes the following contributions:
Formalization and verification. We formalize correctness for retractable folds by requiring that any supported sequence of insertions and deletions produce the same result as re-running the original fold on the remaining elements. We then give a sound and relatively complete verification methodology based on inductive relational invariants.
Synthesis of retractable folds. We develop a synthesis framework that turns an insertion-only fold into a retractable one by jointly discovering the enriched summary state and the operations over it. Rather than assuming a fixed representation, our method determines what information must be retained to make the required deletions possible, synthesizes lifted update logic to maintain that information, and derives a retraction function that is correct by construction.
Implementation and evaluation. We implement the approach in a prototype called Rewind and evaluate it on 94 benchmarks over 48 real-world aggregations and multiple retraction policies. Our evaluation demonstrates high synthesis success rates, the ability to uncover bugs in manual implementations, and the practical importance of the method’s main ingredients.
Motivating Example
We illustrate our approach using an aggregation function, adapted from [50 ] , that computes auction statistics, including the maximum bid and the number of “high” bids exceeding $1000. This example captures a common pattern in streaming analytics, where an accumulator maintains key statistics, such as extremal values or counts. The insertion-only version of this computation, shown in Figure 1 , is straightforward: the accumulator stores two fields, namely the current maximum and the high-bid count, and each call to accumulate updates these fields incrementally.
Figure 1. Insertion-only aggregation.
Now suppose the auction allows bidders to remove previously placed bids, but only under a specified service policy. One natural policy is a fixed guarantee: the auction is guaranteed to support up to N N N bid removals, regardless of which bids are removed (Policy A ). We express this policy as:
θ A ( x s ) = min ( length ( x s ) , N ) \theta_A(xs) = \min(\operatorname{length}(xs), N) θ A ( x s ) = min ( length ( x s ) , N )
which says that, after bid history x s xs x s , the system must support removing N N N bids if there are at least N N N bids and all bids otherwise. Another natural policy is to protect the stability of the standing price. Under this policy, if the highest bid seen so far appears k k k times, the auction guarantees support for k − 1 k - 1 k − 1 arbitrary bid removals, so that the highest bid is not completely removed even in the worst case (Policy B ). We express this policy as:
θ B ( x s ) = count ( x s , max ( x s ) ) − 1 \theta_B(xs) = \operatorname{count}(xs, \max(xs)) - 1 θ B ( x s ) = count ( x s , max ( x s )) − 1
These two policies illustrate different kinds of retraction requirements: Policy A imposes a fixed removal budget, while Policy B makes the guarantee dependent on redundancy in the current auction state. Furthermore, the original aggregation state in Figure 1 cannot support either policy as written, because it discards information that may be needed when bids are later removed.
The missing information depends on the policy. To support Policy A, the aggregate must retain enough high-valued bids to recover the maximum after a bounded number of removals, and it must also track how many removals have already been processed. To support Policy B, the aggregate only needs to know whether the current maximum has another copy. Thus, even for the same base computation, different retraction requirements call for different auxiliary summaries.
Figure 2 a and Figure 2 b show two corresponding retractable variants of the computation from Figure 1 implementing Policies A and B, respectively. Both extend the original aggregation in three ways: they add auxiliary state, revise the insertion logic so that this state is maintained as new bids arrive, and implement retraction logic that updates the enriched state when bids are removed. Crucially, the details of all three components depend on the policy.
Figure 2. Two retractable variants of the auction aggregation: (a) supports a fixed budget of up to N N N withdrawals; (b) supports the worst-case budget induced by multiplicity of the maximum bid.
The rest of this paper shows how to automatically generate retractable computations like those in Figure 2 a and Figure 2 b. Given an insertion-only computation such as Figure 1 and a user-provided retraction specification, such as θ A \theta_A θ A or θ B \theta_B θ B , our method synthesizes the enriched state, the lifted insertion logic, and the corresponding retraction procedure. Crucially, the synthesized computation comes with a formal guarantee: it satisfies the specified retraction requirement, and every supported sequence of removals yields the same observable result as recomputing the original aggregation.
Problem Statement
In this section, we formalize retractable folds and their semantics.
Semantics of Retractable Folds
Definition 3.1 (Fold-based computation). A fold-based computation is a triple P = ( I , acc , eval ) \mathcal{P} = (I, \operatorname{acc}, \operatorname{eval}) P = ( I , acc , eval ) , where I ∈ S I \in S I ∈ S is an initial state, acc : S × A → S \operatorname{acc}: S \times A \to S acc : S × A → S is an accumulator function, and eval : S → O \operatorname{eval}: S \to O eval : S → O is an evaluation function. For an input list x s = [ x 1 , … , x n ] xs = [x_1, \ldots, x_n] x s = [ x 1 , … , x n ] , let s 0 , … , s n s_0, \ldots, s_n s 0 , … , s n be a state sequence defined as:
s 0 = I , s j = acc ( s j − 1 , x j ) for 1 ≤ j ≤ n \begin{aligned}
s_0 &= I, \qquad s_j = \operatorname{acc}(s_{j-1}, x_j) \quad\text{for } 1 \leq j \leq n
\end{aligned} s 0 = I , s j = acc ( s j − 1 , x j ) for 1 ≤ j ≤ n The state semantics of P \mathcal{P} P , denoted [ P ] x s [\mathcal{P}]_{xs} [ P ] x s , is the final state [ P ] x s ≜ s n [\mathcal{P}]_{xs} \triangleq s_n [ P ] x s ≜ s n , and the observable semantics of P \mathcal{P} P is ⟦ P ⟧ x s ≜ eval ( [ P ] x s ) \llbracket\mathcal{P} \rrbracket_{xs} \triangleq\operatorname{eval}([\mathcal{P}]_{xs}) [ [ P ] ] x s ≜ eval ([ P ] x s ) .
Thus, the computation has the standard form eval ( fold ( acc , I , X , x s ) ) \operatorname{eval}(\operatorname{fold}(\operatorname{acc}, \mathcal{I}, \mathcal{X}, \mathit{xs})) eval ( fold ( acc , I , X , xs )) , where fold builds the internal state, and eval converts that state into an observable output. For example, an average computation may maintain a sum and count, and eval returns their quotient. A retractable fold, which is the central object of study in this paper, extends Definition 3.1 with a retraction function for supporting incremental deletions:
Definition 3.2 (Retractable fold). A retractable fold is a quadruple R = ( I , acc , retract , eval ) \mathcal{R} = (\mathcal{I}, \operatorname{acc}, \operatorname{retract}, \operatorname{eval}) R = ( I , acc , retract , eval ) , where I ∈ S \mathcal{I} \in S I ∈ S is an initial state, acc : S × A → S \operatorname{acc}: S \times A \to S acc : S × A → S is an accumulator function, retract : S × A → S ∪ { Err } \operatorname{retract}: S \times A \to S \cup\{\operatorname{Err}\} retract : S × A → S ∪ { Err } is a retraction function, and eval : S → O ∪ { Err } \operatorname{eval}: S \to O \cup\{\operatorname{Err}\} eval : S → O ∪ { Err } is an evaluation function.
At a high level, retract ( s , a ) \operatorname{retract}(s,a) retract ( s , a ) attempts to remove one prior occurrence of a a a from the history represented by summary state s s s . Since the maintained state may not preserve enough information to support such a reversal, retract is allowed to fail with Err. The function eval maps the internal state to the observable output, discarding any auxiliary state. To define the semantics of a retractable fold under both insertions and deletions, we represent its input as an update sequence :
Definition 3.3 (Update sequence). An update sequence ω \omega ω is a list ω ∈ [ ( acc , retract ) × A ] \omega\in[( \operatorname{acc}, \operatorname{retract}) \times A] ω ∈ [( acc , retract ) × A ] , where each element consists of an operation tag (acc or retract) and an input value a ∈ A a \in A a ∈ A . For ω = ⟨ ( o 1 , a 1 ) , … , ( o n , a n ) ⟩ \omega= \langle(o_1,a_1),\ldots,(o_n,a_n)\rangle ω = ⟨( o 1 , a 1 ) , … , ( o n , a n )⟩ , we define:
ω + = [ ( o j , a j ) ∣ o j = acc ] , ω − = [ ( o j , a j ) ∣ o j = retract ] , ζ ( ω ) = [ a j ] . \omega^{+} = [(o_j,a_j)\mid o_j=\operatorname{acc}],\qquad\omega^{-}=[(o_j,a_j)\mid o_j=\operatorname{retract}],\qquad\zeta(\omega)=[a_j]. ω + = [( o j , a j ) ∣ o j = acc ] , ω − = [( o j , a j ) ∣ o j = retract ] , ζ ( ω ) = [ a j ] .
In other words, ω + \omega^{+} ω + and ω − \omega^{-} ω − yield only the insertions and deletions respectively, whereas ζ ( ω ) \zeta(\omega) ζ ( ω ) drops the operation tags entirely.
For a pair of input sequences ω , ω ′ \omega,\omega' ω , ω ′ , we write ω ⪯ ω ′ \omega\preceq\omega' ω ⪯ ω ′ if ω \omega ω is a prefix of ω ′ \omega' ω ′ . Next, we define a valid update sequence where each deletion contains a matching insertion:
Definition 3.4 (Valid sequence). An update sequence ω = ⟨ ( o 1 , a 1 ) , … , ( o n , a n ) ⟩ \omega= \langle(o_1,a_1),\ldots,(o_n,a_n)\rangle ω = ⟨( o 1 , a 1 ) , … , ( o n , a n )⟩ is valid if
∀ j . ( 1 ≤ j ≤ n ) ⟹ ( ∑ 1 ≤ i ≤ j 1 { o i = acc ∧ a i = a j } ≥ ∑ 1 ≤ i ≤ j 1 { o i = retract ∧ a i = a j } ) \forall j.\,(1 \le j \le n)\Longrightarrow\left(\sum_{1\le i\le j}\mathbf{1}\{o_i=\operatorname{acc}\land a_i=a_j\}\ge\sum_{1\le i\le j}\mathbf{1}\{o_i=\operatorname{retract}\land a_i=a_j\}\right) ∀ j . ( 1 ≤ j ≤ n ) ⟹ ( 1 ≤ i ≤ j ∑ 1 { o i = acc ∧ a i = a j } ≥ 1 ≤ i ≤ j ∑ 1 { o i = retract ∧ a i = a j } )
That is, an input to a retractable fold is valid if every deletion has a matching insertion that precedes it. From now on, we use the term update sequence to mean one that is valid.
Definition 3.5 (Retractable fold semantics). Consider a retractable fold R = ( I , acc , retract , eval ) \mathcal{R}=(\mathcal{I},\operatorname{acc},\operatorname{retract},\operatorname{eval}) R = ( I , acc , retract , eval ) and an update sequence ω = ⟨ ( o 1 , a 1 ) , … , ( o n , a n ) ⟩ \omega=\langle(o_1,a_1),\ldots,(o_n,a_n)\rangle ω = ⟨( o 1 , a 1 ) , … , ( o n , a n )⟩ . Let s 0 , … , s n s_0,\ldots,s_n s 0 , … , s n be a state sequence defined as:
s 0 = I , s j = { acc ( s j − 1 , a j ) if s j − 1 ≠ Err ∧ o j = acc retract ( s j − 1 , a j ) if s j − 1 ≠ Err ∧ o j = retract Err otherwise s_0=\mathcal{I},\qquad s_j=
\begin{cases}
\operatorname{acc}(s_{j-1},a_j) & \text{if }s_{j-1}\ne\operatorname{Err}\land o_j=\operatorname{acc}\\
\operatorname{retract}(s_{j-1},a_j) & \text{if }s_{j-1}\ne\operatorname{Err}\land o_j=\operatorname{retract}\\
\operatorname{Err} & \text{otherwise}
\end{cases} s 0 = I , s j = ⎩ ⎨ ⎧ acc ( s j − 1 , a j ) retract ( s j − 1 , a j ) Err if s j − 1 = Err ∧ o j = acc if s j − 1 = Err ∧ o j = retract otherwise
The state semantics of R \mathcal{R} R , denoted [ R ] ω [\mathcal{R}]_{\omega} [ R ] ω , is the final state [ R ] ω = ^ s n [\mathcal{R}]_{\omega}\mathrel{\widehat{=}}s_n [ R ] ω = s n . The observable semantics is the corresponding output value:
[ [ R ] ] ω = ^ ( [ R ] ω ≠ Err ? eval ( [ R ] ω ) : Err ) [\![\mathcal{R}]\!]_{\omega}\mathrel{\widehat{=}}\left([\mathcal{R}]_{\omega}\ne\operatorname{Err}\ ?\ \operatorname{eval}([\mathcal{R}]_{\omega})\ :\ \operatorname{Err}\right) [ [ R ] ] ω = ( [ R ] ω = Err ? eval ([ R ] ω ) : Err )
Retraction Specifications and Equivalence
To define correctness for a retractable fold, we must first specify the type of deletion support it is required to provide. As illustrated in § 2 , a computation can admit multiple retractable variants, each supporting a different class of deletions. Requiring unrestricted deletion support may impose unnecessary storage costs when an application needs only a limited guarantee. We therefore specify how many arbitrary deletions must be supported, allowing this number to depend on the insertion history. For example, in Policy A, the budget is bounded by both N N N and the number of placed bids, whereas in Policy B, it depends on the multiplicity of the maximum bid in the insertion history. We formalize these requirements as follows:
Definition 3.6 (Retraction specification). A retraction specification for R \mathcal{R} R is a function θ : [ A ] → N \theta: [\mathcal{A}] \to\mathbb{N} θ : [ A ] → N . Formally, R \mathcal{R} R satisfies θ \theta θ , denoted R ⊨ θ \mathcal{R} \models\theta R ⊨ θ , iff \forall \omega. \left(\forall \delta \preceq \omega. |\delta^{-}| \leq \theta(\zeta(\delta^{+}))\right) \Longrightarrow \llbracket \mathcal{R} \rrbracket_{\omega} \ne \operatorname{Err}.
In other words, R \mathcal{R} R satisfies θ \theta θ if it can process any update sequence whose prefixes stay within the deletion budget specified by θ \theta θ . This budget specifies a minimum guarantee . In particular, the fold may support additional deletions, but it is not required to.
Example 3.7. Suppose an application must support withdrawing any previously inserted record. This requirement is expressed by θ ( x s ) = ∣ x s ∣ \theta(x_s) = |x_s| θ ( x s ) = ∣ x s ∣ . Each new insertion increases the deletion allowance, so a record that is withdrawn and later reinserted can be withdrawn again. In contrast, θ ( x s ) = min ( 1 , ∣ x s ∣ ) \theta(x_s) = \min(1, |x_s|) θ ( x s ) = min ( 1 , ∣ x s ∣ ) guarantees only one withdrawal over the entire execution, even if more records are subsequently inserted. An application may also require a correction allowance proportional to its workload. For example, θ ( x s ) = ⌊ ∣ x s ∣ / 100 ⌋ \theta(x_s) = \lfloor|x_s|/100 \rfloor θ ( x s ) = ⌊ ∣ x s ∣/100 ⌋ guarantees support for a total of up to one withdrawal per hundred records inserted, regardless of which records are withdrawn.
Next, to define equivalence between the original computation and its retractable version, we introduce the concept of normalizing an update sequence:
Definition 3.8 (Normalization). Given ω = ⟨ ( o 1 , a 1 ) , … , ( o n , a n ) ⟩ \omega= \langle(o_1,a_1), \ldots, (o_n,a_n)\rangle ω = ⟨( o 1 , a 1 ) , … , ( o n , a n )⟩ , the normalization of ω \omega ω , written Norm ( ω ) \operatorname{Norm}(\omega) Norm ( ω ) , is the result of repeatedly applying the following rewrite rule to a fixpoint:
( o i , a ) , ( o j , a ) ⇝ ϵ where: ( 1 ) o i = acc , o j = retract , i < j , (o_i,a),\ (o_j,a) \leadsto\epsilon\quad\text{where:}\quad(1)\ \ o_i = \operatorname{acc},\ o_j = \operatorname{retract},\ i < j, ( o i , a ) , ( o j , a ) ⇝ ϵ where: ( 1 ) o i = acc , o j = retract , i < j , ( 2 ) ∀ k . ( 1 ≤ k < i ⟹ a k ≠ a ∨ o k ≠ acc ) , ( 3 ) ∀ k . ( i < k < j ⟹ a k ≠ a ∨ o k ≠ retract ) (2)\ \ \forall k.\ (1 \leq k < i \Longrightarrow a_k \ne a \lor o_k \ne\operatorname{acc}),\quad(3)\ \ \forall k.\ (i < k < j \Longrightarrow a_k \ne a \lor o_k \ne\operatorname{retract}) ( 2 ) ∀ k . ( 1 ≤ k < i ⟹ a k = a ∨ o k = acc ) , ( 3 ) ∀ k . ( i < k < j ⟹ a k = a ∨ o k = retract ) Effect ( ω ) \operatorname{Effect}(\omega) Effect ( ω ) yields the list of elements in Norm ( ω ) \operatorname{Norm}(\omega) Norm ( ω ) , i.e., Effect ( ω ) = ζ ( Norm ( ω ) ) \operatorname{Effect}(\omega) = \zeta(\operatorname{Norm}(\omega)) Effect ( ω ) = ζ ( Norm ( ω )) .
Hence, normalization repeatedly cancels the earliest valid retraction of a value with its earliest preceding accumulation, thereby producing a canonical sequence that reflects the net effect.
Definition 3.9 (Equivalence). Let P = ( I , acc , eval ) \mathcal{P} = (\mathcal{I}, \operatorname{acc}, \operatorname{eval}) P = ( I , acc , eval ) be a regular fold and R = ( I ′ , acc ′ , retract , eval ′ ) \mathcal{R} = (\mathcal{I}', \operatorname{acc}', \operatorname{retract}, \operatorname{eval}') R = ( I ′ , acc ′ , retract , eval ′ ) a retractable one. We say that R \mathcal{R} R is equivalent to P \mathcal{P} P modulo θ \theta θ , denoted P ≡ θ R \mathcal{P} \equiv_{\theta} \mathcal{R} P ≡ θ R , iff (1) R ⊨ θ \mathcal{R} \models\theta R ⊨ θ , and (2) for every valid update sequence ω \omega ω with x s = Effect ( ω ) x_s = \operatorname{Effect}(\omega) x s = Effect ( ω ) , if ⟦ R ⟧ ω ≠ Err \llbracket\mathcal{R} \rrbracket_{\omega} \ne\operatorname{Err} [ [ R ] ] ω = Err , then ⟦ R ⟧ ω = ⟦ P ⟧ x s \llbracket\mathcal{R} \rrbracket_{\omega} = \llbracket\mathcal{P} \rrbracket_{x_s} [ [ R ] ] ω = [ [ P ] ] x s .
The definition above captures two requirements: R \mathcal{R} R must satisfy the retraction specification, and it must agree semantically with P \mathcal{P} P . That is, for any valid update sequence that does not produce an error, R \mathcal{R} R returns the same output as P \mathcal{P} P run on the normalized sequence, even if the sequence exceeds the deletion allowance specified by θ \theta θ .
Definition 3.10 (Problem statement). Given a regular fold P \mathcal{P} P and a retraction specification θ \theta θ , the synthesis problem is to construct a retractable fold R \mathcal{R} R such that P ≡ θ R \mathcal{P} \equiv_{\theta} \mathcal{R} P ≡ θ R .
For a given P \mathcal{P} P and specification θ \theta θ , no retractable fold R \mathcal{R} R over the same internal state S S S may satisfy P ≡ θ R \mathcal{P} \equiv_{\theta} \mathcal{R} P ≡ θ R . In such cases, we enrich R \mathcal{R} R ’s state to a different type S ′ S' S ′ , a process we call lifting .
Verification Methodology
To solve the synthesis problem, we first need a way to formally verify equivalence between a retractable computation and its insertion-only counterpart. However, this verification problem raises several core difficulties that are not directly handled by standard relational proof techniques:
Arbitrary update interleavings. Second, the desired guarantee must hold for all valid update sequences, including arbitrarily long executions in which insertions and deletions are interleaved arbitrarily. This requires an inductive argument showing that each supported deletion leaves the retractable state consistent with the normalized history and able to support subsequent updates.
Multiple semantic views of execution. Third, establishing correctness requires tracking three related but distinct views of the execution: the mixed sequence executed by the retractable fold, the normalized insertion-only sequence that determines its observable result, and the original insertion history that determines which future deletions must still be supported. This differs from traditional equivalence checking, where it suffices to relate two executions.
History-sensitive retraction obligations. Finally, the retraction requirement is itself history-sensitive, meaning it depends on which elements were previously inserted and how many deletions have already been performed. Thus, the verifier must reason about when a retraction is within the promised budget, rather than treating deletion as an unconstrained inverse operation.
These requirements do not fit naturally into standard relational equivalence checking frameworks. For example, although logics such as RHL [12 ] , CHL [47 ] , and their variants [7 , 8 , 10 , 11 , 18 ] , can relate multiple executions, their specifications are formulated as relational pre/postconditions over a fixed collection of runs. In contrast, correctness for retractable folds requires an inductive invariant that is closed under arbitrary future updates.
Motivated by these challenges, this section develops a domain-specific relational proof system for verifying retractable computations. The proof system decomposes the global equivalence argument into three local obligations, summarized in Figure 3 :
R. Reachability invariant
R1 (Base) Reach ( [ ] , I ′ ) \operatorname{Reach}([], \mathcal{I}') Reach ([ ] , I ′ ) .
R2 (Step) Reach ( x s , s ′ ) ⇒ Reach ( x s + + [ a ] , acc ′ ( s ′ , a ) ) \operatorname{Reach}(xs, s') \Rightarrow\operatorname{Reach}(xs \mathbin{+\!\!+} [a], \operatorname{acc}'(s', a)) Reach ( x s , s ′ ) ⇒ Reach ( x s + + [ a ] , acc ′ ( s ′ , a )) .
A. Lifting consistency
A1 (Base) eval ( I ) = eval ′ ( I ′ ) \operatorname{eval}(\mathcal{I}) = \operatorname{eval}'(\mathcal{I}') eval ( I ) = eval ′ ( I ′ ) .
A2 (Acc step) eval ( s ) = eval ′ ( s ′ ) ⇒ eval ( acc ( s , a ) ) = eval ′ ( acc ′ ( s ′ , a ) ) \operatorname{eval}(s) = \operatorname{eval}'(s') \Rightarrow\operatorname{eval}(\operatorname{acc}(s,a)) = \operatorname{eval}'(\operatorname{acc}'(s',a)) eval ( s ) = eval ′ ( s ′ ) ⇒ eval ( acc ( s , a )) = eval ′ ( acc ′ ( s ′ , a )) .
B. Retraction consistency
B1 (Base) Ψ ( [ ] , I ′ , I ′ ) \Psi([], \mathcal{I}', \mathcal{I}') Ψ ([ ] , I ′ , I ′ ) .
B2 (Acc step) Ψ ( x s , s 1 ′ , s 2 ′ ) ⇒ Ψ ( x s + + [ x ] , acc ′ ( s 1 ′ , x ) , acc ′ ( s 2 ′ , x ) ) \Psi(xs,s'_1,s'_2) \Rightarrow\Psi(xs \mathbin{+\!\!+} [x],\operatorname{acc}'(s'_1,x),\operatorname{acc}'(s'_2,x)) Ψ ( x s , s 1 ′ , s 2 ′ ) ⇒ Ψ ( x s + + [ x ] , acc ′ ( s 1 ′ , x ) , acc ′ ( s 2 ′ , x )) .
B3 (Ret step) ( Reach ( x s , s 1 ′ ) ∧ Ψ ( x s , s 1 ′ , s 2 ′ ) ∧ x ∈ x s ∧ retract ( s 2 ′ , x ) ≠ Err ∧ y s = remove ( x s , x ) ∧ Reach ( y s , s 3 ′ ) ) ⇒ Ψ ( y s , s 3 ′ , retract ( s 2 ′ , x ) ) \bigl(\operatorname{Reach}(xs,s'_1) \land\Psi(xs,s'_1,s'_2) \land x \in xs \land\operatorname{retract}(s'_2,x) \ne\operatorname{Err} \land ys = \operatorname{remove}(xs,x) \land\operatorname{Reach}(ys,s'_3)\bigr) \Rightarrow\Psi(ys,s'_3,\operatorname{retract}(s'_2,x)) ( Reach ( x s , s 1 ′ ) ∧ Ψ ( x s , s 1 ′ , s 2 ′ ) ∧ x ∈ x s ∧ retract ( s 2 ′ , x ) = Err ∧ y s = remove ( x s , x ) ∧ Reach ( y s , s 3 ′ ) ) ⇒ Ψ ( y s , s 3 ′ , retract ( s 2 ′ , x )) .
B4 (Eval) Ψ ( x s , s 1 ′ , s 2 ′ ) ⇒ eval ′ ( s 1 ′ ) = eval ′ ( s 2 ′ ) \Psi(xs,s'_1,s'_2) \Rightarrow\operatorname{eval}'(s'_1) = \operatorname{eval}'(s'_2) Ψ ( x s , s 1 ′ , s 2 ′ ) ⇒ eval ′ ( s 1 ′ ) = eval ′ ( s 2 ′ ) .
C. Quantitative retraction support
C1 (Base) Ω ( I ′ , I ′ , 0 ) \Omega(\mathcal{I}',\mathcal{I}',0) Ω ( I ′ , I ′ , 0 ) .
C2 (Acc step) Ω ( s 1 ′ , s 2 ′ , n ) ⇒ Ω ( acc ′ ( s 1 ′ , x ) , acc ′ ( s 2 ′ , x ) , n ) \Omega(s'_1,s'_2,n) \Rightarrow\Omega(\operatorname{acc}'(s'_1,x),\operatorname{acc}'(s'_2,x),n) Ω ( s 1 ′ , s 2 ′ , n ) ⇒ Ω ( acc ′ ( s 1 ′ , x ) , acc ′ ( s 2 ′ , x ) , n ) .
C3 (Ret step) ( Ω ( s 1 ′ , s 2 ′ , n ) ∧ Ψ ( x s , s 3 ′ , s 2 ′ ) ∧ x ∈ x s ∧ retract ( s 2 ′ , x ) ≠ Err ) ⇒ Ω ( s 1 ′ , retract ( s 2 ′ , x ) , n + 1 ) \bigl(\Omega(s'_1,s'_2,n) \land\Psi(xs,s'_3,s'_2) \land x \in xs \land\operatorname{retract}(s'_2,x) \ne\operatorname{Err}\bigr) \Rightarrow\Omega(s'_1,\operatorname{retract}(s'_2,x),n+1) ( Ω ( s 1 ′ , s 2 ′ , n ) ∧ Ψ ( x s , s 3 ′ , s 2 ′ ) ∧ x ∈ x s ∧ retract ( s 2 ′ , x ) = Err ) ⇒ Ω ( s 1 ′ , retract ( s 2 ′ , x ) , n + 1 ) .
C4 (Capability) ( Reach ( y s , s 1 ′ ) ∧ Ω ( s 1 ′ , s 2 ′ , n ) ∧ Ψ ( x s , s 3 ′ , s 2 ′ ) ∧ DeletesTo ( y s , x s , n ) ∧ x ∈ x s ∧ n < θ ( y s ) ) ⇒ retract ( s 2 ′ , x ) ≠ Err \bigl(\operatorname{Reach}(ys,s'_1) \land\Omega(s'_1,s'_2,n) \land\Psi(xs,s'_3,s'_2) \land\operatorname{DeletesTo}(ys,xs,n) \land x \in xs \land n < \theta(ys)\bigr) \Rightarrow\operatorname{retract}(s'_2,x) \ne\operatorname{Err} ( Reach ( y s , s 1 ′ ) ∧ Ω ( s 1 ′ , s 2 ′ , n ) ∧ Ψ ( x s , s 3 ′ , s 2 ′ ) ∧ DeletesTo ( y s , x s , n ) ∧ x ∈ x s ∧ n < θ ( y s ) ) ⇒ retract ( s 2 ′ , x ) = Err .
Figure 3. Proof rules for proving equivalence modulo a retraction specification θ \theta θ . The primed versions (e.g., acc ′ \operatorname{acc}' acc ′ ) refer to functions in the retractable fold, and non-primed function names refer to those in the insertion-only one. DeletesTo ( y s , x s , n ) \operatorname{DeletesTo}(ys,xs,n) DeletesTo ( y s , x s , n ) means x s xs x s is obtained from y s ys y s by deleting exactly n n n elements.
Lifting consistency (Block A). The first obligation relates the original state to the enriched state maintained by the retractable computation. Since the retractable version may store additional information, the two states need not be equal; instead, Block A shows that they agree observationally on insertion-only histories.
Retraction consistency (Block B). The second obligation relates the actual mixed execution of the retractable fold to its own normalized execution. In particular, Block B proof rules establish that each successful retraction has the same semantic effect as removing a matching insertion from the history, thereby keeping the mixed execution aligned with its normalized counterpart.
Finally, Block R defines the reachability predicate used to characterize states produced by insertion-only histories. These rules collectively reduce the global equivalence theorem to local proof obligations over relational invariants that can be discharged by SMT solvers. We next explain each group of verification conditions in more detail. Throughout, s s s , I \mathcal{I} I , a c c \mathit{acc} acc , and e v a l \mathit{eval} eval refer to the original fold, while s ′ s' s ′ , I ′ \mathcal{I}' I ′ , a c c ′ \mathit{acc}' acc ′ , and e v a l ′ \mathit{eval}' eval ′ refer to the retractable fold.
Reachability rules. These are the simplest rules and define the auxiliary predicate Reach ( x s , s ′ ) \operatorname{Reach}(xs,s') Reach ( x s , s ′ ) , which holds when s ′ s' s ′ is the state of the retractable fold that is obtained through insertions x s xs x s starting at the initial state. Rule R1 establishes the base case, and R2 captures the inductive step.
Lifting consistency rules. Next, Block A rules deal with insertion-only executions and aim to show that the retractable fold preserves the observable behavior of the original fold even when it contains a richer internal summary. Rule A1 establishes this agreement at the initial states, and Rule A2 shows that the agreement is preserved when both folds process the same insertion.
Retraction consistency rules. Block B defines a crucial relational invariant Ψ \Psi Ψ that connects two different states of the same retractable aggregation: one reached through an insertion-only update sequence, and the other reached by executing a mixed sequence with both insertions and deletions. In particular, Ψ ( x s , s 1 ′ , s 2 ′ ) \Psi(xs,s'_1,s'_2) Ψ ( x s , s 1 ′ , s 2 ′ ) means that s 1 ′ s'_1 s 1 ′ is the state obtained by running the retractable fold on the insertion-only history x s xs x s , while s 2 ′ s'_2 s 2 ′ is the state obtained from some mixed update sequence whose net effect after normalization is exactly x s xs x s . Thus, Ψ \Psi Ψ says that the mixed state s 2 ′ s'_2 s 2 ′ has the same semantic meaning as the insertion-only state s 1 ′ s'_1 s 1 ′ , and its purpose is to show that the semantics of retraction is consistent with the net effect of the normalized (insertion-only) update sequence.
Concretely, Rule B1 initializes Ψ \Psi Ψ at the empty history, and Rule B2 shows that the relation is preserved when both sides process the same insertion. The key rule is B3, which captures the effect of a successful retraction. Suppose s 1 ′ s'_1 s 1 ′ is a reachable reference state for an insertion-only history x s xs x s , and suppose the actual mixed state s 2 ′ s'_2 s 2 ′ is related to s 1 ′ s'_1 s 1 ′ by Ψ ( x s , s 1 ′ , s 2 ′ ) \Psi(xs,s'_1,s'_2) Ψ ( x s , s 1 ′ , s 2 ′ ) . If x x x is one of the elements in x s xs x s and retracting x x x from s 2 ′ s'_2 s 2 ′ succeeds, then the rule compares the result against the reference state for the history obtained by removing one occurrence of x x x . That is, letting y s = remove ( x s , x ) ys = \operatorname{remove}(xs,x) y s = remove ( x s , x ) and letting s 3 ′ s'_3 s 3 ′ be a reachable state for y s ys y s , the post-retraction state retract ( s 2 ′ , x ) \operatorname{retract}(s'_2,x) retract ( s 2 ′ , x ) must be related to s 3 ′ s'_3 s 3 ′ by Ψ ( y s , s 3 ′ , retract ( s 2 ′ , x ) ) \Psi(ys,s'_3,\operatorname{retract}(s'_2,x)) Ψ ( y s , s 3 ′ , retract ( s 2 ′ , x )) . Thus, retracting x x x from the mixed sequence corresponds exactly to removing one matching occurrence of x x x from the insertion-only history. Finally, rule B4 turns this state relation into observational agreement: whenever two retractable states are related by Ψ \Psi Ψ , they must produce the same output under eval ′ \operatorname{eval}' eval ′ .
Quantitative retraction support rules. The previous rules show that retractions preserve the observable semantics of the computation if they succeed. But they do not enforce that a retraction actually succeeds when it is required to do so. This is the role of Block C. Its rules track the gap between two executions: the full insertion-only execution that determines the deletion budget promised by θ \theta θ , and the actual mixed execution in which some deletions may already have occurred. To capture this gap, Block C introduces an indexed relation Ω ( s 1 ′ , s 2 ′ , n ) \Omega(s'_1, s'_2, n) Ω ( s 1 ′ , s 2 ′ , n ) , where s 1 ′ s'_1 s 1 ′ is the full insertion-only state, s 2 ′ s'_2 s 2 ′ is the current mixed-execution state, and n n n counts the deletions already used. Rule C1 initializes the relation with distance 0; Rule C2 preserves this distance across common insertions; and Rule C3 increments it after a successful retraction.
The key rule is C4, which turns this bookkeeping into the actual proof obligation. The rule compares two insertion-only histories associated with the same mixed execution. The first, written y s ys y s , is the full insertion history: it contains every element that has been inserted so far, ignoring deletions. The second, x s xs x s , is the effective history that remains after canceling the deletions that have already occurred. Thus, for a mixed update sequence ω \omega ω , we can think of y s ys y s as ζ ( ω + ) \zeta(\omega^+) ζ ( ω + ) and x s xs x s as Effect(ω \omega ω ). Thus, in this rule, the premise Reach(y s ys y s , s 1 ′ s'_1 s 1 ′ ) identifies the reference state for the full insertion history, which is the history to which θ \theta θ applies. The premise Ω ( s 1 ′ , s 2 ′ , n ) \Omega(s'_1, s'_2, n) Ω ( s 1 ′ , s 2 ′ , n ) says that the current state s 2 ′ s'_2 s 2 ′ has already used n n n deletions relative to that full history. The premise Ψ ( x s , s 3 ′ , s 2 ′ ) \Psi(xs, s'_3, s'_2) Ψ ( x s , s 3 ′ , s 2 ′ ) identifies the effective history represented by the current mixed state, where s 3 ′ s'_3 s 3 ′ is the corresponding insertion-only reference state. The premise DeletesTo(y s ys y s , x s xs x s , n n n ) connects the two histories by saying that x s xs x s is obtained from y s ys y s by deleting exactly n n n elements. With these facts in place, Rule C4 states the promised capability condition. If n < θ ( y s ) n < \theta(ys) n < θ ( y s ) , then the deletion budget promised for the full insertion history y s ys y s has not yet been exhausted. Therefore, every element x x x that still remains in x s xs x s must be retractable from the current state s 2 ′ s'_2 s 2 ′ .
Putting the pieces together. Figure 4 illustrates how these proof rules combine to establish equivalence between P \mathcal{P} P and R \mathcal{R} R modulo θ \theta θ . Consider a valid mixed update sequence ω \omega ω . Let x s = Effect ( ω ) xs = \operatorname{Effect}(\omega) x s = Effect ( ω ) be the normalized history, and let n = ∣ ω − ∣ n = |\omega^-| n = ∣ ω − ∣ be the number of retractions in ω \omega ω . The proof associates four states with this execution. State s 1 ′ s'_1 s 1 ′ is obtained by running R \mathcal{R} R on the full insertion history ζ ( ω + ) \zeta(\omega^+) ζ ( ω + ) , where all insertions are replayed and retractions are ignored. State s 2 ′ s'_2 s 2 ′ is obtained by running R \mathcal{R} R on the actual mixed sequence ω \omega ω . State s 3 ′ s'_3 s 3 ′ is obtained by running R \mathcal{R} R on the effective history x s xs x s . Finally, state s s s is obtained by running the original fold P \mathcal{P} P on the same history x s xs x s .
Figure 4. Proof methodology
The right side of the figure establishes observational agreement. Block B relates the actual mixed state s 2 ′ s'_2 s 2 ′ to the reference retractable state s 3 ′ s'_3 s 3 ′ for the effective history x s xs x s . This shows that, whenever retractions succeed, executing the mixed sequence has the same effect as the normalized history. Block A then relates s 3 ′ s'_3 s 3 ′ to the original fold state s s s , showing that the retractable fold agrees with the original fold on insertion-only histories. Hence, Blocks A and B together establish that, as long as ω \omega ω does not produce Err, the output of R \mathcal{R} R on ω \omega ω agrees with the output of P \mathcal{P} P on x s xs x s .
The left side of the figure establishes that the execution does not get stuck before the promised deletion budget is exhausted. Block C relates the full-history state s 1 ′ s'_1 s 1 ′ to the actual mixed state s 2 ′ s'_2 s 2 ′ using the indexed relation Ω ( s 1 ′ , s 2 ′ , n ) \Omega(s'_1, s'_2, n) Ω ( s 1 ′ , s 2 ′ , n ) , where n n n records how many retractions have already been used. Since Block B also identifies x s xs x s as the effective history represented by s 2 ′ s'_2 s 2 ′ , Block C can express the capability obligation imposed by θ \theta θ : if n < θ ( ζ ( ω + ) ) n < \theta(\zeta(\omega^+)) n < θ ( ζ ( ω + )) , then every element still present in x s xs x s must be retractable from s 2 ′ s'_2 s 2 ′ . Thus, every update sequence whose prefixes remain within the budget specified by θ \theta θ avoids Err and agrees observationally with the original fold.
Example 4.1. Consider the auction example from § 2 . Here, Figure 1 is the insertion-only fold, and Figure 2 b is its retractable version under Policy B. To prove equivalence modulo the specification θ B \theta_B θ B from § 2 , we can instantiate the invariants in Figure 3 as follows, abbreviating the state as ( m , h , c ) (m,h,c) ( m , h , c ) for maxBid, highBidCount, and maxBidCount respectively:
Reach ( x s , ( m , h , c ) ) ≔ h = # { y ∈ x s ∣ y > 1000 } ∧ ( x s = [ ] ⇒ m = − ∞ ∧ c = 0 ) ∧ ( x s ≠ [ ] ⇒ m = max ( x s ) ∧ c = count ( x s , m ) ∧ c > 0 ) \operatorname{Reach}(x_s,(m,h,c)) \coloneqq h = \#\{y \in x_s \mid y > 1000\} \land(x_s = [] \Rightarrow m = -\infty\land c = 0)
\land(x_s \ne[] \Rightarrow m = \max(x_s) \land c = \operatorname{count}(x_s,m) \land c > 0) Reach ( x s , ( m , h , c )) : = h = # { y ∈ x s ∣ y > 1000 } ∧ ( x s = [ ] ⇒ m = − ∞ ∧ c = 0 ) ∧ ( x s = [ ] ⇒ m = max ( x s ) ∧ c = count ( x s , m ) ∧ c > 0 )
Ψ ( x s , ( m 1 , h 1 , c 1 ) , ( m 2 , h 2 , c 2 ) ) ≔ m 1 = m 2 ∧ h 1 = h 2 ∧ c 1 = c 2 , \Psi(x_s,(m_1,h_1,c_1),(m_2,h_2,c_2)) \coloneqq m_1 = m_2 \land h_1 = h_2 \land c_1 = c_2, Ψ ( x s , ( m 1 , h 1 , c 1 ) , ( m 2 , h 2 , c 2 )) : = m 1 = m 2 ∧ h 1 = h 2 ∧ c 1 = c 2 ,
Ω ( ( m 1 , h 1 , c 1 ) , ( m 2 , h 2 , c 2 ) , n ) ≔ n ≥ 0 ∧ m 1 = m 2 ∧ c 2 + n ≥ c 1 ∧ ( n = 0 ⇒ h 1 = h 2 ) . \Omega((m_1,h_1,c_1),(m_2,h_2,c_2),n) \coloneqq n \ge0 \land m_1 = m_2 \land c_2 + n \ge c_1 \land(n = 0 \Rightarrow h_1 = h_2). Ω (( m 1 , h 1 , c 1 ) , ( m 2 , h 2 , c 2 ) , n ) : = n ≥ 0 ∧ m 1 = m 2 ∧ c 2 + n ≥ c 1 ∧ ( n = 0 ⇒ h 1 = h 2 ) .
Here, Reach gives the insertion-only meaning of the state: h h h counts high bids, m m m is the maximum bid, and c c c is its multiplicity. Since these fields fully characterize the retractable summary, Ψ \Psi Ψ simply requires componentwise equality. Ω \Omega Ω tracks the remaining support under Policy B. If the full insertion history contains c 1 c_1 c 1 copies of the maximum and the current mixed state contains c 2 c_2 c 2 copies, then after n n n retractions we must have c 2 + n ≥ c 1 c_2 + n \ge c_1 c 2 + n ≥ c 1 . This ensures that, as long as the deletion budget is not exhausted, the current state still retains enough copies to support another retraction.
Theorem 4.2 (Soundness and Relative Completeness). Let P \mathcal{P} P be an insertion-only fold, R \mathcal{R} R a retractable fold, and θ \theta θ a retraction specification. Then, we have:
(1) Soundness: If there exist predicates Ψ \Psi Ψ , Ω \Omega Ω , Reach satisfying proof rules in Figure 3 , then P ≡ θ R \mathcal{P} \equiv_\theta\mathcal{R} P ≡ θ R .
(2) Relative completeness: Conversely, if P ≡ θ R \mathcal{P} \equiv_\theta\mathcal{R} P ≡ θ R and the assumptions stated in Appendix § 15 .§ 1 hold, then there exist predicates Ψ \Psi Ψ , Ω \Omega Ω , and Reach such that all proof rules in Figure 3 are valid.1
Relative completeness relies on three assumptions. First, the assertion language must be expressive enough to describe the required invariants. Second, states with the same observable output must remain indistinguishable under future insertions. Third, executions with the same surviving input sequence must agree on their outputs and on which future deletions they can support. A more formal treatment of these assumptions can be found in Appendix § 15 .§ 1 .
Synthesis Algorithm
Given a standard fold P \mathcal{P} P and retraction requirement θ \theta θ , the synthesis goal is to generate a retractable fold R \mathcal{R} R such that P ≡ θ R \mathcal{P} \equiv_\theta\mathcal{R} P ≡ θ R . To solve this problem, the algorithm must first determine how to enrich the internal state so that the synthesis task is realizable. It must then update the existing insertion logic to correctly maintain the lifted state and, finally, construct the retraction function from scratch. However, the core challenge is that these three tasks are fundamentally entangled: The very existence of a correct retraction function depends on what the lifted state is, yet deciding how to enrich the state in turn depends on the semantics of retraction.
Our method mitigates this apparent circularity by first reasoning about lifting invariants rather than directly searching over retractable folds . A lifting invariant specifies what information the enriched state is intended to contain, relative to the original fold state and the insertion history, without yet specifying how the accumulator maintains that information. This allows the algorithm to rule out semantically inadequate lifts before attempting to synthesize either the lifted insertion logic or the retraction function. Once a suitable lift has been identified and realized, the remaining challenge is to synthesize a retraction function whose behavior is only indirectly specified by the retraction requirement and is often case-dependent. Our algorithm addresses this challenge by symbolically partitioning the deletion space into semantically distinct cases, deriving local synthesis constraints for each case, and solving the resulting subproblems separately. The rest of this section first presents the top-level synthesis algorithm and then explains its key subprocedures.
Algorithm 1 RewindFold
Require: Standard fold P = ( I , a c c , e v a l ) \mathcal{P} = (\mathcal{I}, \mathit{acc}, \mathit{eval}) P = ( I , acc , eval ) , requirement θ ( x s ) \theta(x_s) θ ( x s ) Ensure: Retractable fold R = ( I ⋆ , a c c ⋆ , r e t r a c t ⋆ , e v a l ⋆ ) \mathcal{R} = (\mathcal{I}^{\star}, \mathit{acc}^{\star}, \mathit{retract}^{\star}, \mathit{eval}^{\star}) R = ( I ⋆ , acc ⋆ , retract ⋆ , eval ⋆ ) or ⊥ \perp ⊥
Σ ← InferLiftSpec ( P , θ ) \Sigma\gets\operatorname{InferLiftSpec}(\mathcal{P}, \theta) Σ ← InferLiftSpec ( P , θ )
for each ( τ a u x , χ , U , G ) ∈ Σ (\tau_{\mathit{aux}}, \chi, U, G) \in\Sigma ( τ aux , χ , U , G ) ∈ Σ in increasing order do
( o k , I ⋆ , a c c ⋆ , e v a l ⋆ ) ← Lift ( P , τ a u x , χ , U ) (\mathit{ok}, \mathcal{I}^{\star}, \mathit{acc}^{\star}, \mathit{eval}^{\star}) \gets\operatorname{Lift}(\mathcal{P}, \tau_{\mathit{aux}}, \chi, U) ( ok , I ⋆ , acc ⋆ , eval ⋆ ) ← Lift ( P , τ aux , χ , U )
if ¬ o k \neg\mathit{ok} ¬ ok then continue
r e t r a c t ⋆ ← SynRetract ( ( I ⋆ , a c c ⋆ , e v a l ⋆ ) , P , G , θ ) \mathit{retract}^{\star} \gets\operatorname{SynRetract}((\mathcal{I}^{\star}, \mathit{acc}^{\star}, \mathit{eval}^{\star}), \mathcal{P}, G, \theta) retract ⋆ ← SynRetract (( I ⋆ , acc ⋆ , eval ⋆ ) , P , G , θ )
if r e t r a c t ⋆ ≠ ⊥ \mathit{retract}^{\star} \ne\perp retract ⋆ = ⊥ then return ( I ⋆ , a c c ⋆ , r e t r a c t ⋆ , e v a l ⋆ ) (\mathcal{I}^{\star}, \mathit{acc}^{\star}, \mathit{retract}^{\star}, \mathit{eval}^{\star}) ( I ⋆ , acc ⋆ , retract ⋆ , eval ⋆ )
return ⊥ \perp ⊥
Synthesis Overview
Algorithm 1 gives an overview of our synthesis procedure. The top-level search ranges over candidate lifting specifications , consisting of a lifted state together with a lifting invariant that describes the intended semantics. Concretely, a lifting specification has the form:
{ ( s : τ , a : τ a u x ) ∣ χ ( s , a , x s ) } \{(s : \tau,\ a : \tau_{\mathit{aux}}) \mid\chi(s,a,x_s)\} {( s : τ , a : τ aux ) ∣ χ ( s , a , x s )}
where τ \tau τ is the type of the original state, τ a u x \tau_{\mathit{aux}} τ aux is the type of the auxiliary state, and the logical qualifier χ ( s , a , x s ) \chi(s,a,x_s) χ ( s , a , x s ) is the lifting invariant that describes how the auxiliary component a a a relates to the input sequence x s x_s x s and existing state s s s .
Example 5.1. Consider an insertion-only fold that stores only the current minimum s = min ( x s ) s = \min(x_s) s = min ( x s ) . A possible lift augments the state s s s with a counter c c c tracking the multiplicity of the minimum:
{ ( s : Int , c : Int ) ∣ ( s = ∞ ⇒ c = 0 ) ∧ ( s ≠ ∞ ⇒ c = count ( x s , s ) ) } \{(s : \operatorname{Int},\ c : \operatorname{Int}) \mid(s = \infty\Rightarrow c = 0) \land(s \ne\infty\Rightarrow c = \operatorname{count}(x_s,s))\} {( s : Int , c : Int ) ∣ ( s = ∞ ⇒ c = 0 ) ∧ ( s = ∞ ⇒ c = count ( x s , s ))}
Here, the qualifier specifies that the auxiliary state c c c corresponds to the multiplicity of s s s in x s x_s x s .
Lifting invariants play a key role in the synthesis procedure by enabling decomposition . As reflected in the proof rules of Figure 3 , the lifted accumulator and the retraction function are tightly coupled: insertions must maintain the auxiliary state in a form that retractions can use, while the required retractions constrain what auxiliary information must be retained. Lifting invariants help break this circularity by fixing the meaning of the auxiliary state before fixing its implementation. Once such an invariant is chosen, the search can be stratified into two stages. First, the algorithm synthesizes lifted insertion logic and verifies that it maintains the chosen representation. Only after this stage succeeds does the algorithm synthesize the retraction function over that representation. Thus, retraction synthesis can proceed assuming a well-maintained lifted state; if it fails, the failure is not due to an incorrectly maintained accumulator.
Algorithm 1 implements this idea. Concretely, it first invokes InferLiftSpec \operatorname{InferLiftSpec} InferLiftSpec , which generates candidate lifting specifications by enumerating auxiliary-state types and candidate invariants from a fixed component library. These candidates are explored in increasing order of space cost, reflecting the goal of retaining only the auxiliary information needed to support the required retractions. This avoids implementations that carry unnecessary state or incur avoidable per-update cost.
For each candidate lifting specification, the algorithm next invokes Lift \operatorname{Lift} Lift , which attempts to synthesize a lifted initializer I ⋆ \mathcal{I}^{\star} I ⋆ , accumulator a c c ⋆ \mathit{acc}^{\star} acc ⋆ , and evaluation function e v a l ⋆ \mathit{eval}^{\star} eval ⋆ . This step realizes the semantic description given by χ \chi χ as concrete insertion logic over the enriched state. If the proposed auxiliary state cannot be maintained, the candidate specification is discarded.
Next, the algorithm invokes SynRetract \operatorname{SynRetract} SynRetract to synthesize a retraction function for the realized lift. This is the most subtle part of the algorithm for two reasons. First, the retraction requirement
Algorithm 2 InferLiftSpec
Require: insertion-only fold P = ( I , acc , eval ) \mathcal{P} = (\mathcal{I}, \operatorname{acc}, \operatorname{eval}) P = ( I , acc , eval ) ; retraction spec θ \theta θ ; library L ( τ a ) L(\tau_a) L ( τ a ) with ( τ a u x , E τ ) (\tau_{\mathrm{aux}}, \mathcal{E}_\tau) ( τ aux , E τ )
Ensure: Candidate liftings Σ \Sigma Σ
Σ ← ∅ \Sigma\gets\varnothing Σ ← ∅ ; D ← { x s ∈ A ∗ ∣ ∣ x s ∣ ≤ K , x s drawn from test inputs X } \mathcal{D} \gets\{x_s \in A^* \mid|x_s| \le K,\ x_s\text{ drawn from test inputs }\mathcal{X}\} D ← { x s ∈ A ∗ ∣ ∣ x s ∣ ≤ K , x s drawn from test inputs X }
for ( τ a u x , E ) ∈ L ( τ a ) (\tau_{\mathrm{aux}}, \mathcal{E}) \in L(\tau_a) ( τ aux , E ) ∈ L ( τ a ) do
for χ ∈ GenQualifierCandidates ( τ a u x , acc , E ) \chi\in\operatorname{GenQualifierCandidates}(\tau_{\mathrm{aux}}, \operatorname{acc}, \mathcal{E}) χ ∈ GenQualifierCandidates ( τ aux , acc , E ) do
( ok , U ) ← CheckFunctionalConsistency ( P , χ , D , X ) (\operatorname{ok}, U) \gets\operatorname{CheckFunctionalConsistency}(\mathcal{P}, \chi, \mathcal{D}, \mathcal{X}) ( ok , U ) ← CheckFunctionalConsistency ( P , χ , D , X )
( bad , G ) ← CheckRetractability ( P , θ , χ , D , X ) (\operatorname{bad}, G) \gets\operatorname{CheckRetractability}(\mathcal{P}, \theta, \chi, \mathcal{D}, \mathcal{X}) ( bad , G ) ← CheckRetractability ( P , θ , χ , D , X )
if ¬ ok ∨ bad \neg\operatorname{ok} \lor\operatorname{bad} ¬ ok ∨ bad then continue
Σ ← Σ ∪ { ( τ a u x , χ , U , G ) } \Sigma\gets\Sigma\cup\{(\tau_{\mathrm{aux}}, \chi, U, G)\} Σ ← Σ ∪ {( τ aux , χ , U , G )}
return Sort ( Σ ) \operatorname{Sort}(\Sigma) Sort ( Σ )
T : : = int ∣ τ a ∣ ( T , … , T ) ∣ sized_list_of ( T , n ) ∣ sized_sorted_bag_of ( T , n ) ∣ sized_map_of ( T , T ) ∣ list_of ( T ) ∣ sorted_bag_of ( T ) ∣ map_of ( T , T ) \begin{aligned}
T &::= \operatorname{int} \mid\tau_a \mid(T,\ldots,T)\\
&\quad\mid\operatorname{sized\_list\_of}(T,n) \mid\operatorname{sized\_sorted\_bag\_of}(T,n) \mid\operatorname{sized\_map\_of}(T,T)\\
&\quad\mid\operatorname{list\_of}(T) \mid\operatorname{sorted\_bag\_of}(T) \mid\operatorname{map\_of}(T,T)
\end{aligned} T ::= int ∣ τ a ∣ ( T , … , T ) ∣ sized_list_of ( T , n ) ∣ sized_sorted_bag_of ( T , n ) ∣ sized_map_of ( T , T ) ∣ list_of ( T ) ∣ sorted_bag_of ( T ) ∣ map_of ( T , T )
E : : = s ∣ x s ∣ length ( x s ) ∣ count ( x s , E ) ∣ nth_max ( x s , E ) ∣ nth_min ( x s , E ) ∣ multiplicity_map ( E ) ∣ sorted_bag ( E , Order , n ) ∣ ( E , … , E ) ∣ tuple_select ( E , i ) \begin{aligned}
E &::= s \mid x_s \mid\operatorname{length}(x_s) \mid\operatorname{count}(x_s,E) \mid\operatorname{nth\_max}(x_s,E) \mid\operatorname{nth\_min}(x_s,E)\\
&\quad\mid\operatorname{multiplicity\_map}(E) \mid\operatorname{sorted\_bag}(E,\operatorname{Order},n) \mid(E,\ldots,E) \mid\operatorname{tuple\_select}(E,i)
\end{aligned} E ::= s ∣ x s ∣ length ( x s ) ∣ count ( x s , E ) ∣ nth_max ( x s , E ) ∣ nth_min ( x s , E ) ∣ multiplicity_map ( E ) ∣ sorted_bag ( E , Order , n ) ∣ ( E , … , E ) ∣ tuple_select ( E , i )
χ : : = a = E ( s , x s ) \chi::= a = E(s,x_s) χ ::= a = E ( s , x s )
n , i ∈ { 0 , 1 , 2 , … } n,i \in\{0,1,2,\ldots\} n , i ∈ { 0 , 1 , 2 , … } \quad Order ∈ { Arrival , ReverseArrival , Ascending , Descending } \operatorname{Order} \in\{\operatorname{Arrival},\operatorname{ReverseArrival},\operatorname{Ascending},\operatorname{Descending}\} Order ∈ { Arrival , ReverseArrival , Ascending , Descending }
indirect: it says when the fold must avoid Err, but does not by itself specify the local behavior of each call to retract. Second, the retraction function is partial and often case-based, as different deletion scenarios require different updates to the lifted state. As we explain in § 5.4 , SynRetract derives local constraints for these cases and solves them using symbolic partitioning.
Theorem 5.2 (Soundness). Let P = ( I , acc , eval ) \mathcal{P} = (\mathcal{I}, \operatorname{acc}, \operatorname{eval}) P = ( I , acc , eval ) be an insertion-only fold and let θ \theta θ be a retraction specification. If Algorithm 1 returns a retractable fold R = ( I ′ , acc ′ , retract ′ , eval ′ ) \mathcal{R} = (\mathcal{I}', \operatorname{acc}', \operatorname{retract}', \operatorname{eval}') R = ( I ′ , acc ′ , retract ′ , eval ′ ) , then P ≡ θ R \mathcal{P} \equiv_\theta\mathcal{R} P ≡ θ R .
Theorem 5.3 (Completeness). Let P \mathcal{P} P be an insertion-only fold and θ \theta θ a retraction specification. Assuming (i) there exists a correct retractable fold whose lifting is expressible in the DSL from Figure 5 , and (ii) the oracle, fairness, and search-space assumptions of Appendix § 16 hold, then Algorithm 1 will find a retractable fold R \mathcal{R} R such that P ≡ θ R \mathcal{P} \equiv_\theta\mathcal{R} P ≡ θ R .
Figure 5. DSL used by InferLiftSpec. The nonterminals T T T and E E E range over candidate auxiliary-state types and summary expressions, respectively. τ a \tau_a τ a denotes the input element type of the source fold.
Intuitively, completeness requires a sufficiently expressive search space, complete solvers, and a fair search strategy that eventually considers every candidate. Of course, in practice, solver limitations and timeouts may prevent Rewind from finding a solution even when one exists in its search space.
Inference of Lift Specifications
The first subprocedure in Algorithm 1 is InferLiftSpec, defined in Algorithm 2. This procedure searches for candidate ways of enriching the fold state, using the DSL in Figure 5 . This DSL separates the shape of the auxiliary state from its meaning . Concretely, the type language T T T consists of possible auxiliary representations, such as scalars, tuples, fixed-size lists, sorted bags, and maps. In contrast, the expression grammar E E E describes generic summaries of the insertion history that are useful for deletion, such as cardinalities, multiplicities, and order statistics. To organize the search, we group these types and expressions into a component library L L L . Each entry ( τ a u x , E ) (\tau_{\mathrm{aux}}, \mathcal{E}) ( τ aux , E ) pairs an auxiliary-state
Algorithm 3 CheckFunctionalConsistency
Require: P = ( I , a c c , e v a l ) P = (I, \mathit{acc}, \mathit{eval}) P = ( I , acc , eval ) ; qualifier χ ( s , a , x s ) \chi(s, a, x_s) χ ( s , a , x s ) ; histories D \mathcal{D} D ; test inputs X \mathcal{X} X
Ensure: ( o k , U ) (\mathit{ok}, U) ( ok , U ) with U : ( S × A a u x × A ) ⇀ A a u x U : (S \times A_{\mathrm{aux}} \times A) \rightharpoonup A_{\mathrm{aux}} U : ( S × A aux × A ) ⇀ A aux
R ← { ( ( s , a , x ) , a ′ ) ∣ x s ∈ D , s = [ P ] x s , χ ( s , a , x s ) , x ∈ X , s ′ = a c c ( s , x ) , χ ( s ′ , a ′ , x s + + [ x ] ) } R \gets\left\{((s, a, x), a') \mid x_s \in\mathcal{D},\ s = [P]_{x_s},\ \chi(s, a, x_s),\ x \in\mathcal{X},\ s' = \mathit{acc}(s, x),\ \chi(s', a', x_s{+}{+}[x])\right\} R ← { (( s , a , x ) , a ′ ) ∣ x s ∈ D , s = [ P ] x s , χ ( s , a , x s ) , x ∈ X , s ′ = acc ( s , x ) , χ ( s ′ , a ′ , x s + + [ x ]) }
o k ← ( ∀ k , v 1 , v 2 . ( k , v 1 ) ∈ R ∧ ( k , v 2 ) ∈ R ⇒ v 1 = v 2 ) \mathit{ok} \gets\left(\forall k, v_1, v_2.\ (k, v_1) \in R \land(k, v_2) \in R \Rightarrow v_1 = v_2\right) ok ← ( ∀ k , v 1 , v 2 . ( k , v 1 ) ∈ R ∧ ( k , v 2 ) ∈ R ⇒ v 1 = v 2 )
return ( o k , { k ↦ v ∣ ( k , v ) ∈ R } ) (\mathit{ok},\ \{k \mapsto v \mid(k, v) \in R\}) ( ok , { k ↦ v ∣ ( k , v ) ∈ R })
type with summary expressions that produce values of that type. Choosing an expression E ∈ E E \in\mathcal{E} E ∈ E then gives a lifting invariant χ ( s , a , x s ) ≡ a = E ( s , x s ) \chi(s, a, x_s) \equiv a = E(s, x_s) χ ( s , a , x s ) ≡ a = E ( s , x s ) , which gives meaning to the auxiliary state.
At a high level, InferLiftSpec generates candidate specifications by enumerating auxiliary types in increasing order of space cost. Here, space cost is a simple structural measure on auxiliary-state types, with scalar summaries ranked below tuples and bounded containers, and larger containers ranked after smaller ones. For each such type, the procedure then instantiates candidate qualifiers from the DSL and subjects them to two filters: functional consistency (Algorithm 3) and retractability (Algorithm 4). The first check asks whether the proposed auxiliary state can actually be maintained by insertion; the second one asks whether it preserves enough information to support the retractions required by θ \theta θ . Candidates that fail either check are discarded.
In more detail, the functional consistency check (Algorithm 3) asks whether a candidate qualifier χ \chi χ is realizable , which requires that the next auxiliary value be determined by the current lifted state and the next input alone. That is, whenever two histories x s 1 x_{s_1} x s 1 and x s 2 x_{s_2} x s 2 induce the same lifted state ( s , a ) (s, a) ( s , a ) under χ \chi χ , appending the same input x x x must also induce the same next auxiliary value a ′ a' a ′ . Otherwise, the desired auxiliary state depends on information about the past that is not present in ( s , a ) (s, a) ( s , a ) , and no deterministic lifted accumulator a c c ⋆ \mathit{acc}^{\star} acc ⋆ can maintain χ \chi χ .
Algorithm 3 checks this condition on bounded histories D \mathcal{D} D by constructing a relation R ⊆ ( S × A a u x × A ) × A a u x R \subseteq(S \times A_{\mathrm{aux}} \times A) \times A_{\mathrm{aux}} R ⊆ ( S × A aux × A ) × A aux . For each sampled history x s x_s x s and input x x x , it computes s = [ P ] x s s = [P]_{x_s} s = [ P ] x s , chooses an auxiliary value a a a satisfying χ ( s , a , x s ) \chi(s, a, x_s) χ ( s , a , x s ) , and records the value a ′ a' a ′ required by χ ( a c c ( s , x ) , a ′ , x s + + [ x ] ) \chi(\mathit{acc}(s, x), a', x_s{+}{+}[x]) χ ( acc ( s , x ) , a ′ , x s + + [ x ]) . The relation R R R must be functional, meaning that every triple ( s , a , x ) (s, a, x) ( s , a , x ) may map to at most one a ′ a' a ′ . If R R R is non-functional, the candidate χ \chi χ is rejected. Otherwise, R R R defines a partial update map U : ( s , a , x ) ↦ a ′ U : (s, a, x) \mapsto a' U : ( s , a , x ) ↦ a ′ that provides examples for synthesizing the lifted accumulator.
Example 5.4. Consider again Example 5.1, where the insertion-only fold stores s = min ( x s ) s = \min(xs) s = min ( x s ) . Suppose we use the candidate lift invariant
χ ≡ ( a = count ( x s , nth_min ( x s , 2 ) ) ) \chi\equiv(a = \operatorname{count}(xs,\operatorname{nth\_min}(xs,2))) χ ≡ ( a = count ( x s , nth_min ( x s , 2 )))
where nth_min ( x s , 2 ) \operatorname{nth\_min}(xs,2) nth_min ( x s , 2 ) denotes the second smallest distinct element of x s xs x s . In other words, χ \chi χ says that the auxiliary state corresponds to the multiplicity of the second minimum. This candidate fails the functional consistency check. To see why, consider the two histories x s 1 = [ 1 , 2 ] x_{s_1} = [1, 2] x s 1 = [ 1 , 2 ] and x s 2 = [ 1 , 3 ] x_{s_2} = [1, 3] x s 2 = [ 1 , 3 ] . In both cases, the minimum is s = 1 s = 1 s = 1 , and the second minimum occurs exactly once, so both histories yield the same lifted state ( s , a ) = ( 1 , 1 ) (s, a) = (1, 1) ( s , a ) = ( 1 , 1 ) . Now suppose we append the same new input x = 2 x = 2 x = 2 . For x s 1 + + [ 2 ] = [ 1 , 2 , 2 ] x_{s_1}{+}{+}[2] = [1, 2, 2] x s 1 + + [ 2 ] = [ 1 , 2 , 2 ] , the second minimum remains 2 2 2 and now occurs twice, so a ′ = 2 a' = 2 a ′ = 2 . By contrast, for x s 2 + + [ 2 ] = [ 1 , 3 , 2 ] x_{s_2}{+}{+}[2] = [1, 3, 2] x s 2 + + [ 2 ] = [ 1 , 3 , 2 ] , the second minimum becomes 2 2 2 and occurs only once, so a ′ = 1 a' = 1 a ′ = 1 . Thus the same current lifted state ( s , a ) (s, a) ( s , a ) and the same next input x x x lead to two different next auxiliary values. Hence, χ \chi χ does not induce a well-defined update function.
In addition to checking functional consistency, InferLiftSpec performs a retractability check (Algorithm 4) to determine whether the candidate invariant χ \chi χ retains enough information to support the retraction requirement θ \theta θ . The key idea is simple: histories that lead to the same lifted Algorithm 4 CheckRetractability
Require: P = ( I , acc , eval ) \mathcal{P} = (I, \operatorname{acc}, \operatorname{eval}) P = ( I , acc , eval ) ; spec θ \theta θ ; qualifier χ ( s , a , x s ) \chi(s, a, x_s) χ ( s , a , x s ) ; histories D \mathcal{D} D ; test inputs X \mathcal{X} X
Ensure: ( b a d , G ) (\mathit{bad}, G) ( bad , G ) where b a d \mathit{bad} bad iff χ \chi χ is insufficient for θ \theta θ on D \mathcal{D} D , and each V ∈ G V \in G V ∈ G is a set of histories whose reached states should be related by Ψ \Psi Ψ
M ← { ( y s , x s ) ∣ y s ∈ D , Allowed θ ( y s , x s ) } M \gets\{(y_s, x_s) \mid y_s \in\mathcal{D},\ \operatorname{Allowed}_{\theta}(y_s, x_s)\} M ← {( y s , x s ) ∣ y s ∈ D , Allowed θ ( y s , x s )}
E 0 ← { ( ( x s , x s ) , ( x s ′ , x s ′ ) ) ∣ x s , x s ′ ∈ D , ∃ ( s , a ) . χ ( s , a , x s ) ∧ χ ( s , a , x s ′ ) ∧ s = [ P ] x s = [ P ] x s ′ } E_0 \gets\{((x_s, x_s), (x_s', x_s')) \mid x_s, x_s' \in\mathcal{D},\ \exists(s,a).\ \chi(s,a,x_s) \land\chi(s,a,x_s') \land s = [\mathcal{P}]_{x_s} = [\mathcal{P}]_{x_s'}\} E 0 ← {(( x s , x s ) , ( x s ′ , x s ′ )) ∣ x s , x s ′ ∈ D , ∃ ( s , a ) . χ ( s , a , x s ) ∧ χ ( s , a , x s ′ ) ∧ s = [ P ] x s = [ P ] x s ′ }
Acc ( E ) ← { ( ( y s + + [ x ] , x s + + [ x ] ) , ( y s ′ + + [ x ] , x s ′ + + [ x ] ) ) ∣ ( ( y s , x s ) , ( y s ′ , x s ′ ) ) ∈ E , x ∈ X } \operatorname{Acc}(E) \gets\{((y_s{+\!\!+}[x], x_s{+\!\!+}[x]), (y_s'{+\!\!+}[x], x_s'{+\!\!+}[x])) \mid((y_s,x_s),(y_s',x_s')) \in E,\ x \in\mathcal{X}\} Acc ( E ) ← {(( y s + + [ x ] , x s + + [ x ]) , ( y s ′ + + [ x ] , x s ′ + + [ x ])) ∣ (( y s , x s ) , ( y s ′ , x s ′ )) ∈ E , x ∈ X }
Ret ( E ) ← { ( ( y s , remove ( x s , x ) ) , ( y s ′ , remove ( x s ′ , x ) ) ) ∣ ( ( y s , x s ) , ( y s ′ , x s ′ ) ) ∈ E , x ∈ x s ∩ x s ′ } \operatorname{Ret}(E) \gets\{((y_s,\operatorname{remove}(x_s,x)), (y_s',\operatorname{remove}(x_s',x))) \mid((y_s,x_s),(y_s',x_s')) \in E,\ x \in x_s \cap x_s'\} Ret ( E ) ← {(( y s , remove ( x s , x )) , ( y s ′ , remove ( x s ′ , x ))) ∣ (( y s , x s ) , ( y s ′ , x s ′ )) ∈ E , x ∈ x s ∩ x s ′ }
E ⋆ ← EquivClosure ( E 0 ∪ Acc ( E ⋆ ) ∪ Ret ( E ⋆ ) ) ∩ ( M × M ) E^\star\gets\operatorname{EquivClosure}(E_0 \cup\operatorname{Acc}(E^\star) \cup\operatorname{Ret}(E^\star)) \cap(M \times M) E ⋆ ← EquivClosure ( E 0 ∪ Acc ( E ⋆ ) ∪ Ret ( E ⋆ )) ∩ ( M × M )
G ← { { x s ′ ∣ ∃ y s ′ . ( ( y s , x s ) , ( y s ′ , x s ′ ) ) ∈ E ⋆ } ∣ ( y s , x s ) ∈ M } G \gets\{\{x_s' \mid\exists y_s'.\ ((y_s,x_s),(y_s',x_s')) \in E^\star\} \mid(y_s,x_s) \in M\} G ← {{ x s ′ ∣ ∃ y s ′ . (( y s , x s ) , ( y s ′ , x s ′ )) ∈ E ⋆ } ∣ ( y s , x s ) ∈ M }
b a d ← ∃ ( y s , x s ) , ( y s ′ , x s ′ ) ∈ E ⋆ . [ P ] x s ≠ [ P ] x s ′ \mathit{bad} \gets\exists(y_s,x_s),(y_s',x_s') \in E^\star.\ [\mathcal{P}]_{x_s} \ne[\mathcal{P}]_{x_s'} bad ← ∃ ( y s , x s ) , ( y s ′ , x s ′ ) ∈ E ⋆ . [ P ] x s = [ P ] x s ′
return ( b a d , G ) (\mathit{bad}, G) ( bad , G )
state will be indistinguishable to the synthesized retraction function. Thus, if such histories can later require different retraction behavior, the candidate lift is insufficient.
To formalize this idea, Algorithm 4 considers pairs ( y s , x s ) (y_s, x_s) ( y s , x s ) , where y s y_s y s is a full insertion history and x s x_s x s is an effective history that can be obtained from y s y_s y s by deleting at most θ ( y s ) \theta(y_s) θ ( y s ) elements. These are the history pairs for which the retraction specification requires correct behavior:
M ≜ { ( y s , x s ) ∣ y s ∈ D , Allowed θ ( y s , x s ) } , M \triangleq\{(y_s, x_s) \mid y_s \in\mathcal{D},\ \operatorname{Allowed}_{\theta}(y_s, x_s)\}, M ≜ {( y s , x s ) ∣ y s ∈ D , Allowed θ ( y s , x s )} ,
where:
Allowed θ ( y s , x s ) ≜ ∃ z s . ∣ z s ∣ ≤ θ ( y s ) ∧ y s = merge ( x s , z s ) , \operatorname{Allowed}_{\theta}(y_s, x_s) \triangleq\exists z_s.\ |z_s| \le\theta(y_s) \land y_s = \operatorname{merge}(x_s, z_s), Allowed θ ( y s , x s ) ≜ ∃ z s . ∣ z s ∣ ≤ θ ( y s ) ∧ y s = merge ( x s , z s ) ,
Here, merge ( x s , z s ) \operatorname{merge}(x_s, z_s) merge ( x s , z s ) denotes an order-preserving interleaving, so Allowed θ ( y s , x s ) \operatorname{Allowed}_{\theta}(y_s, x_s) Allowed θ ( y s , x s ) says that x s x_s x s can result from y s y_s y s after deleting an allowed number of elements.
The algorithm then tracks how indistinguishability propagates through future updates. Line 2 initializes a seed relation E 0 E_0 E 0 with insertion-only histories that χ \chi χ maps to the same lifted state. Lines 3 and 4 define two closure operations. The operator Acc \operatorname{Acc} Acc says that related history pairs remain related after appending the same new element, while Ret \operatorname{Ret} Ret says that they remain related after deleting the same element from both effective histories. Line 5 computes the least fixed point E ⋆ E^\star E ⋆ containing E 0 E_0 E 0 and closed under these two operations, restricted to the allowed pairs in M M M . Thus, E ⋆ E^\star E ⋆ captures the history pairs that the chosen lift forces the retractable implementation to treat identically.
Finally, lines 6–8 use E ⋆ E^\star E ⋆ to check whether this forced identification is safe. If E ⋆ E^\star E ⋆ relates two pairs whose effective histories x s x_s x s and x s ′ x_s' x s ′ produce different outputs under the original fold, then the lift has lost information needed to satisfy θ \theta θ , and the candidate χ \chi χ is rejected. The algorithm also returns groups G G G of histories that remain indistinguishable under the lift; these groups are later used when synthesizing the retraction function in § 5.4 .
Example 5.5. Consider again Example 5.1 that computes s = min ( x s ) s = \min(x_s) s = min ( x s ) . Suppose θ \theta θ requires deleting one element, and suppose the candidate lift augments the minimum with its multiplicity:
χ ( s , a , x s ) ≜ a = count ( x s , s ) , \chi(s, a, x_s) \triangleq a = \operatorname{count}(x_s, s), χ ( s , a , x s ) ≜ a = count ( x s , s ) ,
This candidate fails the retractability check. To see why, observe that the two histories x s 1 = [ 1 , 2 ] x_{s_1} = [1, 2] x s 1 = [ 1 , 2 ] and x s 2 = [ 1 , 3 ] x_{s_2} = [1, 3] x s 2 = [ 1 , 3 ] both yield the same lifted state ( s , a ) = ( 1 , 1 ) (s, a) = (1, 1) ( s , a ) = ( 1 , 1 ) . So ( ( x s 1 , x s 1 ) , ( x s 2 , x s 2 ) ) ∈ E 0 ((x_{s_1}, x_{s_1}), (x_{s_2}, x_{s_2})) \in E_0 (( x s 1 , x s 1 ) , ( x s 2 , x s 2 )) ∈ E 0 , and applying Ret \operatorname{Ret} Ret with x = 1 x = 1 x = 1 propagates this into ( ( x s 1 , [ 2 ] ) , ( x s 2 , [ 3 ] ) ) ∈ E ⋆ ((x_{s_1}, [2]), (x_{s_2}, [3])) \in E^\star (( x s 1 , [ 2 ]) , ( x s 2 , [ 3 ])) ∈ E ⋆ . However, min ( [ 2 ] ) = 2 \min([2]) = 2 min ([ 2 ]) = 2 and min ( [ 3 ] ) = 3 \min([3]) = 3 min ([ 3 ]) = 3 , meaning that the same lifted state ( 1 , 1 ) (1, 1) ( 1 , 1 ) is compatible with two different outcomes. This shows that χ \chi χ does not preserve enough information to support a correct retract function.
Algorithm 5 Lift
Require: P = ( I , acc , eval ) P = (I, \operatorname{acc}, \operatorname{eval}) P = ( I , acc , eval ) ; T = { ( s : τ , a : τ a u x ) ∣ χ ( s , a , x s ) } T = \{(s : \tau, a : \tau_{\mathrm{aux}}) \mid\chi(s, a, x_s)\} T = {( s : τ , a : τ aux ) ∣ χ ( s , a , x s )} ; update map U U U ; grammar G \mathcal{G} G
Ensure: ( t r u e , I ⋆ , acc ⋆ , eval ⋆ ) (\mathrm{true}, I^\star, \operatorname{acc}^\star, \operatorname{eval}^\star) ( true , I ⋆ , acc ⋆ , eval ⋆ ) or ( f a l s e , ⊥ ) (\mathrm{false}, \bot) ( false , ⊥ )
while true do
UpdAux ← PBE ( G , U ) \operatorname{UpdAux} \leftarrow\operatorname{PBE}(\mathcal{G}, U) UpdAux ← PBE ( G , U )
if UpdAux = ⊥ \operatorname{UpdAux} = \bot UpdAux = ⊥ then return ( f a l s e , ⊥ ) (\mathrm{false}, \bot) ( false , ⊥ )
let Bad ( s , a , x , x s ) ≔ χ ( s , a , x s ) ∧ Reach ( s , x s ) ∧ ¬ χ ( acc ( s , x ) , UpdAux ( s , a , x ) , x s + + [ x ] ) \operatorname{Bad}(s, a, x, x_s) \coloneqq\chi(s, a, x_s) \land\operatorname{Reach}(s, x_s) \land\neg\chi(\operatorname{acc}(s, x), \operatorname{UpdAux}(s, a, x), x_s {+\!\!+} [x]) Bad ( s , a , x , x s ) : = χ ( s , a , x s ) ∧ Reach ( s , x s ) ∧ ¬ χ ( acc ( s , x ) , UpdAux ( s , a , x ) , x s + + [ x ])
( s a t , s ^ , a ^ , x ^ , x ^ s , a ^ ′ ) ← SAT ( Bad ( s , a , x , x s ) ∧ χ ( acc ( s , x ) , a ′ , x s + + [ x ] ) ) (\mathrm{sat}, \hat{s}, \hat{a}, \hat{x}, \hat{x}_s, \hat{a}') \leftarrow\operatorname{SAT}(\operatorname{Bad}(s, a, x, x_s) \land\chi(\operatorname{acc}(s, x), a', x_s {+\!\!+} [x])) ( sat , s ^ , a ^ , x ^ , x ^ s , a ^ ′ ) ← SAT ( Bad ( s , a , x , x s ) ∧ χ ( acc ( s , x ) , a ′ , x s + + [ x ]))
if sat then U ← U ∪ { ( s ^ , a ^ , x ^ ) ↦ a ^ ′ } U \leftarrow U \cup\{(\hat{s}, \hat{a}, \hat{x}) \mapsto\hat{a}'\} U ← U ∪ {( s ^ , a ^ , x ^ ) ↦ a ^ ′ }
else let acc ⋆ ( ( s , a ) , x ) ≔ ( acc ( s , x ) , UpdAux ( s , a , x ) ) \operatorname{acc}^\star((s, a), x) \coloneqq(\operatorname{acc}(s, x), \operatorname{UpdAux}(s, a, x)) acc ⋆ (( s , a ) , x ) : = ( acc ( s , x ) , UpdAux ( s , a , x )) ; eval ⋆ ( s , a ) ≔ eval ( s ) \operatorname{eval}^\star(s, a) \coloneqq\operatorname{eval}(s) eval ⋆ ( s , a ) : = eval ( s ) ;
let I ⋆ ≔ ( I , a 0 ) I^\star\coloneqq(I, a_0) I ⋆ : = ( I , a 0 ) where ⊨ χ ( I , a 0 , [ ] ) \models\chi(I, a_0, []) ⊨ χ ( I , a 0 , [ ])
return ( t r u e , I ⋆ , acc ⋆ , eval ⋆ ) (\mathrm{true}, I^\star, \operatorname{acc}^\star, \operatorname{eval}^\star) ( true , I ⋆ , acc ⋆ , eval ⋆ )
Theorem 5.6 (Soundness of CheckRetractability). If CheckRetractability ( P , θ , χ , D , X ) \operatorname{CheckRetractability}(P, \theta, \chi, D, X) CheckRetractability ( P , θ , χ , D , X ) returns ( t r u e , _ ) (\mathrm{true}, \_) ( true , _ ) , then for every lifted fold that realizes χ \chi χ , no retract function satisfies the Block B and C rules of Figure 3 .
Discussion. The realizability checks performed by InferLiftSpec \operatorname{InferLiftSpec} InferLiftSpec play an important role in the overall algorithm. In particular, inference of the lifting specification is only the first step of the synthesis pipeline, and Algorithm 1 must still synthesize both a lifted accumulator and a retraction function. Without these checks, the algorithm could waste effort on lifts that are infeasible from the outset. By filtering such candidates early, our method focuses the later synthesis stages on promising lifts. However, these checks are sound but incomplete: any rejected candidate is infeasible, as stated in Theorem 5.6 , but some infeasible candidates may still pass InferLiftSpec \operatorname{InferLiftSpec} InferLiftSpec and be eliminated in later stages of the algorithm.
Synthesizing Lifted Accumulator
Given a candidate lifting specification { ( s : τ , a : τ a u x ) ∣ χ ( s , a , x s ) } \{(s : \tau, a : \tau_{\mathrm{aux}}) \mid\chi(s, a, x_s)\} {( s : τ , a : τ aux ) ∣ χ ( s , a , x s )} , the next step is to refine the accumulator so that it correctly updates the auxiliary state a a a . Fortunately, this task is relatively straightforward due to the groundwork laid by InferLiftSpec \operatorname{InferLiftSpec} InferLiftSpec . First, for each candidate type, we have access to an update map U U U consisting of input-output examples ( s , a , x ) ↦ a ′ (s, a, x) \mapsto a' ( s , a , x ) ↦ a ′ that specify how the auxiliary state should evolve. Second, the invariant χ \chi χ enables correctness checking by verifying that the synthesized accumulator preserves χ \chi χ inductively. Without this invariant, correctness of the lifted accumulator would have to be checked indirectly through the proof rules in Figure 3 , which only constrain the behavior needed for equivalence. By contrast, χ \chi χ gives a direct specification of the auxiliary state, allowing Lift \operatorname{Lift} Lift to verify that the synthesized insertion logic realizes the chosen representation before retraction synthesis begins.
Building on the examples and invariant produced by InferLiftSpec \operatorname{InferLiftSpec} InferLiftSpec , the Lift \operatorname{Lift} Lift procedure in Algorithm 5 synthesizes the lifted accumulator using a standard CEGIS loop. In each iteration, it invokes a PBE solver to synthesize a candidate update function UpdAux \operatorname{UpdAux} UpdAux from the examples in U U U . If synthesis fails, the procedure returns ⊥ \bot ⊥ ; otherwise, it checks whether UpdAux \operatorname{UpdAux} UpdAux preserves χ \chi χ . This is done by querying an SMT solver for a reachable ( s , a ) (s, a) ( s , a ) , input x x x , and sequence x s x_s x s such that χ ( s , a , x s ) \chi(s, a, x_s) χ ( s , a , x s ) holds but, after applying acc \operatorname{acc} acc and UpdAux \operatorname{UpdAux} UpdAux , the resulting state violates χ \chi χ . Any counterexample refines U U U with the new example ( s , a , x ) ↦ a ′ (s, a, x) \mapsto a' ( s , a , x ) ↦ a ′ , and the loop repeats. If no counterexample exists, the lifted accumulator is constructed by pairing acc \operatorname{acc} acc with UpdAux \operatorname{UpdAux} UpdAux (line 7).
Figure 6. Auxiliary procedure used in SynRetract.
Retraction Synthesis with Lifted State
The most involved component of our synthesis algorithm is the retraction procedure itself. At a high level, retraction synthesis must address three key difficulties that make this step substantially more challenging than lifting the insertion logic:
Lack of local specifications. The correctness requirements for retraction, given by Block B in Figure 3 , do not directly constrain the behavior of retract. Instead, they specify retraction behavior indirectly through the relational invariant Ψ \Psi Ψ . Furthermore, the lifting invariant χ \chi χ cannot play this role because it describes the meaning of a lifted state on insertion-only histories, whereas retraction correctness requires relating two lifted states across a deletion step. Thus, the algorithm must first infer a suitable Ψ \Psi Ψ relation to constrain synthesis of retract. But this introduces a circular dependency between the synthesis of retract and the inference of the Ψ \Psi Ψ relation used to specify its behavior.
Unknown domain. A retraction function need not succeed on every input x x x ; returning Err is permitted whenever θ \theta θ does not require supporting the deletion of x x x . However, the key difficulty is that θ \theta θ is a function from insertion histories to a numerical deletion budget, so it does not directly translate into a precondition on individual calls to Retract. Thus, the valid domain of the retraction function is not known in advance and must be inferred during synthesis.
Piecewise implementation. The third difficulty is that retraction is inherently piecewise: the correct behavior of retract often depends on how the deleted element interacts with the current summary (e.g., whether it is an extremum or other copies remain). These distinctions lead to different update rules, causing the retraction logic to involve intricate case analysis.
Our synthesis procedure addresses these difficulties by combining two key ideas. First, it infers a candidate relational invariant Ψ \Psi Ψ that can be used to impose meaningful constraints on retraction logic by utilizing (a) the examples returned by InferLiftSpec and (b) proof rules that do not require access to the implementation of retract. Second, once such a Ψ \Psi Ψ is given, it performs symbolic execution on bounded input lists to derive a symbolic partition of the input space under that relation. This partition serves two purposes. First, it identifies regions of the input space on which retraction is required to succeed, thereby addressing the unknown domain problem. Second, it separates retraction scenarios that are semantically distinct under Ψ \Psi Ψ , allowing the algorithm to decompose the synthesis problem into semantically different cases.
Algorithm overview. Algorithm 6 implements retraction synthesis as a nested search. The outer loop searches for a candidate relation Ψ \Psi Ψ that can serve as a local specification for retraction, whereas the inner loop then attempts to synthesize a retract function that satisfies this specification. A subtle point here is that, while the relational invariant Ψ \Psi Ψ used in Figure 3 is history-indexed (i.e., Ψ ( x s , s 1 , s 2 ) \Psi(x_s,s_1,s_2) Ψ ( x s , s 1 , s 2 ) relates two states relative to a normalized history x s x_s x s ), the synthesis procedure uses a state-based version of this relation. This is because retract is invoked only with the current lifted state and the element being deleted and does not have access to the normalized history x s x_s x s . Thus, the constraints used to synthesize retract must ultimately be expressed as local conditions over states. In the rest of this section, we write Ψ ( s 1 , s 2 ) \Psi(s_1,s_2) Ψ ( s 1 , s 2 ) for this state-based relation.
Figure 6 (PDF p. 16). Auxiliary procedure used in SynRetract.
Algorithm 6 SynRetract
Require: Lifted fold R = ( I ⋆ , acc ⋆ , eval ⋆ ) \mathcal{R} = (I^\star, \operatorname{acc}^\star, \operatorname{eval}^\star) R = ( I ⋆ , acc ⋆ , eval ⋆ ) , original fold P \mathcal{P} P , set of compatible histories G G G , spec θ \theta θ
Ensure: retract or ⊥ \perp ⊥
B ← ∅ \mathcal{B} \gets\varnothing B ← ∅
while true do
find Ψ ∉ B \Psi\notin\mathcal{B} Ψ ∈ / B such that ValidCand ( G , R , Ψ ) \operatorname{ValidCand}(G, \mathcal{R}, \Psi) ValidCand ( G , R , Ψ )
if Ψ = ⊥ \Psi= \perp Ψ =⊥ then return ⊥ \perp ⊥
B ← B ∪ { Ψ } \mathcal{B} \gets\mathcal{B} \cup\{\Psi\} B ← B ∪ { Ψ } ; E ← ∅ \mathcal{E} \gets\varnothing E ← ∅ ; Π ← [ ] \Pi\gets[\ ] Π ← [ ]
while true do
r ← SolveSyGuS ( Π ) r \gets\operatorname{SolveSyGuS}(\Pi) r ← SolveSyGuS ( Π )
if r = ⊥ r = \perp r =⊥ then break
( ok , cex ) ← Verify ( P , θ , R , r , Ψ ) (\operatorname{ok},\operatorname{cex}) \gets\operatorname{Verify}(\mathcal{P}, \theta, \mathcal{R}, r, \Psi) ( ok , cex ) ← Verify ( P , θ , R , r , Ψ )
if ok then return r r r
let cex = ( x s , s ⋆ , x ⋆ ) \operatorname{cex} = (x_s, s^\star, x^\star) cex = ( x s , s ⋆ , x ⋆ ) and ℓ ← ∣ x s ∣ \ell\gets|x_s| ℓ ← ∣ x s ∣
if ℓ ∉ E \ell\notin\mathcal{E} ℓ ∈ / E then
Π ← UpdateSymbolicPartitions ( Π , R , ℓ , Ψ ) \Pi\gets\operatorname{UpdateSymbolicPartitions}(\Pi, \mathcal{R}, \ell, \Psi) Π ← UpdateSymbolicPartitions ( Π , R , ℓ , Ψ ) ; E ← E ∪ { ℓ } \mathcal{E} \gets\mathcal{E} \cup\{\ell\} E ← E ∪ { ℓ }
else
let ( Γ j , ψ j ) ∈ Π (\Gamma_j, \psi_j) \in\Pi ( Γ j , ψ j ) ∈ Π such that ( s ⋆ , x ⋆ ) ⊨ Γ j (s^\star, x^\star) \models\Gamma_j ( s ⋆ , x ⋆ ) ⊨ Γ j
Π [ Γ j ] ← ψ j ∧ Ψ ( [ R ] remove ( x s , x ⋆ ) , retract ( s ⋆ , x ⋆ ) ) \Pi[\Gamma_j] \gets\psi_j \land\Psi([\mathcal{R}]_{\operatorname{remove}(x_s,x^\star)}, \operatorname{retract}(s^\star,x^\star)) Π [ Γ j ] ← ψ j ∧ Ψ ([ R ] remove ( x s , x ⋆ ) , retract ( s ⋆ , x ⋆ ))
The outer loop filters candidate relations using the ValidCand condition in Figure 6. Intuitively, ValidCand checks that a candidate relation is compatible with both the examples produced by InferLiftSpec and the proof obligations from Block B. Here, the examples correspond to the compatible-history sets G G G returned by InferLiftSpec: histories in the same set arise from the same retraction scenario, so the lifted states they reach should be related by any reasonable Ψ \Psi Ψ . Additionally, any suitable candidate for Ψ \Psi Ψ must satisfy the basic closure conditions from Block B in Figure 3 : it must (1) hold at the initial state, (2) be preserved by insertion steps, and (3) imply observational agreement.
Example 5.7. Continuing Example 5.1, consider the lifted state ( m , c ) (m,c) ( m , c ) and eval ⋆ ( m , c ) = m \operatorname{eval}^\star(m,c) = m eval ⋆ ( m , c ) = m . At this stage, SynRetract searches for a binary relation Ψ \Psi Ψ satisfying ValidCand ( G , R , Ψ ) \operatorname{ValidCand}(G,\mathcal{R},\Psi) ValidCand ( G , R , Ψ ) . Suppose G G G contains a set V V V with histories [ 1 , 2 , 1 ] [1, 2, 1] [ 1 , 2 , 1 ] and [ 2 , 1 , 1 ] [2, 1, 1] [ 2 , 1 , 1 ] . Since both histories yield the same lifted state ( 1 , 2 ) (1,2) ( 1 , 2 ) , any valid candidate for Ψ \Psi Ψ should satisfy Ψ ( ( 1 , 2 ) , ( 1 , 2 ) ) \Psi((1,2),(1,2)) Ψ (( 1 , 2 ) , ( 1 , 2 )) . Now consider the candidate
Ψ b a d ( ( m 1 , c 1 ) , ( m 2 , c 2 ) ) ≡ c 1 = c 2 . \Psi_{\mathrm{bad}}((m_1,c_1),(m_2,c_2)) \equiv c_1 = c_2. Ψ bad (( m 1 , c 1 ) , ( m 2 , c 2 )) ≡ c 1 = c 2 .
This candidate is too weak. Although it is compatible with the evidence from V V V , it also relates ( 1 , 2 ) (1,2) ( 1 , 2 ) and ( 2 , 2 ) (2,2) ( 2 , 2 ) , which violates the observational agreement requirement in ValidCand, since eval ⋆ ( 1 , 2 ) = 1 ≠ 2 = eval ⋆ ( 2 , 2 ) \operatorname{eval}^\star(1,2) = 1 \ne2 = \operatorname{eval}^\star(2,2) eval ⋆ ( 1 , 2 ) = 1 = 2 = eval ⋆ ( 2 , 2 ) . By contrast,
Ψ ( ( m 1 , c 1 ) , ( m 2 , c 2 ) ) ≡ ( m 1 = m 2 ) ∧ ( c 1 = c 2 ) (1) \Psi((m_1,c_1),(m_2,c_2)) \equiv(m_1 = m_2) \land(c_1 = c_2) \tag*{(1)} Ψ (( m 1 , c 1 ) , ( m 2 , c 2 )) ≡ ( m 1 = m 2 ) ∧ ( c 1 = c 2 ) (1)
is compatible with the histories in V V V and also satisfies the remaining conditions of ValidCand. Hence, it is a suitable candidate for the outer loop.
Once a candidate Ψ \Psi Ψ is fixed, the inner loop searches for a retraction function consistent with that relation. This search is organized around a symbolic partition
Π = { ( γ i ( s , x ) , ψ i ( s , x ) ) ∣ i = 1 , … , n } \Pi= \{(\gamma_i(s,x), \psi_i(s,x)) \mid i = 1,\ldots,n\} Π = {( γ i ( s , x ) , ψ i ( s , x )) ∣ i = 1 , … , n }
where each guard γ i ( s , x ) \gamma_i(s,x) γ i ( s , x ) describes a region of the input space, and each formula ψ i ( s , x ) \psi_i(s,x) ψ i ( s , x ) gives the local specification that retract must satisfy on that region. Intuitively, such a partition decomposes synthesis into independent subproblems, each corresponding to a distinct deletion scenario.
Example 5.8. Continuing Example 5.1, consider the lifted state ( m , c ) (m,c) ( m , c ) , where m m m is the minimum and c c c is its multiplicity. The deletion space splits according to how the deleted element relates to the current minimum. In particular, symbolic partitioning yields the following two feasible regions:
γ k e e p ( ( m , c ) , x ) ≡ x > m ψ k e e p ( ( m , c ) , x ) ≡ retract ( ( m , c ) , x ) = ( m , c ) γ d e c ( ( m , c ) , x ) ≡ x = m ∧ c > 1 ψ d e c ( ( m , c ) , x ) ≡ retract ( ( m , c ) , x ) = ( m , c − 1 ) \begin{aligned}
\gamma_{\mathrm{keep}}((m,c),x) &\equiv x > m
&\qquad\psi_{\mathrm{keep}}((m,c),x) &\equiv\operatorname{retract}((m,c),x) = (m,c) \\
\gamma_{\mathrm{dec}}((m,c),x) &\equiv x = m \land c > 1
&\qquad\psi_{\mathrm{dec}}((m,c),x) &\equiv\operatorname{retract}((m,c),x) = (m,c-1)
\end{aligned} γ keep (( m , c ) , x ) γ dec (( m , c ) , x ) ≡ x > m ≡ x = m ∧ c > 1 ψ keep (( m , c ) , x ) ψ dec (( m , c ) , x ) ≡ retract (( m , c ) , x ) = ( m , c ) ≡ retract (( m , c ) , x ) = ( m , c − 1 ) The first region corresponds to deleting a non-minimum element, which leaves the lifted state unchanged. The second corresponds to deleting one copy of the minimum while another copy remains, which decrements the multiplicity. The remaining case, x = m ∧ c = 1 x = m \land c = 1 x = m ∧ c = 1 , is excluded: after deleting the unique minimum, the new minimum cannot be determined from ( m , c ) (m,c) ( m , c ) alone, so retract may return Err on that input.
With this partition representation, Algorithm 6 proceeds by alternating between synthesis and verification. It starts from an empty partition and invokes SolveSyGuS(Π \Pi Π ) to synthesize a candidate retraction satisfying the current local specifications (line 7). If full verification at line 9 succeeds, the algorithm terminates successfully. Otherwise, verification produces a counterexample ( x s , s ∗ , x ∗ ) (xs,s^*,x^*) ( x s , s ∗ , x ∗ ) , where x s xs x s is an insertion-only history, s ∗ s^* s ∗ is the current lifted state, and x ∗ x^* x ∗ is the deleted element.
The algorithm uses such counterexamples in two different ways, depending on the nature of the counterexample. If sequence x s xs x s has a length ℓ \ell ℓ that is larger than the ones previously explored, the counterexample could reveal a new class of retraction scenarios, so the algorithm invokes UpdateSymbolicPartitions (explained later) to refine the current partition. By contrast, if ℓ \ell ℓ has already been explored, then the symbolic execution phase has already exposed the relevant case structure at that length. Hence, failure indicates not that a new branch is missing, but that the local specification is too weak. In this case, if ( γ j , ψ j ) (\gamma_j,\psi_j) ( γ j , ψ j ) is the matched partition element, the algorithm replaces ψ j \psi_j ψ j with the stronger specification:
ψ j ∧ Ψ ( [ R ] r e m o v e ( x s , x ∗ ) , retract ( s ∗ , x ∗ ) ) . \psi_j \land\Psi([R]_{\mathrm{remove}}(xs,x^*),\operatorname{retract}(s^*,x^*)). ψ j ∧ Ψ ([ R ] remove ( x s , x ∗ ) , retract ( s ∗ , x ∗ )) .
That is, the local specification of this branch is strengthened to require Ψ \Psi Ψ -agreement between the synthesized post-retraction state and the ground truth state.
Symbolic partitioning. A key step in the synthesis process is Algorithm 7, which derives local retraction specifications through symbolic execution on a list of size ℓ \ell ℓ . To do so, the algorithm first introduces fresh symbols x 1 , … , x ℓ x_1,\ldots,x_\ell x 1 , … , x ℓ to represent each inserted element (line 1) and deleting them one by one. For each potential deletion, it re-constructs a “what-if” scenario: What would the accumulator state have been had a particular element x i x_i x i never been added? This is captured by the formula β i ( x s , s ′ ) \beta_i(xs,s') β i ( x s , s ′ ) at line 3, which serves as the “ground truth” reference for validation.
The inner loop (lines 4–12) then explores the overall execution of P \mathcal{P} P to reason about the relationship between x i x_i x i and the other elements. Specifically, it performs path-by-path symbolic execution to obtain a condition Φ i j \Phi_{ij} Φ ij that describes when a particular control-flow path is taken and a corresponding symbolic output state o i j o_{ij} o ij . We then ask: “What should happen if x i x_i x i were retracted from this resulting state? ” To answer this, the algorithm constructs the formula:
φ i j ( s , x , x s ) ≜ Φ i j ( x s ) ∧ ( s = o i j ( x s ) ) ∧ ( x = x i ) \varphi_{ij}(s,x,xs) \triangleq\Phi_{ij}(xs) \land(s = o_{ij}(xs)) \land(x = x_i) φ ij ( s , x , x s ) ≜ Φ ij ( x s ) ∧ ( s = o ij ( x s )) ∧ ( x = x i )
representing the situation where the accumulator ends in state s s s after processing x s xs x s , and the element chosen for deletion is x i x_i x i . The algorithm then conjoins φ i j \varphi_{ij} φ ij with the reference formula β i ( x s , s ′ ) \beta_i(xs,s') β i ( x s , s ′ ) and eliminates the history variables x s xs x s . This yields two artifacts: a guard γ i j ( s , x ) \gamma_{ij}(s,x) γ ij ( s , x ) , which identifies Algorithm 7 UpdateSymbolicPartitions
Require: Partition Π \Pi Π , lifted fold P = ( I , acc , eval ) \mathcal{P} = (I, \operatorname{acc}, \operatorname{eval}) P = ( I , acc , eval ) , length ℓ \ell ℓ , relation Ψ \Psi Ψ Ensure: Updated partition Π \Pi Π
1: let x s = [ x 1 , … , x ℓ ] \mathbf{x}_{\mathrm{s}} = [x_1, \ldots, x_\ell] x s = [ x 1 , … , x ℓ ] be fresh symbols 2: for each i ∈ { 1 , … , ℓ } i \in\{1, \ldots, \ell\} i ∈ { 1 , … , ℓ } do 3: β i ( x s , s ′ ) ← SymExec ( P , remove ( x s , x i ) ) \beta_i(\mathbf{x}_{\mathrm{s}},s') \gets\operatorname{SymExec}(\mathcal{P},\operatorname{remove}(\mathbf{x}_{\mathrm{s}},x_i)) β i ( x s , s ′ ) ← SymExec ( P , remove ( x s , x i )) 4: for each ( Φ i j ( x s ) , o i j ( x s ) ) ∈ SymExecByPath ( P , x s ) (\Phi_{ij}(\mathbf{x}_{\mathrm{s}}),o_{ij}(\mathbf{x}_{\mathrm{s}})) \in\operatorname{SymExecByPath}(\mathcal{P},\mathbf{x}_{\mathrm{s}}) ( Φ ij ( x s ) , o ij ( x s )) ∈ SymExecByPath ( P , x s ) do 5: φ i j ( s , x , x s ) ← ( s = o i j ( x s ) ∧ ( x = x i ) ∧ Φ i j ( x s ) ) \varphi_{ij}(s,x,\mathbf{x}_{\mathrm{s}}) \gets(s=o_{ij}(\mathbf{x}_{\mathrm{s}})\land(x=x_i)\land\Phi_{ij}(\mathbf{x}_{\mathrm{s}})) φ ij ( s , x , x s ) ← ( s = o ij ( x s ) ∧ ( x = x i ) ∧ Φ ij ( x s )) 6: γ i j ( s , x ) ← QE x s ( φ i j ) \gamma_{ij}(s,x) \gets\operatorname{QE}_{\mathbf{x}_{\mathrm{s}}}(\varphi_{ij}) γ ij ( s , x ) ← QE x s ( φ ij ) 7: β i j ( s , x , s ′ ) ← QE x s ( φ i j ∧ β i ) \beta_{ij}(s,x,s') \gets\operatorname{QE}_{\mathbf{x}_{\mathrm{s}}}(\varphi_{ij}\land\beta_i) β ij ( s , x , s ′ ) ← QE x s ( φ ij ∧ β i ) 8: if UNSAT ( γ i j ( s , x ) ∧ ∃ s ′ . β i j ( s , x , s ′ ) ) \operatorname{UNSAT}(\gamma_{ij}(s,x)\land\exists s'.\,\beta_{ij}(s,x,s')) UNSAT ( γ ij ( s , x ) ∧ ∃ s ′ . β ij ( s , x , s ′ )) then continue 9: if SAT ( γ i j ( s , x ) ∧ ∃ s 1 ′ , s 2 ′ . β i j ( s , x , s 1 ′ ) ∧ β i j ( s , x , s 2 ′ ) ∧ ¬ Ψ ( s 1 ′ , s 2 ′ ) ) \operatorname{SAT}(\gamma_{ij}(s,x)\land\exists s'_1,s'_2.\,\beta_{ij}(s,x,s'_1)\land\beta_{ij}(s,x,s'_2)\land\neg\Psi(s'_1,s'_2)) SAT ( γ ij ( s , x ) ∧ ∃ s 1 ′ , s 2 ′ . β ij ( s , x , s 1 ′ ) ∧ β ij ( s , x , s 2 ′ ) ∧ ¬Ψ ( s 1 ′ , s 2 ′ )) then 10: Π ← InsertOrMerge ( Π , γ i j , ⊥ ) \Pi\gets\operatorname{InsertOrMerge}(\Pi,\gamma_{ij},\bot) Π ← InsertOrMerge ( Π , γ ij , ⊥ ) ; continue 11: ψ i j ( s , x ) ← ∃ s ′ . ( β i j ( s , x , s ′ ) ∧ Ψ ( s ′ , retract ( s , x ) ) ) \psi_{ij}(s,x)\gets\exists s'.(\beta_{ij}(s,x,s')\land\Psi(s',\operatorname{retract}(s,x))) ψ ij ( s , x ) ← ∃ s ′ . ( β ij ( s , x , s ′ ) ∧ Ψ ( s ′ , retract ( s , x ))) 12: Π ← InsertOrMerge ( Π , γ i j , ψ i j ) \Pi\gets\operatorname{InsertOrMerge}(\Pi,\gamma_{ij},\psi_{ij}) Π ← InsertOrMerge ( Π , γ ij , ψ ij ) 13: return Π \Pi Π
the region of the input space covered by this case, and a path-specific deletion relation β i j ( s , x , s ′ ) \beta_{ij}(s,x,s') β ij ( s , x , s ′ ) (line 7), which characterizes the reference post-deletion state s ′ s' s ′ associated with that case.
Next, the algorithm checks whether retraction is even possible for inputs satisfying γ i j \gamma_{ij} γ ij by testing the pair ( γ i j , β i j ) (\gamma_{ij},\beta_{ij}) ( γ ij , β ij ) for realizability and ambiguity:
Unrealizable case: Intuitively, an unrealizable case means that the current state s s s and element x x x describe a situation that could never arise from any input history. That is, there is simply no predecessor state s ′ s' s ′ that, when x x x were re-inserted, would yield s s s . Since retraction is meant to “undo” an actual insertion, the absence of any such valid predecessor means that no implementation could return a correct result. The satisfiability query at line 8 checks precisely this situation.
Ambiguous case: Intuitively, an ambiguous case means that the current state s s s and element x x x could correspond to different predecessors s 1 ′ , s 2 ′ s'_1,s'_2 s 1 ′ , s 2 ′ that are distinguishable under the relational invariant Ψ \Psi Ψ . In other words, retract ( s , x ) \operatorname{retract}(s,x) retract ( s , x ) is ambiguous because we don’t have sufficient information to decide whether the output should be s 1 ′ s'_1 s 1 ′ or s 2 ′ s'_2 s 2 ′ . Thus, under Ψ \Psi Ψ , the only retract implementation that can satisfy proof rule B3 from Figure 3 is one for which retraction must be undefined on this region. The satisfiability check at lines 9–10 detects precisely this condition.
Thus, if either of these checks fail, retract is undefined on γ i j \gamma_{ij} γ ij , and the guard falls outside of the domain of Π \Pi Π . Otherwise, the algorithm constructs a local specification ψ i j \psi_{ij} ψ ij for γ i j \gamma_{ij} γ ij at line 11 enforcing that retract ( s , x ) \operatorname{retract}(s,x) retract ( s , x ) must be consistent with the effect of deleting x x x under Ψ \Psi Ψ . Finally, the pair ( γ i j , ψ i j ) (\gamma_{ij},\psi_{ij}) ( γ ij , ψ ij ) is added to the partition Π \Pi Π , which accumulates all feasible retraction behaviors. Since different paths may yield overlapping guards after quantifier elimination, line 12 calls InsertOrMerge to merge overlapping cases by inserting holes (i.e., unknown expressions) to force generalization. A worked example showing this refinement process appears in Appendix § 13 .
Implementation
We implemented our approach in a prototype tool, Rewind,2 written in Python. Rewind takes as input a fold and a retraction specification, and outputs a retractable fold satisfying Definition 3.9 . The tool communicates with solvers via the SMT-LIB interface [6 ] and uses CVC5 [5 ] for validity and satisfiability queries, quantifier elimination, and the SyGuS queries used by Algorithms 5 and 6.
Synthesis. The implementation follows § 5 : candidate lifting specifications are generated from a small summary-component library and filtered using the checks from § 5.2 . For retraction synthesis, Rewind builds symbolic partitions via Algorithm 7 and uses CVC5’s SyGuS engine to synthesize code for the resulting local specifications. Importantly, the grammar supplied to the SyGuS engine excludes calls to the original fold. This prevents the synthesizer from simply removing an element from the retained history and rerunning the original fold. Instead, it must implement retraction using operations on the chosen data structures.
Validation and verification. Before full verification, Rewind performs lightweight randomized testing. Concretely, it samples update sequences satisfying the retraction specification and checks that required retractions avoid Err and that the final result matches the original fold on the normalized history. Candidates that pass are then checked against the verification conditions from Figure 3 . The verifier reuses synthesis artifacts when possible: the lifting invariant yields Reach, and the relation chosen by SynRetract yields Ψ \Psi Ψ . It then infers Ω \Omega Ω using a small portfolio of strategies, including template-based search, and checks all proof obligations with the CVC5 SMT solver.
Framework integration. Our approach is intended for data-processing frameworks that support incremental deletion through user-defined retraction handlers. In particular, our implementation targets Flink’s aggregation model: the synthesized state serves as the accumulator, and I I I , acc, retract, and eval correspond to createAccumulator, accumulate, retract, and getValue, respectively.
Evaluation
In this section, we describe the results of an evaluation designed to answer the following questions:
RQ1. How effective is our verifier at checking candidate retractable folds?
RQ2. How effective is our synthesizer at producing verified retractable folds?
RQ3. How efficient are the synthesized folds compared with recomputation?
RQ4. How do the main algorithmic components contribute to synthesis success?
Benchmarks and set-up. Our evaluation is based on real-world aggregate computations from Apache Flink, a widely used stream-processing framework for implementing stateful computations over continuously arriving data. In Flink, a user-defined aggregate maintains an accumulator that is incrementally updated via an accumulate method. Flink also supports incremental deletion of prior inputs if the user implements a suitable retract method. This makes Flink a natural source of benchmarks for our setting because its aggregate functions are stateful folds, and the corresponding retract methods capture real-world attempts to make such folds retractable.
Our benchmark suite contains 48 Flink aggregations from two sources. The first is prior work on synthesizing homomorphic folds for parallel aggregation [50 ] . Although Ink addresses a different problem, its benchmarks are relevant because they contain real-world Flink aggregation code with complex accumulator structures. We include all 23 Ink benchmarks and, since they do not provide retraction logic, we evaluate them under three generic specifications: bounded retraction with budgets k ∈ { 2 , 3 } k \in\{2, 3\} k ∈ { 2 , 3 } , modeling small numbers of late or corrected events, and unbounded retraction, modeling long-lived aggregates with arbitrary deletions. The second source consists of 25 additional GitHub Flink aggregations with manually written retraction logic, excluding those already in Ink. For each, we extracted the base fold and intended retraction specification. In total, this yields 94 evaluation scenarios, each pairing a source fold with a retraction specification.
Experimental setup. All experiments are conducted on a machine with two AMD EPYC 9654 CPUs and 256 GB of memory, running NixOS 25.11. We use a 15-minute timeout for each benchmark.
RQ1: Verification Effectiveness
Our first research question asks whether REWIND can verify candidate retractable folds. For this experiment, each benchmark consists of a source fold, a candidate retractable fold, and a retraction specification. The GitHub benchmarks already include candidate implementations, namely the manually written Flink retraction logic. For the Ink benchmarks, we generate candidate retractable implementations using GPT-5.5 with a standard self-repair loop [40 ] . That is, after each failed validation attempt, we provide structured feedback to the LLM and allow up to three repair rounds. Thus, these LLM-generated implementations provide plausible candidates for evaluating the verifier and also help assess the need for specialized synthesis methods for retractable folds.
Verification benchmarks. Table 1 summarizes statistics for the candidate verification benchmarks. Both sources contain a non-trivial number of incorrect retractable implementations: 16 of the 69 LLM-generated candidates and 5 of the 25 human-written GitHub implementations are buggy. Thus, retraction logic appears error-prone across both automatically generated and developer-written implementations. Since the LLM candidates were produced with up to three repair rounds, the remaining errors indicate that testing-based repair alone does not reliably catch semantic bugs in retraction logic, motivating techniques that reason directly about retraction semantics.
Table 1. Statistics for verification benchmarks
To better understand these failures, we manually inspected the incorrect candidates from both sources. In the human-written implementations, bugs often arise in boundary cases of the retraction semantics, such as incorrectly restoring auxiliary state after a deletion. In the LLM-generated candidates, we observed two recurring patterns. Some fail to guard retractions properly. For example, when the retained summary no longer contains enough information to update the state correctly, they return an updated state instead of Err, causing the result to diverge from full recomputation. Others update bounded auxiliary summaries incorrectly during retraction. For example, when the same value appears multiple times, they may remove the wrong copy from the retained list, so the auxiliary state no longer corresponds to the elements that remain after deletion.
For the verification experiment, we use the 73 source–target pairs that are semantically correct according to our manual analysis. For each pair, we ask whether our verifier can automatically prove equivalence with respect to the source fold and the corresponding retraction specification.
Baseline. Since there is no off-the-shelf verifier for checking equivalence in this setting, we also implement a baseline based on standard product-program encodings from the relational verification literature [7 , 8 , 9 , 49 , 53 ] . Our baseline simulates four executions: the retractable fold on the full insertion history, the retractable fold on the actual mixed sequence, the original fold on the normalized history, and the retractable fold on the same normalized history. It also maintains relevant ghost state, such as the number of retractions performed so far.
To make the comparison as strong as possible, we instrument the product program with assertions that mirror the key ingredients of our proof system. In particular, we ensure that (1) the mixed execution agrees with the normalized retractable execution, (2) the retraction budget is respected, (3) in-budget retractions do not return Err, and (4) the original and retractable folds must produce the same output on the normalized history. We then encode the resulting product program as a CHC-based safety problem and use a portfolio solver of Spacer [38 ] and Eldarica [30 ] to infer the invariants needed to prove these assertions. Thus, the baseline is given the same semantic ingredients as our verifier, but must prove the resulting relational safety property monolithically rather than using the structured proof obligations from Figure 3 .
Results. Our main verification results are presented in Table 2 . Overall, Rewind verifies 71 of the 73 correct candidates (97%) within the 15-minute timeout. It verifies all 20 correct human-written GitHub candidates and 51 of the 53 correct LLM-generated candidates. In contrast, the fully automated baseline is unable to verify any benchmark within the time limit: both Spacer and Eldarica time out while trying to infer the required invariants for the product-program encoding. To separate invariant inference from verification-condition discharge, we also evaluate an oracle-aided variant that is supplied with the invariants inferred by Rewind and checks the resulting VCs using CVC5. This semi-automated variant verifies 21 of the 73 correct candidates, but the remaining cases still time out during VC discharge. These results suggest that the difficulty is not only finding suitable relational invariants, but also organizing the resulting proof obligations in a form that is tractable for automated solvers. Thus, Rewind’s structured decomposition is important both for invariant discovery and for VC discharge.
Table 2. RQ1: Verification evaluation results. “Baseline Auto” uses Spacer/Eldarica to infer invariants automatically, while “Baseline +Inv” uses invariants inferred by Rewind and checks the resulting VCs with CVC5.
Failure analysis. In the two unverified cases, Rewind cannot establish that deletion produces a state consistent with recomputation. The solver either times out or returns unknown when asked to strengthen the relational invariant needed for this proof.
Result for RQ1: Rewind verifies 71 of the 73 correct candidate retractable folds. The fully automated product-program baseline times out on all benchmarks, and even an oracle-aided variant verifies only 21, showing the benefit of our structured proof obligations.
RQ2: Synthesis Effectiveness
Our second research question asks whether Rewind can synthesize a verified retractable computation from a source fold and retraction specification. Unlike in RQ1, the retractable fold is not provided as input; so, Rewind must discover the auxiliary state, synthesize the retraction logic, and prove equivalence to the source fold.
Table 3 reports end-to-end synthesis results for the 94 synthesis scenarios in our benchmark suite. We separate benchmarks according to a key source of difficulty: whether the source fold can be made retractable using its existing accumulator state or whether Rewind must synthesize additional auxiliary state. Overall, Rewind synthesizes and verifies a correct retractable computation for all 94 scenarios. At first glance, this may seem surprising in light of RQ1, where Rewind could not verify every correct source–target pair. The key difference is that RQ1 evaluates Rewind on externally produced implementations, whose state representations and update logic were not chosen with our proof strategy in mind. Such implementations may be correct, but still require invariants that our verifier does not infer automatically. In RQ2, by contrast, Rewind controls the construction of the retractable computation. In particular, it chooses the auxiliary state through a lifting invariant and synthesizes retraction logic using the same semantic decomposition used during verification. As a result, the synthesized implementations come with proof structure that makes them more amenable to automatic verification.
Table 3. RQ2: End-to-end synthesis results by lifting requirement.
As shown in Table 3 , instances that require lifting are unsurprisingly more challenging, as reflected in synthesis time. In such cases, Rewind must both identify suitable auxiliary state and correctly maintain it during both insertions and deletions. As a result, the synthesis task becomes significantly more difficult. Table 4 further characterizes the synthesized auxiliary state. The lifted solutions fall into three categories. Multiplicity maps are used when the computation needs to distinguish whether an extremal value, such as the current minimum or maximum, has other copies. Tuples and bounded sorted bags retain selected values ordered by magnitude, supporting order statistics such as top-k k k . Finally, a significant number of benchmarks require storing the entire sequence of currently retained inputs. This arises when the result depends on information about the relative order of elements, such as which value was seen first or most recently, that cannot be recovered from the original state after deletion.
Table 4. RQ2: Kinds of auxiliary state synthesized by Rewind.
Manual analysis. We manually inspected the benchmarks where Rewind retains the full input sequence and found one case where a bounded summary would suffice. In this case, synthesizing the retraction logic over the bounded summary times out because it requires complex conditional updates. Rewind therefore proceeds to a larger representation that retains the full sequence and admits simpler retraction logic. This illustrates that prioritizing smaller representations does not guarantee a space-minimal result.
Result for RQ2: Rewind synthesizes verified retractable computations for all 94 benchmarks. Even in the 49 cases where the original accumulator state is insufficient, Rewind discovers suitable auxiliary state and synthesizes the corresponding update logic.
RQ3: Efficiency of Synthesized Implementations
Our third research question asks how efficient the synthesized folds are compared with recomputation. We first analyze retraction complexity across all 94 synthesized folds, with particular attention to those retaining full input history. We then compare runtime against recomputation on the 45 scenarios supporting unrestricted retraction.
Retraction complexity. We manually analyzed the expected amortized complexity of all synthesized retraction functions and report the results in Table 5 , where n n n denotes the number of inputs currently present. As shown in the table, 85 of the 94 folds (90.4%) have sublinear retraction cost. Of particular interest are the 39 folds whose auxiliary state retains the entire sequence of inputs that have not been deleted. Of these, 30 (76.9%) support sublinear retraction, showing that retaining the input sequence
Table 5. Expected amortized complexity.
does not necessarily entail linear-time retraction. Sublinear retraction is also common among the 45 folds required to support arbitrary (unrestricted) deletions. Of these, 42 (93.3%) support O ( 1 ) O(1) O ( 1 ) or O ( log n ) O(\log n) O ( log n ) retraction, while only three require O ( n ) O(n) O ( n ) time.
Runtime evaluation. We next evaluate whether these complexity differences translate into lower update times. We consider all 45 scenarios supporting unrestricted retraction, which allows us to evaluate sustained workloads of insertions and deletions without exhausting a specified deletion allowance. Of these, 18 retain the entire sequence of remaining inputs. We compare each synthesized fold against a baseline that processes insertions incrementally but recomputes the original fold after each deletion. We evaluate workloads with initial input sizes ranging from 1,000 to 30,000 records, followed by twice as many updates. Updates are randomly chosen insertions and deletions with equal probability, with deletions sampled uniformly from the records currently present. Both implementations process identical workloads and produce an output after each update. We measure mean time per update, including output evaluation but excluding initialization, using five random seeds and three runs per seed. We report speedup as the baseline’s mean update time divided by that of the synthesized fold and aggregate speedups across scenarios using the geometric mean.
Results. Table 6 summarizes the results for workloads initialized with 30,000 inputs. All 45 synthesized implementations outperform recomputation, with speedups ranging from 1.14× to over 8,000× and a geometric mean of 1,067.3×. Notably, even the 18 implementations that retain the entire input sequence achieve substantial gains, with a geometric-mean speedup of 272.0× over recomputation. Figure 7 shows that the benefits grow with the initial number of inputs. Across all 45 scenarios, geometric-mean speedup increases from 47.9× at 1,000 initial inputs to 1,067.3× at 30,000. For implementations that retain the input sequence, speedup increases from 15.2× to 272.0×.
Table 6. Speedup over recomputation.
Figure 7. Speedup vs input size.
Result for RQ3: 85 of 94 synthesized folds support sublinear retraction. At 30,000 initial inputs, geometric-mean speedup over recomputation is 1,067.3×.
RQ4: Ablation Studies
Our fourth research question evaluates the importance of Rewind’s core synthesis mechanisms. We compare the full system against three ablations that disable one component at a time:
NoLiftInv disables invariant-guided lifting by setting the logical qualifier to true. Without this guidance, the system must search for lifted accumulators without a relational explanation of how the lifted state corresponds to the original fold.
NoFeasibilityCheck disables the feasibility checks (Algorithms 3 and 4) for lifting specifications. Without these checks, the system may spend time pursuing lift specifications that cannot be realized by any valid lifted accumulator or retraction function.
NoSymbolicPartition replaces our symbolic-partitioning-based retraction synthesis procedure with a direct SyGuS query for the entire retraction function. Hence, this ablation tests whether our symbolic partitioning idea is useful.
Figure 8 reports the results of this ablation study using a 15-minute timeout per benchmark. The running time excludes verification time. Because all variants invoke the same verifier after synthesis, the intention is to isolate the effect of the synthesis procedure itself. As standard for a cactus plot, the y y y -axis shows the percentage of benchmarks solved within a given time budget.
Figure 8. RQ4: Comparison between Rewind and its ablations. The x x x -axis shows synthesis time, excluding verification time, since all variants use the same final verifier. The y y y -axis shows the percentage of benchmarks solved within the corresponding time budget.
The results show that each of our algorithmic ideas contributes to scalability. Removing lifting invariants (NoLiftInv) substantially reduces the success rate, dropping from 100.0% for the full system to 47.9%. Disabling the feasibility checks from Algorithms 3 and 4 (NoFeasibilityCheck) similarly reduces the success rate to 58.5%. The last ablation, NoSymbolicPartition, has the largest effect: it solves only 28.7% of the benchmarks and plateaus almost immediately. This suggests that direct retraction synthesis is only effective for the simplest cases. For the remaining benchmarks, the retraction logic is too case-dependent to synthesize as a single monolithic SyGuS query, and our symbolic partitioning method is essential for scalability.
Result for RQ4: Each ablation significantly degrades performance, showing that Rewind relies on all three algorithmic ingredients. Disabling symbolic partitioning or lifting invariants substantially reduces the success rate. Feasibility checks have a smaller effect on success rate, but a large effect on synthesis time.
Related Work
Dynamic and static incremental computation. A substantial line of work studies how to maintain outputs efficiently as inputs change. Self-adjusting computation [2 , 3 , 26 ] records dynamic dependencies during execution and propagates input changes to avoid full recomputation. This idea has led to a number of language and runtime systems for general-purpose incremental computation, including CEAL [27 ] and demand-driven systems such as Adapton [28 , 29 ] . A complementary static line of work derives incremental programs by transforming existing programs [16 , 25 , 39 ] . Unlike these approaches, which provide general mechanisms for propagating input changes, our setting starts from a fold with a given forward update function and synthesizes the missing logic for removing previously processed inputs. These approaches are complementary to ours: runtime systems reuse portions of previous executions, whereas synthesis can discover auxiliary summaries and update rules not explicit in those executions. Moreover, general-purpose incremental-computation systems are not drop-in alternatives for Flink aggregations targeted by Rewind.
Synthesis-based algorithmic transformations. Program synthesis has also been used to derive more efficient implementations from high-level computations. For example, OPERA synthesizes online algorithms from offline ones [51 ] , AUTOLIFTER applies divide-and-conquer-like algorithmic paradigms by synthesizing lifted programs [35 ] , SUPERFUSION eliminates intermediate data structures through inductive synthesis [34 ] , and Ji et al. synthesize efficient dynamic-programming algorithms via thinning transformations [36 ] . These systems, like ours, use synthesis to obtain a more efficient computation. However, they focus on reorganizing how the output is computed from the input, whereas we synthesize retraction logic to remove a previously processed input.
Incremental view maintenance and dataflow systems. Many database and dataflow systems support efficient maintenance under insertions and deletions. Systems such as DBTOASTER, DIFFERENTIAL DATAFLOW, and DBSP maintain results for queries or dataflows by exploiting the algebraic structure of the underlying language, where changes to inputs can be propagated compositionally through operators and deletions can be represented as negative updates [15 , 37 , 42 ] . Our setting is different in that the computation is not given as an algebraic query or dataflow graph. Instead, our setting involves a stateful fold with an arbitrary accumulator, which may not contain enough information to support deletion. REWIND addresses this problem by synthesizing both the auxiliary state and the retraction function, together with a proof of correctness.
Bidirectional transformations. Our work is related to bidirectional programming and lenses. In the lens setting, a program maintains consistency between a source and a derived view via paired get and put functions satisfying round-trip laws [13 , 14 , 23 , 24 ] . Our problem has a similar flavor because it also relates a forward computation to a state update that preserves semantic consistency. However, the technical problem is different. A lens update is given an edited view and produces a new source consistent with that view. In contrast, a retraction is given the current accumulator state and a deleted input element, and must produce a state equivalent to recomputing the fold on the remaining inputs. Thus, rather than repairing a source after a view edit, our challenge is to determine what information the accumulator must retain so that deletions can be processed incrementally and synthesize the retraction logic.
Program inversion. Retraction synthesis is closely related to program inversion, which aims to construct an inverse or partial inverse for a given program. Prior work has studied inversion for functional programs [43 ] , symbolic transducer-based inversion [33 ] , and path-based inductive synthesis of imperative program inverses [48 ] . Despite this connection, retraction differs from ordinary program inversion in the specification it must satisfy. An inverse of accumulate would recover a pre-state from a post-state and the last inserted element, e.g., an operation satisfying unacc ( acc ( s , x ) , x ) = s \operatorname{unacc}(\operatorname{acc}(s,x),x)=s unacc ( acc ( s , x ) , x ) = s . Such an operation is only meaningful for undoing the most recent update. In contrast, retract must remove the effect of an element from an accumulated history, possibly after many other insertions have occurred.
Synthesis of auxiliary state. Prior work has shown that program transformations often become realizable only after lifting a computation with additional state. In divide-and-conquer synthesis, Farzan and Nicolet show how to synthesize conditional accumulators so that partial results can be composed correctly [20 , 21 ] . Subsequent work on phased synthesis [22 ] and SE²GIS [19 ] process more compositionally and uses symbolic reasoning, including unrealizability witnesses, to guide synthesis. More recently, AUTOLIFTER [35 ] automatically infers auxiliary variables that make relational specifications realizable. Our work builds on the same broad insight, but applies it to a different kind of transformation. In this setting, the lifted state must support not only the forward computation, but also a well-defined deletion operation that satisfies a user-specified requirement and whose result agrees with recomputation on the normalized history.
Solver-aided synthesis. Our method builds on standard solver-aided synthesis techniques such as CEGIS [45 , 46 ] and SyGuS [4 ] , as well as later work that uses symbolic reasoning to improve synthesis [19 , 31 , 32 ] . These techniques provide some of the solver infrastructure used by Rewind, but the main challenge in our setting is formulating retraction as a tractable synthesis problem. Rewind addresses this challenge by searching for feasible lift specifications, inferring relational invariants between mixed and normalized executions, and using symbolic partitioning to decompose retract synthesis into simpler subproblems that can be solved using off-the-shelf tools.
Relational verification. This paper is also related to the long line of work on relational verification, which reasons about multiple executions of one or more programs. Relational Hoare logics and their variants have been used to prove program equivalence, refinement, security, differential privacy, and quantitative relational properties [10 , 11 , 12 , 18 , 47 ] . Another line of work reduces relational verification to ordinary safety verification using self-composition, cross-products, and product programs [7 , 8 , 9 , 49 , 53 ] . We use this latter idea as the basis for our verification baseline. As discussed in § 4 , however, verifying retractable folds raises additional challenges beyond standard equivalence checking. Our proof system addresses these challenges by making the retraction-specific invariants and support obligations explicit.
Retraction support in data processing. Modern data-processing systems expose deletion support through explicit retraction interfaces. For example, Flink allows an AggregateFunction to provide a retract method for incrementally removing previously processed records [17 ] . RisingWave provides a similar retract interface for user-defined aggregates; without it, the aggregate cannot be updated incrementally in materialized views [1 ] . These interfaces motivate our work because they make it possible for the system to take advantage of retraction function. However, they leave the developer responsible for implementing and validating the corresponding retraction logic.
Conclusion
This paper studied how to automatically add deletion support to fold-based incremental computations. We developed a semantic framework in which retraction specifications describe the required deletion support, together with a proof system that decomposes correctness into local relational obligations. Building on this foundation, our synthesis procedure searches for auxiliary state through lifting invariants, synthesizes the operations needed to maintain that state, and derives retraction logic through symbolic partitioning. We also experimentally evaluated our approach and showed that Rewind synthesizes verified retractable folds for all 94 benchmark scenarios. Furthermore, the resulting implementations substantially outperform recomputation.
Our approach also suggests broader opportunities in data representation synthesis, where changing a representation requires adapting its operations while preserving correctness. Retraction adds a further challenge, as the new representation must support functionality absent from the original program. Our techniques address this by using lifting invariants to reject unrealizable representations, leveraging relational reasoning to connect existing and new operations, and employing symbolic partitioning to derive smaller synthesis problems from a reference implementation. We believe these ideas can help automate other program transformations that introduce new functionality while preserving existing behavior.
Data-Availability Statement
Our artifact, including the Rewind implementation, benchmark suite, and evaluation scripts, is available on Zenodo [52 ] .
Acknowledgments
We thank the anonymous reviewers for their thoughtful and constructive feedback. This work was conducted in a research group supported by NSF awards CCF-1918889, CNS-1908304, CCF-1901376, CNS-2120696, CCF-2210831, CCF-2319471, CCF-2422130, and CCF-2403211, as well as DARPA awards under agreements HR00112590133 and HR0011262E020, and a Renaissance Philanthropy award.
References
Detailed Trace of Symbolic Partition Refinement
Example A.1. Continuing Example 5.8 , we now explain how Algorithm 7 derives symbolic partitions as the explored history length increases. For ℓ = 1 \ell= 1 ℓ = 1 , let x s = [ x 1 ] x_s = [x_1] x s = [ x 1 ] . Symbolic execution yields the single final state ( x 1 , 1 ) (x_1,1) ( x 1 , 1 ) , and deleting x 1 x_1 x 1 leads to the unique predecessor ( ∞ , 0 ) (\infty,0) ( ∞ , 0 ) . After quantifier elimination, this yields the symbolic partition with just one element:
γ 0 ( ( m , c ) , x ) ≡ ( c = 1 ∧ x = m ) , ψ 0 ( ( m , c ) , x ) ≡ retract ( ( m , c ) , x ) = ( ∞ , 0 ) . \gamma_0((m,c),x) \equiv(c = 1 \land x = m), \qquad\psi_0((m,c),x) \equiv\operatorname{retract}((m,c),x) = (\infty,0). γ 0 (( m , c ) , x ) ≡ ( c = 1 ∧ x = m ) , ψ 0 (( m , c ) , x ) ≡ retract (( m , c ) , x ) = ( ∞ , 0 ) . Now, suppose inductive synthesis returns the following solution:
retract ( ( m , c ) , x ) = ( ∞ , 0 ) . \operatorname{retract}((m,c),x) = (\infty,0). retract (( m , c ) , x ) = ( ∞ , 0 ) . This is consistent with the current local specification, but it fails full verification, for example, with the concrete counterexample x s = [ 1 , 2 ] x_s = [1,2] x s = [ 1 , 2 ] , s ∗ = ( 1 , 1 ) s^* = (1,1) s ∗ = ( 1 , 1 ) , x ∗ = 2 x^* = 2 x ∗ = 2 . This counterexample has previously unseen length ℓ = 2 \ell= 2 ℓ = 2 ; hence, Algorithm 6 invokes UpdateSymbolicPartitions. For x s = [ x 1 , x 2 ] x_s = [x_1,x_2] x s = [ x 1 , x 2 ] , symbolic execution now separates the possible histories according to whether the deleted element is equal to one of two minima, larger than the minimum, or equal to a unique minimum. For the first case, deleting one of two equal minima yields the partition element:
γ 1 ( ( m , c ) , x ) ≡ ( c = 2 ∧ x = m ) , ψ 1 ( ( m , c ) , x ) ≡ retract ( ( m , c ) , x ) = ( m , c − 1 ) . \gamma_1((m,c),x) \equiv(c = 2 \land x = m), \qquad\psi_1((m,c),x) \equiv\operatorname{retract}((m,c),x) = (m,c-1). γ 1 (( m , c ) , x ) ≡ ( c = 2 ∧ x = m ) , ψ 1 (( m , c ) , x ) ≡ retract (( m , c ) , x ) = ( m , c − 1 ) . For the second case, we obtain the candidate partitions:
γ 2 a ( ( m , c ) , x ) ≡ ( c = 1 ∧ x > m ) , ψ 2 a ( ( m , c ) , x ) ≡ retract ( ( m , c ) , x ) = ( m , c ) , \gamma_2^a((m,c),x) \equiv(c = 1 \land x > m), \qquad\psi_2^a((m,c),x) \equiv\operatorname{retract}((m,c),x) = (m,c), γ 2 a (( m , c ) , x ) ≡ ( c = 1 ∧ x > m ) , ψ 2 a (( m , c ) , x ) ≡ retract (( m , c ) , x ) = ( m , c ) , γ 2 b ( ( m , c ) , x ) ≡ ( c = 2 ∧ x > m ) , ψ 2 b ( ( m , c ) , x ) ≡ retract ( ( m , c ) , x ) = ( m , c ) . \gamma_2^b((m,c),x) \equiv(c = 2 \land x > m), \qquad\psi_2^b((m,c),x) \equiv\operatorname{retract}((m,c),x) = (m,c). γ 2 b (( m , c ) , x ) ≡ ( c = 2 ∧ x > m ) , ψ 2 b (( m , c ) , x ) ≡ retract (( m , c ) , x ) = ( m , c ) . When performing InsertOrMerge, these two get merged into a common partition by generalizing constants into an unknown predicate, yielding the guard γ 2 ( ( m , c ) , x ) ≡ □ 1 ∧ x > m \gamma_2((m,c),x) \equiv\square_1 \land x > m γ 2 (( m , c ) , x ) ≡ □ 1 ∧ x > m and its corresponding specification retract ( ( m , c ) , x ) = ( m , c ) \operatorname{retract}((m,c),x) = (m,c) retract (( m , c ) , x ) = ( m , c ) .
Finally, the third case where the deleted element is equal to the unique minimum has path condition c = 1 c = 1 c = 1 and x = m x = m x = m . But this case fails the ambiguity test at lines 9–10 and is pruned. Thus, at the end of the second CEGIS iteration, we have the following symbolic partition:
{ ( c = 2 ∧ x = m ) ↦ ( m , c − 1 ) , ( □ 1 { c = 1 ∨ c = 2 } ∧ x > m ) ↦ ( m , c ) } \{(c = 2 \land x = m) \mapsto(m,c-1),\;(\square_1\{c = 1 \lor c = 2\} \land x > m) \mapsto(m,c)\} {( c = 2 ∧ x = m ) ↦ ( m , c − 1 ) , ( □ 1 { c = 1 ∨ c = 2 } ∧ x > m ) ↦ ( m , c )} where the notation □ { ϕ } \square\{\phi\} □ { ϕ } indicates that □ \square □ must be instantiated with a formula that generalizes ϕ \phi ϕ .
In the next iteration, suppose SolveSyGuS returns the following plausible solution:
retract ( ( m , c ) , x ) = if c = 2 ∧ x = m then ( m , 1 ) else if x > m then ( m , c ) else Err \operatorname{retract}((m,c),x) = \text{if } c = 2 \land x = m \text{ then } (m,1) \text{ else if } x > m \text{ then } (m,c) \text{ else Err} retract (( m , c ) , x ) = if c = 2 ∧ x = m then ( m , 1 ) else if x > m then ( m , c ) else Err This solution is also incorrect, and Verify can find a new counterexample of length 3; for example, x s = [ 1 , 1 , 1 ] x_s = [1,1,1] x s = [ 1 , 1 , 1 ] , s ∗ = ( 1 , 3 ) s^* = (1,3) s ∗ = ( 1 , 3 ) , x ∗ = 1 x^* = 1 x ∗ = 1 . Algorithm 6 then invokes UpdateSymbolicPartitions, now with ℓ = 3 \ell= 3 ℓ = 3 . Symbolic execution discovers a new candidate partition element:
γ 4 ( ( m , c ) , x ) ≡ ( c = 3 ∧ x = m ) , ψ 4 ( ( m , c ) , x ) ≡ retract ( ( m , c ) , x ) = ( m , c − 1 ) . \gamma_4((m,c),x) \equiv(c = 3 \land x = m), \qquad\psi_4((m,c),x) \equiv\operatorname{retract}((m,c),x) = (m,c-1). γ 4 (( m , c ) , x ) ≡ ( c = 3 ∧ x = m ) , ψ 4 (( m , c ) , x ) ≡ retract (( m , c ) , x ) = ( m , c − 1 ) . This is the same pattern γ 1 \gamma_1 γ 1 already seen at ℓ = 2 \ell= 2 ℓ = 2 , except with c = 3 c = 3 c = 3 . Thus, InsertOrMerge merges γ 4 \gamma_4 γ 4 with γ 1 \gamma_1 γ 1 and yields γ 1 ′ ( ( m , c ) , x ) ≡ ( □ 2 ∧ x = m ) \gamma_1'((m,c),x) \equiv(\square_2 \land x = m) γ 1 ′ (( m , c ) , x ) ≡ ( □ 2 ∧ x = m ) , where □ 2 \square_2 □ 2 is a predicate that generalizes c = 2 ∨ c = 3 c = 2 \lor c = 3 c = 2 ∨ c = 3 . This gives rise to the new symbolic partition:
{ ( □ 2 { c = 2 ∨ c = 3 } ∧ x = m ) ↦ ( m , c − 1 ) , ( □ 1 { c = 1 ∨ c = 2 ∨ c = 3 } ∧ x > m ) ↦ ( m , c ) } \{(\square_2\{c = 2 \lor c = 3\} \land x = m) \mapsto(m,c-1),\;(\square_1\{c = 1 \lor c = 2 \lor c = 3\} \land x > m) \mapsto(m,c)\} {( □ 2 { c = 2 ∨ c = 3 } ∧ x = m ) ↦ ( m , c − 1 ) , ( □ 1 { c = 1 ∨ c = 2 ∨ c = 3 } ∧ x > m ) ↦ ( m , c )} Instantiating □ 1 \square_1 □ 1 with T \mathsf{T} T and □ 2 \square_2 □ 2 with c > 1 c > 1 c > 1 , a plausible synthesized solution is then
retract ( ( m , c ) , x ) = if x > m then ( m , c ) else if x = m ∧ c > 1 then ( m , c − 1 ) else Err . \operatorname{retract}((m,c),x) = \text{if } x > m \text{ then } (m,c) \text{ else if } x = m \land c > 1 \text{ then } (m,c-1) \text{ else Err}. retract (( m , c ) , x ) = if x > m then ( m , c ) else if x = m ∧ c > 1 then ( m , c − 1 ) else Err . This solution passes Verify and the CEGIS loop terminates.
Algorithm 8 ProductVerify(P, R, θ)
1: ys ← [ ]; xs ← [ ]; n ← 0
2: sfull ← R.I; smix ← R.I; seff ← R.I; sorig ← P.I
3: CheckInv(P, R, ys, xs, n, sfull, smix, seff, sorig)
4: while ⋆ do
5: choose op ∈ {insert, retract}
6: choose a
7: if op = insert then
8: ys ← ys · [a]; xs ← xs · [a]
9: sfull ← R.acc(sfull, a); smix ← R.acc(smix, a); seff ← R.acc(seff, a); sorig ← P.acc(sorig, a)
10: else if op = retract then
11: assume a ∈ xs
12: if ¬(n < θ(ys)) then assume false
13: tmp ← R.retract(smix, a)
14: assert tmp ≠ Err
15: smix ← tmp
16: xs ← remove(xs, a)
17: n ← n + 1
18: seff ← R.I; sorig ← P.I
19: for each b ∈ xs do
20: seff ← R.acc(seff, b); sorig ← P.acc(sorig, b)
21: CheckInv(P, R, ys, xs, n, sfull, smix, seff, sorig)
Product-Program Baseline
This appendix describes the product-program encoding used for the baseline verifier in § 7 . The goal of the encoding is to reduce equivalence modulo a retraction specification to an ordinary safety-verification problem. The product program simulates the executions that appear in the semantic correctness condition and asserts, after each update, that these executions remain appropriately related.
Let P P P be the original fold, let R R R be the candidate retractable fold, and let θ \theta θ be the retraction specification. The product program maintains three ghost variables:
y s y_s y s for the full insertion history
x s x_s x s for the effective history
n n n for the number of retractions so far
It also maintains four program states:
s f u l l s_{\mathrm{full}} s full , which is the state of R R R on the full insertion history y s y_s y s ;
s m i x s_{\mathrm{mix}} s mix , which is the state of R R R on the actual mixed update sequence;
s e f f s_{\mathrm{eff}} s eff , which is the state of R R R on the effective insertion-only history x s x_s x s ;
s o r i g s_{\mathrm{orig}} s orig , which is the state of P P P on the same effective history x s x_s x s .
Algorithm 8 shows the product-program encoding. Insertion steps advance all four executions in lockstep (lines 7–9): both ghost histories are extended with the inserted element, and each simulated state takes the corresponding accumulator step. Retraction steps are handled only when they are within the budget prescribed by θ \theta θ (lines 11–13): if n ≥ θ ( y s ) n \ge\theta(y_s) n ≥ θ ( y s ) , the path is pruned because the specification imposes no further support obligation. Otherwise, the product program asserts Algorithm 9 CheckInv (P P P , R R R , y s y_s y s , x s x_s x s , n n n , s f u l l s_{\mathrm{full}} s full , s m i x s_{\mathrm{mix}} s mix , s e f f s_{\mathrm{eff}} s eff , s o r i g s_{\mathrm{orig}} s orig )
assert Reach(y s y_s y s , s f u l l s_{\mathrm{full}} s full )
assert Ω ( s f u l l , s m i x , n ) \Omega(s_{\mathrm{full}}, s_{\mathrm{mix}}, n) Ω ( s full , s mix , n )
assert Reach(x s x_s x s , s e f f s_{\mathrm{eff}} s eff )
assert Ψ ( x s , s e f f , s m i x ) \Psi(x_s, s_{\mathrm{eff}}, s_{\mathrm{mix}}) Ψ ( x s , s eff , s mix )
assert P . eval ( s o r i g ) = R . eval ( s e f f ) P.\operatorname{eval}(s_{\mathrm{orig}}) = R.\operatorname{eval}(s_{\mathrm{eff}}) P . eval ( s orig ) = R . eval ( s eff )
that the candidate retract succeeds, updates the mixed execution with the returned state, removes the deleted element from the effective history, and increments the retraction count (lines 14–18).
After a retraction, the effective-history executions are reconstructed by replaying the elements of x s x_s x s from the initial states of R R R and P P P in lockstep (lines 19–22). This makes s e f f s_{\mathrm{eff}} s eff and s o r i g s_{\mathrm{orig}} s orig explicit reference executions on the normalized history. The ghost variables are updated so that x s x_s x s is obtained from y s y_s y s by exactly n n n deletions.
Algorithm 9 contains the safety assertions used by the baseline verifier. The first and third assertions check that s f u l l s_{\mathrm{full}} s full and s e f f s_{\mathrm{eff}} s eff are valid insertion-only executions of R R R on y s y_s y s and x s x_s x s , respectively. The Ω \Omega Ω assertion relates the full-history execution to the mixed execution while accounting for the number of retractions already performed. The Ψ \Psi Ψ assertion relates the mixed execution to the effective insertion-only execution. Finally, the last assertion checks observational equivalence between the original fold and the retractable fold on the effective history.
Soundness of Proof Rules
In this section we prove the soundness direction of Theorem 4.2 : if there exist predicates Reach, Ψ \Psi Ψ , and Ω \Omega Ω satisfying all rules in Figure 3 , then the candidate retractable fold R R R is equivalent to P P P modulo θ \theta θ in the sense of Definition 3.9 . Throughout this section we fix an insertion-only fold P = ( I , acc , eval ) P = (I, \operatorname{acc}, \operatorname{eval}) P = ( I , acc , eval ) , a retractable fold R = ( I ′ , acc ′ , retract , eval ′ ) R = (I', \operatorname{acc}', \operatorname{retract}, \operatorname{eval}') R = ( I ′ , acc ′ , retract , eval ′ ) , and a retraction specification θ \theta θ . Up to and including Theorem C.10 , we additionally fix predicates Reach, Ψ \Psi Ψ , and Ω \Omega Ω satisfying all rules in Figure 3 .
Conventions. All update sequences below are valid in the sense of Definition 3.4 ; following the convention adopted in § 3 , the quantifier in Definition 3.6 also ranges over valid sequences (clause (2) of Definition 3.9 is already stated for valid sequences). Validity is closed under taking prefixes, since the defining condition of Definition 3.4 is imposed separately at every index j j j . We call an update sequence accumulation-only if every operation tag in it is acc \operatorname{acc} acc ; accumulation-only sequences are trivially valid. We use two direct consequences of Definition 3.5 . First, Err is absorbing: if [ R ] ω = E r r [R]_{\omega} = \mathrm{Err} [ R ] ω = Err , then [ R ] ω + + ω ′ ′ = E r r [R]_{\omega++\omega''} = \mathrm{Err} [ R ] ω ++ ω ′′ = Err for every extension of ω \omega ω ; contrapositively, if some extension of ω \omega ω has non-Err state semantics, then so does ω \omega ω itself. Second, if [ R ] ω ≠ E r r [R]_{\omega} \ne\mathrm{Err} [ R ] ω = Err , then
[ R ] ω + + [ ( acc , x ) ] = acc ′ ( [ R ] ω , x ) , [ R ] ω + + [ ( retract , x ) ] = retract ( [ R ] ω , x ) . [R]_{\omega++[(\operatorname{acc},x)]} = \operatorname{acc}'([R]_{\omega},x), \qquad[R]_{\omega++[(\operatorname{retract},x)]} = \operatorname{retract}([R]_{\omega},x). [ R ] ω ++ [( acc , x )] = acc ′ ([ R ] ω , x ) , [ R ] ω ++ [( retract , x )] = retract ([ R ] ω , x ) .
For a list x s xs x s and an element x ∈ x s x \in xs x ∈ x s , we write remove ( x s , x ) \operatorname{remove}(xs,x) remove ( x s , x ) for the list obtained by deleting the first occurrence of x x x . This convention makes remove well defined on lists and matches condition (2) of Definition 3.8 , which cancels the earliest accumulation of the retracted value. Part (iii) of Lemma C.1 establishes the correspondence.
The following normalization facts are used throughout the section.
Lemma C.1 (Elementary Properties of Normalization). Let ω \omega ω be a valid update sequence. Then:
(i) Norm ( ω ) \operatorname{Norm}(\omega) Norm ( ω ) is accumulation-only, and ζ ( Norm ( ω ) ) = Effect ( ω ) \zeta(\operatorname{Norm}(\omega)) = \operatorname{Effect}(\omega) ζ ( Norm ( ω )) = Effect ( ω ) ;
(ii) Norm ( ω + + [ ( a c c , x ) ] ) = Norm ( ω ) + + [ ( a c c , x ) ] \operatorname{Norm}(\omega++[(\mathrm{acc},x)])=\operatorname{Norm}(\omega)++[(\mathrm{acc},x)] Norm ( ω + + [( acc , x )]) = Norm ( ω ) + + [( acc , x )] , and hence
Effect ( ω + + [ ( a c c , x ) ] ) = Effect ( ω ) + + [ x ] ; \operatorname{Effect}(\omega++[(\mathrm{acc},x)])=\operatorname{Effect}(\omega)++[x]; Effect ( ω + + [( acc , x )]) = Effect ( ω ) + + [ x ] ;
(iii) if ω + + [ ( r e t r a c t , x ) ] \omega++[(\mathrm{retract},x)] ω + + [( retract , x )] is valid, then x ∈ Effect ( ω ) x \in\operatorname{Effect}(\omega) x ∈ Effect ( ω ) , the sequence Norm ( ω + + [ ( r e t r a c t , x ) ] ) \operatorname{Norm}(\omega++[(\mathrm{retract},x)]) Norm ( ω + + [( retract , x )]) is obtained from Norm ( ω ) \operatorname{Norm}(\omega) Norm ( ω ) by deleting its first accumulation of x x x , and consequently
Effect ( ω + + [ ( r e t r a c t , x ) ] ) = remove ( Effect ( ω ) , x ) ; \operatorname{Effect}(\omega++[(\mathrm{retract},x)])=\operatorname{remove}(\operatorname{Effect}(\omega),x); Effect ( ω + + [( retract , x )]) = remove ( Effect ( ω ) , x ) ;
(iv) DeletesTo ( ζ ( ω + ) , Effect ( ω ) , ∣ ω − ∣ ) \operatorname{DeletesTo}(\zeta(\omega^{+}),\operatorname{Effect}(\omega),\lvert\omega^{-}\rvert) DeletesTo ( ζ ( ω + ) , Effect ( ω ) , ∣ ω − ∣) , that is, Effect ( ω ) \operatorname{Effect}(\omega) Effect ( ω ) is obtained from ζ ( ω + ) \zeta(\omega^{+}) ζ ( ω + ) by deleting exactly ∣ ω − ∣ \lvert\omega^{-}\rvert ∣ ω − ∣ elements.
Proof. The rewrite rule of Definition 3.8 terminates in a unique fixpoint on every valid sequence. Termination follows because every rewrite strictly shortens the sequence. For uniqueness, each rewrite preserves validity. A rewrite deletes one accumulation of some value a a a (at position i i i ) and one retraction of the same value (at position j > i j>i j > i ), leaving the prefix counts of every value other than a a a unchanged. As for a a a itself, condition (2) makes position i i i the first accumulation of a a a , so by validity no retraction of a a a occurs before i i i , and condition (3) excludes retractions of a a a strictly between i i i and j j j . Hence, in the rewritten sequence, the count condition of Definition 3.4 for a a a still holds at every index: prefixes ending before i i i are unchanged, prefixes ending in [ i , j ) [i,j) [ i , j ) contain no retraction of a a a at all, and prefixes ending at or after j j j lose one accumulation and one retraction of a a a . Next, in a valid sequence, the redex for each value a a a is uniquely determined: condition (2) forces i i i to be the position of the first accumulation of a a a , and condition (3), together with validity, forces j j j to be the position of the first retraction of a a a . Moreover, rewrites for distinct values neither enable nor disable one another, because all side conditions of the rule constrain only positions carrying the value being cancelled. Consequently, exhaustive rewriting deletes the same set of positions under every rewrite order, and the fixpoint is unique.
(i) Suppose, toward a contradiction, that Norm ( ω ) \operatorname{Norm}(\omega) Norm ( ω ) contains a retraction, and consider the first retraction in Norm ( ω ) \operatorname{Norm}(\omega) Norm ( ω ) , say of value a a a at position j j j . Since rewriting preserves validity, Norm ( ω ) \operatorname{Norm}(\omega) Norm ( ω ) is valid, so some accumulation of a a a occurs before position j j j ; let i i i be the position of the first accumulation of a a a . Then i < j i<j i < j , condition (2) holds by the choice of i i i , and condition (3) holds because there is no retraction at all before position j j j . Hence the rewrite rule applies to Norm ( ω ) \operatorname{Norm}(\omega) Norm ( ω ) , contradicting the fact that Norm ( ω ) \operatorname{Norm}(\omega) Norm ( ω ) is a fixpoint. Therefore Norm ( ω ) \operatorname{Norm}(\omega) Norm ( ω ) is accumulation-only, and ζ ( Norm ( ω ) ) = Effect ( ω ) \zeta(\operatorname{Norm}(\omega))=\operatorname{Effect}(\omega) ζ ( Norm ( ω )) = Effect ( ω ) holds by the definition of Effect in Definition 3.8 .
(ii) Appending an accumulation preserves validity, so ω + + [ ( a c c , x ) ] \omega++[(\mathrm{acc},x)] ω + + [( acc , x )] is valid. The trailing accumulation survives every rewrite because it has no later matching retraction. Its presence leaves all other redexes unchanged: conditions (2) and (3) for a redex ( i , j ) (i,j) ( i , j ) constrain positions strictly before j j j , whereas the trailing element occupies the last position. Exhaustive rewriting therefore follows the same steps on both sequences, and the resulting fixpoint is Norm ( ω ) + + [ ( a c c , x ) ] \operatorname{Norm}(\omega)++[(\mathrm{acc},x)] Norm ( ω ) + + [( acc , x )] . Taking value lists yields
Effect ( ω + + [ ( a c c , x ) ] ) = Effect ( ω ) + + [ x ] . \operatorname{Effect}(\omega++[(\mathrm{acc},x)])=\operatorname{Effect}(\omega)++[x]. Effect ( ω + + [( acc , x )]) = Effect ( ω ) + + [ x ] .
(iii) The fact x ∈ Effect ( ω ) x \in\operatorname{Effect}(\omega) x ∈ Effect ( ω ) follows by counting. Every rewrite deletes exactly one accumulation and one retraction of the same value, and by (i) the fixpoint Norm ( ω ) \operatorname{Norm}(\omega) Norm ( ω ) contains no retraction. Hence, for every value a a a , exactly as many accumulations of a a a are cancelled as there are retractions of a a a in ω \omega ω , so the number of occurrences of a a a in Effect ( ω ) \operatorname{Effect}(\omega) Effect ( ω ) equals the number of accumulations of a a a in ω \omega ω minus the number of retractions of a a a in ω \omega ω . Validity of ω + + [ ( r e t r a c t , x ) ] \omega++[(\mathrm{retract},x)] ω + + [( retract , x )] , instantiated at its last index, says that ω \omega ω contains strictly more accumulations of x x x than retractions of x x x ; therefore x x x occurs at least once in Effect ( ω ) \operatorname{Effect}(\omega) Effect ( ω ) .
For the remaining claims, use the order independence established above and normalize ω + + [ ( r e t r a c t , x ) ] \omega++[(\mathrm{retract},x)] ω + + [( retract , x )] by first rewriting its prefix. Every redex of ω \omega ω remains a redex of ω + + [ ( r e t r a c t , x ) ] \omega++[(\mathrm{retract},x)] ω + + [( retract , x )] , because conditions (2) and (3) constrain positions strictly before the retraction being cancelled, whereas the trailing element occupies the last position. appended element occupies the last position. Performing the rewrites internal to ω \omega ω first reaches Norm ( ω ) + + [ ( retract , x ) ] \operatorname{Norm}(\omega)++[(\operatorname{retract},x)] Norm ( ω ) + + [( retract , x )] . Since Norm ( ω ) \operatorname{Norm}(\omega) Norm ( ω ) is accumulation-only and contains an accumulation of x x x , one further rewrite applies: it cancels the trailing retraction against the first accumulation of x x x in Norm ( ω ) \operatorname{Norm}(\omega) Norm ( ω ) (condition (2) selects the first accumulation of x x x , and condition (3) is vacuous because no other retraction is present). The result is accumulation-only. Since every redex requires a retraction at position j j j , this result is a fixpoint. Thus Norm ( ω + + [ ( retract , x ) ] ) \operatorname{Norm}(\omega++[(\operatorname{retract},x)]) Norm ( ω + + [( retract , x )]) is Norm ( ω ) \operatorname{Norm}(\omega) Norm ( ω ) with its first accumulation of x x x deleted, and taking value lists gives
Effect ( ω + + [ ( retract , x ) ] ) = remove ( Effect ( ω ) , x ) . \operatorname{Effect}(\omega++[(\operatorname{retract},x)])=\operatorname{remove}(\operatorname{Effect}(\omega),x). Effect ( ω + + [( retract , x )]) = remove ( Effect ( ω ) , x ) .
(iv) By the counting argument in (iii), exhaustive rewriting of ω \omega ω performs exactly ∣ ω − ∣ |\omega^-| ∣ ω − ∣ rewrites: each rewrite deletes exactly one retraction, and the fixpoint contains none. Each rewrite also deletes exactly one accumulation, and deletions preserve the relative order of the surviving entries. Hence Norm ( ω ) \operatorname{Norm}(\omega) Norm ( ω ) is obtained from ω + \omega^+ ω + by deleting exactly ∣ ω − ∣ |\omega^-| ∣ ω − ∣ entries. Taking value lists, Effect ( ω ) \operatorname{Effect}(\omega) Effect ( ω ) is obtained from ζ ( ω + ) \zeta(\omega^+) ζ ( ω + ) by deleting exactly ∣ ω − ∣ |\omega^-| ∣ ω − ∣ elements, which is precisely DeletesTo ( ζ ( ω + ) , Effect ( ω ) , ∣ ω − ∣ ) \operatorname{DeletesTo}(\zeta(\omega^+),\operatorname{Effect}(\omega),|\omega^-|) DeletesTo ( ζ ( ω + ) , Effect ( ω ) , ∣ ω − ∣ ) .
The next lemma shows a property of the retractable-fold semantics used below.
Lemma C.2 (Accumulation-only executions avoid Err). For every accumulation-only update sequence ω \omega ω , the state semantics [ R ] ω [R]_\omega [ R ] ω is an element of the state type of R R R ; in particular, [ R ] ω ≠ Err [R]_\omega\ne\operatorname{Err} [ R ] ω = Err .
Proof. Let s 0 , … , s n s_0,\ldots,s_n s 0 , … , s n be the state sequence of Definition 3.5 for R R R on ω \omega ω . We show by induction on j j j that every s j s_j s j is a proper state. For j = 0 j=0 j = 0 we have s 0 = I ′ s_0=I' s 0 = I ′ , which is a state by the definition of a retractable fold. For j > 0 j>0 j > 0 , the induction hypothesis gives s j − 1 ≠ Err s_{j-1}\ne\operatorname{Err} s j − 1 = Err , and o j = acc o_j=\operatorname{acc} o j = acc because ω \omega ω is accumulation-only, so s j = acc ′ ( s j − 1 , a j ) s_j=\operatorname{acc}'(s_{j-1},a_j) s j = acc ′ ( s j − 1 , a j ) . The codomain of acc ′ \operatorname{acc}' acc ′ is the state type itself. In the definition of a retractable fold, Err \operatorname{Err} Err belongs to the codomains of only retract \operatorname{retract} retract and eval ′ \operatorname{eval}' eval ′ . Hence s j s_j s j is a proper state, and in particular
[ R ] ω = s n ≠ Err . [R]_\omega=s_n\ne\operatorname{Err}. [ R ] ω = s n = Err .
Lemma C.3 (Reachability soundness). For every accumulation-only update sequence ω \omega ω ,
Reach ( ζ ( ω ) , [ R ] ω ) . \operatorname{Reach}(\zeta(\omega),[R]_\omega). Reach ( ζ ( ω ) , [ R ] ω ) . Proof. By induction on ω \omega ω .
Base case. For ω = [ ] \omega=[] ω = [ ] we have ζ ( ω ) = [ ] \zeta(\omega)=[] ζ ( ω ) = [ ] and [ R ] ω = I ′ [R]_\omega=I' [ R ] ω = I ′ , so rule R1 gives the claim.
Inductive step (accumulation). Let ω ′ = ω + + [ ( acc , x ) ] \omega'=\omega++[(\operatorname{acc},x)] ω ′ = ω + + [( acc , x )] with ω \omega ω accumulation-only, and let x s = ζ ( ω ) xs=\zeta(\omega) x s = ζ ( ω ) and s ′ = [ R ] ω s'=[R]_\omega s ′ = [ R ] ω . By Lemma C.2, s ′ ≠ Err s'\ne\operatorname{Err} s ′ = Err , so
ζ ( ω ′ ) = x s + + [ x ] , [ R ] ω ′ = acc ′ ( s ′ , x ) . \zeta(\omega')=xs++[x],\qquad[R]_{\omega'}=\operatorname{acc}'(s',x). ζ ( ω ′ ) = x s + + [ x ] , [ R ] ω ′ = acc ′ ( s ′ , x ) . The induction hypothesis gives Reach ( x s , s ′ ) \operatorname{Reach}(xs,s') Reach ( x s , s ′ ) . Instantiating rule R2 with this x s xs x s , this s ′ s' s ′ , and the element x x x yields Reach ( x s + + [ x ] , acc ′ ( s ′ , x ) ) \operatorname{Reach}(xs++[x],\operatorname{acc}'(s',x)) Reach ( x s + + [ x ] , acc ′ ( s ′ , x )) , which is the claim for ω ′ \omega' ω ′ .
Lemma C.4 (Existence of reachable insertion states). For every list x s xs x s , there exists a state s ′ ≠ Err s'\ne\operatorname{Err} s ′ = Err such that Reach ( x s , s ′ ) \operatorname{Reach}(xs,s') Reach ( x s , s ′ ) .
Proof. Let Canon ( x s ) \operatorname{Canon}(xs) Canon ( x s ) denote the accumulation-only update sequence that inserts the elements of x s xs x s in order, so that ζ ( Canon ( x s ) ) = x s \zeta(\operatorname{Canon}(xs))=xs ζ ( Canon ( x s )) = x s . Take s ′ = [ R ] Canon ( x s ) s'=[R]_{\operatorname{Canon}(xs)} s ′ = [ R ] Canon ( x s ) . Then s ′ ≠ Err s'\ne\operatorname{Err} s ′ = Err by Lemma C.2, and Lemma C.3 applied to Canon ( x s ) \operatorname{Canon}(xs) Canon ( x s ) gives Reach ( x s , s ′ ) \operatorname{Reach}(xs,s') Reach ( x s , s ′ ) .
Lemma C.5 (Lifting preserves semantics). For every accumulation-only update sequence ω \omega ω ,
⟦ P ⟧ ζ ( ω ) = ⟦ R ⟧ ω . \llbracket P \rrbracket_{\zeta}(\omega)=\llbracket R \rrbracket_{\omega}. [ [ P ] ] ζ ( ω ) = [ [ R ] ] ω . In particular, ⟦ R ⟧ ω ≠ E r r \llbracket R \rrbracket_{\omega} \ne\mathrm{Err} [ [ R ] ] ω = Err for every accumulation-only ω \omega ω .
Proof. We first show, by induction on ω \omega ω , that rules A1 and A2 imply, for every accumulation-only ω \omega ω ,
eval ( ⟦ P ⟧ ζ ( ω ) ) = eval ′ ( ⟦ R ⟧ ω ) . \operatorname{eval}(\llbracket P \rrbracket_{\zeta}(\omega))=\operatorname{eval}^{\prime}(\llbracket R \rrbracket_{\omega}). eval ( [ [ P ] ] ζ ( ω )) = eval ′ ( [ [ R ] ] ω ) .
Base case. For ω = [ ] \omega=[] ω = [ ] we have ⟦ P ⟧ ζ ( [ ] ) = I \llbracket P \rrbracket_{\zeta}([])=\mathcal{I} [ [ P ] ] ζ ([ ]) = I and ⟦ R ⟧ [ ] = I ′ \llbracket R \rrbracket_{[] }=\mathcal{I}^{\prime} [ [ R ] ] [ ] = I ′ , so the desired equality is exactly rule A1.
Inductive step (accumulation). Let ω ′ = ω + + [ ( acc , x ) ] \omega^{\prime}=\omega++[(\operatorname{acc},x)] ω ′ = ω + + [( acc , x )] with ω \omega ω accumulation-only, and let s = ⟦ P ⟧ ζ ( ω ) s=\llbracket P \rrbracket_{\zeta}(\omega) s = [ [ P ] ] ζ ( ω ) and s ′ = ⟦ R ⟧ ω s^{\prime}=\llbracket R \rrbracket_{\omega} s ′ = [ [ R ] ] ω . By Definition 3.1 and, since s ′ ≠ E r r s^{\prime}\ne\mathrm{Err} s ′ = Err by Lemma C.2, by Definition 3.5 ,
⟦ P ⟧ ζ ( ω ′ ) = acc ( s , x ) , ⟦ R ⟧ ω ′ = acc ′ ( s ′ , x ) . \llbracket P \rrbracket_{\zeta}(\omega^{\prime})=\operatorname{acc}(s,x),\qquad\llbracket R \rrbracket_{\omega^{\prime}}=\operatorname{acc}^{\prime}(s^{\prime},x). [ [ P ] ] ζ ( ω ′ ) = acc ( s , x ) , [ [ R ] ] ω ′ = acc ′ ( s ′ , x ) .
The induction hypothesis gives eval ( s ) = eval ′ ( s ′ ) \operatorname{eval}(s)=\operatorname{eval}^{\prime}(s^{\prime}) eval ( s ) = eval ′ ( s ′ ) , and rule A2 applied to this equality yields eval ( acc ( s , x ) ) = eval ′ ( acc ′ ( s ′ , x ) ) \operatorname{eval}(\operatorname{acc}(s,x))=\operatorname{eval}^{\prime}(\operatorname{acc}^{\prime}(s^{\prime},x)) eval ( acc ( s , x )) = eval ′ ( acc ′ ( s ′ , x )) , which is the desired equality for ω ′ \omega^{\prime} ω ′ .
Now let ω \omega ω be an arbitrary accumulation-only update sequence. By Lemma C.2, ⟦ R ⟧ ω ≠ E r r \llbracket R \rrbracket_{\omega}\ne\mathrm{Err} [ [ R ] ] ω = Err , so unfolding the observable semantics of Definition 3.5 gives ⟦ R ⟧ ω = eval ′ ( ⟦ R ⟧ ω ) \llbracket R \rrbracket_{\omega}=\operatorname{eval}^{\prime}(\llbracket R \rrbracket_{\omega}) [ [ R ] ] ω = eval ′ ( [ [ R ] ] ω ) . Likewise, by Definition 3.1 , ⟦ P ⟧ ζ ( ω ) = eval ( ⟦ P ⟧ ζ ( ω ) ) \llbracket P \rrbracket_{\zeta}(\omega)=\operatorname{eval}(\llbracket P \rrbracket_{\zeta}(\omega)) [ [ P ] ] ζ ( ω ) = eval ( [ [ P ] ] ζ ( ω )) . Combining these with the equality proved above yields ⟦ P ⟧ ζ ( ω ) = ⟦ R ⟧ ω \llbracket P \rrbracket_{\zeta}(\omega)=\llbracket R \rrbracket_{\omega} [ [ P ] ] ζ ( ω ) = [ [ R ] ] ω . The codomain of eval \operatorname{eval} eval is the output type O O O , which excludes E r r \mathrm{Err} Err . Hence
⟦ P ⟧ ζ ( ω ) ≠ E r r and ⟦ R ⟧ ω ≠ E r r . \llbracket P \rrbracket_{\zeta}(\omega)\ne\mathrm{Err}\quad\text{and}\quad\llbracket R \rrbracket_{\omega}\ne\mathrm{Err}. [ [ P ] ] ζ ( ω ) = Err and [ [ R ] ] ω = Err .
Lemma C.6 (Retraction consistency invariant). For every valid update sequence ω \omega ω with ⟦ R ⟧ ω ≠ E r r \llbracket R \rrbracket_{\omega}\ne\mathrm{Err} [ [ R ] ] ω = Err , there exists a state s 1 ′ s^{\prime}_{1} s 1 ′ such that
Reach ( Effect ( ω ) , s 1 ′ ) and Ψ ( Effect ( ω ) , s 1 ′ , ⟦ R ⟧ ω ) . \operatorname{Reach}(\operatorname{Effect}(\omega),s^{\prime}_{1})\quad\text{and}\quad\Psi(\operatorname{Effect}(\omega),s^{\prime}_{1},\llbracket R \rrbracket_{\omega}). Reach ( Effect ( ω ) , s 1 ′ ) and Ψ ( Effect ( ω ) , s 1 ′ , [ [ R ] ] ω ) .
Proof. By induction on ω \omega ω .
Base case. For ω = [ ] \omega=[] ω = [ ] we have Effect ( [ ] ) = [ ] \operatorname{Effect}([])=[] Effect ([ ]) = [ ] and ⟦ R ⟧ [ ] = I ′ \llbracket R \rrbracket_{[]}=\mathcal{I}^{\prime} [ [ R ] ] [ ] = I ′ . Take s 1 ′ = I ′ s^{\prime}_{1}=\mathcal{I}^{\prime} s 1 ′ = I ′ : rule R1 gives Reach ( [ ] , I ′ ) \operatorname{Reach}([],\mathcal{I}^{\prime}) Reach ([ ] , I ′ ) , and rule B1 gives Ψ ( [ ] , I ′ , I ′ ) \Psi([], \mathcal{I}^{\prime},\mathcal{I}^{\prime}) Ψ ([ ] , I ′ , I ′ ) .
Inductive step (accumulation). Let ω ′ = ω + + [ ( acc , x ) ] \omega^{\prime}=\omega++[(\operatorname{acc},x)] ω ′ = ω + + [( acc , x )] be valid with ⟦ R ⟧ ω ′ ≠ E r r \llbracket R \rrbracket_{\omega^{\prime}}\ne\mathrm{Err} [ [ R ] ] ω ′ = Err . Since E r r \mathrm{Err} Err is absorbing, ⟦ R ⟧ ω ≠ E r r \llbracket R \rrbracket_{\omega}\ne\mathrm{Err} [ [ R ] ] ω = Err , and ω \omega ω is valid as a prefix of ω ′ \omega^{\prime} ω ′ . Let x s = Effect ( ω ) x_s=\operatorname{Effect}(\omega) x s = Effect ( ω ) . The induction hypothesis yields a state s 1 ′ s^{\prime}_{1} s 1 ′ with Reach ( x s , s 1 ′ ) \operatorname{Reach}(x_s,s^{\prime}_{1}) Reach ( x s , s 1 ′ ) and Ψ ( x s , s 1 ′ , ⟦ R ⟧ ω ) \Psi(x_s,s^{\prime}_{1},\llbracket R \rrbracket_{\omega}) Ψ ( x s , s 1 ′ , [ [ R ] ] ω ) . By part (ii) of Lemma C.1 , Effect ( ω ′ ) = x s + + [ x ] \operatorname{Effect}(\omega^{\prime})=x_s++[x] Effect ( ω ′ ) = x s + + [ x ] , and by the semantics, ⟦ R ⟧ ω ′ = acc ′ ( ⟦ R ⟧ ω , x ) \llbracket R \rrbracket_{\omega^{\prime}}=\operatorname{acc}^{\prime}(\llbracket R \rrbracket_{\omega},x) [ [ R ] ] ω ′ = acc ′ ( [ [ R ] ] ω , x ) . Rule R2 gives Reach ( x s + + [ x ] , acc ′ ( s 1 ′ , x ) ) \operatorname{Reach}(x_s++[x],\operatorname{acc}^{\prime}(s^{\prime}_{1},x)) Reach ( x s + + [ x ] , acc ′ ( s 1 ′ , x )) , and rule B2 gives Ψ ( x s + + [ x ] , acc ′ ( s 1 ′ , x ) , acc ′ ( ⟦ R ⟧ ω , x ) ) \Psi(x_s++[x],\operatorname{acc}^{\prime}(s^{\prime}_{1},x),\operatorname{acc}^{\prime}(\llbracket R \rrbracket_{\omega},x)) Ψ ( x s + + [ x ] , acc ′ ( s 1 ′ , x ) , acc ′ ( [ [ R ] ] ω , x )) . Hence acc ′ ( s 1 ′ , x ) \operatorname{acc}^{\prime}(s^{\prime}_{1},x) acc ′ ( s 1 ′ , x ) witnesses the claim for ω ′ \omega^{\prime} ω ′ .
Inductive step (retraction). Let ω ′ = ω + + [ ( retract , x ) ] \omega^{\prime}=\omega++[(\operatorname{retract},x)] ω ′ = ω + + [( retract , x )] be valid with ⟦ R ⟧ ω ′ ≠ E r r \llbracket R \rrbracket_{\omega^{\prime}}\ne\mathrm{Err} [ [ R ] ] ω ′ = Err . As before, ⟦ R ⟧ ω ≠ E r r \llbracket R \rrbracket_{\omega}\ne\mathrm{Err} [ [ R ] ] ω = Err and ω \omega ω is valid. Write s 2 ′ = ⟦ R ⟧ ω s^{\prime}_{2}=\llbracket R \rrbracket_{\omega} s 2 ′ = [ [ R ] ] ω , x s = Effect ( ω ) x_s=\operatorname{Effect}(\omega) x s = Effect ( ω ) , and y s = remove ( x s , x ) y_s=\operatorname{remove}(x_s,x) y s = remove ( x s , x ) . The premises of rule B3 hold:
Reach ( x s , s 1 ′ ) \operatorname{Reach}(x_s,s^{\prime}_{1}) Reach ( x s , s 1 ′ ) and Ψ ( x s , s 1 ′ , s 2 ′ ) \Psi(x_s,s^{\prime}_{1},s^{\prime}_{2}) Ψ ( x s , s 1 ′ , s 2 ′ ) hold for the state s 1 ′ s^{\prime}_{1} s 1 ′ provided by the induction hypothesis applied to ω \omega ω ;
x ∈ x s x\in x_s x ∈ x s holds by part (iii) of Lemma C.1 , since ω + + [ ( retract , x ) ] \omega++[(\operatorname{retract},x)] ω + + [( retract , x )] is valid;
retract ( s 2 ′ , x ) ≠ E r r \operatorname{retract}(s^{\prime}_{2},x)\ne\mathrm{Err} retract ( s 2 ′ , x ) = Err holds because the semantics gives
⟦ R ⟧ ω ′ = retract ( s 2 ′ , x ) ≠ E r r \llbracket R \rrbracket_{\omega^{\prime}}=\operatorname{retract}(s^{\prime}_{2},x)\ne\mathrm{Err} [ [ R ] ] ω ′ = retract ( s 2 ′ , x ) = Err
by assumption;
y s = remove ( x s , x ) y_s=\operatorname{remove}(x_s,x) y s = remove ( x s , x ) holds by the definition of y s y_s y s ;
Reach ( y s , s 3 ′ ) \operatorname{Reach}(y_s,s^{\prime}_{3}) Reach ( y s , s 3 ′ ) holds for some state s 3 ′ s^{\prime}_{3} s 3 ′ by Lemma C.4.
Proc. ACM Program. Lang., Vol. 11, No. POPL, Article 30. Publication date: January 2027. Rule B3 therefore yields Ψ ( y s , s 3 ′ , retract ( s 2 ′ , x ) ) \Psi(y_s,s_3',\operatorname{retract}(s_2',x)) Ψ ( y s , s 3 ′ , retract ( s 2 ′ , x )) . By part (iii) of Lemma C.1 , y s = Effect ( ω ′ ) y_s=\operatorname{Effect}(\omega') y s = Effect ( ω ′ ) , and retract ( s 2 ′ , x ) = ⟦ R ⟧ ω ′ \operatorname{retract}(s_2',x)=\llbracket R\rrbracket_{\omega'} retract ( s 2 ′ , x ) = [ [ R ] ] ω ′ . Since s 3 ′ s_3' s 3 ′ also satisfies Reach ( Effect ( ω ′ ) , s 3 ′ ) \operatorname{Reach}(\operatorname{Effect}(\omega'),s_3') Reach ( Effect ( ω ′ ) , s 3 ′ ) , it witnesses the claim for ω ′ \omega' ω ′ . □ \square □
Lemma C.7 (Normalization preserves semantics). For every valid update sequence ω \omega ω with ⟦ R ⟧ ω ≠ E r r \llbracket R\rrbracket_\omega\ne\mathrm{Err} [ [ R ] ] ω = Err ,
⟦ R ⟧ Norm ( ω ) = ⟦ R ⟧ ω . \llbracket R\rrbracket_{\operatorname{Norm}(\omega)}=\llbracket R\rrbracket_\omega. [ [ R ] ] Norm ( ω ) = [ [ R ] ] ω .
Proof. We prove the following invariant by induction on ω \omega ω :
for every valid ω : ⟦ R ⟧ ω ≠ E r r ⟹ Ψ ( Effect ( ω ) , ⟦ R ⟧ Norm ( ω ) , ⟦ R ⟧ ω ) . (2) \text{for every valid }\omega:\quad\llbracket R\rrbracket_\omega\ne\mathrm{Err}\ \Longrightarrow\ \Psi(\operatorname{Effect}(\omega),\llbracket R\rrbracket_{\operatorname{Norm}(\omega)},\llbracket R\rrbracket_\omega). \tag*{(2)} for every valid ω : [ [ R ] ] ω = Err ⟹ Ψ ( Effect ( ω ) , [ [ R ] ] Norm ( ω ) , [ [ R ] ] ω ) . (2)
The middle argument of Ψ \Psi Ψ in (*) is a proper state because Norm ( ω ) \operatorname{Norm}(\omega) Norm ( ω ) is accumulation-only by part (i) of Lemma C.1 , so Lemma C.2 applies.
Base case. For ω = [ ] \omega=[] ω = [ ] we have Norm ( [ ] ) = [ ] \operatorname{Norm}([])=[] Norm ([ ]) = [ ] , Effect ( [ ] ) = [ ] \operatorname{Effect}([])=[] Effect ([ ]) = [ ] , and ⟦ R ⟧ [ ] = I ′ \llbracket R\rrbracket_{[]}=\mathcal{I}' [ [ R ] ] [ ] = I ′ , so rule B1 gives (*).
Inductive step (accumulation). Let ω ′ = ω + + [ ( a c c , x ) ] \omega'=\omega++[(\mathrm{acc},x)] ω ′ = ω + + [( acc , x )] be valid with ⟦ R ⟧ ω ′ ≠ E r r \llbracket R\rrbracket_{\omega'}\ne\mathrm{Err} [ [ R ] ] ω ′ = Err . Then ⟦ R ⟧ ω ≠ E r r \llbracket R\rrbracket_\omega\ne\mathrm{Err} [ [ R ] ] ω = Err since Err is absorbing, and ω \omega ω is valid as a prefix, so the induction hypothesis gives
Ψ ( Effect ( ω ) , ⟦ R ⟧ Norm ( ω ) , ⟦ R ⟧ ω ) . \Psi(\operatorname{Effect}(\omega),\llbracket R\rrbracket_{\operatorname{Norm}(\omega)},\llbracket R\rrbracket_\omega). Ψ ( Effect ( ω ) , [ [ R ] ] Norm ( ω ) , [ [ R ] ] ω ) .
By part (ii) of Lemma C.1 , Norm ( ω ′ ) = Norm ( ω ) + + [ ( a c c , x ) ] \operatorname{Norm}(\omega')=\operatorname{Norm}(\omega)++[(\mathrm{acc},x)] Norm ( ω ′ ) = Norm ( ω ) + + [( acc , x )] and Effect ( ω ′ ) = Effect ( ω ) + + [ x ] \operatorname{Effect}(\omega')=\operatorname{Effect}(\omega)++[x] Effect ( ω ′ ) = Effect ( ω ) + + [ x ] . Since both ⟦ R ⟧ Norm ( ω ) \llbracket R\rrbracket_{\operatorname{Norm}(\omega)} [ [ R ] ] Norm ( ω ) and ⟦ R ⟧ ω \llbracket R\rrbracket_\omega [ [ R ] ] ω are non-Err, the semantics gives
⟦ R ⟧ Norm ( ω ′ ) = acc ′ ( ⟦ R ⟧ Norm ( ω ) , x ) , ⟦ R ⟧ ω ′ = acc ′ ( ⟦ R ⟧ ω , x ) . \llbracket R\rrbracket_{\operatorname{Norm}(\omega')}=\operatorname{acc}'(\llbracket R\rrbracket_{\operatorname{Norm}(\omega)},x),\qquad
\llbracket R\rrbracket_{\omega'}=\operatorname{acc}'(\llbracket R\rrbracket_\omega,x). [ [ R ] ] Norm ( ω ′ ) = acc ′ ( [ [ R ] ] Norm ( ω ) , x ) , [ [ R ] ] ω ′ = acc ′ ( [ [ R ] ] ω , x ) .
Applying rule B2 to the induction hypothesis yields exactly (*) for ω ′ \omega' ω ′ .
Inductive step (retraction). Let ω ′ = ω + + [ ( r e t r a c t , x ) ] \omega'=\omega++[(\mathrm{retract},x)] ω ′ = ω + + [( retract , x )] be valid with ⟦ R ⟧ ω ′ ≠ E r r \llbracket R\rrbracket_{\omega'}\ne\mathrm{Err} [ [ R ] ] ω ′ = Err . Then ⟦ R ⟧ ω ≠ E r r \llbracket R\rrbracket_\omega\ne\mathrm{Err} [ [ R ] ] ω = Err and ω \omega ω is valid. Write s 2 ′ = ⟦ R ⟧ ω s_2'=\llbracket R\rrbracket_\omega s 2 ′ = [ [ R ] ] ω , x s = Effect ( ω ) x_s=\operatorname{Effect}(\omega) x s = Effect ( ω ) , and y s = remove ( x s , x ) y_s=\operatorname{remove}(x_s,x) y s = remove ( x s , x ) . We instantiate rule B3 with s 1 ′ = ⟦ R ⟧ Norm ( ω ) s_1'=\llbracket R\rrbracket_{\operatorname{Norm}(\omega)} s 1 ′ = [ [ R ] ] Norm ( ω ) and s 3 ′ = ⟦ R ⟧ Norm ( ω ′ ) s_3'=\llbracket R\rrbracket_{\operatorname{Norm}(\omega')} s 3 ′ = [ [ R ] ] Norm ( ω ′ ) and discharge its premises one by one:
Reach ( x s , s 1 ′ ) \operatorname{Reach}(x_s,s_1') Reach ( x s , s 1 ′ ) : by part (i) of Lemma C.1 , Norm ( ω ) \operatorname{Norm}(\omega) Norm ( ω ) is accumulation-only with ζ ( Norm ( ω ) ) = x s \zeta(\operatorname{Norm}(\omega))=x_s ζ ( Norm ( ω )) = x s , so Lemma C.3 applies;
Ψ ( x s , s 1 ′ , s 2 ′ ) \Psi(x_s,s_1',s_2') Ψ ( x s , s 1 ′ , s 2 ′ ) : by the induction hypothesis for ω \omega ω ;
x ∈ x s x\in x_s x ∈ x s : by part (iii) of Lemma C.1 , since ω ′ \omega' ω ′ is valid;
retract ( s 2 ′ , x ) ≠ E r r \operatorname{retract}(s_2',x)\ne\mathrm{Err} retract ( s 2 ′ , x ) = Err : the semantics gives ⟦ R ⟧ ω ′ = retract ( s 2 ′ , x ) ≠ E r r \llbracket R\rrbracket_{\omega'}=\operatorname{retract}(s_2',x)\ne\mathrm{Err} [ [ R ] ] ω ′ = retract ( s 2 ′ , x ) = Err by assumption;
y s = remove ( x s , x ) y_s=\operatorname{remove}(x_s,x) y s = remove ( x s , x ) : by the definition of y s y_s y s ;
Reach ( y s , s 3 ′ ) \operatorname{Reach}(y_s,s_3') Reach ( y s , s 3 ′ ) : by parts (i) and (iii) of Lemma C.1 , Norm ( ω ′ ) \operatorname{Norm}(\omega') Norm ( ω ′ ) is accumulation-only with ζ ( Norm ( ω ′ ) ) = Effect ( ω ′ ) = y s \zeta(\operatorname{Norm}(\omega'))=\operatorname{Effect}(\omega')=y_s ζ ( Norm ( ω ′ )) = Effect ( ω ′ ) = y s , so Lemma C.3 applies.
Rule B3 yields Ψ ( y s , s 3 ′ , retract ( s 2 ′ , x ) ) \Psi(y_s,s_3',\operatorname{retract}(s_2',x)) Ψ ( y s , s 3 ′ , retract ( s 2 ′ , x )) . Since y s = Effect ( ω ′ ) y_s=\operatorname{Effect}(\omega') y s = Effect ( ω ′ ) , s 3 ′ = ⟦ R ⟧ Norm ( ω ′ ) s_3'=\llbracket R\rrbracket_{\operatorname{Norm}(\omega')} s 3 ′ = [ [ R ] ] Norm ( ω ′ ) , and retract ( s 2 ′ , x ) = ⟦ R ⟧ ω ′ \operatorname{retract}(s_2',x)=\llbracket R\rrbracket_{\omega'} retract ( s 2 ′ , x ) = [ [ R ] ] ω ′ , we obtain (*) for ω ′ \omega' ω ′ , completing the induction.
Now fix a valid ω \omega ω with ⟦ R ⟧ ω ≠ E r r \llbracket R\rrbracket_\omega\ne\mathrm{Err} [ [ R ] ] ω = Err . Applying rule B4 to (*) gives
eval ′ ( ⟦ R ⟧ Norm ( ω ) ) = eval ′ ( ⟦ R ⟧ ω ) . \operatorname{eval}'(\llbracket R\rrbracket_{\operatorname{Norm}(\omega)})=\operatorname{eval}'(\llbracket R\rrbracket_\omega). eval ′ ( [ [ R ] ] Norm ( ω ) ) = eval ′ ( [ [ R ] ] ω ) .
Both state arguments are non-Err: the right one by assumption, and the left one by Lemma C.2, because Norm ( ω ) \operatorname{Norm}(\omega) Norm ( ω ) is accumulation-only. Unfolding the observable semantics of Definition 3.5 on both sides gives ⟦ R ⟧ Norm ( ω ) = ⟦ R ⟧ ω \llbracket R\rrbracket_{\operatorname{Norm}(\omega)}=\llbracket R\rrbracket_\omega [ [ R ] ] Norm ( ω ) = [ [ R ] ] ω . □ \square □
Lemma C.8 (Budget invariant). For every valid update sequence ω \omega ω with ⟦ R ⟧ ω ≠ E r r \llbracket R\rrbracket_\omega\ne\mathrm{Err} [ [ R ] ] ω = Err ,
Ω ( ⟦ R ⟧ ω + , ⟦ R ⟧ ω , ∣ ω − ∣ ) . \Omega(\llbracket R\rrbracket_{\omega^+},\llbracket R\rrbracket_\omega,\lvert\omega^-\rvert). Ω ( [ [ R ] ] ω + , [ [ R ] ] ω , ∣ ω − ∣) .
Proof. We proceed by induction on ω \omega ω . The index of Ω \Omega Ω counts the retractions separating the insertion-only replay ω + \omega^+ ω + from the mixed execution ω \omega ω . The sequence ω + \omega^+ ω + is accumulation-only, so
⟦ R ⟧ ω + ≠ E r r \llbracket R\rrbracket_{\omega^+}\ne\mathrm{Err} [ [ R ] ] ω + = Err
by Lemma C.2.
Base case. For ω = [ ] \omega= [] ω = [ ] we have ω + = [ ] \omega^+ = [] ω + = [ ] and ω − = [ ] \omega^- = [] ω − = [ ] , so both state arguments are I ′ I' I ′ and ∣ ω − ∣ = 0 |\omega^-| = 0 ∣ ω − ∣ = 0 . Rule C1 shows the claim.
Inductive step (accumulation). Let ω ′ = ω + + [ ( a c c , x ) ] \omega' = \omega^{++}[(\mathrm{acc},x)] ω ′ = ω ++ [( acc , x )] be valid with [ R ] ω ′ ≠ E r r [R]_{\omega'} \ne\mathrm{Err} [ R ] ω ′ = Err . Then [ R ] ω ≠ E r r [R]_{\omega} \ne\mathrm{Err} [ R ] ω = Err since Err is absorbing, and ω \omega ω is valid as a prefix, so the induction hypothesis gives
Ω ( [ R ] ω + , [ R ] ω , ∣ ω − ∣ ) . \Omega([R]_{\omega^+},[R]_{\omega},|\omega^-|). Ω ([ R ] ω + , [ R ] ω , ∣ ω − ∣ ) .
We have ω ′ + = ω + + + [ ( a c c , x ) ] \omega'^+ = \omega^{+++}[(\mathrm{acc},x)] ω ′ + = ω +++ [( acc , x )] and ω ′ − = ω − \omega'^- = \omega^- ω ′ − = ω − . Since [ R ] ω + ≠ E r r [R]_{\omega^+} \ne\mathrm{Err} [ R ] ω + = Err and [ R ] ω ≠ E r r [R]_{\omega} \ne\mathrm{Err} [ R ] ω = Err , the semantics gives [ R ] ω ′ + = acc ′ ( [ R ] ω + , x ) [R]_{\omega'^+} = \operatorname{acc}'([R]_{\omega^+},x) [ R ] ω ′ + = acc ′ ([ R ] ω + , x ) and [ R ] ω ′ = acc ′ ( [ R ] ω , x ) [R]_{\omega'} = \operatorname{acc}'([R]_{\omega},x) [ R ] ω ′ = acc ′ ([ R ] ω , x ) . Applying rule C2 to the induction hypothesis yields
Ω ( acc ′ ( [ R ] ω + , x ) , acc ′ ( [ R ] ω , x ) , ∣ ω − ∣ ) . \Omega(\operatorname{acc}'([R]_{\omega^+},x),\operatorname{acc}'([R]_{\omega},x),|\omega^-|). Ω ( acc ′ ([ R ] ω + , x ) , acc ′ ([ R ] ω , x ) , ∣ ω − ∣ ) .
which proves the claim for ω ′ \omega' ω ′ .
Inductive step (retraction). Let ω ′ = ω + + [ ( r e t r a c t , x ) ] \omega' = \omega^{++}[(\mathrm{retract},x)] ω ′ = ω ++ [( retract , x )] be valid with [ R ] ω ′ ≠ E r r [R]_{\omega'} \ne\mathrm{Err} [ R ] ω ′ = Err . Then [ R ] ω ≠ E r r [R]_{\omega} \ne\mathrm{Err} [ R ] ω = Err and ω \omega ω is valid. Write s 2 ′ = [ R ] ω s'_2 = [R]_{\omega} s 2 ′ = [ R ] ω and x s = Effect ( ω ) xs = \operatorname{Effect}(\omega) x s = Effect ( ω ) . The semantics gives [ R ] ω ′ = retract ( s 2 ′ , x ) ≠ E r r [R]_{\omega'} = \operatorname{retract}(s'_2,x) \ne\mathrm{Err} [ R ] ω ′ = retract ( s 2 ′ , x ) = Err , and part (iii) of Lemma C.1 gives x ∈ x s x \in xs x ∈ x s . By Lemma C.6 applied to ω \omega ω , there is a state s 3 ′ s'_3 s 3 ′ with Ψ ( x s , s 3 ′ , s 2 ′ ) \Psi(xs,s'_3,s'_2) Ψ ( x s , s 3 ′ , s 2 ′ ) . Together with the induction hypothesis Ω ( [ R ] ω + , s 2 ′ , ∣ ω − ∣ ) \Omega([R]_{\omega^+},s'_2,|\omega^-|) Ω ([ R ] ω + , s 2 ′ , ∣ ω − ∣ ) , all premises of rule C3 are discharged, and the rule yields
Ω ( [ R ] ω + , retract ( s 2 ′ , x ) , ∣ ω − ∣ + 1 ) . \Omega([R]_{\omega^+},\operatorname{retract}(s'_2,x),|\omega^-|+1). Ω ([ R ] ω + , retract ( s 2 ′ , x ) , ∣ ω − ∣ + 1 ) .
Since ω ′ + = ω + \omega'^+ = \omega^+ ω ′ + = ω + , ∣ ω ′ − ∣ = ∣ ω − ∣ + 1 |\omega'^-| = |\omega^-|+1 ∣ ω ′ − ∣ = ∣ ω − ∣ + 1 , and [ R ] ω ′ = retract ( s 2 ′ , x ) [R]_{\omega'} = \operatorname{retract}(s'_2,x) [ R ] ω ′ = retract ( s 2 ′ , x ) , this proves the claim for ω ′ \omega' ω ′ .
Lemma C.9 (Budget support). Let ω \omega ω be a valid update sequence such that every prefix δ ⪯ ω \delta\preceq\omega δ ⪯ ω satisfies ∣ δ − ∣ ≤ θ ( ζ ( δ + ) ) |\delta^-| \le\theta(\zeta(\delta^+)) ∣ δ − ∣ ≤ θ ( ζ ( δ + )) . Then [ R ] ω ≠ E r r [R]_{\omega} \ne\mathrm{Err} [ R ] ω = Err .
Proof. Suppose, toward a contradiction, that [ R ] ω = E r r [R]_{\omega} = \mathrm{Err} [ R ] ω = Err , and let s 0 , … , s m s_0,\ldots,s_m s 0 , … , s m be the state sequence of Definition 3.5 . Since s 0 = I ′ ≠ E r r s_0 = I' \ne\mathrm{Err} s 0 = I ′ = Err , there is a least index j j j with s j = E r r s_j = \mathrm{Err} s j = Err , and then s j − 1 ≠ E r r s_{j-1} \ne\mathrm{Err} s j − 1 = Err . If o j = a c c o_j = \mathrm{acc} o j = acc , then s j = acc ′ ( s j − 1 , a j ) s_j = \operatorname{acc}'(s_{j-1},a_j) s j = acc ′ ( s j − 1 , a j ) is a proper state because acc ′ \operatorname{acc}' acc ′ has the state type as its codomain (as in Lemma C.2). Hence o j = r e t r a c t o_j = \mathrm{retract} o j = retract , so ω \omega ω has a prefix ω 0 = δ + + [ ( r e t r a c t , x ) ] \omega_0 = \delta^{++}[(\mathrm{retract},x)] ω 0 = δ ++ [( retract , x )] such that s 2 ′ ≔ [ R ] δ = s j − 1 ≠ E r r s'_2 \coloneqq[R]_{\delta} = s_{j-1} \ne\mathrm{Err} s 2 ′ : = [ R ] δ = s j − 1 = Err and retract ( s 2 ′ , x ) = E r r \operatorname{retract}(s'_2,x) = \mathrm{Err} retract ( s 2 ′ , x ) = Err . Both δ \delta δ and ω 0 \omega_0 ω 0 are valid, being prefixes of ω \omega ω . Define
y s ≜ ζ ( δ + ) , x s ≜ Effect ( δ ) , n ≜ ∣ δ − ∣ . ys \triangleq\zeta(\delta^+), \qquad xs \triangleq\operatorname{Effect}(\delta), \qquad n \triangleq|\delta^-|. y s ≜ ζ ( δ + ) , x s ≜ Effect ( δ ) , n ≜ ∣ δ − ∣.
The premises of rule C4 hold with s 1 ′ = [ R ] δ + s'_1 = [R]_{\delta^+} s 1 ′ = [ R ] δ + and the state s 3 ′ s'_3 s 3 ′ defined below:
Reach ( y s , s 1 ′ ) \operatorname{Reach}(ys,s'_1) Reach ( y s , s 1 ′ ) : the sequence δ + \delta^+ δ + is accumulation-only with ζ ( δ + ) = y s \zeta(\delta^+) = ys ζ ( δ + ) = y s , so Lemma C.3 applies;
Ω ( s 1 ′ , s 2 ′ , n ) \Omega(s'_1,s'_2,n) Ω ( s 1 ′ , s 2 ′ , n ) : by Lemma C.8 applied to δ \delta δ , using [ R ] δ ≠ E r r [R]_{\delta} \ne\mathrm{Err} [ R ] δ = Err ;
Ψ ( x s , s 3 ′ , s 2 ′ ) \Psi(xs,s'_3,s'_2) Ψ ( x s , s 3 ′ , s 2 ′ ) : by Lemma C.6 applied to δ \delta δ , which yields a state s 3 ′ s'_3 s 3 ′ with Reach ( x s , s 3 ′ ) \operatorname{Reach}(xs,s'_3) Reach ( x s , s 3 ′ ) and Ψ ( x s , s 3 ′ , s 2 ′ ) \Psi(xs,s'_3,s'_2) Ψ ( x s , s 3 ′ , s 2 ′ ) ;
DeletesTo ( y s , x s , n ) \operatorname{DeletesTo}(ys,xs,n) DeletesTo ( y s , x s , n ) : by part (iv) of Lemma C.1 applied to δ − \delta^- δ − ;
x ∈ x s x \in xs x ∈ x s : by part (iii) of Lemma C.1 , since ω 0 = δ + + [ ( r e t r a c t , x ) ] \omega_0 = \delta^{++}[(\mathrm{retract},x)] ω 0 = δ ++ [( retract , x )] is valid;
n < θ ( y s ) n < \theta(ys) n < θ ( y s ) : applying the budget hypothesis to the prefix ω 0 \omega_0 ω 0 gives n + 1 = ∣ ω 0 − ∣ ≤ θ ( ζ ( ω 0 + ) ) = θ ( y s ) n+1 = |\omega_0^-| \le\theta(\zeta(\omega_0^+)) = \theta(ys) n + 1 = ∣ ω 0 − ∣ ≤ θ ( ζ ( ω 0 + )) = θ ( y s ) , where the last equality holds because ω 0 + = δ + \omega_0^+ = \delta^+ ω 0 + = δ + .
Rule C4 therefore yields retract ( s 2 ′ , x ) ≠ E r r \operatorname{retract}(s'_2,x) \ne\mathrm{Err} retract ( s 2 ′ , x ) = Err , contradicting the choice of ω 0 \omega_0 ω 0 . Thus
[ R ] ω ≠ E r r . [R]_{\omega} \ne\mathrm{Err}. [ R ] ω = Err .
Theorem C.10 (Soundness). If there exist predicates Reach \operatorname{Reach} Reach , Ψ \Psi Ψ , and Ω \Omega Ω satisfying all rules in Figure 3 , then P ⊨ θ R \mathcal{P} \models_{\theta} \mathcal{R} P ⊨ θ R .
Proof. By Definition 3.9 , we must establish (1) R ⊨ θ \mathcal{R} \models\theta R ⊨ θ , and (2) semantic agreement on all valid update sequences that do not produce Err. (1) By Definition 3.6 , we must show that ⟦ R ⟧ ω ≠ E r r \llbracket R \rrbracket_{\omega} \ne\mathrm{Err} [ [ R ] ] ω = Err for every valid update sequence ω \omega ω whose prefixes respect the budget. So fix an arbitrary valid ω \omega ω such that every prefix δ ⪯ ω \delta\preceq\omega δ ⪯ ω satisfies ∣ δ − ∣ ≤ θ ( ζ ( δ + ) ) \lvert\delta^{-} \rvert\le\theta(\zeta(\delta^{+})) ∣ δ − ∣ ≤ θ ( ζ ( δ + )) . By Lemma C.9 , ⟦ R ⟧ ω ≠ E r r \llbracket R \rrbracket_{\omega} \ne\mathrm{Err} [ [ R ] ] ω = Err . It remains to rule out the possibility that eval ′ \operatorname{eval}' eval ′ itself returns Err on the final state. By Lemma C.7 , ⟦ R ⟧ ω = ⟦ R ⟧ Norm ( ω ) \llbracket R \rrbracket_{\omega} = \llbracket R \rrbracket_{\operatorname{Norm}(\omega)} [ [ R ] ] ω = [ [ R ] ] Norm ( ω ) . By part (i) of Lemma C.1 , Norm ( ω ) \operatorname{Norm}(\omega) Norm ( ω ) is accumulation-only with ζ ( Norm ( ω ) ) = Effect ( ω ) \zeta(\operatorname{Norm}(\omega)) = \operatorname{Effect}(\omega) ζ ( Norm ( ω )) = Effect ( ω ) , so Lemma C.5 gives ⟦ R ⟧ Norm ( ω ) = ⟦ P ⟧ Effect ( ω ) \llbracket R \rrbracket_{\operatorname{Norm}(\omega)} = \llbracket P \rrbracket_{\operatorname{Effect}(\omega)} [ [ R ] ] Norm ( ω ) = [ [ P ] ] Effect ( ω ) . Finally, by Definition 3.1 , ⟦ P ⟧ Effect ( ω ) = eval ( ⟦ P ⟧ Effect ( ω ) ) \llbracket P \rrbracket_{\operatorname{Effect}(\omega)} = \operatorname{eval}(\llbracket P \rrbracket_{\operatorname{Effect}(\omega)}) [ [ P ] ] Effect ( ω ) = eval ( [ [ P ] ] Effect ( ω ) ) , and the output type O O O excludes Err. Chaining these equalities, ⟦ R ⟧ ω = ⟦ P ⟧ Effect ( ω ) ≠ E r r \llbracket R \rrbracket_{\omega} = \llbracket P \rrbracket_{\operatorname{Effect}(\omega)} \ne\mathrm{Err} [ [ R ] ] ω = [ [ P ] ] Effect ( ω ) = Err . Since ω \omega ω was arbitrary, R ⊨ θ R \models\theta R ⊨ θ .
(2) Let ω \omega ω be an arbitrary valid update sequence with x s = Effect ( ω ) x_s = \operatorname{Effect}(\omega) x s = Effect ( ω ) and ⟦ R ⟧ ω ≠ E r r \llbracket R \rrbracket_{\omega} \ne\mathrm{Err} [ [ R ] ] ω = Err . By Definition 3.5 , ⟦ R ⟧ ω ≠ E r r \llbracket R \rrbracket_{\omega} \ne\mathrm{Err} [ [ R ] ] ω = Err , since otherwise ⟦ R ⟧ ω \llbracket R \rrbracket_{\omega} [ [ R ] ] ω would be Err. By Lemma C.7 , ⟦ R ⟧ ω = ⟦ R ⟧ Norm ( ω ) \llbracket R \rrbracket_{\omega} = \llbracket R \rrbracket_{\operatorname{Norm}(\omega)} [ [ R ] ] ω = [ [ R ] ] Norm ( ω ) , and, as in part (1), Lemma C.5 applied to the accumulation-only sequence Norm ( ω ) \operatorname{Norm}(\omega) Norm ( ω ) gives ⟦ R ⟧ Norm ( ω ) = ⟦ P ⟧ x s \llbracket R \rrbracket_{\operatorname{Norm}(\omega)} = \llbracket P \rrbracket_{x_s} [ [ R ] ] Norm ( ω ) = [ [ P ] ] x s . Hence
⟦ R ⟧ ω = ⟦ R ⟧ Norm ( ω ) = ⟦ P ⟧ x s , \llbracket R \rrbracket_{\omega} = \llbracket R \rrbracket_{\operatorname{Norm}(\omega)} = \llbracket P \rrbracket_{x_s}, [ [ R ] ] ω = [ [ R ] ] Norm ( ω ) = [ [ P ] ] x s ,
which is precisely clause (2) of Definition 3.9 . □ \square □
Completeness proof
This subsection elaborates on the relative completeness part of Theorem 4.2 . We fix an insertion-only fold P = ( I , acc , eval ) P = (I, \operatorname{acc}, \operatorname{eval}) P = ( I , acc , eval ) , a retractable fold R = ( I ′ , acc ′ , retract , eval ′ ) R = (I', \operatorname{acc}', \operatorname{retract}, \operatorname{eval}') R = ( I ′ , acc ′ , retract , eval ′ ) , and a retraction specification θ \theta θ as in § 3 , and we assume throughout that P ≡ θ R P \equiv_{\theta} R P ≡ θ R in the sense of Definition 3.9 . We construct predicates Reach, Ψ \Psi Ψ , and Ω \Omega Ω under which every rule of Figure 3 is valid, where a rule is valid if every instantiation of its metavariables that satisfies all premises also satisfies the conclusion. The construction is relative to the assumptions stated below. These assumptions correspond to the two conditions summarized after Theorem 4.2 : (a) logical expressiveness (Assumption A.3) and (b) content-determined behavior (Assumption A.2), together with an accumulator congruence condition (Assumption A.1) corresponding to Block A.
Preliminaries. Recall that Canon ( x s ) \operatorname{Canon}(x_s) Canon ( x s ) denotes the accumulation-only update sequence that inserts the elements of x s x_s x s in order, so that ζ ( Canon ( x s ) ) = x s \zeta(\operatorname{Canon}(x_s)) = x_s ζ ( Canon ( x s )) = x s . We collect the elementary facts about update sequences and normalization used below; they follow directly from Definitions 3.4 , 3.5 and 3.8 (cf. Lemmas C.1 and C.2).
(F1) Accumulation-only update sequences are valid and are fixpoints of Norm \operatorname{Norm} Norm ; in particular
Effect ( Canon ( x s ) ) = x s . \operatorname{Effect}(\operatorname{Canon}(x_s)) = x_s. Effect ( Canon ( x s )) = x s .
Moreover, ⟦ R ⟧ ω ≠ E r r \llbracket R \rrbracket_{\omega} \ne\mathrm{Err} [ [ R ] ] ω = Err for every accumulation-only ω \omega ω , because acc ′ \operatorname{acc}' acc ′ maps states to states. Under Definition 3.5 , retraction steps and absorption are the sources of Err.
(F2) If ω \omega ω is valid, then ω + + [ ( acc , x ) ] \omega++[(\operatorname{acc},x)] ω + + [( acc , x )] is valid and Effect ( ω + + [ ( acc , x ) ] ) = Effect ( ω ) + + [ x ] \operatorname{Effect}(\omega++[(\operatorname{acc},x)]) = \operatorname{Effect}(\omega)++[x] Effect ( ω + + [( acc , x )]) = Effect ( ω ) + + [ x ] .
(F3) If ω \omega ω is valid, then ω + + [ ( retract , x ) ] \omega++[(\operatorname{retract},x)] ω + + [( retract , x )] is valid iff x ∈ Effect ( ω ) x \in\operatorname{Effect}(\omega) x ∈ Effect ( ω ) , and in that case
Effect ( ω + + [ ( retract , x ) ] ) = remove ( Effect ( ω ) , x ) . \operatorname{Effect}(\omega++[(\operatorname{retract},x)]) = \operatorname{remove}(\operatorname{Effect}(\omega),x). Effect ( ω + + [( retract , x )]) = remove ( Effect ( ω ) , x ) .
(F4) Err is absorbing. In particular, if ⟦ R ⟧ ω ≠ E r r \llbracket R \rrbracket_{\omega} \ne\mathrm{Err} [ [ R ] ] ω = Err , then ⟦ R ⟧ δ ≠ E r r \llbracket R \rrbracket_{\delta} \ne\mathrm{Err} [ [ R ] ] δ = Err for every prefix δ ⪯ ω \delta\preceq\omega δ ⪯ ω .
(F5) We read the side predicate DeletesTo of Figure 3 as follows: DeletesTo ( y s , x s , n ) \operatorname{DeletesTo}(y_s,x_s,n) DeletesTo ( y s , x s , n ) holds iff there are values z 1 , … , z n z_1,\ldots,z_n z 1 , … , z n such that x s = remove ( ⋯ remove ( remove ( y s , z 1 ) , z 2 ) ⋯ , z n ) x_s = \operatorname{remove}(\cdots\operatorname{remove}(\operatorname{remove}(y_s,z_1),z_2)\cdots,z_n) x s = remove ( ⋯ remove ( remove ( y s , z 1 ) , z 2 ) ⋯ , z n ) , where each intermediate application of remove \operatorname{remove} remove is defined, i.e., z i z_i z i occurs in the list it is applied to. This formalizes the caption of Figure 3 (x s x_s x s is obtained from y s y_s y s by deleting exactly n n n elements, one occurrence per deleted element). The soundness proof uses this form when normalization deletes one accumulated occurrence per retraction.
Assumption A.1 (Accumulator congruence). The insertion-only components of the two folds are observationally congruent: eval ( I ) = eval ′ ( I ′ ) \operatorname{eval}(\mathcal{I}) = \operatorname{eval}'(\mathcal{I}') eval ( I ) = eval ′ ( I ′ ) , and for all states s , s ′ s,s' s , s ′ and elements a a a ,
eval ( s ) = eval ′ ( s ′ ) ⟹ eval ( acc ( s , a ) ) = eval ′ ( acc ′ ( s ′ , a ) ) . \operatorname{eval}(s) = \operatorname{eval}'(s') \implies\operatorname{eval}(\operatorname{acc}(s,a)) = \operatorname{eval}'(\operatorname{acc}'(s',a)). eval ( s ) = eval ′ ( s ′ ) ⟹ eval ( acc ( s , a )) = eval ′ ( acc ′ ( s ′ , a )) .
The assumption states that inserting the same element preserves observational agreement between original and retractable states. Rules A1 and A2 quantify over all states and are independent of Reach, Ψ \Psi Ψ , and Ω \Omega Ω . Their validity is therefore equivalent to Assumption A.1 for every definition of the three semantic invariants.
Assumption A.2 (Content-determined behavior). For all valid update sequences ω 1 , ω 2 \omega_1,\omega_2 ω 1 , ω 2 with [ R ] ω 1 ≠ E r r [R]_{\omega_1} \ne\mathrm{Err} [ R ] ω 1 = Err , [ R ] ω 2 ≠ E r r [R]_{\omega_2} \ne\mathrm{Err} [ R ] ω 2 = Err , and Effect ( ω 1 ) = Effect ( ω 2 ) = x s \operatorname{Effect}(\omega_1) = \operatorname{Effect}(\omega_2) = xs Effect ( ω 1 ) = Effect ( ω 2 ) = x s , the following hold:
(a) eval ′ ( [ R ] ω 1 ) = eval ′ ( [ R ] ω 2 ) \operatorname{eval}'([R]_{\omega_1}) = \operatorname{eval}'([R]_{\omega_2}) eval ′ ([ R ] ω 1 ) = eval ′ ([ R ] ω 2 ) ;
(b) for every x ∈ x s x \in xs x ∈ x s , retract ( [ R ] ω 1 , x ) ≠ E r r \operatorname{retract}([R]_{\omega_1},x) \ne\mathrm{Err} retract ([ R ] ω 1 , x ) = Err iff retract ( [ R ] ω 2 , x ) ≠ E r r \operatorname{retract}([R]_{\omega_2},x) \ne\mathrm{Err} retract ([ R ] ω 2 , x ) = Err .
The assumption requires histories with the same active content to have the same observable output and one-step retraction support. Whenever [ R ] ω 1 ≠ E r r [R]_{\omega_1} \ne\mathrm{Err} [ R ] ω 1 = Err and [ R ] ω 2 ≠ E r r [R]_{\omega_2} \ne\mathrm{Err} [ R ] ω 2 = Err , clause (2) of Definition 3.9 already forces both sides of part (a) to equal [ [ P ] ] x s [\![P]\!]_{xs} [ [ P ] ] x s . Part (a) additionally requires that whether eval ′ \operatorname{eval}' eval ′ itself fails is content-determined, and part (b) states the analogous requirement for the definedness of a single retraction. This extra strength matters because a valid history may exceed the retraction budget promised by θ \theta θ and yet be successful; on the future failure behavior of such histories, Definition 3.9 is silent.
Assumption A.3 (Logical expressiveness). The assertion logic is expressive enough to represent semantic relations over retractable fold states, lists of elements, and natural numbers. More precisely, for each of the three semantic relations defined in the proof below, there exists a predicate of the assertion language with the same truth set. As noted after Theorem 4.2 , automation also requires decidability of the resulting proof obligations. The existence result below uses expressiveness.
Theorem C.11 (Relative completeness). Suppose P ≡ θ R P \equiv_{\theta} R P ≡ θ R and Assumptions A.1–A.3 hold. Then there exist predicates Reach, Ψ \Psi Ψ , and Ω \Omega Ω such that all proof rules in Figure 3 are valid.
Proof. We define Reach, Ψ \Psi Ψ , and Ω \Omega Ω as semantic relations and verify each rule of Figure 3 . By Assumption A.3, each relation can then be replaced by an assertion of the proof logic with the same truth set, which yields the theorem.
Reachability Reach (Block R R R ). Define
Reach ( x s , s ′ ) ⟺ s ′ = [ R ] Canon ( x s ) . \operatorname{Reach}(xs,s') \Longleftrightarrow s' = [R]_{\operatorname{Canon}(xs)}. Reach ( x s , s ′ ) ⟺ s ′ = [ R ] Canon ( x s ) .
R 1 R1 R 1 . We have Canon ( [ ] ) = [ ] \operatorname{Canon}([]) = [] Canon ([ ]) = [ ] and [ R ] [ ] = I [R]_{[]} = I [ R ] [ ] = I , so Reach ( [ ] , I ) \operatorname{Reach}([],I) Reach ([ ] , I ) holds.
R 2 R2 R 2 . Suppose Reach ( x s , s ′ ) \operatorname{Reach}(xs,s') Reach ( x s , s ′ ) , i.e., s ′ = [ R ] Canon ( x s ) s' = [R]_{\operatorname{Canon}(xs)} s ′ = [ R ] Canon ( x s ) . Since Canon ( x s + + [ a ] ) = Canon ( x s ) + + [ ( acc , a ) ] \operatorname{Canon}(xs{+\!\!+}[a]) = \operatorname{Canon}(xs){+\!\!+}[(\operatorname{acc},a)] Canon ( x s + + [ a ]) = Canon ( x s ) + + [( acc , a )] and [ R ] Canon ( x s ) ≠ E r r [R]_{\operatorname{Canon}(xs)} \ne\mathrm{Err} [ R ] Canon ( x s ) = Err by (F1), the semantics unfolds to
[ R ] Canon ( x s + + [ a ] ) = acc ′ ( [ R ] Canon ( x s ) , a ) = acc ′ ( s ′ , a ) , [R]_{\operatorname{Canon}(xs{+\!\!+}[a])} = \operatorname{acc}'([R]_{\operatorname{Canon}(xs)},a) = \operatorname{acc}'(s',a), [ R ] Canon ( x s + + [ a ]) = acc ′ ([ R ] Canon ( x s ) , a ) = acc ′ ( s ′ , a ) ,
so Reach ( x s + + [ a ] , acc ′ ( s ′ , a ) ) \operatorname{Reach}(xs{+\!\!+}[a],\operatorname{acc}'(s',a)) Reach ( x s + + [ a ] , acc ′ ( s ′ , a )) holds.
Lifting consistency (Block A A A ). Rule A1 is the first conjunct of Assumption A.1, and rule A2 is the second conjunct, instantiated with the states s , s ′ s,s' s , s ′ and the element a a a appearing in the rule.
Retraction consistency Ψ \Psi Ψ (Block B B B ). Define
Ψ ( x s , s 1 ′ , s 2 ′ ) ⟺ Reach ( x s , s 1 ′ ) ∧ ∃ ω 2 . ω 2 valid ∧ Effect ( ω 2 ) = x s ∧ [ R ] ω 2 = s 2 ′ ∧ s 2 ′ ≠ E r r . \Psi(xs,s'_1,s'_2) \Longleftrightarrow\operatorname{Reach}(xs,s'_1)\land\exists\omega_2.\ \omega_2\ \text{valid}\land\operatorname{Effect}(\omega_2)=xs\land[R]_{\omega_2}=s'_2\land s'_2\ne\mathrm{Err}. Ψ ( x s , s 1 ′ , s 2 ′ ) ⟺ Reach ( x s , s 1 ′ ) ∧ ∃ ω 2 . ω 2 valid ∧ Effect ( ω 2 ) = x s ∧ [ R ] ω 2 = s 2 ′ ∧ s 2 ′ = Err .
The conjunct Reach ( x s , s 1 ′ ) \operatorname{Reach}(xs,s'_1) Reach ( x s , s 1 ′ ) pins the first state component to the canonical insertion-only run of x s xs x s for the proofs of B3 and B4.
B 1 B1 B 1 . Take ω 2 : = [ ] \omega_2 := [] ω 2 := [ ] : it is valid, Effect ( [ ] ) = [ ] \operatorname{Effect}([]) = [] Effect ([ ]) = [ ] , and [ R ] [ ] = I ≠ E r r [R]_{[]} = I \ne\mathrm{Err} [ R ] [ ] = I = Err . Together with Reach( [ ] , I ) ([],I) ([ ] , I ) (rule R1 above), this gives Ψ ( [ ] , I , I ) \Psi([],I,I) Ψ ([ ] , I , I ) .
Proc. ACM Program. Lang., Vol. 11, No. POPL, Article 30. Publication date: January 2027. B2. Suppose Ψ ( x s , s 1 ′ , s 2 ′ ) \Psi(xs,s'_1,s'_2) Ψ ( x s , s 1 ′ , s 2 ′ ) with witness ω 2 \omega_2 ω 2 . For the first conjunct, the R2 argument gives
Reach ( x s + + [ x ] , acc ′ ( s 1 ′ , x ) ) . \operatorname{Reach}(xs{++}[x],\operatorname{acc}'(s'_1,x)). Reach ( x s + + [ x ] , acc ′ ( s 1 ′ , x )) .
For the second conjunct, consider the extension ω 2 + + [ ( acc , x ) ] \omega_2{++}[(\operatorname{acc},x)] ω 2 + + [( acc , x )] : it is valid with effect x s + + [ x ] xs{++}[x] x s + + [ x ] by (F2), and since s 2 ′ ≠ Err s'_2 \ne\operatorname{Err} s 2 ′ = Err , its state semantics is the proper state acc ′ ( s 2 ′ , x ) \operatorname{acc}'(s'_2,x) acc ′ ( s 2 ′ , x ) . Thus, we have
Ψ ( x s + + [ x ] , acc ′ ( s 1 ′ , x ) , acc ′ ( s 2 ′ , x ) ) . \Psi(xs{++}[x],\operatorname{acc}'(s'_1,x),\operatorname{acc}'(s'_2,x)). Ψ ( x s + + [ x ] , acc ′ ( s 1 ′ , x ) , acc ′ ( s 2 ′ , x )) .
B3. Assume the premises of the rule: Reach ( x s , s 1 ′ ) \operatorname{Reach}(xs,s'_1) Reach ( x s , s 1 ′ ) , Ψ ( x s , s 1 ′ , s 2 ′ ) \Psi(xs,s'_1,s'_2) Ψ ( x s , s 1 ′ , s 2 ′ ) with witness ω 2 \omega_2 ω 2 , x ∈ x s x \in xs x ∈ x s , retract ( s 2 ′ , x ) ≠ Err \operatorname{retract}(s'_2,x) \ne\operatorname{Err} retract ( s 2 ′ , x ) = Err , y s = remove ( x s , x ) ys=\operatorname{remove}(xs,x) y s = remove ( x s , x ) , and Reach ( y s , s 3 ′ ) \operatorname{Reach}(ys,s'_3) Reach ( y s , s 3 ′ ) . Consider the extension ω 2 + + [ ( retract , x ) ] \omega_2{++}[(\operatorname{retract},x)] ω 2 + + [( retract , x )] . Since x ∈ x s = Effect ( ω 2 ) x \in xs=\operatorname{Effect}(\omega_2) x ∈ x s = Effect ( ω 2 ) , the extension is valid by (F3), and its effect is remove ( x s , x ) = y s \operatorname{remove}(xs,x)=ys remove ( x s , x ) = y s . Its state semantics is
retract ( [ R ] ω 2 , x ) = retract ( s 2 ′ , x ) ≠ Err \operatorname{retract}([R]_{\omega_2},x)=\operatorname{retract}(s'_2,x) \ne\operatorname{Err} retract ([ R ] ω 2 , x ) = retract ( s 2 ′ , x ) = Err
by the premise. Together with Reach ( y s , s 3 ′ ) \operatorname{Reach}(ys,s'_3) Reach ( y s , s 3 ′ ) , the extended sequence therefore has
Ψ ( y s , s 3 ′ , retract ( s 2 ′ , x ) ) . \Psi(ys,s'_3,\operatorname{retract}(s'_2,x)). Ψ ( y s , s 3 ′ , retract ( s 2 ′ , x )) .
B4. Suppose Ψ ( x s , s 1 ′ , s 2 ′ ) \Psi(xs,s'_1,s'_2) Ψ ( x s , s 1 ′ , s 2 ′ ) . The conjunct Reach ( x s , s 1 ′ ) \operatorname{Reach}(xs,s'_1) Reach ( x s , s 1 ′ ) forces s 1 ′ = [ R ] Canon ( x s ) s'_1=[R]\operatorname{Canon}(xs) s 1 ′ = [ R ] Canon ( x s ) , and the witness provides a valid ω 2 \omega_2 ω 2 with Effect ( ω 2 ) = x s \operatorname{Effect}(\omega_2)=xs Effect ( ω 2 ) = x s and [ R ] ω 2 = s 2 ′ ≠ Err [R]_{\omega_2}=s'_2 \ne\operatorname{Err} [ R ] ω 2 = s 2 ′ = Err . By (F1), Canon ( x s ) \operatorname{Canon}(xs) Canon ( x s ) is a valid update sequence with Effect ( Canon ( x s ) ) = x s \operatorname{Effect}(\operatorname{Canon}(xs))=xs Effect ( Canon ( x s )) = x s and [ R ] Canon ( x s ) ≠ Err [R]_{\operatorname{Canon}(xs)} \ne\operatorname{Err} [ R ] Canon ( x s ) = Err . Assumption A.2(a), applied to ω 2 \omega_2 ω 2 and Canon ( x s ) \operatorname{Canon}(xs) Canon ( x s ) , yields
eval ′ ( s 2 ′ ) = eval ′ ( [ R ] Canon ( x s ) ) = eval ′ ( s 1 ′ ) , \operatorname{eval}'(s'_2)=\operatorname{eval}'([R]\operatorname{Canon}(xs))=\operatorname{eval}'(s'_1), eval ′ ( s 2 ′ ) = eval ′ ([ R ] Canon ( x s )) = eval ′ ( s 1 ′ ) ,
which is the conclusion of B4. Clause (2) of Definition 3.9 applies when [ R ] ω 2 ≠ Err [R]_{\omega_2} \ne\operatorname{Err} [ R ] ω 2 = Err . The witness may exceed the budget of θ \theta θ , in which case eval ′ ( s 2 ′ ) \operatorname{eval}'(s'_2) eval ′ ( s 2 ′ ) may equal Err \operatorname{Err} Err even though s 2 ′ s'_2 s 2 ′ is a proper state. Assumption A.2(a) covers this case.
Budget relation Ω \Omega Ω (Block C). Define
Ω ( s 1 ′ , s 2 ′ , n ) ⟺ ∃ ω . [ R ] ω ≠ Err ∧ s 1 ′ = [ R ] ω + ∧ s 2 ′ = [ R ] ω − ∧ n = ∣ ω − ∣ . \Omega(s'_1,s'_2,n) \Longleftrightarrow\exists\omega.\ [R]_{\omega} \ne\operatorname{Err} \land s'_1=[R]_{\omega^+} \land s'_2=[R]_{\omega^-} \land n=|\omega^-|. Ω ( s 1 ′ , s 2 ′ , n ) ⟺ ∃ ω . [ R ] ω = Err ∧ s 1 ′ = [ R ] ω + ∧ s 2 ′ = [ R ] ω − ∧ n = ∣ ω − ∣.
Here ω \omega ω ranges over arbitrary update sequences. Their state semantics is defined by Definition 3.5 , and the relation retains the conjunct [ R ] ω ≠ Err [R]_{\omega} \ne\operatorname{Err} [ R ] ω = Err . Allowing arbitrary witnesses makes C3 closed under extension. The element x x x in C3 comes from a Ψ \Psi Ψ witness that may have a different effect from the Ω \Omega Ω witness, so appending its retraction may produce an invalid update sequence. The sequence is accumulation-only, so [ R ] ω + ≠ Err [R]_{\omega^+} \ne\operatorname{Err} [ R ] ω + = Err by (F1), and the first component of the relation is always a proper state.
C1. Take ω = [ ] \omega=[] ω = [ ] : then ω + = ω − = [ ] \omega^+=\omega^-=[] ω + = ω − = [ ] , both state components equal I ′ ≠ Err I' \ne\operatorname{Err} I ′ = Err , and n = 0 n=0 n = 0 . Hence
Ω ( I ′ , I ′ , 0 ) . \Omega(I',I',0). Ω ( I ′ , I ′ , 0 ) .
C2. Suppose Ω ( s 1 ′ , s 2 ′ , n ) \Omega(s'_1,s'_2,n) Ω ( s 1 ′ , s 2 ′ , n ) with witness ω \omega ω , and let ω ′ = ω + + [ ( acc , x ) ] \omega'=\omega{++}[(\operatorname{acc},x)] ω ′ = ω + + [( acc , x )] . Then ω ′ + = ω + + + [ ( acc , x ) ] \omega'^+=\omega^+{++}[(\operatorname{acc},x)] ω ′ + = ω + + + [( acc , x )] , so [ R ] ω ′ + = acc ′ ( s 1 ′ , x ) [R]_{\omega'^+}=\operatorname{acc}'(s'_1,x) [ R ] ω ′ + = acc ′ ( s 1 ′ , x ) by (F1), and since s 2 ′ = [ R ] ω ≠ Err s'_2=[R]_{\omega} \ne\operatorname{Err} s 2 ′ = [ R ] ω = Err , we get [ R ] ω ′ = acc ′ ( s 2 ′ , x ) [R]_{\omega'}=\operatorname{acc}'(s'_2,x) [ R ] ω ′ = acc ′ ( s 2 ′ , x ) , which is a proper state. Also, ∣ ω ′ − ∣ = ∣ ω − ∣ = n |\omega'^-|=|\omega^-|=n ∣ ω ′ − ∣ = ∣ ω − ∣ = n . Hence
Ω ( acc ′ ( s 1 ′ , x ) , acc ′ ( s 2 ′ , x ) , n ) . \Omega(\operatorname{acc}'(s'_1,x),\operatorname{acc}'(s'_2,x),n). Ω ( acc ′ ( s 1 ′ , x ) , acc ′ ( s 2 ′ , x ) , n ) .
C3. Assume the premises: Ω ( s 1 ′ , s 2 ′ , n ) \Omega(s'_1,s'_2,n) Ω ( s 1 ′ , s 2 ′ , n ) with witness ω \omega ω , Ψ ( x s , s 3 ′ , s 2 ′ ) \Psi(xs,s'_3,s'_2) Ψ ( x s , s 3 ′ , s 2 ′ ) , x ∈ x s x \in xs x ∈ x s , and retract ( s 2 ′ , x ) ≠ Err \operatorname{retract}(s'_2,x) \ne\operatorname{Err} retract ( s 2 ′ , x ) = Err . Let ω ′ = ω + + [ ( retract , x ) ] \omega'=\omega{++}[(\operatorname{retract},x)] ω ′ = ω + + [( retract , x )] . Arbitrary update sequences are admissible witnesses, so ω ′ \omega' ω ′ is admissible. Then ω ′ + = ω + \omega'^+=\omega^+ ω ′ + = ω + , so the first state component is still s 1 ′ s'_1 s 1 ′ ; the state semantics is [ R ] ω ′ = retract ( s 2 ′ , x ) ≠ Err [R]_{\omega'}=\operatorname{retract}(s'_2,x) \ne\operatorname{Err} [ R ] ω ′ = retract ( s 2 ′ , x ) = Err by the premise; and ∣ ω ′ − ∣ = n + 1 |\omega'^-|=n+1 ∣ ω ′ − ∣ = n + 1 . Hence
Ω ( s 1 ′ , retract ( s 2 ′ , x ) , n + 1 ) . \Omega(s'_1,\operatorname{retract}(s'_2,x),n+1). Ω ( s 1 ′ , retract ( s 2 ′ , x ) , n + 1 ) .
C4. Assume the premises:
Reach ( y s , s 1 ′ ) ∧ Ω ( s 1 ′ , s 2 ′ , n ) ∧ Ψ ( x s , s 3 ′ , s 2 ′ ) ∧ DeletesTo ( y s , x s , n ) ∧ x ∈ x s ∧ n < θ ( y s ) . \operatorname{Reach}(ys,s'_1)\land\Omega(s'_1,s'_2,n)\land\Psi(xs,s'_3,s'_2)\land\operatorname{DeletesTo}(ys,xs,n)\land x\in xs\land n<\theta(ys). Reach ( y s , s 1 ′ ) ∧ Ω ( s 1 ′ , s 2 ′ , n ) ∧ Ψ ( x s , s 3 ′ , s 2 ′ ) ∧ DeletesTo ( y s , x s , n ) ∧ x ∈ x s ∧ n < θ ( y s ) .
Unfolding Ψ ( x s , s 3 ′ , s 2 ′ ) \Psi(x_s,s'_3,s'_2) Ψ ( x s , s 3 ′ , s 2 ′ ) yields a valid update sequence ω 2 \omega_2 ω 2 with Effect ( ω 2 ) = x s \operatorname{Effect}(\omega_2)=x_s Effect ( ω 2 ) = x s and [ R ] ω 2 = s 2 ′ + Err [R]_{\omega_2}=s'_2+\operatorname{Err} [ R ] ω 2 = s 2 ′ + Err . Unfolding DeletesTo ( y s , x s , n ) \operatorname{DeletesTo}(y_s,x_s,n) DeletesTo ( y s , x s , n ) by (F5) yields values z 1 , … , z n z_1,\ldots,z_n z 1 , … , z n such that the iterated removal of z 1 , … , z n z_1,\ldots,z_n z 1 , … , z n from y s y_s y s is defined and equals x s x_s x s . Define
σ 0 ≔ Canon ( y s ) , σ i ≔ σ i − 1 + + [ ( retract , z i ) ] for 1 ≤ i ≤ n , σ ≔ σ n + + [ ( retract , x ) ] . \sigma_0 \coloneqq\operatorname{Canon}(y_s),\qquad\sigma_i \coloneqq\sigma_{i-1} \mathbin{+\!\!+} [(\operatorname{retract},z_i)]\text{ for }1\le i\le n,\qquad\sigma\coloneqq\sigma_n \mathbin{+\!\!+} [(\operatorname{retract},x)]. σ 0 : = Canon ( y s ) , σ i : = σ i − 1 + + [( retract , z i )] for 1 ≤ i ≤ n , σ : = σ n + + [( retract , x )] .
We use an induction on i i i using (F1) and (F3) to show that each σ i \sigma_i σ i is valid with Effect ( σ i ) = remove ( ⋯ remove ( y s , z 1 ) ⋯ , z i ) \operatorname{Effect}(\sigma_i)=\operatorname{remove}(\cdots\operatorname{remove}(y_s,z_1)\cdots,z_i) Effect ( σ i ) = remove ( ⋯ remove ( y s , z 1 ) ⋯ , z i ) : the i i i -th extension is valid because definedness of the i i i -th removal in (F5) gives z i ∈ Effect ( σ i − 1 ) z_i\in\operatorname{Effect}(\sigma_{i-1}) z i ∈ Effect ( σ i − 1 ) . In particular, σ n \sigma_n σ n is valid with Effect ( σ n ) = x s \operatorname{Effect}(\sigma_n)=x_s Effect ( σ n ) = x s , and since x ∈ x s x\in x_s x ∈ x s , the full sequence σ \sigma σ is valid as well, again by (F3).
Next, we check that every prefix δ ⪯ σ \delta\preceq\sigma δ ⪯ σ satisfies ∣ δ ∣ ≤ θ ( ζ ( δ + ) ) |\delta|\le\theta(\zeta(\delta^+)) ∣ δ ∣ ≤ θ ( ζ ( δ + )) . If δ ⪯ Canon ( y s ) \delta\preceq\operatorname{Canon}(y_s) δ ⪯ Canon ( y s ) , then ∣ δ ∣ = 0 |\delta|=0 ∣ δ ∣ = 0 , and θ ( ζ ( δ + ) ) ≥ 0 \theta(\zeta(\delta^+))\ge0 θ ( ζ ( δ + )) ≥ 0 because θ \theta θ maps into N \mathbb{N} N . Otherwise, δ \delta δ extends Canon ( y s ) \operatorname{Canon}(y_s) Canon ( y s ) by retractions only, so ζ ( δ + ) = y s \zeta(\delta^+)=y_s ζ ( δ + ) = y s and ∣ δ ∣ ≤ n + 1 ≤ θ ( y s ) |\delta|\le n+1\le\theta(y_s) ∣ δ ∣ ≤ n + 1 ≤ θ ( y s ) , using the premise n < θ ( y s ) n<\theta(y_s) n < θ ( y s ) . Hence clause (1) of Definition 3.9 , i.e., R ⊨ θ R\models\theta R ⊨ θ , gives ⟦ R ⟧ σ ≠ Err \llbracket R\rrbracket_\sigma\ne\operatorname{Err} [ [ R ] ] σ = Err , and in particular [ R ] σ ≠ Err [R]_\sigma\ne\operatorname{Err} [ R ] σ = Err . By Err absorption (F4), [ R ] σ n ≠ Err [R]_{\sigma_n}\ne\operatorname{Err} [ R ] σ n = Err , and unfolding the last step of the semantics,
retract ( [ R ] σ n , x ) = [ R ] σ ≠ Err . \operatorname{retract}([R]_{\sigma_n},x)=[R]_\sigma\ne\operatorname{Err}. retract ([ R ] σ n , x ) = [ R ] σ = Err .
Now σ n \sigma_n σ n and ω 2 \omega_2 ω 2 are two valid update sequences with non-Err state semantics and the same effect x s x_s x s , and x ∈ x s x\in x_s x ∈ x s . Assumption A.2(b) therefore transfers the definedness of retracting x x x from [ R ] σ n [R]_{\sigma_n} [ R ] σ n to [ R ] ω 2 = s 2 ′ [R]_{\omega_2}=s'_2 [ R ] ω 2 = s 2 ′ , which yields
retract ( s 2 ′ , x ) ≠ Err . \operatorname{retract}(s'_2,x)\ne\operatorname{Err}. retract ( s 2 ′ , x ) = Err .
The C4 validity argument depends on Ψ \Psi Ψ , DeletesTo \operatorname{DeletesTo} DeletesTo , x ∈ x s x\in x_s x ∈ x s , and the bound n < θ ( y s ) n<\theta(y_s) n < θ ( y s ) . The Reach and Ω \Omega Ω premises are used in the soundness direction (Lemma C.9 ) to supply the corresponding instantiation of y s y_s y s , x s x_s x s , and n n n .
Thus all rules of Figure 3 are valid under the semantic definitions of Reach, Ψ \Psi Ψ , and Ω \Omega Ω , and by Assumption A.3 these relations are expressible in the assertion logic. This completes the proof. □
Proofs about the Synthesis Method
Throughout this section, we make the following assumptions.
(A1) Oracles. The SMT, SyGuS, PBE, symbolic-execution, and quantifier-elimination procedures used by Rewind are sound and complete for the queries issued by the algorithms.
(A2) Fairness. All enumeration and CEGIS loops used by Rewind are fair.
(A3) Search spaces. Whenever a completeness result assumes the existence of a correct solution, its auxiliary type, qualifier, auxiliary update, relation, retraction function, and predicates satisfying the proof rules are expressible in the corresponding libraries, grammars, and templates. This includes the guard expressions used to fill the holes introduced by InsertOrMerge. For the corresponding qualifier, the lifted fold returned by Lift admits such a retraction and predicates, and its relation described in Theorem D.1 occurs in the SynRetract search space.
The pseudocode and proofs use two interface conventions. First, an invocation of (A3) fixes one witness for the entire synthesis run: a single retraction, its proof predicates, and the guard instantiations used by every partition refinement. Second, a counterexample ( x s , s ⋆ , x ⋆ ) (x_s,s^\star,x^\star) ( x s , s ⋆ , x ⋆ ) returned by Verify is a local deletion obligation: x ⋆ ∈ x s x^\star\in x_s x ⋆ ∈ x s , the rejected candidate violates
Ψ ( [ R ] remove ( x s , x ⋆ ) , retract ( s ⋆ , x ⋆ ) ) . \Psi([R]_{\operatorname{remove}(x_s,x^\star)},\operatorname{retract}(s^\star,x^\star)). Ψ ([ R ] remove ( x s , x ⋆ ) , retract ( s ⋆ , x ⋆ )) .
and, once length ∣ x s ∣ |x_s| ∣ x s ∣ has been explored, ( s ⋆ , x ⋆ ) (s^\star,x^\star) ( s ⋆ , x ⋆ ) is covered by the retained partition element selected at line 12 of Algorithm 6. Its reference post-deletion state is one of the local obligations associated with that element. Lines 8–13 rely on this counterexample contract to define the selection at line 12.
Theorem D.1 (Necessity of ValidCand). Let R = ( I ⋆ , acc ⋆ , eval ⋆ ) \mathcal{R} = (I^\star, \operatorname{acc}^\star, \operatorname{eval}^\star) R = ( I ⋆ , acc ⋆ , eval ⋆ ) be a lifted fold that realizes a qualifier χ \chi χ , and let G G G be the compatible-history sets returned by CheckRetractability ( P , θ , χ , D , X ) \operatorname{CheckRetractability}(P, \theta, \chi, D, \mathcal{X}) CheckRetractability ( P , θ , χ , D , X ) . Suppose there is a retraction function retract and predicates Reach, Ψ \Psi Ψ , and Ω \Omega Ω satisfying Blocks R, B, and C of Figure 3 . Let Ψ \Psi Ψ be the smallest relation that contains
{ ( s 1 , s 2 ) ∣ ∃ x s . Ψ ( x s , s 1 , s 2 ) } \{(s_1,s_2) \mid\exists x_s.\ \Psi(x_s,s_1,s_2)\} {( s 1 , s 2 ) ∣ ∃ x s . Ψ ( x s , s 1 , s 2 )} and is closed under reflexivity, symmetry, and transitivity. Then ValidCand ( G , R , Ψ ) \operatorname{ValidCand}(G,\mathcal{R},\Psi) ValidCand ( G , R , Ψ ) holds.
Proof sketch. We establish the three proof-rule clauses in ValidCand \operatorname{ValidCand} ValidCand . Rule B1 gives Ψ ( [ ] , I ⋆ , I ⋆ ) \Psi([], I^\star, I^\star) Ψ ([ ] , I ⋆ , I ⋆ ) , so Ψ ( I ⋆ , I ⋆ ) \Psi(I^\star, I^\star) Ψ ( I ⋆ , I ⋆ ) by the definition of Ψ \Psi Ψ .
For B2, suppose Ψ ( s 1 , s 2 ) \Psi(s_1,s_2) Ψ ( s 1 , s 2 ) . By the definition of Ψ \Psi Ψ , there is a finite chain s 1 = u 0 , … , u n = s 2 s_1 = u_0, \ldots, u_n = s_2 s 1 = u 0 , … , u n = s 2 in which each adjacent pair is related by Ψ ( x s i , ⋅ , ⋅ ) \Psi(x_{s_i}, \cdot, \cdot) Ψ ( x s i , ⋅ , ⋅ ) for some x s i x_{s_i} x s i , possibly in the reverse direction. Applying B2 to every such edge gives a chain from acc ⋆ ( s 1 , x ) \operatorname{acc}^\star(s_1,x) acc ⋆ ( s 1 , x ) to acc ⋆ ( s 2 , x ) \operatorname{acc}^\star(s_2,x) acc ⋆ ( s 2 , x ) whose adjacent states are related by Ψ ( x s i + + [ x ] , ⋅ , ⋅ ) \Psi(x_{s_i} \mathbin{+\!\!+} [x], \cdot, \cdot) Ψ ( x s i + + [ x ] , ⋅ , ⋅ ) . Hence Ψ ( acc ⋆ ( s 1 , x ) , acc ⋆ ( s 2 , x ) ) \Psi(\operatorname{acc}^\star(s_1,x), \operatorname{acc}^\star(s_2,x)) Ψ ( acc ⋆ ( s 1 , x ) , acc ⋆ ( s 2 , x )) . For B4, rule B4 gives equal evaluations across every edge in the original chain. Symmetry of equality and transitivity along the chain yield eval ⋆ ( s 1 ) = eval ⋆ ( s 2 ) \operatorname{eval}^\star(s_1) = \operatorname{eval}^\star(s_2) eval ⋆ ( s 1 ) = eval ⋆ ( s 2 ) .
We next consider the compatible-history sets in G G G . The seed relation E 0 E_0 E 0 in Algorithm 4 relates histories that reach the same lifted state under χ \chi χ . Reflexivity of Ψ \Psi Ψ relates the states reached by a reflexive pair, and both Acc and Ret preserve reflexive pairs. For a non-reflexive pair ( ( y s , x s ) , ( y s ′ , x s ′ ) ) ((y_s,x_s),(y_{s'},x_{s'})) (( y s , x s ) , ( y s ′ , x s ′ )) generated while computing E ⋆ E^\star E ⋆ , we maintain the following facts. The full histories y s y_s y s and y s ′ y_{s'} y s ′ reach the same lifted state q q q . There is a common sequence of n n n deletions that transforms y s y_s y s into x s x_s x s and y s ′ y_{s'} y s ′ into x s ′ x_{s'} x s ′ . Applying those deletions to q q q produces a state t t t such that
Ω ( q , t , n ) , Ψ ( x s , [ R ] x s , t ) and Ψ ( x s ′ , [ R ] x s ′ , t ) . \Omega(q,t,n), \qquad\Psi(x_s,[\mathcal{R}]x_s,t) \qquad\text{and} \qquad\Psi(x_{s'},[\mathcal{R}]x_{s'},t). Ω ( q , t , n ) , Ψ ( x s , [ R ] x s , t ) and Ψ ( x s ′ , [ R ] x s ′ , t ) .
The deletion sequence also witnesses DeletesTo ( y s , x s , n ) \operatorname{DeletesTo}(y_s,x_s,n) DeletesTo ( y s , x s , n ) and DeletesTo ( y s ′ , x s ′ , n ) \operatorname{DeletesTo}(y_{s'},x_{s'},n) DeletesTo ( y s ′ , x s ′ , n ) .
For a seed pair, n = 0 n = 0 n = 0 and q = t q = t q = t is the shared lifted state. Repeated applications of B2 from B1 establish the two Ψ \Psi Ψ facts, while repeated applications of C2 from C1 establish Ω ( q , q , 0 ) \Omega(q,q,0) Ω ( q , q , 0 ) . For a common Acc step with input x x x , take the new full-history state to be acc ⋆ ( q , x ) \operatorname{acc}^\star(q,x) acc ⋆ ( q , x ) and the new mixed state to be acc ⋆ ( t , x ) \operatorname{acc}^\star(t,x) acc ⋆ ( t , x ) . Rules B2 and C2 preserve the displayed facts, and the same deletion sequence still connects the two extended histories.
For a common Ret step with input x x x , Rules R1 and R2 provide reachability of the two insertion-only reference states and of the full-history state q q q . The successor pair remains in M M M , so the extended deletion sequence has length at most n n n ; hence the current count satisfies n < θ ( y s ) n < \theta(y_s) n < θ ( y s ) . Rule C4, using Ω ( q , t , n ) \Omega(q,t,n) Ω ( q , t , n ) , the first Ψ \Psi Ψ fact, and DeletesTo ( y s , x s , n ) \operatorname{DeletesTo}(y_s,x_s,n) DeletesTo ( y s , x s , n ) , gives retract( t , x ) ≠ Err (t,x) \ne\operatorname{Err} ( t , x ) = Err . Rule B3 then relates the unprimed insertion-only reference state after deleting x x x to retract( t , x ) (t,x) ( t , x ) . A second application of B3 uses the primed Ψ \Psi Ψ and reachability facts and gives the same result on the primed side. Finally, C3 establishes Ω ( q , retract ( t , x ) , n + 1 ) \Omega(q,\operatorname{retract}(t,x),n+1) Ω ( q , retract ( t , x ) , n + 1 ) .
Symmetry preserves these facts. For transitivity, if either premise is reflexive, the composed pair is the other premise. Otherwise, two pairs sharing a middle component have the same full-history state, deletion count, and multiset of deleted values, including multiplicity, since these are determined by the shared full and effective histories. Choose the deletion order from the first pair. The third history removes the same values with the same multiplicities, so this order is valid there as well; removals of different values commute, and the chosen order reaches the same effective history. Replaying this common sequence from the shared full-history state establishes the invariant for the composed pair. Thus every non-reflexive pair in E ⋆ E^\star E ⋆ has the displayed witnesses, while reflexive pairs satisfy the desired conclusion directly. In either case, the effective histories reach states related by Ψ \Psi Ψ .
Finally, each V ∈ G V \in G V ∈ G collects histories paired with the same anchor in E ⋆ E^\star E ⋆ . Thus, for x s 1 , x s 2 ∈ V x_{s1}, x_{s2} \in V x s 1 , x s 2 ∈ V , both reached states are Ψ \Psi Ψ -related to the anchor state, and symmetry and transitivity give Ψ ( [ R ] x s 1 , [ R ] x s 2 ) \Psi([R]_{x_{s1}}, [R]_{x_{s2}}) Ψ ([ R ] x s 1 , [ R ] x s 2 ) .
Proof of Theorem 5.2 . When Algorithm 1 returns a retractable fold, SynRetract has produced a retraction accepted by Verify. The assembled fold therefore satisfies every rule in Figure 3 . By the soundness direction of Theorem 4.2 , the returned fold is equivalent to the original fold modulo θ \theta θ .
Theorem D.2 (Completeness of Lift). Let P = ( I , acc , eval ) P = (\mathcal{I}, \operatorname{acc}, \operatorname{eval}) P = ( I , acc , eval ) , let T = { ( s : τ , a : τ a u x ) ∣ χ ( s , a , x s ) } T = \{(s : \tau, a : \tau_{\mathrm{aux}}) \mid\chi(s, a, x_s)\} T = {( s : τ , a : τ aux ) ∣ χ ( s , a , x s )} be a lifting specification, let U 0 U_0 U 0 be the initial update map, and let G G G be the grammar given to Lift. Suppose there are a 0 a_0 a 0 and UpdAux ⋆ ∈ G \operatorname{UpdAux}^{\star} \in G UpdAux ⋆ ∈ G such that
χ ( I , a 0 , [ ] ) \chi(\mathcal{I}, a_0, []) χ ( I , a 0 , [ ]) holds;
UpdAux ⋆ \operatorname{UpdAux}^{\star} UpdAux ⋆ is consistent with U 0 U_0 U 0 ; and
for every history x s x_s x s , auxiliary value a a a , and input x x x , with s = [ P ] x s s = [P]x_s s = [ P ] x s , χ ( s , a , x s ) ⇒ χ ( acc ( s , x ) , UpdAux ⋆ ( s , a , x ) , x s + + [ x ] ) \chi(s, a, x_s) \Rightarrow\chi(\operatorname{acc}(s, x), \operatorname{UpdAux}^{\star}(s, a, x), x_s ++ [x]) χ ( s , a , x s ) ⇒ χ ( acc ( s , x ) , UpdAux ⋆ ( s , a , x ) , x s + + [ x ]) .
Under (A1) and (A2), Lift eventually returns ( t r u e , I ⋆ , acc ⋆ , eval ⋆ ) (\mathrm{true}, I^\star, \operatorname{acc}^\star, \operatorname{eval}^\star) ( true , I ⋆ , acc ⋆ , eval ⋆ ) , where
I ⋆ = ( I , a 0 ′ ) , acc ⋆ ( ( s , a ) , x ) = ( acc ( s , x ) , UpdAux ( s , a , x ) ) , eval ⋆ ( s , a ) = eval ( s ) , I^\star= (\mathcal{I}, a_0^\prime), \qquad\operatorname{acc}^\star((s, a), x) = (\operatorname{acc}(s, x), \operatorname{UpdAux}(s, a, x)), \qquad\operatorname{eval}^\star(s, a) = \operatorname{eval}(s), I ⋆ = ( I , a 0 ′ ) , acc ⋆ (( s , a ) , x ) = ( acc ( s , x ) , UpdAux ( s , a , x )) , eval ⋆ ( s , a ) = eval ( s ) , and acc ⋆ \operatorname{acc}^\star acc ⋆ preserves χ \chi χ on all reachable states.
Proof sketch. The update map is initially consistent with UpdAux ⋆ \operatorname{UpdAux}^{\star} UpdAux ⋆ . Whenever the current candidate fails the inductiveness query in Algorithm 5, the counterexample supplies the unique auxiliary value required by χ \chi χ after that step. The qualifier grammar in Figure 5 has the form a = E ( s , x s ) a = E(s, x_s) a = E ( s , x s ) , so this value is also the value returned by UpdAux ⋆ \operatorname{UpdAux}^{\star} UpdAux ⋆ . The example map therefore remains consistent with UpdAux ⋆ \operatorname{UpdAux}^{\star} UpdAux ⋆ after each refinement.
By (A1), PBE returns a candidate after each refinement. Each counterexample eliminates the current update function while preserving UpdAux ⋆ \operatorname{UpdAux}^{\star} UpdAux ⋆ , so fairness (A2) eventually produces an update function for which the inductiveness query is unsatisfiable. The definitions at lines 7 and 8 then give the stated initializer, accumulator, and evaluation function. For every reachable post-state, the functional qualifier supplies the unique auxiliary value a ′ = E ( acc ( s , x ) , x s + + [ x ] ) a^\prime= E(\operatorname{acc}(s, x), x_s ++ [x]) a ′ = E ( acc ( s , x ) , x s + + [ x ]) . Every model of Bad therefore extends to a model of the query in Algorithm 5. Unsatisfiability of that query establishes preservation of χ \chi χ on reachable states.
Lemma D.3 (Functional consistency is necessary for lifting). Let R R R be the relation constructed by Algorithm 3 for a qualifier χ \chi χ . If there is a deterministic update UpdAux ⋆ \operatorname{UpdAux}^{\star} UpdAux ⋆ that preserves χ \chi χ on every sampled history and test input, then R R R is functional.
Proof sketch. Each ( ( s , a , x ) , a ′ ) ∈ R ((s, a, x), a^\prime) \in R (( s , a , x ) , a ′ ) ∈ R records the auxiliary value required by χ \chi χ after updating ( s , a ) (s, a) ( s , a ) with x x x . Preservation by UpdAux ⋆ \operatorname{UpdAux}^{\star} UpdAux ⋆ shows that this value must be UpdAux ⋆ ( s , a , x ) \operatorname{UpdAux}^{\star}(s, a, x) UpdAux ⋆ ( s , a , x ) . Since χ \chi χ has the form a = E ( s , x s ) a = E(s, x_s) a = E ( s , x s ) , the required value is unique. Hence two entries with the same key ( s , a , x ) (s, a, x) ( s , a , x ) have the same value.
Lemma D.4 (Valid lifts pass the retractability check). Suppose a lifted fold realizing χ \chi χ satisfies eval ⋆ ( s , a ) = eval ( s ) \operatorname{eval}^{\star}(s, a) = \operatorname{eval}(s) eval ⋆ ( s , a ) = eval ( s ) and admits a retraction function and predicates satisfying Blocks R, B, and C of Figure 3 for θ \theta θ . Then CheckRetractability ( P , θ , χ , D , λ ) \operatorname{CheckRetractability}(P, \theta, \chi, \mathcal{D}, \lambda) CheckRetractability ( P , θ , χ , D , λ ) returns b a d = f a l s e \mathrm{bad} = \mathrm{false} bad = false .
Proof sketch. Suppose to the contrary that the check reports a conflict. Then E ⋆ E^\star E ⋆ relates two allowed pairs whose effective histories x s xs x s and x s ′ xs^\prime x s ′ have different observable results under P P P . As in the proof of Theorem D.1 , induction over the construction of E ⋆ E^\star E ⋆ uses B2 for Acc steps, B3 for Ret steps, and Block C to show that the required retractions are defined. The lifted states reached by $xs and x s ′ x_{s'} x s ′ must therefore be related by the relation Ψ \Psi Ψ from Theorem D.1 . The B4 argument in that proof gives equal observations along every Ψ \Psi Ψ chain. By the assumed equation eval ⋆ ( s , a ) = eval ( s ) \operatorname{eval}^{\star}(s,a)=\operatorname{eval}(s) eval ⋆ ( s , a ) = eval ( s ) , this contradicts [ [ P ] ] x s ≠ [ [ P ] ] x s ′ [\![\mathcal{P}]\!]_{x_s}\ne[\![\mathcal{P}]\!]_{x_{s'}} [ [ P ] ] x s = [ [ P ] ] x s ′ .
Proof of Theorem 5.6. The result is the contrapositive of Lemma D.4. For a lift covered by the theorem, the existence of predicates and a retraction function satisfying Blocks R, B, and C would force the check to return b a d = f a l s e \mathit{bad}=\mathit{false} bad = false .
Theorem D.5 (Completeness of InferLiftSpec). Let P = ( I , acc , eval ) \mathcal{P}=(\mathcal{I},\operatorname{acc},\operatorname{eval}) P = ( I , acc , eval ) , θ \theta θ , L ( τ ) L(\tau) L ( τ ) , X \mathcal{X} X , and K K K be the inputs to InferLiftSpec. Suppose the library and qualifier enumeration contain a pair ( τ a u x ⋆ , χ ⋆ ) (\tau_{\mathrm{aux}}^{\star},\chi^{\star}) ( τ aux ⋆ , χ ⋆ ) for which there is a deterministic lifted retractable fold that realizes χ ⋆ \chi^{\star} χ ⋆ , uses eval ⋆ ( s , a ) = eval ( s ) \operatorname{eval}^{\star}(s,a)=\operatorname{eval}(s) eval ⋆ ( s , a ) = eval ( s ) , and admits predicates satisfying Blocks R, B, and C of Figure 3 . Under (A1) and (A2), InferLiftSpec eventually adds ( τ a u x ⋆ , χ ⋆ , U ⋆ , G ⋆ ) (\tau_{\mathrm{aux}}^{\star},\chi^{\star},U^{\star},G^{\star}) ( τ aux ⋆ , χ ⋆ , U ⋆ , G ⋆ ) to Σ \Sigma Σ .
Proof sketch. Fair enumeration eventually reaches ( τ a u x ⋆ , χ ⋆ ) (\tau_{\mathrm{aux}}^{\star},\chi^{\star}) ( τ aux ⋆ , χ ⋆ ) . The auxiliary update of the assumed fold preserves χ ⋆ \chi^{\star} χ ⋆ , so Lemma D.3 shows that CheckFunctionalConsistency returns o k = t r u e \mathit{ok}=\mathit{true} ok = true and an update map U ⋆ U^{\star} U ⋆ . By Lemma D.4, CheckRetractability returns b a d = f a l s e \mathit{bad}=\mathit{false} bad = false and evidence G ⋆ G^{\star} G ⋆ . The candidate therefore passes the guard in Algorithm 2 and is added to Σ \Sigma Σ .
Theorem D.6 (Properties of the partition checks). Let Ψ \Psi Ψ be the relation constructed in Theorem D.1 , and let ( γ i j , β i j ) (\gamma_{ij},\beta_{ij}) ( γ ij , β ij ) be a case constructed by Algorithm 7.
If the check at line 8 succeeds, no input satisfying γ i j \gamma_{ij} γ ij has a reference post-deletion state satisfying β i j \beta_{ij} β ij .
If the ambiguity check succeeds with witness s , x , s 1 ′ , s 2 ′ s,x,s'_1,s'_2 s , x , s 1 ′ , s 2 ′ , no single state can be Ψ \Psi Ψ -related to both s 1 ′ s'_1 s 1 ′ and s 2 ′ s'_2 s 2 ′ .
Proof sketch. For the first claim, suppose ( s , x ) (s,x) ( s , x ) satisfies γ i j \gamma_{ij} γ ij . If some s ′ s' s ′ satisfied β i j ( s , x , s ′ ) \beta_{ij}(s,x,s') β ij ( s , x , s ′ ) , then ( s , x , s ′ ) (s,x,s') ( s , x , s ′ ) would satisfy the conjunction declared unsatisfiable by the first check. Hence no such reference state exists.
For the second claim, let s , x , s 1 ′ , s 2 ′ s,x,s'_1,s'_2 s , x , s 1 ′ , s 2 ′ be the values returned by the ambiguity query. If a successful result t t t satisfied the local requirement for both reference states, then Ψ ( s 1 ′ , t ) \Psi(s'_1,t) Ψ ( s 1 ′ , t ) and Ψ ( s 2 ′ , t ) \Psi(s'_2,t) Ψ ( s 2 ′ , t ) would hold. The construction in Theorem D.1 is closed under symmetry and transitivity, so these two facts would imply Ψ ( s 1 ′ , s 2 ′ ) \Psi(s'_1,s'_2) Ψ ( s 1 ′ , s 2 ′ ) , contradicting the ambiguity query. The two B3 obligations are therefore incompatible for this input.
Lemma D.7 (Partition completeness for a fixed length). Fix a lifted fold R \mathcal{R} R and a length ℓ \ell ℓ . Suppose a retraction function retract ⋆ \operatorname{retract}^{\star} retract ⋆ and predicates satisfy the proof rules, and let Ψ \Psi Ψ be the relation constructed from their history-indexed Ψ \Psi Ψ as in Theorem D.1 . Under (A1) and (A3), the resulting partition is satisfied by a fixed correct retraction in the grammar and retains every length-ℓ \ell ℓ case on which the specification requires success.
Proof sketch. By completeness of symbolic execution, every history of length ℓ \ell ℓ follows one of the paths enumerated at line 4 of Algorithm 7. For each deletion position, β i \beta_i β i characterizes the reference post-deletion state for the history with that element removed. Quantifier elimination therefore produces a guard containing the current state and deleted element, together with a relation containing the corresponding reference state.
Consider a concrete history x s xs x s of length ℓ \ell ℓ , let s = [ [ R ] ] x s s=[\![\mathcal{R}]\!]_{xs} s = [ [ R ] ] x s , and delete x = x i x=x_i x = x i . The reference post-deletion state s ′ = [ [ R ] ] remove ( x s , x ) s'=[\![\mathcal{R}]\!]_{\operatorname{remove}(xs,x)} s ′ = [ [ R ] ] remove ( x s , x ) witnesses β i j ( s , x , s ′ ) \beta_{ij}(s,x,s') β ij ( s , x , s ′ ) , so the unsatisfiability check retains this input. If the specification requires the deletion, R1 and R2 give Reach( x s , s ) (xs,s) ( x s , s ) and Reach( remove ( x s , x ) , s ′ ) (\operatorname{remove}(xs,x),s') ( remove ( x s , x ) , s ′ ) .
Repeated applications of B2 and C1 from B1 and C1 give Ψ ( x s , s , s ) \Psi(x_s,s,s) Ψ ( x s , s , s ) and Ω ( s , s , 0 ) \Omega(s,s,0) Ω ( s , s , 0 ) . Since 0 < θ ( x s ) 0 < \theta(x_s) 0 < θ ( x s ) , C4 gives retract ⋆ ( s , x ) ≠ E r r \operatorname{retract}^{\star}(s,x) \ne\mathrm{Err} retract ⋆ ( s , x ) = Err , and B3 gives
Ψ ( remove ( x s , x ) , s ′ , retract ⋆ ( s , x ) ) . \Psi(\operatorname{remove}(x_s,x),s',\operatorname{retract}^{\star}(s,x)). Ψ ( remove ( x s , x ) , s ′ , retract ⋆ ( s , x )) .
Thus Ψ ( s ′ , retract ⋆ ( s , x ) ) \Psi(s',\operatorname{retract}^{\star}(s,x)) Ψ ( s ′ , retract ⋆ ( s , x )) , which is the local specification. Applying the same argument to every reference state for ( s , x ) (s,x) ( s , x ) shows that all of them are Ψ \Psi Ψ -related to the same result. They are therefore related to each other, so this required input passes the ambiguity check.
The constraints generated at different lengths share a common model. Let Refs ( s , x ) \operatorname{Refs}(s,x) Refs ( s , x ) contain every reference post-deletion state contributed for ( s , x ) (s,x) ( s , x ) across all symbolic lengths and by the concrete-refinement branch of Algorithm 6.
Fix the single correct witness supplied by (A3), and call its retraction retract † \operatorname{retract}^{\dagger} retract † . Its behavior on optional inputs is fixed for the entire run. On a retained input, it returns a representative of the single Ψ \Psi Ψ -class containing Refs ( s , x ) \operatorname{Refs}(s,x) Refs ( s , x ) ; on an input whose obligations contain two distinct Ψ \Psi Ψ -classes, it returns E r r \mathrm{Err} Err . On inputs where the proof rules require success, it agrees with the successful behavior of the fixed proof-rule witness. The C4 and B3 argument above shows that every reference state on such an input belongs to the class of that successful result. Thus, it returns a successful result on every required input.
The representative choices are shared across all lengths to account for mixed histories. Assumption (A3) supplies proof-rule correctness and predicates for the fixed witness. The representative construction above establishes that the local constraints generated by the algorithm are consistent with this witness.
For overlapping cases, instantiate the guard holes introduced by I n s e r t O r M e r g e \mathrm{InsertOrMerge} InsertOrMerge with the condition that all reference states contributed on the overlap are Ψ \Psi Ψ -related. On the satisfying region, retract † \operatorname{retract}^{\dagger} retract † meets every incoming local specification. On the remaining region, the obligations are incompatible, so that overlap is assigned ⊥ \bot ⊥ . Required inputs are always in the first region by the C4 and B3 argument above. By (A3), retract † \operatorname{retract}^{\dagger} retract † , its proof predicates, and these guard expressions occur in their search spaces. Hence every finite partition generated by symbolic refinement has the same fixed correct model and retains every required case. □
Theorem D.8 (Completeness of retraction synthesis). Fix a lifted fold R = ( I ⋆ , acc ⋆ , eval ⋆ ) R = (I^{\star},\operatorname{acc}^{\star},\operatorname{eval}^{\star}) R = ( I ⋆ , acc ⋆ , eval ⋆ ) that realizes χ \chi χ , and let G G G be the evidence returned by CheckRetractability. Suppose there is a deterministic retraction retract ⋆ \operatorname{retract}^{\star} retract ⋆ in the SyGuS grammar and predicates satisfying the proof rules for ( I ⋆ , acc ⋆ , retract ⋆ , eval ⋆ ) (I^{\star},\operatorname{acc}^{\star},\operatorname{retract}^{\star},\operatorname{eval}^{\star}) ( I ⋆ , acc ⋆ , retract ⋆ , eval ⋆ ) . Suppose also that the relation constructed in Theorem D.1 is in the state-relation search space. Under (A1) to (A3), S y n R e t r a c t \mathrm{SynRetract} SynRetract eventually returns a retraction accepted by V e r i f y \mathrm{Verify} Verify .
Proof sketch. By Theorem D.1 , the state relation obtained from the proof of retract ⋆ \operatorname{retract}^{\star} retract ⋆ satisfies V a l i d C a n d \mathrm{ValidCand} ValidCand . Fairness therefore makes the outer loop of Algorithm 6 consider that relation, unless an earlier relation already produces a verified retraction.
Fix the global correct retraction retract † \operatorname{retract}^{\dagger} retract † from Lemma D.7. Our induction invariant is that this retraction satisfies the current partition. The invariant holds initially because the partition is empty. When verification reports a previously unseen length, Lemma D.7 shows that symbolic partitioning preserves satisfaction by this same retraction and every required case. When the length has already been seen, write the counterexample as ( x , s ⋆ , x ⋆ ) (x,s^{\star},x^{\star}) ( x , s ⋆ , x ⋆ ) . The refinement adds
Ψ ( [ [ R ] ] remove ( x , s ⋆ , x ⋆ ) , retract ( s ⋆ , x ⋆ ) ) . \Psi([\![R]\!]_{\operatorname{remove}(x,s^{\star},x^{\star})},\operatorname{retract}(s^{\star},x^{\star})). Ψ ([ [ R ] ] remove ( x , s ⋆ , x ⋆ ) , retract ( s ⋆ , x ⋆ )) .
By the counterexample contract stated at the beginning of this appendix, the reference post-deletion state in this conjunct is one of the obligations in Refs ( s ⋆ , x ⋆ ) \operatorname{Refs}(s^{\star},x^{\star}) Refs ( s ⋆ , x ⋆ ) for the retained partition element. If this is a required-success obligation, the C4 and B3 instance from Lemma D.7 relates it to retract † ( s ⋆ , x ⋆ ) \operatorname{retract}^{\dagger}(s^\star, x^\star) retract † ( s ⋆ , x ⋆ ) . Otherwise, retract † \operatorname{retract}^{\dagger} retract † uses the representative of the common Ψ \Psi Ψ -class for that retained element. In both cases it satisfies the new conjunct. The rejected candidate violates the conjunct by the same counterexample contract, so the refinement eliminates that candidate while preserving the induction invariant.
Thus the same correct retraction remains a model after every refinement. By (A1), the refined SyGuS problem remains feasible. Fair enumeration (A2) eventually proposes the fixed retraction, even if other candidates are considered again after later partition changes. Its predicates satisfy the proof rules by (A3), so Verify accepts it. Hence SynRetract returns a verified retraction. □
Proof of Theorem 5.3 . Let the correct retractable fold from hypothesis (i) use the lifting specification ( τ a u x ⋆ , χ ⋆ ) (\tau_{\mathrm{aux}}^\star, \chi^\star) ( τ aux ⋆ , χ ⋆ ) . By (A3), its retraction and predicates satisfying the proof rules occur in the corresponding search spaces. By (A2), InferLiftSpec eventually considers this pair. Its auxiliary update makes the functional-consistency check succeed by Lemma D.3, while its retraction together with those predicates makes the retractability check succeed by Lemma D.4. Thus Theorem D.5 places the candidate in Σ \Sigma Σ .
The candidates in Σ \Sigma Σ are explored in increasing order. Fairness ensures that the loop reaches this candidate unless an earlier one already returns a verified fold, in which case Theorem 5.2 already gives the desired conclusion.
When the top-level loop reaches this candidate, its auxiliary update witnesses the hypotheses of Theorem D.2 , so Lift returns a lifted fold preserving χ ⋆ \chi^\star χ ⋆ . By (A3), the required retraction and predicates satisfying the proof rules are available for this lifted fold. Therefore Theorem D.8 makes SynRetract return a retraction accepted by Verify.
The top-level algorithm then returns the assembled fold. By Theorem 5.2 , it is equivalent to P P P modulo θ \theta θ . □
Received 2026-07-09; accepted 2026-10-05