ASP(AC): Answer Set Programming with Algebraic Constraints

Weighted Logic is a powerful tool for the specification of calculations over semirings that depend on qualitative information. Using a novel combination of Weighted Logic and Here-and-There (HT) Logic, in which this dependence is based on intuitionistic grounds, we introduce Answer Set Programming with Algebraic Constraints (ASP(AC)), where rules may contain constraints that compare semiring values to weighted formula evaluations. Such constraints provide streamlined access to a manifold of constructs available in ASP, like aggregates, choice constraints, and arithmetic operators. They extend some of them and provide a generic framework for defining programs with algebraic computation, which can be fruitfully used e.g. for provenance semantics of datalog programs. While undecidable in general, expressive fragments of ASP(AC) can be exploited for effective problem-solving in a rich framework. This work is under consideration for acceptance in Theory and Practice of Logic Programming.


Introduction
Answer Set Programming (ASP) is a well-known non-monotonic declarative programming paradigm. Due to the need for more expressiveness and succinct descriptions, it has been extended with many different constructs, ranging from nested expressions (Lifschitz et al. 1999) to weight constraints with conditionals (Niemelä et al. 1999) and aggregates (Ferraris 2011). A more recent trend combines ASP with Constraint Processing (CP) employing both solvers for ASP and Satisfaction Modulo Theories (SMT), cf. (Lierler 2014; Janhunen 2018). Many of these approaches keep nonmonotonicity on the ASP side, but also its use on the CP side was explored (Aziz et al. 2013). Cabalar et al. (2020a;2020b) recently introduced a general non-monotonic integration of CP into ASP providing aggregates and conditionals for the specification of numeric values that depend on the satisfaction of formulas. The conditionals can be flexibly evaluated under the vicious circle (vc) or the definedness principles (d f ). Their approach treats constraints as black boxes, leaving their syntax open, and incorporates many previously introduced constructs.
An important feature of constraint extensions is the possibility to express (in)equations involving computations on an algebraic structure, whose solutions are accessible by the ASP rules. Basic such structures are semirings R = (R, ⊕, ⊗, e ⊕ , e ⊗ ), where ⊕ and ⊗ are addition and multiplication with neutral elements e ⊕ and e ⊗ , respectively. They have been considered for constraint semantics e.g. in (Bistarelli et al. 1997) and were used to provide rule languages with parame-arXiv:2008.04008v1 [cs.AI] 10 Aug 2020 terised calculation in a uniform syntax but flexible semantics, cf. (Kimmig et al. 2011;Eiter and Kiesel 2020).
Notably, Weighted Logic (Droste and Gastin 2007) links semirings with predicate logic, where weighted formulas are interpreted as algebraic expressions over a semiring R. Similar to conditionals in (Cabalar et al. 2020b), the value of a weighted formula α depends on the truth of the atoms in α. E.g. the weighted formula 1 + + + deadline * (2 + + + pagelimit * 3) over the natural numbers N may represent how many cups of coffee a researcher drinks: if there is a deadline but no pagelimit, the result is 1 + 1 · (2 + 0 · 3) = 3.
In recent work (Eiter and Kiesel 2020), we exploited Weighted Logic in quantitative stream reasoning to assign weights to answer streams and aggregate over them. Here, we introduce a non-monotonic version of it in terms of First-Order Weighted Here-and-There Logic (FO-WHT). The resulting logic complements Cabalar et al.'s work on abstract constraints in several respects: • it offers an elegant way of specifying calculations, aggregates and non-monotonic conditionals natively and without the need for auxiliary definitions; • the semantics provides a natural alternative to the vicious circle and definedness principles which arguably combines their strengths; • the parameterisation with semirings allows for terms in a uniform syntax that are not bound to the reals but can be over any semiring (which may be fixed at runtime).
As customary for ASP we restrict ourselves to a fragment of FO-WHT Logic and introduce A Cprograms that allow for algebraic constraints, i.e. constraints on the values of weighted formulas, in both heads and bodies of rules. A C -programs incorporate and extend many previous ASP constructs and thus provide a rich framework for declarative problem solving in a succinct form.
The main contributions of this paper are briefly summarised as follows: • We introduce First-Order Weighed HT Logic and A C -programs that include constraints over weighted formulas (Section 3). By using a variant of HT Logic with non-disjoint sorts, such that variables can range over subsets shared by a domain and semirings, we enable the usage of constraints over different semirings within the same program.
• We consider different constructs in extensions of ASP like aggregates, choice constraints and conditionals, and we demonstrate how to model them in our framework. Further, we present the novel minimised constraints that allow for subset minimal guessing (Section 4).
• To demonstrate the power of ASP(A C ) , we illustrate how provenance semantics for positive datalog programs can be elegantly encoded (Section 5).
• We consider different language aspects leading, firstly, to a broad class of safe A C -programs, which we show to be domain independent; and, secondly, to a characterisation of strong equivalence for A C -programs by equivalence in FO-WHT Logic (Section 6).
• We obtain that in the propositional (ground) case, the complexity of disjunctive logic programs is retained, i.e. model checking (MC) and strong equivalence are co-NP-complete while answer set existence (SAT) is Σ p 2 -complete, if the used semirings satisfy a practically mild encoding condition. For safe non-ground programs, MC is feasible in EXPTIME at most; SAT and SE are undecidable in general, but expressive decidable fragments are available (Section 7).

