<!DOCTYPE article PUBLIC "-//NLM//DTD JATS (Z39.96) Journal Archiving and Interchange DTD v1.0 20120330//EN" "JATS-archivearticle1.dtd">
<article xmlns:xlink="http://www.w3.org/1999/xlink">
  <front>
    <journal-meta />
    <article-meta>
      <title-group>
        <article-title>Translating Definitions into the Language of Logic Programming: A Case Study</article-title>
      </title-group>
      <contrib-group>
        <contrib contrib-type="author">
          <string-name>Vladimir Lifschitz</string-name>
          <email>vl@cs.utexas.edu</email>
          <xref ref-type="aff" rid="aff0">0</xref>
        </contrib>
        <aff id="aff0">
          <label>0</label>
          <institution>University of Texas at Austin</institution>
          ,
          <country country="US">USA</country>
        </aff>
      </contrib-group>
      <abstract>
        <p>In the process of creating a declarative program, the programmer transforms a problem specification expressed in a natural language into an executable specification. We study the case when the given specification is expressed by a mathematically precise definition, and the goal is to write a program for an answer set solver. Transition from a problem specification to a declarative program is essentially a translation: the programmer reformulates a specification expressed in a natural language (intermixed with mathematical notation) as an executable specification. In the discussion of answer set programming (ASP), Gelfond and Kahl [6] observe that as in any translation, the translation of even simple English sentences to statements in ASP may be a nontrivial task . . . Any type of translation is an art, and translation into ASP is no exception.</p>
      </abstract>
      <kwd-group>
        <kwd>eol&gt;declarative programming</kwd>
        <kwd>programming methodology</kwd>
        <kwd>infinite stable models</kwd>
        <kwd>safety</kwd>
      </kwd-group>
    </article-meta>
  </front>
  <body>
    <sec id="sec-1">
      <title>1. Introduction</title>
      <p>Nevertheless, translating mathematical definitions is nontrivial and deserves careful study.
Specifically, we consider here the definition of prime numbers:</p>
      <p>
        A prime is a natural number greater than 1
that is not a product of two smaller natural numbers.
2. Expressing the definition in formal notation
(
        <xref ref-type="bibr" rid="ref1">1</xref>
        )
(
        <xref ref-type="bibr" rid="ref3">3</xref>
        )
where the variables range over nonnegative integers. The universal closure of this formula
has a unique model that is standard in the sense that its universe consists of all nonnegative
integers, and that the symbols
1 ×
&gt; &lt;
are interpreted in the usual way. In this model, the extent of  is the set of all primes.
      </p>
      <p>
        In logic programming, the same idea can be expressed by the rule obtained from (
        <xref ref-type="bibr" rid="ref2">2</xref>
        ) by
replacing the equivalence sign by a left arrow:
The most conspicuous feature of programming languages, both procedural and declarative,
is that they use formal notation. Many mathematical assertions are easy to express in the
formal language of first-order logic—this is what that language was invented for. In particular,
definition (
        <xref ref-type="bibr" rid="ref1">1</xref>
        ) can be written as the first-order formula
() ↔  &gt; 1 ∧ ¬∃( =  ×  ∧  &lt;  ∧  &lt; ),
(
        <xref ref-type="bibr" rid="ref2">2</xref>
        )
() ←
      </p>
      <p>&gt; 1 ∧ ¬∃( =  ×  ∧  &lt;  ∧  &lt; ).</p>
      <p>
        This expression is not very close to executable code, but it is similar to rules discussed in
many theoretical publications. For instance, Lloyd and Topor [10] defined an extended program
clause as a first-order formula of the form  ←  , where  is an atom and  is an arbitrary
ifrst-order formula. They observed that program completion [ 2] can be generalized to extended
clauses in a straightforward way. The relationship between formulations (
        <xref ref-type="bibr" rid="ref2">2</xref>
        ) and (
        <xref ref-type="bibr" rid="ref3">3</xref>
        ) can be
described by saying that the universal closure of the former is the completed definition of  in
the latter.
      </p>
      <p>
        This example illustrates the fact that definitions in the sense of logic programming are not
