On the Semantics of Gringo
Abstract
Input languages of answer set solvers are based on the mathematically simple concept of a stable model. But many useful constructs available in these languages, including local variables, conditional literals, and aggregates, cannot be easily explained in terms of stable models in the sense of the original definition of this concept and its straightforward generalizations. Manuals written by designers of answer set solvers usually explain such constructs using examples and informal comments that appeal to the user’s intuition, without references to any precise semantics. We propose to approach the problem of defining the semantics of gringo programs by translating them into the language of infinitary propositional formulas. This semantics allows us to study equivalent transformations of gringo programs using natural deduction in infinitary propositional logic.
1 Introduction
In this note, Gringo is the name of the input language of the grounder gringo,^{1}^{1}1http://potassco.sourceforge.net/. which is used as the front end in many answer set programming (ASP) systems. Several releases of gringo have been made public, and more may be coming in the future; accordingly, we can distinguish between several “dialects” of the language Gringo. We concentrate here on Version 4, released in March of 2013. (It differs from Version 3, described in the User’s Guide dated October 4, 2010,^{2}^{2}2The User’s Guide can be downloaded from the Potassco website (Footnote 1). It is posted also at http://www.cs.utexas.edu/users/vl/teaching/lbai/clingo_ guide.pdf. in several ways, including the approach to aggregates—it is modified as proposed by the ASP Standardization Working Group.^{3}^{3}3https://www.mat.unical.it/aspcomp2013/ASPStandardization. )
The basis of Gringo is the language of logic programs with negation as failure, with the syntax and semantics defined in [6]. Our goal here is to extend that semantics to a larger subset of Gringo. Specifically, we would like to cover arithmetical functions and comparisons, conditions, and aggregates.^{4}^{4}4The subset of Gringo discussed in this note includes also constraints, disjunctive rules, and choice rules, treated along the lines of [7] and [3]. The first of these papers introduces also “classical” (or “strong”) negation—a useful feature that we do not include. (Extending our semantics of Gringo to programs with classical negation is straightforward, using the process of eliminating classical negation in favor of additional atoms described in [7, Section 4].)
Our proposal is based on the informal and sometimes incomplete description of the language in the User’s Guide, on the discussion of ASP programming constructs in [4], on experiments with gringo, and on the clarifications provided in response to our questions by its designers.
The proposed semantics uses a translation from Gringo into the language of infinitary propositional formulas—propositional formulas with infinitely long conjunctions and disjunctions. Including infinitary formulas is essential, as we will see, when conditions or aggregates use variables ranging over infinite sets (for instance, over integers).
Alternatively, the semantics of Gringo can be approached using quantified equilibrium logic [12] or its syntactic counterpart defined in [2]. This method involves translating rules into the language of firstorder logic. For instance, the rule
(1) 
can be represented by the sentence
However, this translation is not sufficiently general. For instance, it is not clear how to represent the rule
(2) 
from Section 3.1.10 of the Gringo 3 User’s Guide with a firstorder formula. One reason is that the aggregate sum is used here instead of count. The second difficulty is that the variable is used rather than a constant.
General aggregate expressions, as used in Gringo, can be represented by firstorder formulas with generalized quantifiers.^{5}^{5}5Stable models of formulas with generalized quantifiers are defined by Lee and Meng [9][10][11]. The advantage of infinitary propositional formulas as the target language is that properties of these formulas, and of their stable models, are better understood. We may be able to prove, for instance, that two Gringo programs have the same stable models by observing that the corresponding infinitary formulas are equivalent in one of the natural deduction systems discussed in [8]. We give here several examples of reasoning about Gringo programs based on this idea.
The process of converting Gringo programs into infinitary propositional formulas defined in this note uses substitutions to eliminate variables. This form of grounding is quite different, of course, from the process of intelligent instantiation implemented in gringo and other grounders. Mathematically, it is much simpler than intelligent instantiation; as a computational procedure, it is much less efficient, not to mention the fact that sometimes it produces infinite objects. Like grounding in the original definition of a stable model [6], it is modular, in the sense that it applies to the program rule by rule, and it is applicable even if the program is not safe. From this perspective, gringo’s safety requirement is an implementation restriction.
Our description of the syntax of Gringo disregards some of the
features related to representing programs as strings of ASCII characters,
such as using :
to separate the head from the body, using semicolons,
rather than parentheses, to indicate the boundaries of a conditional literal,
and representing
falsity (which we denote here by ) as #false
. Since the subset
of Gringo discussed in this note does not include assignments, we can
disregard also the requirement that equality be represented by two characters
==
.
2 Syntax
We begin with a signature in the sense of firstorder logic that includes, among others,

