Presburger arithmetic with threshold counting quantifiers is easy

Dmitry Chistikov ! Centre for Discrete Mathematics and its Applications (DIMAP) & Department of Computer Science, University of Warwick, Coventry, UK Christoph Haase ! Department of Computer Science, University of Oxford, Oxford, UK Alessio Mansutti ! Department of Computer Science, University of Oxford, Oxford, UK

Abstract We give a quantifier elimination procedures for the extension of Presburger arithmetic with aunary threshold counting quantifier ∃≥cy that determines whether the number of different y satisfying some formula is at least c ∈ N, where c is given in binary. Using a standard quantifier elimination procedure for Presburger arithmetic, the resulting theory is easily seen to be decidable in 4ExpTime. Our main contribution is to develop a novel quantifier-elimination procedure for a more general counting quantifier that decides this theory in 3ExpTime, meaning that it is no harder to decide than standard Presburger arithmetic. As a side result, we obtain an improved quantifier elimination procedure for Presburger arithmetic with counting quantifiers as studied by Schweikardt [ACM Trans. Comput. Log., 6(3), pp. 634-671, 2005], and a 3ExpTime quantifier-elimination procedure for Presburger arithmetic extended with a generalised modulo counting quantifier.

2012 ACM Subject Classification Theory of computation → Logic

Keywords and phrases Presburger arithmetic, counting quantifiers, quantifier elimination

Funding This work is part of a project that has received funding from the European Research Council (ERC) under the European Union’s Horizon 2020 research and innovation programme (Grant agreement No. 852769, ARiAT).

1 Introduction

Counting the number of solutions to an equation, or the number of elements in a set subject to constraints, is a fundamental problem in mathematics and computer science. In discrete geometry, a canonical #P-complete problem is to count the number of integral points in polyhedra. The celebrated due to Barvinok [2] solves this problem in polynomial time in fixed dimension. This and other powerful insights motivate the study of algorithmic aspects of the more general problem of counting the number of models of formulae in Presburger arithmetic, the first-order theory of the integers with and order, and more generally to considering counting extensions of this logic. arXiv:2103.05087v1 [cs.LO] 8 Mar 2021 It has long been known that the decision problem for Presburger arithmetic itself is recursively solvable [8] and that there is a quantifier elimination procedure for Presburger arithmetic running in 3ExpTime [7]. In this article, we study quantifier elimination pro- cedures for extensions of Presburger arithmetic with counting quantifiers. We are primarily interested in its extension with a unary threshold counting quantifier ∃≥cy, where c is given in binary. Given an assignment of integers to the first-order variables y, z1, . . . , zn, a formula ≥c ∃ y Ψ(y, z1, . . . , zn) evaluates to true whenever there are at least c different values of y satisfying Ψ(y, z1, . . . , zn). Note that the number of different y may depend on the values of ≥c the variables z1, . . . , zn. It is easily seen that a formula ∃ yΨ(y, z1, . . . , zn) can equivalently V V be expressed as ∃x1 · · · ∃xc 1≤i≤c Ψ(xi, z1, . . . , zn) ∧ 1≤i

given in binary, this translation incurs an exponential blow-up, and thus the extension of Presburger arithmetic with an ∃≥cy counting quantifier can only be decided in 4ExpTime using the standard quantifier elimination procedure for Presburger arithmetic. The main contribution of this paper is to develop a novel quantifier-elimination procedure that enables us to solve this decision problem in 3ExpTime, i.e., at no additional cost compared to standard Presburger arithmetic.

The counting quantifiers ∃≥xy and ∃=xy. The unary threshold counting quantifier we consider is a special instance of a more general unary counting quantifier ∃≥xy, which itself generalises the counting quantifier ∃=xy for Presburger arithmetic studied by Apelt [1] and Schweikardt [10]. Given an assignment of integers to the first-order variables x, z1, . . . , zn, ≥x a formula ∃ y Ψ(x, y, z1, . . . , zn) evaluates to true whenever the number of different y =x satisfying Ψ(x, y, z1, . . . , zn) is at least the value of x. The semantics of the ∃ y counting quantifier is defined analogously, but observe that ∃=xy does not hold when there is an infinite number of different y. Both [1] and [10] show of Presburger arithmetic extended with the counting quantifier ∃=xy by establishing a quantifier-elimination procedure. For our 3ExpTime algorithm for Presburger arithmetic extended with the ∃≥cy counting quantifier, we develop a novel quantifier-elimination procedure for the most general ∃≥xy counting quantifier. While this procedure a priori runs in non-elementary time, we show that it can be performed in 3ExpTime when specialised to ∃≥cy counting quantifiers. We remark that, crucially, such counting quantifiers are always unary, so as to keep the lo- =x gic decidable. Indeed, consider a binary counting quantifier ∃ (y1, y2) counting the number of =x different y1 and y2 satisfying a formula. Then the formula Φ(x, z) = ∃ (y1, y2)(0 ≤ y1, y2 < z) holds for x = z2, which in turn allows one to define , leading to undecidability of the resulting first-order theory. Note that for threshold counting, in contrast, non-unary quantifiers do not lead to undecidability. Thus, our results lead to the problem of eliminating such quantifiers in a resource-efficient manner, which we leave open.

The counting quantifier ∃(r,q)y. It is wide open whether there is an algorithm with element- ary running time that decides Presburger arithmetic extended with ∃=xy or ∃≥xy counting quantifiers. Moreover, at present, no stronger lower bounds than those established forplain Presburger arithmetic are known [3]. To shed more light on the complexity of Presburger arithmetic with an ∃=xy counting quantifier, Habermehl and Kuske [5] gave a quantifier- elimination procedure for eliminating a unary modulo counting quantifier ∃(r,q)y: here (r,q) ∃ y Ψ(y, z1, . . . , zn) holds whenever the number of different y satisfying Ψ(y, z1, . . . , zn) is congruent to r modulo q. An analysis of the growth of the constants and coefficients occurring in their quantifier-elimination procedure then enables them to derive an automata- based 3ExpTime algorithm for deciding Presburger arithmetic extended with the ∃(r,q)y counting quantifier. As a side result, we show that our quantifier elimination procedure gives a 3ExpTime upper bound for Presburger arithmetic with a generalised modulo counting (x,q) quantifier ∃ y Ψ(x, y, z1, . . . , zn) that evaluates to true when x is congruent modulo q to the number of different y satisfying Ψ, thereby strictly generalising the result of Habermehl and Kuske.

Key techniques. An advantage of our quantifier elimination procedure for the ∃≥xy and ∃=xy counting quantifiers is that it avoids the introduction of additional ∃- and ∀-quantifiers when eliminating a counting quantifier on which Schweikardt’s procedure10 [ ] relies. Her quantifier-elimination procedure replaces a counting quantifier ∃=xy with an equivalent quantified formula of Presburger arithmetic and requires a full transformation into disjunctive normal form. D. Chistikov, C. Haase, A. Mansutti 3

One key technique we employ is that, for Presburger arithmetic, it is possible to trans- form any Boolean combination of inequalities into a “disjoint” disjunctive normal form in polynomial time when the number of variables is fixed, see e.g.9 [ , 13]. This enables us to make use of a highly desirable disjunctive normal form for counting purposes without the drawback of non-elementary growth that repeated translation into usual disjunctive normal form normally entails. Another crucial ingredient of our quantifier eliminating procedure is evaluation of counting functions on bounded segments during the elimination process. This enables us to circumvent the introduction of additional standard first-order quantifiers that occurs in Schweikardt’s procedure. Putting these two ingredients together, we obtain a procedure that, in an analogue of nondeterministic guessing, pre-evaluates atomic predicates in the input formula. When a quantifier is eliminated, our “almost evaluated” formula is reduced to subformulae thatare introduced during this “guessing” and evaluation of the counts. Further related work. The counting quantifiers considered in this paper are derived from the so-called Härtig quantifier that enables reasoning about equicardinality between the sets defined by two formulae of first-order logic6 [ ]. For Presburger arithmetic, the aforementioned undecidability result imposes tight restrictions on how first-order variables counting integral points can be used. Woods [13] studied properties of Presburger counting functions: given a formula Φ(x1, . . . , xm, p1, . . . , pn) of Presburger arithmetic, the Presburger counting function associated to Φ is the function

m gΦ(p1, . . . , pn) = # {(x1, . . . , xm) ∈ N : Φ(x1, . . . , xm, p1, . . . , pn)} .

In [13], Woods shows that gΦ is a so-called piecewise quasi-polynomial. Computing such quasi-polynomials is of high relevance, for instance in numerous compiler optimisation approaches, see e.g. [11] and the references therein. More generally, Bogart et al. [4] have recently studied quantifier elimination procedures and counting problems for parametric Presburger arithmetic.

2 Presburger arithmetic with counting quantifiers

General notation. The symbols Z, N and N+ denote the set of integers, natural numbers including zero, and natural numbers without zero, respectively. We usually use a, b, c, . . . for integer numbers, which we assume being encoded in binary. Given n ∈ N, we write [n] =def {0, . . . , n − 1}. We write #A for the cardinality of a set A. If A is infinite, then #A = ∞, and we postulate n ≤ ∞ for all n ∈ Z.

Structure. We consider the structure Z = ⟨Z, (c)c∈Z, +, <, (≡q)q∈N+ ⟩, where (c)c∈Z are constant symbols that shall be interpreted as their homographic integer numbers, the binary function symbol + is interpreted as addition on Z, the binary relation < is interpreted as “less than”, and ≡q is interpreted as the modulo relation, i.e., a ≡q b iff q divides a − b. Basic syntax. Let X = {x, y, z, . . . } be a countable set of first-order variables. Linear terms, usually denoted by t, t1, t2, etc., are expressions of the form a1x1 + . . . adxd + c where x1, . . . , xd ∈ X, a1, . . . , ad, c ∈ Z. The integer ai is the coefficient of the variable xi. Variables not appearing in the linear term are tacitly assumed to have a 0 coefficient.A term t is said to be x-free if the coefficient of the variable x in t is 0. The integer c is the constant of the linear term. Linear terms with constant 0 are said to be homogeneous. Given a term t, the lexeme t < 0 is understood as a linear inequality, and t ≡ 0 mod q is a modulo constraint. Syntactically, Presburger arithmetic with counting quantifiers (PAC) is 4 Presburger arithmetic with threshold counting quantifiers is easy

the closure of linear inequalities and modulo constraints under the Boolean connectives ∧ and ¬ (i.e. conjunction and negation, respectively), the first-order quantifier ∃y and the (unary) counting quantifier ∃≥xy, where x, y ∈ X. We assume the two variables x and y appearing in a counting quantifier to be syntactically different. Formulae of PAC are denoted by Ψ, φ, ψ, γ, etc. We write vars(φ) and fv(φ) for the set of variables and free variables of φ, respectively. For the counting quantifier, we have fv(∃≥xy φ) = {x} ∪ (fv(φ) \{y}). We say that a formula φ is z-free if z ∈ X does not occur in φ. Given linear terms t and t′, we write φ[t′/t] for the formula obtained from φ by syntactically replacing every occurrence of t by t′.

Semantics. An assignment is a function ν : X → Z assigning an integer value to every variable. As usual, we extend ν in the standard way to a function that maps every term to an element of Z. For instance, ν(x + 3x + 2) = ν(x) + 3ν(y) + 2. Given a variable x and an integer n, we write ν[n/x] for the assignment obtained form ν by updating the value of x to n, i.e. ν[n/x](x) = n, and for all variables y distinct from x, ν[n/x](y) = ν(y). Given a formula φ of PAC and an assignment ν, the satisfaction relation ν |= φ is defined as usual for linear inequalities, modulo constraints, Boolean connectives and the existential quantifier ranging over Z. For the counting quantifier, we define

ν |= ∃≥xy φ if and only if #{n ∈ Z | ν[n/y] |= φ} ≥ ν(x). Informally, ∃≥xy φ is satisfied by ν whenever there are at least ν(x) distinct values for the variable y that make the formula φ true. A formula φ of PAC is satisfiable whenever there is an assignment ν such that ν |= φ. Two formulae φ and ψ are equivalent, written φ ↔ ψ, whenever they are satisfied by the same set of assignments. Syntactic abbreviations. We define ⊥ =def 0 < 0 and ⊤ =def ¬ ⊥. The Boolean connectives ∨, → and ↔ and the universal first-order quantifier ∀ are derived as usual, and so are the (in)equalities <, ≤, =, ≥, and >, between terms. For instance, t1 < t2 corresponds to t1 − t2 < 0, where we tacitly manipulate t1 − t2 with standard operation of linear arithmetic in order to obtain an equivalent term. Given two terms t1 and t2, and q ∈ N+, we write t1 ≡q t2 for the modulo constraint t1 − t2 ≡ 0 mod q. For a variable x ∈ X and r ∈ [q], we call x ≡q r a simple modulo constraint. All modulo constraints introduced by our quantifier elimination procedure given in Section 3 are simple. We now introduce the counting quantifiers ∃≥cy, ∃=xy and ∃(x,q)y, where x, y ∈ X, c ∈ Z and q ≥ 1, and both c and q are encoded in binary. Let ν be an assignment. Informally, ν |= ∃≥cy φ if and only if there are at least c values for the variable y that make φ true. Similarly, ν |= ∃=xy φ if and only if there are exactly ν(x) values for the variable y that make φ true. Finally, ν |= ∃(x,q)y φ if and only if the number of values for the variable y that make φ true is congruent to ν(x) modulo q. The formal definition of these three counting quantifiers is given below, where z is a variable not occurring in fv(φ),

∃≥cy φ =def ∃z (z = c ∧ ∃≥zy φ), ∃=xy φ =def (∃≥xy φ) ∧ ¬∃z (z = x + 1 ∧ ∃≥z φ), (x,q) def =z ∃ y φ = ∃z (z ≡q x ∧ ∃ y φ).

The counting quantifier ∃=xy is the counting quantifier considered in10 [ ], whereas the modulo counting quantifier ∃(x,q)y is a generalisation of the quantifier ∃(r,q)y introduced in [5], where r is a fixed instead of a variable. More precisely, ∃(r,q)y φ is equivalent to (x,q) ≥1 ∃x (x ≡q r ∧ ∃ y φ). Finally, notice that the formula ∃y φ is equivalent to ∃ y φ. This fact enables us to eliminate a standard first-order existential quantifier by slightly tweaking the quantifier-elimination procedure for ∃≥cy φ. D. Chistikov, C. Haase, A. Mansutti 5

