<!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>
      <journal-title-group>
        <journal-title>Virtual) Workshop on Goal-directed Execution of Answer Set Programs, September</journal-title>
      </journal-title-group>
    </journal-meta>
    <article-meta>
      <title-group>
        <article-title>A Short Tutorial on s(CASP), a Goal-directed Execution of Constraint Answer Set Programs</article-title>
      </title-group>
      <contrib-group>
        <contrib contrib-type="author">
          <string-name>Joaquín Arias</string-name>
          <xref ref-type="aff" rid="aff0">0</xref>
        </contrib>
        <contrib contrib-type="author">
          <string-name>Gopal Gupta</string-name>
          <xref ref-type="aff" rid="aff3">3</xref>
        </contrib>
        <contrib contrib-type="author">
          <string-name>Manuel Carro</string-name>
          <xref ref-type="aff" rid="aff1">1</xref>
          <xref ref-type="aff" rid="aff2">2</xref>
        </contrib>
        <aff id="aff0">
          <label>0</label>
          <institution>CETINIA, Universidad Rey Juan Carlos</institution>
          ,
          <addr-line>Madrid</addr-line>
          ,
          <country country="ES">Spain</country>
        </aff>
        <aff id="aff1">
          <label>1</label>
          <institution>IMDEA Software Institute</institution>
          ,
          <addr-line>Madrid</addr-line>
          ,
          <country country="ES">Spain</country>
        </aff>
        <aff id="aff2">
          <label>2</label>
          <institution>Universidad Politécnica de Madrid</institution>
          ,
          <country country="ES">Spain</country>
        </aff>
        <aff id="aff3">
          <label>3</label>
          <institution>University of Texas at Dallas</institution>
          ,
          <addr-line>Richardson</addr-line>
          ,
          <country country="US">USA</country>
        </aff>
      </contrib-group>
      <pub-date>
        <year>2021</year>
      </pub-date>
      <volume>20</volume>
      <issue>2021</issue>
      <fpage>0000</fpage>
      <lpage>0003</lpage>
      <abstract>
        <p>This paper presents a short tutorial on s(CASP), highlighting some of the novel aspects and the reasons for its design. The most important aspect of s(CASP) is its goal-directed top-down execution model that implements Constraints Answer Set Programming. The execution strategy of s(CAPS) avoids the grounding phase, present in most ASP systems, and can constraint variables that, as in CLP, are kept during the execution and in the answer sets. Additionally, s(CASP) generates a human-understandable justifications (in natural language) of the resulting answer sets. s(CASP) is implemented in Prolog (currently available for Ciao and SWI Prolog) and has been used in several applications, including medical advisors, avionic, legal reasoner, XAI, and natural language processing.</p>
      </abstract>
      <kwd-group>
        <kwd>eol&gt;Answer Set Programming</kwd>
        <kwd>Constraint</kwd>
        <kwd>Goal-directed</kwd>
        <kwd>s(CASP)</kwd>
        <kwd>Tutorial</kwd>
      </kwd-group>
    </article-meta>
  </front>
  <body>
    <sec id="sec-1">
      <title>-</title>
      <p>
        1. Introduction
• An automated reasoner that uses Event Calculus (EC) [
        <xref ref-type="bibr" rid="ref6">6</xref>
        ], available at http://bit.ly/
EventCalculus. The expressiveness of s(CASP) allows deductive reasoning tasks in
domains featuring constraints involving dense time and fluents with continuous properties.
It is being used to model real-world avionics systems, to verify (timed) properties as well
as to identify gaps with respect to system requirements [
        <xref ref-type="bibr" rid="ref7">7</xref>
        ].
• s(CASP) justification framework has been used to bring Explainable Artificial Intelligence
(XAI) principles to rule-based systems capturing expert knowledge [
        <xref ref-type="bibr" rid="ref3 ref8">8, 3</xref>
        ], ILP systems that
generate ASP programs [
        <xref ref-type="bibr" rid="ref9">9</xref>
        ], and concurrent imperative programs based on behavioral,
observable specifications [
        <xref ref-type="bibr" rid="ref10">10</xref>
        ].
• Two natural language understanding systems [
        <xref ref-type="bibr" rid="ref11">11</xref>
        ]: SQuARE, a Semantic-based Question
Answering and Reasoning Engine, and StaCACK, Stateful Conversational Agent using
Commonsense Knowledge. They use the s(CASP) engine to “truly understand” and
perform reasoning while generating a natural language explanation for their responses.
Building on these systems Kinjal, leader of one of the nine teams selected to participate in
Amazon Alexa Socialbot Grand Challenge 41, is developing a conversational AI chatbot.
• Jason Morris’ team2 has developed an expert system at the SMU Centre for Computational
Law at Singapore. They have coded rule 34 of Singapore’s Legal Profession. The front-end
of the system is a web interview that will collect information from a user, run the query
on s(CASP), and display the results with a corresponding explanation.
• s(LAW), an administrative and judicial discretion reasoner[
        <xref ref-type="bibr" rid="ref12">12</xref>
        ], which allows
modeling legal rules involving ambiguity and infers conclusion, providing (natural
language)justifications based on them.
      </p>
      <p>In addition, in January 2021, during the HackReason3 at UT Dallas, 17 teams developed
applications that rely on simulating human-style commonsense reasoning using s(CASP).</p>
      <p>This tutoral provides a brief description of s(CASP). Section 2 describes the installation
process. Section 3 presents the language supported by s(CASP). Section 4 provides some ticks
to tune the size, and/or the number of resulting answer sets and to adjust the constraint’s
accuracy. Section 5 gives a brief description of the available flags to generate justifications
and/or encoding (in natural language) with diferent levels of detail. In Section 6 we present
the Dynamic Consistency Check (DCC), still a work in progress, and how can be activated for
testing. Finally, in Section 7 we propose some ideas for improving s(CASP).
2. s(CASP) Installation on Ciao or SWI-Prolog
As we mentioned before, s(CASP) is currently available to be executed under Ciao http://
ciao-lang.org/, or SWI-Prolog https://www.swi-prolog.org/. The s(CASP) source code for Ciao
is available at https://gitlab.software.imdea.org/ciao-lang/sCASP and for SWI-Prolog, thanks to
Jan Wielemaker, at https://github.com/JanWielemaker/sCASP.</p>
      <p>1https://cs.utdallas.edu/computer-scientists-enhance-alexas-small-talk-skills/
