On Program Completion, with an Application to the Sum and Product Puzzle

This paper describes a generalization of Clark's completion that is applicable to logic programs containing arithmetic operations and produces syntactically simple, natural looking formulas. If a set of first-order axioms is equivalent to the completion of a program then we may be able to find standard models of these axioms by running an answer set solver. As an example, we apply this"reverse completion"procedure to the Sum and Product Puzzle.


Introduction
Program completion (Clark 1978;Lloyd and Topor 1984) is a transformation that converts logic programs into sets of first-order formulas.The study of completion improved our understanding of the relationship between these two knowledge representation formalisms, and it has been used in the design of answer set solvers (Lierler and Maratea 2004;Lin and Zhao 2004).
The definition of completion has been extended to programs with operations on integers (Fandinno et al. 2020).That generalized completion process produces formulas in a twosorted first-order language (Lifschitz et al. 2019, Section 5).In addition to "general" variables, which range over both symbolic constants and (symbols for) integers, a formula in that language may include also variables ranging over integers only.The need to use a language with two sorts is explained by the fact that function symbols in a first-order language are supposed to represent total functions, and arithmetic operations are not defined on symbolic constants.In answer set programming languages, applying arithmetic operations to symbolic constants is usually handled in a different way; when a rule is instantiated, a substituition is not used unless it is "well-formed" (Calimeri et al. 2020, Section 3).
In this paper, the idea of a natural translation (Lifschitz 2021) is used to define a version of generalized completion that is limited to relatively simple ("regular") rules but produces simpler, and more natural-looking, formulas.The modified completion operator is denoted by NCOMP, for "natural completion."For example, the natural completion of the one-rule program even(2*X) :-X = -10..10. (1) in the input language of the answer set solver clingo (Gebser et al. 2019) is the sentence ∀V (even(V ) ↔ ∃I(−10 ≤ I ≤ 10 ∧ V = 2 * I)). ( Here V is a general variable, I is an integer variable, and −10, 10, 2 are "numerals"object constants representing integers.Two theorems, stated in Section 3.4 and proved in Section 5, relate stable models of a regular program to standard models of its completion (standard in the sense that they interpret symbols related to integers as usual in arithmetic).These theorems extend well-known results due to François Fages (Fages 1991).
If a set of first-order axioms happens to be equivalent to the completion of a regular program then we may be able to find standard models of these axioms by running an answer set solver.As an example, we apply this "reverse completion" procedure to a formalization of the Sum and Product Puzzle (https://en.wikipedia.org/wiki/Sum_and_Product_Puzzle).From the perspective of knowledge representation and automated reasoning, that puzzle presents a challenge: express it in a formal declarative language so that the answer can be found, or at least verified, by an automated reasoning tool.This has been accomplished using first-order axioms for Kripke-style possible worlds and the first-order theorem prover fol (McCarthy 1990), and also using a modal logic of public announcements and the epistemic model checker demo (van Ditmarsch et al. 2005).More recently, Jayadev Misra proposed a simple first-order formalization that does not refer to possible worlds (Misra 2022, Section 2.8.3).In Section 4 we show that the answer to the puzzle can be found by applying the reverse completion process to a variant of his axiom set and then running clingo.
2 Review: rules and formulas

Regular rules
To simplify presentation, we do not include here some of the programming constructs that are classified as regular in the previous publication on natural translations (Lifschitz 2021).As in the Abstract Gringo article (Gebser et al. 2015), rules will be written in abstract notation, which disregards some details related to representing rules by strings of ASCII characters.For example, rule (1) will be written as even(2 × X) ← X = −10 .. 10. (3) We assume that three disjoint countably infinite sets of symbols are selected: numerals, symbolic constants, and (general) variables.We assume that a 1-1 correspondence between numerals and integers is chosen; the numeral corresponding to an integer n is denoted by n.Precomputed terms are numerals and symbolic constants.We assume that a total order on the set of precomputed terms is chosen so that, for all integers m and n, • m < n iff m < n, and • every precomputed term t such that m < t < n is a numeral.
Regular terms are formed from numerals and variables using the binary function symbols +, −, ×.A regular atom is an expression of the form p(t), where p is a symbolic constant and t is a tuple of symbolic constants and regular terms, separated by commas.Regular comparisons are expressions of the forms • t 1 ≺ t 2 , where each of t 1 , t 2 is a symbolic constant or a regular term, and ≺ is one of the comparison symbols =, =, <, >, ≤, ≥, and • t 1 = t 2 .. t 3 , where t 1 , t 2 , t 3 are regular terms.
A regular rule is an expression of the form where • Head is either a regular atom (then (4) is a basic rule), or a regular atom in braces (then (4) is a choice rule), or empty (then (4) is a constraint ), and • Body is a conjunction, possibly empty, of (i) regular atoms, possibly preceded by not , and (ii) regular comparisons.
For example, (3) is a regular rule.
A regular program is a finite set of regular rules.This is a special case of Abstract Gringo programs (Gebser et al. 2015), and stable models of a regular program are understood in the sense of the semantic of Abstract Gringo.Thus stable models are sets of ground atoms that do not contain arithmetic operations.

Two-sorted formulas
A predicate symbol is a pair p/n, where p is a symbolic constant and n is a nonnegative integer.About a predicate symbol p/n we say that it occurs in a regular program Π if some atom of the form p(t 1 , . . ., t n ) occurs in one of the rules of Π.
For any regular program Π, by σ Π we denote the two-sorted signature with the sort general and its subsort integer, which includes • every numeral as an object constant of the sort integer, • every symbolic constant as an object constant of the sort general, • the symbols +, −, × as binary function constants with the argument sorts integer and the value sort integer, • every predicate symbol p/n that occurs in Π as an n-ary predicate constant with the argument sorts general, • the symbols =, <, >, ≤, ≥ as binary predicate constants with the argument sorts general.
A formula over σ Π that has the form (p/n)(t) can be abbreviated as p(t).This convention allows us to view regular atoms occurring in Π as atomic formulas over σ Π .
Conjunctions of equalities and inequalities can be abbreviated as usual in algebra; for instance, In this paper, integer variables are denoted by capital letters from the middle of the alphabet (I, . . ., N ), and general variables by letters from the end (U, . . ., Z).

Replacing variables
In the process of constructing the natural completion of a regular program Π, the bodies of rules of Π will be transformed into formulas over σ Π .Since general variables are not allowed in a formula in the scope of an arithmetic operation, this process has to involve replacing some of them by integer variables.
A critical variable of a regular rule R is a general variable X such that at least one occurrence of X in R is in the scope of an arithmetic operation or is part of a comparison of the form t 1 = t 2 .. t 3 .For every regular rule R, choose a function f R that maps its critical variables to pairwise distinct integer variables.This function f R is extended to other subexpressions of R as follows.For a tuple t of symbolic constants and regular terms, f R (t) is the tuple of terms over σ Π obtained from t by replacing all occurrences of every critical variable X with the integer variable f R (X).Applying f R to a regular atom and to a comparison that does not contain intervals is defined in a similar way.The result of applying f R to not A is defined as the formula ¬f R (A), and the result of applying f R to a comparison For instance, if R is rule (3) then the variable X is critical, and f R maps X to some integer variable I.It transforms the term 2 × X in the head into 2 × I, and the body X = −10 .. 10 into −10 ≤ I ≤ 10.

Completed definitions
Consider a regular program Π and a predicate symbol p/n that occurs in Π.The definition of p/n in Π is the set of all rules of Π that have the form such that the length of the tuple t is n.The completed definition of p/n in Π is the sentence over σ Π constructed as follows.Choose a tuple V of n general variables that do not occur in Π.For every rule R in the definition D of p/n in Π, by F R we denote the formula where U R is the list of all variables occurring in f R (Body ) or in f R (t).For example, if the only rule R of the program is (3), and p/n is even/1, then F R is where I is f R (X).The completed definition of even/1 is (2).The formula obtained from the completed definition (7) by replacing the global variables V with fresh integer variables will be called the arithmetic completed definition of p/n in Π.For example, the arithmetic completed definition of even/1 in program (3) is The arithmetic completed definition is entailed by the completed definition, but not the other way around.For example, from formula (2) we can derive ¬even(t) for every symbolic constant t, but this conclusion is not warranted by formula (8).

Natural completion
The natural completion NCOMP(Π) of a regular program Π is the set of sentences that includes • for every predicate symbol p/n occurring in Π, its completed definition in Π, and • for every constraint ← Body in Π, the universal closure of the formula ¬f ←Body (Body ).
Consider, for example, the program that consists of rule (3), the choice rule and the constraint Its natural completion consists of the completed definition (2) of even/2, the completed definition of foo/1 which can be rewritten1 as and the sentence ¬¬foo(0), which is equivalent to foo(0).

Relation to stable models
Theorem 1 For any regular program Π and any subset S of its Herbrand base, if S is a stable model of Π then S ↑ satisfies NCOMP(Π).
The positive predicate dependency graph of a regular program Π is the directed graph defined as follows.Its vertices are the predicate symbols p/n occurring in Π.It has an edge from p/n to q/m if Π has a rule (4) such that • Head has the form p(t 1 , . . ., t n ) or {p(t 1 , . . ., t n )}, and • one of the conjunctive terms of Body has the form q(t 1 , . . ., t m ).
A regular program Π is tight if its positive predicate dependency graph is acyclic.

Theorem 2
For any tight regular program Π and any subset S of its Herbrand base, S is a stable model of Π iff S ↑ satisfies NCOMP(Π).

The puzzle
Two mathematicians, S and P, talk about two integers, M and N .S knows the sum M + N , and P knows the product M × N .Both S and P know also that the integers are greater than 1; that their sum is not greater than 100; and that N is greater than M .The following conversation occurs: 1. S says: P does not know M and N .2. P says: Now I know M and N .3. S says: Now I also know M and N .
What are M and N ?4.1 First-order axioms Jayadev Misra's approach to translating this puzzle into a first-order language (Misra 2022, Section 2.8.3) involves the use of binary predicates b 0 , . . ., b 3 .The formula b 0 (M, N ) expresses that before the beginning of the conversation the pair M , N was considered a possible solution.This can be expressed by the formula The formula b 1 (M, N ) expresses that M , N was considered a possible solution at step 1, that is, after hearing the words "P does not know M and N "; and so forth.
There are several ways to write axioms for b 1 , b 2 and b 3 .One possibility is described below.
We say that an integer I is puzzling at time 0 if is there is more than one way to represent it as the product of two numbers J, K satisfying b 0 (J, K): We say that an integer I is possibly easy if it can be represented as the sum of two numbers J, K satisfying b 0 (J, K) such that J × K is not puzzling at time 0: Then the assumption "at Step 1, S knows that P does not know M and N " can be expressed by the axiom We say that an integer I is puzzling at time 1 is there is more than one way to represent it as the product of two numbers J, K satisfying b 1 (J, K): The assumption "at Step 2, P knows M and N " can be expressed by the axiom We say that an integer I is puzzling at time 2 is there is more than one way to represent it as the sum of two numbers J, K satisfying b 2 (J, K): Finally, the assumption "at Step 3, S knows M and N " can be expressed by the axiom Since axioms ( 11)-( 18) form a chain of explicit definitions, the predicates represented by the symbols b 0 /2, . . ., b 3 /2, puzzling 0 /1, . . ., puzzling 2 /1, possibly easy/1 (19) are uniquely defined, assuming that variables range over the integers and that the symbols are interpreted in the standard way.To solve the Sum and Product Puzzle, we will calculate the extents of these predicates.This will be accomplished by running clingo on the "reverse completion" of axioms ( 11)-(18).

Reverse completion
Consider the regular program These rules are obtained from equivalences ( 11)-( 18) by • replacing the equivalence signs ↔ by left arrows, • dropping existential quantifiers, • replacing integer variables by general variables, and • replacing ¬ by not .

The natural completion of program (20) looks very similar to axiom set (11)-(18).
There is a difference though: the former consists of formulas over the two-sorted signature described in Section 2.2, and the axioms formalizing the Sum and Product puzzle are onesorted; there are no general variables in them.Consider then the arithmetic completed definitions of predicate symbols (19) (see Section 3.2).Those are one-sorted formulas, and they are equivalent to the universal closures of the corresponding axioms.For example, the completed definition of the predicate symbol b 0 /2 in program ( 20) is and the arithmetic completed definition of this symbol is the one-sorted formula This formula is equivalent to the universal closure of axiom (11).Similarly, the arithmetic completed definition of puzzling 0 /2 is equivalent to the universal closure of axiom ( 12), and so forth.

Calculating the answer
By running clingo we can determine that program (20) has a unique stable model S. By Theorem 1, the interpretation S ↑ satisfies the completed definitions of symbols ( 19).It follows that S ↑ satisfies the arithmetic completed definitions of these symbols, which are equivalent to axioms ( 11)-( 18).In other words, S describes the extents of the predicates that we want to calculate.Since the only atom in S that begins with b 3 is b 3 (4, 13), the answer to the puzzle is To perform this calculation, we used version 5.6.0 of clingo.Earlier versions do not accept the first rule of the program as safe unless the expression in the body is rewritten in the interval notation: M = 2 .. N − 1.

Proofs
Proofs of Theorems 1 and 2 are based on similar results from an earlier publication (Fandinno et al. 2020), and we begin by reviewing them for the special case of regular programs.

Review: Completion according to Fandinno et al.
For any regular term t, the formula val t (Z), where Z is a general variable that does not occur in t, is defined recursively: and similarly for t 1 − t 2 and t 1 × t 2 .
If t is a symbolic constant then val t (Z) stands for Z = t.If t 1 , . . ., t k is a tuple of symbolic constants and regular terms, and Z 1 , . . ., Z k are pairwise distinct general variables that do not occur in t 1 , . . ., t k , then val t1,...,t k (Z 1 , . . ., Z k ) stands for val The translation τ B transforms expressions in the body of a regular rule into formulas as follows: The completed definition of p/n in Π in the sense of Fandinno et al. is the sentence over σ Π constructed as follows.Choose a tuple V of n general variables that do not occur in Π.For every rule R in the definition D of p/n in Π, by F ′ R we denote the formula τ B (Body ) ∧ val t (V) if R is (5), and where U ′ R is the list of all variables occurring in R. For example, the completed definition of one-rule program (3) is where val −10 .. 10 (Z 2 ) stands for and val 2×X (V ) stands for By COMP(Π) we denote the set of sentences that includes • for every predicate symbol p/n occurring in Π, its completed definition in Π in the sense of Fandinno et al, and • for every constraint ← Body in Π, the universal closure of the formula ¬τ B (Body ).

Lemma 1
For any regular program Π and any subset S of its Herbrand base, if S is a stable model of Π then S ↑ satisfies COMP(Π) (Fandinno et al. 2020, Theorem 1).

Lemma 2
For any tight regular program Π and any subset S of its Herbrand base, S is a stable model of Π iff S ↑ satisfies COMP(Π) (Fandinno et al. 2020, Theorem 2).

Main Lemma
Theorems 1 and 2 follow from Lemmas 1 and 2 in view of the following fact, proved below:

Main Lemma
For any regular program Π, the formula NCOMP(Π) is equivalent to COMP(Π) in classical predicate calculus with equality.
In the statements of Lemmas 3-7, R is a regular rule, X is the list of its critical variables, and I is f R (X).

Lemma 3
If t is a list of symbolic constants and regular terms that occur in R, and Z is a list of pairwise distinct general variables that do not occur in R, then the formula is logically valid (Lifschitz 2021, Lemma 1(i)).

Lemma 4
For any regular atom p(t) occurring in R, the formulas are logically valid (Lifschitz 2021, Lemma 1(iii,iv)).

Lemma 5
For any comparison t 1 ≺ t 2 occurring in R, the formula is logically valid (Lifschitz 2021, Lemma 2).

Lemma 7
The formula where Body is the body of R, is logically valid.

Lemma 8
Let R be a regular rule with the body (a) For every variable X that occurs in B i in the scope of an arithmetic operation, the formula is logically valid.(b) If B i is a comparison of the form t 1 = t 2 .. t 3 then formula ( 24) is logically valid for every variable X that occurs in B i .(Lifschitz 2021, Lemmas 7 and 9).
From Lemma 8 we conclude: Lemma 9 Let R be a regular rule, and let X be the list of its critical variables.The formula where Body is the body of R and I is f R (X), is logically valid.
In the following lemma, R is a regular rule of form ( 5) or ( 6), F R and U R are as defined in Section 3.2, and F ′ R and U ′ R are as defined in Section 5.1.

Lemma 10
The formula ∃U ′ R F ′ R is equivalent to ∃U R F R .Proof Let X be the list of all critical variables of R, and let I be f R (X).It is sufficient to prove the equivalence The case of rule ( 6) is similar.Left-to-right: assume because the critical variables X do not occur in the formula f R (Body ) ∧ V = f R (t).Then ∃IX(τ B (Body ) ∧ val t (V)) follows using the iniversal closures of ( 22) and ( 23).Since the integer variables I do not occur in τ B (Body ) ∧ val t (V), the quantifiers binding I can be dropped.

It follows that
because the integer variables I do not occur in the formula τ B (Body ) ∧ val t (V)).Then ∃IX(f R (Body ) ∧ V = f R (t)) follows using the iniversal closures of ( 22) and ( 23).Since the critical variables X do not occur in f R (Body ) ∧ V = f R (t), the quantifiers binding X can be dropped.

Lemma 11
If R is a regular constraint ← Body then the universal closures of the formulas ¬f R (Body) and ¬τ B (Body) are equivalent to each other.
Proof Let X be the list of all critical variables of R, and let I be f R (X).It is sufficient to prove the equivalence and consequently entails also the equivalence between the universal closures of ¬f R (Body) and ¬τ B (Body).
Left-to-right: assume ∃I f R (Body ).Since ∀I∃X(I = X), we can conclude that because the critical variables X do not occur in f R (Body ).Then ∃IX τ B (Body ) follows using the iniversal closure of (23).Since the integer variables I do not occur in τ B (Body ), the quantifiers binding I can be dropped.Right-to-left: assume ∃X τ B (Body ).We can conclude, using the universal closure of (25), that ∃IX(I = X ∧ τ B (Body )), because the integer variables I do not occur in τ B (Body ).Then ∃IX f R (Body ) follows using the iniversal closure of (23).Since the critical variables X do not occur in f R (Body ), the quantifiers binding X can be dropped.
Main Lemma follows from Lemmas 10 and 11.

Discussion
In the presence of arithmetic operations, the completed definition in the sense of Fandinno et al. is often longer and syntactically more complex than the "natural" completed definition introduced in Section 3.2; compare, for instance, formula ( 21) with (2).On the other hand, the approach of Fandinno et al. is applicable to some types of rules that are accepted by clingo but are not regular, such as p(1..8,1..8).p(2*(1..8)).
that the current version of clingo considers unsafe.
The translation COMP is used in the design of the proof assistant anthem (Fandinno et al. 2020), and our Main Lemma shows that NCOMP can be employed in the same way.In the process of interacting with anthem, the user often has to read and modify completion formulas.A version of anthem that implements natural completion would make this work easier.
It would be interesting to extend the definition of NCOMP to programs containing conditional literals (Gebser et al. 2019, Section 3.1.11).Such an extension would make the reverse completion process applicable to some formulas that are more complex syntactically than the current version.For instance, it may be able to handle the formula b 1 (M, N ) ↔ b 0 (M, N ) ∧ ¬∃JK(b 0 (J, K) ∧ M + N = J + K ∧ ¬puzzling 0 (J × K)), which can replace axioms ( 13), ( 14) in the first-order formalization of the Sum and Product Puzzle.