so diferent from definitions in the sense of first-order logic, provided that we allow bodies of
rules to be arbitrarily complex. The view accepted in logic programming—a definition is a set
of rules with the same predicate symbol in the head—is actually more general. Equivalences
like (
        <xref ref-type="bibr" rid="ref2">2</xref>
        ) correspond to the logic programming definitions consisting of a single non-recursive
rule such that the arguments of the atom in its head are distinct variables. If a definition in
a logic program consists of several rules, and/or the arguments in the heads are not distinct
variables, then transforming that definition into an equivalence becomes more involved. If the
definition is recursive (as for instance, the definition of the transitive closure) then transforming
it into a first-order equivalence may be impossible.
      </p>
      <p>
        The stable model semantics [7] can be extended to arbitrary first-order sentences using the
syntactic transformation SM [3]. Under some conditions, the result of applying that operator
is equivalent to completion [3, Theorem 11]. In particular, the result of applying SM to the
universal closure of implication (
        <xref ref-type="bibr" rid="ref3">3</xref>
        ) is equivalent to the universal closure of (
        <xref ref-type="bibr" rid="ref2">2</xref>
        ). The subscript 
in the symbol SM indicates that the predicate symbol  is treated as intensional, and the other
predicate symbols occurring in the formula (in this case, &gt; and &lt;) are considered extensional.
      </p>
      <p>
        The definition of SM is based on the view that the negation sign represents negation as failure;
in equivalence (
        <xref ref-type="bibr" rid="ref2">2</xref>
        ), this sign is understood as classical negation. This diference is inessential,
however, because the negated formula in this case does not contain intensional symbols.
      </p>
      <p>
        In the rest of this paper, variables in rules will be capitalized, as usual in logic programming.
In particular, we will write rule (
        <xref ref-type="bibr" rid="ref3">3</xref>
        ) as
( ) ←
 &gt; 1 ∧ ¬∃  ( =  ×  ∧  &lt;  ∧  &lt;  ).
(
        <xref ref-type="bibr" rid="ref4">4</xref>
        )
      </p>
    </sec>
    <sec id="sec-2">
      <title>3. Making the answer finite</title>
      <p>
        Standard Prolog systems answer queries about stable models of logic programs. A standard
answer set solver, on the other hand, is designed to display the entire model of the program, or
at least the extents of the predicates that it is instructed to show. For this reason, stable models
of an ASP program are expected to be finite [ 1, Section 5]. Since the set of all primes is infinite,
rule (
        <xref ref-type="bibr" rid="ref4">4</xref>
        ) violates this condition, and we need to replace it by the definition of a finite set.
      </p>
      <p>
        Let ,  be object constants (“placeholders”) representing integers such that  ≥  &gt; 1.
Consider the rule obtained from (
        <xref ref-type="bibr" rid="ref4">4</xref>
        ) by replacing  &gt; 1 with  ≤  ≤ :
( ) ←
 ≤  ≤  ∧ ¬∃  ( =  ×  ∧  &lt;  ∧  &lt;  ).
(
        <xref ref-type="bibr" rid="ref5">5</xref>
        )
It defines the set of all primes in the interval {, . . . , }. Since this interval can be arbitrarily
large, the modified definition is as general as the definition (
        <xref ref-type="bibr" rid="ref4">4</xref>
        ) of the set of all primes.
      </p>
    </sec>
    <sec id="sec-3">
      <title>4. Simplifying the body</title>
      <p>
        The body of rule (
        <xref ref-type="bibr" rid="ref5">5</xref>
        ) is syntactically more complex than expressions allowed in the core language
of answer set programming. We can get around this dificulty using an auxiliary predicate. 1
The atomic formula aux( ) will represent the subformula
Rule (
        <xref ref-type="bibr" rid="ref5">5</xref>
        ) will be rewritten as
∃  ( =  ×  ∧  &lt;  ∧  &lt;  ).
      </p>
      <p>
        ( ) ←
 ≤  ≤  ∧ ¬aux( ),
(
        <xref ref-type="bibr" rid="ref6">6</xref>
        )
(
        <xref ref-type="bibr" rid="ref7">7</xref>
        )
1The input language of the answer set solver clingo includes conditional literals, which correspond to universally
quantified implications in the body of a rule. A conditional literal can be used to convert (
        <xref ref-type="bibr" rid="ref5">5</xref>
        ) into a clingo rule