Parameters of formulae. Following Oppen [7] and Weispfenning [12], we establish bounds on the absolute value of the variable assignments that suffice for deciding satisfiability of φ. To this end, we introduce a set of parameters of formulae of PAC:

|φ| denotes the length of the formula φ, i.e., the number of symbols to write down φ, with numbers encoded in binary, lin(φ) is the set of all linear terms t that appear in a linear inequality t < 0 of φ (recall that t1 < t2 is syntactic sugar for t1 − t2 < 0), hom(φ) is the set of homogeneous linear terms obtained from all terms in lin(φ) by setting their constants to 0, and mod(φ) is the set of all moduli q ∈ N appearing in a modulo constraint t1 ≡q t2 of φ. We always assume 1 ∈ mod(φ), even if φ has no modulo constraints.

For A ⊆ Z finite set, we write ||A|| = max{|n| | n ∈ A} for the absolute-value norm of A. For a term t, ||t|| it the maximum coefficient or constant appearing in t, in absolute value. For a set of terms T , ||T || =def max{||t|| | t ∈ T }. For a formula φ, we define ||φ|| as ||lin(φ) ∪ mod(φ)||.

3 A quantifier elimination procedure for unary counting quantifiers

In this section, we develop a quantifier elimination procedure (QE procedure) for the counting quantifier ∃≥xy that allows us to establish the following result.

≥x ▶ Theorem 1. Let φ be quantifier-free. Then ∃ y φ is equivalent to a Boolean combination of linear inequalities and simple modulo constraints.

Our QE procedure perform a series of formula manipulations that we divide into five steps. At the end of the i-th step, the procedure produces a formula Ψi equivalent to the original ≥x formula ∃ y φ. Ultimately, Ψ5 is a Boolean combination of inequalities and simple modulo constraints allowing us to establish Theorem 1. In this section, we present the procedure and briefly discuss its correctness, leaving the computational analysis of parameters lin(Ψ5), hom(Ψ5) and mod(Ψ5) to subsequent sections.

≥x Step I: Normalise the coefficients of the variable y. Given the input formula Ψ0 = ∃ y φ, the first step of the procedure is a standard step for QE procedures for Presburger arithmetic. It produces an equivalent formula Ψ1 in which all non-zero coefficients of y appearing in a linear term are normalised to 1 or −1. For simplicity, we first translate every modulo constraint in φ into simple modulo constraints, by relying on the lemma below.

▶ Lemma 2. Every modulo constraint t ≡q 0 is equivalent to a disjunction ψ of simple modulo constraints such that vars(ψ) ⊆ vars(t ≡q 0) and mod(ψ) = {q}. Here is the first step of the procedure:

Using Lemma 2, translate every modulo constraint in φ into simple modulo constraints. Let k be the lcm of the absolute values of all coefficients of y appearing in hom(φ). Let φ′ be the formula obtained from φ by applying the following three rewrite rules to each linear inequality and simple modulo constraint in which y appears, where t is a term, q ≥ 1 and r ∈ [q]: ay + t < 0 −→ ky + (k/a) · t < 0, if a > 0, ay + t < 0 −→ −ky − (k/a) · t < 0, if a < 0, and y ≡q r −→ ky ≡kq kr. 6 Presburger arithmetic with threshold counting quantifiers is easy

def ≥x ′ Let Ψ1 = ∃ y (y ≡k 0 ∧ φ [y/ky]).

▷ Claim 3. Ψ0 ↔ Ψ1, and in Ψ1, all non-zero coefficients of y are either 1 or −1.

Step II: Subdivide the formula according to term orderings and residue classes. We define an ordering of n linear terms to be a formula of the form

(t1 ◁1 t2) ∧ (t2 ◁2 t3) ∧ · · · ∧ (tn−1 ◁n−1 tn), (1)

where {t1, . . . , tn} is the set being ordered and {◁1,..., ◁n−1} ⊆ {<, =}.

▶ Lemma 4. There is an algorithm that, given a set T of n linear terms over d variables, com- O(d) O(1) W putes in time n log ||T || a set {O1,...,Oo} of orderings for T such that (I) i∈[1,o] Oi 2d is a tautology, (II) for every i ̸= j in [1, o], Oi ∧ Oj is unsatisfiable, and (III) o = O(n ).

Our QE procedure manipulates Ψ1 as follows:

Let T be the set of all y-free terms t such that t, y − t or −y + t belong to lin(Ψ1). def W Using Lemma 4, build a disjunction of orderings ψord = i∈[1,o] Oi for the terms T ∪ {0}. Let Z = vars(φ) and m = lcm(mod(Ψ1)). def V For each i ∈ [1, o] and every r : Z → [m], let Γi,r = Oi ∧ ( z∈Z z ≡m r(z)). def W W Let Ψ2 = i∈[1,o] r : Z→[m] (Γi,r ∧ Ψ1).

▷ Claim 5. Ψ1 ↔ Ψ2. In Steps III to V of the procedure, we focus on each disjunct separately, iterating over all pairs of i ∈ [1, o] and r : Z → [m].

≥x Step III: Split the range of y into segments. Recall that Ψ1 = ∃ y ψ, where ψ is some Boolean combination of inequalities and modulo constraints with variables from vars(φ), in ′ ′ which the non-zero coefficients of y are either 1 or −1 (by Claim 3). Let t1, . . . , tℓ be all of the terms T ∪ {0} that the formula Oi asserts pairwise non-equal, taken in the ascending order. ′ ′ In other words, we obtain t1, . . . , tℓ by removing from the sequence t1, . . . , tn in Equation (1) all terms tj+1 for which ◁j is =. Let seg(y, Oi) be the set of formulae

 ′ ′ ′ ′ ′ ′ ′ ′ ′ y < t1, y = t1, (t1 < y ∧ y < t2), y = t2, ... , (tℓ−1 < y ∧ y < tℓ), y = tℓ, tℓ < y .

We have #(seg(y, Oi)) = 2ℓ + 1. Given κ ∈ seg(y, Oi), the formula Oi ∧ κ imparts a linear ordering on the terms T ∪ {0, y}. This enables us to “almost evaluate” the formula ψ:

i,r ▷ Claim 6. For every κ ∈ seg(y, Oi), there is a Boolean combination ψκ of simple modulo i,r i,r i,r constraints s.t. vars(ψκ ) = {y}, mod(ψκ ) ⊆ mod(ψ) and Γi,r ∧ κ ∧ ψ ↔ Γi,r ∧ κ ∧ ψκ . The procedure continues as follows:

Let seg(y, O ) = {κ , . . . , κ } and, for every j ∈ [0, 2ℓ], take ψi,r from Claim 6. i 0 2ℓ κj i,r def   Let Ψ = ∃x ... ∃x x ≤ x + ··· + x ∧ V ∃≥xj y(κ ∧ ψi,r) . 3 0 2ℓ 0 2ℓ j∈[0,2ℓ] j κj def W W i,r Let Ψ3 = i∈[1,o] r : Z→[m](Γi,r ∧ Ψ3 ).

▷ Claim 7. Ψ2 ↔ Ψ3. D. Chistikov, C. Haase, A. Mansutti 7

Step IV: Compute the number of solutions for each segment. We next aim to eliminate the counting quantifiers introduced in Step III in the sub-formulae ∃≥xj y(κ ∧ ψi,r). We j κj go over each κ ∈ seg(y, Oi), and consider three cases depending on whether it specifies (syntactically) an infinite interval, a finite segment, or a single value for y. Notice that r is in fact an assignment to variables, so r(t) ∈ Z is well-defined for every term t with free variables Z. Compute the following numbers for j ∈ [1, ℓ]:

′ i,r ′ cj is 1 if the assignment y 7→ r(tj) satisfies ψκ where κ = (y = tj) and 0 otherwise. For j ∈ [2, ℓ]:

i,r pj ∈ [0, m] is the number of y ∈ [m] satisfying ψκ , ′ ′ uj = (r(tj−1) mod m) and uj is the smallest integer congruent to r(tj) mod m and > uj, ′ i,r rj ∈ [0, m] is the number of y ∈ [uj + 1, uj − 1] satisfying ψκ , 2 2 ′ rj ∈ [−m , m ] and rj is −pj · (uj − uj) + m · rj.

i,r ′ ▶ Lemma 8. Given a formula ψκ and m, uj, uj, the numbers pj and rj can be computed i,r in #P, or by a deterministic algorithm with running time O(m · |ψκ |).

The numbers cj, pj, rj determine, for each formula κ ∈ seg(y, Oi), how many assignments i,r i,r to the variable y satisfy the formula ψκ in the conjunction Γi,r ∧ κ ∧ ψκ . Intuitively, this is ′ ′ ′ ′ ′ cj for κ of the form y = tj, and (pj(tj − tj−1) + rj)/m for κ of the form tj−1 < y ∧ y < tj. We say “intuitively” here, because in the latter case the expression above depends on other variables so is not, strictly speaking, a number. The following claims formalise this.

′ ′ i,r i,r ▷ Claim 9. Let κ ∈ {y < t1, tℓ < y}. If ∃y (κ ∧ ψκ ) is satisfiable, then Γi,r ∧ Ψ3 ↔ Γi,r.

′ ▷ Claim 10. Let κ be the formula y = tj for some j ∈ [1, ℓ] and let z be a fresh variable. ≥z i,r Then Γi,r ∧ ∃ y (κ ∧ ψκ ) ↔ Γi,r ∧ z ≤ cj.

′ ′ ▷ Claim 11. Let κ be the formula tj−1 < y ∧ y < tj for some j ∈ [2, ℓ] and let z be a fresh ≥z i,r ′ ′ variable. Then Γi,r ∧ ∃ y (κ ∧ ψκ ) ↔ Γi,r ∧ mz ≤ pj(tj − tj−1) + rj.

The procedure replaces each disjunct of Ψ3 with a new formula as follows:

i,r def i,r ′ ′ Let Ψ4 = ⊤ if ∃y (κ ∧ ψκ ) is satisfiable for some κ ∈ {y < t1, tℓ < y}; otherwise i,r def  Pℓ Pℓ V ′ ′  let Ψ4 = ∃x2 ... ∃xℓ x ≤ j=2 xj + j=1 cj ∧ j∈[2,ℓ] mxj ≤ pj(tj − tj−1) + rj . def W W i,r Let Ψ4 = i∈[1,o] r : Z→[m](Γi,r ∧ Ψ4 ).

▷ Claim 12. Ψ3 ↔ Ψ4.

Step V: Sum up the numbers of solutions. It remains to get rid of the variables xi i,r introduced earlier. For each disjunct Γi,r ∧ Ψ4 of Ψ4, we use the notation from Step IV.

i,r def i,r Let Ψ5 = ⊤ if Ψ4 = ⊤; otherwise i,r def Pℓ ′ ′ Pℓ let Ψ5 = mx ≤ j=2(pj(tj − tj−1) + rj) + m · j=1 cj. def W W i,r Let Ψ5 = i∈[1,o] r : Z→[m](Γi,r ∧ Ψ5 ).

The procedure terminates with Ψ5 as output. The following claim implies Theorem 1.

▷ Claim 13. Ψ4 ↔ Ψ5. The formula Ψ5 is quantifier-free. 8 Presburger arithmetic with threshold counting quantifiers is easy

4 Discussion and summary of results, and roadmap

The QE procedure for a single counting quantifier ∃≥xy from Section 3 forms the basis of our results. In this section we discuss its use and lay out its applications.

Analysis of the procedure. The next lemma tells us how fast formulae and their parameters grow in our QE procedure.

▶ Lemma 14. Let the formula Ψ5 be obtained by applying the quantifier elimination procedure from Section 3 to a formula ∃≥yx φ, where φ is quantifier-free and #vars(φ) = d. Then:

#hom(φ) mod(Ψ5) = {m} with m = k · lcm(mod(φ)) and k ≤ ||hom(φ)|| , O(d) #lin(Ψ5) ≤ N , ||lin(Ψ5)|| ≤ O(N) · ||lin(φ)||, O(d) 2 #hom(Ψ5) ≤ N , ||hom(Ψ5)|| ≤ O(N) · ||hom(φ)||, where N = m · #lin(φ).

A trivial consequence of Lemma 14 is that the QE procedure from Section 3 gives an algorithm for deciding a formula φ of Presburger arithmetic with counting quantifiers ∃≥zy . . 2 in time 2. , where the height of the tower is at most O(|φ|).

=x ▶ Remark 15. With minor changes to the procedure, one can eliminate quantifiers ∃ y in addition to ∃≥xy, with the same complexity bounds as in Lemma 14. Because of space constraints, this is only briefly described in Section 6 and further details are relegated to Appendix D. Let us pinpoint where the non-elementary blow-up appears if the procedure is applied multiple times to eliminate all quantifiers from a formula. Putting together the upper bounds and equations given by Lemma 14 for #hom(Ψ5), N, m, and k, we observe that the upper bound for #hom(Ψ5) is exponential in #hom(φ). This means that more fine-grained bounds are necessary for decision procedures with elementary complexity, i.e., with running time bounded from above by a k-fold exponential in the size of the input formula.

Tracing the exponential dependence of #hom(Ψ5) on #hom(φ) back to the QE procedure, one can see that the quantity k from Lemma 14 stems from computing the least common multiple of the coefficients at y in Step I of the procedure. Each of them can be as big as ||hom(φ)||, and there can be #hom(φ)-many of them. Unfortunately, there does not appear to be a stronger upper bound on the magnitude of their common multiple, even in ′ ′ subsequent rounds of the QE procedure. Indeed, y-free terms t1, . . . , tℓ in the remaining i,r variables do not only get subtracted from one another in Ψ5 , but also get multiplied by i,r factors pj as they are in formulae Ψ4 . These factors, computed at the beginning of Step IV, ′ ′ represent the limit density of suitable assignments for y in the intervals tj−1 < y < tj that i,r are long enough. As such, they are model counts of univariate quantifier-free formulae ψκ , so a priori nothing prevents many different factors pj from taking different values in the range [0, m] and contributing to a big least common multiple in the next QE round.