Preliminaries
We start by introducing classical programs and their semantics. We use a variant of first-order HT semantics (Pearce and Valverde 2008) as this facilitates the generalisation of the semantics later on and is useful for work on strong equivalence. The variant is that we assign variables nondisjoint sorts, which lets us quantify over subsets of the domain. This slightly differs from other approaches in logic programming that use sorts, like (Balai et al. 2013), where the arguments of predicates are sorted.
We consider sorted first-order formulas over a signature σ = D, P, X , S , r , where D is a set of domain elements, P a set of predicates, X a set of sorted variables, S a set of sorts and r : S → 2 D a range function assigning each sort a subset of the domain. When x ∈ X , we write s(x) for the sort of x. Given a signature σ , we define the syntax of σ -formulas by where p ∈ P, x = x 1 , . . . , x n , with x i ∈ D or x i ∈ X and y ∈ X ; p( x) is called a σ -atom. We define ¬φ = φ → ⊥. A σ -sentence is a σ -formula without free variables.
To introduce weighted formulas, we first recall semirings.
Definition 3 (Semiring). A semiring R = (R, ⊕, ⊗, e ⊕ , e ⊗ ) is a set R equipped with two binary operations ⊕ and ⊗, which are called addition and multiplication, such that • (R, ⊕) is a commutative monoid with identity element e ⊕ , • (R, ⊗) is a monoid with identity element e ⊗ , • multiplication left and right distributes over addition, • and multiplication by e ⊕ annihilates R, i.e. ∀r ∈ R : r⊗e ⊕ = e ⊕ = e ⊕ ⊗r.
, the max tropical semiring. It is typically used in the context of provenance and optimisation.
• 2 A = (2 A , ∪, ∩, / 0, A), the powerset semiring over the set A. It is typically used in the context of measure theory but also for set arithmetic and succinct specifications of multiple requirements.
For space reasons, we must omit introducing Weighted Logic (Droste and Gastin 2007) explicitly and confine to compare our logic to the one by Droste and Gastin below.
Definition 4 (Syntax). For a signature σ = D, P, X , S , r , the weighted σ -formulas over the semiring R = (R, ⊕, ⊗, e ⊕ , e ⊗ ) are of the form where k ∈ R, x, y ∈ X s.t. r(s(x)) ⊆ R (i.e., x takes only values from R) and φ is a σ -formula. The use of − − − and − − −1 1 1 require that ⊕ and ⊗ are invertible, the use of Πy requires that ⊗ is commutative.
Example 2. Let σ = Q, {p}, {X}, {S}, {S → Q} and s(X) = S; thus, X ranges over the rational numbers. Then ΣX p(X) * X is a weighted σ -sentence over the semirings R max , Q but not over N.
Definition 5 (Semantics). Let σ = D, P, X , S , r be a signature. The semantics of a weighted σ -sentence over semiring R w.r.t. I w = (I H , I T , w) is inductively defined in Figure 1.
Apart from being an HT Logic, the main difference between ours and the Weighted Logic introduced in (Droste and Gastin 2007) is that we allow for the additional connectives − − −, − − −1 1 1 , and → and that ours is first-order over infinite domains instead of second-order over finite words.
Defining a reasonable semantics for the case of infinite support seems challenging in general. For example, Q is not closed under taking the limit of converging sequences and even in R not every infinite sum of numbers converges. For ω-continuous semirings such as N ∞ , where both conditions above are satisfied, a definition would be possible (but omitted here).
Intuitively, weighted formulas specify calculations over semirings depending on the truth of formulas. The quantifier Σ allows us to aggregate the values of weighted formulas for all variable assignments using ⊕ as the aggregate function.
The semantics of weighted formulas is multi-valued in general. In order to return to the Boolean semantics for programs, we define algebraic constraints, which are (in)equations between a semiring value and a weighted formula.
A sentence k ∼ R α is satisfied w.r.t. I w , if for all w ≥ w. The syntax of σ -formulas in Section 2 is extended to include algebraic constraints in (1) as a further case. The definitions of satisfaction (Defn. 1) and equilibrium model (Defn. 2) are amended in the obvious way. However, as the semantics of weighted formulas is undefined for infinite supports, there are two variants of interpreting the condition I I : (I , I , H) |= σ φ in the definition of equilibrium models. If we adopt that |= σ holds when the semantics is undefined, we end up with weak equilibrium models, otherwise with strong equilibrium models.
To verify that the semantics of algebraic constraints is in line with the intuition of HT logic, we show that the persistence property is maintained for sentences that include algebraic constraints.
Having established that the semantics behaves as desired, we formally define programs that can contain algebraic constraints in terms of a fragment of the logic over semiring signatures.
Definition 9 (Semiring Signature). A signature σ = D, P, X , S , r is a semiring signature for semirings R 1 , . . . , R n , where ..,n} , (ii) D contains R i , for all i = 1, . . . , n, and (iii) r : S → 2 D maps {i 1 , . . . , i m } to m j=1 R i j . Intuitively, if a variable x has sort {i 1 , . . . , i m }, then we only want to quantify over those domain-values that are in every semiring R i 1 to R i m . Imagine for example that a variable x is used as a placeholder for a semiring value in two algebraic constraints, one over N and one over Q. Then it only makes sense to quantify over domain-values that are contained in N.
Definition 10 (A C -Rules, A C -Programs). Let σ = D, P, X , S , r be a semiring signature for R 1 , . . . , R n . Then an A C -program is a set of A C -rules of the form r = H(r) ← B(r) = φ ← ψ 1 , . . . , ψ n , ¬θ 1 , . . . , ¬θ m , where each φ , ψ i and θ j is either a σ -atom or an algebraic constraint over R i for some i = 1, . . . , n, in which no quantifiers or nested constraints occur. Furthermore, we require for each variable x occurring in r that i ∈ s(x) iff x occurs in place of a value from the semiring R i .
Example 4 (Rules). The following are examples of A C -rules: Note that in A C -rules quantifiers occur neither in weighted nor in unweighted formulas. Variables are quantified implicitly, depending on their scope defined as follows.
Definition 11 (Local & Global). A variable x that occurs in an A C -rule r is local, if it occurs in r only in weighted formulas, and global otherwise. A rule or program is locally (resp. globally) ground, if it has no local (resp. global) variables.
Example 5 (cont.). In the previous example Y and I are respectively global and local in both rules, whereas W is local in rule (3) and global in rule (4).
We then quantify global variables universally and local variables "existentially" (i.e. using Σ).

Construct
ASP Table 1. Constructs expressible in ASP(A C ) and how they are expressed in other formalisms.
Example 6 (cont.). Consequently, the A C -rules from above correspond to the formulas We see that rule (3) calculates the sum over all indices {i | ind(i)} weighted locally with w when loc weight(i, w) holds. Rule (4) calculates the sum over all indices {i | ind(i)} where all of them are weighted with the same weight w when glob weight(w) holds.
Note that we strongly restricted the weighted formulas that are allowed in A C -programs. The quantifier Π and nested algebraic constraints are unavailable and Σ quantifiers can only occur as a prefix. Removing these restrictions would lead to a much higher complexity. Already constraint evaluation would be PSPACE-hard for any non-trivial semiring. In addition, our choice allows us to keep the syntax of A C -programs closer to the one of other programs with constraints.
In the sequel, we drop A C from A C -rules and A C -programs if no ambiguity arises.

Constructs in ASP(A C ) and in other formalisms
We consider several constructs that we can express in ASP(A C ) and relate them to constructs known from previous extensions of ASP; a summary is given in Table 1.