without auxiliary predicates, because formula (
        <xref ref-type="bibr" rid="ref6">6</xref>
        ) can be rewritten as
      </p>
      <p>∀ ( &lt;  ∧  &lt;  →  ̸=  ×  ).</p>
      <p>Conditional literals are not part of the core language and are not available in the current version of the solver dlv.
and aux can be defined by the rule</p>
      <p>aux( ) ← ∃  ( =  ×  ∧  &lt;  ∧  &lt;  ).</p>
      <p>
        Furthermore, the meaning of the last rule will not change if we drop the existential quantifier
from its body:
 =  ×  ∧  &lt;  ∧  &lt; .
(
        <xref ref-type="bibr" rid="ref8">8</xref>
        )
For any choice of  and , program (
        <xref ref-type="bibr" rid="ref7">7</xref>
        ), (
        <xref ref-type="bibr" rid="ref8">8</xref>
        ) has a unique standard stable model, and the extent
of  in that model is the set of all primes in the interval {, . . . , }.
      </p>
      <p>The transformation described in this section is an example of the general process implemented
in the translator f2lp [8]. That process is applicable to first-order formulas that contain any
propositional connectives and quantifiers.</p>
      <p>
        Program (
        <xref ref-type="bibr" rid="ref7">7</xref>
        ), (
        <xref ref-type="bibr" rid="ref8">8</xref>
        ) looks pretty much like a typical piece of ASP code, modulo simple syntactic
changes, such as replacing ← with :-, ∧ with a comma, and ¬ with not. A little more work is
needed, however, to convert it into an input for an answer set solver.
      </p>
    </sec>
    <sec id="sec-4">
      <title>5. Making the entire stable model finite</title>
      <p>
        One of the reasons why program (
        <xref ref-type="bibr" rid="ref7">7</xref>
        ), (
        <xref ref-type="bibr" rid="ref8">8</xref>
        ) is not completely satisfactory is that the problem of
infinite stable models, discussed in Section 3, has reappeared with the introduction of aux. The
extent of this predicate is an infinite set—it includes 0, 1, and all composite numbers. The extent
of the predicate  that we would like to see displayed on the screen has not changed; it is the
same finite set as before. But the entire stable model of program (
        <xref ref-type="bibr" rid="ref7">7</xref>
        ), (
        <xref ref-type="bibr" rid="ref8">8</xref>
        ) is infinite.
      </p>
      <p>
        We can deal with this dificulty by modifying the auxiliary predicate. In the body of rule (
        <xref ref-type="bibr" rid="ref7">7</xref>
        ),
¬aux( ) is conjoined with the condition  ≤  ≤ . Consequently the meaning of the rule
will not be afected if we understand aux( ) in a diferent way:
      </p>
      <p>is a number between  and  that equals 0 or 1 or is composite.</p>
      <p>
        This can be accomplished by adding  ≤  ≤  as a conjunctive term to the body of rule (
        <xref ref-type="bibr" rid="ref8">8</xref>
        ):
 ≤  ≤  ∧  =  ×  ∧  &lt;  ∧  &lt; .
(
        <xref ref-type="bibr" rid="ref9">9</xref>
        )
The stable model of program (
        <xref ref-type="bibr" rid="ref7">7</xref>
        ), (
        <xref ref-type="bibr" rid="ref9">9</xref>
        ) is finite, and the extent of  in that model is again the set of
all primes in the interval {, . . . , }.
      </p>
    </sec>
    <sec id="sec-5">
      <title>6. Allowing variables to take general values</title>
      <p>In the rules above, the symbols ,  ,  are understood as variables for nonnegative integers.
On the other hand, the set of values that can be assigned to variables by a global substitution
includes not only integers, but also symbolic constants and other expressions [1, Section 3].
The only restriction is that the evaluation of arithmetic subterms is required to be well-defined;
for instance, substituting non-integer values for ,  in a rule containing the term  ×  is not
allowed.</p>
      <p>
        Because of this diference, the meaning of program (
        <xref ref-type="bibr" rid="ref7">7</xref>
        ), (
        <xref ref-type="bibr" rid="ref9">9</xref>
        ) in the core language of ASP can be
diferent from its understanding adopted in the discussion above. Rule (
        <xref ref-type="bibr" rid="ref7">7</xref>
        ) is not problematic in
