<!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>Constraint Answer Set Programming without Grounding and its Applications?</article-title>
      </title-group>
      <contrib-group>
        <contrib contrib-type="author">
          <string-name>Joaquin Arias</string-name>
          <xref ref-type="aff" rid="aff0">0</xref>
          <xref ref-type="aff" rid="aff1">1</xref>
        </contrib>
        <contrib contrib-type="author">
          <string-name>Manuel Carro</string-name>
          <email>manuel.carro@upm.es</email>
          <email>manuel.carrog@imdea.org</email>
          <xref ref-type="aff" rid="aff0">0</xref>
          <xref ref-type="aff" rid="aff1">1</xref>
        </contrib>
        <contrib contrib-type="author">
          <string-name>Zhuo Chen</string-name>
          <xref ref-type="aff" rid="aff2">2</xref>
        </contrib>
        <contrib contrib-type="author">
          <string-name>Gopal Gupta</string-name>
          <email>guptag@utdallas.edu</email>
          <xref ref-type="aff" rid="aff2">2</xref>
        </contrib>
        <aff id="aff0">
          <label>0</label>
          <institution>IMDEA Software Institute</institution>
        </aff>
        <aff id="aff1">
          <label>1</label>
          <institution>Universidad Polite ́cnica de Madrid</institution>
        </aff>
        <aff id="aff2">
          <label>2</label>
          <institution>University of Texas at Dallas</institution>
          ,
          <country country="US">USA</country>
        </aff>
      </contrib-group>
      <fpage>22</fpage>
      <lpage>27</lpage>
      <abstract>
        <p>Extending Datalog/ASP with constraints (CASP) enhances its expressiveness and performance but it is not straightforward as the grounding phase removes variables and the links among them. We incorporate constraints into s(ASP), a goal-directed, top-down execution model which implements predicate answer set programming without grounding. The resulting model, s(CASP), can constrain variables that, as in CLP, are kept during the execution and in the answer sets. We show the enhanced expressiveness of s(CASP) w.r.t. other CASP systems, through a non-trivial example of modeling the event calculus.</p>
      </abstract>
    </article-meta>
  </front>
  <body>
    <sec id="sec-1">
      <title>1 Introduction</title>
      <p>
        Answer Set Programming (ASP) has emerged as a successful paradigm for developing
intelligent applications. It uses the stable model semantics [
        <xref ref-type="bibr" rid="ref4">4</xref>
        ] and has attracted much
attention due to its expressiveness, ability to incorporate non-monotonicity, represent
knowledge, and model combinatorial problems. ASP can be seen as Datalog extended
with negation-as-failure.
      </p>
      <p>
        s(ASP) [
        <xref ref-type="bibr" rid="ref8">8</xref>
        ] is a goal-directed, top-down, SLD resolution-like procedure which
evaluates programs under the ASP semantics without a grounding phase either before or
during execution. s(ASP) supports predicates and can thus retain logical variables both
during execution and in the answer sets.
      </p>
      <p>
        Constraints have been used both to enhance expressiveness and to increase
performance in logic programming. Therefore, it is natural to incorporate constraints in
ASP systems. The s(CASP) system [
        <xref ref-type="bibr" rid="ref2">2</xref>
        ] extends s(ASP) by integrating it with generic