Nested Expressions
The logic programs with arbitrary propositional formulas defined in (Lifschitz et al. 1999) are modelled simply using constraints over the Boolean semiring B. As a special case, this shows the expressibility of disjunctive logic programs using 1 = B a 1 + + + . . . + + + a n ← B(r).
Conditionals Cabalar et al. (2020b) defined two semantics for conditionals s = (s |s : φ ), where s , s are terms and φ is a (quantifier-free) formula. They are named vicious circle (vc) and definedness (df), respectively. Given an interpretation (I H , I T ), Syntax and semantics of weighted formulas could be readily extended to include these constructs. We present instead an alternative evaluation of conditionals as formulas s * φ + + + s * ¬φ . Then That is, when neither φ nor ¬φ is satisfied, we end up with the neutral element e ⊕ . Consider the following rules r 1 and r 2 : According to vc resp. df, they are equivalent: under vc, both have no stable model while under df both have the stable model {p}. We may expect that r 1 has the stable model {p} as the formula s ∨ is equivalent to regardless of the value of s. Therefore, the value of p should not influence the truth of the body of r 1 . On the other hand, the value of the conditional in r 2 influences the truth of the body of r 2 and it depends on p. Therefore, if {p} were a stable model of r 2 , we would arguably derive p using the truth value of p. Accordingly, we may expect that r 2 does not have a stable model. These expectations align with the semantics for r 1 and r 2 from (5) above. This evaluation combines the ideas behind the vc and the df principle: the value of a conditional is always defined, but the vicious circle of deriving p by the truth value of p is avoided.
Apart from that, we may express vc and df in our formalism: adding the constraint 1 = B φ + + + ¬φ to a rule using a conditional on φ as in (5) corresponds to vc semantics. The constraint enforces that when the rule fires, φ either must be false already at world T , or when φ is true at T then it must also be forced to hold at world H by the rest of the program. If this is not the case, then I T |= 1 = B φ + + + ¬φ but I H |= 0 = B φ + + + ¬φ . Thus, in this case, any rule containing this constraint in the body is trivially satisfied. Furthermore, provided the addition in R is invertible, we can use φ * s + + + (e ⊗ + + +−φ ) * s to capture df (cf. Appendix for more details and discussion). Summarising, the possibility to express vc and df as well as to define other semantics of conditionals exemplifies the power of FO-WHT Logic and A C -programs.
Constraints in the head for guessing In many ASP extensions, constraints in rule heads and rule bodies behave differently; in heads, they are used as choice constraints. Consider for example the rule 10{p(X) : q(X)}. in lparse syntax. Any interpretation s.t. p(x) holds for ten or more values x can be stable. In order to express this constraint in our semantics, we need to take care of two aspects. The first one is that the above constraint only supports p(x) for x s.t. q(x) was already derived in another way. If we simply use the rule 10 ≤ N p(X) * q(X) ← it can also derive q(x) instead of using it as a precondition. We can however use instead the formula ¬¬q(X) * (q(X) → p(X)) to achieve the desired effect. More generally, we use the pattern Abstractly, it ensures that p(x) can only be asserted for x s.t. we already know that q(x, w) holds, so we cannot "invent" new constants. This can be seen as follows. Assume I contains The variable W assigns the addition of p(x) a weight w. Secondly, given the rule 10 ≤ N ¬¬q(X) * (q(X) → p(X)) ← only interpretations that assert p(x) for exactly ten elements x can be stable. While such minimised constraints are useful in a different context, we also need to be able to specify choice constraints in our language. This can be achieved naturally without extending the semantics of our language, by introducing a syntactic shorthand k ∼ c R α for algebraic choice constraints in rule-heads. We define that (7) where α ¬¬ is obtained from α by adding ¬¬ in front of each atom p( x). These algebraic choice constraints behave as expected of choice constraints. (The next proposition considers only globally ground rules in order to decrease the amount of syntactic noise.) Proposition 13 (Choice Semantics). For any σ -HT-interpretation (I H , I T ) and any rule r . Proof (sketch). Any satisfying interpretation has to satisfy both k ∼ R α ← B(r) and X = R α ← X = R α ¬¬ , B(r). The first of the two says that the minimised constraint has to be satisfied when B(r) is satisfied and corresponds to (i). The second says that when B(r) is satisfied and α ¬¬ has value X then also α needs to have value X. Since the value of α ¬¬ is the value of α under I T the second rule corresponds to (ii).
Choice constraints are already well-known from previous ASP extensions, so we do not explain them in more detail. The usefulness of the novel minimised choice constraints is demonstrated in the following example.
Example 7 (Integer Subset Sum). Consider the following variation of the Subset Sum Problem: Given a set S ⊆ Z and two bounds l, u ∈ Z, determine a ⊆-minimal solution S ⊆ S such that l ≤ ∑ x∈S x ≤ u. When s(x) holds for x ∈ S, we can use the A C -rules For every equilibrium model I the set S = {x | in(x) ∈ I } is a ⊆-minimal solution and for every ⊆-minimal solution there exists an equilibrium model. When using choice constraints, i.e. replacing ∼ Z by ∼ c Z , the program still obtains solutions, but not only ⊆-minimal ones.
Aggregates As can be seen in Example 3, we can model aggregates whose aggregation function is the addition of some semiring. This restriction is mild in practice: The aggregates min, max, sum, count are expressible using a single algebraic constraint. times and avg are expressible using multiple algebraic constraints (e.g. avg is sum divided by count).
Value Guessing and Arithmetic Operators Value guessing and arithmetic operators are especially used in combinations of ASP and CP (Lierler 2014). We can guess a value from a semiring, perform arithmetic operations over semirings and evaluate (in)equations on the results. Again, we are mildly restricted as only semiring operations are available.

Provenance
Green at al. (2007) introduced a semiring-based semantics that is capable of expressing bag semantics, why-provenance and more. For positive logic programs, their semantics over a semiring (R, ⊕, ⊗, e ⊕ , e ⊗ ) is as follows: the label of a query result q( x) is the sum (using ⊕) of the labels of derivation trees for q( x), where the label of a derivation tree is the product (using ⊗) of the labels of the leaf nodes (i.e. extensional atoms). As the number of derivation trees may be countably infinite, Green et al. used ω-continuous semirings such as N ∞ that allow to have countable sums.
We can model the semiring semantics in our formalism, by allowing operations over countable supports supp (α(x), I w ) for ω-continuous semirings. Over N ∞ they always have the value ∞.
Example 9 (cont.). The following A C -program calculates the provenance semantics over N ∞ for the above positive logic program, depending on the edb: Here p(A,V, X) represents that X is the sum of all labels of derivation trees for A having exactly V many leaf nodes. We obtain this value first for all derivation trees that apply rule r i last, in p(A, i,V, X), and sum them up in rule (12). Similarly the final provenance value is obtained as the sum over the provenance values for each number of leaf nodes V * in rule (13); d(A,V ) says that there is a derivation tree of A using V leaf nodes and ensures safety (see next section).
We can apply this strategy in general: Even for a non-ground positive logic program we can give an A C -program that computes the provenance semantics. This can be achieved in a similar fashion as in the example above. Details can be found in the appendix. Exploring extensions of Green et al.'s semantics for the provenance of negated formulas remains for future work.

Language Aspects
Domain Independence and Safety We need to restrict ourselves to programs that are well behaved, i.e. independent of the domain they are evaluated over.
Example 10. Consider the weighted formula α = Σx ¬q(x), which counts the elements d in the domain s.t. q(d) does not hold. It is easy to see that if we consider the semantics using the same interpretation but over different domains (or rather signatures) it can vary.
We are interested in formulas that do not exhibit this kind of behaviour, formalised as: φ is a σ i -formula (resp. α is a weighted σ i -formula) for i = 1, 2 and every I w = (I H , I T , w) that is a pointed σ i -HT-interpretation for i = 1, 2 it holds that ). We restrict ourselves to a fragment of weighted formulas. Intuitively, we need to ensure that every variable X in α( X) is bound by a positive occurrence of a predicate p(X).
Definition 15 (Syntactic Domain Independence). A weighted formula α( X) over a semiring R is syntactically domain independent w.r.t. X, if it is constructible following Example 11 (cont.). While ¬q(Y ) from Example 10 is not syntactically domain independent w.r.t. Y , the formula p(Y ) * ¬q(Y ), which counts the number d s.t. p(d) holds but not q(d), is. It can be constructed using α( X) * β ( X ).
Our syntactic criterion guarantees domain independence.
We show the invariance for one interesting case, namely α = α 1 (x) * α 2 . Note that: As the semantics of variable-free formulas is domain independent the claim follows.
Safety of programs is defined as follows.
Definition 17 (Safety). A program Π is safe, if each rule r ∈ Π of form (2) is safe, i.e. fulfills that (i) every weighted formula in r is syntactically domain independent w.r.t. its local variables; (ii) for every global variable X there exists some β i s.t. (1) β i is an atom and X occurs in it, or (2) β i is X = R β i and X does not occur in any weighted formula in the body of r.
The restriction in (ii) that X does not reoccur is necessary to prohibit p(X) ← X = R Y,Y = R X. It could however be replaced by a more sophisticated acyclicity condition.
Example 12 (Safety). The rules (3) and (4) are safe. Without the predicate d the program in Example 9 would not be safe.
Theorem 18 (Program Domain Independence). Safe programs are domain independent.
Proof (sketch). Let σ i = D i , P, X , S , r i , i = 1, 2 be semiring signatures s.t. Π is a σ i -formula for i = 1, 2 and let I w = (I H , I T , w) be a pointed σ i -HT-interpretation for i = 1, 2.
Not every domain independent program is safe. E.g. p(X) ← = B q(X) is not safe but is equivalent to the safe rule p(X) ← q(X) since X is a global variable and we can only derive p(x) when q(x) B = 1, i.e. when q(x) holds. Domain independence is undecidable but safety is sufficient, allows for complex rules like (3), (4) and those in Example 9, and is easily checked.
In the rest of the paper, we restrict ourselves to domain independent programs and can therefore remove the annotation σ from |= σ and · σ R and use |= and · R instead. Accordingly, we do not need to specify the signature for A C -programs Π anymore, as any semiring signature σ s.t. Π is an A C -program over σ suffices.
Program Equivalence An additional benefit of HT-semantics is that we are able to characterise strong program equivalence as equivalence in the logic of HT.
Similar results have already been proven for classical programs with (Pearce and Valverde 2008) or without variables (Lifschitz et al. 2001) and many more. As with classical programs: Theorem 20. For any Π 1 , Π 2 programs, Π 1 ≡ s Π 2 iff Π 1 has the same HT-models, i.e. satisfying pointed HT-interpretations, as Π 2 .
Proof (sketch). The direction ⇐ is clear. For ⇒ we can generalise the proof in (Lifschitz et al. 2001), by constructing Π , which asserts a subset of the interpretation I T that is ensured to be stable (I H ), and a subset that if partly present is ensured to be fully present (I T \ I H ).
Let Π 1 and Π 2 have different HT-models. W.l.o.g. there must be at least one HT-interpretation (I H , I T ) that is an HT-model of Π 1 but not of Π 2 . As in (Lifschitz et al. 2001) we simply define Then I T is an equilibrium model of Π 2 ∪ Π , but not of Π 1 ∪ Π and therefore Π 1 and Π 2 are not strongly equivalent.
Note that since I H may be infinite, this may result in programs of infinite size. This can be circumvented if auxiliary predicates are allowed in Π (see the Appendix).