this sense, because the condition  ≤  ≤  in its body restricts the values of  to integers
that are greater than or equal to , and  is expected to be a positive integer (Section 3). But the
body of rule (
        <xref ref-type="bibr" rid="ref9">9</xref>
        ) can be satisfied for substitutions that make  and  negative, for instance
 = 7,  = − 1,  = − 7.
      </p>
      <p>
        We will make such substitutions harmless by saying explicitly in the body of the rule that 
and  are nonnegative:
( ) ←
 ≤  ≤  ∧  =  ×  ∧ 0 ≤  &lt;  ∧ 0 ≤  &lt; .
(
        <xref ref-type="bibr" rid="ref10">10</xref>
        )
      </p>
    </sec>
    <sec id="sec-6">
      <title>7. Helping the grounder to ground</title>
      <p>
        Rules (
        <xref ref-type="bibr" rid="ref7">7</xref>
        ) and (
        <xref ref-type="bibr" rid="ref10">10</xref>
        ) need to be further modified, because they are not safe.
      </p>
      <p>The definition of safety [ 1, Section 5] is recursive and rather complicated; but violating
the safety requirement usually causes the solvers clingo and dlv to terminate with an error
message.2 In a safe rule, every global variable has at least one occurrence in the body that either
belongs to a nonnegated atom or is the left-hand side of an equality. A condition of this kind is
needed to facilitate the process of grounding. An ASP solver calculates a finite set of “essential
values” of each variable in each rule of the program such that substituting inessential values is
not needed for generating stable models. This is a prerequisite for transforming a program with
variables into a finite ground program.</p>
      <p>
        Rule (
        <xref ref-type="bibr" rid="ref7">7</xref>
        ) is not safe, because one of the two occurrences of  in its body is within an inequality,
and the other is part of a negated atom. Rule (
        <xref ref-type="bibr" rid="ref10">10</xref>
        ) is not safe because of the variables  ,  .
      </p>
      <p>
        The interval construct (. .) provides a way out.3 The expression  = .. (“the value of 
belongs to the interval {, . . . , }”) has almost the same meaning as the inequality  ≤  ≤ ;
the diference is that the latter can be satisfied even for non-integer values of ,  , and .4 The
other two inequalities in the body of (
        <xref ref-type="bibr" rid="ref10">10</xref>
        ) can be replaced by interval expressions in a similar
way.
      </p>
      <p>Now we are done. The program
2There are exceptions, however. clingo does not complain, for example, about the unsafe rule
p(N) :- 3*N+1 = 10.</p>
      <p>
        Rules (
        <xref ref-type="bibr" rid="ref7">7</xref>
        ) and (
        <xref ref-type="bibr" rid="ref10">10</xref>
        ) will be groundable by Version 5.6.0 of clingo, which is in the works at the time of this writing
(Roland Kaminski, personal communication, June 16, 2022).
      </p>
      <p>3This construct is available in the input languages of clingo and dlv, but it is not mentioned in the ASP-Core-2
document [1]. The authors of the document view this now as an oversight (Martin Gebser, personal communication,
April 10, 2022).</p>
      <p>4In this context, the set membership symbol ∈ between  and .. would look more natural than the equality
sign. But the general view adopted in the input language of clingo is that a ground term may have several values
[4, Section 4.2], and a comparison 1 ≺ 2 in the body of a rule expresses that the relation ≺ holds between some
value of 1 and some value of 2. For instance, each of the comparisons 2 = 1..3, 1..3 = 2 expresses that the only
value of 2 equals one of the three values of 1..3. The comparison 1..3 = 3..5 in the body of a rule is true because its
left-hand side and right-hand side have a common value.</p>
      <p>#const a = 20.
#const b = 30.</p>
      <p>p(N) :- N = a..b, not aux(N).
aux(N) :- N = a..b, N = I*J, I = 0..N-1, J = 0..N-1.</p>
      <p>#show p/1.
can be successfully grounded by clingo, and the answer
will be displayed on the screen.</p>
    </sec>
    <sec id="sec-7">
      <title>8. Conclusion</title>
      <p>The process of translating the English language definition of primes into executable code
described in this note involved
• rewriting the definition in formal notation,
• modifying it to make the answer finite,
• introducing an auxiliary definition to make the body simpler,
• making the entire stable model finite,
• allowing variables to take arbitrary values, and
• helping the grounder to ground the program.</p>
      <p>This is somewhat similar to the process of stepwise refinement in procedural programming,