3ExpTime decision procedures. We will now explain how this growth of parameters can be countered for more restricted quantifiers, arriving at a 3ExpTime quantifier elimination and decision procedures. This analysis relies on and extends Lemma 14. The following theorem is our main result.

▶ Theorem 16. There is a 3ExpTime quantifier elimination procedure for Presburger arithmetic with threshold counting quantifiers ∃≥cy. D. Chistikov, C. Haase, A. Mansutti 9

In essence, the procedure of Theorem 16 is our main QE procedure from Section 3 that treats the quantifier ∃≥cy as if it were ∃≥zy. After substituting c for z at the end, we are able to improve the bound on #hom(Ψ5) from Lemma 14. This results in an elementary decision procedure. We discuss details in Section 5. Similarly to the case of quantifiers ∃=xy mentioned in Remark 15, with very minor changes to the procedure from Section 3 one can eliminate quantifiers ∃(x,q)y too, with the same complexity bounds as in Lemma 14.

▶ Theorem 17. There is a 3ExpTime quantifier elimination procedure for Presburger arithmetic with modulo counting quantifiers ∃(x,q)y.

A proof outline for Theorem 17 is given in Section 6.

5 Eliminating threshold counting quantifiers

In this section, we extend the quantifier elimination procedure of Section 3 in order to directly deal with the threshold counting quantifiers ∃≥cy. Afterwards, we provide the complexity analysis of the quantifier elimination procedure. ≥c Consider a formula Ψ0 = ∃ y φ with φ quantifier free. By definition, Ψ0 is equivalent to the formula ∃z (z = c ∧ ∃≥zy φ) for some variable z not occurring in φ. In order to eliminate the threshold counting quantifier ∃≥cy, we first perform the quantifier elimination procedure ≥z described in Section 3 on input ∃ y φ, obtaining the formula Ψ5. We then eliminate the existential quantifier ∃z from the formula ∃z (z = c ∧ Ψ5) by relying on the ad-hoc procedure we now describe. As explained in Section 4, the prefix “∃z (z = c ∧” allows us to drastically simplify the set of homogeneous terms in Ψ5, leading to 3ExpTime.

Dealing with threshold quantifiers in a single step. With Ψ0 and Ψ5 defined as above, we have Ψ0 ↔ ∃z (z = c ∧ Ψ5) ↔ Ψ5[c/z]. Recall that Ψ5 is defined as

def W W i,r Ψ5 = i∈[1,o] r : Z→[m](Γi,r ∧ Ψ5 ).

Here, Z = vars(φ), m = lcm(mod(Ψ1)) (defined as in the Step II of the procedure) and Γi,r is a conjunction of inequalities and simple modulo constraints with variables from Z (hence, z-free). i,r Therefore, Γi,r[c/z] = Γi,r. Moreover, Ψ5 is either ⊤ or a formula of the form

′ ′ ′ ′ mz ≤ p2(t2 − t1) + r2 + ··· + pℓ(tℓ − tℓ−1) + rℓ + m(c1 + ··· + cℓ). (2)

′ ′ where the terms t1, . . . , tℓ are from T ∪ {0} (with T defined as in Step II of Section 3),and thus written with variables from Z. Therefore, the following property holds:

▷ Claim 18. In Ψ5, z only appears on the left hand side of inequalities of the form (2).

We manipulate the disjuncts of Ψ5[c/z] separately. Fix i ∈ [1, o] and r : Z → [m]. We i,r i,r define a formula equivalent to Ψ5 [c/z] by relying on the following lemma, where d = vars(Ψ5 ). i,r Pℓ Pℓ ▶ Lemma 19. Consider Ψ5 as in (2). Let e = m(c − j=1 cj) − j=2 rj. It is possible to compute in time (e+ℓ)O(d) log(c·||Ψi,r||)O(1) a formula γ = W V t′ − t′ ≥ i 5 i,r (i2,...,iℓ)∈I j∈[2,ℓ] j j−1 j ℓ 2d i,r such that (1) I ⊆ [0, e] , (2) #I ≤ O((e + ℓ) ), and (3) Γi,r ∧ Ψ5 [c/z] ↔ Γi,r ∧ γi,r.

′ ′ To prove Lemma 19, we apply Lemma 4 on the set of terms {tj − tj−1 | j ∈ [2, ℓ]} ∪ [0, e], i,r and manipulate the resulting tautology to filter out orderings that do not satisfy Ψ5 [c/z]. The QE procedure proceeds as follows. 10 Presburger arithmetic with threshold counting quantifiers is easy

For every i ∈ [1, o] and r : Z → [m],

i,r i,r def if Ψ5 = ⊤, then let Ψ6 = ⊤, i,r def else let Ψ6 = γi,r, according to Lemma 19. c def W W i,r Let Ψ6 = i∈[1,o] r : Z→[m](Γi,r ∧ Ψ6 ).

c ′ ′ After defining Ψ6, the procedure ends. Notice that the inequalities tj − tj−1 ≥ ij that ′ ′ replace the inequalities given in (2) are such that tj−1, tj ∈ T ∪ {0}. This leads to a better c bound on the size of the set hom(Ψ6) (more precisely, quadratic on #hom(φ)), which ultimately enables us to establish the 3ExpTime membership of Presburger arithmetic with threshold counting quantifiers.

c c ▷ Claim 20. Ψ0 ↔ Ψ6. The formula Ψ6 is quantifier-free.

Proof idea for Theorem 16 The key role in the analysis is played by the following lemma.

c 2 ▶ Lemma 21. #hom(Ψ6) = O(#hom(φ) ). As already stated, this quadratic bound is key in order to obtain an elementary decision procedure. In particular, this improvement over the “baseline” Lemma 14 leads to the following bounds on the elimination of an arbitrary number of threshold counting quantifiers.

▶ Lemma 22. Let φ be a formula of Presburger arithmetic with threshold quantifiers. There is an equivalent quantifier-free formula Ψ such that

2 2O(|φ| ) #lin(Ψ), ||lin(Ψ)||, ||hom(Ψ)|| and ||mod(Ψ)|| are at most 22 ,

O(|φ|2) #hom(Ψ) ≤ 22 and #mod(Ψ) ≤ |φ|.

Proof idea. In a nutshell, elementary upper bounds of Lemma 22 are obtained by first iterating Lemma 21 across all quantifier elimination rounds. This results in a doubly c exponential bound on the cardinalities of sets hom(Ψ6) throughout the entire procedure. With this bound in hand, exponentiation in the right-hand side of the inequalities of Lemma 14 does not blow the parameters above triple exponential. ◀ Theorem 16 follows by combining Lemma 22 with upper bounds on the running time of a single quantifier elimination round. These upper bounds are all subsumed by the sizeofthe obtained formulae, except possibly for the procedures of Lemmas 4 and 19, and the model counting procedure of Lemma 8. For Lemmas 4 and 19, the running time is only exponential in the size of the original formula, and thus runs in polynomial time on the size of the obtained formula, as soon as this formula has size at least exponential. For Lemma 8, observe that the factor m in the running time cannot be more than the product of all elements of the set hom(Ψ). Hence, the bounds of Lemma 22 suffice for a triply exponential time overall.

6 Eliminating modulo counting quantifiers

(x,q) Consider a formula Ψ0 = ∃ y φ, where φ is quantifier-free. By definition, Ψ0 is equivalent =z to ∃z(z ≡q x ∧ ∃ y φ), where z is a variable not occurring in φ. Thus, in order to eliminate a modulo counting quantifier, it makes sense to piggyback on a quantifier elimination procedure for the ∃=xy counting quantifier. Due to space constraints, we only briefly describe the main aspects of eliminating an ∃=xy counting quantifier, further details can be found D. Chistikov, C. Haase, A. Mansutti 11 in Appendix D. In its essence, the quantifier elimination procedure mirrors Steps I-V ofthe i,r i,r procedure described in Section 3, replacing the inequalities in the definitions of Ψ3 and Ψ4 i,r in Steps 3 and 4 with an equality. However, in addition in Step 4, instead of setting Ψ4 to i,r ⊤ when there is an infinite number of solutions, Ψ4 is set to ⊥ to capture the semantics of the ∃=xy quantifier. Apart from that, the quantifier-elimination procedure forthe ∃=xy quantifier inherits all the properties of the one described in Section 3. = Let Ψ5 be the formula obtained from performing the quantifier-elimination procedure for =x =z = = the ∃ y counting quantifier on ∃ y φ, so that Ψ0 ↔ ∃z(z ≡q x ∧ Ψ5 ). We have that Ψ5 is defined as

= def W W i,r Ψ5 = i∈[1,o] r : Z→[m](Γi,r ∧ Ψ5 ), V where Z = vars(φ), m = lcm(mod(Ψ1)) and Γi,r = Oi ∧ ( w∈Z w ≡m r(w)) is a conjunction of an ordering Oi and simple modulo constraints w ≡m r(w) with variables from Z (and i,r so, z-free). Moreover, Ψ5 is either ⊥ or a formula of the form

Pℓ ′ ′ Pℓ mz = j=2(pj(tj − tj−1) + rj) + m · j=1 cj. (3)

′ ′ where the terms t1, . . . , tℓ are from T ∪ {0} (where T is defined as in Step II of Section 3), and hence z-free. Analogously to Section 5, Claim 18, the following property holds.

= ▷ Claim 23. In Ψ5 , z only appears on the left hand side of equalities of the form (3). = We manipulate ∃z(z ≡q x ∧ Ψ5 ) with the following steps, denoted by VI and VII to stress the fact that they are performed after the five steps of the ∃=xy quantifier elimination procedure. Step VI: Subdivide the formula according the residue classes (again). To efficiently = eliminate the existential quantifier of the formula ∃z(z ≡q x ∧ Ψ5 ), we first guess the residue = classes of all variables in Z ∪ {x} modulo mq (instead of just m, as done in Ψ5 for the variables in Z). W W W V i,r Let γ = s :(Z∪{x})→[mq] i∈[1,o] r : Z→[m](( w∈Z∪{x} w ≡mq s(w)) ∧ Γi,r ∧ Ψ5 ). For every s: (Z ∪ {x}) → [mq], every i ∈ [1, o] and every r : Z → [m], consider the V i,r disjunct ( w∈Z∪{x} w ≡mq s(w)) ∧ Γi,r ∧ Ψ5 of the formula γ and evaluate every modulo constraint w ≡m r(w) in Γi,r (w ∈ Z) to ⊤ or ⊥, according to the truth of s(w) ≡m r(w). Since every function r : Z → [m] can be seen as a partial function from Z ∪ {x} to [mq], after the two steps above, for every s: (Z ∪ {x}) → [mq] and i ∈ [1, o], all but one disjunct of the W V i,r subformula r : Z→[m](( w∈Z∪{x} w ≡mq s(w)) ∧ Γi,r ∧ Ψ5 ) of γ evaluate ⊥. We conclude that γ is equivalent to W W i,s i∈[1,o] s :(Z∪{x})→[mq](Γi,s ∧ Ψ5 ),

def V i,s where Γi,s = Oi ∧ ( w∈Z∪{x} w ≡mq s(w)) and Ψ5 is ⊥ or a formula as described in (3). (x,q) def W W i,s Let Ψ6 = i∈[1,o] s :(Z∪{x})→[mq] ∃z(z ≡q x ∧ Γi,s ∧ Ψ5 ), the following holds:

= (x,q) ▷ Claim 24. ∃z(z ≡q x ∧ Ψ5 ) ↔ Ψ6 .

Step VII: Eliminate existential quantifiers. We conclude the procedure by manipulating (x,q) each disjunct of Ψ6 separately. Fix i ∈ [1, o] and s: (Z ∪ {x}) → [mq]. We aim at defining i,s (x,q) a quantifier-free formula γi,s equivalent to the disjunct ∃z(z ≡q x ∧ Γi,s ∧ Ψ5 ) of Ψ6 .

i,s If Ψ5 =⊥ then let γi,s =⊥. 12 Presburger arithmetic with threshold counting quantifiers is easy

i,s Else, Ψ5 has the form in Equation (3). By modular arithmetic, z ≡q x ↔ mz ≡mq mx. Pℓ ′ ′ Pℓ Consider the formula mx ≡mq j=2(pj(tj − tj−1) + rj) + m · j=1 cj. By Claim 23, this formula has variables from Z ∪ {x}. Evaluate this formula on s. If it is found to be def def equivalent to ⊥, let γi,s = ⊥. Otherwise, let γi,s = Γi,s. (x,q) def W W Let Ψ7 = i∈[1,o] s :(Z∪{x})→[mq] γi,s.

(x,q) (x,q) After defining Ψ7 , the procedure ends. Notice that all the disjuncts of Ψ7 are either ⊥ V or Γi,s = Oi ∧ w∈Z∪{x} w ≡mq s(w), where vars(Oi) ⊆ Z ∪{x}. The (in)equalities appearing ′ ′ in Oi are of the form t ◁ t , where ◁ ∈ {<, =} and t, t ∈ T ∪ {0}. Exactly as in the case of (x,q) 2 threshold quantifiers, this leads to #hom(Ψ7 ) ≤ (#T ∪ {0}) , which ultimately leads to a 3ExpTime running time for the QE procedure.

(x,q) (x,q) (x,q) ▷ Claim 25. Ψ6 ↔ Ψ7 . The formula Ψ7 is quantifier-free.

Proof idea for Theorem 17 The key role in the analysis is played by the following lemma.

(x,q) 2 ▶ Lemma 26. #hom(Ψ7 ) = O(#hom(φ) ). When eliminating an arbitrary number of ∃(x,q)y, Lemma 26 leads to the following result.

▶ Lemma 27. Let φ be a formula of Presburger arithmetic with threshold quantifiers. There is an equivalent quantifier-free formula Ψ such that

2 2O(|φ| ) #lin(Ψ), ||lin(Ψ)||, ||hom(Ψ)|| and ||mod(Ψ)|| are at most 22 ,

O(|φ|2) #hom(Ψ) ≤ 22 and #mod(Ψ) ≤ |φ|.

Lemma 27 allows us to establish that the QE procedure for Presburger arithmetic with modulo counting quantifiers runs in 3ExpTime. The proof follows the pattern of Theorem 16.

7 Conclusion

We developed a QE procedure for Presburger arithmetic extended with the unary threshold counting quantifier ∃≥cy that runs in 3ExpTime, i.e., at no additional cost compared to standard QE procedures for Presburger arithmetic, see e.g. [7]. From the estimation of the growth of the constants occurring in our QE procedure, using standard relativisation arguments, see e.g. [12], we can derive that the decision problem for Presburger extended with the ∃≥c quantifier is in 2ExpSpace. This matches the complexity of deciding standard Presburger arithmetic closely. Indeed, the latter is complete for the complexity class poly(n) STA(∗, 22 ,O(n)) [3]. Fully settling the complexity of Presburger arithmetic extended with ∃≥cy will likely require generalising the STA complexity measure, which we leave as an interesting avenue for further investigation. Our QE procedure is based on a QE procedure for the more general ∃≥xy counting quantifier that we developed in this paper. While the latter procedure slightly improves the QE procedure given by Schweikardt [10], it still only runs in non-elementary time. We have pinpointed precisely at where the non-elementary growth occurs. It remains to be seen whether our QE procedure can be further improved, or whether, possibly based on the insights obtained from our QE procedure, a non-elementary lower bound for Presburger arithmetic extended with the ∃≥xy quantifier can be established. D. Chistikov, C. Haase, A. Mansutti 13

References 1 Harry Apelt. Axiomatische Untersuchungen über einige mit der Presburgerschen Arithmetik verwandte Systeme. Math. Log. Q., 12(1):131–168, 1966. 2 Alexander I. Barvinok. A polynomial time algorithm for counting integral points in polyhedra when the dimension is fixed. Math. Oper. Res., 19(4):769–779, 1994. 3 Leonard Berman. The complexitiy of logical theories. Theor. Comput. Sci., 11:71–77, 1980. 4 Tristram Bogart, John Goodrick, Danny Nguyen, and Kevin Woods. Parametric Presburger arithmetic: complexity of counting and quantifier elimination. Math. Log. Q., 65(2):237–250, 2019. 5 Peter Habermehl and Dietrich Kuske. On Presburger arithmetic extended with modulo counting quantifiers. In FoSSaCS, volume 9034 of Lecture Notes in Computer Science, pages 375–389. Springer, 2015. 6 Heinrich Herre, Michał Krynicki, Alexandr Pinus, and Jouko Väänänen. The Härtig quantifier: A survey. J. Symb. Comput., 56(4):1153–1183, 1991. 2pn 7 Derek C. Oppen. A 22 upper bound on the complexity of Presburger arithmetic. J. Comput. Syst. Sci., 16(3):323–332, 1978. 8 Mojżesz Presburger. Über die Vollständigkeit eines gewissen Systems der Arithmetik ganzer Zahlen, in welchem die Addition als einzige Operation hervortritt. In Comptes Rendus du I congres de Mathematiciens des Pays Slaves, pages 92–101. 1929. 9 Bruno Scarpellini. Complexity of subcases of Presburger arithmetic. Trans. Am. Math. Soc, 284:203–218, 1984. 10 Nicole Schweikardt. Arithmetic, first-order logic, and counting quantifiers. ACM Trans. Comput. Log., 6(3):634–671, 2005. 11 Sven Verdoolaege, Rachid Seghir, Kristof Beyls, Vincent Loechner, and Maurice Bruynooghe. Counting integer points in parametric polytopes using Barvinok’s rational functions. Algorith- mica, 48(1):37–66, 2007. 12 Volker Weispfenning. The complexity of almost linear diophantine problems. J. Symb. Comput., 10(5):395–404, 1990. 13 Kevin Woods. Presburger arithmetic, rational generating functions, and quasi-polynomials. J. Symb. Log., 80(2):433–449, 2015. 14 Presburger arithmetic with threshold counting quantifiers is easy

A Missing proofs from Section 3

▶ Lemma 2. Every modulo constraint t ≡q 0 is equivalent to a disjunction ψ of simple modulo constraints such that vars(ψ) ⊆ vars(t ≡q 0) and mod(ψ) = {q}.

Proof. Let Z = vars(t ≡q 0). We guess the residue classes of the variables in Z, as shown in the right hand side of the following equivalence: W V t ≡q 0 ↔ r : Z→[q](t ≡q 0 ∧ z∈Z z ≡q r(z)). V Fix r : Z → [q], and consider the disjunct (t ≡q 0 ∧ z∈Z z ≡q r(z)). Let x be a variable occurring in t. As r assigns to x a residue class modulo q, the following equivalence holds: V V t ≡q 0 ∧ z∈Z z ≡q r(z) ↔ (t ≡q 0)[r(x)/x] ∧ z∈Z z ≡q r(z). Therefore, by substituting in t every variable x with r(x), we derive V V t ≡q 0 ∧ z∈Z z ≡q r(z) ↔ r(t) ≡q 0 ∧ z∈Z z ≡q r(z).

Since r(t) ≡q 0 does not have free variables (i.e. it is a statement), it is equivalent to ⊤ or ⊥. W V Let ψr ∈ {⊤, ⊥} such that r(t) ≡q 0 ↔ ψr, and let ψ = r : Z→[q](ψr ∧ z∈Z z ≡q r(z)). The formula ψ satisfies the required properties. ◀

▷ Claim 3. Ψ0 ↔ Ψ1, and in Ψ1, all non-zero coefficients of y are either 1 or −1. Proof. We show the following sequence of equivalences:

≥x ′ Ψ0 ↔ ∃ y φ (4) ↔ ∃≥xy ∃z(z = ky ∧ φ′[z/ky]) for some fresh variable z (5) ↔ ∃≥xz ∃y (z = ky ∧ φ′[z/ky]) (6) ≥x ′ ↔ ∃ z (z ≡k 0 ∧ φ [z/ky]) (7) ≥x ′ ↔ ∃ y (y ≡k 0 ∧ φ [y/ky]) = Ψ1. (8)

The equivalence (4) holds, because the rewrite rules used to produce φ′ from φ come from ′ biconditional axioms of (modular) arithmetic, e.g. a ≡b c ↔ ka ≡kb kc, for all k ≥ 1. In φ , all non-zero coefficients of y are either k or −k. This directly establishes equivalence (5). ′ In the formula φ [z/ky] (and thus in Ψ1) all non-zero coefficients of y are either 1 or −1. The equivalence (6) holds as the expression z = ky induces a bijection between all possible values of y and z. The equivalence (7) holds as y does not occur in φ′[z/ky]. Notice that z ≡k 0 ↔ ∃y z = ky. The equivalence (8) follows as we rename z by y. ◁

▶ Lemma 4. There is an algorithm that, given a set T of n linear terms over d variables, com- O(d) O(1) W putes in time n log ||T || a set {O1,...,Oo} of orderings for T such that (I) i∈[1,o] Oi 2d is a tautology, (II) for every i ̸= j in [1, o], Oi ∧ Oj is unsatisfiable, and (III) o = O(n ). Proof. We first show the existence of a family of orderings with required properties. This part of the proof relies on the insight that n hyperplanes split Rd into O(nd) regions. This is the basis of multiple “geometric” decision procedures and ; see, e.g., [9, 13].

d ▷ Claim 28. Given s1, . . . , sm linear terms over d variables, there are at most O(m ) conjunctions of the form

(s1R10) ∧ (s2R20) ∧ ... ∧ (smRm0),

where Ri ∈ {<, =, >}, that are satisfiable over the reals R. D. Chistikov, C. Haase, A. Mansutti 15

Proof. This is a small variation of the classic proof, using double induction. For d = 1, the number of such conjunctions is clearly at most 2m + 1, because m points can split the line into (at most) m + 1 finite or infinite open intervals and m points themselves. For larger d, we proceed as follows. We assume an O(md−1) bound for dimension d − 1. For m = 1, the number of satisfiable conjunctions is at most 3. Let us deal with larger m now. Suppose we have already computed (or, rather, bounded from above) the number of satisfiable conjunctions of terms s1, . . . , sj−1, and suppose this number is N. Consider what happens when the term sj is added to them. Each region of R that corresponds to one of the N satisfiable conjunctions of s1, . . . , sj−1 can be “cut” by the new term into at most 3 regions, according to whether sj < 0, sj = 0, or sj > 0 (and thus adding 2 new regions). This is the only way new regions, and thus satisfiable conjunctions of the form (s1R10) ∧ ... ∧ (sjRj0) are composed. However, we can now observe that the number of regions that are “cut” is not, in general, as big as N. Indeed, the number of regions that are d “cut” is bounded from above by the number of regions inside the set {x ∈ R | sj = 0} formed d−1 by the terms s1, . . . , sj−1. But this number is O((j −1) ) by the bound for dimension d−1. Therefore, for dimension d we obtain an overall bound of

m X d−1 d O(1) + 2 ·O((j − 1) ) = O(m ). ◀ j=2

2d ▷ Claim 29. Given t1, . . . , tn linear terms over d variables, there are at most O(n ) orderings that satisfy properties (I) and (II).

n Proof. This is a consequence of Claim 28. Indeed, we can form m = 2 terms of the form ti − tj, i ̸= j. For any valuation to the d variables, the signs of these m terms determine d 2d an ordering of t1, . . . , tn, satisfiable over the reals. Therefore, we obtain O(m ) = O(n ) orderings in total. ◁ Given Claim 29, let us now proceed to the second, algorithmic part of the proof. The idea can be seen as dynamic programming. Our algorithm runs as follows. Let t1, . . . , tn be the terms from the statement of the lemma. We construct several families of orderings, incrementally: family Fj is the required family for the the first j terms, t1, . . . , tj. For j = 1, the family F1 is trivial. For j = 2, . . . , n, we compute Fj from Fj−1 as follows. Start from Fj = ∅. For each ordering o from Fj−1, enumerate all possible positions to insert tj into it. There are at most 2j − 1 possible options here; their precise number depends on the number of equalities (as opposed to inequalities) among the relations on t1, . . . , tj. For each of these options, check if the resulting ordering is satisfiable when the variables are interpreted over R, using any polynomial-time algorithm for linear programming. If so, add it to Fj, otherwise just skip it. In the end, Fn is a family of orderings with the required properties. Let us analyse this algorithm. From the (non-algorithmic) part of the lemma, already proved above as Claim 29, we know that there are O(j2d) orderings for j terms. For each of 2d+1 these orderings, we will try to insert tj+1 at O(j) possible positions. So there are O(j ) satisfiability checks to run. Over all j, this is O(n2d+2) checks. Notice that it is sufficient for us to look at satisfiability over the reals or rationals here, as long as the number of orderings we get is not too high. Indeed, if some ordering is satisfiable over R (or, equivalently, over Q) but not satisfiable over Z, then we may still include it. This means that we can rely on polynomial-time algorithms for linear programming. Each instance has at most n constraints over d variables. The bit size of each coefficient be bounded by 16 Presburger arithmetic with threshold counting quantifiers is easy

def b = log maxj ||tj||. Therefore, a satisfiability check for a system of constraints of this form can be run in time poly(b, n, d). The overall running time for the entire algorithm is

2d+2 O(1) O(d) O(1) O(1) O(d) O(1) O(n )(bnd) = n (nd) b = n log max ||tj|| . j ◀

▷ Claim 5. Ψ1 ↔ Ψ2. W V Proof. Let ψrem be the formula r : Z→[m] z∈Z z ≡m r(z) whose disjuncts represent a combination of residue classes modulo m for the variables in Z. We have

Ψ1 ↔ ψord ∧ ψrem ∧ Ψ1 (9)

↔ Ψ2. (10)

The equivalence (9) follows from the fact that both the formulae ψord and ψrem are tautologies. W From ψord ∧ ψrem ∧ Ψ1, we distribute the conjunctions over the disjunctions given by i∈[1,o] W and r : Z→[m], which shows equivalence (10). ◁

i,r ▷ Claim 6. For every κ ∈ seg(y, Oi), there is a Boolean combination ψκ of simple modulo i,r i,r i,r constraints s.t. vars(ψκ ) = {y}, mod(ψκ ) ⊆ mod(ψ) and Γi,r ∧ κ ∧ ψ ↔ Γi,r ∧ κ ∧ ψκ .

Proof. Let κ be fixed. We recall that the formulae Oi were constructed based on the set of terms that includes 0. This means, in particular, that for all assignments that satisfy the conjunction Γi,r ∧ κ (if any exist) the truth value of all inequalities that occur in the formula ψ is the same. Indeed, for inequalities not involving the variable y this is because the formula Oi asserts or implies the sign of every linear term. For inequalities involving y, this is due to our choice of the set seg(y, Oi). We now consider modulo constraints that occur in ψ. Those of them where the variable y does not appear also evaluate to just true or false on all assignments satisfying Γi,r ∧ κ, because r specifies residue classes modulo m = lcm(mod(ψ)) for all variables except y. Since y can only occur with coefficient 1 or −1, all the remaining modulo constraints become simple, i.e., take the form y ≡q r for some q ∈ mod(ψ). To sum up, replacing all constraints in the ψ part of the formula Γi,r ∧ κ ∧ ψ with their truth values or their simplified form, as described above, we obtain an equivalent formula i,r Γi,r ∧ κ ∧ ψκ , as required. ◁

▷ Claim 7. Ψ2 ↔ Ψ3. i,r Proof. Let i ∈ [1, o], r : Z → [m]. Establishing Γi,r ∧ Ψ1 ↔ Γi,r ∧ Ψ3 suffices, where Ψ1 = i,r   ∃≥xy ψ and Ψ = ∃x ... ∃x x ≤ x + ··· + x ∧ V ∃≥xj y(κ ∧ ψi,r) . Directly 3 0 2ℓ 0 2ℓ j∈[0,2ℓ] j κj i,r from Claim 6, The formula Γi,r ∧ Ψ3 is equivalent to   def V ≥xj γ = Γi,r ∧ ∃x0 ... ∃x2ℓ x ≤ x0 + ··· + x2ℓ ∧ j∈[0,2ℓ] ∃ y(κj ∧ ψ) where we notice that all the formulae of the form ψi,r are substituted with ψ. Proving the κj equivalence Γi,r ∧ Ψ1 ↔ γ is rather straightforward. (⇐): Let ν be an assignment such that ν |= Ψ1 ↔ γ. Therefore, there are values v0, . . . , v2ℓ for the variables x0, . . . , x2ℓ such V ≥xj that ν[v0/x0, . . . , v2ℓ/x2ℓ] |= x ≤ x0 + ··· + x2ℓ ∧ j∈[0,2ℓ] ∃ y(κj ∧ ψ). Since the variables V ≥vj x0, . . . , x2ℓ do not appear in κj ∧ ψ, we have ν |= x ≤ v0 + ··· + v2ℓ ∧ j∈[0,2ℓ] ∃ y(κj ∧ ψ). ′ Lastly, in view of the definition of the set seg(y, Oi), given κ, κ ∈ seg(y, Oi) there is no value v for y such that ν[v/y] |= κ ∧ ψ and ν[v/y] |= κ′ ∧ ψ. We conclude that there are at P2ℓ least j=0 vj distinct values v for y such that ν[v/y] |= ψ, and thus ν |= Γi,r ∧ Ψ1, directly from ν |= x ≤ v0 + ··· + v2ℓ. D. Chistikov, C. Haase, A. Mansutti 17

(⇒): Suppose ν |= Γi,r ∧ Ψ1. So, there are at least ν(x) distinct values v for y such that ν[v/y] |= ψ. Given j ∈ [0, 2ℓ], let vj be the number distinct values for y such that ′ ν[v/y] |= κj ∧ ψ. Again from the fact that, given κ, κ ∈ seg(y, Oi) there is no value v for y ′ P2ℓ such that ν[v/y] |= κ ∧ ψ and ν[v/y] |= κ ∧ ψ, we conclude that ν(x) ≤ j=0 vj. Thus, V ≥xj ν[v0/x0, . . . , v2ℓ/x2ℓ] |= x ≤ x0 + ··· + x2ℓ ∧ j∈[0,2ℓ] ∃ y(κj ∧ ψ), and so ν |= γ. ◁

i,r ′ ▶ Lemma 8. Given a formula ψκ and m, uj, uj, the numbers pj and rj can be computed i,r in #P, or by a deterministic algorithm with running time O(m · |ψκ |).

′ Proof. We notice that each of the numbers pj and rj are defined by counting the number of i,r i,r solutions of ψκ in a finite interval. This formula is quantifier-free, and vars(ψκ ) = {y}. If the formula and the interval bounds are given as input, then counting these numbers is a #P problem. One can enumerate all possible values of y and check each of them against the formula. Since uj − uj ≤ m, there are at most m values to check in each of the intervals. ◀

′ ′ i,r i,r ▷ Claim 9. Let κ ∈ {y < t1, tℓ < y}. If ∃y (κ ∧ ψκ ) is satisfiable, then Γi,r ∧ Ψ3 ↔ Γi,r.

def ′ Proof. We focus without loss of generality on the case κ = (y < t1). Suppose the formula i,r ∃y (κ ∧ ψκ ) is satisfiable. It suffices to show that, for every assignment µ that satisfies the i,r formula Γi,r, the formula Ψ3 is satisfied by µ too. i,r Recall that the formula Ψ3 was defined previously as   ^ ∃x ... ∃x x ≤ x + ··· + x ∧ ∃≥xj y(κ ∧ ψi,r) . 0 2ℓ  0 2ℓ j κj  j∈[0,2ℓ]

Let us take an arbitrary assignment µ that satisfies the formula Γi,r. Our goal is to show that the formula above is satisfied by µ too. We have our κ ∈ seg(y, Oi), and we assume without ′ loss of generality that κ = κ0. Consider a new assignment µ obtained from µ by updating ′ the values of all x1, . . . , x2ℓ to 0 and the value of x0 to x. Clearly, µ satisfies the inequality x ≤ x + ··· + x . For each j ∈ [1, 2ℓ], the formula ∃≥xj y(κ ∧ ψi,r) is now satisfied too, 0 2ℓ j κj because µ′(x ) = 0, so it remains to argue that µ′ satisfies ∃≥xy(κ ∧ ψi,r) where κ = κ. j 0 κ0 0 ′ Let us capitalise on the fact that κ is just a single inequality, y < t1. First, note ′ ′ that setting the value of y to any integer strictly smaller than µ (t1) satisfies κ. Second, i,r recall from Step III (and Claim 6) that the formula ψκ is a Boolean combination of i,r i,r simple modulo constraints with vars(ψκ ) = {y} and that mod(ψκ ) ⊆ mod(ψ). So our i,r previous choice of m = lcm(mod(Ψ1)) is a multiple of lcm(mod(ψκ )), and thus the set of all i,r assignments (for y) that satisfy the formula ψκ is periodic with period m. Importantly, this i,r set is non-empty because the formula ∃y (κ ∧ ψκ ) was assumed to be satisfiable (and thus i,r ψκ ̸↔⊥). Therefore, this set contains all elements of some infinitely descending sequence ′ ′ with difference m. Therefore, setting the value of y to any such element smaller than µ (t1) satisfies the formula (κ ∧ ψi,r). This completes the proof, as there are indeed infinitely many 0 κ0 such values, whilst the formula ∃≥xy(κ ∧ ψi,r) asserts the existence of just µ′(x) of them, 0 κ0 ′ for some µ (x) ∈ Z. ◁ ′ ▷ Claim 10. Let κ be the formula y = tj for some j ∈ [1, ℓ] and let z be a fresh variable. ≥z i,r Then Γi,r ∧ ∃ y (κ ∧ ψκ ) ↔ Γi,r ∧ z ≤ cj. Proof. For every assignment ν to variables other than y, there cannot be more than one i,r ′ value of y that satisfies the formula κ ∧ ψκ . Indeed, since κ is y = tj, then the only possible ′ i,r choice for y is ν(tj). If this choice satisfies ψκ ), then z can be chosen to be any integer less than or equal to 1. Otherwise, there is no update to ν by any value assigned to y that would 18 Presburger arithmetic with threshold counting quantifiers is easy

i,r make the formula κ ∧ ψκ satisfied, in which case z can be set to all non-positive integers (and only to them). ◁

′ ′ ▷ Claim 11. Let κ be the formula tj−1 < y ∧ y < tj for some j ∈ [2, ℓ] and let z be a fresh ≥z i,r ′ ′ variable. Then Γi,r ∧ ∃ y (κ ∧ ψκ ) ↔ Γi,r ∧ mz ≤ pj(tj − tj−1) + rj.

′ Proof of Claim 11. For every assignment ν to variables other than y, take L = ν(tj−1) + 1 ′ and U = ν(tj) and consider the segment of integers [L, U − 1], using the convention [a, b] = ∅ if a > b. These are all the potential values of y that satisfy the formula κ, which under ′ ′ the conditions of the Claim has the form tj−1 < y ∧ y < tj. We need to determine how i,r many of these values actually satisfy the larger formula κ ∧ ψκ , and this number will be the maximum possible value attained by the variable z. i,r As in the proof of Claim 9, recall from Step III (and Claim 6) that the formula ψκ i,r is a Boolean combination of simple modulo constraints with vars(ψκ ) = {y} and that i,r mod(ψκ ) ⊆ mod(ψ). So our previous choice of m = lcm(mod(Ψ1)) is a multiple of i,r i,r lcm(mod(ψκ )), and thus the set of all assignments (for y) that satisfy the formula ψκ is i,r periodic with period m. Formally, denote S = {n ∈ Z | (y 7→ n) |= ψκ } and note that n ∈ S iff n + m ∈ S. The technical hurdle we need to overcome in this proof is that the values of L and U depend on the assignment ν. Importantly, it suffices to consider assignments that satisfy Γi,r, V i.e., the formula Oi ∧ ( z∈Z z ≡m r(z)). We focus on the modulo constraints in this formula and, from now on, we assume that ν satisfies all of them. Our goal is to compute the cardinality of the set [L, U − 1] ∩ S, which by the arguments above is exactly the number i,r of variable assignments to y that satisfy κ ∧ ψκ , if all other variables have already been assigned values by ν. ′ ′ ′ ′ ′ Let L = r(tj−1)+1 and U = min{r(tj)+m·h | h ∈ Z}∩[L , +∞). These two numbers are almost the same as uj and uj, respectively, but we will use the capital letter notation to keep symbols for different segment endpoints uniform. Now L′ ≤ U ′, #[L′,U ′ − 1] = U ′ − L′ ∈ [0, m − 1] and, because of our assumption about ν, L ≡ L′ mod m and U ≡ U ′ mod m. (For the proof of these congruences, observe that, firstly, r(z) ≡ ν(z) mod m because ν satisfies Γi,r. This implies that ar(z) ≡ aν(z) mod m for all a ∈ Z. Summing up several congruences ′ of this kind results in another congruence, of the form r(t) ≡ ν(t) mod m. Setting t = tj−1 +1 ′ and t = tj concludes the proof.) We are now ready to compute the cardinality of [L, U − 1] ∩ S. As S is periodic with period m, we will split [L, U − 1] into two disjoint parts: [L, U − 1] = [L, L∗ − 1] ∪ [L∗,U − 1], where L∗ is the largest integer not exceeding U and congruent to L modulo m. We consider each part separately:

As L∗ is congruent to L modulo m, it is clear that the integer segment [L, L∗ − 1] consists of zero, one, two or more copies of a full period of S. Therefore,

L∗ − L L∗ − L #([L, L∗ − 1] ∩ S) = #([0, m − 1] ∩ S) · = p · . m j m For the second part, observe that all three numbers L, L′, and L∗ are congruent modulo m; similarly, U ′ and U are congruent modulo m. By definition of L∗, we have #[L∗,U − 1] = U − L∗ ∈ [0; m − 1]. Therefore, the following two constraints hold:

#[L∗,U − 1] = #[L′,U ′ − 1] and L∗ ≡ L′ mod m. D. Chistikov, C. Haase, A. Mansutti 19

By periodicity of S, for all v ∈ [L∗,U − 1] we have

v ∈ S if and only if v + (L′ − L∗) ∈ S

and therefore

∗ ′ ′ ′ #([L ,U − 1] ∩ S) = #([L ,U − 1] ∩ S) = rj.

Let us sum up the results above. Due to the semantics of the quantifier ∃≥zy, the constraint on the variable z is equivalent to the following one: L∗ − L z ≤ p · + r′ , j m j

∗ ′ which is the same as m · z ≤ pj · (L − L) + m · rj. It remains to return to the original terms, “undoing” the variable assignment ν. Observe that

L∗ − L = (U − L) − (U − L∗) = (U − L) − (U ′ − L′).

′ ′ ′ ′ In the constraint, instead of U − L we write tj − (tj−1 + 1), and the value of U − L can be ′ ′ computed from r(tj−1) and r(tj) using simple arithmetic. Putting everything together, we obtain

′ ′ ′ ′ ′ m · z ≤ pj · (tj − tj−1 − 1 − (U − L )) + m · rj.

′ ′ ′ Since U − L ∈ [0, m − 1] and rj ∈ [0, pj], the following bounds hold:

′ ′ ′ ′ 2 pj · (−1 − (U − L )) + m · rj ≤ m · rj ≤ m , ′ ′ ′ 2 pj · (−1 − (U − L )) + m · rj ≥ −pj · m ≥ −m .

This completes the proof. ◁

▷ Claim 12. Ψ3 ↔ Ψ4. Proof. Follows directly from Claim 9, Claim 10 and Claim 11, together with simple formulae manipulations. ◁

▷ Claim 13. Ψ4 ↔ Ψ5. The formula Ψ5 is quantifier-free. i,r i,r i,r Proof. Let i ∈ [1, o], r : Z → [m]. Establishing Γi,r ∧ Ψ4 ↔ Γi,r ∧ Ψ5 suffices. If Ψ4 = ⊤ i,r then Ψ5 is defined as ⊤ and the equivalence holds. Otherwise, we have

 ℓ ℓ  i,r def X X ^ ′ ′ Ψ4 = ∃x2 ... ∃xℓ x ≤ xj + cj ∧ mxj ≤ pj(tj − tj−1) + rj j=2 j=1 j∈[2,ℓ] ℓ ℓ i,r def X ′ ′ X Ψ5 = mx ≤ (pj(tj − tj−1) + rj) + m · cj. j=2 j=1

i,r i,r (⇒): It is easy to see that Ψ5 is obtained from Ψ4 by first multiplying both sides of the Pℓ Pℓ ′ ′ inequality x ≤ j=2 xj + j=1 cj by m, and then substituting mxj with pj(tj − tj−1) + rj. i,r i,r (⇐): Let ν be an assignment such that ν |= Γi,r ∧ Ψ5 . We show that ν |= Ψ4 . First of all, ′ ′ we consider j ∈ [2, ℓ], and aim at showing that ν(pj(tj − tj−1) + rj)) is a multiple of m. We recall the definition of rj, uj and uj, as introduced in Step IV: 20 Presburger arithmetic with threshold counting quantifiers is easy

