anthem: Transforming gringo Programs into First-Order Theories (Preliminary Report)

09/30/2018 ∙ by Vladimir Lifschitz, et al. ∙ The University of Texas at Austin Universität Potsdam 0

In a recent paper by Harrison et al., the concept of program completion is extended to a large class of programs in the input language of the ASP grounder gringo. We would like to automate the process of generating and simplifying completion formulas for programs in that language, because examining the output produced by this kind of software may help programmers to see more clearly what their program does and to what degree its set of stable models conforms with their intentions. If a formal specification for the program is available, then it may be possible to use this software, in combination with automated reasoning tools, to verify that the program is correct. This note is a preliminary report on a project motivated by this idea.



There are no comments yet.


page 1

page 2

page 3

page 4

This week in AI

Get the week's most popular data science and artificial intelligence research sent straight to your inbox every Saturday.

1 Introduction

Harrison et al. 2017 extended the concept of program completion Clark (1978) to a large class of nondisjunctive programs in the input language of the ASP grounder gringo Gebser et al. (2012). They argued that it would be useful to automate the process of generating and simplifying completion formulas for (tight111Tightness

is a syntactic condition that guarantees the equivalence between the stable model semantics and the completion semantics of a logic program 

Fages (1994); Erdem and Lifschitz (2003).) gringo programs, because examining the output produced by this kind of software may help programmers to see more clearly what their program does and to what degree its set of stable models conforms with their intentions. Furthermore, if a formal specification for a gringo program is available, then it may be possible to use this software, in combination with automated reasoning tools, to verify that the program is correct.

This note is a preliminary report on a software development project that follows up on this idea. anthem is a translator that converts a gringo program into its completion and simplifies it. By simplifying we mean, in this case, not so much making formulas shorter as writing them in a form that is “readable”—natural from the perspective of a human who is accustomed to expressing mathematical ideas using propositional connectives, quantifiers, variables for objects of various types, the summation symbol, and other standard notation. The language of gringo and many other input languages of answer set solvers, including those of smodels Niemelä and Simons (1997) and dlv Leone et al. (2006)

, classify variables into global and local, instead of using quantifiers to classify occurrences into free and bound, and that distinguishes them from traditional notation. The same can be said about assuming that all variables range over the same universe, instead of using variables of different sorts or types (for points, lines, and planes; for integers and real numbers; or for sets and classes; etc.).

222Answer set programming with sorts Balai et al. (2013) is an exception. What these authors proposed, however, is assigning sorts to argument positions but not assigning them to variables. This is different from what is customary in logic and in procedural programming languages. Each of the two notational traditions has its advantages, and anthem provides a bridge between them.

Simplifying a logical formula in the sense of making it more understandable is not the same thing, of course, as rewriting it using fewer characters. For instance, the equivalent formulas


use the same number of logical symbols, but the latter is much easier to understand.

Besides generating and simplifying the completion of a program, anthem “hides” auxiliary predicate symbols occurring in the program when possible. In the language of gringo, the fact that a predicate symbol is not considered an essential part of the output can be expressed by not including it in #show directives. To eliminate such predicate symbols from its output, anthem replaces them by their completed definitions.

The input language of anthem is a large part of the input language of gringo. Input programs are supposed to be nondisjunctive. They may use arithmetic operations, intervals, comparisons, singleton choice rules without lower and upper bounds, and constraints. Aggregates and conditional literals are not supported in the current version.

The output of anthem is a list of first-order formulas with variables of two sorts—for arbitrary precomputed terms (that is, for all elements of the Herbrand universe) and for the precomputed terms that correspond to integers—as proposed by Harrison et al. 2017, Sections 3 and 9. Differences between atoms in gringo programs and atomic parts of formulas are related mostly to arithmetic expressions (see Section 3 below).

The GitHub repository of anthem contains the source code, installation steps, usage instructions, as well as multiple examples to experiment with.333

2 Examples

Example 1. Given the input file

  s(X) :- p(X).
  s(X) :- q(X).
  #external p(1).
  #external q(1).

anthem generates the formula

  forall V1 (s(V1) <-> (p(V1) or q(V1)))

The first two lines of the input file express the condition

in the language of logic programming. The last two lines tell us that the unary predicates p and q are not defined in this file; they can be specified separately.444This is slight abuse of the syntax of #external directives, since a predicate’s arity is indicated by its argument. (Without those lines in the input, anthem would have decided that p and q are identically false.) The output of anthem, shown above, expresses the same condition by a first-order formula.

Example 2. The condition

can be represented by the gringo program

  s(X) :- p(X).
  s(X) :- q(X).
  u(X) :- r(X), not s(X).
  #show u/1.
  #external p(1).
  #external q(1).
  #external r(1).