Computational Complexity
We consider the computational complexity of the following problems: • Model Checking (MC): Given a safe program Π and an interpretation I of Π, is I is an equilibrium model of Π? • Satisfiability (SAT): Given a safe program Π, does Π have an equilibrium model?
The main factor that complicates these problems is that we may have to evaluate weighted formulas over an arbitrary semiring. If we want to prevent an increase in complexity, then we need to encode the elements of the semiring in some way which allows for efficient calculations and comparison. To this end, we use Efficient Encodability from (Eiter and Kiesel 2020).
This restriction is mild in practice; for example B, N, Z, Q, R max , 2 A are efficiently encodable.
Theorem 22 (Ground Complexity). For variable-free programs over efficiently encoded semirings (i) MC and (propositional) SE are co-NP-complete, and (ii) SAT is Σ p 2 -complete. Proof (sketch). The hardness parts are inherited from disjunctive logic programs (Dantsin et al. 2001), cf. Section 4, resp. HT-Logic (Lifschitz et al. 2001). The membership parts result by guess and check algorithms: for similar bounds as in ordinary ASP, we just need that I H |= k ∼ R α is polynomially decidable given (I H , I T ) and k ∼ R α; as R is efficiently encoded, this holds.
The non-ground complexity is significantly higher.
Theorem 23 (Non-ground Complexity). For safe programs over efficiently encoded semirings (i) MC is in EXPTIME, both co-NP PP -hard and NP PP -hard, and (ii) SAT and SE are undecidable.
Proof (sketch). (i) Given an interpretation I as a set of ground atoms, we check (I , I , H) |= r and (I , I , H) |= r for each I I and each ground instance r of a rule r ∈ Π in exponential time. The evaluation of algebraic constraints k ∼ R α is feasible in exponential time, since if α is of the form Σy 1 , . . . , y n α (y 1 , . . . , y n ) where α is quantifier-free, by safety of the program each y i must occur in some atom p( x). That is, to evaluate α, we only need to consider values ξ (y i ) for y i , i = 1, . . . , n that occur in the interpretation I . There are exponentially many such ξ ; for each of them, the value of α (ξ (y 1 ), . . . , ξ (y n )) can be computed in polynomial time given that R is efficiently encoded, yielding a value r ξ such that e(r ξ ) occupies polynomially many bits. The aggregation Σ ξ r ξ over all ξ is then feasible in exponential time by the assertion that r 1 ⊕ r 2 ≤ r 1 + r 2 + c and that e(r 1 ⊕ r 2 ) is computable in polynomial time given e(r 1 ), e(r 2 ). The (co-)NP PP -hardness is by a reduction from (co-)E-MAJSAT (Littman et al. 1998), which asks whether for a Boolean formula φ (x 1 , . . . , x n ) a partial assignment to x 1 , . . . , x k exists s.t. more than m = 2 n−k−1 of the assignments to x k+1 , . . . , x n satisfy φ ( x). Then the program (1)} if the answer for E-MAJSAT is yes and an equilibrium model {v (0), v(1)} if the answer is no.
has an equilibrium model iff X is a yes-instance of MMP, as p(0 d ) needs to be supported. For undecidability, let Π be the program from above and Π = Π \ {⊥ ← ¬p(0) d )}. As Π has no negation, its HT-models are the interpretations (I , I ) where both I and I are closed under the rules of Π , sets S such that p(X 1 ), . . . , p(X n ) ∈ S and whenever p(Y ), p(Z) ∈ S then also p(Y * Z) ∈ S. Similarly, the HT-models of Π are the interpretations (I , I ) where I and I are closed under the rules of Π and in addition p(0 d ) ∈ I . Therefore, Π ≡ s Π iff p(0 d ) ∈ L, where L is the least set closed under the rules of Π , which holds iff the answer for the mortal matrix problem on X is yes.
As NP PP contains the polynomial hierarchy (PH), this places MC between PH and EXPTIME; stronger assumptions on the encoding e(r) allow for PSPACE. In particular, for programs over the canonical semiring N, MC is co-NP C -complete for C = NP PP (while SAT and SE are undecidable). Naïve evaluation of k ∼ R α is infeasible in polynomial space, as α R (I H ) can be exponential in the number of variables in α. We can retain decidability for SAT and SE by limiting value invention, i.e. constraints X = R α( Y ), and value guessing. For the latter, we adapt domain restrictedness from (Niemelä et al. 1999).
Intuitively, only constants "known" by predicates in α can be "transferred" to predicates in β , and γ assigns a weight to each substitution. The pattern is explained less generally in Section 4. Let us call a semiring computable if ⊕, ⊗, −, −1 , > are computable. Then we obtain: Theorem 25. For safe programs without value invention where all algebraic constraints in rule heads are domain restricted and all semirings are computable, both SAT and SE are decidable.
Proof (sketch). For this class of programs we can show that they are finitely groundable, i.e. groundable over a finite domain without changing the answer sets, by using only the constants that occur in a program as the domain. Then the ground programs are variable free and since the semirings are computable both SAT and SE are decidable.
However, prohibiting value invention entirely is unnecessarily strong. Weaker restrictions like aggregate stratification (Faber et al. 2011) or argument restrictedness (Lierler and Lifschitz 2009) can be adapted to ASP(A C ); the resulting programs are finitely ground and decidable.