′ rj = −pj · (uj − uj) + m · rj, ′ ′ uj = r(tj−1) and uj is the smallest integer congruent to r(tj) mod m and > uj.

′ ′ ′ ′ ′ By definition of rj, the term pj(tj − tj−1) + rj is equivalent to pj(tj − tj−1 − uj + uj) + m · rj. ′ ′ Since ν |= Γi,r, we have uj ≡m ν(tj−1) and uj ≡m ν(tj). From axioms of modular arithmetic, ′ ′ ′ ′ ′ ν(tj) − ν(tj−1) − uj + uj ≡m 0, and thus pj(ν(tj) − ν(tj−1) − uj + uj) + m · rj ≡m 0, ′ ′ which allows us to conclude that ν(pj(tj − tj−1) + rj)) is a multiple of m. Therefore, for ′ ′ every j ∈ [2, ℓ], there is vj ∈ Z such that m·vj = ν(pj(tj −tj−1)+rj)). Let x2, . . . , xℓ be fresh variables. We consider the assignment ν[v2/x2, . . . , vℓ/xℓ] that updates ν by assigning vj to the variable xj, for every j ∈ [2, ℓ]. We have,

Pℓ Pℓ V ′ ′ ν[v2/x2, . . . , vℓ/xℓ] |= mx ≤ j=2 mxj + m · j=1 cj ∧ j∈[2,ℓ] mxj = pj(tj − tj−1) + rj.