anthem converts this program into the formula

  forall V1 (u(V1) <-> (r(V1) and not (p(V1) or q(V1))))

which expresses the same condition in traditional logical notation. It is obtained from the completed definition

  forall V1 (u(V1) <-> (r(V1) and not s(V1)))

of u by replacing s with its completed definition.

Example 3. The rules

  p(a).  {q(a)}.

express—assuming that p and q do not occur in the heads of any other rules—that and . The formulas

  forall V1 (p(V1) <-> V1 = a)
  forall V2 (q(V2) -> V2 = a)

generated by anthem in response to this input express the same conditions in traditional notation.

Example 4. The vertex coloring program

  % assign a set of colors to each vertex
  {color(V, C)} :- vertex(V), color(C).
  % at most one color per vertex
  :- color(V, C1), color(V, C2), C1 != C2.
  % at least one color per vertex
  colored(V) :- color(V, _).
  :- vertex(V), not colored(V).
  % adjacent vertices don’t share the same color
  :- color(V1, C), color(V2, C), edge(V1, V2).
  #show color/2.
  #external vertex(1).
  #external edge(2).
  #external color(1).

is transformed by anthem into

  forall V1, V2 (color(V1, V2) -> (vertex(V1) and color(V2)))
  forall U1, U2, U3 (not color(U1, U2) or not color(U1, U3) or U2 = U3)
  forall U4 (vertex(U4) -> exists U5 color(U4, U5))
  forall U6, U7, U8 (not color(U6, U7) or not color(U8, U7)
    or not edge(U6, U8))

The second of these formulas will look more natural if we rewrite it as

  forall U1, U2, U3 ((color(U1, U2) and color(U1, U3)) -> U2 = U3)

and the last of them can be improved by writing it in the form

  not exists U6, U7, U8 (color(U6, U7) and color(U8, U7)
    and edge(U6, U8)).

These simplifications will be implemented in a future version of anthem.

3 Arithmetic Expressions in Formulas

In the output of anthem, an integer variable can be recognized by its first character—the letter N. For instance, the formula

exists N p(N)

is stronger than

exists X p(X)

—it expresses that the set p contains at least one integer (and not only ground terms formed using symbolic constants).

In the language of gringo, a ground term represents, generally, a finite set of values, rather than a single value (Harrison et al., 2017, Section A.1). For example, the term 1 + 3 has a single value 4, but 1..3 has the values 1, 2, 3. The set of values of 3..1 is empty, and so is the set of values of 3 / (1 - 1). Accordingly, an atomic formula can be formed from two terms using the symbol in, expressing set membership, for instance:

The first of these formulas is equivalent to X = 4; the second, to

the last two, to #false.

On the other hand, intervals are allowed in a formula in only one position—to the right of the symbol in. For instance, the atom p(1..3), which can be used in gringo rules, is not a formula; in the world of formulas, we distinguish between

exists X (X in 1..3 and p(X))


The use of arithmetic operations in formulas is restricted in the same way, except for terms formed from integer variables using addition, subtraction, and multiplication. Such terms can be used in a formula anywhere. This exception is motivated by the fact that any ground term of this type has a unique value. For instance, p(N + 1) is a formula, but p(X + 1) is not. To express that

we write

In the output of anthem, an expression of the form int(p/n@k), where is a symbolic constant and , are integers such that , stands for the formula

which expresses that the -th member of any -tuple satisfying is an integer.

4 Examples Involving Arithmetic Expressions

Example 5. The program

  letter(a).  letter(b).  letter(c).
  {p(1..3, Y)} :- letter(Y).
  :- p(X1, Y), p(X2, Y), X1 != X2.
  q(X) :- p(X, _).
  :- X = 1..3, not q(X).
  #show p/2.

encodes the set of permutations of the letters , , . anthem transforms it into

  forall N1, V1 (p(N1, V1) -> (N1 in 1..3 and (V1 = a or V1 = b
    or V1 = c)))
  forall N2, U1, N3 (not p(N2, U1) or not p(N3, U1) or N2 = N3)
  forall N4 (N4 in 1..3 -> exists U2 p(N4, U2))

Example 6. The program

  composite(I * J) :- I = 2..n, J = 2..n.
  prime(N) :- N = 2..n, not composite(N).
  #show prime/1.

encodes the set of primes up to . anthem turns it into

  forall N1 (prime(N1) <-> (N1 in 2..n and not exists N2, N3
    (N1 = (N2 * N3) and N2 in 2..n and N3 in 2..n)))

