# 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.

## 1Introduction

In this note, Gringo is the name of the input language of the grounder gringo,^{1}*User’s Guide* dated October 4, 2010,^{2}^{3}

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}

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 first-order logic. For instance, the rule

can be represented by the sentence

However, this translation is not sufficiently general. For instance, it is not clear how to represent the rule

from Section 3.1.10 of the Gringo 3 *User’s Guide* with a first-order formula. One reason is that the aggregate is used here instead of . The second difficulty is that the variable is used rather than a constant.

General aggregate expressions, as used in Gringo, can be represented by first-order formulas with generalized quantifiers.^{5}

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 `==`

.

## 2Syntax

We begin with a signature in the sense of first-order 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 , , and 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}

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 and 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 is a unary predicate symbol and is a binary predicate symbol then

is an aggregate expression.

A *rule* is an expression of the form

(), 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 ,

is a program.

## 3Semantics

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.1Review: 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.2Semantics of Well-Formed Ground Literals

A term **t** is *well-formed* 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 well-formed; is not well-formed. The definition of “well-formed” for literals, aggregate expressions, and so forth is the same.

For every well-formed 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 well-formed 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.3Global 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 (Equation 3), if it is global in at least one of the expressions , .

For instance, the head of the rule (Equation 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 well-formed closed rule that can be obtained from by substituting precomputed terms for global variables. For instance,

is an instance of rule (Equation 2). It is clear that if a rule is not well-formed then it has no instances.

### 3.4Semantics 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 well-formed. 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.5Semantics of Closed Aggregate Expressions

In this section, the semantics of ground aggregates proposed in [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 well-formed. About a set of admissible tuples we say that it *justifies* if the relation holds between and . For instance, consider the aggregate expression

In this case, admissible tuples are arbitrary pairs of precomputed terms. The set justifies (Equation 5), because

More generally, a set of pairs of precomputed terms justifies (Equation 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

over all sets of admissible tuples that do not justify , where is the set of all admissible tuples. For instance, if is (Equation 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.6Semantics of Rules and Programs

For any rule , stands for the conjunction of the formulas

for all instances (Equation 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 (Equation 4). If is the rule then is

(). 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 well-formed.) Consequently is in this case the infinite conjunction

The stable models of program (Equation 4) are the stable models of formulas (Equation 7), (Equation 8), that is, sets of the form for all sum-free subsets of .

## 4Reasoning 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.1Simplifying a Rule from Example 3.7 of User’s Guide

Consider the rule^{7}

Replacing this rule with the fact within any program will not affect the set of stable models. Indeed, the result of applying translation to (Equation 9) is the formula

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 (Equation 10) is equivalent to in the basic system. By the main theorem of [8], it follows that replacing (Equation 10) with the atom within any set of formulas does not affect the set of stable models.

### 4.2Simplifying the Sorting Rule

The rule

can be used for sorting.^{8}

Let’s prove this claim for rule (Equation 12). By the main theorem of [8] it is sufficient to show that the result of applying to (Equation 11) is equivalent in the basic system to the result of applying to (Equation 12). The instances of (Equation 11) are the rules

and the instances of (Equation 12) are the rules

where and are arbitrary numerals. The result of applying to (Equation 11) is the conjunction of the formulas

for all numerals . The result of applying to (Equation 12) is the conjunction of the formulas

By the replacement property of the basic system, it is sufficient to observe that

is intuitionistically equivalent to

The proof for rule (Equation 13) is similar. Rule (Equation 12), like rule (Equation 11), is safe; rule (Equation 13) is not.

### 4.3Eliminating Choice in Favor of a Conditional Literal

Replacing the rule

with

within any program will not affect the set of stable models. Indeed, the result of applying translation to (Equation 14) is

where the conjunction extends over all precomputed terms , and the result of applying to (Equation 15) is

The implication from (Equation 16) is equivalent to the implication from (Equation 17) in the extension of intuitionistic logic obtained by adding the axiom schema

and consequently in the extended system presented in [8]. By the replacement property of the extended system, it follows that (Equation 16) is equivalent to (Equation 17) in the extended system as well.

### 4.4Eliminating a Trivial Aggregate Expression

The rule (Equation 1) says, informally speaking, that we can conclude once we established that there exists at least one such that . Replacing this rule with

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 (Equation 1). The instances of (Equation 1) are the rules

for all precomputed terms . Consider the aggregate expression in the body of (Equation 19). Any precomputed term is admissible w.r.t. . A set of precomputed terms justifies if

that is to say, if is non-empty. Consequently consists of only one implication (Equation 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 (Equation 1) is

On the other hand, the result of applying to (Equation 18) is

This formula is equivalent to (Equation 20) in the basic system [8].

### 4.5Replacing an Aggregate Expression with a Conditional Literal

Informally speaking, the rule

says that we can conclude once we have established that the cardinality of the set is ; the rule

says that we can conclude once we have established that does not hold for any . We’ll prove that replacing (Equation 21) with (Equation 22) within any program will not affect the set of stable models. To this end, we’ll show that the results of applying to (Equation 21) and (Equation 22) are equivalent to each other in the extended system from [8].

First, we’ll need to calculate the result of applying to rule (Equation 21). Consider the aggregate expression in the body of (Equation 21). 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

for all non-empty subsets of the set of precomputed terms. The result of applying to (Equation 21) is

The result of applying to (Equation 22), on the other hand, is

The fact that the antecedents of (Equation 24) and (Equation 25) are equivalent to each other in the extended system can be established by essentially the same argument as in [8]. By the replacement property of the extended system, it follows that (Equation 24) is equivalent to (Equation 25) in the extended system as well.

### 4.6Eliminating Summation over the Empty Set

Informally speaking, the rule

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

replacing (Equation 26) with the fact will not affect the stable models.

To see this, first we calculate the result of applying to rule (Equation 26). Consider the aggregate expression in the body of (Equation 26). 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

for subsets of the set of precomputed terms that contain at least one positive integer. The result of applying to (Equation 26) is

The result of applying to (Equation 27), on the other hand, is

For every nonempty , the antecedent of (Equation 28) contradicts (Equation 30). Consequently, the antecedent of (Equation 29) can be derived from (Equation 30) in the basic system. It follows that the equivalence between (Equation 29) and the atom can be derived in the basic system under assumption (Equation 30).

## 5Conclusion

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.

### Footnotes

`http`

://potassco.sourceforge.net/.- The
*User’s Guide*can be downloaded from the Potassco website (Footnote ?). It is posted also at`http://www.cs.utexas.edu/users/vl/teaching/lbai/clingo_ guide.pdf`

. - The 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].)
- Stable models of formulas with generalized quantifiers are defined by Lee and Meng [9][10][11].
- To 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.
- This rule is similar to a rule from Example 3.7 of the Gringo 3
*User’s Guide*(see Footnote ?). - This rule was communicated to us by Roland Kaminski on October 21, 2012.

### References

- Ferraris, P.: Answer sets for propositional theories. In: Proceedings of International Conference on Logic Programming and Nonmonotonic Reasoning (LPNMR). pp. 119–131 (2005)
- Ferraris, P., Lee, J., Lifschitz, V.: Stable models and circumscription. Artificial Intelligence 175, 236–263 (2011)
- Ferraris, P., Lifschitz, V.: Weight constraints as nested expressions. Theory and Practice of Logic Programming 5, 45–74 (2005)
- 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)
- Gelfond, M.: Representing knowledge in A-Prolog. Lecture Notes in Computer Science 2408, 413–451 (2002)
- 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)
- Gelfond, M., Lifschitz, V.: Classical negation in logic programs and disjunctive databases. New Generation Computing 9, 365–385 (1991)
- Harrison, A., Lifschitz, V., Truszczynski, M.: On equivalent transformations of infinitary formulas under the stable model semantics (preliminary report)
^{1}. In: Proceedings of International Conference on Logic Programming and Nonmonotonic Reasoning (LPNMR) (2013), to appear - Lee, J., Meng, Y.: Stable models of formulas with generalized quantifiers. In: Working Notes of the 14th International Workshop on Non-Monotonic Reasoning (NMR) (2012)
- 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)
- 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)
- 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)
- Truszczynski, M.: Connecting first-order ASP and the logic FO(ID) through reducts. In: Correct Reasoning: Essays on Logic-Based AI in Honor of Vladimir Lifschitz. Springer (2012)