Divide both side of the leftmost inequality by m ∈ N, and weaken the equalities of the form ′ ′ ′ ′ mxj = pj(tj − tj−1) + rj to inequalities of the form mxj ≤ pj(tj − tj−1) + rj. We obtain

Pℓ Pℓ V ′ ′ ν[v2/x2, . . . , vℓ/xℓ] |= x ≤ j=2 xj + · j=1 cj ∧ j∈[2,ℓ] mxj ≤ pj(tj − tj−1) + rj. By definition of the existential quantifier,

 Pℓ Pℓ V ′ ′  ν |= ∃x2 ... ∃xℓ x ≤ j=2 xj + · j=1 cj ∧ j∈[2,ℓ] mxj ≤ pj(tj − tj−1) + rj .

i,r That is, ν |= Ψ4 . ◁

B Missing proofs from Section 4

In this appendix, we provide the computational analysis on the parameters lin(.), hom(.) and mod(.), of the formula obtained form the elimination of the quantifier ∃≥xy via the procedure of Section 3.

Let φ be a quantifier-free formula, and let d = #fv(φ). Consider the formula Ψ5 obtained by performing the quantifier-elimination procedure of Section 3 on the formula ∃≥xy φ.

The following lemma restates Lemma 14 by expressing the bounds on Ψ5 explicitly.