Related Work & Conclusion
A number of related works has already been mentioned above; we concentrate here on highlighting the differences of our approach to others.
• Semiring-based Constraint Logic Programming (Bistarelli et al. 1997), aProbLog (Kimmig et al. 2011) and our previous work (2020) also use semiring semantics. However, Bistarelli et al. aimed at semantics for CLP with multi-valued interpretations over lattice-like semirings and the other works aimed at semantics for weighted model counting and model selection over semirings.
• Hybrid ASP by Cabalar et al. (2020b;2020a). They defined an extension of HT Logic that includes general constraints and multi-valued interpretations for handling mixtures of ASP and CP. The approach integrates conditionals and aggregates; however, it relies on extra definitions to introduce their semantics while our semantics can capture the different constructs natively. The syntax of constraints (apart from over the reals) is left open, while we provide a uniform syntax over any semiring. Moreover, we study domain independence and safety, characterise strong equivalence, and provide complexity results.
• Nested Formulas with Aggregates due to Ferraris and Lifschitz (2010) have semantics similar to that of HT. They allow for arbitrary aggregate functions but only over the integers, whereas we allow for arbitrary values using semiring operations. While defined, the usage of non-ground constraints in rule heads was not considered. We can transfer our results and show that both choice and minimised constraints can be encoded and used safely in the formalism.
• Gelfond-Zhang Aggregates (2014) are semantically different from ours but presumably encodable in ASP(A C ). Their semantics introduced the vicious circle principle to ASP. Regarding expressiveness, aggregates are not allowed in rule-heads but aggregation functions are arbitrary.
• Arbitrary Constraint Atoms due to Son et al. (2007) as well as (monotone) Abstract Constraint Atoms due to Marek et al. (2006) define semantics for constraints abstractly by allowing them to be specified as a set of alternative sets of atoms that need to be satisfied. Naturally, this gives a semantics to arbitrary constraints, however syntactic shorthands are desirable to avoid an exponential blowup of the representation of the constraints. Marek et al. focus on monotone constraints, showing that their behaviour can be characterised by fixed-points of a non-deterministic operator.
• Formalisms for Intensional Functions in ASP as in (Bartholomew and Lee 2019; Cabalar 2011) define a semantics that allows the definition of functions using ASP. A priori, this differs from our goal aiming at an expressive predicate-based formalism. Nevertheless, Weighted Formulas could be used to specify the values of functions. Semantically we are closer to Cabalar et al.'s approach, where function values can be undefined and the stability condition is more similar.

Summary & Outlook
We have seen that algebraic constraints unify many previously proposed constructs for more succinct answer set programs, with low practical restrictions and no increase in the ground complexity. Among other novelties, we can specify whether constraints in ruleheads are minimised or guessed, can explicitly represent values from different sets and give an interesting alternative semantics for conditionals. Overall, the introduced framework opens up new possibilities for expressing programs succinctly and it gives rise to interesting questions.
We currently consider only a fragment of the weighted formulas. It would be interesting to see in the future, if other new and useful constructs can be expressed with a different fragment. Besides this, we want to use the general applicability of HT and Weighted Logic and extend ASP(A C ) to other domains, like temporal reasoning. Furthermore, an in-depth study of suitable conditions for finite groundability and the non-ground complexity in this context are indispensable for our ongoing work on an implementation.

