On the Foundations of Grounding in Answer Set Programming

We provide a comprehensive elaboration of the theoretical foundations of variable instantiation, or grounding, in Answer Set Programming (ASP). Building on the semantics of ASP's modeling language, we introduce a formal characterization of grounding algorithms in terms of (fixed point) operators. A major role is played by dedicated well-founded operators whose associated models provide semantic guidance for delineating the result of grounding along with on-the-fly simplifications. We address an expressive class of logic programs that incorporates recursive aggregates and thus amounts to the scope of existing ASP modeling languages. This is accompanied with a plain algorithmic framework detailing the grounding of recursive aggregates. The given algorithms correspond essentially to the ones used in the ASP grounder gringo.


Introduction
Answer Set Programming (ASP; Lifschitz 2002) allows us to address knowledge-intense search and optimization problems in a greatly declarative way due to its integrated modeling, grounding, and solving workflow (Gebser and Schaub 2016;Kaufmann et al . 2016). Problems are modeled in a rule-based logical language featuring variables, function symbols, recursion, and aggregates, among others. Moreover, the underlying semantics allows us to express defaults and reachability in an easy way. A corresponding logic program is then turned into a propositional format by systematically replacing all variables by variable-free terms. This process is called grounding. Finally, the actual ASP solver takes the resulting propositional version of the original program and computes its answer sets.
Given that both grounding and solving constitute the computational cornerstones of ASP, it is surprising that the importance of grounding has somehow been eclipsed by that of solving. This is nicely reflected by the unbalanced number of implementations. With lparse (Syrjänen 2001b), (the grounder in) dlv (Faber et al . 2012), and gringo (Gebser et al . 2011), three grounder implementations face dozens of solver implementations, among them smodels (Simons et al . 2002), (the solver in) dlv (Leone et al . 2006), assat (Lin and Zhao 2004), cmodels (Giunchiglia et al . 2006), clasp (Gebser et al . 2012), wasp  just to name the major ones. What caused this imbalance? 2 R. Kaminski and T. Schaub One reason may consist in the high expressiveness of ASP's modeling language and the resulting algorithmic intricacy (Gebser et al . 2011). Another may lie in the popular viewpoint that grounding amounts to database materialization, and thus that most fundamental research questions have been settled. And finally the semantic foundations of full-featured ASP modeling languages have been established only recently (Harrison et al . 2014;Gebser et al . 2015a), revealing the semantic gap to the just mentioned idealized understanding of grounding. In view of this, research on grounding focused on algorithm and system design (Faber et al . 2012;Gebser et al . 2011) and the characterization of language fragments guaranteeing finite propositional representations (Syrjänen 2001b;Gebser et al . 2007;Lierler and Lifschitz 2009;Calimeri et al . 2008).
As a consequence, the theoretical foundations of grounding are much less explored than those of solving. While there are several alternative ways to characterize the answer sets of a logic program (Lifschitz 2008), and thus the behavior of a solver, we still lack indepth formal characterizations of the input-output behavior of ASP grounders. Although we can describe the resulting propositional program up to semantic equivalence, we have no formal means to delineate the actual set of rules.
To this end, grounding involves some challenging intricacies. First of all, the entire set of systematically instantiated rules is infinite in the worst -yet not uncommon -case. For a simple example, consider the program: This program induces an infinite set of variable-free terms, viz. a, f (a), f (f (a)), . . . , that leads to an infinite propositional program by systematically replacing variable X by all these terms in the second rule, viz.
On the other hand, modern grounders only produce the fact p(a) and no instances of the second rule, which is semantically equivalent to the infinite program. As well, ASP's modeling language comprises (possibly recursive) aggregates, whose systematic grounding may be infinite in itself. To illustrate this, let us extend the above program with the rule q ← #count {X : p(X)} = 1 (1) 4 R. Kaminski and T. Schaub Section 2 lays the basic foundations of our approach. We start in Section 2.1 by recalling definitions of (monotonic) operators on lattices; they constitute the basic building blocks of our characterization of grounding algorithms. We then review infinitary formulas along with their stable and well-founded semantics in Sections 2.2, 2.3 and 2.4, respectively. In this context, we explore several operators and define a class of infinitary logic programs that allows us to capture full-featured ASP languages with (recursive) aggregates. Interestingly, we have to resort to concepts borrowed from id-logic (Bruynooghe et al . 2016;Truszczyński 2012) to obtain monotonic operators that are indispensable for capturing iterative algorithms. Notably, the id-well-founded model can be used for approximating regular stable models. Finally, we define in Section 2.5 our concept of program simplification and elaborate upon its semantic properties. The importance of program simplification can be read off two salient properties. First, it results in a finite program whenever the interpretation used for simplification is finite. And second, it preserves all stable models when simplified with the id-well-founded model of the program.
Section 3 is dedicated to the formal foundations of component-wise grounding. As mentioned, each rule is instantiated in the context of all atoms being possibly derivable up to this point. In addition, grounding has to take subsequent atom definitions into account. To this end, we extend well-known operators and resulting semantic concepts with contextual information, usually captured by two-and four-valued interpretations, respectively, and elaborate upon their formal properties that are relevant to grounding. In turn, we generalize the contextual operators and semantic concepts to sequences of programs in order to reflect component-wise grounding. The major emerging concept is essentially a well-founded model for program sequences that takes backward and forward contextual information into account. We can then iteratively compute this model to approximate the well-founded model of the entire program. This model-theoretic concept can be used for governing an ideal grounding process.
Section 4 turns to logic programs with variables and aggregates. We align the semantics of such aggregate programs with the one of Ferraris (2011) but consider infinitary formulas (Harrison et al . 2014). In view of grounding aggregates, however, we introduce an alternative translation of aggregates that is strongly equivalent to that of Ferraris but provides more precise well-founded models. In turn, we refine this translation to be bound by an interpretation so that it produces finitary formulas whenever this interpretation is finite. Together, the program simplification introduced in Section 2.5 and aggregate translation provide the basis for turning programs with aggregates into semantically equivalent finite programs with finitary subformulas.
Section 5 further refines our semantic approach to reflect actual grounding processes. To this end, we define the concept of an instantiation sequence based on rule dependencies. We then use the contextual operators of Section 3 to define approximate models of instantiation sequences. While approximate models are in general less precise than well-founded ones, they are better suited for on-the-fly grounding along an instantiation sequence. Nonetheless, they are strong enough to allow for completely evaluating stratified programs.
Section 6 lays out the basic algorithms for grounding rules, components, and entire programs and characterizes their output in terms of the semantic concepts developed in the previous sections. Of particular interest is the treatment of aggregates, which are decomposed into dedicated normal rules before grounding, and reassembled afterward. 6 R. Kaminski and T. Schaub • F i+1 is the set of all elements of F i , all expressions H ∧ and H ∨ with H ⊆ F i , and all expressions F → G with F, G ∈ F i .
The set F = ∞ i=0 F i contains all (infinitary propositional) formulas over Σ. In the following, we use the shortcuts We say that a formula is finitary, if it has a finite number of subformulas. An occurrence of a subformula in a formula is called positive, if the number of implications containing that occurrence in the antecedent is even, and strictly positive if that number is zero; if that number is odd the occurrence is negative. The sets F + and F − gather all atoms occurring positively or negatively in formula F , respectively; if applied to a set of formulas, both expressions stand for the union of the respective atoms in the formulas. Also, we define F ± = F + ∪ F − as the set of all atoms occurring in F .
A two-valued interpretation over signature Σ is a set I of propositional atoms such that I ⊆ Σ. Atoms in an interpretation I are considered true and atoms in Σ \ I as false.
The set of all interpretations together with the ⊆ relation forms a complete lattice.
The satisfaction relation between interpretations and formulas is defined as follows: • I |= a for atoms a if a ∈ I, An interpretation I is a model of a set H of formulas, written I |= H, if it satisfies each formula in the set. In the following, all atoms, formulas, and interpretations operate on the same (implicit) signature, unless mentioned otherwise.

Logic programs and stable models
Our terminology in this section keeps following the one of Truszczyński (2012).
The reduct F I of a formula F w.r.t. an interpretation I is defined as: • {G I | G ∈ H} ∨ if I |= F and F = H ∨ , and An interpretation I is a stable model of a formula F if it is among the (set inclusion) minimal models of F I . Note that the reduct removes (among other unsatisfied subformulas) all occurrences of atoms that are false in I. Thus, the satisfiability of the reduct does not depend on such atoms, and all minimal models of F I are subsets of I. Hence, if I is a stable model of F , then it is the only minimal model of F I .
Sets H 1 and H 2 of infinitary formulas are equivalent if they have the same stable models and classically equivalent if they have the same models; they are strongly equivalent if, for https://doi.org/10.1017/S1471068422000308 Published online by Cambridge University Press Foundations of grounding in ASP 7 any set H of infinitary formulas, H 1 ∪H and H 2 ∪H are equivalent. As shown by Harrison et al . (2017), this also allows for replacing a part of any formula with a strongly equivalent formula without changing the set of stable models.
In the following, we consider implications with atoms as consequent and formulas as antecedent. As common in logic programming, they are referred to as rules, heads, and bodies, respectively, and denoted by reversing the implication symbol. More precisely, an F-program is set of rules of form h ← F where h ∈ F 0 and F ∈ F. We use H(h ← F ) = h to refer to rule heads and B(h ← F ) = F to refer to rule bodies. We extend this by letting H(P ) = {H(r) | r ∈ P } and B(P ) = {B(r) | r ∈ P } for any program P .
An interpretation I is a model of an F-program P , written I |= P , if I |= B(r) → H(r) for all r ∈ P . The latter is also written as I |= r. We define the reduct of P w.r.t. I as P I = {r I | r ∈ P } where r I = H(r) ← B(r) I . As above, an interpretation I is a stable model of P if I is among the minimal models of P I . Just like the original definition of Gelfond and Lifschitz (1988), the reduct of such programs leaves rule heads intact and only reduces rule bodies. (This feature fits well with the various operators defined in the sequel.) This program-oriented reduct yields the same stable models as obtained by applying the full reduct to the corresponding infinitary formula.

Proposition 2
Let P be an F-program.
Then, the stable models of formula {B(r) → H(r) | r ∈ P } ∧ are the same as the stable models of program P .
For programs, Truszczyński (2012) introduces in an alternative reduct, replacing each negatively occurring atom with ⊥, if it is falsified, and with , otherwise. More precisely, the so-called id-reduct F I of a formula F w.r.t. an interpretation I is defined as where a is an atom, H a set of formulas, and F and G are formulas. The id-reduct of an F-program P w.r.t. an interpretation I is P I = {r I | r ∈ P } where r I = H(r) ← B(r) I . As with r I , the transformation of r into r I leaves the head of r unaffected.

Example 1
Consider the program containing the single rule p ← ¬¬p.
We get the following reduced programs w.r.t. interpretations ∅ and {p}: Kaminski and T. Schaub Extending the definition of positive occurrences, we define a formula as (strictly) positive if all its atoms occur (strictly) positively in the formula. We define an F-program as (strictly) positive if all its rule bodies are (strictly) positive.
For example, the program in Example 1 is positive but not strictly positive because the only body atom p appears in the scope of two antecedents within the rule body ¬¬p.
As put forward by van Emden and Kowalski (1976), we may associate with each program P its one-step provability operator T P , defined for any interpretation X as Proposition 3 (Truszczyński 2012) Let P be a positive F-program.
Then, the operator T P is monotone.
Fixed points of T P are models of P guaranteeing that each contained atom is supported by some rule in P ; prefixed points of T P correspond to the models of P . According to Theorem 1(a), the T P operator has a least fixed point for positive F-programs. We refer to this fixed point as the least model of P and write it as LM (P ).
Observing that the id-reduct replaces all negative occurrences of atoms, any id-reduct P I of a program w.r.t. an interpretation I is positive and thus possesses a least model LM (P I ). This gives rise to the following definition of a stable operator (Truszczyński 2012): Given an F-program P , its id-stable operator is defined for any interpretation I as S P (I) = LM (P I ).
The fixed points of S P are the id-stable models of P .
Note that neither the program reduct P I nor the formula reduct F I guarantee (least) models. Also, stable models and id-stable models do not coincide in general.