▶ Lemma 30. The following bounds are established for Ψ5:

#hom(φ) #mod(Ψ5) = {m} with m = k · lcm(mod(φ)) and k ≤ O(||hom(φ)|| ). O(d) #lin(Ψ5) and #hom(Ψ5) are bounded by (m · #lin(φ)) , 2 ||lin(Ψ5)|| ≤ O(m · #lin(φ) · ||lin(φ)||), 2 ||hom(Ψ5)|| ≤ O(m · #lin(φ) · ||hom(φ)||).

Proof. First of all, from Lemma 2, we notice that translating every modulo constraint appearing in φ into simple modulo constraints does not change the sets lin(φ), hom(φ) and mod(φ). Therefore, assume φ to be a Boolean combination of linear inequalities and simple modulo constraints. Let k be the lcm of the absolute values of all coefficients in y appearing in hom(φ). We have k ≤ ||hom(φ)||#hom(φ). The first step essentially multiplies every term in φ by k, producing the formula Ψ1 with bounds

#lin(Ψ1) = #lin(φ) and ||lin(Ψ1)|| ≤ k||lin(φ)||,

#hom(Ψ1) = #hom(φ) and ||hom(Ψ1)|| ≤ k||hom(φ)||,

mod(Ψ1) = {kq | q ∈ mod(φ)}. D. Chistikov, C. Haase, A. Mansutti 21

Let T be the set of all y-free terms t such that t, y − t or −y + t belong to lin(Ψ1). So, #T ≤ #lin(Ψ1), ||T || ≤ ||lin(Ψ1)|| and all coefficients of variables in terms of T are bounded by ||hom(Ψ1)||. Let m = lcm(mod(Ψ1)) = k · lcm(mod(φ)). In the second step of the ′ ′ procedure, the orderings introduce terms t ◁ t , where ◁ ∈ {<, =} and t, t ∈ T ∪ {0}. So, at most (#T ∪ {0})2 new terms are introduces, increasing lin(.) and hom(.) quadratically in cardinality. The magnitude of coefficients and constants doubles. Simple modulo constraints of the form x ≡m r are also introduced. Because of this, the formula Ψ2 produced in the second step of the procedure has the following bounds:

2 #lin(Ψ2) ≤ (#lin(Ψ1) + 1) + #lin(Ψ1) and ||lin(Ψ2)|| ≤ 2||lin(Ψ1)||, 2 #hom(Ψ2) = (#hom(Ψ1) + 1) + #hom(Ψ1) and ||hom(Ψ2)|| ≤ 2||hom(Ψ1)||,

mod(Ψ2) = {m} ∪ mod(Ψ1).

To study the bounds on the formula Ψ5, analysing the bounds obtained from the third and fourth steps of the procedure is unnecessary. Indeed, we recall that Ψ5 is defined as W W i,r Ψ5 = i∈[1,o] r : Z→[m](Γi,r ∧ Ψ5 ), where every Γi,r is a conjunction of simple modulo constraints of the form z ≡m r and linear i,r inequalities from Ψ2, and every Ψ5 is either ⊤ or a formula of the form Pℓ ′ ′ Pℓ mx ≤ j=2(pj(tj − tj−1) + rj) + m · j=1 cj where ℓ ≤ #T + 1 ≤ #lin(Ψ1) + 1, for every j ∈ [1, ℓ] cj ∈ {0, 1}, and for every j ∈ [2, ℓ], 2 2 ′ ′ pj ∈ [0, m] and |rj| ∈ [−m , m ] (see Claim 11) and the terms tj and tj−1 belongs to i,r T . This implies that variable coefficients in Ψ5 are bounded (in absolute value) by m · (2 · ℓ · ||hom(Ψ1)|| + 1), whereas the constant term is bounded by 2 · ℓ · m · ||lin(Ψ1)|| + 2 ℓ · m + m, again in absolute values. By recalling that the number of disjunctions of Ψ5 is mdo ≤ md(2(#(T ∪ {0})2 + 1)d (see Lemma 4 for the bound on o), we derive #hom(φ) mod(Ψ5) = {m}, and so ||mod(Ψ5)|| ≤ lcm(mod(φ)) · ||hom(φ)|| , 2 2 d d #lin(Ψ5) ≤ (#lin(Ψ1) + 1) + (2(#lin(Ψ1) + 1) + 1) m ≤ O(md · #lin(φ)2d) 2 ||lin(Ψ5)|| ≤ 2 · ℓ · m · ||lin(Ψ1)|| + ℓ · m + m ≤ O(m2 · #lin(φ) · ||lin(φ)||) 2 2 d d #hom(Ψ5) ≤ (#hom(Ψ1) + 1) + (2(#lin(Ψ1) + 1) + 1) m ≤ O(md · #lin(φ)2d)