Appendix A Proofs and additional details
In the following, we give proofs of the theorems and propositions of the paper. Furthermore, some more detailed explanations are included. Theorems and Propositions have the same numbering as in the original document. Auxiliary Theorems etc. are numbered with roman numerals.
Expressing unweighted formulas over semirings If we have a semiring R = (R, ⊕, ⊗, e ⊕ , e ⊗ ), where the addition ⊕ is invertible, then we can translate any formula φ without weights into one with weights τ(φ ) that does not use the boolean connectives and such that We define τ(φ ) inductively as follows: We can show by induction that τ(φ ) R (I w ) ∈ {e ⊕ , e ⊗ } and thus that the product Πy is always defined. Therefore, this encoding even includes the quantifiers. Furthermore, it is easy to see that the formula τ(φ ) is constructible in linear time from φ , and that hence the size of τ(φ ) is linear in the size φ . It is thus possible to eliminate unweighted formulas with polynomial overhead if addition ⊕ is invertible.
We note that while addition over the natural numbers N is not invertible, we can use the encoding τ(φ ) by moving from N to the integers Z and exploiting the fact that every natural number can be written as the sum of the squares of four integers (known as Lagrange's Four-Square Theorem); we then can add for global variables x over N the algebraic constraint x ≥ Z 0 and use for local variables y in constraint formulas over N the expression y 2 1 + y 2 2 + y 2 3 + y 2 4 over four local variables y 1 , . . . , y 4 over Z.
A similar translation is possible if the addition ⊕ is idempotent, i.e., k⊕k = k (as in the Boolean semiring), where the value of Σyα(y) over I w is naturally defined as k whenever a non-empty support supp ⊕ (α(x), I w ) leads to the single value k, i.e., for every ξ ∈ supp ⊕ (α(x), I w ) we have α(ξ ) σ R (I w ) = k, since supp ⊕ (τ(φ ), I w ) leads to the single value e ⊗ .
Proof. The claim can be easily verified by comparing the weighted semantics for R = B and the unweighted semantics. We consider the case φ = ψ → θ in more detail. Then α = β → B θ , where β and γ correspond to the rewritten versions of ψ and θ .
The proof for → R works over any semiring and not just the Boolean semiring. Therefore, we can drop the subscript R.
Proof. It is know that the proposition holds for formulas φ without algebraic constraints (Pearce and Valverde 2006). We can use the same proof by structural induction, given that we can prove that the claim holds for the additional base case φ = k ∼ R α.
In this case however, the definition of satisfaction tells us that

Constructs in ASP(A C ) and in other formalisms
Conditionals We consider in more detail, how the conditional semantics vc and df of (Cabalar et al. 2020) can be modelled in our formalism. Since we do not capture arbitrary constraints as Cabalar et al. do, we assume instead that conditionals in weighted formulas are allowed and show that it is unnecessary to allow them explicitly. We start with vc. Let r(s) = H(r(s)) ← B(r(s)) be some rule containing a conditional s = (s |s : φ ) which is supposed to be evaluated under vc semantics. This means that . Now if we simply replace s by φ * s + + + ¬φ * s we get if I w |= ¬φ I T |= r(s) and I w |= r(e ⊕ ) otherwise.
as desired. In order to model df, we further need that the addition ⊕ of the semiring R = (R, ⊕, ⊗, e ⊕ , e ⊗ ) is invertible, i.e. that we can use the connective − − −. Assume this is the case and let s = (s | s : φ ) be a conditional over the semiring R that we want to evaluate under df. Then its semantics is We can simply use the weighted formula φ * s + + + (e ⊗ + + + − − −φ ) * s :

Provenance
For provenance we define a translation of a positive datalog program Π = {r 1 , . . . , r m } as follows. First, we discuss terminology. For each predicate q in Π we introduce the following predicates.
• p q ( Y ,V, L, i, Z), which stores the value V of the provenance of q( Y ) using any derivation that uses exactly L leaf nodes, uses the rule r i last and the global variables in r i that do not occur in the head of r i had the value Z.
• p q ( Y ,V, L, i), which stores the value V of the provenance of q( Y ) using any derivation that uses exactly L leaf nodes, uses the rule r i last and the global variables in r i that do not occur in the head of r i took any value.
• p q ( Y ,V, L), which stores the value V of the provenance of q( Y ) using any derivation that uses exactly L leaf nodes and uses any rule r i last.
• p q ( Y ,V ), which stores the value V of the provenance of q( Y ).
• d q ( Y , L, i, Z), which asserts that there is a derivation of q( Y ) using exactly L leaf nodes that uses the rule r i last and the global variables in r i that do not occur in the head of r i had the value Z.
• d q ( Y , L, i), which asserts that there is a derivation of q( Y ) using exactly L leaf nodes that uses the rule r i last and the global variables in r i that do not occur in the head of r i took any value.
• d q ( Y , L), which asserts that there is a derivation of q( Y ) using exactly L leaf nodes that uses any rule r i . Let be some rule with index i, where w.l.o.g. n > 1 (we can always add a new extensional atom e() with provenance e ⊗ . We add the following rules to our translation T (Π): L 1 ), . . . , p q n ( X n ,V n , L n ), L = N L 1 + + + . . . + + + L n (E2) Here Z = n i=1 X i \ Y , i.e. the global variables of r i that do not occur in its head.
Further, for every predicate q in Π we add the rules This translation works as follows. The last rule sums up the value of each derivation tree to obtain the final provenance, whereas the previous rules calculate the provenance of less and less restricted derivation trees. Therefore, there is at least one answer set I such that p r ( Y ,V ) ∈ I iff the provenance of r( Y ) is V . On the other hand, since these rules are also all positive, there is exactly one answer set. The following results shows the correctness of the translation.
Theorem 14 (Provenance Encoding). Given a positive datalog program Π the A C -program T (Π) computes the provenance semantics over the ω-continuous semiring R in the following sense. Let D be an edb and r( x) a query result of D ∪ Π with semiring provenance v. Then the unique equilibrium model We proceed by induction on the number L of leaf nodes that are used in the derivation tree, to show that our construction is correct and the predicates indeed behave as they should according to their description, which among other things implies that v is the provenance of the query result r( x) iff the unique equilibrium model I of T (Π) ∪ {p e ( x, v, 1) ←| (e( x), v) ∈ D} contains p r ( x, v). In the proof we only consider the predicates for the values of the provenance in detail. The correctness of the derivability predicates d r ( Y , L[, i, Z]) follows analogous reasoning since the rules for derivability are just simplified versions of the rules for the provenance values.
The case L = 0 is impossible, since we always use at least one leaf node in each derivation. The case L = 1 occurs exactly when r( Y ) is a leaf node. Since edb predicates do not occur in heads of rules in Π, the only rules we have to consider are of the form p e ( x, v, 1) ←. Here, the claim holds.
Assume the claim holds for all L < L. Consider the rule (E1). For the body to be satisfied,we need that p q 1 ( X 1 ,V 1 , L 1 ), . . . , p q n ( X n ,V n , L n ) are contained in I . Since L i > 0, L = L 1 +· · ·+L n and n > 1, we know that L i < L and therefore the claim holds for p q 1 ( X 1 ,V 1 , L 1 ), . . . , p q n ( X n ,V n , L n ). Therefore, p q i ( x i , v i , l i ) ∈ I iff v i is the provenance of q i ( x i ) using any derivation that uses exactly l i leaf nodes. Let us denote by dtree (q i ( x i )) the set of all derivation trees τ for q i ( x i ) that use exactly leaf nodes, and by leaves(τ) the set of leaf nodes in the tree τ. Then Then for v = v 1 ⊗· · ·⊗v n , and l = l 1 +· · ·+l n we have that p r ( y, v, l, i, z) is in I iff for i = 1, . . . , n the atoms p q i ( x i , v i , l i ) are in I . This however means that for i = 1, . . . , n the equation (E8) holds.
We use that for every combination of derivations τ 1 , . . . , τ n respectively for q 1 ( x 1 ), . . . , q n ( x n ) there is a derivation of r( y) using the rule r i last, where the global variables that do not occur in the head of r i have the value z. According to the distributive law, assuming that last(τ) denotes the last rule in derivation tree τ and that gvar(r i ) denotes the value of the global variables in rule r i that do not occur in the head of r i , we obtain that v = v 1 ⊗ . . . ⊗v n = τ∈dtree l (r( y)), r i =last(τ), gvar(r i )= z t∈leaves(τ) R(t).
It follows that rule (E1) ensures that the predicates of the form p r ( Y ,V, L, i, Z) satisfy our claim (for L).
Next for rule (E3). Here, we simply aggregate over the global variables in r i that do not occur in the head of r i .
The head atom p r ( Y ,V, L, i) should describe the value V of the provenance of r( Y ) using any derivation that uses exactly L leaf nodes and uses rule r i last. This value is given by τ∈dtree L (r( x)), r i =last(τ) t∈leaves(τ) R(t) = z τ∈dtree l (r( x)), r i =last(τ), gvar(r i )= z t∈leaves(τ) R(t).
We know that according to the previous rule (E1), the predicate p r ( Y ,V, L, i, Z) encodes exactly the inner sum. Since rule (E3) performs the outer sum, it follows that rule (E3) ensures that the predicates of the form p r ( Y ,V, L, i) satisfy our claim (for L).
Next, the rule (E5) aggregates over the different rules that were used last to derive r( x) using l leaf nodes. The argumentation is analogous to the one for the last rule (E3), where we aggregated over z instead of the rule index i like here.
Overall, the inductive proof of the correctness of the rules specifying the predicates p r ( Y ,V, L[, i, Z]) succeeds, since all the predicates are correctly defined for L given that predicates are well defined for L < L.
Last but not least we consider the last rule (E7) which should produce the final result. According to the definition of provenance for datalog programs in (Green et al. 2007), the label v of the query result r( x) is v = τ∈dtree(r( x)) t∈leaves(τ) where dtree(r( x)) is the set of all derivation trees for r( x) and R(t) is the provenance of the leaf t. We reformulate this equation as follows: v = τ∈dtree(r( x)) t∈leaves(τ) As due to rule (E7), we have p r ( x, v) we obtain that p r ( x, v) ∈ I . This concludes the proof.
6 Language Aspects

Safety
Theorem I (Support Independence). Let σ 1 , σ 2 be semiring signatures, I w be a pointed σ i -HTinterpretation (i = 1, 2) and α be a weighted σ i -formula (i = 1, 2) that is syntactically domain independent w.r.t. variable x. Then Proof. We give a proof using structural induction on the syntactically domain independent formula α. In the following let I w some pointed HT-interpretation and σ 1 , σ 2 semiring signatures that contain all the constants of α and I w • Case α = k: This formula contains no local variables, therefore the equality is trivially fulfilled.
The given formulas are all range restricted. For range restricted formulas it is known, that they are domain independent (see for example (Demolombe 1992)), which implies that when they are seen as weighted formulas, their support does not depend on the signature.
The semantics of ¬ ⊕ β is the inverse of the semantics of β w.r.t. ⊕, which is e ⊕ iff the semantics of β is e ⊕ . Therefore we have The semantics of ¬ ⊗ β is the inverse of the semantics of β w.r.t. ⊗ or e ⊕ if the semantics of β is e ⊕ . Therefore we have on the one hand that β (ξ ) σ i R (I ) = e ⊕ ⇒ ¬ ⊗ β (ξ ) σ i R (I ) = e ⊕ . Furthermore, we have for the other direction that The second disjunct implies that e ⊕ = e ⊗ since e ⊕ annihilates R. Therefore since ∀r ∈ R : e ⊗ ⊗ r = r holds we have that ∀r ∈ R : e ⊕ ⊗ r = e ⊕ = r, meaning our semiring has exactly one element, namely e ⊕ . Therefore we have we know that the semantics of ¬ ⊗ β is e ⊕ iff the semantics of β is e ⊕ and as in the previous case we obtain • Case α = ¬¬α 1 (x): We know that {ξ ∈ r 1 (s(x)) | ¬¬α 1 (ξ ) σ 1 R (I w ) = e ⊕ } is equal to {ξ ∈ r 1 (s(x)) | α 1 (ξ ) σ 1 R (I w ) = e ⊕ }. Therefore this case follows immediately from the inductive hypothesis for α 1 (x).
works analogously to the one above.
The proof for more than one local variable works analogously.
Theorem 16 is a corollary of Theorem I: Theorem 16 (Domain Independence). If a formula is syntactically domain independent, then it is also domain independent.
Proof. When we evaluate α( X) we take the sum over supp ⊕ (α( X), I w ). Due to the previous lemma we know that the support is invariant under changing the domain. Further, we know that for a given assignment of the local variables the semantics is independent of the domain. Therefore, the semantics is invariant under changing the domain for syntactically domain independent formulas.
Theorem 18 (Program Domain Independence). If a program Π is safe, then it is domain independent.
Proof (sketch). The direction ⇐ is clear. For ⇒ we can generalise the proof in (Lifschitz et al. 2001), by constructing Π , which asserts a subset of the interpretation I T that is ensured to be stable (I H ), and a subset that if partly present is ensured to be fully present (I T \ I H ).
Let Π 1 and Π 2 have different HT-models. W.l.o.g. there must be at least one HT-interpretation (I H , I T ) that is an HT-model of Π 1 but not of Π 2 . As in (Lifschitz et al. 2001) we simply define Then I T is an equilibrium model of Π 2 ∪ Π , but not of Π 1 ∪ Π and therefore Π 1 and Π 2 are not strongly equivalent.
The above proof relies on the fact that our semantics is defined for program with infinite sets of rules. If we want to avoid this, there are multiple options. In (Lifschitz et al. 2007) the strong equivalence of arbitrary first-order formulas was considered and characterised as equivalence in HT Logic. The proof however uses the fact that the strong equivalence considered in their work is for any first-order sentence and not only for programs, which are a syntactic fragment. A straight forward way to reproduce their proof strategy in our setting seems not to be apparent.
Nevertheless, it is possible to prove the statement when programs are finite sets of rules in our setting, provided that auxiliary predicate symbols are available not occurring in the program (which trivially holds if we have infinitely many predicates of each arity in the underlying predicate signature P).
Proof (sketch). We have two directions to prove. Based on the idea of (Lifschitz et al. 2001). (⇒) We prove this direction using contraposition, that is we assume that we have two programs The program ∆ consists of the following rules, where G is the set of predicates that occur in Π 1 ∪ Π 2 : for p ∈ G with arity n.
for p,q ∈ G with arities n, m.
for p,q ∈ G with arities n, m.
Intuitively repair p (X 1 , . . . , X n ) guesses some tuple (X 1 , . . . , X n ) for predicate p such that the atom p(X 1 , . . . , X n ) should definitely be satisfied. Similarly, fill p,q (X 1 , . . . , X n ,Y 1 , . . . ,Y m ) guesses some values (X 1 , . . . , X n ) and (Y 1 , . . . ,Y m ) for the predicates p and q, respectively, such that if p(X 1 , . . . , X n ) is satisfied then also q(Y 1 , . . . ,Y n ) should be satisfied. Consider now the interpretation Then we have that (I H , I * , H) |= Π 1 ∪ ∆, therefore I * is not an equilibrium model of Π 1 ∪ ∆. However for Π 2 we have that (I * , I * , H) |= Π 2 ∪ ∆. Furthermore, consider now some interpretation I ⊆ I * s.t. (I , I * , H) |= Π 2 ∪ ∆. Due to the included repairs, we know that at least I H ⊆ I . Moreover, we know that this inclusion is strict even when we consider only the predicates occurring in Π 1 ∪ Π 2 , since (I H , I T , H) |= Π 2 . Therefore, due to the fills we have to include all the predicates from I T . It follows that I = I * and therefore that I * is an equilibrium model of Π 2 ∪ ∆.
(⇐) Assume that Π 1 has the same HT-models as Π 2 and consider for an arbitrary program ∆ the HT-models of Π 1 ∪ ∆ and Π 2 ∪ ∆. Those are exactly the HT-models (I H , I T ) s.t. since Π 1 and Π 2 have the same HT-models. Since the HT-models of Π 1 ∪ ∆ and Π 2 ∪ ∆ are the same, we also know that the equilibrium models I are the same, since it follows that

Complexity
Theorem II (Complexity of evaluation). Let R = (R, ⊕, ⊗, e ⊕ , e ⊗ ) some semiring and e : R → N some encoding function s.t. R is efficiently encoded by e.
Then for a quantifier-free weighted formula over R and pointed HT-interpretation I w , we can calculate e( α R (I w )) in polynomial time.
Proof. The proof is by structural induction on the formula α, with induction invariant that t(α) the time needed is in O(N n ), where N is the size of the input, n ∈ N is a constant not depending on the input. Further, s(α) the size of the representation of the obtained value, i.e. α R (I w ) , is in O(N).' • Base Cases: -α = e(k): Then one can evaluate the expression by simply returning e(k). This is feasible in polynomial time. The size of the output is linear in the size of the input. -α = φ : We simply check if I w |= φ and return e ⊕ or e ⊗ accordingly. This is possible in polynomial time since φ is quantifier-free and the size of the output is also bounded by a constant.
We have shown the invariant for all formulae up to a certain structural complexity.

• Induction
Step: -α = β 1 → β 2 : We know that for β i the invariant holds, therefore we can check in time bounded polynomially in the size of the formula, whether β i R (I w ) = e ⊕ and output e ⊕ or e ⊗ accordingly. The size of the output is again bounded by a constant. -α = β 1 + + +β 2 : We know that the invariant holds for β 1 , β 2 . Further t(α) = t(β 1 )+t(β 2 )+ x, where x is the time needed for addition of the results for β 1 and β 2 . We know that x is polynomial in s(β 1 ) + s(β 2 ), which we know to be in O(N). Therefore x ∈ O(N l ), where l is the degree of the polynomial bounding the time needed to add two numbers. It follows that t(α) ∈ O(N n ). For s(α) we can see that And therefore s(α) ∈ O(N). -α = β 1 * β 2 : The proof works analogously to the proof for the case α = β 1 + + + β 2 .
α = − − −β : We know that the invariant holds for β . Further t(α) = t(β ) + x, where x is the time needed for inversion of the result for β . We know that x is polynomial in s(β ), which we know to be in O(N). Therefore x ∈ O(N l ), where l is the degree of the polynomial bounding the time needed to invert a number. It follows that t(α) ∈ O(N n ). For s(α) we can see that And therefore s(α) ∈ O(N). -α = β − − −1 1 1 : The proof works analogously to the proof for the case α = − − −β .
Theorem 22 (Ground Complexity). Let Π be a variable-free program s.t. each semiring in Π is efficiently encoded. Then • MC is co-NP-complete. • (propositional) SE is co-NP-complete.
The memberships follow from the possibility of applying guess and check algorithms. We only need that given (I H , I T ) and algebraic constraint k ∼ R α, we can decide in polynomial time whether I H |= k ∼ R α. This is possible since we know that R is efficiently encoded: We only need to perform polynomially many additions, multiplications and inversions which each take polynomial time as Theorem II says.
Theorem 23 (Non-gound Complexity). Let Π be a safe program such that each semiring in Π is efficiently encoded. Then (i) MC is in EXPTIME, and co-NP NP PP -hard (thus also co-NP PP -hard and NP PP -hard).
Proof (sketch). (i) Given the interpretation I (as set of ground atoms), we can iterate over all I I and check (I , I , H) |= r , as well as (I , I , H) |= r for each ground instance r of a rule r ∈ Π in exponential time. The iteration and considering one ground instance r at a time is feasible in polynomial space; the evaluation of algebraic constraints k ∼ R α is feasible in exponential time, since if α is of the form Σy 1 , . . . , y n α (y 1 , . . . , y n ) where α is quantifier-free, by safety of the program each y i must occur in some atom p( x). That is, to evaluate α, we only need to consider values ξ (y i ) for y i , i = 1, . . . , n that occur in the interpretation I . There are exponentially many such ξ ; for each of them, the value of α (ξ (y 1 ), . . . , ξ (y n )) can be computed in polynomial time given that R is efficiently encoded, yielding a value r ξ such that e(r ξ ) occupies polynomially many bits. The aggregation Σ ξ r ξ over all ξ is then feasible in exponential time by the assertion that r 1 ⊕ r 2 ≤ r 1 + r 2 + c and that e(r 1 ⊕ r 2 ) is computable in polynomial time given e(r 1 ), e(r 2 ).
We note that the value of Σ ξ r ξ may under the assertions occupy exponentially many bits; under stronger assumptions on the encoding e(r), a smaller upper bound may be derived. E.g., we obtain membership in PSPACE if it is ensured that for the addition, we have the stronger condition r 1 ⊕ r 2 ⊕ · · · ⊕ r n ≤ (1 + log n) max i r i + c, for every n ≥ 2 where c is a constant. For example, the canonical semiring N of the natural numbers satisfies this property.
Assume the answer for AE-MAJSAT is yes. Then for every subset I ⊆ I we have (I , I , H) |= Π. If we remove e(i) or both a i (0) and a i (1) for some i, this is clear. Otherwise, we know that for each i some a i ( j i ) holds. Then for these values there exist values j m+1 , . . . , j k s.t. φ ( j 1 , . . . , j k , X k+1 , . . . , X n ) is a yes instance for MAJSAT. Therefore the body a 1 ( j 1 ), . . . , a m ( j m ), e(X j+1 ), . . . , e( j k ), 2 n−k−1 < N e(X k+1 ) * . . . * e(X n ) * φ ( X) is satisfied and if (I , I , H) |= Π we know that I = I .
On the other hand if the answer for AE-MAJSAT is no, due to the partial assignment j 1 , . . . , j m to the variables x 1 , . . . , x m , for I = {a 1 ( j 1 ), . . . , a m ( j m ), e(0), e(1)} we have (I , I , H) |= Π, and therefore I is not an equilibrium model.
(iii) Let Π be the program from above. Then the answer to the mortal matrix problem for X is yes iff Π is strongly equivalent to Π = Π \ {⊥ ← ¬p(0) d )}. This can be seen as follows.
As Π has no negation, its HT-models are the interpretations (I , I ) where both I and I are closed under the rules of Π , sets S such that p(X 1 ), . . . , p(X n ) ∈ S and whenever p(Y ), p(Z) ∈ S then also p(Y * Z) ∈ S. Similarly, the HT-models of Π are the interpretations (I , I ) where I and I are closed under the rules of Π and in addition p(0 d ) ∈ I . Therefore, Π ≡ s and Π iff p(0 d ) ∈ L, where L is the least set closed under the rules of Π , which holds iff the answer for the mortal matrix problem on X is yes.
Corollary III. When the program Π is over the semiring N of the natural numbers, we have co-NP NP PP -completeness for MC.
Proof. The hardness is due to the proof of the previous theorem. The membership follows from the fact that we can check the satisfaction of constraints over N using a PP oracle.
This can be seen as follows. We can evaluate weighted formulas over N of the form Σy 1 . . . Σy n α where α is quantifier-free using a #P oracle: we can non-deterministically choose an assignment ξ to y 1 , . . . , y n , calculate r = α(ξ ) N (I w ) in polynomial time and generate r accepting branches.
Since P PP is equal to P #P also co-NP NP PP is equal to co-NP NP #P . As for the co-NP NP #P membership: Given a program Π and a potential equilibrium model I we can guess a subset I I and check whether (I , I , H) |= Π. The latter can be achieved in co-NP #P by guessing a rule r ∈ Π and an assignment ξ to its global variables. Then we can check whether (I , I , H) |= r(ξ ) in P #P by checking satisfaction of each atom and constraint in r(ξ ).
We note that MC is decidable for A C -programs over the natural numbers while SAT and SE are undecidable. This may not be much surprising from Theorem ??, given that the semiring Z d×d is (efficiently) encodable to N. The undecidability can directly be shown by a reduction from solving Diophantine equations, i.e., polynomial equations P(x 1 , ..., x n ) = 0 in variables x 1 , . . . , x n over the integers, which by Matiyasevich's celebrated result is undecidable; this holds if the solutions are restricted to the natural numbers (Matiyjasevich 1996). We can equivalently consider polynomial equations P(x 1 , ..., x n ) = Q(x 1 , ..., x n ) where all coefficients in the polynomial expressions P(x 1 , . . . , x n ) and Q(x 1 , . . . , x n ) are non-negative. We then can write a program Π consisting of the rules n(0) ← . n(X) ← n(Y ), X = N 1 +Y.