Example 2
Reconsider the program from Example 1, comprising rule This program has the two stable models ∅ and {p}, but the empty model is the only id-stable model.
Then, the id-stable operator S P is antimonotone.
No analogous antimonotone operator is obtainable for F-programs by using the program reduct P I (and for general theories with the formula reduct F I ). To see this, reconsider Example 2 along with its two stable models ∅ and {p}. Given that both had to be fixed points of such an operator, it would behave monotonically on ∅ and {p}.
In view of this, we henceforth consider exclusively id-stable operators and drop the prefix "id". However, we keep the distinction between stable and id-stable models. Truszczyński (2012) identifies in a class of programs for which stable models and idstable models coincide. The set N consists of all formulas F such that any implication in F has ⊥ as consequent and no occurrences of implications in its antecedent. An Nprogram consists of rules of form h ← F where h ∈ F 0 and F ∈ N .
Then, the stable and id-stable models of P coincide.
Note that a positive N -program is also strictly positive.

Well-founded models
Our terminology in this section follows the one of Truszczyński (2018) and traces back to the early work of Belnap (1977) and Fitting (2002). 2 We deal with pairs of sets and extend the basic set relations and operations accordingly. Given sets I , I, J , J, and X, we define: A four-valued interpretation over signature Σ is represented by a pair (I, J) (Σ, Σ) where I stands for certain and J for possible atoms. Intuitively, an atom that is • certain and possible is true, • certain but not possible is inconsistent, • not certain but possible is unknown, and • not certain and not possible is false.
A four-valued interpretation (I , J ) is more precise than a four-valued interpretation (I, J), written (I, J) ≤ p (I , J ), if I ⊆ I and J ⊆ J. The precision ordering also has an intuitive reading: the more atoms are certain or the fewer atoms are possible, the more precise is an interpretation. The least precise four-valued interpretation over Σ is (∅, Σ). As with two-valued interpretations, the set of all four-valued interpretations over a signature Σ together with the relation ≤ p forms a complete lattice. A four-valued interpretation is called inconsistent if it contains an inconsistent atom; otherwise, it is called consistent. It is total whenever it makes all atoms either true or false. Finally, (I, J) is called finite whenever both I and J are finite.
Following Truszczyński (2018), we define the id-well-founded operator of an F-program P for any four-valued interpretation (I, J) as This operator is monotone w.r.t. the precision ordering ≤ p . Hence, by Theorem 1(b), W P has a least fixed point, which defines the id-well-founded model of P , also written as WM (P ). In what follows, we drop the prefix "id" and simply refer to the id-well-founded model of a program as its well-founded model. (We keep the distinction between stable and id-stable models.) Any well-founded model (I, J) of an F-program P satisfies I ⊆ J.

Lemma 6
Let P be an F-Program. Then, the well-founded model WM (P ) of P is consistent.

10
R. Kaminski and T. Schaub Example 3 Consider program P 3 consisting of the following rules: We compute the well-founded model of P 3 in four iterations starting from (∅, Σ): The left and right column reflect the certain and possible atoms computed at each iteration, respectively. We reach a fixed point at Step 4. Accordingly, the well-founded model of P 3 is ({a, b, e}, {a, b, e}).
Unlike general F-programs, the class of N -programs warrants the same stable and id-stable models for each of its programs. Unfortunately, N -programs are too restricted for our purpose (for instance, for capturing aggregates in rule bodies 3 ). To this end, we define a more general class of programs and refer to them as R-programs. Although id-stable models of R-programs may differ from their stable models (see below), their well-founded models encompass both stable and id-stable models. Thus, well-founded models can be used for characterizing stable model-preserving program transformations. In fact, we see in Section 2.5 that the restriction of Fto R-programs allows us to provide tighter semantic characterizations of program simplifications.
We define R to be the set of all formulas F such that implications in F have no further occurrences of implications in their antecedents. Then, an R-program consists of rules of form h ← F where h ∈ F 0 and F ∈ R. As with N -programs, a positive R-program is also strictly positive.
Our next result shows that (id-)well-founded models can be used for approximating (regular) stable models of R-programs.

Theorem 7
Let P be an R-program and (I, J) be the well-founded model of P .
If X is a stable model of P , then I ⊆ X ⊆ J.

Example 4
Consider the R-program P 4 : 4 Observe that {a, b, c} is the only stable model of P 4 , the program does not have any id-stable models, and the well-founded model of P 4 is (∅, {a, b, c}). In accordance with Theorem 7, the stable model of P 4 is enclosed in the well-founded model. Note that the id-reduct handles b → a the same way as ¬b ∨ a. In fact, the program obtained by replacing is an N -program and has neither stable nor id-stable models.
Further, note that the program in Example 2 is not an R-program, whereas the one in Example 3 is an R-program.

Program simplification
In this section, we define a concept of program simplification relative to a four-valued interpretation and show how its result can be characterized by the semantic means from above. This concept has two important properties. First, it results in a finite program whenever the interpretation used for simplification is finite. And second, it preserves all (regular) stable models of R-programs when simplified with their well-founded models.

Definition 1
Let P be an F-program, and (I, J) be a four-valued interpretation.
We define the simplification of P w.r.t. (I, J) as For simplicity, we drop parentheses and we write P I,J instead of P (I,J) whenever clear from context. The program simplification P I,J acts as a filter eliminating inapplicable rules that fail to satisfy the condition J |= B(r) I . That is, first, all negatively occurring atoms in B(r) are evaluated w.r.t. the certain atoms in I and replaced accordingly by ⊥ and , respectively. Then, it is checked whether the reduced body B(r) I is satisfiable by the possible atoms in J. Only in this case, the rule is kept in P I,J . No simplifications are applied to the remaining rules. This is illustrated in Example 5 below.
Note that P I,J is finite whenever (I, J) is finite.
Observe that for an F-program P the head atoms in P I,J correspond to the result of applying the provability operator of program P I to the possible atoms in J, that is, H(P I,J ) = T PI (J).
Our next result shows that programs simplified with their well-founded model maintain this model.

Theorem 8
Let P be an F-program and (I, J) be the well-founded model of P .
Then, P and P I,J have the same well-founded model.

12
R. Kaminski and T. Schaub Example 5 In Example 3, we computed the well-founded model ({a, b, e}, {a, b, e}) of P 3 . With this, we obtain the simplified program P 3 = P {a,b,e},{a,b,e} 3 after dropping c ← ¬b and d ← c: Next, we check that the well-founded model of P 3 corresponds to the well-founded model of P 3 : We observe that it takes two applications of the well-founded operator to obtain the well-founded model. This could be reduced to one step if atoms false in the well-founded model would be removed from the negative bodies by the program simplification. Keeping them is a design decision with the goal to simplify notation in the following.
The next series of results further elaborates on semantic invariants guaranteed by our concept of program simplification. The first result shows that it preserves all stable models between the sets used for simplification.

Theorem 9
Let P be an F-program, and I, J, and X be two-valued interpretations.
If I ⊆ X ⊆ J, then X is a stable model of P iff X is a stable model of P I,J .
As a consequence, we obtain that R-programs simplified with their well-founded model also maintain stable models.

Corollary 10
Let P be an R-program and (I, J) be the well-founded model of P .
Then, P and P I,J have the same stable models.
For instance, the R-program in Example 3 and its simplification in Example 5 have the same stable model. Unlike this, the program from Example 2 consisting of rule p ← ¬¬p induces two stable models, while its simplification w.r.t. its well-founded model (∅, ∅) yields an empty program admitting the empty stable model only.
Note that given an R-program with a finite well-founded model, we obtain a semantically equivalent finite program via simplification. As detailed in the following sections, grounding algorithms only compute approximations of the well-founded model. However, as long as the approximation is finite, we still obtain semantically equivalent finite programs. This is made precise by the next two results showing that any program between the original and its simplification relative to its well-founded model preserves the wellfounded model, and that this extends to all stable models for R-programs.

Theorem 11
Let P and Q be F-programs, and (I, J) be the well-founded model of P .
If P I,J ⊆ Q ⊆ P , then P and Q have the same well-founded models.

Corollary 12
Let P and Q be R-programs, and (I, J) be the well-founded model of P . If P I,J ⊆ Q ⊆ P , then P and Q are equivalent.

Splitting
One of the first steps during grounding is to group rules into components suitable for successive instantiation. This amounts to splitting a logic program into a sequence of subprograms. The rules in each such component are then instantiated with respect to the atoms possibly derivable from previous components, starting with some component consisting of facts only. In other words, grounding is always performed relative to a set of atoms that provide a context. Moreover, atoms found to be true or false can be used for on-the-fly simplifications. Accordingly, this section parallels the above presentation by extending the respective formal concepts with contextual information provided by atoms in a two-and four-valued setting. We then assemble the resulting concepts to enable their consecutive application to sequences of subprograms. Interestingly, the resulting notion of splitting allows for more fine-grained splitting than the traditional concept (Lifschitz and Turner 1994) since it allows us to partition rules in an arbitrary way. In view of grounding, we show that once a program is split into a sequence of programs, we can iteratively compute an approximation of the well-founded model by considering in turn each element in the sequence.
In what follows, we append letter "C" to names of interpretations having a contextual nature.
To begin with, we extend the one-step provability operator accordingly.

Definition 2
Let P be an F-program and IC be a two-valued interpretation. For any two-valued interpretation I, we define the one-step provability operator of P relative to IC as P is a also a prefixed point of T P . Thus, each prefixed point of T IC P is a model of P but not vice versa. To see this, consider program P = {a ← b}. We have T P (∅) = ∅ and T

Proposition 13
Let P be a positive program, and IC and J be two valued interpretations.
Then, the operators T IC P and T · P (J) are both monotone. We use Theorems 1 and 13 to define a contextual stable operator. 14 R. Kaminski and T. Schaub Definition 3 Let P be an F-program and IC be a two-valued interpretation.
For any two-valued interpretation J, we define the stable operator relative to IC , written S IC P (J), as the least fixed point of T IC PJ . While the operator is antimonotone w.r.t. its argument J, it is monotone regarding its parameter IC .

Proposition 14
Let P be an F-program, and IC and J be two-valued interpretations.
Then, the operators S IC P and S · P (J) are antimonotone and monotone, respectively. By building on the relative stable operator, we next define its well-founded counterpart. Unlike above, the context is now captured by a four-valued interpretation.

Definition 4
Let P be an F-program and (IC , JC ) be a four-valued interpretation.
For any four-valued interpretation (I, J), we define the well-founded operator relative to (IC , JC ) as . Also, we keep refraining from prepending the prefix "id" to the well-founded operator along with all concepts derived from it below.
Unlike the stable operator, the relative well-founded one is monotone on both its argument and parameter.

Proposition 15
Let P be an F-program, and (I, J) and (IC , JC ) be four-valued interpretations.
Then, the operators W IC ,JC P and W · P (I, J) are both monotone w.r.t. the precision ordering.
From Theorems 1 and 15, we get that the relative well-founded operator has a least fixed point.

