Essential Gringo (Draft) Vladimir Lifschitz, University of Texas 1 Introduction The designers of the Abstract Gringo language AG [Gebser et al., 2015a] aimed at creating a relatively simple mathematical model of the input language of the grounder gringo [Gebser et al., 2015b] that includes as many gringo constructs as possible, including even those that are not essential not particularly useful to the programmer. The Essential Gringo language EG, described in this note, is a simplified, less ambitious version of AG. For example, gringo rules can use double negations : p :- q, not not r. This construct can be easily avoided; instead of the rule above we can write p ; not r :- q. In EG, double negations are not allowed. An smodels-style weight constraint 1 {p(x)} 1 in the body of a gringo rule can be also written as the dlv-style aggregate atom #count{x:p(x)} = 1. In EG, weight constraints in the bodies of rules are not allowed. The expression not #count{x:p(x)} = 1 in the body can be rewritten as #count{x:p(x)}!= 1. The syntax of EG does not allow us to negate aggregate atoms, so that the first version is not available. Classical ( strong ) negation, which is used in applications of ASP to knowledge representation [Gelfond and Kahl, 2014], is reflected in AG by distinguishing between symbolic constants and negated constants. Classical negation can be 1
easily eliminated from a program [Gelfond and Lifschitz, 1991, Section 4]; it is valuable as part of the methodology of knowledge representation but not so much as a syntactic construct. There are no negated constants in EG. EG is a subset of AG, except for a few details. We correct here a minor oversight in the design of AG that is related to arithmetic operations. (The list of operations in Section 2.1 of the AG paper does not include the absolute value, exponentiation, and some other operations implemented in the current version of gringo. In EG, an arbitrary collection of arithmetic operations is allowed.) The semantics of EG takes into account the fact that a logic program can accept an input the observation that led to the concept of an lp-function [Gelfond and Przymusinska, 1996]. In EG, an input can include input predicates and values of placeholders. 2 Syntax of EG We assume that five disjoint sets of symbols are selected: numerals, symbolic constants, variables, operation names of various arities, and aggregate names. We assume that these sets do not contain the interval symbol the relation symbols and the symbols.. = < > inf sup not, ; : ( ) { } All these symbols together form the alphabet of AG, and AG rules will be defined as strings over this alphabet. Terms are defined recursively, as follows: numerals, symbolic constants, variables, and the symbols inf and sup are terms; if f is a symbolic constant and t is a non-empty tuple of terms (separated by commas) then f(t) is a term; if op is an n-ary operation name and t is an n-tuple of terms then op(t) is a term; if t and s are terms then (t..s) is a term. 2
Atoms are expressions of the form p(t), where p is a symbolic constant and t is tuple of terms, possibly empty. An atom of the form p() can be written as p. A literal is an atom possibly preceded by not. A comparison is an expression of the form t s where t, s are terms and is a relation symbol. An aggregate expression is an expression of the form where where α is an aggregate name, t is a non-empty tuple of terms, α{t : C} s (1) C is a non-empty conjunction of literals and comparisons, is a relation symbol. s is a term. A choice expression is an expression of the form {A} where A is an atom. A rule is an expression of the form Head Body (2) Body is a conjunction of literals, comparisons, and aggregate expressions (possibly empty), and Head is either a disjunction of literals, possibly empty (then we say that (2) is a basic rule) or a choice expression (then (2) is a choice rule). If the body of a rule is empty and the head is not then the arrow can be dropped. A program is a set of rules. 3 Semantic Assumptions We assume that a 1 1 correspondence between the set of numerals and the set Z of integers is chosen. For every integer n, the corresponding numeral will be denoted by n. We will identify a numeral with the corresponding integer when this does not lead to confusion. We assume that for every operation name op, a function ôp from a subset of Z n to Z is chosen, where n is the arity of op. For instance, we can choose plus as 3
a binary operation name, define plus as the addition of integers, and use t + s as shorthand for plus(t, s). A term, or a tuple of terms, is precomputed if variables, operation names, and the interval symbol do not occur in it. We assume a total order on precomputed terms such that inf is its least element, sup is its greatest element, and, for any integers m and n, m n iff m n. We assume that for each aggregate name α a function α is chosen that maps every set of non-empty tuples of precomputed terms to a precomputed term. Two aggregate names are particularly useful. One is count; ĉount(t ) is defined as the cardinality of T if T is finite, and sup otherwise. The other is sum; ŝum(t ) is the sum of the weights of all tuples in T if T contains finitely many tuples with non-zero weights, and 0 otherwise. (The weight of a tuple t of precomputed terms is defined as the first member of t if it is a numeral, and 0 otherwise.) 4 Abbreviations An expression of the form p(t 1 ;... ; t m ) Body where m > 1, each t i is a non-empty tuple of terms, and Body is as in the definition of a rule, stands for the group of m rules p(t i ) Body (i = 1,..., m). An expression of the form s 1 {A : C} s 2 Body (3) where s 1 and s 2 are terms, A is an atom that does not contain the interval symbol, C is a non-empty conjunction of literals and comparisons, Body is as in the definition of a rule stands for the group of three rules: {A} Body C, (4) Body count{a : A C} < s 1, (5) Body count{a : A C} > s 2. (6) In (3), s 1 and s 2 are optional. If s 1 is missing then drop rule (5); if s 2 is missing then drop rule (6). 4
5 Semantics The semantics of EG is defined using a syntactic transformation τ, which turns programs into sets of infinitary formulas formed from atoms of the form p(t), where p is a symbolic constant, and t is a tuple of precomputed terms. Then the stable models of a program Π are defined as the stable models of τπ in the sense of [Truszczynski, 2012]. 5.1 Ground Literals and Comparisons A term, literal, or comparison is ground if it does not contain variables. In this section we define how to apply τ to ground literals and comparisons. Semantically, every ground term t represents a finite set of precomputed terms [t], which is defined recursively: if t is a numeral, a symbolic constant, or one of the symbols inf, sup then [t] is the singleton {t}; if t is f(t 1,..., t n ) where f is a symbolic constant then [t] consists of the terms f(r 1,..., r n ) for all r 1 [t 1 ],..., r n [t n ]; if t is op(t 1,..., t n ) where op is an operation name then [t] consists of the numerals ôp(k 1,..., k n ) for all tuples k 1,..., k n in the domain of ôp such that k 1 [t 1 ],..., k n [t n ]; if t is (t 1.. t 2 ) then [t] consists of the numerals m for all integers m such that, for some integers k 1, k 2, For any ground atom p(t 1,..., t n ), k 1 [t 1 ], k 2 [t 2 ], k 1 m k 2. by [p(t 1,..., t n )] we denote the set consisting of the atoms p(r 1,..., r n ) for all r 1 [t 1 ],..., r n [t n ]; τp(t 1,..., t n ) stands for the disjunction of these atoms; by [not p(t 1,..., t n )] we denote the set consisting of the formulas p(r 1,..., r n ) for all r 1 [t 1 ],..., r n [t n ]; τ(not p(t 1,..., t n )) stands for the disjunction of these formulas. For any ground comparison t 1 t 2, τ(t 1 t 2 ) is the formula if the relation holds between some terms r 1, r 2 such that r 1 [t 1 ] and r 2 [t 2 ], and the formula otherwise. If each of the expressions C 1,..., C k (k > 1) is a ground literal or a ground comparison then τ(c 1 C k ) stands for τc 1 τc k. 5
5.2 Closed Aggregate Expressions An aggregate expression (1) is closed if the term s is ground. In this section we define how to apply τ to closed aggregate expressions. Let x be the list of variables occurring in the left-hand side α{t : C} of a closed aggregate expression (1), and let A be the set of tuples r of precomputed terms of the same length as x. A subset of A justifies the aggregate expression (1) if the relation holds between α({[t x r ] : r }) and an element of the set [s]. We define the result of applying τ to (1) as the conjunction of the implications τ(c x r ) τ(c x r ) r r A\ over all subsets of A that do not justify E. The definition of τ for conjunctions of ground literals and ground comparisons at the end of Section 5.1 extends in the obvious way to the case when some conjunctive members are closed aggrerate expressions. 5.3 Rules and Programs A variable V occurring in a rule R is local if every occurrence of V in R belongs to the left-hand side α{t : C} of one of the aggregate expressions (1) in its body, and global otherwise. A rule is closed if all its variables are local. It is clear that if a rule is closed then all aggregate expressions in its body are closed. If R is a closed basic rule then τr is the formula L 1 L k Body τ(body) [L 1 ] [L k ]. (Recall that S is the conjunction of all elements of a set S of formulas [Truszczynski, 2012].) If R is a closed choice rule then τr is the formula τ(body) {p(t 1,..., t n )} Body r 1 [t 1 ],...,r n [t n] (p(r 1,..., r n ) p(r 1,..., r n )). An instance of a rule R is a closed rule obtained from R by substituting precomputed terms for its global variables. For any program Π, τπ is the set consisting of all instances R of rules of Π. 6
6 Inputs The predicate symbol of an atom p(t 1,..., t n ) is the pair p/n. An input is a function i defined on a finite set consisting of predicate symbols and symbolic constants such that if p/n is a predicate symbol in the domain of i then i(p/n) is a finite set of n-tuples of precomputed terms; if c is a symbolic constant in the domain of i then i(c) is a precomputed term. Occurrences of symbolic constants in a rule can be classified into three groups. We say that an occurrence of a symbolic constant is in the predicate position if it is at the beginning of an atom, and in the function position if it is at the beginning of a term of the form f(t). All other occurrences are in the argument position. 1 For any program Π and input i, by i(π) we denote the program obtained from Π by adding the facts p(t) for all predicate symbols p/n in the domain of i and all tuples t in i(p/n), and simultaneously substituting the terms i(c) for all occurrences of symbolic constants c in the domain of i that are in the argument position. Stable models of i(π) will be also called stable models of Π for input i, or i-stable models of Π. To use gringo for generating i-stable models of a program, we append to it the facts representing the values that i assigns to predicate symbols, and specify the terms that i assigns to symbolic constants by #const directives. In rare cases this may fail to produce the correct result because gringo executes #const directives sequentially, not simultaneously. 1 This distinction is essential for understanding how the #const directive of gringo [Gebser et al., 2015b, Section 3.1.15] operates: it replaces an occurence of a placeholder only if it is in the argument position. For instance, the directives #const a=1. #const b=2. #const c=3. turn rule a(b(c)) b(c(a)) into a(b(3)) b(c(1)). 7
References [Gebser et al., 2015a] Martin Gebser, Amelia Harrison, Roland Kaminski, Vladimir Lifschitz, and Torsten Schaub. Abstract Gringo. Theory and Practice of Logic Programming, 15:449 463, 2015. [Gebser et al., 2015b] Martin Gebser, Roland Kaminski, Benjamin Kaufmann, Marius Lindauer, Max Ostrowski, Javier Romero, Torsten Schaub, and Sven Thiele. Potassco User Guide, version 2.0. Available at http://potassco.sourceforge.net, 2015. [Gelfond and Kahl, 2014] Michael Gelfond and Yulia Kahl. Knowledge Representation, Reasoning, and the Design of Intelligent Agents: The Answer-Set Programming Approach. Cambridge University Press, 2014. [Gelfond and Lifschitz, 1991] Michael Gelfond and Vladimir Lifschitz. Classical negation in logic programs and disjunctive databases. New Generation Computing, 9:365 385, 1991. [Gelfond and Przymusinska, 1996] Michael Gelfond and Halina Przymusinska. Towards a theory of elaboration tolerance: Logic programming approach. International Journal of Software Engineering and Knowledge Engineering, 6(1):89 112, 1996. [Truszczynski, 2012] Miroslaw Truszczynski. Connecting first-order ASP and the logic FO(ID) through reducts. In Esra Erdem, Joohyung Lee, Yuliya Lierler, and David Pearce, editors, Correct Reasoning: Essays on Logic-Based AI in Honor of Vladimir Lifschitz, pages 543 559. Springer, 2012. 8