⊥ ← ¬sol.
The program Π is safe and it has a (unique) equilibrium model (in which sol is true) iff a solution to P(x 1 , ..., x n ) = Q(x 1 , ..., x n ) exists. Furthermore, the existence of an equilibrium model is equivalent to Π ≡ s Π \ {⊥ ← ¬sol}.
Definition IV (Finite Groundability). Let σ some semiring signature and Π an A C -program over σ . Then Π is finitely groundable if there is a signature σ = D, P, X , S , r s.t. the equilibrium models of Π over σ are the same as the equilibrium models over σ and D is finite.
Theorem V. For finitely ground programs over σ = D, P, X , S , r (|D| < ∞) that only use computable semirings, SAT and SE are decidable.
Proof. We can replace universally quantified formulas with finite conjunctions over all the substitutions and existentially quantifies formulas with finite disjunctions over all the substitutions.
For variable free programs we have decidability when all the semirings are computable.
Theorem VI. Let Π be a safe program over σ without value invention, where all algebraic constraints in heads are domain restricted. Then Π is finitely ground over σ = D, P, X , S , r , where D is the subset of domain values that occur in Π.
Proof. Let I be a σ -interpretation s.t. (I , I , T ) |= Π. Then for I obtained from I by removing all atoms that contain constants not from D, we have (I , I , H) |= Π. This can be seen as follows: Assume r ∈ Π with global variables x 1 , . . . , x n . Since r is safe and does not contain value invention, the body of r can only be satisfied for substitutions of the variables with elements from D. Therefore, if the head of r is an atom p( x), we can only derive p( ξ ) for substitutions from D and therefore, if the rule was satisfied previously, it is still satisfied. Otherwise, if the head of r is a constraint, we know that it is domain restricted, i.e. of the form k ∼ R ¬¬α( X) * (α( X) → β ( X)) * γ( X), where α( X), β ( X) are syntactically domain independent and all atoms in γ( X) are locally ground. Let ξ be some assignments to X over the original domain. We consider first ¬¬α( ξ ). It holds that ¬¬α( ξ ) R (I H ) = e ⊕ ⇐⇒ ¬α( ξ ) R (I H ) = e ⊗ ∨ ¬α( ξ ) R (I T ) = e ⊗ ⇐⇒ α( ξ ) R (I H ) = e ⊕ ∧ α( ξ ) R (I T ) = e ⊕ ∨ α( ξ ) R (I T ) = e ⊕ ⇐⇒ α( ξ ) R (I T ) = e ⊕ Therefore this part of the formula is not influenced by I . Secondly we consider α( ξ ) → β ( ξ ). We only need to consider this value if ¬¬α( ξ ) R (I H ) is unequal to zero, i.e. if α( ξ ) R (I T ) is unequal to zero. Now if α( ξ ) → β ( ξ ) R (I T ) is unequal to zero this implies that β ( ξ ) R (I T ) is unequal to zero. If all the values in ξ are from D, there is no change. Otherwise we know that α( ξ ) R (I H ) = β ( ξ ) R (I H ) = e ⊕ since α( X) and β ( X) are syntactically domain independent and therefore have value e ⊕ for values that are not mentioned in the interpretation I (see proof of the invariance of the support for syntactically domain independent weighted formulas). It follows that α( ξ ) → β ( ξ ) R (I T ) is also unequal to zero.
We see that since (I , I , T ) |= Π also (I , I , T ) |= Π. Therefore I can only be an equilibrium model if it contains only constants from D, which implies that Π is finitely ground over σ Theorem 25. For safe programs without value invention where all algebraic constraints in rule heads are domain restricted and all semirings are computable, both SAT and SE are decidable.
Proof. This result follows easily from Theorems V and VI.