Example 7. The program

  {in(1..n, 1..r)}.
  covered(I) :- in(I, _).
  :- I = 1..n, not covered(I).
  :- in(I, S), in(J, S), in(I + J, S).
  #show in/2.

encodes partitions of into  sum-free sets. anthem turns it into

  forall N1, N2 (in(N1, N2) -> (N1 in 1..n and N2 in 1..r))
  forall N3 (N3 in 1..n -> exists N4 in(N3, N4))
  forall N5, N6, N7 (not in(N5, N6) or not in(N7, N6)
    or not in((N5 + N7), N6))

5 Implementation

The implementation of anthem takes advantage of gringo’s library functionality for accessing the abstract syntax tree (AST) of a nonground program. The AST obtained from gringo is taken by anthem and turned into the AST of the collection of formulas representing the rules of the program (Harrison et al., 2017, Section 4). That tree is then turned into the AST of the program’s completion. Auxiliary predicates are eliminated, and the result is subject to several simplifications, including those that involve the use of integer variables. In Example 5, for instance, the formula

  forall V2, V3 (p(V2, V3) -> (V2 in 1..3 and letter(V3)))

is replaced at the last stage by the pair of formulas

  forall N1, V2 (p(N1, V2) -> (N1 in 1..3 and letter(V2))),

The final result is pretty-printed to standard output.

In the process of eliminating auxiliary predicates by replacing them with their definitions, a cycle check is employed to detect the cases when this process would not terminate. For example, in response to the input

  p :- not q.
  q :- not p.
  r :- not p.
  #show r/0.

anthem displays a warning:

Apart from the standard options (for help, version, etc.), anthem allows us to switch on/off completion, simplification, and introducing integer variables from the command line.

6 Future Work

Future work on anthem will proceed in two main directions. First, we would like to support aggregates and conditional literals. When this is accomplished, we will be able to replace, for instance, the first four rules of Example 4 by a single rule

  1 {color(V, C) : color(C)} 1 :- vertex(V).

With aggregates added to the input language of anthem, formulas in its output will have more complex syntactic structure (Harrison et al., 2017, Section 8.2).

Second, we will investigate the possibility of using automated reasoning tools for classical logic, in combination with anthem, for verifying programs written in the input language of gringo. For instance, the specification for the program from Example 6—encode the set of primes up to —can be expressed by the formula

  forall N1 (prime(N1) <-> (1 < N1 <= n and not exists N2, N3
    (N1 = N2 * N3 and N2 > 1 and N3 > 1))).

It is easy to show that this formula is equivalent to the first formula in the output of anthem using first-order logic, simple properties of inequalities, and the meaning of the interval notation. We expect that it will be possible to verify claims of this kind using a proof checker.


Thanks to Amelia Harrison and to the anonymous referees for comments on a draft of this note. The first author was partially supported by the National Science Foundation under Grant IIS-1422455.


  • Balai et al. (2013) Balai, E., Gelfond, M., and Zhang, Y. 2013. Towards Answer Set Programming with Sorts. In Proceedings of International Conference on Logic Programming and Nonmonotonic Reasoning (LPNMR). 135–147.
  • Clark (1978) Clark, K. 1978. Negation as Failure. In Logic and Data Bases, H. Gallaire and J. Minker, Eds. Plenum Press, New York, 293–322.
  • Erdem and Lifschitz (2003) Erdem, E. and Lifschitz, V. 2003. Tight Logic Programs. Theory and Practice of Logic Programming 3, 499–518.
  • Fages (1994) Fages, F. 1994. Consistency of Clark’s Completion and Existence of Stable Models. Journal of Methods of Logic in Computer Science 1, 51–60.
  • Gebser et al. (2012) Gebser, M., Kaminski, R., Kaufmann, B., and Schaub, T. 2012. Answer Set Solving in Practice.

    Synthesis Lectures on Artificial Intelligence and Machine Learning. Morgan and Claypool Publishers.

  • Harrison et al. (2017) Harrison, A., Lifschitz, V., and Raju, D. 2017. Program Completion in the Input Language of gringo. Theory and Practice of Logic Programming 17, 5–6, 855–871.
  • Leone et al. (2006) Leone, N., Pfeifer, G., Faber, W., Eiter, T., Gottlob, G., Perri, S., and Scarcello, F. 2006. The DLV System for Knowledge Representation and Reasoning. ACM Transactions on Computational Logic 7, 3, 499–562.
  • Niemelä and Simons (1997) Niemelä, I. and Simons, P. 1997. Smodels—An Implementation of the Stable Model and Well-Founded Semantics for Normal Logic Programs. In Proceedings 4th International Conference on Logic Programming and Nonmonotonic Reasoning. Springer, 420–429.