in the sense that each step took us closer to executable code. There is a diference, however.
Stepwise refinement makes the outline of the algorithm longer and longer, as the programmer
breaks down steps into substeps. Our transformations, on the other hand, had almost no efect
on the size of the specification.</p>
      <p>The final result is the first version of executable code; we did not talk here about the subsequent
process of improving it in the sense of making the program more eficient. An example of the
latter is the topic of Section 3.1 of the paper by Gebser et al. [5], where a series of encodings
of the -queens problem is presented, each of them an improvement of the one before. Case
studies of both kinds contribute to developing and clarifying the methodology of answer set
programming.</p>
      <p>The detailed elaborations on the development of even a short program form a long
story, indicating that careful programming is not a trivial subject. If this paper
has helped to dispel the widespread belief that programming is easy as long as
the programming language is powerful enough and the available computer is fast
enough, then it has achieved one of its purposes [11].
Many thanks to Jorge Fandinno, Michael Gelfond, Roland Kaminski, Yuliya Lierler, Jayadev
Misra and the anonymous referees for comments on a draft of this paper. Jessica Zangari and
Martin Gebser helped me by answering questions about the input language of dlv and about
the core language of answer set programming.</p>
    </sec>
  </body>
  <back>
    <ref-list>
      <ref id="ref1">
        <mixed-citation>
          [1]
          <string-name>
            <given-names>Francesco</given-names>
            <surname>Calimeri</surname>
          </string-name>
          , Wolfgang Faber, Martin Gebser, Giovambattista Ianni, Roland Kaminski, Thomas Krennwallner, Nicola Leone, Marco Maratea, Francesco Ricca, and
          <string-name>
            <given-names>Torsten</given-names>
            <surname>Schaub</surname>
          </string-name>
          .
          <article-title>ASP-Core-2 input language format</article-title>
          .
          <source>Theory and Practice of Logic Programming</source>
          ,
          <volume>20</volume>
          :
          <fpage>294</fpage>
          -
          <lpage>309</lpage>
          ,
          <year>2020</year>
          .
        </mixed-citation>
      </ref>
      <ref id="ref2">
        <mixed-citation>
          [2]
          <string-name>
            <given-names>Keith</given-names>
            <surname>Clark</surname>
          </string-name>
          .
          <article-title>Negation as failure</article-title>
          .
          <source>In Herve Gallaire and Jack Minker</source>
          , editors,
          <source>Logic and Data Bases</source>
          , pages
          <fpage>293</fpage>
          -
          <lpage>322</lpage>
          . Plenum Press, New York,
          <year>1978</year>
          .
        </mixed-citation>
      </ref>
      <ref id="ref3">
        <mixed-citation>
          [3]
          <string-name>
            <given-names>Paolo</given-names>
            <surname>Ferraris</surname>
          </string-name>
          ,
          <string-name>
            <given-names>Joohyung</given-names>
            <surname>Lee</surname>
          </string-name>
          ,
          <string-name>
            <given-names>and Vladimir</given-names>
            <surname>Lifschitz</surname>
          </string-name>
          .
          <article-title>Stable models and circumscription</article-title>
          .
          <source>Artificial Intelligence</source>
          ,
          <volume>175</volume>
          :
          <fpage>236</fpage>
          -
          <lpage>263</lpage>
          ,
          <year>2011</year>
          .
        </mixed-citation>
      </ref>
      <ref id="ref4">
        <mixed-citation>
          [4]
          <string-name>
            <given-names>Martin</given-names>
            <surname>Gebser</surname>
          </string-name>
          , Amelia Harrison, Roland Kaminski, Vladimir Lifschitz, and
          <string-name>
            <given-names>Torsten</given-names>
            <surname>Schaub</surname>
          </string-name>
          .
          <source>Abstract Gringo. Theory and Practice of Logic Programming</source>
          ,
          <volume>15</volume>
          :
          <fpage>449</fpage>
          -
          <lpage>463</lpage>
          ,
          <year>2015</year>
          .
        </mixed-citation>
      </ref>
      <ref id="ref5">
        <mixed-citation>
          [5]
          <string-name>
            <given-names>Martin</given-names>
            <surname>Gebser</surname>
          </string-name>
          , Roland Kaminski, Benjamin Kaufmann, and
          <string-name>
            <given-names>Torsten</given-names>
            <surname>Schaub</surname>
          </string-name>
          .
          <article-title>Challenges in answer set solving. In Logic programming, knowledge representation, and nonmonotonic reasoning</article-title>
          .
          <source>Essays Dedicated to Michael Gelfond on the Occasion of His 65th Birthday</source>
          , pages
          <fpage>74</fpage>
          -
          <lpage>90</lpage>
          . Springer,
          <year>2011</year>
          .
        </mixed-citation>
      </ref>
      <ref id="ref6">
        <mixed-citation>
          [6]
          <string-name>
            <given-names>Michael</given-names>
            <surname>Gelfond</surname>
          </string-name>
          and
          <string-name>
            <given-names>Yulia</given-names>
            <surname>Kahl</surname>
          </string-name>
          .
          <article-title>Knowledge Representation, Reasoning, and the Design of Intelligent Agents: The Answer-Set Programming Approach</article-title>
          . Cambridge University Press,
          <year>2014</year>
          .
        </mixed-citation>
      </ref>
      <ref id="ref7">
        <mixed-citation>
          [7]
          <string-name>
            <given-names>Michael</given-names>
            <surname>Gelfond</surname>
          </string-name>
          and
          <string-name>
            <given-names>Vladimir</given-names>
            <surname>Lifschitz</surname>
          </string-name>
          .
          <article-title>The stable model semantics for logic programming</article-title>
          .
          <source>In Robert Kowalski and Kenneth Bowen</source>
          , editors,
          <source>Proceedings of International Logic Programming Conference and Symposium</source>
          , pages
          <fpage>1070</fpage>
          -
          <lpage>1080</lpage>
          . MIT Press,
          <year>1988</year>
          .
        </mixed-citation>
      </ref>
      <ref id="ref8">
        <mixed-citation>
          [8]
          <string-name>
            <given-names>Joohyung</given-names>
            <surname>Lee</surname>
          </string-name>
          and
          <string-name>
            <given-names>Ravi</given-names>
            <surname>Palla</surname>
          </string-name>
          .
          <article-title>System F2LP - computing answer sets of first-order formulas</article-title>
          .
          <source>In Proceedings of 10th International Conference on Logic Programming and Nonmonotonic Reasoning</source>
          , pages
          <fpage>515</fpage>
          -
          <lpage>521</lpage>
          ,
          <year>1988</year>
          .
        </mixed-citation>
      </ref>
      <ref id="ref9">
        <mixed-citation>
          [9]
          <string-name>
            <given-names>Vladimir</given-names>
            <surname>Lifschitz</surname>
          </string-name>
          .
          <article-title>What is answer set programming</article-title>
          ?
          <source>In Proceedings of the 23rd AAAI Conference on Artificial Intelligence</source>
          , pages
          <fpage>1594</fpage>
          -
          <lpage>1597</lpage>
          . MIT Press,
          <year>2008</year>
          .
        </mixed-citation>
      </ref>
      <ref id="ref10">
        <mixed-citation>
          [10]
          <string-name>
            <given-names>John</given-names>
            <surname>Lloyd</surname>
          </string-name>
          and
          <string-name>
            <given-names>Rodney</given-names>
            <surname>Topor</surname>
          </string-name>
          .
          <article-title>Making Prolog more expressive</article-title>
          .
          <source>Journal of Logic Programming</source>
          ,
          <volume>1</volume>
          :
          <fpage>225</fpage>
          -
          <lpage>240</lpage>
          ,
          <year>1984</year>
          .
        </mixed-citation>
      </ref>
      <ref id="ref11">
        <mixed-citation>
          [11]
          <string-name>
            <given-names>Niklaus</given-names>
            <surname>Wirth</surname>
          </string-name>
          .
          <article-title>Program development by stepwise refinement</article-title>
          .
          <source>Communications of the ACM</source>
          ,
          <volume>14</volume>
          (
          <issue>4</issue>
          ):
          <fpage>221</fpage>
          -
          <lpage>227</lpage>
          ,
          <year>1971</year>
          .
        </mixed-citation>
      </ref>
    </ref-list>
  </back>
</article>