numerals—object constants representing all integers,

arithmetical functions—binary function constants , , ,

comparisons—binary predicate constants , , , .
We will identify numerals with the corresponding elements of the set Z of integers. Object, function, and predicate symbols not listed under (i)–(iii) will be called symbolic. A term over is arithmetical if it does not contain symbolic object or function constants. A ground term is precomputed if it does not contain arithmetical functions.
We assume that in addition to the signature, a set of symbols called aggregate names is specified, and that for each aggregate name , the function denoted by , , maps every tuple of precomputed terms to an element of .
Examples. The functions denoted by the aggregate names count, max, and sum are defined as follows. For any set of tuples of precomputed terms,

is the cardinality of if is finite, and otherwise;

is the least upper bound of the set of the integers over all tuples from in which is an integer;

is the sum of the integers over all tuples from in which is a positive integer; it is if there are infinitely many such tuples.^{6}^{6}6To allow negative numbers in this example, we would have to define summation for a set that contains both infinitely many positive numbers and infinitely many negative numbers. It is unclear how to do this in a natural way.
A literal is an expression of one of the forms
where is a symbolic predicate constant of arity , and each is a term over , or
where is a comparison, and , are arithmetical terms. A conditional literal is an expression of the form , where is a literal or the symbol , and L is a list of literals, possibly empty. The members of L will be called conditions. If L is empty then we will drop the colon after , so that every literal can be viewed as a conditional literal.
Example. If available and person are unary predicate symbols then
and
are conditional literals.
An aggregate expression is an expression of the form
where is an aggregate name, is a list of terms, is a list of literals, is a comparison or the symbol , and is an arithmetical term.
Example. If enroll is a unary predicate symbol and hours is a binary predicate symbol then
is an aggregate expression.
A rule is an expression of the form
(3) 
(), where each is a conditional literal, and each is a conditional literal or an aggregate expression. A program is a set of rules.
If is a symbolic predicate constant of arity , and t is a tuple of terms, then
is shorthand for
Example. For any positive integer ,
(4) 
is a program.
3 Semantics
We will define the semantics of Gringo using a syntactic transformation . It converts Gringo rules into infinitary propositional combinations of atoms of the form , where is a symbolic predicate constant, and t is a tuple of precomputed terms. Then the stable models of a program will be defined as stable models, in the sense of [13], of the set consisting of the translations of all rules of the program. Truszczynski’s definition of stable models for infinitary propositional formulas is reviewed below.
Prior to defining the translation for rules, we will define it for ground literals, conditional literals, and aggregate expressions.
3.1 Review: Stable Models of Infinitary Formulas
Let be a propositional signature, that is, a set of propositional atoms. The sets , , are defined as follows:

,

is obtained from by adding expressions and for all subsets of , and expressions for all .
The elements of are called (infinitary) formulas over . Negation and equivalence are abbreviations.
Subsets of a signature will be also called its interpretations. The satisfaction relation between an interpretation and a formula is defined in a natural way.
The reduct of a formula w.r.t. an interpretation is defined as follows:

.

For , if ; otherwise .

.

.

if ; otherwise .
An interpretation is a stable model of a set of formulas if it is minimal w.r.t. set inclusion among the interpretations satisfying the reducts of all formulas from .
3.2 Semantics of WellFormed Ground Literals
A term t is wellformed if it contains neither symbolic object constants nor symbolic function constants in the scope of arithmetical functions. For instance, all arithmetical terms and all precomputed terms are wellformed; is not wellformed. The definition of “wellformed” for literals, aggregate expressions, and so forth is the same.
For every wellformed ground term , by we denote the precomputed term obtained from by evaluating all arithmetical functions, and similarly for tuples of terms. For instance, is .
The translation of a wellformed ground literal is defined as follows:

is ;

, where is the symbol or a comparison, is if the relation holds between and , and otherwise;

is .
For instance, is , and is .
Furthermore, stands for , and, for any list L of ground literals, is the conjunction of the formulas for all members of L.
3.3 Global Variables
About a variable we say that it is global

in a conditional literal , if it occurs in but does not occur in L;

in an aggregate expression , if it occurs in the term ;

in a rule (3), if it is global in at least one of the expressions , .
For instance, the head of the rule (2) is a literal with the global variable , and its body is an aggregate expression with the global variable . Consequently is global in the rule as well.
A conditional literal, an aggregate expression, or a rule is closed if it has no global variables. An instance of a rule is any wellformed closed rule that can be obtained from by substituting precomputed terms for global variables. For instance,
is an instance of rule (2). It is clear that if a rule is not wellformed then it has no instances.
3.4 Semantics of Closed Conditional Literals
If is a term, x is a tuple of distinct variables, and r is a tuple of terms of the same length as x, then the term obtained from by substituting r for x will be denoted by . Similar notation will be used for the result of substituting r for x in expressions of other kinds, such as literals and lists of literals.
The result of applying to a closed conditional literal is the conjunction of the formulas
where x is the list of variables occurring in , over all tuples r of precomputed terms of the same length as x such that both and are wellformed. For instance,
is the conjunction of the formulas over all precomputed terms ;
is the conjunction of the formulas over all numerals . When a conditional literal occurs in the head of a rule, we will translate it in a different way. By we denote the disjunction of the formulas
where x and r are as above. For instance,
is the disjunction of the formulas over all precomputed terms .
3.5 Semantics of Closed Aggregate Expressions
In this section, the semantics of ground aggregates proposed in [1, Section 4.1] is adapted to closed aggregate expressions. Let be a closed aggregate expression , and let x be the list of variables occurring in . A tuple r of precomputed terms of the same length as x is admissible (w.r.t. ) if both and are wellformed. About a set of admissible tuples we say that it justifies if the relation holds between and . For instance, consider the aggregate expression
(5) 
In this case, admissible tuples are arbitrary pairs of precomputed terms. The set justifies (5), because
More generally, a set of pairs of precomputed terms justifies (5) whenever contains finitely many pairs in which is a positive integer, and the sum of the integers over all these pairs is 6.
We define as the conjunction of the implications
(6) 
over all sets of admissible tuples that do not justify , where is the set of all admissible tuples. For instance, if is (5) then the conjunctive terms of are the formulas
The conjunctive term corresponding to as says: if I am enrolled in CS101 for 3 hours then I am enrolled in at least one other course.
3.6 Semantics of Rules and Programs
For any rule , stands for the conjunction of the formulas
for all instances (3) of . A stable model of a program is a stable model, in the sense of [13], of the set consisting of the formulas for all rules of .
Consider, for instance, the rules of program (4). If is the rule then is
(7) 
(). If is the rule
then the instances of are rules of the form
for all numerals , . (Substituting precomputed ground terms other than numerals would produce a rule that is not wellformed.) Consequently is in this case the infinite conjunction
(8) 
The stable models of program (4) are the stable models of formulas (7), (8), that is, sets of the form for all sumfree subsets of .
4 Reasoning about Gringo Programs
In this section we give examples of reasoning about Gringo programs on the basis of the semantics defined above. These examples use the results of [8], and we assume here that the reader is familiar with that paper.
4.1 Simplifying a Rule from Example 3.7 of User’s Guide
Consider the rule^{7}^{7}7This rule is similar to a rule from Example 3.7 of the Gringo 3 User’s Guide (see Footnote 2).
(9) 
Replacing this rule with the fact weekdays within any program will not affect the set of stable models. Indeed, the result of applying translation to (9) is the formula
(10) 
where the conjunction extends over all precomputed terms . The formula
is intuitionistically provable. By the replacement property of the basic system of natural deduction from [8], it follows that (10) is equivalent to weekdays in the basic system. By the main theorem of [8], it follows that replacing (10) with the atom weekdays within any set of formulas does not affect the set of stable models.
4.2 Simplifying the Sorting Rule
The rule
(11) 
can be used for sorting.^{8}^{8}8This rule was communicated to us by Roland Kaminski on October 21, 2012. It can be replaced by either of the following two shorter rules within any program without changing that program’s stable models.
(12) 
(13) 
Let’s prove this claim for rule (12). By the main theorem of [8] it is sufficient to show that the result of applying to (11) is equivalent in the basic system to the result of applying to (12). The instances of (11) are the rules
and the instances of (12) are the rules
where and are arbitrary numerals. The result of applying to (11) is the conjunction of the formulas
(14) 
for all numerals . The result of applying to (12) is the conjunction of the formulas
(15) 
By the replacement property of the basic system, it is sufficient to observe that
is intuitionistically equivalent to
4.3 Eliminating Choice in Favor of a Conditional Literal
Replacing the rule
(16) 
with
(17) 
within any program will not affect the set of stable models. Indeed, the result of applying translation to (16) is
(18) 
where the conjunction extends over all precomputed terms , and the result of applying to (17) is
(19) 
The implication from (18) is equivalent to the implication from (19) in the extension of intuitionistic logic obtained by adding the axiom schema
and consequently in the extended system presented in [8, Section 7]. By the replacement property of the extended system, it follows that (18) is equivalent to (19) in the extended system as well.
4.4 Eliminating a Trivial Aggregate Expression
The rule (1) says, informally speaking, that we can conclude once we established that there exists at least one such that . Replacing this rule with
(20) 
within any program will not affect the set of stable models.
To prove this claim, we need to calculate the result of applying to rule (1). The instances of (1) are the rules
(21) 
for all precomputed terms . Consider the aggregate expression in the body of (21). Any precomputed term is admissible w.r.t. . A set of precomputed terms justifies if
that is to say, if is nonempty. Consequently consists of only one implication (6), with the empty . The antecedent of this implication is the empty conjunction , and its consequent is the disjunction over all precomputed terms . Then the result of applying to (1) is
(22) 
On the other hand, the result of applying to (20) is
This formula is equivalent to (22) in the basic system [8, Example 2].
4.5 Replacing an Aggregate Expression with a Conditional Literal
Informally speaking, the rule
(23) 
says that we can conclude once we have established that the cardinality of the set is ; the rule
(24) 
says that we can conclude once we have established that does not hold for any . We’ll prove that replacing (23) with (24) within any program will not affect the set of stable models. To this end, we’ll show that the results of applying to (23) and (24) are equivalent to each other in the extended system from [8, Section 7].
First, we’ll need to calculate the result of applying to rule (23). Consider the aggregate expression in the body of (23). Any precomputed term is admissible w.r.t. . A set of precomputed terms justifies if
that is to say, if is empty. Consequently is the conjunction of the implications
(25) 
for all nonempty subsets of the set of precomputed terms. The result of applying to (23) is
(26) 
The result of applying to (24), on the other hand, is
(27) 
The fact that the antecedents of (26) and (27) are equivalent to each other in the extended system can be established by essentially the same argument as in [8, Example 7]. By the replacement property of the extended system, it follows that (26) is equivalent to (27) in the extended system as well.
4.6 Eliminating Summation over the Empty Set
Informally speaking, the rule
(28) 
says that we can conclude once we have established that the sum of the elements of the set is . In the presence of the constraint
(29) 
replacing (28) with the fact will not affect the stable models.
To see this, first we calculate the result of applying to rule (28). Consider the aggregate expression in the body of (28). Any precomputed term is admissible w.r.t. . A set of precomputed terms justifies if
that is to say, if contains no positive integers. Consequently is the conjunction of the implications
(30) 
for subsets of the set of precomputed terms that contain at least one positive integer. The result of applying to (28) is
(31) 
The result of applying to (29), on the other hand, is
(32) 
For every nonempty , the antecedent of (30) contradicts (32). Consequently, the antecedent of (31) can be derived from (32) in the basic system. It follows that the equivalence between (31) and the atom can be derived in the basic system under assumption (32).
5 Conclusion
In this note we approached the problem of defining the semantics of Gringo by reducing Gringo programs to infinitary propositional formulas. We argued that this approach to semantics may allow us to study equivalent transformations of programs using natural deduction in infinitary propositional logic.
In the absence of a precise semantics, it is impossible to put the study of some important issues on a firm foundation. This includes the correctness of ASP programs, grounders, solvers, and optimization methods, and also the relationship between input languages of different solvers (for instance, the equivalence of the semantics of aggregate expressions in Gringo to their semantics in the ASP Core language and in the language proposed in [5] under the assumption that aggregates are used nonrecursively). As future work, we are interested in addressing some of these tasks on the basis of the semantics proposed in this note. Proving the correctness of the intelligent instantiation algorithms implemented in gringo will provide justification for our informal claim that for a safe program, the semantics proposed here correctly describes the output produced by gringo.
Acknowledgements
Many thanks to Roland Kaminski and Torsten Schaub for helping us understand the input language of gringo. Roland, Michael Gelfond, Yuliya Lierler, Joohyung Lee, and anonymous referees provided valuable comments on drafts of this note.
References
 [1] Ferraris, P.: Answer sets for propositional theories. In: Proceedings of International Conference on Logic Programming and Nonmonotonic Reasoning (LPNMR). pp. 119–131 (2005)
 [2] Ferraris, P., Lee, J., Lifschitz, V.: Stable models and circumscription. Artificial Intelligence 175, 236–263 (2011)
 [3] Ferraris, P., Lifschitz, V.: Weight constraints as nested expressions. Theory and Practice of Logic Programming 5, 45–74 (2005)
 [4] Gebser, M., Kaminski, R., Kaufmann, B., Schaub, T.: Answer Set Solving in Practice. Synthesis Lectures on Artificial Intelligence and Machine Learning, Morgan and Claypool Publishers (2012)
 [5] Gelfond, M.: Representing knowledge in AProlog. Lecture Notes in Computer Science 2408, 413–451 (2002)
 [6] Gelfond, M., Lifschitz, V.: The stable model semantics for logic programming. In: Kowalski, R., Bowen, K. (eds.) Proceedings of International Logic Programming Conference and Symposium. pp. 1070–1080. MIT Press (1988)
 [7] Gelfond, M., Lifschitz, V.: Classical negation in logic programs and disjunctive databases. New Generation Computing 9, 365–385 (1991)
 [8] Harrison, A., Lifschitz, V., Truszczynski, M.: On equivalent transformations of infinitary formulas under the stable model semantics (preliminary report)^{9}^{9}9http://www.cs.utexas.edu/users/vl/papers/etinf.pdf. In: Proceedings of International Conference on Logic Programming and Nonmonotonic Reasoning (LPNMR) (2013), to appear
 [9] Lee, J., Meng, Y.: Stable models of formulas with generalized quantifiers. In: Working Notes of the 14th International Workshop on NonMonotonic Reasoning (NMR) (2012)
 [10] Lee, J., Meng, Y.: Stable models of formulas with generalized quantifiers (preliminary report). In: Technical Communications of the 28th International Conference on Logic Programming (ICLP). pp. 61–71 (2012)
 [11] Lee, J., Meng, Y.: Two new definitions of stable models of logic programs with generalized quantifiers. In: Working Notes of the 5th Workshop of Answer Set Programming and Other Computing Paradigms (ASPOCP) (2012)
 [12] Pearce, D., Valverde, A.: Towards a first order equilibrium logic for nonmonotonic reasoning. In: Proceedings of European Conference on Logics in Artificial Intelligence (JELIA). pp. 147–160 (2004)
 [13] Truszczynski, M.: Connecting firstorder ASP and the logic FO(ID) through reducts. In: Correct Reasoning: Essays on LogicBased AI in Honor of Vladimir Lifschitz. Springer (2012)