||hom(Ψ5)|| ≤ m · (2 · ℓ · ||hom(Ψ1)|| + 1) 2 ≤ O(m · #lin(φ) · ||hom(φ)||). ◀

C Missing proofs from Section 5

i i,r In the lemma below, we recall that d = vars(Ψ5, r) and that Ψ5 has the following form (see Equation (2) in the body of the paper):

Pℓ ′ ′ Pℓ mx ≤ j=2(pj(tj − tj−1) + rj) + m · j=1 cj i,r Pℓ Pℓ ▶ Lemma 19. Consider Ψ5 as in (2). Let e = m(c − j=1 cj) − j=2 rj. It is possible to compute in time (e+ℓ)O(d) log(c·||Ψi,r||)O(1) a formula γ = W V t′ − t′ ≥ i 5 i,r (i2,...,iℓ)∈I j∈[2,ℓ] j j−1 j ℓ 2d i,r such that (1) I ⊆ [0, e] , (2) #I ≤ O((e + ℓ) ), and (3) Γi,r ∧ Ψ5 [c/z] ↔ Γi,r ∧ γi,r. 22 Presburger arithmetic with threshold counting quantifiers is easy

′ ′ ′ Proof. Consider the set of e + ℓ terms T = {tj − tj−1 | j ∈ [2, ℓ]} ∪ [0, e]. Note that i,r ′ i,r e ≤ 2 · c · ||Ψ5 ||, and so ||T || ≤ O(c · ||Ψ5 ||). Applying Lemma 4, we compute a set ′ ′ ′ def W ′ ′ 2d {O1,...,Oo} of orderings for T such that ψ = k∈[1,o] Ok is a tautology and o = O((e+ℓ) ). ′ ′ ′ ′ ′′ ′ Since Ok is an ordering for T , for all k ∈ [1, o ] and t , t ∈ T , exactly one of the entailments ′ ′ ′′ ′ ′ ′′ ′ ′ ′′ Ok |= t < t , Ok |= t = t or Ok |= t > t holds. ′ We iterate over all k ∈ [1, o ], at each step generating a formula ψk that satisfies

i,r Γi,r ∧ Ψ5 [c/z] ↔ (Γi,r ∧ ψk). (11)

At the end of the process, the formula ψo′ is the formula γi,r required by the lemma. Let W i,r ′ i,r ψ0 = k∈[1,o](Ψ5 [c/z] ∧ Ok). Since ψ is a tautology and ψ0 ↔ Ψ5 [c/z] ∧ ψ, the formula ′ ′ ψ0 satisfies the equivalence in (11). Let n = #T = ℓ + e. For all k ∈ [1, o ], suppose

′ Ok = b1 ◁1 b2 ∧ · · · ∧ bn−1 ◁n−1 bn

′ where {b1, . . . , bn} = T and {◁1,..., ◁n−1} ⊆ {<, =}. We inductively assume that ψk−1 satisfies the equivalence (11), and we compute ψk following the cases below. Notice that ′ ′ checking which of the cases is satisfied by Ok can be done in linear time with respect to |Ok|, by simply scanning the ordering.

′ ′ case: Ok does not respect the order 0 < 1 < ··· < e. Then, Ok is unsatisfiable and ψk i,r ′ is obtained from ψk−1 by removing the disjunct Ψ5 [c/z] ∧ Ok. Since ψk−1 satisfies the equivalence (11), so does ψk. ′ ′ ′ i,r ′ ′ case: Ok |= tj − tj−1 < 0, for some j ∈ [2, ℓ] . Since Ψ5 |= tj−1 < tj, the formula i,r ′ Ψ5 [c/z] ∧ Ok is unsatisfiable. Again, ψk is obtained from ψk−1 by removing the disjunct i,r ′ Ψ5 [c/z] ∧ Ok, and ψk satisfies the equivalence (11). ′ ′ ′ otherwise, for every j ∈ [2, ℓ] there is ij ∈ [0, e] such that either Ok |= ij = tj − tj−1 or ′ ′ ij = e and Ok |= ij < tj − tj−1. By simply parsing of the ordering, can find all the ij in ′ Pℓ i,r ′ time O(|Ok|). Now, if e ≤ j=2 pj · ij does not hold, then the formula Ψ5 [c/z] ∧ Ok is unsatisfiable and, as in the previous cases, we define ψk from ψk−1 by removing this disjunct. We obtain a formula that satisfies the equivalence (11). Otherwise, let V ′ ′ i,r γ = j∈[2,ℓ] tj −tj−1 ≥ ij. By definition, Ok |= γ and γ |= Ψ5 [c/z]. Let ψk be the formula i,r ′ obtained from ψk−1 by replacing the disjunct Ψ5 [c/z] ∧ Ok by the formula γ. Notice ′ that ψk−1 |= ψk, directly from Ok |= γ. We show that ψk satisfies the equivalence (11). i,r (⇒): Let ν be an assignment such that ν |= Γi,r ∧ Ψ5 . Since ψk−1 satisfies the equivalence (11), we have ν |= Γi,r ∧ ψk−1. By ψk−1 |= ψk, we derive ν |= Γi,r ∧ φk. (⇐): Let ν be an assignment such that ν |= Γi,r ∧ ψk. If ν satisfies a disjunct of ψk that is different from γ, then ν |= ψk−1 and, since ψk−1 satisfies the equivalence (11), i,r i,r i,r ν |= Γi,r ∧Ψ5 . Otherwise, ν |= γ and, by γ |= Ψ5 [c/z] we deduce that ν |= Γi,r ∧Ψ5 [c/z].

def As already said, the formula γi,r = ψo′ . The formula γi,r satisfies all the expected properties. ℓ In particular, I ⊆ [0, e] holds by definition of the various ij in the third case of the procedure, and #I ≤ O((e + ℓ)2d) holds from the bound on the number o′ of disjuncts of ψ. By Lemma 4, computing the initial formula ψ can be done in time (e + ℓ)O(d) log ||T ′||O(1). Similarly, the case analysis on the disjuncts of ψ has a overall running time that is linear in O(d) ′ O(1) |ψ| ≤ (e + ℓ) log ||T || . ◀ c c ▷ Claim 20. Ψ0 ↔ Ψ6. The formula Ψ6 is quantifier-free. c c Proof. By definition of Ψ6 and Lemma 19, Ψ5 ↔ Ψ6. Then, the claim follows from the chain of claims “Ψi ↔ Ψi+1” starting from Claim 3 and ending with Claim 13. ◁ D. Chistikov, C. Haase, A. Mansutti 23

c ▶ Lemma 31. The following bounds are established for Ψ6:

c #hom(φ) mod(Ψ6) = {m} with m = k · lcm(mod(φ)) and k ≤ ||hom(φ)|| , c 2 c #hom(Ψ6) ≤ O(#hom(φ) ) and ||hom(Ψ6)|| ≤ O(k · ||hom(φ)||), c 2 2 c 2 #lin(Ψ6) ≤ O(c · m · #lin(φ) ) and ||lin(Ψ6)|| ≤ O(k · c · m · #lin(φ) · ||lin(φ)||). Proof. Without loss of generality, we assume #hom(φ), #lin(φ), ||hom(φ)|| and ||lin(φ)|| to be at least 1. We also assume c to be at least 1, as otherwise the formula ∃≥cy φ is trivially true. These assumptions hide constant factors in the exponent. We recall the bounds (as in Lemma 30) on the formula Ψ1 obtained after performing the normalisation of the coefficients of y, as described in Step I of Section 3. We have

#lin(Ψ1) = #lin(φ) and ||lin(Ψ1)|| ≤ k||lin(φ)||,

#hom(Ψ1) = #hom(φ) and ||hom(Ψ1)|| ≤ k||hom(φ)||. where k ≤ ||hom(φ)||#hom(φ) is the lcm of all coefficients of y appearing in linear inequalities. c W W i,r We recall that the formula Ψ6 is defined as i∈[1,o] r : Z→[m](Γi,r ∧Ψ6 ), where Z = fv(φ), V m = k · lcm(mod(φ)), Γi,r = Oi ∧ ( w∈Z w ≡m r(w)), where Oi is an ordering on the set of i,r terms T ∪ {0}, and Ψ6 is either ⊤ or of the form (see Lemma 19) W V t′ − t′ ≥ i . (i2,...,iℓ)∈I j∈[2,ℓ] j j−1 j ′ ′ Here, ℓ ≤ #T +1 and, for all j ∈ [2, ℓ], tj, tj−1 ∈ T and ij ∈ [0, e] where e ≤ m·(c+(m+1)·ℓ). Moreover, #T ≤ #lin(Ψ1), ||T || ≤ ||lin(Ψ1)|| and all coefficients of variables in terms of T are bounded by ||hom(Ψ1)||. So, when accounting for all orderings (Oi)i∈[1,o] and all inequalities ′ ′ c 2 of the form tj − tj−1 ≥ ij, the formula Ψ6 contains (e + 1) · (#T + 1) inequalities. However, c the set hom(Ψ6) is only quadratic on the size of the set of homogeneous terms built from pairs of terms in T ∪ {0}, as we do not account for the natural numbers ij. Since the terms in T c are constructed by removing y from terms in lin(Ψ1), #hom(Ψ6) is quadratic on #hom(Ψ1). c The magnitude of the coefficients of the variables in linear inequalities of Ψ6 doubles with respect to ||T ||, whereas the magnitude of the constants is bounded by 2||T || + e. Lastly, c c every modulo constraint in Ψ6 is of the form w ≡m r(w), and thus mod(Ψ6) = {m}. Overall, the following bounds are derived:

c #hom(φ) #mod(Ψ6) = {m} with m = k · lcm(mod(φ)) and k ≤ ||hom(φ)|| , c 2 2 #hom(Ψ6) ≤ (#hom(φ) + 1) ≤ 4 · #hom(φ) , c ||hom(Ψ6)|| ≤ 2 · k · ||hom(φ)||, c 2 2 3 #lin(Ψ6) ≤ (e + 1) · (#lin(φ) + 1) ≤ 20 · c · m · #lin(φ) , c 2 ||lin(Ψ6)|| ≤ 2 · k · ||lin(φ)|| + e ≤ 6 · k · c · m · #lin(φ) · ||lin(φ)||, where we recall that we are assuming #hom(φ), #lin(φ), ||hom(φ)||, ||lin(φ)||, c ≥ 1. ◀

c 2 ▶ Lemma 21. #hom(Ψ6) = O(#hom(φ) ).

Proof. This is a simple consequence of Lemma 31. ◀

▶ Lemma 22. Let φ be a formula of Presburger arithmetic with threshold quantifiers. There is an equivalent quantifier-free formula Ψ such that

2 2O(|φ| ) #lin(Ψ), ||lin(Ψ)||, ||hom(Ψ)|| and ||mod(Ψ)|| are at most 22 ,

O(|φ|2) #hom(Ψ) ≤ 22 and #mod(Ψ) ≤ |φ|. 24 Presburger arithmetic with threshold counting quantifiers is easy

Proof. Recall that the standard first-order quantifier ∃y is equivalent to ∃≥1y. Therefore, without loss of generality, we can assume φ to only contain threshold counting quantifiers. For simplicity, we also assume #hom(φ), #lin(φ), ||hom(φ)|| and ||lin(φ)|| to be at least 1. This hides constant factors in the exponent of the bounds that we derive. Le us introduce some shortcuts.

Let d be the the quantifier-depth of φ, let B be 2 plus #mod(φ), plus the number of Boolean connectives in φ, and let c¯ be the maximal integer such that ∃≥c¯y occurs in φ.

We show the following bounds for Ψ, sharpening the ones in the statement of the lemma.

d def 2d−1 2 #hom(Ψ) ≤ Ad = (4 · B) · #hom(φ) , #mod(Ψ) ≤ B,

d d def (2A ) −1 (2Ad) ||hom(Ψ)|| ≤ Cd = 2 d ||hom(φ)|| , def (Bd−1) Bd ||mod(Ψ)|| ≤ Dd = (Cd) · lcm(mod(φ)) , d def 2 3d−1 3 #lin(Ψ) ≤ Ed = (20 · c¯ · B · Dd ) · #lin(φ) , def 3 d ||lin(Ψ)|| ≤ Fd = (6 · c¯ · Dd · Ed) ||lin(φ)||.

Notice that Ad, Cd, Dd, Ed and Fd are monotonous in d. Moreover, notice that B ≤ O(|φ|). The proof is by induction on the quantifier-depth of φ. The base case fore d = 0, i.e. φ ≥c1 ≥ quantifier-free, is trivial. For the induction step, let S = {∃ y1 ψ1,..., ∃ k yn ψn} be a minimal family of formulae such that φ is a Boolean combination of formulae from S. Notice that n ≤ B. Let j ∈ [1, n]. The quantifier-depth of ψj is at most d − 1. We apply the quantifier elimination procedure on ψj, obtaining the formula Ψj. By induction hypothesis,

d−1 2d−1−1 2 #hom(Ψj) ≤ Ad−1 = (4 · B) · #hom(φ) ,

#mod(Ψj) ≤ B, d−1 d−1 (2A ) −1 (2Ad−1) ||hom(Ψj)|| ≤ Cd−1 = 2 d−1 ||hom(φ)|| , (Bd−1−1) Bd−1 ||mod(Ψj)|| ≤ Dd−1 = (Cd−1) · lcm(mod(φ)) , d−1 2 (3d−1−1) 3 #lin(Ψj) ≤ Ed−1 = (20 · c¯ · B · Dd−1 ) · #lin(φ) , 3 d−1 ||lin(Ψj)|| ≤ Fd−1 = (6 · c¯ · Dd−1 · Ed−1) ||lin(φ)||.

≥c For j ∈ [1, k], we consider every formula ∃ yj Ψj and perform the quantifier elimination procedure for threshold counting quantifiers, obtaining a formula Ψe j. From Lemma 31 (see the proof of this lemma for the exact bounds) we have

#hom(Ψj ) #mod(Ψe j) = {m} with m = k · lcm(mod(Ψj)) and k ≤ ||hom(Ψj)|| , 2 #hom(Ψe j) ≤ 4 · #hom(Ψj) ,

2#hom(Ψj ) ||hom(Ψe j)|| ≤ 2 · k · ||hom(Ψj)|| ≤ 2 · ||hom(Ψj)|| , 2 3 #lin(Ψe j) ≤ 20 · c¯ · m · #lin(Ψj) , 2 3 ||lin(Ψe j)|| ≤ 6 · k · c¯ · m · #lin(Ψj) · ||lin(Ψj)|| ≤ 6 · c¯ · m · #lin(Ψj) · ||lin(Ψj)||, We derive:

d−1 d−1 d d d 2 −1 2 2 2 −2 2 −1 2 Ad #hom(Ψe j) ≤ 4 · ((4 · B) · #hom(φ)) ) ≤ B 4 #hom(φ) = B . d−1 d−1 (2Ad−1) −1 (2Ad−1) 2Ad−1 ||hom(Ψe j)|| ≤ 2(2 ||hom(φ)|| ) d d d d (2Ad−1) −1 (2Ad−1) (2Ad) −1 (2Ad) ≤ 2 ||hom(φ)|| ≤ 2 ||hom(φ)|| = Cd. D. Chistikov, C. Haase, A. Mansutti 25

#hom(Ψj ) B Notice that k ≤ ||hom(Ψj)|| ≤ Cd and that lcm(mod(Ψj)) ≤ ||mod(Ψj)|| .

B m ≤ lcm(mod(Ψj)) · Cd ≤ Cd · ||mod(Ψj)|| (Bd−1−1) Bd−1 B ≤ Cd · ((Cd−1) · lcm(mod(φ)) ) (Bd−B+1) Bd (Bd−1) Bd ≤ (Cd) · lcm(mod(φ)) ≤ (Cd) · lcm(mod(φ)) = Dd,

where we recall that we assume B ≥ 2. Hence, #mod(Ψe j) = 1 and ||mod(Ψ)e j|| ≤ Dd. 2 3 2 (3d−1−1) 3d−1 3 #lin(Ψe j) ≤ 20 · c¯ · m · #lin(Ψj) ≤ (20 · c¯ · m) · ((20 · c¯ · B · Dd ) · #lin(φ) ) d d d 3 −3 2 3 −2 3 Ed ≤ B (20 · c¯ · Dd ) · #lin(φ) ≤ B . 3 3 3 d−1 ||lin(Ψe j)|| ≤ 6·c¯·m ·#lin(Ψj)·||lin(Ψj)|| ≤ (6·c¯·Dd ·Ed)·(6·c¯·Dd ·Ed) ||lin(φ)|| = Fd.

≥cj For every j ∈ [1, n], we replace in φ each occurrence of the formula ∃ xj ψj not in the scope of a quantification with the formula Ψe j. We obtain the formula Ψ that is a Boolean combination of at most B formulae from {Ψe 1,..., Ψe n}. So, Ψ is quantifier-free. We have: Pn #hom(Ψ) = j=1 #hom(Ψe j) ≤ Ad, Pn #mod(Ψ) = j=1 #mod(Ψe j) ≤ B, ||hom(Ψ)|| ≤ max{||hom(Ψe j)|| | j ∈ [1, n]} ≤ Cd, ||mod(Ψ)|| ≤ max{||mod(Ψe j)|| | j ∈ [1, n]} ≤ Dd, Pn #lin(Ψ) = j=1 #lin(Ψe j) ≤ Ed, ||lin(Ψ)|| ≤ max{||lin(Ψe j)|| | j ∈ [1, n]} ≤ Fd. ◀

D Eliminating the counting quantifier ∃=xy

We adapt the quantifier-elimination procedure of Section 3 in order to directly deal withthe counting quantifier ∃=xy. To shorten the presentation, we only provide the (minor) changes =x to the procedure of Section 3. Consider Ψ0 = ∃ y φ, where φ is quantifier-free.

Steps I–III. The first two steps of the procedure follow in the same way as described in Section 3, the only difference being that the counting quantifier ∃≥xy is substituted =x i,r with ∃ y. The third step is also analogous, but instead of defining Ψ3 as   ∃x ... ∃x x ≤ x + ··· + x ∧ V ∃≥xj y(κ ∧ ψi,r) , 0 2ℓ 0 2ℓ j∈[0,2ℓ] j κj we define it as   ∃x ... ∃x x = x + ··· + x ∧ V ∃=xj y(κ ∧ ψi,r) . 0 2ℓ 0 2ℓ j∈[0,2ℓ] j κj

This change of the procedure is to be expected, in view of the difference between the two forms of quantification.

Step IV. The fourth step of the procedure is updated to deal with the different semantics that ∃≥xy ψ and ∃=xy ψ assume when infinitely many values of y satisfy ψ. In the first case, the formula ∃≥xy ψ is equivalent to ⊤, this follows from Claim 9. As already stated, the formula ∃=xy ψ instead evaluates to ⊥, which lead us to update Claim 9 as follows.

′ ′ i,r i,r ▷ Claim 32. Let κ ∈ {y < t1, tℓ < y}. If ∃y (κ ∧ ψκ ) is satisfiable, then Γi,r ∧ Ψ3 ↔ ⊥. The procedure then follows as described in Section 3, using Claim 32 instead of Claim 9 to i,r i,r set Ψ4 to ⊥ when necessary. Whenever Ψ4 is different form ⊥, instead of having the form 26 Presburger arithmetic with threshold counting quantifiers is easy

 V ′ ′  ∃x2 ... ∃xℓ x ≤ x2 + ··· + xℓ + c1 + ··· + cℓ ∧ j∈[2,ℓ] mxj ≤ pj(tj − tj−1) + rj

(as defined in Section 3), it is of the form

 V ′ ′  ∃x2 ... ∃xℓ x = x2 + ··· + xℓ + c1 + ··· + cℓ ∧ j∈[2,ℓ] mxj = pj(tj − tj−1) + rj .

Again, this update to the procedure only reflects the differences between ∃≥xy ψ and ∃=xy ψ.

i,r Step V. The last step of the procedure is updated following the changes done to Ψ4 . In i,r i,r particular, for every i ∈ [1, o] and r : Z → [m], if Ψ4 =⊥ then Ψ5 =⊥, otherwise

i,r def ′ ′ ′ ′ Ψ5 = mx = p2(t2 − t1) + r2 + ··· + pℓ(tℓ − tℓ−1) + rℓ + m(c1 + ··· + cℓ).

= def W W i,r Let Ψ5 = i∈[1,o] r : Z→[m](Γi,r ∧ Ψ5 ) be the formula obtained by performing the QE procedure described in this section, on input Ψ0. Recall that the formula Γi,r defined in the second step of the procedure is a conjunction of inequalities with variables from vars(φ) together with simple modulo constraints. One can show the following claim with minor adaptation to the proof of correctness of the QE procedure of Section 3.

= = ▷ Claim 33. Ψ0 ↔ Ψ5 . The formula Ψ5 is a Boolean combination of linear inequalities and simple modulo constraints.

E Missing proofs from Section 6

= (x,q) ▷ Claim 24. ∃z(z ≡q x ∧ Ψ5 ) ↔ Ψ6 . W V Proof. Let φres be the formula s :(Z∪{x})→[mq] w∈Z∪{x} w ≡mq s(w). We have

= = Ψ5 ↔ φres ∧ Ψ5 (12) ↔ γ (13) _ _ i,s ↔ (Γi,s ∧ Ψ5 ) (14) i∈[1,o] s :(Z∪{x})→[mq]

Indeed, the equivalence (12) holds since φres is a tautology. The equivalence (13) holds as = γ is obtained from φres ∧ Ψ5 by distributing the atomic formulae w ≡mq s(w) of φres over W W the disjunctions given by i∈[1,o] and r : Z→[m]. The nature of (14) is already explained during the procedure: since every function r : Z → [m] can be seen as a partial function from Z ∪ {x} to [mq], after the two steps above, for every s: (Z ∪ {x}) → [mq] and i ∈ [1, o], W V i,r all but one disjunct of the subformula r : Z→[m](( w∈Z∪{x} w ≡mq s(w)) ∧ Γi,r ∧ Ψ5 ) of γ evaluate ⊥. Thanks to (12)–(14), we conclude:

=  _ _ i,s  ∃z(z ≡q x ∧ Ψ5 ) ↔ ∃z z ≡q x ∧ (Γi,s ∧ Ψ5 ) (15) i∈[1,o] s :(Z∪{x})→[mq] (x,q) ↔ Ψ6 (16)

The equivalence (15) follows from (12)–(14), whereas for the equivalence (16) it is sufficient to distribute the existential quantifier ∃z and the formula z ≡q x over all the disjunctions W W given by i∈[1,o] and s :(Z∪{x})→[mq]. ◀

(x,q) (x,q) (x,q) ▷ Claim 25. Ψ6 ↔ Ψ7 . The formula Ψ7 is quantifier-free. D. Chistikov, C. Haase, A. Mansutti 27

i,s Proof. We show that, given i ∈ [1, o] and s: (Z∪{x}) → [mq], ∃z(z ≡q x ∧ Γi,s ∧ Ψ5 ) ↔ γi,s. i,s i,s def If Ψ5 =⊥ then γi,s is defined as ⊥ and the equivalence holds. Otherwise we have Ψ5 = ′ ′ ′ ′ mz = p2(t2 − t1) + r2 + ··· + pℓ(tℓ − tℓ−1) + rℓ + m(c1 + ··· + cℓ). In this case, γi,s = Γi,s if def ′ ′ ′ ′ s(mx) is congruent to S = s(p2(t2 − t1) + r2 + ··· + pℓ(tℓ − tℓ−1) + rℓ + m(c1 + ··· + cℓ)) modulo mq, and otherwise γi,s =⊥. We do a case split following these to cases:

Suppose that s(mx) and S are not congruent modulo mq. In order to prove that the i,s equivalence ∃z(z ≡q x ∧ Γi,s ∧ Ψ5 ) ↔ γi,s holds, it is sufficient to show that the formula i,s ∃z(z ≡q x ∧ Γi,s ∧ Ψ5 ) is unsatisfiable (since γi,s is defined as ⊥). From

′ ′ ′ ′ mz = p2(t2 − t1) + r2 + ··· + pℓ(tℓ − tℓ−1) + rℓ + m(c1 + ··· + cℓ).

and z ≡q x ↔ mz ≡mq mx we derive

′ ′ ′ ′ mx ≡mq p2(t2 − t1) + r2 + ··· + pℓ(tℓ − tℓ−1) + rℓ + m(c1 + ··· + cℓ).

i,s Ad absurdum, suppose that ν |= ∃z(z ≡q x∧Γi,s∧Ψ5 ), for some assignment ν. As ν |= Γi,s, for every variable w ∈ Z ∪ {x} we have ν(w) ≡mq s(w). However, this is contradictory, as

′ ′ ′ ′ ν |= mx ≡mq p2(t2 − t1) + r2 + ··· + pℓ(tℓ − tℓ−1) + rℓ + m(c1 + ··· + cℓ)

implies s(mx) ≡mq S. Assume that s(mx) and S are congruent modulo mq. By definition, γi,s = Γi,s. The i,s left-to-right direction of the equivalence ∃z(z ≡q x ∧ Γi,s ∧ Ψ5 ) ↔ γi,s is thus trivial: if an assignment ν satisfies the left hand side of this equivalence, then ν |= Γi,s. For the right-to-left direction, assume ν |= Γi,s. Hence, for every variable w ∈ Z ∪ {x}, ν(w) is ′ ′ equivalent to s(w) modulo mq. This implies that ν(mx) and V = ν(p2(t2 − t1) + r2 + ′ ′ ··· + pℓ(tℓ − tℓ−1) + rℓ + m(c1 + ··· + cℓ)) are congruent modulo mq. Since moreover ν(mx) = mν(x) is a multiple of m, we conclude that there is v ∈ Z such that m · v = V . Consider the assignment ν[v/z], with z fresh. Since z ̸∈ Z ∪ {x}, we have ν[v/z] |= Γi,s. Moreover, from ν(mx) ≡mq mv, we conclude that ν[v/z] |= mx ≡mq mz. Equivalently, ν[v/z] |= x ≡q z. Lastly, by definition of v,

′ ′ ′ ′ ν[v/z] |= mz = p2(t2 − t1) + r2 + ··· + pℓ(tℓ − tℓ−1) + rℓ + m(c1 + ··· + cℓ).

i,s i,s Therefore, ν[v/z] |= z ≡q x ∧ Γi,s ∧ Ψ5 , and thus ν |= ∃z (z ≡q x ∧ Γi,s ∧ Ψ5 ). ◀

(x,q) ▶ Lemma 34. The following bounds are established for Ψ7 :

(x,q) #hom(φ) mod(Ψ7 ) = {m · q} with m = k · lcm(mod(φ)) and k ≤ ||hom(φ)|| , (x,q) 2 (x,q) #hom(Ψ7 ) ≤ O(#hom(φ) ) and ||hom(Ψ7 )|| ≤ O(k · ||hom(φ)||), (x,q) 2 (x,q) #lin(Ψ7 ) ≤ O(#lin(φ) ) and ||lin(Ψ7 )|| ≤ O(k · ||lin(φ)||). Proof. The proof follows similarly to the one of Lemma 31. Without loss of generality, we assume #hom(φ), #lin(φ), ||hom(φ)|| and ||lin(φ)|| to be at least 1. This hides constant factors in the exponent. The quantifier-elimination procedure for ∃(x,q)y φ starts by performing the normalisation of the coefficients of y, as described in Step I of Section 3. Let Ψ1 be the resulting formula. As already discussed in the proof of Lemma 30, we have

#lin(Ψ1) = #lin(φ) and ||lin(Ψ1)|| ≤ k · ||lin(φ)||,

#hom(Ψ1) = #hom(φ) and ||hom(Ψ1)|| ≤ k · ||hom(φ)||. 28 Presburger arithmetic with threshold counting quantifiers is easy

where k ≤ ||hom(φ)||#hom(φ) is the lcm of all coefficients of y appearing in linear inequalities. (x,q) W W We recall that the formula Ψ7 is defined as i∈[1,o] s :(Z∪{x})→[mq] γi,s, where Z = fv(φ), #hom(φ) V m = lcm(mod(φ)) · ||hom(φ)|| , γi,s is ⊥ or Oi ∧ ( w∈Z∪{x} w ≡mq s(w)). Here, the sub-formula Oi is an ordering on the set of terms T ∪ {0}, as defined in Step II of Section 3. In particular, #T ≤ #lin(Ψ1), ||T || ≤ ||lin(Ψ1)|| and all coefficients of variables in terms of T are bounded by ||hom(Ψ1)||. So, even when accounting for all orderings (Oi)i∈[1,o], the (x,q) 2 formula Ψ7 only contains at most (#T ∪{0}) inequalities, whose magnitude of coefficients and constants doubles with respect to the one of the terms in Ψ1. Lastly, every modulo (x,q) constraint in Ψ7 is of the form w ≡mq s(w). Therefore, we have the following bounds:

(x,q) 2 2 #lin(Ψ7 ) ≤ (#lin(φ) + 1) ≤ 4 · #lin(φ) , (x,q) ||lin(Ψ7 )|| ≤ 2 · k · ||lin(φ)||, (x,q) 2 2 #hom(Ψ7 ) ≤ (#hom(φ) + 1) ≤ 4 · #hom(φ) , (x,q) ||hom(Ψ7 )|| ≤ 2 · k · ||hom(φ)||, (x,q) mod(Ψ7 ) = {mq}, where m = k · lcm(mod(φ)),

where we recall that we are assuming #hom(φ), #lin(φ), ||hom(φ)|| and ||lin(φ)|| to be at least 1. ◀

(x,q) 2 ▶ Lemma 26. #hom(Ψ7 ) = O(#hom(φ) ).

Proof. This is a simple consequence of Lemma 34. ◀

▶ Lemma 27. Let φ be a formula of Presburger arithmetic with threshold quantifiers. There is an equivalent quantifier-free formula Ψ such that

2 2O(|φ| ) #lin(Ψ), ||lin(Ψ)||, ||hom(Ψ)|| and ||mod(Ψ)|| are at most 22 ,

O(|φ|2) #hom(Ψ) ≤ 22 and #mod(Ψ) ≤ |φ|.

Proof. Without loss of generality, we assume #hom(φ), #lin(φ), ||hom(φ)|| and ||lin(φ)|| to be at least 1. This hides constant factors in the exponent. The proof follows very closely the one of Lemma 22, with no surprises.

Let d be the the quantifier-depth of φ, let B be 2 plus #mod(φ), plus the number of Boolean connectives in φ, and let q¯ be the maximal integer such that ∃(x,q¯)y occurs in φ.

We show the following bounds for Ψ, sharpening the ones in the statement of the lemma.

d def 2d−1 2 #hom(Ψ) ≤ Ad = (4 · B) · #hom(φ) , #mod(Ψ) ≤ B,

d d def (2A ) −1 (2Ad) ||hom(Ψ)|| ≤ Cd = 2 d ||hom(φ)|| , def (Bd−1) Bd ||mod(Ψ)|| ≤ Dd = (¯q · Cd) · lcm(mod(φ)) , d def 2 3d−1 3 #lin(Ψ) ≤ Ed = (20 · B · Dd ) · #lin(φ) , def 3 d ||lin(Ψ)|| ≤ Fd = (6 · Dd · Ed) · ||lin(φ)||. D. Chistikov, C. Haase, A. Mansutti 29

Recall that the logic features both modulo counting quantifiers and standard first-order quantifiers. To this end, notice that Ad, B, Cd, Ed and Fd overapproximate the homonymous bounds given in Lemma 22 for the threshold quantifiers, for the case where all thresholds c in ∃≥cy equal 1 (since ∃≥1ψ ↔ ∃ψ). Therefore, we deal with standard first-order quantifiers exactly as in Lemma 22. Below, let us focus uniquely on modulo counting quantifiers.

Notice that Ad, Cd, Dd, Ed and Fd are monotonous in d. Moreover, notice that B ≤ O(|φ|). The proof is by induction on the quantifier-depth of φ (d takes into account both modulo counting quantifiers and first-order quantifiers). The base case fore d = 0, i.e. φ quantifier-free, (x1,qn) (xn,qn) is trivial. For the induction step, let S = {∃ y1 ψ1,..., ∃ yn ψn} be a minimal family of formulae such that φ is a Boolean combination of formulae from S. Notice that n ≤ B. Let j ∈ [1, n]. The quantifier-depth of ψj is at most d − 1. We apply the quantifier elimination procedure on ψj, obtaining the formula Ψj. By induction hypothesis,

d−1 2d−1−1 2 #hom(Ψj) ≤ Ad−1 = (4 · B) · #hom(φ) ,

#mod(Ψj) ≤ B,

d−1 d−1 (2A ) −1 (2Ad−1) ||hom(Ψj)|| ≤ Cd−1 = 2 d−1 ||hom(φ)|| , (Bd−1−1) Bd−1 ||mod(Ψj)|| ≤ Dd−1 = (¯q · Cd−1) · lcm(mod(φ)) , d−1 2 (3d−1−1) 3 #lin(Ψj) ≤ Ed−1 = (20 · B · Dd−1 ) · #lin(φ) , 3 d−1 ||lin(Ψj)|| ≤ Fd−1 = (6 · Dd−1 · Ed−1) · ||lin(φ)||.

(xj ,qj ) For j ∈ [1, k], we consider every formula ∃ yj Ψj and perform the quantifier elimination procedure for threshold counting quantifiers, obtaining a formula Ψe j. Lemma 34 (see the proof of this lemma for the exact bounds) we have

#hom(Ψj ) #mod(Ψe j) = {qj · m} with m = k · lcm(mod(Ψj)) and k ≤ ||hom(Ψj)|| . 2 #lin(Ψe j) ≤ 4 · #lin(Ψj) ,

||lin(Ψe j)|| ≤ 2 · k · ||lin(Ψj)||, 2 #hom(Ψe j) ≤ 4 · #hom(Ψj)

2#hom(Ψj ) ||hom(Ψe j)|| ≤ 2 · k · ||hom(Ψj)|| ≤ 2 · ||hom(Ψj)|| ,

We derive:

d−1 d−1 d d d 2 −1 2 2 2 −2 2 −1 2 Ad #hom(Ψe j) ≤ 4 · ((4 · B) · #hom(φ)) ) ≤ B 4 #hom(φ) = B . d−1 d−1 (2Ad−1) −1 (2Ad−1) 2Ad−1 ||hom(Ψe j)|| ≤ 2(2 ||hom(φ)|| ) d d d d (2Ad−1) −1 (2Ad−1) (2Ad) −1 (2Ad) ≤ 2 ||hom(φ)|| ≤ 2 ||hom(φ)|| = Cd.

#hom(Ψj ) B Notice that k ≤ ||hom(Ψj)|| ≤ Cd and that lcm(mod(Ψj)) ≤ ||mod(Ψj)|| .

B m ≤ qj · Cd · lcm(mod(Ψj)) ≤ q¯ · Cd · ||mod(Ψj)|| (Bd−1−1) Bd−1 B ≤ q¯ · Cd · ((¯q · Cd−1) · lcm(mod(φ)) ) (Bd−B+1) Bd (Bd−1) Bd ≤ (¯q · Cd) · lcm(mod(φ)) ≤ (¯q · Cd) · lcm(mod(φ)) = Dd,

where we recall that we assume B ≥ 2. Hence, #mod(Ψe j) = 1 and ||mod(Ψ)e j|| ≤ Dd. d−1 d−1 d d d 2 3 −1 3 2 3 −2 2 3 −1 3 Ed #lin(Ψe j) ≤ 4·((20·B·Dd−1 ) #lin(φ)) ) ≤ B (20·Dd ) #lin(φ) ≤ B . 3 d−1 ||lin(Ψe j)|| ≤ 2 · Cd · ||lin(Ψj)|| ≤ (2 · Cd) · (6 · Dd−1 · Ed−1) · ||lin(φ)|| ≤ Fd, where we notice that Cd ≤ Dd. 30 Presburger arithmetic with threshold counting quantifiers is easy

(xj ,qj ) For every j ∈ [1, n], we replace in φ each occurrence of the formula ∃ xj ψj not in the scope of a quantification with the formula Ψe j. We obtain the formula Ψ that is a Boolean combination of at most B formulae from {Ψe 1,..., Ψe n}. So, Ψ is quantifier-free. We have: Pn #hom(Ψ) = j=1 #hom(Ψe j) ≤ Ad, Pn #mod(Ψ) = j=1 #mod(Ψe j) ≤ B, ||hom(Ψ)|| ≤ max{||hom(Ψe j)|| | j ∈ [1, n]} ≤ Cd, ||mod(Ψ)|| ≤ max{||mod(Ψe j)|| | j ∈ [1, n]} ≤ Dd, Pn #lin(Ψ) = j=1 #lin(Ψe j) ≤ Ed, ||lin(Ψ)|| ≤ max{||lin(Ψe j)|| | j ∈ [1, n]} ≤ Fd. ◀