Definition 5
Let P be an F-program and (IC , JC ) be a four-valued interpretation.
We define the well-founded model of P relative to ( In what follows, we use the relativized concepts defined above to delineate the semantics and resulting simplifications of the sequence of subprograms resulting from a grounder's decomposition of the original program. For simplicity, we first present a theorem capturing the composition under the well-founded operation, before we give the general case involving a sequence of programs. Just like suffix C, we use the suffix E (and similarly letter E further below) to indicate atoms whose defining rules are yet to come.
As in traditional splitting, we begin by differentiating a bottom and a top program. In addition to the input atoms (I, J) and context atoms in (IC , JC ), we moreover distinguish a set of external atoms, (IE , JE ), which occur in the bottom program but are defined in the top program. Accordingly, the bottom program has to be evaluated relative to (IC , JC ) (IE , JE ) (and not just (IC , JC ) as above) to consider what could be derived by the top program. Also, observe that our notion of splitting aims at computing wellfounded models rather than stable models. Partially expanding the statements of the two previous result nicely reflects the decomposition of the application of the well-founded founded model of a program: Note that the formulation of the theorem forms the external interpretation (IE , JE ), by selecting atoms from the overarching well-founded model (I, J). This warrants the correspondence of the overall interpretations to the union of the bottom and top wellfounded model. This global approach is dropped below (after the next example) and leads to less precise composed models.

Example 6
Let us illustrate the above approach via the following program: The well-founded model of this program relative to (IC , JC ) = (∅, ∅) is (I, J) = ({a, b, c}, {a, b, c}).
First, we partition the four rules of the program into PB and PT as given above. We We see that the union of (IB , JB ) (IT , JT ) is the same as the well-founded model of PB ∪ PT relative to (IC , JC ). This corresponds to standard splitting in the sense that {a, b} is a splitting set for PB ∪ PT and PB is the "bottom" and PT is the "top" (Lifschitz and Turner 1994).

16
R. Kaminski and T. Schaub Example 7 For a complement, let us reverse the roles of programs PB and PT in Example 6. Unlike above, body atoms in PB now occur in rule heads of PT , that is, B ( Again, we see that the union of both models is identical to (I, J). This decomposition has no direct correspondence to standard splitting (Lifschitz and Turner 1994) since there is no splitting set.
Next, we generalize the previous results from two programs to sequences of programs. For this, we let I be a well-ordered index set and direct our attention to sequences (P i ) i∈I of F-programs.

Definition 6
Let (P i ) i∈I be a sequence of F-programs.
We define the well-founded model of (P i ) i∈I as where The well-founded model of a program sequence is itself assembled in (2) from a sequence of well-founded models of the individual subprograms in (5). This provides us with semantic guidance for successive program simplification, as shown below. In fact, proceeding along the sequence of subprograms reflects the iterative approach of a grounding algorithm, one component is grounded at a time. At each stage i ∈ I, this takes into account the truth values of atoms instantiated in previous iterations, viz. (IC i , JC i ), as well as dependencies to upcoming components in E i . Note that unlike Proposition 16, the external atoms in E i are identified purely syntactically, and the interpretation (∅, E i ) treats them as unknown. Grounding is thus performed under incomplete information and each well-founded model in (5) can be regarded as an over-approximation of the actual one. This is enabled by the monotonicity of the well-founded operator in Proposition 15 that only leads to a less precise result when overestimating its parameter.
Accordingly, the next theorem shows that once we split a program into a sequence of F-programs, we can iteratively compute an approximation of the well-founded model by considering in turn each element in the sequence.

Theorem 17
Let (P i ) i∈I be a sequence of F-programs.
The next two results transfer Theorem 17 to program simplification by successively simplifying programs with the respective well-founded models of the previous programs.

Corollary 19
Let (P i ) i∈I be a sequence of R-programs, and E i , (IC i , JC i ), and (I i , J i ) be defined as in Equation (3)-Equation (5). Then, i∈I P i and i∈I P have the same well-founded and stable models.
Let us mention that the previous result extends to sequences of F-programs and their well-founded models but not their stable models.

Example 8
To illustrate Theorem 17, let us consider the following programs, P 1 and P 2 : The well-founded model of P 1 ∪ P 2 is Let us evaluate P 1 before P 2 . While no head literals of P 2 occur positively in P 1 , the head literals c and d of P 2 occur negatively in rule bodies of P 1 . Hence, we get E 1 = {c, d} and treat both atoms as unknown while calculating the well-founded model of P 1 relative to (∅, {c, d}): We obtain that both a and b are unknown. With this and E 2 = ∅, we can calculate the well-founded model of P 2 relative to (I 1 , J 1 ): We see that because a is unknown, we have to derive c as unknown, too. And because there is no rule defining e, we cannot derive d. Hence, (I 1 , J 1 ) (I 2 , J 2 ) is less precise than (I, J) because, when evaluating P 1 , it is not yet known that c is true and d is false.
Next, we illustrate the simplified programs according to Theorem 18:

R. Kaminski and T. Schaub
The left column contains the simplification of P 1 ∪ P 2 w.r.t. (I, J) and the right column the simplification of P 1 w.r.t. (I 1 , J 1 ) and P 2 w.r.t. (I 1 , J 1 ) (I 2 , J 2 ). Note that d ← e has been removed in both columns because e is false in both (I, J) and (I 1 , J 1 ) (I 2 , J 2 ). But we can only remove c ← ¬b from the left column because, while b is false in (I, J), it is unknown in (I 1 , J 1 ) (I 2 , J 2 ). Finally, observe that in accordance with Theorem 9 and Corollaries 10 and cor:sequence:simplification:stable, the program P 1 ∪ P 2 and the two simplified programs have the same stable and well-founded models.
Clearly, the best simplifications are obtained when simplifying with the actual wellfounded model of the overall program. This can be achieved for a sequence as well whenever E i is empty, that is, if there is no need to approximate the impact of upcoming atoms.

Corollary 20
Let (P i ) i∈I be a sequence of F-programs and E i be defined as in (3).

Example 9
Next, let us illustrate Corollary 20 on an example. We take the same rules as in Example 8 but use a different sequence: Observe that the head literals of P 2 do not occur in the bodies of P 1 , that is, And the well-founded model of P 2 relative to ({b}, {b}) is Hence, the union of both models is identical to the well-founded model of P 1 ∪ P 2 . Next, we investigate the simplified program according to Corollary 21: As in Example 8, we delete rule d ← e because e is false in (I 1 , J 1 ). But this time, we can also remove rule c ← ¬b because b is true in (I 1 , J 1 ) (I 2 , J 2 ).

Aggregate programs
We now turn to programs with aggregates and, at the same time, to programs with variables. That is, we now deal with finite nonground programs whose instantiation may lead to infinite ground programs including infinitary subformulas. This is made precise by Harrison et al . (2014) and Gebser et al . (2015a) where aggregate programs are associated with infinitary propositional formulas (Truszczyński 2012). However, the primary goal of grounding is to produce a finite set of ground rules with finitary subformulas only. In fact, the program simplification introduced in Section 2.5 allows us to produce an equivalent finite ground program whenever the well-founded model is finite. The source of infinitary subformulas lies in the instantiation of aggregates. We address this below by introducing an aggregate translation bound by an interpretation that produces finitary formulas whenever this interpretation is finite. Together, our concepts of program simplification and aggregate translation provide the backbone for turning programs with aggregates into semantically equivalent finite programs with finitary subformulas. Our concepts follow the ones of Gebser et al . (2015a); the semantics of aggregates is aligned with that of Ferraris (2011) yet lifted to infinitary formulas (Truszczyński 2012;Harrison et al . 2014).
We consider a signature Σ = (F, P, V) consisting of sets of function, predicate, and variable symbols. The sets of variable and function symbols are disjoint. Function and predicate symbols are associated with non-negative arities. For short, a predicate symbol p of arity n is also written as p/n. In the following, we use lower case strings for function and predicate symbols, and upper case strings for variable symbols. Also, we often drop the term 'symbol' and simply speak of functions, predicates, and variables.
As usual, terms over Σ are defined inductively as follows: • v ∈ V is a term and • f (t 1 , . . . , t n ) is a term if f ∈ F is a function symbol of arity n and each t i is a term over Σ.
Parentheses for terms over function symbols of arity 0 are omitted. Unless stated otherwise, we assume that the set of (zero-ary) functions includes a set of numeral symbols being in a one-to-one correspondence to the integers. For simplicity, we drop this distinction and identify numerals with the respective integers.
An atom over signature Σ has form p(t 1 , . . . , t n ) where p ∈ P is a predicate symbol of arity n and each t i is a term over Σ. As above, parentheses for atoms over predicate symbols of arity 0 are omitted. Given an atom a over Σ, a literal over Σ is either the atom itself or its negation ¬a. A literal without negation is called positive, and negative otherwise.
A comparison over Σ has form where t 1 and t 2 are terms over Σ and ≺ is a relation symbol among <, ≤, >, ≥, =, and =.

R. Kaminski and T. Schaub
where t i is a term and a j is an atom, both over Σ for 0 ≤ i ≤ m and 0 ≤ j ≤ n. The terms t 1 , . . . , t m are seen as a tuple, which is empty for m = 0; the conjunction a 1 ∧ · · · ∧ a n is called the condition of the aggregate element. For an aggregate element e of form (7), we use H(e) = (t 1 , . . . , t m ) and B(e) = {a 1 , . . . , a n }. We extend both to sets of aggregate elements in the straightforward way, that is, An aggregate atom over Σ has form where n ≥ 0, f is an aggregate name among #count, #sum, #sum + , and #sum − , each e i is an aggregate element, ≺ is a relation symbol among <, ≤, >, ≥, =, and = (as above), and s is a term representing the aggregate's bound.
Without loss of generality, we refrain from introducing negated aggregate atoms. 5 We often refer to aggregate atoms simply as aggregates.
An aggregate program over Σ is a finite set of aggregate rules of form where n ≥ 0, h is an atom over Σ and each b i is either a literal, a comparison, or an aggregate over Σ. We refer to b 1 , . . . , b n as body literals, and extend functions H(r) and B(r) to any aggregate rule r.

Example 10
An example for an aggregate program is shown below, giving an encoding of the Company Controls Problem (Mumick et al . 1990): A company X controls a company Y if X directly or indirectly controls more than 50% of the shares of Y .
The aggregate #sum + implements summation over positive integers. Notably, it takes part in the recursive definition of predicate controls. In the following, we use an instance with ownership relations between four companies: We say that an aggregate rule r is normal if its body does not contain aggregates. An aggregate program is normal if all its rules are normal.
A term, literal, aggregate element, aggregate, rule, or program is ground whenever it does not contain any variables.
We assume that all ground terms are totally ordered by a relation ≤, which is used to define the relations <, >, ≥, =, and = in the standard way. For ground terms t 1 , t 2 and a corresponding relation symbol ≺, we say that ≺ holds between t 1 and t 2 whenever the corresponding relation holds between t 1 and t 2 . Furthermore, >, ≥, and = hold between ∞ and any other term, and <, ≤, and = hold between −∞ and any other term. Finally, we require that integers are ordered as usual.
For defining sum-based aggregates, we define for a tuple t = t 1 , . . . , t m of ground terms the following weight functions: With this at hand, we now define how to apply aggregate functions to sets of tuples of ground terms in analogy to Gebser et al . (2015a).

Definition 7
Let T be a set of tuples of ground terms.
We define Note that in our setting the application of aggregate functions to infinite sets of ground terms is of theoretical relevance only, since we aim at reducing them to their finite equivalents so that they can be evaluated by a grounder.
• an aggregate if it occurs in its bound, and • a rule if it is global in its head atom or in one of its body literals.
For example, the variables X and Y are global in the aggregate rule in Example 10, while Z and S are neither global in the rule nor the aggregate.

Definition 8
Let r be an aggregate rule.
We define r to be safe • if all its global variables occur in some positive literal in the body of r and • if all its non-global variables occurring in an aggregate element e of an aggregate in the body of r, also occur in some positive literal in the condition of e.

R. Kaminski and T. Schaub
For instance, the aggregate rule in Example 10 is safe. Note that comparisons are disregarded in the definition of safety. That is, variables in comparisons have to occur in positive body literals. 6 An aggregate program is safe if all its rules are safe. An instance of an aggregate rule r is obtained by substituting ground terms for all its global variables. We use Inst(r) to denote the set of all instances of r and Inst(P ) to denote the set of all ground instances of rules in aggregate program P . An instance of an aggregate element e is obtained by substituting ground terms for all its variables. We let Inst(E) stand for all instances of aggregate elements in a set E. Note that Inst(E) consists of ground expressions, which is not necessarily the case for Inst(r). As seen from the first example in the introductory section, both Inst(r) and Inst(E) can be infinite.
A literal, aggregate element, aggregate, or rule is closed if it does not contain any global variables.
For example, the following rule is an instance of the aggregate rule in Example 10.
Note that both the rule and its aggregate are closed. It is also noteworthy to realize that the two elements of the aggregate induce an infinite set of instances, among them 20 : owns(c 1 , c 2 , 20) and We now turn to the semantics of aggregates as introduced by Ferraris (2011) but follow its adaptation to closed aggregates by Gebser et al . (2015a): Let a be a closed aggregate of form (8) and E be its set of aggregate elements. We say that a set D ⊆ Inst(E) of its elements' instances justifies a, written D a, if f (H(D)) ≺ s holds.
An aggregate a is monotone whenever D 1 a implies D 2 a for all D 1 ⊆ D 2 ⊆ Inst(E), and accordingly a is antimonotone if D 2 a implies D 1 a for all D 1 ⊆ D 2 ⊆ Inst(E).
We observe the following monotonicity properties.
Proposition 22 (Harrison et al. 2014) • Aggregates over functions #sum + and #count together with relations > and ≥ are monotone. • Aggregates over functions #sum + and #count together with relations < and ≤ are antimonotone. • Aggregates over function #sum − have the same monotonicity properties as #sum + aggregates with the complementary relation.
Next, we give the translation τ from aggregate programs to R-programs, derived from the ones of Ferraris (2011) and Harrison et al . (2014): For a closed literal l, we have for a closed comparison l of form (6), we have τ (l) = if ≺ holds between t 1 and t 2 ⊥ otherwise and for a set L of closed literals, comparisons and aggregates, we have For a closed aggregate a of form (8) and its set E of aggregate elements, we have where For a closed aggregate rule r, we have For an aggregate program P , we have While aggregate programs like P are finite sets of (non-ground) rules, τ (P ) can be infinite and contain (ground) infinitary expressions. Observe that τ (P ) is an R-program. In fact, only the translation of aggregates introduces R-formulas; rules without aggregates form N -programs.

Example 11
To illustrate Ferraris' approach to the semantics of aggregates, consider a count aggregate a of form #count{X : p(X)} ≥ n.
Since the aggregate is non-ground, the set G of its element's instances consists of all t : p(t) for each ground term t.
The count aggregate cannot be justified by any subset D of G satisfying |{t | t : p(t) ∈ D}| < n, or D a for short. Accordingly, we have that τ (a) is the conjunction of all formulas such that D ⊆ G and D a. Restricting the set of ground terms to the numerals 1, 2, 3 and letting n = 2 results in the formulas

24
R. Kaminski and T. Schaub Note that a smaller number of ground terms than n yields an unsatisfiable set of formulas.
However, it turns out that a Ferraris-style translation of aggregates (Ferraris 2011;Harrison et al . 2014) is too weak for propagating monotone aggregates in our id-based setting. That is, when propagating possible atoms (i.e., the second component of the well-founded model), an id-reduct may become satisfiable although the original formula is not. So, we might end up with too many possible atoms and a well-founded model that is not as precise as it could be. To see this, consider the following example.

Example 12
For some m, n ≥ 0, the program P m,n consists of the following rules: Given the ground instances G of the aggregate's elements and some two-valued interpretation I, observe that To see this, observe that the formula obtained via τ for the aggregate in the last rule's body consists of positive occurrences of implications of the form G ∧ → H ∨ where either p(t) ∈ G or p(t) ∈ H. The id-reduct makes all such implications with some p(t) ∈ G such that p(t) / ∈ I true because their antecedent is false. All of the remaining implications in the id-reduct are equivalent to H ∨ where H contains all p(t) / ∈ I. Thus, we can factor out the formula on the right-hand side of (12).
Next, observe that for 1 ≤ m < n, the four-valued interpretation (I, J) = (∅, H(τ (P m,n ))) is the well-founded model of P m,n : Ideally, atom r should not be among the possible atoms because it can never be in a stable model. Nonetheless, it is due to the second disjunct in (12).
Note that not just monotone aggregates exhibit this problem. In general, we get for a closed aggregate a with elements E and an interpretation I that The second disjunct is undesirable when propagating possible atoms.
To address this shortcoming, we augment the aggregate translation so that it provides stronger propagation. The result of the augmented translation is strongly equivalent to that of the original translation (cf. Proposition 23). Thus, even though we get more precise well-founded models, the stable models are still contained in them.

Definition 9
We define π as the translation obtained from τ by replacing the case of closed aggregates in (9) by the following: For a closed aggregate a of form (8) and its set E of aggregate elements, we have Note that just as τ also π is recursively applied to the whole program. Let us illustrate the modified translation by revisiting Example 11.

Example 13
Let us reconsider the count aggregate a: As with τ (a) in Example 11, π(a) yields a conjunction of formulas, one conjunct for each set D ⊆ Inst(E) satisfying D a of the form: Restricting again the set of ground terms to the numerals 1, 2, 3 and letting n = 2 results now in the formulas (2)).
Note that the last disjunct can be dropped from each rule's consequent. And as above, a smaller number of ground terms than n yields an unsatisfiable set of formulas.
The next result ensures that τ (P ) and π(P ) have the same stable models for any aggregate program P .

Proposition 23
Let a be a closed aggregate.
The next example illustrates that we get more precise well-founded models using the strongly equivalent refined translation.

Example 14
Reconsider Program P m,n from Example 12.

R. Kaminski and T. Schaub
As above, we apply the well-founded operator to program P m,n for m < n and fourvalued interpretation (I, J) = (∅, H(π(P m,n ))): Unlike before, r is now found to be false since it does not belong to S π(Pm,n) (∅).
To see this, we can take advantage of the following proposition.

Proposition 24
Let a be a closed aggregate.
If a is monotone, then π(a) I is classically equivalent to π(a) for any two-valued interpretation I.
Note that π(a) is a negative formula whenever a is antimonotone; cf. Proposition 36. Let us briefly return to Example 14. We now observe that π(a) I = π(a) for a = #count{X : p(X)} ≥ n and any interpretation I in view of the last proposition. Hence, our refined translation π avoids the problematic disjunct in Equation (12) on the right. By Proposition 23, we can use π(P m,n ) instead of τ (P m,n ); both formulas have the same stable models.
Using Proposition 24, we augment the translation π to replace monotone aggregates a by the strictly positive formula π(a) ∅ . That is, we only keep the implication with the trivially true antecedent in the aggregate translation (cf. Section 5).
While π improves on propagation, it may still produce infinitary R-formulas when applied to aggregates. This issue is addressed by restricting the translation to a set of (possible) atoms.

Definition 10
Let J be a two-valued interpretation. We define the translation π J as the one obtained from τ by replacing the case of closed aggregates in (9) by the following: For a closed aggregate a of form (8) and its set E of aggregate elements, we have Note that π J (a) is a finitary formula whenever J is finite. Clearly, π J also conforms to π except for the restricted translation for aggregates defined above. The next proposition elaborates this by showing that π J and π behave alike whenever J limits the set of possible atoms.

Theorem 25
Let a be a closed aggregate, and I ⊆ J and X ⊆ J be two-valued interpretations.
In view of Proposition 23, this result extends to Ferraris' original aggregate translation (Ferraris 2011;Harrison et al . 2014).
The next example illustrates how a finitary formula can be obtained for an aggregate, despite a possibly infinite set of terms in the signature.

Example 15
Let P m,n be the program from Example 12. The well-founded model (I, J) of π(P m,n ) is (∅, H(π(P m,n The translation π J (P 3,2 ) consists of the rules (1), (3), and where the aggregate translation corresponds to the conjunction of the formulas in Example 13. Note that the translation π(P 3,2 ) depends on the signature whereas the translation π J (P 3,2 ) is fixed by the atoms in J.
Importantly, Proposition 25 shows that given a finite (approximation of the) wellfounded model of an R-program, we can replace aggregates with finitary formulas. Moreover, in this case, Theorem 9 and Proposition 23 together indicate how to turn a program with aggregates into a semantically equivalent finite R-program with finitary formulas as bodies. That is, given a finite well-founded model of an R-program, the program simplification from Definition 1 results in a finite program and the aggregate translation from Definition 10 produces finitary formulas only.
This puts us in a position to outline how and when (safe non-ground) aggregate programs can be turned into equivalent finite ground programs consisting of finitary subformulas only. To this end, consider an aggregate program P along with the well-founded model (I, J) of π(P ). We have already seen in Corollary 10 that π(P ) and its simplification π(P ) I,J have the same stable models, just like π(P ) I,J and its counterpart π J (P ) I,J in view of Proposition 25. Now, if (I, J) is finite, then π(P ) I,J is finite, too. Seen from the perspective of grounding, the safety of all rules in P implies that all global variables appear in positive body literals. Thus, the number of ground instances of each rule in π(P ) I,J is determined by the number of possible substitutions for its global variables. Clearly, there are only finitely many possible substitutions such that all positive body literals are satisfied by a finite interpretation J (cf. Definition 1). Furthermore, if J is finite, aggregate translations in π J (P ) I,J introduce finitary subformulas only. Thus, in this case, we obtain from P a finite set of rules with finitary propositional formulas as bodies, viz. π J (P ) I,J , that has the same stable models as π(P ) (as well as τ (P ), the traditional Ferraris-style semantics of P (Ferraris 2011;Harrison et al . 2014)).
An example of a class of aggregate programs inducing finite well-founded models as above consists of programs over a signature with nullary function symbols only. Any such program can be turned into an equivalent finite set of propositional rules with finitary bodies.

Dependency analysis
We now further refine our semantic approach to reflect actual grounding processes. In fact, modern grounders process programs on-the-fly by grounding one rule after another without storing any rules. At the same time, they try to determine certain, possible, and false atoms. Unfortunately, well-founded models cannot be computed on-the-fly, which is why we introduce below the concept of an approximate model. More precisely, we start by defining instantiation sequences of (non-ground) aggregate programs based on their rule dependencies. We show that approximate models of instantiation sequences are underapproximations of the well-founded model of the corresponding sequence of (ground) R-programs, as defined in Section 3. The precision of both types of models coincides on stratified programs. We illustrate our concepts comprehensively at the end of this section in Examples 19 and 20.
To begin with, we extend the notion of positive and negative literals to aggregate programs. For atoms a, we define a + = (¬a) − = {a} and a − = (¬a) + = ∅. For comparisons a, we define a + = a − = ∅. For aggregates a with elements E, we define positive and negative atom occurrences, using Proposition 24 to refine the case for monotone aggregates: For a set of body literals B, we define B + = b∈B b + and B − = b∈B b − , as well as We see in the following, that a special treatment of monotone aggregates yields better approximations of well-founded models. A similar case could be made for antimonotone aggregates but had led to a more involved algorithmic treatment.
Inter-rule dependencies are determined via the predicates appearing in their heads and bodies. We define pred(a) to be the predicate symbol associated with atom a and pred(A) = {pred(a) | a ∈ A} for a set A of atoms. An aggregate rule r 1 depends on another aggregate rule r 2 if pred(H(r 2 )) ∈ pred(B(r 1 ) ± ). Rule r 1 depends positively or negatively on r 2 if pred(H(r 2 )) ∈ pred(B(r 1 ) + ) or pred(H(r 2 )) ∈ pred(B(r 2 ) − ), respectively.
For simplicity, we first focus on programs without aggregates in examples and delay a full example with aggregates until the end of the section.

Example 16
Let us consider the following rules from the introductory example: We first determine the rule heads and positive and negative atom occurrences in rule bodies: https://doi.org/10.1017/S1471068422000308 Published online by Cambridge University Press

29
With this, we infer the corresponding predicates: We see that r 2 depends positively on r 1 and that r 2 and r 3 depend negatively on each other. View Figure 1 in Example 17 for a graphical representation of these inter-rule dependencies.
The strongly connected components of an aggregate program P are the equivalence classes under the transitive closure of the dependency relation between all rules in P . A strongly connected component P 1 depends on another strongly connected component P 2 if there is a rule in P 1 that depends on some rule in P 2 . The transitive closure of this relation is antisymmetric.
A strongly connected component of an aggregate program is unstratified if it depends negatively on itself or if it depends on an unstratified component. A component is stratified if it is not unstratified.
A topological ordering of the strongly connected components is then used to guide grounding.
For example, the sets {r 1 } and {r 2 , r 3 } of rules from Example 16 are strongly connected components in a topological order. There is only one topological order because r 2 depends on r 1 . While the first component is stratified, the second component is unstratified because r 2 and r 3 depend negatively on each other.

Definition 11
We define an instantiation sequence for P as a sequence (P i ) i∈I of its strongly connected components such that i < j if P j depends on P i .
Note that the components can always be well ordered because aggregate programs consist of finitely many rules.
The consecutive construction of the well-founded model along an instantiation sequence results in the well-founded model of the entire program.

Theorem 26
Let (P i ) i∈I be an instantiation sequence for aggregate program P .

Example 17
The following example shows how to split an aggregate program into an instantiation sequence and gives its well-founded model. Let P be the following aggregate program, extending the one from the introductory section: (3). We have already seen how to determine inter-rule dependencies in Example 16. A possible instantiation sequence for program P is given in Figure 1. Rules are depicted in solid boxes. Solid and dotted edges between such boxes depict positive and negative dependencies between the corresponding rules, respectively. Dashed and dashed/dotted boxes represent components in the instantiation sequence (we ignore dotted boxes for now but turn to them in Example 18). The number in the corner of a component box indicates the index in the corresponding instantiation sequence. By Theorem 26, the ground sequence (τ (P i )) i∈I has the same well-founded model as π(P ):

R. Kaminski and T. Schaub
Note that the set F comprises the facts derived from stratified components. In fact, for stratified components, the set of external atoms (3) is empty. We can use Corollary 20 to confirm that the well founded model (F, F ) of sequence (π(P i )) 1≤i≤4 is total. In fact, each of the intermediate interpretations (5) is total and can be computed with just one application of the stable operator. For example, I 1 = J 1 = {u(1)} for component P 1 .
We further refine instantiation sequences by partitioning each component along its positive dependencies.

Definition 12
Let P be an aggregate program and (P i ) i∈I be an instantiation sequence for P . Furthermore, for each i ∈ I, let (P i,j ) j∈Ii be an instantiation sequence of P i considering positive dependencies only.
A refined instantiation sequence for P is a sequence (P i,j ) (i,j)∈J where the index set J = {(i, j) | i ∈ I, j ∈ I i } is ordered lexicographically.
We call (P i,j ) (i,j)∈J a refinement of (P i ) i∈I .
We define a component P i,j to be stratified or unstratified if the encompassing component P i is stratified or unstratified, respectively.
Examples of refined instantiation sequences are given in Figures 1 and 2. The advantage of such refinements is that they yield better or equal approximations (cf. Theorem 28 and Example 19). On the downside, we do not obtain that WM ((P i ) i∈J ) equals WM (π(P )) for refined instantiation sequences in general.

Example 18
The refined instantiation sequence for program P from Example 17 is given in Figure 1. A dotted box indicates a component in a refined instantiation sequence. Components that cannot be refined further are depicted with a dashed/dotted box. The number or pair in the corner of a component box indicates the index in the corresponding refined instantiation sequence.
We have already seen in Section 3 that external atoms may lead to less precise semantic characterizations. This is just the same in the non-ground case, whenever a component comprises predicates that are defined in a following component of a refined instantiation sequence. This leads us to the concept of an approximate model obtained by overapproximating the extension of such externally defined predicates.

Definition 13
Let P be an aggregate program, (IC , JC ) be a four-valued interpretation, E be a set of predicates, and P be the program obtained from P by removing all rules r with pred(B(r) − ) ∩ E = ∅.
We define the approximate model of P relative to (IC , JC ) as where I = S IC π(P ) (JC ) and J = S JC π(P ) (IC ∪ I).
We keep dropping parentheses and simply write AM IC ,JC E (P ) instead of AM (IC ,JC ) E (P ). The approximate model amounts to an immediate consequence operator, similar to the relative well-founded operator in Definition 4; it refrains from any iterative applications, 32 R. Kaminski and T. Schaub as used for defining a well-founded model. More precisely, the relative stable operator is applied twice to obtain the approximate model. This is similar to Van Gelder's alternating transformation (Van Gelder 1993). The certain atoms in I are determined by applying the operator to the ground program obtained after removing all rules whose negative body literals comprise externally defined predicates, while the possible atoms J are computed from the entire program by taking the already computed certain atoms in I into account. In this way, the approximate model may result in fewer unknown atoms than the relative well-founded operator when applied to the least precise interpretation (as an easy point of reference). How well we can approximate the certain atoms with the approximate operator depends on the set of external predicates E. When approximating the model of a program P in a sequence, the set E comprises all negative predicates occurring in P for which possible atoms have not yet been fully computed. This leads to fewer certain atoms obtained from the reduced program, P = {r ∈ P | pred(B(r) − )∩E = ∅}, stripped of all rules from P that have negative body literals whose predicates occur in E.
The next theorem identifies an essential prerequisite for an approximate model of a non-ground program to be an underapproximation of the well-founded model of the corresponding ground program.

Theorem 27
Let P be an aggregate program, E be a set of predicates, and (IC , JC ) be a four-valued interpretation.
If pred(H(P )) ∩ pred(B(P ) − ) ⊆ E then AM IC ,JC E (P ) ≤ p WM IC ,JC ∪EC (π(P )) where EC is the set of all ground atoms over predicates in E.
In general, a grounder cannot calculate on-the-fly a well-founded model. Implementing this task efficiently requires an algorithm storing the grounded program, as, for example, implemented in an ASP solver. But modern grounders are able to calculate the stable operator on-the-fly. Thus, an approximation of the well-founded model is calculated. This is where we use the approximate model, which might be less precise than the well-founded model but can be computed more easily.
With the condition of Theorem 27 in mind, we define the approximate model for an instantiation sequence. We proceed similar to Definition 6 but treat in (14) all atoms over negative predicates that have not been completely defined as external.

Definition 14
Let (P i ) i∈I be a (refined) instantiation sequence for P .
Then, the approximate model of (P i ) i∈I is Note that the underlying approximate model removes rules containing negative literals over predicates in E i when calculating certain atoms. This amounts to assuming all ground instances of atoms over E i to be possible. 7 Compared to (3), however, this additionally includes recursive predicates in (14). The set E i is empty for stratified components.
The next result relies on Theorem 17 to show that an approximate model of an instantiation sequence constitutes an underapproximation of the well-founded model of the translated entire program. In other words, the approximate model of a sequence of aggregate programs (as computed by a grounder) is less precise than the well-founded model of the whole ground program.

Theorem 28
Let (P i ) i∈I be an instantiation sequence for aggregate program P and (P j ) j∈J be a refinement of (P i ) i∈I .
The finer granularity of refined instantiation sequences leads to more precise models. Intuitively, this is because a refinement of a component may result in a series of approximate models, which yield a more precise result than the approximate model of the entire component because in some cases fewer predicates are considered external in (14).
We remark that all instantiation sequences of a program have the same approximate model. However, this does not carry over to refined instantiation sequences because their evaluation is order dependent.
The two former issues are illustrated in Example 19. The actual value of approximate models for grounding lies in their underlying series of consecutive interpretations delineating each ground program in a (refined) instantiation sequence. In fact, as outlined after Proposition 25, whenever all interpretations (I i , J i ) in (16) are finite so are the R-programs π JC i∪Ji (P i ) (IC i ,JC i) (Ii,Ji) obtained from each P i in the instantiation sequence.

Theorem 29
Let (P i ) i∈I be a (refined) instantiation sequence of an aggregate program P , and let (IC i , JC i ) and (I i , J i ) be defined as in (15) and (16).
Then, i∈I π JC i∪Ji (P i ) (IC i ,JC i) (Ii,Ji) and π(P ) have the same well-founded and stable models.
Notably, this union of R-programs is exactly the one obtained by the grounding algorithm proposed in the next section (cf. Theorem 34).

Example 19
We continue Example 18.
The approximate model of the instantiation sequence (P i ) i∈I , defined in Definition 14, is less precise than the well-founded model of the sequence, viz.

R. Kaminski and T. Schaub
This is because we have to use AM F,F E (P 5 ) to approximate the well-founded model of component P 5 . Here, the set E = {a/1, b/1} determined by Equation (14) forces us to unconditionally assume instances of ¬q(X) and ¬p(X) to be true. Thus, we get (I 5 , J 5 ) = (∅, {p(1), p(2), q(2), q(3)}) for the intermediate interpretation in (16). This is also reflected in Definition 13, which makes us drop all rules containing negative literals over predicates in E when calculating true atoms.
In accord with Theorem 28, we approximate the well-founded model w.r.t. the refined instantiation sequence (P i,j ) (i,j)∈J and obtain (1), p(2), q(2), q(3), x}) F, which, for this example, is equivalent to the well-founded model of the corresponding ground refined instantiation sequence and more precise than the approximate model of the instantiation sequence.
In an actual grounder implementation the approximate model is only a byproduct, instead, it outputs a program equivalent to the one in Theorem 29: x ← ¬p(1).

Remark 1
The reason why we use the refined grounding is that we cannot expect a grounding algorithm to calculate the well-founded model for a component without further processing. But at least some consequences should be considered. Gringo is designed to ground on-the-fly without storing any rules, so it cannot be expected to compute all possible consequences but it should at least take all consequences from preceding interpretations into account. With the help of a solver, we could calculate the exact well-founded model of a component after it has been grounded.
Whenever an aggregate program is stratified, the approximate model of its instantiation sequence is total (and coincides with the well-founded model of the entire ground program).

Theorem 30
Let (P i ) i∈I be an instantiation sequence of an aggregate program P such that E i = ∅ for each i ∈ I as defined in (14).
Then, AM ((P i ) i∈I ) is total.

Example 20
The dependency graph of the company controls encoding is given in Figure 2 and follows the conventions in Example 19. Because the encoding only uses positive literals and monotone aggregates, grounding sequences cannot be refined further. Since the program is positive, we can apply Theorem 30. Thus, the approximate model of the grounding

Algorithms
This section lays out the basic algorithms for grounding rules, components, and entire programs and characterizes their output in terms of the semantic concepts developed in the previous sections. Of particular interest is the treatment of aggregates, which are decomposed into dedicated normal rules before grounding and reassembled afterward. This allows us to ground rules with aggregates by means of grounding algorithms for normal rules. Finally, we show that our grounding algorithm guarantees that an obtained finite ground program is equivalent to the original non-ground program.
In the following, we refer to terms, atoms, comparisons, literals, aggregate elements, aggregates, or rules as expressions. As in the preceding sections, all expressions, interpretations, and concepts introduced below operate on the same (implicit) signature Σ unless mentioned otherwise.
A substitution is a mapping from the variables in Σ to terms over Σ. We use ι to denote the identity substitution mapping each variable to itself. A ground substitution maps all variables to ground terms or themselves. The result of applying a substitution σ to an expression e, written eσ, is the expression obtained by replacing each variable v in e by σ(v). This directly extends to sets E of expressions, that is, The composition of substitutions σ and θ is the substitution A substitution σ is a unifier of a set E of expressions if e 1 σ = e 2 σ for all e 1 , e 2 ∈ E. In what follows, we are interested in one-sided unification, also called matching. A Algorithm 1: Grounding Rules substitution σ matches a non-ground expression e to a ground expression g, if eσ = g and σ maps all variables not occurring in e to themselves. We call such a substitution the matcher of e to g. Note that a matcher is a unique ground substitution unifying e and g, if it exists. This motivates the following definition. For a (non-ground) expression e and a ground expression g, we define: When grounding rules, we look for matches of non-ground body literals in the possibly derivable atoms accumulated so far. The latter is captured by a four-valued interpretation to distinguish certain atoms among the possible ones. This is made precise in the next definition.

Definition 15
Let σ be a substitution, l be a literal or comparison, and (I, J) be a four-valued interpretation.
We define the set of matches for l in (I, J) w.r.t. σ, written Matches I,J l (σ), for an atom l = a as for a ground literal l = ¬a as for a ground comparison l = t 1 ≺ t 2 as in (6) as Matches I,J t1≺t2 (σ) = {σ | ≺ holds between t 1 σ and t 2 σ}. In this way, positive body literals yield a (possibly empty) set of substitutions, refining the one at hand, while negative and comparison literals are only considered when ground and then act as a test on the given substitution.
Our function for rule instantiation is given in Algorithm 1. It takes a substitution σ and a set L of literals and yields a set of ground instances of a safe normal rule r, passed as a parameter; if called with the identity substitution and the body literals B(r) of r, it yields ground instances of the rule. The other parameters consist of a four-valued interpretation (I, J) comprising the set of possibly derivable atoms along with the certain ones, a two-valued interpretation J reflecting the previous value of J, and a Boolean flag f used to avoid duplicate ground rules in consecutive calls to Algorithm 1. The idea is to extend the current substitution in Lines 4-5 until we obtain a ground substitution σ that induces a ground instance rσ of rule r. To this end, Select σ (L) picks for each call some literal l ∈ L such that l ∈ L + or lσ is ground. That is, it yields either a positive body literal or a ground negative or ground comparison literal, as needed for computing Matches I,J l (σ). Whenever an application of Matches for the selected literal in B(r) results in a non-empty set of substitutions, the function is called recursively for each such substitution. The recursion terminates if at least one match is found for each body literal and an instance rσ of r is obtained in Line 8. The set of all such ground instances is returned in Line 6. (Note that we refrain from applying any simplifications to the ground rules and rather leave them intact to obtain more direct formal characterizations of the results of our grounding algorithms.) The test B(rσ) + J in Line 7 makes sure that no ground rules are generated that were already obtained by previous invocations of Algorithm 1. This is relevant for recursive rules and reflects the approach of semi-naive database evaluation (Abiteboul et al . 1995).
For characterizing the result of Algorithm 1 in terms of aggregate programs, we need the following definition.

Definition 16
Let P be an aggregate program and (I, J) be a four-valued interpretation.
We define Inst I,J (P ) ⊆ Inst(P ) as the set of all instances g of rules in P satisfying J |= π(B(g)) ∧ I . In terms of the program simplification in Definition 1, an instance g belongs to Inst I,J (P ) iff H(g) ← π(B(g)) ∧ ∈ π(r) I,J . Note that the members of Inst I,J (P ) are not necessarily ground, since non-global variables may remain within aggregates; though they are ground for normal rules.
We use Algorithm 1 to iteratively compute ground instances of a rule w.r.t. an increasing set of atoms. The Boolean flag f and the set of atoms J are used to avoid duplicating ground instances in successive iterations. The flag f is initially set to true to not filter any rule instances. In subsequent iterations, duplicates are omitted by setting the flag to false and filtering rules whose positive bodies are a subset of the atoms J used in previous iterations. This is made precise in the next result.

Definition 17
Let P be a safe aggregate program over signature Σ.
Let Σ be the signature obtained by extending Σ with fresh predicates α a,r /n, and (17) a,r /n (18) for each aggregate a occurring in a rule r ∈ P where n is the number of global variables in a, and fresh predicates η e,a,r /(m + n) for each aggregate element e occurring in aggregate a in rule r where m is the size of the tuple H(e). We define P α , P , and P η as normal programs over Σ as follows.
• Program P α is obtained from P by replacing each aggregate occurrence a in P with where α a,r /n is defined as in (17) and X 1 , . . . , X n are the global variables in a.
for each predicate a,r /n as in (18) for each predicate η e,a,r /m + n as in (19) where ( Summarizing the above, we translate an aggregate program P over Σ into a normal program P α along with auxiliary normal rules in P and P η , all over a signature extending Σ by the special-purpose predicates in (17)-(19). In fact, there is a one-to-one correspondence between the rules in P and P α , so that we get P = P α and P = P η = ∅ whenever P is normal.

Example 21
We illustrate the translation of aggregate programs on the company controls example in Example 10. We rewrite the rule
This example illustrates how possible instantiations of aggregate elements are gathered via the rules in P η . Similarly, the rules in P collect instantiations warranting that the result of applying aggregate functions to the empty set is in accord with the respective bound. In both cases, the relevant variable bindings are captured by the special head atoms of the rules. In turn, groups of corresponding instances of aggregate elements are used in Definition 20 to sanction the derivation of ground atoms of form (20). These atoms are ultimately replaced in P α with the original aggregate contents.
We next define two functions gathering information from instances of rules in P and P η . In particular, we make precise how groups of aggregate element instances are obtained from ground rules in P η .

Definition 18
Let P be an aggregate program, and G and G η be subsets of ground instances of rules in P and P η , respectively. Furthermore, let a be an aggregate occurring in some rule r ∈ P and σ be a substitution mapping the global variables in a to ground terms.
We define where r a is a rule of form (21) for aggregate occurrence a, and where E are the aggregate elements of a and r e is a rule of form (22) for aggregate element occurrence e in a.
Given that σ maps the global variables in a to ground terms, r a σ is ground whereas r e σ may still contain local variables from a. The set r,a (G , σ) has an indicative nature: For an aggregate aσ, it contains the identity substitution when the result of applying its aggregate function to the empty set is in accord with its bound, and it is empty otherwise. The construction of η r,a (G η , σ) goes one step further and reconstitutes all ground aggregate elements of aσ from variable bindings obtained by rules in G η . Both functions play a central role below in defining the function Propagate for deriving ground aggregate placeholders of form (20) from ground rules in G and G η .

Example 22
We show how to extract aggregate elements from ground instances of rules (r3) and (r4) in Example 21.
Let G be empty and G η be the program consisting of the following rules: η e1,a,r (60, c 1 , c 2 ) ← owns(c 1 , c 2 , 60) Clearly, we have r,a (G , σ) = ∅ for any substitution σ because G = ∅. This means that aggregate a can only be satisfied if at least one of its elements is satisfiable. In fact, we obtain non-empty sets η r,a (G η , σ) of ground aggregate elements for four substitutions σ: For capturing the result of grounding aggregates relative to groups of aggregate elements gathered via P η , we restrict their original translation to subsets of their ground elements. That is, while π(a) and π a (·) draw in Definition 9 on all instances of aggregate elements in a, their counterparts π G (a) and π a,G (·) are restricted to a subset of such aggregate element instances: 8

Definition 19
Let a be a closed aggregate and of form (8), E be the set of its aggregate elements, and G ⊆ Inst(E) be a set of aggregate element instances.
We define the translation π G (a) of a w.r.t. G as follows: As before, this translation maps aggregates, possibly including non-global variables, to a conjunction of (ground) R-rules. The resulting R-formula is used below in the definition of functions Propagate and Assemble.
The function Propagate yields a set of ground atoms of form (20) that are used in Algorithm 2 to ground rules having such placeholders among their body literals. Each such special atom is supported by a group of ground instances of its aggregate elements.

Definition 20
Let P be an aggregate program, (I, J) be a four-valued interpretation, and G and G η be subsets of ground instances of rules in P and P η , respectively.
We define Propagate I,J P (G , G η ) as the set of all atoms of form ασ such that r,a (G , σ) ∪ G = ∅ and J |= π G (aσ) I with G = η r,a (G η , σ) where α is an atom of form (20) for aggregate a in rule r and σ is a ground substitution for r mapping all global variables in a to ground terms.
An atom ασ is only considered if σ warrants ground rules in G or G η , signaling that the application of α to the empty set is feasible when applying σ or that there is a nonempty set of ground aggregate elements of α obtained after applying σ, respectively. If this is the case, it is checked whether the set G of aggregate element instances warrants that π G (aσ) admits stable models between I and J.

Example 24
We show how to propagate aggregates using the sets G 1 to G 4 and their associated formulas from Example 23. Suppose that I = J = F ∪ {controls(c 1 , c 2 )} using F from Example 20.

Definition 21
Let P be an aggregate program, and G α and G η be subsets of ground instances of rules in P α and P η , respectively.
We define Assemble(G α , G η ) as the R-program obtained from G α by replacing • all comparisons by and • all atoms of form ασ by the corresponding formulas π G (aσ) with G = η r,a (G η , σ) where α is an atom of form (20) for aggregate a in rule r and σ is a ground substitution for r mapping all global variables in a to ground terms.

Example 25
We show how to assemble aggregates using the sets G 1 to G 3 for aggregate atoms that have been propagated in Example 24. Therefore, let G α be the program consisting of the following rules: Then, program Assemble(G α , G η ) consists of the following rules: The next result shows how a (non-ground) aggregate program P is transformed into a (ground) R-program π J (P ) I,J in the context of certain and possible atoms (I, J) via the interplay of grounding P and P η , deriving aggregate placeholders from their ground instances G and G η , and finally replacing them in G α by the original aggregates' contents.
Property (b) highlights the relation of the possible atoms contributed by G α to a corresponding application of the immediate consequence operator. In fact, this is the first of three such relationships between grounding algorithms and consequence operators. 1 function Ground(P ) 2 let (P i ) i∈I be a refined instantiation sequence for P ; 3 (F, G) ← (∅, ∅); 4 foreach i ∈ I do 5 let P i be the program obtained from P i as in Definition 13; H(F ), H(G)); 8 return G;

Algorithm 3: Grounding Programs
Let us now turn to grounding components of instantiation sequences in Algorithm 2. The function GroundComponent takes an aggregate program P along with two sets I and J of ground atoms. Intuitively, P is a component in a (refined) instantiation sequence and I and J form a four-valued interpretation (I, J) comprising the certain and possible atoms gathered while grounding previous components (although their roles get reversed in Algorithm 3). After variable initialization, GroundComponent loops over consecutive rule instantiations in P α , P , and P η until no more possible atoms are obtained. In this case, it returns in Line 10 the R-program obtained from G α by replacing all ground aggregate placeholders of form (20) with the R-formula corresponding to the respective ground aggregate. The body of the loop can be divided into two parts: Lines 4-6 deal with aggregates and Lines 7 and 8 care about grounding the actual program. In more detail, Lines 4 and 5 instantiate programs P and P η , whose ground instances, G and G η , are then used in Line 6 to derive ground instances of aggregate placeholders of form (20). The grounded placeholders are then added via variable JA to the possible atoms J when grounding the actual program P α in Line 7, where J and JA hold the previous value of J and JA, respectively. For the next iteration, J is augmented in Line 8 with all rule heads in G α and the flag f is set to false. Recall that the purpose of f is to ensure that initially all rules are grounded. In subsequent iterations, duplicates are omitted by setting the flag to false and filtering rules whose positive bodies are a subset of the atoms J ∪ JA used in previous iterations.
While the inner workings of Algorithm 2 follow the blueprint given by Proposition 32. its outer functionality boils down to applying the stable operator of the corresponding ground program in the context of the certain and possible atoms gathered so far.

Proposition 33
Let P be an aggregate program, (IC , JC ) be a finite four-valued interpretation, and J = S JC π(P ) (IC ). Then, Finally, Algorithm 3 grounds an aggregate program by iterating over the components of one of its refined instantiation sequences. Just as Algorithm 2 reflects the application of a stable operator, function Ground follows the definition of an approximate model when grounding a component (cf. Definition 13). At first, facts are computed in Line 6 by using the program stripped from rules being involved in a negative cycle overlapping with the present or subsequent components. The obtained head atoms are then used in Line 7 as certain context atoms when computing the ground version of the component at hand. The possible atoms are provided by the head atoms of the ground program built so far, and with roles reversed in Line 6. Accordingly, the whole iteration aligns with the approximate model of the chosen refined instantiation sequence (cf. Definition 14), as made precise next.
Our grounding algorithm computes implicitly the approximate model of the chosen instantiation sequence and outputs the corresponding ground program; it terminates whenever the approximate model is finite.

Theorem 34
Let P be an aggregate program, (P i ) i∈I be a refined instantiation sequence for P , and (IC i , JC i ) and (I i , J i ) be defined as in Equations (15) and (16).
If (P i ) i∈I is selected by Algorithm 3 in Line 2, then we have that (a) the call Ground(P ) terminates iff AM ((P i ) i∈I ) is finite, and As already indicated by Theorem 29, grounding is governed by the series of consecutive approximate models (I i , J i ) in (16) delineating the stable models of each ground program in a (refined) instantiation sequence. Whenever each of them is finite, we also obtain a finite grounding of the original program. Note that the entire ground program is composed of the ground programs of each component in the chosen instantiation sequence. Hence, different sequences may result in different overall ground programs.
Most importantly, our grounding machinery guarantees that an obtained finite ground program has the same stable models as the original non-ground program.

Corollary 35 (Main result)
Let P be an aggregate program.
If Ground(P ) terminates, then P and Ground(P ) have the same well-founded and stable models.

Example 26
The execution of the grounding algorithms on Example 19 is illustrated in Table 1. Each individual table depicts a call to GroundComponent where the header above the double line contains the (literals of the) rules to be grounded and the rows below trace how nested calls to GroundRule proceed. The rules in the header contain the body literals in the order as they are selected by GroundRule with the rule head as the last literal. Calls to GroundRule are depicted with vertical lines and horizontal arrows. A vertical line represents the iteration of the loop in Lines 4-5. A horizontal arrow represents a recursive call to GroundRule in Line 5. Each row in the table not marked with × corresponds to a ground instance as returned by GroundRule. Furthermore, because all components are stratified, we only show the first iteration of the loop in Lines 3-9 of Algorithm 2 as the second iteration does not produce any new ground instances.
Grounding components P 1 to P 4 results in the programs Since grounding is driven by the sets of true and possible The grounding of P 5,1 is depicted in Table 1a and 1b. We have E = {b/1} because predicate b/1 is used in the head of the rule in P 5,2 . Thus, GroundComponent(∅, J 4 , I 4 ) in Line 6 returns the empty set because P 5,1 = ∅. We get I 5,1 = I 4 . In the next line, the algorithm calls GroundComponent(P 5,1 , I 5,1 , J 4 ) and we get J 5,1 = {p(1), p(2)}. Note that at this point, it is not known that q(1) is not derivable and so the algorithm does not derive p(1) as a fact.
The grounding of P 5,2 is given in Table 1c and 1d. This time, we have E = ∅ and P 5,2 = P 5,2 . Thus, the first call to GroundRule determines q(3) to be true while the second call additionally determines the possible atom q(2).
The grounding of P 6 is illustrated in Table 1e and 1f. Note that we obtain that x is possible because p(1) was not determined to be true.
The grounding of P 7 is depicted in Table 1g and 1h. Note that, unlike before, we obtain that y is false because q(3) was determined to be true.
Furthermore, observe that the choice of the refined instantiation sequence determines the output of the algorithm. In fact, swapping P 5,1 and P 5,2 in the sequence would result in x being false and y being possible.
To conclude, we give the grounding of the program as output by gringo in Table 2. The grounder furthermore omits the true body literals marked in green.

Example 27
We illustrate the grounding of aggregates on the company controls example in Example 10 using the grounding sequence (P i ) 1≤i≤9 and the set of facts F from Example 20. Observe that the grounding of components P 1 to P 8 produces the program {a ← | a ∈ F }. We 46 R. Kaminski and T. Schaub u(1) u x ← ¬p(1) focus on how component P 9 is grounded. Because there are no negative dependencies, the components P 9 and P 9 in Line 5 of Algorithm 3 are equal. To ground component P 9 , we use the rewriting from Example 21. The grounding of component P 9 is illustrated in Table 3, which follows the same conventions as in Example 26. Because the program is positive, the calls in Lines 6 and 7 in Algorithm 3 proceed in the same way and we depict only one of them. Furthermore, because this example involves a recursive rule with an aggregate, the header consists of five rows separated by dashed lines processed by Algorithm 2. The first row corresponds to P 9 grounded in Line 4, the second and third to P η 9 grounded in Line 5, the fourth to aggregate propagation in Line 6, and the fifth to P α 9 grounded in Line 7. After the header follow the iterations of the loop in Lines 3-9. Because the component is recursive, processing the component requires four iterations, which are separated by solid lines in the table. The right-hand side column of the table contains the iteration number and a number indicating which row in the header is processed. The row for aggregate propagation lists the aggregate atoms that have been propagated.
The grounding of rule r 2 in Row 1.1 does not produce any rule instances in any iteration because the comparison 0 > 50 is false. By first selecting this literal when grounding the rule, the remaining rule body can be completely ignored. Actual systems implement heuristics to prioritize such literals. Next, in the grounding of rule r 3 in Row 1.2, direct shares given by facts over owns/3 are accumulated. Because the rule does not contain any recursive predicates, it only produces ground instances in the first iteration. Unlike this, rule r 4 contains the recursive predicate controls/2 . It does not produce instances in the first iteration in Row 1.3 because there are no corresponding atoms yet. Next, aggregate propagation is triggered in Row 1.4, resulting in aggregate atoms α a,r (c 1 , c 2 ) and α a,r (c 3 , c 4 ), for which enough shares have been accumulated in Row 1.2. Note that this corresponds to the propagation of the sets G 1 and G 2 in Example 24. With these atoms, rule r 1 is instantiated in Row 1.5, leading to new atoms over controls/2. Observe that, by selecting atom α a,r (X, Y ) first, GroundRule can instantiate the rule without backtracking.
In the second iteration, the newly obtained atoms over predicate controls/2 yield atom η e1,a,r (35, c 2 , c 1 , c 3 ) in Row 2.3, which in turn leads to the aggregate atom α a,r (c 1 , c 3 ) resulting in further instances of r 4 . Note that this corresponds to the propagation of the set G 3 in Example 24.
The following iterations proceed in a similar fashion until no new atoms are accumulated and the grounding loop terminates. Note that the utilized selection strategy affects the amount of backtracking in rule instantiation. One particular strategy used in gringo is to prefer atoms over recursive predicates. If there is only one such atom, GroundRule  (Ullman 1988) that also work in case of multiple atoms over recursive predicates.
To conclude, we give the ground rules as output by a grounder like gringo in Table 4. We omit the translation of aggregates because its main objective is to show correctness of the algorithms. Solvers like clasp implement translations or even native handling of aggregates geared toward efficient solving ). Since our example program is positive, gringo is even able to completely evaluate the rules to facts omitting true literals from rule bodies marked in green.  Table 4. Grounding of the company controls problem from Example 10 as output by gringo

Refinements
Up to now, we were primarily concerned by characterizing the theoretical and algorithmic cornerstones of grounding. This section refines these concepts by further detailing aggregate propagation, algorithm specifics, and the treatment of language constructs from gringo's input language.

Aggregate propagation
We used in Section 6 the relative translation of aggregates for propagation, namely, formula π G (aσ) in Definition 20, to check whether an aggregate is satisfiable. In this section, we identify several aggregate specific properties that allow us to implement more efficient algorithms to perform this check.
To begin with, we establish some properties that greatly simplify the treatment of (arbitrary) monotone or antimonotone aggregates.
We have already seen in Proposition 24 that π(a) I is classically equivalent to π(a) for any closed aggregate a and two-valued interpretation I. Here is its counterpart for antimonotone aggregates.

Proposition 36
Let a be a closed aggregate.
If a is antimonotone, then π(a) I is classically equivalent to if I |= π(a) and ⊥ otherwise for any two-valued interpretation I.
An actual implementation can maintain a counter for the current value of the sum for each closed aggregate instance, which can be updated incrementally and compared with the bound as new instances of aggregate elements are grounded.
Next, we see that such counter based implementations are also possible #sum aggregates using the <, ≤, >, or ≥ relations. We restrict our attention to finite interpretations because Proposition 37 is intended to give an idea on how to implement an actual propagation algorithm for aggregates (infinite interpretations would add more special cases). Furthermore, we just consider the case that the bound is an integer here; the aggregate is constant for any other ground term.

Proposition 37
Let I be a finite two-valued interpretation, E be a set of aggregate elements, and b be an integer. For The remaining propositions identify properties that can be exploited when propagating aggregates over the = and = relations.

Proposition 38
Let I be a two-valued interpretation, E be a set of aggregate elements, and b be a ground term.
We get the following properties: The following proposition identifies special cases when the implications in Proposition 38 are equivalences. Another interesting aspect of this proposition is that we can actually replace #sum aggregates over = and = with a conjunction or disjunction, respectively, at the expense of calculating a less precise approximate model. The conjunction is even strongly equivalent to the original aggregate under Ferraris' semantics but not the disjunction.

Proposition 39
Let I and J be two-valued interpretations, f be an aggregate function among #count, #sum + , #sum − or #sum, E be a set of aggregate elements, and b be an integer.
We get the following properties: The following proposition shows that full propagation of #sum, #sum + , or #sum − aggregates over relations = and = involves solving the subset sum problem (Martello and Toth 1990). We assume that we propagate w.r.t. some polynomial number of aggregate elements. Propagating possible atoms when using the = relation, that is, when I ⊆ J, involves deciding an NP problem and propagating certain atoms when using the = 50 R. Kaminski and T. Schaub relation, that is, when J ⊆ I, involves deciding a co-NP problem. 9 Note that the decision problem for #count aggregates is polynomial, though.

Proposition 40
Let I and J be finite two-valued interpretations, f be an aggregate function, E be a set of aggregate elements, and b be a ground term.

Algorithmic refinements
The calls in Lines 6 and 7 in Algorithm 3 can sometimes be combined to calculate certain and possible atoms simultaneously. This can be done whenever a component does not contain recursive predicates. In this case, it is sufficient to just calculate possible atoms along with rule instances in Line 7 augmenting Algorithm 1 with an additional check to detect whether a rule instance produces a certain atom. Observe that this condition applies to all stratified components but can also apply to components depending on unstratified components. In fact, typical programs following the generate, define, and test methodology (Lifschitz 2002;Niemelä 2008) of ASP, where the generate part uses choice rules (Simons et al . 2002) (see below), do not contain unstratified negation at all. When a grounder is combined with a solver built to store rules and perform inferences, one can optimize for the case that there are no negative recursive predicates in a component. In this case, it is sufficient to compute possible atoms along with their rule instances and leave the computation of certain atoms to the solver. Finally, note that gringo currently does not separate the calculation of certain and possible atoms at the expense of computing a less precise approximate model and possibly additional rule instances.

Example 29
For the following example, gringo computes atom p(4) as unknown but the algorithms in Section 6 identify it as true.
When grounding the last rule, gringo determines p(4) to be possible in the first iteration because p(1) is unknown at this point. In the second iteration, it detects that p(1) is a fact but does not use it for grounding again. If there were further rules depending negatively on predicate p/1, inapplicable rules might appear in gringo's output.
Another observation is that the loop in Algorithm 2 does not produce further rule instances in a second iteration for components without recursive predicates. Gringo maintains an index (Garcia-Molina et al . 2009) for each positive body literal to speed up matching of literals; whenever none of these indexes, used in rules of the component at hand, are updated, further iterations can be skipped.
Just like dlv 's grounder, gringo adapts algorithms for semi-naive evaluation from the field of databases. In particular, it works best on linear programs (Abiteboul et al . 1995), having at most one positive literal occurrence over a recursive predicate in a rule body. The program in Example 10 for the company controls problem is such a linear program because controls/2 is the only recursive predicate. Algorithm 1 can easily be adapted to efficiently ground linear programs by making sure that the recursive positive literal is selected first. We then only have to consider matches that induce atoms not already used for instantiations in previous iterations of the loop in Algorithm 2 to reduce the amount of backtracking to find rule instances. In fact, the order in which literals are selected in Line 3 is crucial for the performance of Algorithm 1. Gringo uses an adaptation of the selection heuristics presented by Leone et al . (2001) that additionally takes into account recursive predicates and terms with function symbols.
To avoid unnecessary backtracking when grounding general logic programs, gringo instantiates rules using an algorithm similar to the improved semi-naive evaluation with optimizations for linear rules (Abiteboul et al . 1995).

Capturing gringo's input language
We presented aggregate programs where rule heads are simple atoms. Beyond that, gringo's input language offers more elaborate language constructs to ease modeling.
A prominent such construct are so-called choice rules (Simons et al . 2002). Syntactically, one-element choice rules have the form {a} ← B, where a is an atom and B a body. Semantically, such a rule amounts to a ∨ ¬a ← B or equivalently a ← ¬¬a ∧ B. We can easily add support for grounding choice rules, that is, rules where the head is not a plain atom but an atom marked as a choice, by discarding choice rules when calculating certain atoms and treating them like normal rules when grounding possible atoms. A translation that allows for supporting head aggregates using a translation to aggregate rules and choice rules is given by Gebser et al . (2015a). Note that gringo implements further refinements to omit deriving head atoms if a head aggregate cannot be satisfied.
Another language feature that can be instantiated in a similar fashion as body aggregates are conditional literals. Gringo adapts the rewriting and propagation of body aggregates to also support grounding of conditional literals.
Yet another important language feature are disjunctions in the head of rules (Gelfond and Lifschitz 1991). As disjunctive logic programs, aggregate programs allow us to solve problems from the second level of the polynomial hierarchy. In fact, using Lukasiewicz' theorem (Lukasiewicz 1941), we can write a disjunctive rule of form as the shifted strongly equivalent R-program: We can use this as a template to design grounding algorithms for disjunctive programs. In fact, gringo calculates the same approximate model for the disjunctive rule and the shifted program.
The usage of negation as failure is restricted in R-programs. Note that any occurrence of a negated literal l in a rule body can be replaced by an auxiliary atom a adding rule a ← l to the program. The resulting program preserves the stable models modulo the auxiliary atoms. This translation can serve as a template for double negation or negation in aggregate elements as supported by gringo.
Integrity constraints are a straightforward extension of logic programs. They can be grounded just like normal rules deriving an auxiliary atom that stands for ⊥. Grounding can be stopped whenever the auxiliary atom is derived as certain. Integrity constraints also allow for supporting negated head atoms, which can be shifted to rule bodies (Janhunen 2001) resulting in integrity constraints, and then treated like negation in rule bodies.
A frequently used convenience feature of gringo are term pools (Gebser et al . 2015a;. The grounder handles them by removing them in a rewriting step. For example, a rule of form is factored out into the following rules: We can then apply the grounding algorithms developed in Section 6. To deal with variables ranging over integers, gringo supports interval terms (Gebser et al . 2015a;. Such terms are handled by a translation to inbuilt range predicates. For example the program h (l..u) for terms l and u is rewritten into h(A) ← rng (A, l, u) by introducing auxiliary variable A and range atom rng (A, l, u). The range atom provides matches including all substitutions that assign integer values between l and u to A. Special care has to be taken regarding rule safety, the range atom can only provide bindings for variable A but needs variables in the terms l and u to be provided elsewhere.
A common feature used when writing logic programs are terms involving arithmetic expressions and assignments. Both influence which rules are considered safe by the grounder. For example, the rule by introducing auxiliary variable A. The rule is safe because we can match the literals in the order as given in the rewritten rule. The comparison X = Y + Y extends the substitution with an assignment for X and the last comparison serves as a test. Gringo does not try to solve complicated equations but supports simple forms like the one given above.
Last but not least, gringo does not just support terms in assignments but it also supports aggregates in assignments. To handle such kind of aggregates, the rewriting and propagation of aggregates has to be extended. This is achieved by adding an additional variable to aggregate replacement atoms (20), which is assigned by propagation. For example, the rule Aggregate elements are grounded as before but values for variable Y are computed during aggregate propagation. In case of multiple assignment aggregates, additional care has to to be taken during the rewriting to ensure that the rewritten rules are safe.

Related work
This section aims at inserting our contributions into the literature, starting with theoretical aspects over algorithmic ones to implementations.
Splitting for infinitary formulas has been introduced by Harrison and Lifschitz (2016) generalizing results of Janhunen et al . (2007) and Ferraris et al . (2009). To this end, the concept of an A-stable model is introduced (Harrison and Lifschitz 2016). We obtain the following relationship between our definition of a stable model relative to a set IC and A-stable models: For an N -program P , we have that if X is a stable model of P relative to IC , then X ∪ IC is an (A \ IC )-stable model of P . Similarly, we get that if X is an Astable model of P , then S X\A P (X) is a stable model of P relative to X \ A. The difference between the two concepts is that we fix atoms IC in our definition while A-stable models allow for assigning arbitrary truth values to atoms in A \ A (Harrison and Lifschitz 2016, Proposition 1). With this, let us compare our handling of program sequences to symmetric splitting (Harrison and Lifschitz 2016). Let (P i ) i∈I be a refined instantiation sequence of aggregate program P , and F = i<j π(P i ) and G = i≥j π(P i ) for some j ∈ I such that H(F ) = H(G). We can use the infinitary splitting theorem of Harrison and Lifschitz (2016) to calculate the stable model of F ∧ ∧ G ∧ through the H(F )-and A \ H(F )-stable models of F ∧ ∧ G ∧ . Observe that instantiation sequences do not permit positive recursion between their components and infinite walks are impossible because an aggregate program consists of finitely many rules inducing a finite dependency graph. Note that we additionally require the condition H(F ) = H(G) because components can be split even if their head atoms overlap. Such a split can only occur if overlapping head atoms in preceding components are not involved in positive recursion.
Next, let us relate our operators to the ones defined by Truszczyński (2012). First of all, it is worthwhile to realize that the motivation of Truszczyński (2012) is to conceive operators mimicking model expansion in id-logic by adding certain atoms. More precisely, let Φ, St, and Wf stand for the versions of the Fitting, stable, and well-founded operators defined by Truszczyński (2012). Then, we get the following relations to the operators defined in the previous sections: Our operators allow us to directly calculate the atoms derived by a program. The versions of Truszczyński (2012) always include the input facts in their output and the well-founded operator only takes certain but not possible atoms as input.
In fact, we use operators as Denecker et al . (2000) to approximate the well-founded model and to obtain a ground program. While we apply operators to infinitary formulas (resulting from a translation of aggregates) as introduced by Truszczyński (2012), there has also been work on applying operators directly to aggregates. Vanbesien et al . (2021) provide an overview. Interestingly, the high complexity of approximating the aggregates pointed out in Proposition 40 has already been identified by Pelov et al . (2007).
Simplification can be understood as a combination of unfolding (dropping rules if a literal in the positive body is not among the head atoms of a program, that is, not among the possible atoms) and negative reduction (dropping rules if an atom in the negative body is a fact, that is, the literal is among the certain atoms) (Brass and Dix 1999;Brass et al . 2001). Even the process of grounding can be seen as a directed way of applying unfolding (when matching positive body literals) and negative reduction (when matching negative body literals). When computing facts, only rules whose negative body can be removed using positive reduction are considered.
The algorithms of Kemp et al . (1991) to calculate well-founded models perform a computation inspired by the alternating sequence to define the well-founded model as Van Gelder (1993). Our work is different in so far as we are not primarily interested in computing the well-founded model but the grounding of a program. Hence, our algorithms stop after the second application of the stable operator (the first to compute certain and the second to compute possible atoms). At this point, a grounder can use algorithms specialized for propositional programs to simplify the logic program at hand. Algorithmic refinements for normal logic programs as proposed by Kemp et al . (1991) also apply in our setting.
Last but not least, let us outline the evolution of grounding systems over the last two decades.
The lparse (Syrjänen 2001a) grounder introduced domain-or omega-restricted programs (Syrjänen 2001b). Unlike safety, omega-restrictedness is not modular. That is, the union of two omega-restricted programs is not necessarily omega-restricted while the union of two safe programs is safe. Apart from this, lparse supports recursive monotone and antimonotone aggregates. However, our company controls encoding in Example 10 is not accepted because it is not omega-restricted. For example, variable X in the second aggregate element needs a domain predicate. Even if we supplied such a domain predicate, lparse would instantiate variable X with all terms provided by the domain predicate resulting in a large grounding. As noted by Ferraris and Lifschitz (2005), recursive nonmonotone aggregates (sum aggregates with negative weights) are not supported correctly by lparse.
Gringo 1 and 2 add support for lambda-restricted programs (Gebser et al . 2007) extending omega-restricted programs. This augments the set of predicates that can be used for instantiation but is still restricted as compared to safe programs. That is, lambdarestrictedness is also not modular and our company controls program is still not accepted. At the time, the development goal was to be compatible to lparse but extend the class of accepted programs. Notably, gringo 2 adds support for additional aggregates ). Given its origin, gringo up to version 4 handles recursive nonmonotone aggregates in the same incorrect way as lparse.
The grounder of the dlv system has been the first one to implement grounding algorithms based on semi-naive evaluation (Eiter et al . 1997). Furthermore, it implements various techniques to efficiently ground logic programs Faber et al . 2001;Perri et al . 2007). The dlv A system is the first dlv -based system to support recursive aggregates (Dell'Armi et al . 2003), which is nowadays also available in recent versions of idlv (Calimeri et al . 2017).
Gringo 3 closed up to dlv being the first gringo version to implement grounding algorithms based on semi-naive evaluation (Gebser et al . 2011). The system accepts safe rules but still requires lambda-restrictedness for predicates within aggregates. Hence, our company controls encoding is still not accepted.
Gringo 4 implements grounding of aggregates with algorithms similar to the ones presented in Section 6 (Gebser et al . 2015c). Hence, it is the first version that accepts our company controls encoding.
Finally, gringo 5 refines the translation of aggregates as proposed by  to properly support nonmonotone recursive aggregates and refines the semantics of pools and undefined arithmetics (Gebser et al . 2015a).
Another system with a grounding component is the idp system (De Cat et al . 2014). Its grounder instantiates a theory by assigning sorts to variables. Even though it supports inductive definitions, it relies solely on the sorts of variables (Wittocx et al . 2010) to instantiate a theory. In case of inductive definitions, this can lead to instances of definitions that can never be applied. We believe that the algorithms presented in Section 6 can also be implemented in an idp system decreasing the instantiation size of some problems (e.g., the company controls problem presented in Example 10).

R. Kaminski and T. Schaub
Last but not least, we mention that not all ASP systems follow a two-phase approach of grounding and solving but rather adapt a lazy approach by grounding on-the-fly during solving (Palù et al . 2009;Lefèvre et al . 2017;Weinzierl et al . 2020).

Conclusion
We have provided a first comprehensive elaboration of the theoretical foundations of grounding in ASP. This was enabled by the establishment of semantic underpinnings of ASP's modeling language in terms of infinitary (ground) formulas (Harrison et al . 2014;Gebser et al . 2015a). Accordingly, we start by identifying a restricted class of infinitary programs, namely, R-programs, by limiting the usage of implications. Such programs allow for tighter semantic characterizations than general F-programs, while being expressive enough to capture logic programs with aggregates. Interestingly, we rely on well-founded models (Bruynooghe et al . 2016;Truszczyński 2018) to approximate the stable models of R-programs (and simplify them in a stable-models preserving way). This is due do the fact that the (id-)well-founded-operator enjoys monotonicity, which lends itself to the characterization of iterative grounding procedures. The actual semantics of non-ground aggregate programs is then defined via a translation to R-programs. This setup allows us to characterize the inner workings of our grounding algorithms for aggregate programs in terms of the operators introduced for R-programs. It turns out that grounding amounts to calculating an approximation of the well-founded model together with a ground program simplified with that model. This does not only allow us to prove the correctness of our grounding algorithms but moreover to characterize the output of a grounder like gringo in terms of established formal means. To this end, we have shown how to split aggregate programs into components and to compute their approximate models (and corresponding simplified ground programs). The key instruments for obtaining finite ground programs with finitary subformulas have been dedicated forms of program simplification and aggregate translation. Even though, we limit ourselves to R-programs, we capture the core aspects of grounding: a monotonically increasing set of possibly derivable atoms and on-the-fly (ground) rule generation. Additional language features of gringo's input language are relatively straightforward to accommodate by extending the algorithms presented in this paper.
For reference, we implemented the presented algorithms in a prototypical grounder, μ-gringo, supporting aggregate programs (see Footnote 1). While it is written to be as concise as possible and not with efficiency in mind, it may serve as a basis for experiments with custom grounder implementations. The actual gringo system supports a much larger language fragment. There are some differences compared to the algorithms presented here. First, certain atoms are removed from rule bodies if not explicitly disabled via a command line option. Second, translation π is only used to characterize aggregate propagation. In practice, gringo translates ground aggregates to monotone aggregates . Further translation (Bomanson et al . 2014) or even native handling ) of them is left to the solver. Finally, in some cases, gringo might produce more rules than the algorithms presented above. This should not affect typical programs. A tighter integration of grounder and solver to further reduce the number of ground rules is an interesting topic of future research.