constraint solvers. The s(CASP) system makes it possible to express constraints on
variables. It extends s(ASP)’s execution model in the same way that CLP extends
Prolog’s. Thus, s(CASP) inherits and generalizes the execution model of s(ASP) and is
parametrized w.r.t. the constraint solver. Due to its basis in s(ASP), the s(CASP) system
avoids grounding the program and the concomitant combinatorial explosion. s(CASP)
also handles answer set programs with arbitrary data structures and/or reals, rationals,
etc. The s(CASP) system successfully executes programs in finite time that loop in other
? Work partially supported by EIT Digital (https://eitdigital.eu), MINECO project
TIN201567522-C3-1-R (TRACES), Comunidad de Madrid as part of the program S2018/TCS-4339
BLOQUES-CM co-funded by EIE Funds of the European Union, and NSF Grant IIS 1718945.
s(CASP) s(ASP)
hanoi(8,T) 1,528 13,297
queens(4,Q) 1,930 20,141
One Hamiltonian cycle 493 3,499
Two Hamiltonian cycle 3,605 18,026
      </p>
      <p>
        Table 1: Speed comparison (time in ms).
top-down systems, as well as programs that require constraints over dense and/or
unbound domains. Thus, s(CASP) is able to solve problems that cannot be easily solved
by other Datalog/ASP systems [
        <xref ref-type="bibr" rid="ref3 ref6 ref9">3,6,9</xref>
        ].
      </p>
      <p>Summarizing, we show how Datalog programs extended with negation following
the stable-model semantics can be executed in a query-driven, goal-directed manner
in the presence of constraints, including constraints over dense domains. The s(CASP)
system is the culmination of this work. To illustrate the power of s(CASP), we show
how event calculus (EC) axioms and narratives can be modeled in s(CASP) and how
its expressiveness makes it possible to perform deductive and abductive reasoning over
continuous domains.</p>
    </sec>
    <sec id="sec-2">
      <title>2 s(CASP): Design and Implementation</title>
      <p>
        The s(CASP) system (https://gitlab.software.imdea.org/joaquin.arias/sCASP),
implemented in Ciao Prolog [
        <xref ref-type="bibr" rid="ref5">5</xref>
        ], is an evolution of the s(ASP) system. The main
contributions of s(CASP) are: (i) a generic interface to connect the disequality constraint
solver CLP(6=) with different constraint solvers (e.g., CLP(Q), a linear constraint solver
over the rationals); (ii) an extension of the compiler to support the compilation of
s(CASP) programs and the generation of the consistency checking rules in the presence
of constraints; and (iii) the design and implementation of C-forall, a generic algorithm
to execute constructive negation that extends the original s(ASP) forall algorithm.
      </p>
      <p>The design of the Ciao Prolog implementation of s(CASP) improved performance
substantially w.r.t. s(ASP) despite these new capabilities. Table 1 shows these
improvements in benchmarks (without constraints) executed on a MacOS 10, Intel i5 at 2GHz.</p>
      <p>
        A s(CASP) program is a finite set of rules of the form: a :- ca, l1, : : :, lm:
where ca is a (conjunction of) constraints and li are literals (also with default negation
not and/or classical (strong) negation -). A s(CASP) program execution starts with
a query and each answer is the resulting mgu of a successful derivation, its
justification, and a (partial) stable model. A partial stable model is a subset of an ASP stable
model [
        <xref ref-type="bibr" rid="ref4">4</xref>
        ] including only the literals necessary to support the query.
3
      </p>
    </sec>
    <sec id="sec-3">
      <title>Application and Evaluation</title>
      <p>
        The s(CASP) system allows programmers to directly write programs and queries that
cannot be written in other Datalog or constraint ASP systems [
        <xref ref-type="bibr" rid="ref3 ref6 ref9">3,6,9</xref>
        ] without resorting
to a complex, unnatural encoding. Additionally, s(CASP) can express answers more
elegantly than that done by Datalog/ASP, due to several reasons:
1 valid_stream(P,Data)
:2 stream(P,Data),
3 not cancelled(P, Data).
4 higher_prio(PHi, PLo)
:5 PHi #&gt; PLo.
6 cancelled(P, Data)
:7 higher_prio(P1, P),
8 stream(P1, Data1),
9 incompt(Data, Data1).
      </p>
      <p>10 incompt(p(X),q(X)).
– s(CASP) inherits the use of unbound variables during the execution and in the
answers from s(ASP). This makes it possible to express constraints more compactly
and naturally (e.g., intervals of times can be written using constraints)
– s(CASP) can use structures/functors directly, thereby avoiding the need to encode
them unnaturally (e.g., to capture continuous change in Event Calculus).
– The constraints and the goal-directed evaluation strategy of s(CASP) make it
possible to use direct algorithms and reduce search space.
3.1</p>
      <sec id="sec-3-1">
        <title>Stream Data Reasoning</title>
        <p>
          Let us assume that we deal with data streams, some of whose items may be
contradictory [
          <xref ref-type="bibr" rid="ref1">1</xref>
          ], and that different data sources may have different degrees of trustworthiness.
We will use these degrees to prefer data items from one source over items from another
source in case of inconsistency.
        </p>
        <p>Figure 1 shows the code for a stream reasoner using s(CASP). The rule
valid_stream/2 states that a data stream stream(P,Data) is valid if it is not
cancelled by another contradictory data stream with a higher confidence degree. A data
stream item contains the degree of confidence P for every Data item. incompt/2
determines which data items are contradictory (in this case, p(X) and q(X)).</p>
        <p>The constraints and the goal-directed strategy of s(CASP) make it possible to
resolve queries without evaluating the whole stream database. For example, the rule
incompt(p(X),q(X)) does not have to be grounded w.r.t. the stream database. If
timestamps were used as trustworthiness measure, then for a query such as ?- T #&gt; 10,
valid_stream(T,p(A)), the reasoner would validate streams received after T=10
regardless of how long they extend in the past.
3.2</p>
      </sec>
      <sec id="sec-3-2">
        <title>Towers of Hanoi</title>
        <p>
          We encoded this problem for n disks with three systems: clingo, a standard ASP solver,
setting a bound to the number of moves that can be done; the incremental version of
clingo [
          <xref ref-type="bibr" rid="ref6">6</xref>
          ], where the number n of allowed movements is iteratively increased (see both
in the clingo 5.2.0 distribution); and s(CASP) which, thanks to the top down
approach, uses a much more economic CLP-like control strategy [
          <xref ref-type="bibr" rid="ref2">2</xref>
          ]. Table 2 compares
execution time (in milliseconds) needed to solve the puzzle.
        </p>
        <p>s(CASP) is orders of magnitude faster than both clingo variants because it does not
have to generate and test all the possible plans. The standard variant is less interesting
than s(CASP)’s, as it merely checks if the problem can be solved in a given number of
moves. The incremental variant is by far the slowest, because the program iteratively
checks with an increasing number of moves until it can be solved.
clingo 5.2.0
standard
3,651
54,104
191,267
clingo 5.2.0
incremental</p>
        <p>
          9,885
174,224
&gt; 5 min
Basic Event Calculus (BEC) [
          <xref ref-type="bibr" rid="ref10">10</xref>
          ] is a family of formalisms that model commonsense
reasoning with a sound, logical basis. Previous attempts [
          <xref ref-type="bibr" rid="ref3 ref9">3,9</xref>
          ] to mechanize reasoning
using BEC faced difficulties in the treatment of continuous change in dense domains
(e.g., time and other physical quantities), constraints among variables, default negation,
and the uniform application of different inference methods, among others. Let us model
the BEC theory using s(CASP) and see how its expressiveness makes it possible to
perform deductive and abductive reasoning tasks in domains featuring, for example,
constraints involving dense time and fluents affected by continuous changes.
Translation of Basic Event Calculus: The translation of BEC axioms [
          <xref ref-type="bibr" rid="ref10">10</xref>
          ] into
s(CASP) program follows, to some extent, that of the systems EC2ASP and F2LP [
          <xref ref-type="bibr" rid="ref7">7</xref>
          ],
but we differ in some key aspects that improve performance and are relevant for
expressiveness: the treatment of rules with negated heads, the possibility of handling unsafe
rules4, and the use of constraints over rationals/reals. Thanks to the usage of non-ground
variables, s(CASP) can directly model event calculus axioms that require unsafe rules,
e.g., BEC4: HoldsAt( f ; t) InitiallyP( f ) ^ :StoppedIn(0; f ; t), is translated into:
1 holds At(F,T) :- 0 #&lt; T, initiallyP(F), not stoppedIn(0,F,T). % BEC4
Translation of the narrative: The definition of a scenario states the events and effects
that occur. Let us consider a vessel that is filled with water from a tap. A possible
translation of the logic statements that define that scenario into s(CASP) follows. Note
that this translation requires constraint handling with local, uninstantiated variables.
1 max_level(10) :- not max_level(16). % Force the vessel to be 10 or
2 max_level(16) :- not max_level(10). % 16 (two possible worlds).
3 happens(tapOn,5). % TapOn happens at time 5.
4 initiates(tapOn,filling,T). % TapOn initiates Filling.
5 terminates(tapOff,filling,T). % TapOff terminates Filling.
6 trajectory(filling,T1,level(X2),T2) :- % Level(X) represents that
7 T1 #&lt; T2, X2 #= X+T2-T1, % the water is at level X
8 max_level(Max), X2 #=&lt; Max, % in the vessel, as long as
9 holdsAt(level(X),T1). % its rim is not reached.
        </p>
      </sec>
      <sec id="sec-3-3">
        <title>Deductive and Abductive reasoning: We can perform deduction (determine possible</title>
        <p>states) in BEC through queries to the corresponding s(CASP) program. For example:
4 In ASP, a rule is safe when every variable that appears in its head or in a negated literal in its
body also appears in a positive literal in its body (it is unsafe otherwise). ASP solvers such as
clingo are not able to process unsafe rules.
1 ?- holdsAt(level(H),15/2).
2 ?- holdsAt(level(5/2),T).
% is true when H = 5/2.
% is true when T = 15/2.</p>
        <p>On the other hand, abduction tries to determine a plausible sequence of events
that reaches a given state. The line #abducible happens(tapOff,U) is a
shortcut that states that it is possible (but not necessary) for the tap to be closed at
some time U. After adding it, the query ?- holdsAt(spilling,T) determines if
and under which conditions the water may overspill, and returns a model
containing holdsAt(spilling,T),T&gt;15,happens(tapOn,5),not happens(TapOff,
U), 5&lt;U&lt;15, max_level(10) meaning that the water will spill at T=15 if the vessel
has a capacity of 10, the tap is open at T=5, and it is not closed between times 5 and 15.
4</p>
      </sec>
    </sec>
    <sec id="sec-4">
      <title>Conclusion and Future Work</title>
      <p>We have reported on the design and implementation of s(CASP), a top-down system to
evaluate constraint answer set programs, based on s(ASP). Its ability to express answer
set programs coupled with the possibility of expressing control in a way similar to
traditional logic programming makes it a powerful system. In fact, a single program can
use both approaches (LP and ASP) simultaneously, achieving the best of both worlds.
We have also reported a very substantial performance increase w.r.t. the original s(ASP)
implementation. Thanks to the possibility of writing pieces of code with control in
mind, it can also beat state-of-the-art ASP systems in certain programs.</p>
      <p>
        The implementation can still be improved substantially. In particular, (i) we want to
use program analysis to interleave the execution of odd loops with even loops during
top-down execution so as to discard models as soon as they are shown inconsistent [
        <xref ref-type="bibr" rid="ref8">8</xref>
        ],
(ii) improve the disequality constraint solver, (iii) use dependency analysis to improve
the generation of dual rules [
        <xref ref-type="bibr" rid="ref8">8</xref>
        ], and (iv) apply partial evaluation and better compilation
techniques to remove (part of) the interpretation overhead.
      </p>
    </sec>
  </body>
  <back>
    <ref-list>
      <ref id="ref1">
        <mixed-citation>
          1.
          <string-name>
            <surname>Arias</surname>
          </string-name>
          , J.:
          <article-title>Tabled CLP for Reasoning over Stream Data</article-title>
          .
          <source>In: Technical Communications of ICLP'16</source>
          , vol.
          <volume>52</volume>
          , pp.
          <fpage>1</fpage>
          -
          <lpage>8</lpage>
          . OASIcs (
          <year>2016</year>
          ). Doctoral Consortium
        </mixed-citation>
      </ref>
      <ref id="ref2">
        <mixed-citation>
          2.
          <string-name>
            <surname>Arias</surname>
            ,
            <given-names>J.</given-names>
          </string-name>
          ,
          <string-name>
            <surname>Carro</surname>
            ,
            <given-names>M.</given-names>
          </string-name>
          ,
          <string-name>
            <surname>Salazar</surname>
            ,
            <given-names>E.</given-names>
          </string-name>
          ,
          <string-name>
            <surname>Marple</surname>
            ,
            <given-names>K.</given-names>
          </string-name>
          ,
          <string-name>
            <surname>Gupta</surname>
            ,
            <given-names>G.</given-names>
          </string-name>
          :
          <article-title>Constraint answer set programming without grounding</article-title>
          .
          <source>Theory and Practice of Logic Programming</source>
          <volume>18</volume>
          (
          <issue>3-4</issue>
          ),
          <fpage>337</fpage>
          -
          <lpage>354</lpage>
          (
          <year>2018</year>
          )
        </mixed-citation>
      </ref>
      <ref id="ref3">
        <mixed-citation>
          3.
          <string-name>
            <surname>East</surname>
            ,
            <given-names>D.</given-names>
          </string-name>
          ,
          <string-name>
            <surname>Truszczynski</surname>
            ,
            <given-names>M.:</given-names>
          </string-name>
          <article-title>DATALOG with constraints - an answer-set programming system</article-title>
          .
          <source>In: AAAI/IAAI</source>
          , pp.
          <fpage>163</fpage>
          -
          <lpage>168</lpage>
          . AAAI Press / The MIT Press (
          <year>2000</year>
          )
        </mixed-citation>
      </ref>
      <ref id="ref4">
        <mixed-citation>
          4.
          <string-name>
            <surname>Gelfond</surname>
            ,
            <given-names>M.</given-names>
          </string-name>
          ,
          <string-name>
            <surname>Lifschitz</surname>
            ,
            <given-names>V.</given-names>
          </string-name>
          :
          <article-title>The Stable Model Semantics for Logic Programming</article-title>
          .
          <source>In: International Conference on Logic Programming</source>
          <year>1988</year>
          , pp.
          <fpage>1070</fpage>
          -
          <lpage>1080</lpage>
          (
          <year>1988</year>
          )
        </mixed-citation>
      </ref>
      <ref id="ref5">
        <mixed-citation>
          5.
          <string-name>
            <surname>Hermenegildo</surname>
            ,
            <given-names>M.</given-names>
          </string-name>
          ,
          <string-name>
            <surname>Bueno</surname>
            ,
            <given-names>F.</given-names>
          </string-name>
          ,
          <string-name>
            <surname>Carro</surname>
            ,
            <given-names>M.</given-names>
          </string-name>
          , Lo´pez,
          <string-name>
            <given-names>P.</given-names>
            ,
            <surname>Mera</surname>
          </string-name>
          ,
          <string-name>
            <given-names>E.</given-names>
            ,
            <surname>Morales</surname>
          </string-name>
          ,
          <string-name>
            <given-names>J.</given-names>
            ,
            <surname>Puebla</surname>
          </string-name>
          ,
          <string-name>
            <surname>G.</surname>
          </string-name>
          :
          <article-title>An Overview of Ciao and its Design Philosophy</article-title>
          .
          <source>TPLP</source>
          <volume>12</volume>
          (
          <issue>1-2</issue>
          ),
          <fpage>219</fpage>
          -
          <lpage>252</lpage>
          (
          <year>2012</year>
          )
        </mixed-citation>
      </ref>
      <ref id="ref6">
        <mixed-citation>
          6.
          <string-name>
            <surname>Janhunen</surname>
            ,
            <given-names>T.</given-names>
          </string-name>
          ,
          <string-name>
            <surname>Kaminski</surname>
            ,
            <given-names>R.</given-names>
          </string-name>
          ,
          <string-name>
            <surname>Ostrowski</surname>
            ,
            <given-names>M.</given-names>
          </string-name>
          ,
          <string-name>
            <surname>Schellhorn</surname>
            ,
            <given-names>S.</given-names>
          </string-name>
          ,
          <string-name>
            <surname>Wanko</surname>
            ,
            <given-names>P.</given-names>
          </string-name>
          ,
          <string-name>
            <surname>Schaub</surname>
            ,
            <given-names>T.</given-names>
          </string-name>
          :
          <article-title>Clingo goes Linear Constraints over Reals and Integers</article-title>
          .
          <source>TPLP</source>
          <volume>17</volume>
          (
          <issue>5-6</issue>
          ),
          <fpage>872</fpage>
          -
          <lpage>888</lpage>
          (
          <year>2017</year>
          )
        </mixed-citation>
      </ref>
      <ref id="ref7">
        <mixed-citation>
          7.
          <string-name>
            <surname>Lee</surname>
            ,
            <given-names>J.</given-names>
          </string-name>
          ,
          <string-name>
            <surname>Palla</surname>
          </string-name>
          , R.:
          <article-title>Reformulating the situation calculus and the event calculus in the general theory of stable models</article-title>
          .
          <source>Journal of Artificial Intelligence Research</source>
          <volume>43</volume>
          ,
          <fpage>571</fpage>
          -
          <lpage>620</lpage>
          (
          <year>2012</year>
          )
        </mixed-citation>
      </ref>
      <ref id="ref8">
        <mixed-citation>
          8.
          <string-name>
            <surname>Marple</surname>
            ,
            <given-names>K.</given-names>
          </string-name>
          ,
          <string-name>
            <surname>Salazar</surname>
            ,
            <given-names>E.</given-names>
          </string-name>
          ,
          <string-name>
            <surname>Gupta</surname>
          </string-name>
          , G.:
          <article-title>Computing stable models of normal logic programs without grounding</article-title>
          .
          <source>arXiv preprint arXiv:1709.00501</source>
          (
          <year>2017</year>
          )
        </mixed-citation>
      </ref>
      <ref id="ref9">
        <mixed-citation>
          9.
          <string-name>
            <surname>Mellarkod</surname>
            ,
            <given-names>V.S.</given-names>
          </string-name>
          ,
          <string-name>
            <surname>Gelfond</surname>
            ,
            <given-names>M.</given-names>
          </string-name>
          ,
          <string-name>
            <surname>Zhang</surname>
          </string-name>
          , Y.:
          <article-title>Integrating answer set programming and constraint logic programming</article-title>
          .
          <source>Ann. Math. Artif. Intell</source>
          .
          <volume>53</volume>
          (
          <issue>1-4</issue>
          ),
          <fpage>251</fpage>
          -
          <lpage>287</lpage>
          (
          <year>2008</year>
          )
        </mixed-citation>
      </ref>
      <ref id="ref10">
        <mixed-citation>
          10.
          <string-name>
            <surname>Mueller</surname>
          </string-name>
          , E.T.:
          <article-title>Commonsense reasoning: an event calculus based approach</article-title>
          . Morgan Kaufmann (
          <year>2014</year>
          )
        </mixed-citation>
      </ref>
    </ref-list>
  </back>
</article>