2https://github.com/smucclaw/r34_sCASP
3https://hackreason.aisutd.org/
Installation on Ciao Ciao is a programming language that is builds from a logic-based
simple kernel and is designed to be extensible and modular. The system implements some
advanced features such as separate and incremental compilation, global program analysis
and static debugging and optimization (via source to source program transformation, CiaoPP
preprocessor), a build automation system, documentation generator, debugger, and
(Emacsbased) development environment.</p>
      <p>The installation of Ciao can be done using an interactive assistant from the command line in
three steps: first, we run the interactive assistant 4, then, we activate the added Ciao path, and
ifnally, we install s(CASP) as a bundle:
curl https://ciao-lang.org/boot -sSfL | sh
source ~/.washrag
ciao get gitlab.software.imdea.org/ciao-lang/sCASP</p>
      <p>When the installation succeeds, a folder with the source code and s(CASP) examples is created
in the ciao workspace (the default path is ~/.ciao/sCASP/). The --help_all flag generates
the list of all available flags 5 to consult and/or adapt s(CASP) behavior.</p>
      <p>Installation on SWI-Prolog SWI-Prolog ofers a comprehensive free Prolog environment.
Since its start in 1987, SWI-Prolog development has been driven by the needs of real-world
applications. SWI-Prolog is widely used in research and education as well as commercial
applications. Additionally, it ofers SWISH (https://swish.swi-prolog.org/), an online environment for
teaching and exchanging ideas. Therefore, thanks to Jan Wielemaker, we can not only generate
a standalone file, but also we have the opportunity to use s(CASP) directly in SWISH.</p>
      <p>To generate the standalone file we have to use the latest development release version of
SWIProlog available at https://www.swi-prolog.org/download/devel – there are already compiled
SWI-Prolog binaries for Linux, Windows, and MacOS. Then, to generate the executable file of
s(CASP), we just need to run the make file available in the corresponding s(CASP) repository
for SWI-Prolog (https://github.com/JanWielemaker/sCASP).
3. Getting started
s(CASP) extends the expressiveness of Answer Set Programming systems by featuring predicates,
constraints among non-ground variables, uninterpreted functions, and, most importantly, a
top-down, query-driven execution strategy. These features make it possible to return answers
with non-ground variables (possibly including constraints among them) and to compute partial
models by returning only the fragment of a stable model that is necessary to support the answer
to a given query.</p>
      <p>In s(CASP), and unlike Prolog’s negation as failure and ASP default negation, not p(X) can
return bindings for X on success, i.e., bindings for which the call p(X) would have failed.
4For Linux and MacOS use the sh-terminal, for Windows the “Windows Subsystem for Linux” is required.
5For the reader’s convenience the help_all output is available in Appendix A.</p>
      <p>Example 1. Considering the program p.pl6
1 p(a).
let’s run s(CASP) in the iterative mode by invoking scasp -i p.pl.</p>
      <p>Then, we introduce the query ?- not p(X). and s(CASP) would return the binding X \= a,
where \= is a disequality constraint meaning  ̸= , and the model {not p(X |{X \= a})}
that represents the set of  ( ) can be proven only when  ̸= .7</p>
      <p>Thanks to the interface of s(CASP) with constraint solvers, sound non-monotonic reasoning
with constraints is possible.</p>
    </sec>
    <sec id="sec-2">
      <title>Example 2. Consider the program p2.pl:</title>
      <p>1 p(X):- X &gt; 0.
which for the same query as above, returns the model {not p(X | {X ≤
0})}.</p>
      <p>As we mentioned before, s(CASP) supports uninterpreted functions (with the same
conventions as Prolog), e.g., [f(a)|Rest] denotes a list with head f(a) and tail Rest. While in
conventional ASP implementations this could give rise to an infinite grounded program, the
s(CASP) execution model can deal with them similarly to Prolog, with the added power of the
using constructive negation in the execution and returned models.</p>
      <p>
        Example 3. For the program member.pl
1 member(X, [X|Xs]).
2 member(X, [_|Xs]):- member(X, Xs).
3
4 list([
        <xref ref-type="bibr" rid="ref1 ref2 ref3 ref4 ref5">1,2,3,4,5</xref>
        ]).
5
6 ?- list(A), not member(B, A).
the predicate member/2 models the membership of a list as usual in (classical) logic programming.
      </p>
    </sec>
    <sec id="sec-3">
      <title>The query derives the conditions for an element B not to belong to a given list A.</title>
      <p>
        Since we include the query as part of the program, invoking s(CASP) in automatic mode8 it
returns the following model and binding:
{ list([
        <xref ref-type="bibr" rid="ref1 ref2 ref3 ref4 ref5">1,2,3,4,5</xref>
        ]),
not member(B | {B ̸= 1,B ̸= 2,B ̸= 3,B ̸= 4,B ̸= 5}, [
        <xref ref-type="bibr" rid="ref1 ref2 ref3 ref4 ref5">1,2,3,4,5</xref>
        ]),
not member(B | {B ̸= 1,B ̸= 2,B ̸= 3,B ̸= 4,B ̸= 5}, [
        <xref ref-type="bibr" rid="ref2 ref3 ref4 ref5">2,3,4,5</xref>
        ]),
not member(B | {B ̸= 1,B ̸= 2,B ̸= 3,B ̸= 4,B ̸= 5}, [
        <xref ref-type="bibr" rid="ref3 ref4 ref5">3,4,5</xref>
        ]),
not member(B | {B ̸= 1,B ̸= 2,B ̸= 3,B ̸= 4,B ̸= 5}, [
        <xref ref-type="bibr" rid="ref4 ref5">4,5</xref>
        ]),
not member(B | {B ̸= 1,B ̸= 2,B ̸= 3,B ̸= 4,B ̸= 5}, [
        <xref ref-type="bibr" rid="ref5">5</xref>
        ]),
not member(B | {B ̸= 1,B ̸= 2,B ̸= 3,B ̸= 4,B ̸= 5}, []) }
6All programs shown in this paper are available at http://platon.etsii.urjc.es/~jarias/papers/scasp-gde21/ and
the results described have been obtained using s(CASP) under Ciao (version 0.21.08.25).
      </p>
      <p>7Uniqueness of names is assumed for constants and function names: any two constants or functions with
diferent names represent diferent objects.</p>
      <p>
        8The automatic mode is selected by default so the flag -a or --auto can be omitted, i.e., scasp member.pl.
A = [
        <xref ref-type="bibr" rid="ref1 ref2 ref3 ref4 ref5">1,2,3,4,5</xref>
        ], B ̸= 1, B ̸= 2, B ̸= 3, B ̸= 4, B ̸= 5
s(CASP), like the ASP implementations, is based on stable model semantics [
        <xref ref-type="bibr" rid="ref4">4</xref>
        ] and, unlike
Prolog, supports non-stratified negation.
      </p>
    </sec>
    <sec id="sec-4">
      <title>Example 4. The following program, in weekend.pl:</title>
      <sec id="sec-4-1">
        <title>1 opera(saturday) :- not home(saturday). 2 home(saturday) :- not opera(saturday). 3 dinner(sunday).</title>
        <p>models that on Saturday, Bob either goes to the opera or stays home and on Sunday he has dinner.
The top-down evaluation of the non-stratified negation in lines 1-2 makes a loop with an even
number of intervening negations (even loop). When the s(CASP) metainterpreter detects an even
loop, the truth/falsehood of the atoms involved is assumed to generate diferent models, the
consistency of which is subsequently checked. In this example, there are two possible models, and given
a query s(CASP) returns (if exists) the relevant partial model for the query:
?- opera(saturday)
?- home(saturday)
?- dinner(sunday)
?- opera(saturday),home(saturday)
returns {opera(saturday),not home(saturday)}.
returns {home(saturday),not opera(saturday)}.</p>
        <p>returns {dinner(sunday)}.</p>
        <p>returns no models.</p>
      </sec>
    </sec>
    <sec id="sec-5">
      <title>Note that opera(saturday) and home(saturday) cannot be part of the same model simultaneously, so for the last query, there are no (partial) models.</title>
      <p>In addition to default negation, s(CASP) supports classical negation (using the prefix ’ -’) to
capture the explicit evidence that a literal is false, e.g. not opera(saturday) means that we
have no evidence that Bob goes to the opera (we can not prove it), and -opera(saturday)
means that we have explicit evidence that Bob does not go to the opera (there is a proof for it).
4. Tuning of the Partial Answer Sets Output
In this section, we describe directives and flags that can be used to tune the output of s(CASP) to
select the atoms that appear in the partial model, avoid consistency checks, generate a specific
number of answers, and/or determine the accuracy of the real numbers.</p>
      <p>Select the atoms that appear in the partial model: The directive #show can be used to
select which atoms should appear in the partial models.</p>
    </sec>
    <sec id="sec-6">
      <title>Example 5. Consider the program weekend_show.pl that includes the directive:</title>
      <sec id="sec-6-1">
        <title>1 #show opera/1, home/1, dinner/1.</title>
        <p>to the program in Example 4. Now, in the partial models returned by s(CASP) only positive atoms
appear, so for the query ?- opera(saturday) it returns {opera(saturday}.</p>
        <p>Negated atoms can also be selected, e.g., #show not home/1,-opera/1 is also valid.
1 opera(D) :- not home(D).
2 home(D) :- not opera(D).
3 home(monday).
4
5 :- baby(D), opera(D).
6
7 baby(tuesday).
8
9 ?- opera(D).
Denials: In ASP, denials are constructions of the form :- p, q, i.e., rules without head, and
are used to express that the conjunction of atoms p ∧ q cannot be true: either p, q, or both,
have to be false in any stable model. Additionally, the s(CASP) compiler also detects statically
rules of the form r:- q,not r., called olon rules, and introduces denials to ensure that models
satisfy ¬ ∨  even if the atoms r or q are not needed to solve the query. Let us look at two
examples.</p>
      </sec>
    </sec>
    <sec id="sec-7">
      <title>Example 6. For the following program in olon.pl</title>
      <p>1 p :- not q.</p>
      <p>2 q :- not p.</p>
      <p>3 r :- not r.
the compiler introduces the denial :- not r which is checked for consistency after any given
query. Therefore, s(CASP) returns no models, regardless of the initial query.</p>
    </sec>
    <sec id="sec-8">
      <title>Example 7. Fig. 1 shows the code of opera.pl, an extended version of weekend.pl.</title>
      <sec id="sec-8-1">
        <title>The denial in line 5 expresses that the conjunction of atoms baby(D) ∧ opera(D) cannot be</title>
        <p>simultaneously true for any value of D. Thus for the query in line 9 the resulting partial model is:
{ opera(D | {D \= monday,D \= tuesday}), not home(D | {D \= monday,D \= tuesday}),
not baby(Var1 | {Var1 \= tuesday}), baby(tuesday), not opera(tuesday), home(tuesday) }
where the atom opera({D |{D \= monday,D \= tuesday}) means that Bob can go to the opera
any day except on Monday or Tuesday.</p>
        <p>For debugging purposes, s(CASP) provides flags to disable consistency checks: (i) --no_olon
disables the consistency check of denials introduced by the compiler due to olon rules but
checks the user-defined denials; (ii) --no_nmr disables consistency check of all non-monotonic
rules. In Example 6, runing s(CASP) by invoking scasp --no_nmr -i olon.pl, for the
query ?- p returns the model {p,not q} which may be useful for debugging.</p>
        <p>
          Since consistency checks are evaluated when a tentative partial model is encountered, they
introduce a run-time penalty. To reduce this overhead we propose a Dynamic Consistency
Check (DCC) [
          <xref ref-type="bibr" rid="ref13">13</xref>
          ] which triggers NMR checks as soon a literal involving them is added to the
partial model. In Section 6 we present promising preliminary results of this technique.
Partial Answer Sets: While in mainstream ASP implementations each answer corresponds
to a stable model, in s(CASP) each answer is a partial answer set containing the subset of
(negated) atoms that supports the query with specific binding of the free variables in the query.
Consequently, two answer sets for a query may (not) correspond to the same stable model.
Example 8. Considering the member.pl program of Example 3, for the query ?- list(A),
member(B,A) s(CASP) generates five answers sets, corresponding to the five possible bindings
of B, i.e., B=1, B=2, B=3, B=4, and B=5. While all five answer sets correspond to a single stable
model, each partial model is diferent because they contain only the atoms needed to support the
query with that specific binding, i.e., the partial answer set for B=1 is:
{ list([
          <xref ref-type="bibr" rid="ref1 ref2 ref3 ref4 ref5">1,2,3,4,5</xref>
          ]), member(1,[
          <xref ref-type="bibr" rid="ref1 ref2 ref3 ref4 ref5">1,2,3,4,5</xref>
          ]) }
and the partial answer set for B=2 is:
{ list([
          <xref ref-type="bibr" rid="ref1 ref2 ref3 ref4 ref5">1,2,3,4,5</xref>
          ]), member(2,[
          <xref ref-type="bibr" rid="ref1 ref2 ref3 ref4 ref5">1,2,3,4,5</xref>
          ]), member(2,[
          <xref ref-type="bibr" rid="ref2 ref3 ref4 ref5">2,3,4,5</xref>
          ]) }
        </p>
        <p>The -sN or -nN flag can be used to specify the number N of answer sets that s(CASP) should
return in automated mode, and by adding the -s0 or -n0 flag s(CASP) would return all the
possible answers sets.</p>
        <p>
          Constraint precision: The s(CASP) system has a generic interface to enable plugging in
constraint solvers. s(CASP) currently includes the CLP(Q) linear constraints solver [
          <xref ref-type="bibr" rid="ref14">14</xref>
          ], that
supports the arithmetic constraints &lt;, &gt;, =, ≤ , ≥ . The selection of CLP(Q) instead of the faster
CLP(R) is motivated by sound reasons. To transform the output from rationals to floating-point
numbers, s(CASP) provides the flag -r[=d], where d can be used to determine the (maximum)
number of decimal places (default is 5 decimal places).
        </p>
      </sec>
    </sec>
    <sec id="sec-9">
      <title>Example 9. For the following program, in rationals.pl</title>
      <p>
        1 s(X,Y) :- X #= Y * 7/53.
the query ?- s(X,4.35) returns the binding X=609/1060, invoking s(CASP) without flags, and
returns the binding X=0.574 invoking scasp -r=3 rationals.pl.
5. Justification and encoding (in natural language)
In the context of the European Union approved the General Data Protection Regulation
(GDPR) [
        <xref ref-type="bibr" rid="ref15">15</xref>
        ] and Explainable Artificial Intelligence [
        <xref ref-type="bibr" rid="ref16">16</xref>
        ] (XAI), we are facing challenges that
demand XAI to understand, appropriately trust, and efectively manage an emerging generation
of artificially intelligent machine partners. However, justifying why an answer is a consequence
from an ASP program may be non-trivial, more so when the user is an expert in a given domain,
but not necessarily knowledgeable in ASP.
      </p>
      <p>Justifications: s(CASP) uses top-down evaluation trees to generate minimal justifications in
which it is possible to control which literals should appear.</p>
      <p>JUSTIFICATION_TREE:
we assume that Bob goes to the opera on a day D not equal monday, nor tuesday, because</p>
      <p>Bob does not stay at home on D not equal monday, nor tuesday.</p>
      <p>The global constraints hold, because
the global constraint number 1 holds, because
there is no evidence that 'baby' holds (for Var1), with Var1 not equal tuesday, and
'baby' holds (for tuesday), and
we assume that there is no evidence that Bob goes to the opera on the day tuesday, because
'home' holds (for tuesday).</p>
      <p>D \= tuesday}) is assumed to holds,
and this assumption is consistent with
the denial. To check the denial, i.e., that
the conjunction of atoms baby(D) ∧</p>
      <p>opera(D) is not simultaneously true
for any value of D, s(CASP) checks that
∀ D ( not baby(D) ∨ (baby(D)
∧ not opera(D)) ).</p>
      <sec id="sec-9-1">
        <title>The flags used to control the literals that Figure 2: Justification of opera.pl.</title>
        <p>appear in the justification are: (i) mid, used
by default, which only displays the user-defined predicates; (ii) long that displays all predicates,
including auxiliary predicates such as the forall/2 used to check this denial; and (iii) short
which only displays the annotated literals. Additionally, the flag neg, used by default, includes
the default-negated version of the annotated/selected predicates, whereas the flag pos does not.</p>
        <p>The s(CASP) justification framework provides a mechanism for presenting natural language
justifications using a generic translation and the possibility of customize it with directives that
provide translation patterns. Both plain text and expandable, user-friendly HTML files can be
generated.</p>
      </sec>
    </sec>
    <sec id="sec-10">
      <title>Example 11. (cont. Example 10) Consider the following module in opera.pred.</title>
      <p>When executed s(CASP) by invoking scasp --tree --human opera.pl opera.pred,
the natural language justification is obtained (Figure 3). Note that lines 1, 2, and 7 follow the
translation patterns defined in opera.pred.</p>
      <p>Additionally, adding the --html=bob flag, s(CASP) generates the HTML file bob.html, which
includes the query, the bindings and an expandable justification tree that can be expanded and/or
collapsed to facilitate its analysis.</p>
      <p>
        Compiled code: Given an ASP program, a dual program compiled by s(CASP) will include
the rules that express the constructive negation of the predicates in the original ASP program [
        <xref ref-type="bibr" rid="ref2">2</xref>
        ].
These dual rules provide a means to constructively determine the constraints under which a
predicate would fail.
      </p>
      <p>During the generation of the duals, for each clause, the compiler generates independent
clauses with the “negated” literals in its body, and to avoid redundant answers, every ℎ clause
for a negated literal includes calls to any ℎ literal with  &lt; .</p>
      <p>Example 12. For the clause h(X,Y):- r(X),not s(X,Y),q(Y), its dual is:
1 not h(X,Y) :- not r(X).
2 not h(X,Y) :- r(X), s(X,Y).
3 not h(X,Y) :- r(X), not s(X,Y), not q(Y).</p>
    </sec>
    <sec id="sec-11">
      <title>In propositional programs this optimization is not desirable because we may lose relevant answers, so s(CASP) provides the -d or --plaindual flag to generate the duals with single-goal clauses.</title>
      <p>When we want to modify the dual program, we can use the --code9 flag to output the dual
program, and then load an updated version with the -c or --compiled flag.</p>
      <p>Additionally, translation patterns can be used to display the compiled program in natural
language, thereby making it easier for experts without a programming background to understand
both the program and the results of its execution.</p>
    </sec>
    <sec id="sec-12">
      <title>Example 13. (cont. Example 11) By invoking scasp --code --human opera.pl opera.pred, s(CASP) would display the dual program (including the duals and denials), following the translation patterns and/or the generic translation.</title>
      <p>6. Dynamic Consistency Check (Work in Progress)
As we mentioned before, the denials are evaluated after the query, ensuring that the tentative
partial model is consistent with them. In cases where this evaluation fails the execution
backtracks to check other alternatives. The goal of the Dynamic Consistency Check (DCC) is to
check the denials as soon as atoms involved are added to the tentative partial model.</p>
      <p>Our proposal is based on the constraint propagation algorithm, which, when the domain of a
decision variable is modified, examines the constraints containing that variable to determine
whether any values in the domains of other decision variables are now inconsistent. During ASP
program evaluation, when an atom is added to the tentative partial model, the denials containing
that atom are examined to determine if the presence of other atoms makes the tentative partial
model inconsistent. In this case, the evaluation fails and is immediately backtracked to avoid
computational waste. We believe that the benefits of the denial propagation in detecting
9By adding the --raw flag, the system prints the clauses in the same order as the compiler does.
1 % Graph
2 vertex(a).
3 vertex(b).
4 vertex(c).
5 vertex(d).
inconsistencies as early as possible outweigh the overhead introduced (i) during compilation to
generate the DCC rules used to trigger denials involving specific atoms, and (ii) due to the extra
work done to check denials when there are no inconsistencies.</p>
      <p>Example 14. Consider the standard ASP code for the Hamiltonian problem, in hamiltonian.pl
1 #show chosen/2.
2 reachable(V) :- chosen(V, a).
3 reachable(V) :- chosen(V,U), reachable(U).
4 % Choose or not an edge of the graph.
5 chosen(U,V) :- edge(U,V), not other(U,V).
6 other(U,V) :- edge(U,V), not chosen(U,V).
7 % Every vertex must be reachable.
8 :- vertex(U), not reachable(U).
9 % Do not choose edges to/from the same vertex
10 :- chosen(U,W), U \= V, chosen(V,W).
11 :- chosen(W,U), U \= V, chosen(W,V).
12 ?- reachable(a).
where the denials in lines 10-11 are used to discard those tentative models that have chosen edges
violating the properties of the Hamiltonian cycle. For the query in line 12, using the graph in Fig. 4
there are three stable models, one for each Hamiltonian cycle:
1 { chosen(a,c), chosen(c,d), chosen(d,b), chosen(b,a),. . . }
2 { chosen(a,b), chosen(b,c), chosen(c,d), chosen(d,a),. . . }
3 { chosen(a,d), chosen(d,b), chosen(b,c), chosen(c,a),. . . }</p>
    </sec>
    <sec id="sec-13">
      <title>However, s(CASP) follows the generate-and-test paradigm, i.e., it generates multiple cycles that are checked for consistency when a cycle that reaches all vertex is complete. As a consequence, if the evaluation chose two edges to/from the same vertex, trying combinations (on backtracking) with the rest of edges would be waste of efort.</title>
      <p>
        Evaluation: We compared the performance of s(CASP) with and without DCC using a MacOS
11.5.2 Intel Core i7 at 2.6GHz. We observe that using DCC, for the evaluation of the Hamiltonian
problem (Example 14) with the graph in Figure 4, we obtain a speedup of 6.8:
• Without DCC: invoking scasp --prev_forall -n0 hamiltonian.pl
graph.pl.10 the evaluation of the three models takes 8.266s.
• With DCC: invoking scasp --prev_forall -n0 --dcc hamiltonian.pl
graph.pl hamiltonian_dcc.pl,11 the evaluation, using this preliminary DCC
implementation, takes only 1.215s.
10Note that we are using a specific forall/2 implementation to run this example (more details in Section 7).
11Note that the DCC implementation is unfinished and the DCC rules for the denials in lines 10-11 are included
manually in hamiltonian_dcc.pl. In the future, they will be compiled automatically.
7. Future Work
As already mentioned, the implementation has been improved in several aspect but can still be
substantially improved, and in particular we are planning to:
• Finish the DCC implementation and work on using analysis to optimize the compilation
of the DCC rules, being able to interleave their execution with the top-down strategy to
discard models as soon as they are shown to be inconsistent.
• Use dependency analysis to improve the generation of the dual programs.
• Apply partial evaluation and better compilation techniques to remove (part of) the
overhead brought about by the interpreting approach.
• Optimize the implementation of the c-forall algorithm using tabling [
        <xref ref-type="bibr" rid="ref17">17</xref>
        ]. Currently there
are four alternatives (by default, all_c_forall, prev_forall, and sasp_forall)
but all of them enter loops, generate redundant answers and/or discard solutions.
• In addition, tabling, and ATCLP [
        <xref ref-type="bibr" rid="ref18">18</xref>
        ], can be use to (i) collect the minimal partial models,
increasing performance and readability, and (ii) increase the range of supported programs,
by handling positive variant loops that are now detected and halted. Note that halting
variant loops may cause a loss of solutions, so s(CASP) provides the --variant flag to
disable this behavior which can lead to loops.
• Improve the disequality constraint solver to handle pending cases. The -w or --warning
lfag can be used to warn if an unsupported constraint or variant loop is detected.
• Finally, although s(CASP) provides flags ( v, v0, v1, and v2) to trace a program evaluation,
better integration with the Ciao and SWI-Prolog debuggers is desirable.
--dcc
--no_olon
--no_nmr
-w, --warning
--variant
-m, --minimal
--raw
      </p>
      <p>Print this help message and terminate.</p>
      <p>Print extended help.</p>
      <p>Run in interactive mode (REP loop).</p>
      <p>Run in batch mode (no user interaction).</p>
      <p>Compute N answer sets, where N &gt;= 0. N = 0 means ’all’.</p>
      <p>Load compiled files (e.g. extracted using --code).</p>
      <p>Generate dual program with single-goal clauses
(for propositional programs).</p>
      <p>Output rational numbers as real numbers.
[d] determines precision. Defaults to d = 5.</p>
      <p>Print program with dual clauses and exit.</p>
      <p>Print justification tree for each answer (if any).</p>
      <p>Output code / justification tree as literals (default).</p>
      <p>Output code / justification tree in natural language.</p>
      <p>Output long version of justification.</p>
      <p>Output mid-sized version of justification (default) .</p>
      <p>Short version of justification.</p>
      <p>Only display the selected literals in the justification.</p>
      <p>Add the negated literals in the justification (default).</p>
      <p>Generate HTML file for the justification. [name]:
use ’name.html’. Default: first InputFile name.</p>
      <p>Trace user-predicate calls.</p>
      <p>Trace user-predicate calls (show tree).</p>
      <p>Trace user-predicate failures.</p>
      <p>Trace user-predicate failures (show tree).</p>
      <p>Automatically update s(CASP).</p>
      <p>Output the current version of s(CASP)
Activate the Dynamic Consistency Check.</p>
      <p>Exhaustive evaluation of c_forall/2.</p>
      <p>Deprecated evaluation of forall/2.</p>
      <p>Deprecated evaluation of forall/2.</p>
      <p>Do not compile olon rules (for debugging purposes).</p>
      <p>Do not compile NMR checks (for debugging purposes).</p>
      <p>Enable warning messages (failures in variant loops / disequality).</p>
      <p>Do not fail in the presence of variant loops.</p>
      <p>Collect only the minimal models (TABLING required).</p>
      <p>Sort the clauses as s(ASP) does (use with --code).</p>
    </sec>
  </body>
  <back>
    <ref-list>
      <ref id="ref1">
        <mixed-citation>
          [1]
          <string-name>
            <given-names>J.</given-names>
            <surname>Arias</surname>
          </string-name>
          ,
          <string-name>
            <given-names>M.</given-names>
            <surname>Carro</surname>
          </string-name>
          ,
          <string-name>
            <given-names>E.</given-names>
            <surname>Salazar</surname>
          </string-name>
          ,
          <string-name>
            <given-names>K.</given-names>
            <surname>Marple</surname>
          </string-name>
          ,
          <string-name>
            <surname>G.</surname>
          </string-name>
          <article-title>Gupta, Constraint Answer Set Programming without Grounding</article-title>
          ,
          <source>Theory and Practice of Logic Programming</source>
          <volume>18</volume>
          (
          <year>2018</year>
          )
          <fpage>337</fpage>
          -
          <lpage>354</lpage>
          . doi:
          <volume>10</volume>
          . 1017/S1471068418000285.
        </mixed-citation>
      </ref>
      <ref id="ref2">
        <mixed-citation>
          [2]
          <string-name>
            <given-names>K.</given-names>
            <surname>Marple</surname>
          </string-name>
          , E. Salazar, G. Gupta,
          <source>Computing Stable Models of Normal Logic Programs Without Grounding, arXiv 1709.00501</source>
          (
          <year>2017</year>
          ). URL: http://arxiv.org/abs/1709.00501. arXiv:
          <volume>1709</volume>
          .
          <fpage>00501</fpage>
          .
        </mixed-citation>
      </ref>
      <ref id="ref3">
        <mixed-citation>
          [3]
          <string-name>
            <given-names>J.</given-names>
            <surname>Arias</surname>
          </string-name>
          ,
          <string-name>
            <given-names>M.</given-names>
            <surname>Carro</surname>
          </string-name>
          ,
          <string-name>
            <given-names>Z.</given-names>
            <surname>Chen</surname>
          </string-name>
          , G. Gupta,
          <article-title>Justifications for goal-directed constraint answer set programming</article-title>
          ,
          <source>in: Proceedings 36th International Conference on Logic Programming (Technical Communications)</source>
          , volume
          <volume>325</volume>
          <source>of EPTCS</source>
          , Open Publishing Association,
          <year>2020</year>
          , pp.
          <fpage>59</fpage>
          -
          <lpage>72</lpage>
          . doi:
          <volume>10</volume>
          .4204/EPTCS.325.12.
        </mixed-citation>
      </ref>
      <ref id="ref4">
        <mixed-citation>
          [4]
          <string-name>
            <given-names>M.</given-names>
            <surname>Gelfond</surname>
          </string-name>
          ,
          <string-name>
            <given-names>V.</given-names>
            <surname>Lifschitz</surname>
          </string-name>
          ,
          <article-title>The Stable Model Semantics for Logic Programming</article-title>
          ,
          <source>in: 5th International Conference on Logic Programming</source>
          ,
          <year>1988</year>
          , pp.
          <fpage>1070</fpage>
          -
          <lpage>1080</lpage>
          . URL: http://www. cse.unsw.edu.au/~cs4415/2010/resources/stable.pdf.
        </mixed-citation>
      </ref>
      <ref id="ref5">
        <mixed-citation>
          [5]
          <string-name>
            <given-names>L. M.</given-names>
            <surname>Pereira</surname>
          </string-name>
          ,
          <string-name>
            <given-names>J. N.</given-names>
            <surname>Aparício</surname>
          </string-name>
          ,
          <article-title>Relevant counterfactuals</article-title>
          ,
          <source>in: EPIA 89, 4th Portuguese Conference on Artificial Intelligence</source>
          , Lisbon, Portugal,
          <source>September 26-29</source>
          ,
          <year>1989</year>
          , Proceedings,
          <year>1989</year>
          , pp.
          <fpage>107</fpage>
          -
          <lpage>118</lpage>
          . doi:
          <volume>10</volume>
          .1007/3-540-51665-4\_
          <fpage>78</fpage>
          .
        </mixed-citation>
      </ref>
      <ref id="ref6">
        <mixed-citation>
          [6]
          <string-name>
            <given-names>J.</given-names>
            <surname>Arias</surname>
          </string-name>
          ,
          <string-name>
            <given-names>Z.</given-names>
            <surname>Chen</surname>
          </string-name>
          ,
          <string-name>
            <given-names>M.</given-names>
            <surname>Carro</surname>
          </string-name>
          , G. Gupta,
          <article-title>Modeling and Reasoning in Event Calculus Using Goal-Directed Constraint Answer Set Programming</article-title>
          ,
          <source>in: Pre-Proc. of the 29th Int'l. Symposium on Logic-based Program Synthesis and Transformation</source>
          ,
          <year>2019</year>
          .
        </mixed-citation>
      </ref>
      <ref id="ref7">
        <mixed-citation>
          [7]
          <string-name>
            <given-names>B.</given-names>
            <surname>Hall</surname>
          </string-name>
          ,
          <string-name>
            <given-names>S. C.</given-names>
            <surname>Varanasi</surname>
          </string-name>
          ,
          <string-name>
            <given-names>J.</given-names>
            <surname>Fiedor</surname>
          </string-name>
          ,
          <string-name>
            <given-names>J.</given-names>
            <surname>Arias</surname>
          </string-name>
          ,
          <string-name>
            <given-names>K.</given-names>
            <surname>Basu</surname>
          </string-name>
          ,
          <string-name>
            <given-names>F.</given-names>
            <surname>Li</surname>
          </string-name>
          ,
          <string-name>
            <given-names>D.</given-names>
            <surname>Bhatt</surname>
          </string-name>
          ,
          <string-name>
            <given-names>K.</given-names>
            <surname>Driscoll</surname>
          </string-name>
          , E. Salazar, G. Gupta,
          <article-title>Knowledge-Assisted Reasoning of Model-Augmented System Requirements with Event Calculus and Goal-Directed Answer Set Programming</article-title>
          ,
          <source>in: Proc. 8th Workshop on Horn Clause Verification and Synthesis</source>
          ,
          <year>2021</year>
          .
        </mixed-citation>
      </ref>
      <ref id="ref8">
        <mixed-citation>
          [8]
          <string-name>
            <given-names>Z.</given-names>
            <surname>Chen</surname>
          </string-name>
          ,
          <string-name>
            <given-names>K.</given-names>
            <surname>Marple</surname>
          </string-name>
          , E. Salazar,
          <string-name>
            <given-names>G.</given-names>
            <surname>Gupta</surname>
          </string-name>
          ,
          <string-name>
            <given-names>L.</given-names>
            <surname>Tamil</surname>
          </string-name>
          ,
          <string-name>
            <given-names>A Physician</given-names>
            <surname>Advisory</surname>
          </string-name>
          <article-title>System for Chronic Heart Failure Management Based on Knowledge Patterns</article-title>
          ,
          <source>Theory and Practice of Logic Programming</source>
          <volume>16</volume>
          (
          <year>2016</year>
          )
          <fpage>604</fpage>
          -
          <lpage>618</lpage>
          . doi:
          <volume>10</volume>
          .1017/S1471068416000429.
        </mixed-citation>
      </ref>
      <ref id="ref9">
        <mixed-citation>
          [9]
          <string-name>
            <given-names>F.</given-names>
            <surname>Shakerin</surname>
          </string-name>
          ,
          <string-name>
            <surname>G.</surname>
          </string-name>
          <article-title>Gupta, Induction of Non-Monotonic Logic Programs to Explain Boosted Tree Models Using LIME</article-title>
          ,
          <source>in: AAAI</source>
          <year>2019</year>
          ,
          <year>2019</year>
          , pp.
          <fpage>3052</fpage>
          -
          <lpage>3059</lpage>
          . doi:
          <volume>10</volume>
          .1609/aaai.v33i01.
          <fpage>33013052</fpage>
          .
        </mixed-citation>
      </ref>
      <ref id="ref10">
        <mixed-citation>
          [10]
          <string-name>
            <given-names>S. C.</given-names>
            <surname>Varanasi</surname>
          </string-name>
          ,
          <string-name>
            <given-names>E.</given-names>
            <surname>Salazar</surname>
          </string-name>
          ,
          <string-name>
            <given-names>N.</given-names>
            <surname>Mittal</surname>
          </string-name>
          , G. Gupta,
          <article-title>Synthesizing Imperative Code from Answer Set Programming Specifications</article-title>
          , in: LOPSTR, volume
          <volume>12042</volume>
          of Lecture Notes in Computer Science, Springer,
          <year>2019</year>
          , pp.
          <fpage>75</fpage>
          -
          <lpage>89</lpage>
          . doi:
          <volume>10</volume>
          .1007/978-3-
          <fpage>030</fpage>
          -45260-5\_5.
        </mixed-citation>
      </ref>
      <ref id="ref11">
        <mixed-citation>
          [11]
          <string-name>
            <given-names>K.</given-names>
            <surname>Basu</surname>
          </string-name>
          ,
          <string-name>
            <given-names>S.</given-names>
            <surname>Varanasi</surname>
          </string-name>
          ,
          <string-name>
            <given-names>F.</given-names>
            <surname>Shakerin</surname>
          </string-name>
          ,
          <string-name>
            <given-names>J.</given-names>
            <surname>Arias</surname>
          </string-name>
          , G. Gupta,
          <article-title>Knowledge-driven Natural Language Understanding of English Text and its Applications</article-title>
          , AAAI'
          <volume>21</volume>
          (
          <year>2021</year>
          ).
        </mixed-citation>
      </ref>
      <ref id="ref12">
        <mixed-citation>
          [12]
          <string-name>
            <given-names>J.</given-names>
            <surname>Arias</surname>
          </string-name>
          ,
          <string-name>
            <given-names>M.</given-names>
            <surname>Moreno-Rebato</surname>
          </string-name>
          ,
          <string-name>
            <given-names>J. A.</given-names>
            <surname>Rodriguez-García</surname>
          </string-name>
          ,
          <string-name>
            <given-names>S.</given-names>
            <surname>Ossowski</surname>
          </string-name>
          ,
          <article-title>Modeling Administrative Discretion Using Goal-Directed Answer Set Programming</article-title>
          ,
          <source>in: Advances in Artificial Intelligence, CAEPIA</source>
          <volume>20</volume>
          /21, Springer International Publishing, Cham,
          <year>2021</year>
          , pp.
          <fpage>258</fpage>
          -
          <lpage>267</lpage>
          .
        </mixed-citation>
      </ref>
      <ref id="ref13">
        <mixed-citation>
          [13]
          <string-name>
            <given-names>K.</given-names>
            <surname>Marple</surname>
          </string-name>
          , G. Gupta,
          <article-title>Dynamic Consistency Checking in Goal-Directed Answer Set Programming</article-title>
          ,
          <source>Theory and Practice of Loging Programming</source>
          <volume>14</volume>
          (
          <year>2014</year>
          )
          <fpage>415</fpage>
          -
          <lpage>427</lpage>
          . URL: https://doi.org/10.1017/S1471068414000118. doi:
          <volume>10</volume>
          .1017/S1471068414000118.
        </mixed-citation>
      </ref>
      <ref id="ref14">
        <mixed-citation>
          [14]
          <string-name>
            <given-names>C.</given-names>
            <surname>Holzbaur</surname>
          </string-name>
          ,
          <string-name>
            <surname>OFAI CLP(Q,R) Manual</surname>
          </string-name>
          , Edition
          <volume>1</volume>
          .3.3,
          <string-name>
            <given-names>Technical</given-names>
            <surname>Report</surname>
          </string-name>
          TR-
          <volume>95</volume>
          -09, Austrian Research Institute for Artificial Intelligence, Vienna,
          <year>1995</year>
          .
        </mixed-citation>
      </ref>
      <ref id="ref15">
        <mixed-citation>
          [15]
          <string-name>
            <given-names>European</given-names>
            <surname>Union</surname>
          </string-name>
          ,
          <article-title>General Data Protection Regulation (GDPR), Regulation (EU) 2016/679 of the European Parliament</article-title>
          and of the Council,
          <year>2016</year>
          . https://eur-lex.europa.eu/eli/reg/2016/ 679/oj.
        </mixed-citation>
      </ref>
      <ref id="ref16">
        <mixed-citation>
          <source>[16] DARPA, Explainable Artificial Intelligence (XAI)</source>
          ,
          <source>Defense Advanced Research Projects Agency</source>
          ,
          <year>2017</year>
          . https://www.darpa.mil/program/explainable-artificial-intelligence.
        </mixed-citation>
      </ref>
      <ref id="ref17">
        <mixed-citation>
          [17]
          <string-name>
            <given-names>J.</given-names>
            <surname>Arias</surname>
          </string-name>
          ,
          <string-name>
            <given-names>M.</given-names>
            <surname>Carro</surname>
          </string-name>
          , Description, Implementation, and
          <article-title>Evaluation of a Generic Design for Tabled CLP</article-title>
          ,
          <source>Theory and Practice of Logic Programming</source>
          <volume>19</volume>
          (
          <year>2019</year>
          )
          <fpage>412</fpage>
          -
          <lpage>448</lpage>
          . URL: https://arxiv.org/abs/
          <year>1809</year>
          .05771. doi:
          <volume>10</volume>
          .1017/S1471068418000571.
        </mixed-citation>
      </ref>
      <ref id="ref18">
        <mixed-citation>
          [18]
          <string-name>
            <given-names>J.</given-names>
            <surname>Arias</surname>
          </string-name>
          ,
          <string-name>
            <surname>M.</surname>
          </string-name>
          <article-title>Carro, Incremental evaluation of lattice-based aggregates in logic programming using modular TCLP</article-title>
          , in: J. J.
          <string-name>
            <surname>Alferes</surname>
          </string-name>
          , M. Johansson (Eds.),
          <source>21st Int'l. Symposium on Practical Aspects of Declarative Languages</source>
          , volume
          <volume>11372</volume>
          <source>of LNCS</source>
          , Springer,
          <year>2019</year>
          , pp.
          <fpage>98</fpage>
          -
          <lpage>114</lpage>
          . URL: https://doi.org/10.1007/978-3-
          <fpage>030</fpage>
          -05998-
          <issue>9</issue>
          _7. doi:
          <volume>10</volume>
          .1007/978-3-
          <fpage>030</fpage>
          -05998-
          <issue>9</issue>
          _
          <fpage>7</fpage>
          .
        </mixed-citation>
      </ref>
    </ref-list>
  </back>
</article>