<!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>s(CASP) for SWI-Prolog</article-title>
      </title-group>
      <contrib-group>
        <contrib contrib-type="author">
          <string-name>Jan Wielemaker</string-name>
          <xref ref-type="aff" rid="aff2">2</xref>
        </contrib>
        <contrib contrib-type="author">
          <string-name>Joaquín Arias</string-name>
          <xref ref-type="aff" rid="aff1">1</xref>
        </contrib>
        <contrib contrib-type="author">
          <string-name>Gopal Gupta</string-name>
          <xref ref-type="aff" rid="aff0">0</xref>
        </contrib>
        <contrib contrib-type="author">
          <string-name>SWI-Prolog Solutions b.v.</string-name>
        </contrib>
        <contrib contrib-type="author">
          <string-name>Amsterdam</string-name>
        </contrib>
        <contrib contrib-type="author">
          <string-name>The Netherlands</string-name>
        </contrib>
        <aff id="aff0">
          <label>0</label>
          <institution>The University of Texas at Dallas</institution>
          ,
          <addr-line>Dallas</addr-line>
          ,
          <country country="US">USA</country>
        </aff>
        <aff id="aff1">
          <label>1</label>
          <institution>Universidad Rey Juan Carlos</institution>
          ,
          <addr-line>Calle Tulipán s/n, 28933 Móstoles (Madrid)</addr-line>
          ,
          <country country="ES">Spain</country>
        </aff>
        <aff id="aff2">
          <label>2</label>
          <institution>Vrije Universiteit Amsterdam</institution>
          ,
          <addr-line>De Boelelaan 1105, 1081 HV Amsterdam</addr-line>
          ,
          <country country="NL">The Netherlands</country>
        </aff>
      </contrib-group>
      <pub-date>
        <year>2021</year>
      </pub-date>
      <volume>20</volume>
      <issue>2021</issue>
      <fpage>0000</fpage>
      <lpage>0001</lpage>
      <abstract>
        <p>s(CASP) is related to ASP. Unlike ASP, which is traditionally solved using grounding and a SAT solver, s(CASP) is solved using top-down goal directed search without grounding. This allows s(CASP) to solve problems that cannot be grounded, while the generated proof tree is a good basis for giving a justification for the answer. s(CASP) supports both negation as failure (NAF) and classical negation. These features make s(CASP) particularly suitable for commonsense reasoning tasks that require a justification of the answer. Currently, scasp is an executable generated using Ciao. We ported s(CASP) to SWI-Prolog. The primary aim of this is to provide s(CASP) as a library that allows for embedding and managing multiple s(CASP) programs from multiple threads. Here, 'Managing' means being able to construct and modify an s(CASP) program dynamically and dynamically run queries against s(CASP) programs.</p>
      </abstract>
      <kwd-group>
        <kwd>eol&gt;s(CASP)</kwd>
        <kwd>answer set programming</kwd>
        <kwd>multi-paradigm</kwd>
        <kwd>Prolog</kwd>
      </kwd-group>
    </article-meta>
  </front>
  <body>
    <sec id="sec-1">
      <title>1. Introduction</title>
      <p>in legal or medical domains. s(CASP) is claimed to be suitable for Commonsense Reasoning.2</p>
      <p>As the family of ASP languages is purely declarative without any side efects the solvers
require some other language that prepares the ASP program, runs the solver and takes action
depending on the output of the solver. Prolog is an ideal language for embedding ASP systems
because manipulating clauses, handling bindings to logical variables, reasoning about the
model and interpreting the justification all fit perfectly with Prolog. In our case, the s(CASP)
implementation is already in Prolog and produces possible bindings, models and justifications
on backtracking.</p>
      <p>This article explains our port of s(CASP) to SWI-Prolog and discusses several alternatives to
making s(CASP) accessible from Prolog. We report on unfinished work. This article is primarily
input for discussion.</p>
    </sec>
    <sec id="sec-2">
      <title>2. Porting s(CASP) to SWI-Prolog</title>
      <p>The GIT repository https://gitlab.software.imdea.org/ciao-lang/sCASP provides the source for
s(CASP) for Ciao. The source reuses the compiler of s(ASP) by Kyle Marple [4]. Operation
between variables in the s(ASP) implementation are handled ‘manually’. The s(ASP) execution
maintains a mapping between variables names and their current bindings (values as well as
disequality constraints). The s(CASP) execution lets Prolog take care of all operations that it
can handle natively, instead of interpreting them. Therefore, a large part of the environment
for the s(CASP) program is carried implicitly in the Prolog environment. Since s(CASP) and
Prolog shared many characteristics (e.g., the behavior of variables), this results in flexibility of
implementation and gives a large performance improvement. [3]</p>
      <p>
        We ported s(CASP) to SWI-Prolog. The aim of this project is twofold: (
        <xref ref-type="bibr" rid="ref1">1</xref>
        ) create a uniform
codebase, avoiding duplicate work and dead code that resulted from building s(CASP) (using
Ciao style) on top of s(ASP) (using SWI-Prolog style) and (
        <xref ref-type="bibr" rid="ref2">2</xref>
        ) provide s(CASP) as an embedded
language in Prolog in addition to the stand-alone executable. With (
        <xref ref-type="bibr" rid="ref1">1</xref>
        ) we aim at a modular
and industrial strength implementation of s(CASP). With (
        <xref ref-type="bibr" rid="ref2">2</xref>
        ) we recognise that an ASP system
can only function as a reasoning component in an application written in some other language
that takes care of assembling the ASP model, running queries and making use of the bindings,
model and justification. We argued that Prolog is an ideal language for this purpose in the
introduction.
      </p>
      <p>We realised two changes in the design of s(CASP) to better facilitate embedding in Prolog.
First of all, we replaced the DCG (Prolog grammar rules) based parser by the Prolog parser.
This is possible by defining an adequate set of Prolog operators. 3 The terms read are validated
to satisfy the restrictions imposed by s(CASP) and converted into the same format that was
emitted by the original parser. Second, where the original implementation generated the
justification directly from the resulting s(CASP) solver’s stack, the current implementation
creates a Prolog representation from the stack that naturally represents the justification while
hiding the internals of the solver.</p>
      <p>2See https://personal.utdallas.edu/~gupta/csg/
3s(ASP) allows for predicate names that start with an underscore, as in _p(X). This can be handled in
SWIProlog, but not in standard ISO Prolog.
?- p(X).
sCASP model: [p(X),not q(X)],
sCASP justification
query
p(X)
not q(X)</p>
      <p>
        chs(p(X)) ∧
o_nmr_check ;
X = 1,
sCASP model: [p(
        <xref ref-type="bibr" rid="ref1">1</xref>
        )],
sCASP justification
query
p(
        <xref ref-type="bibr" rid="ref1">1</xref>
        ) ∧
o_nmr_check ;
false.
      </p>
    </sec>
    <sec id="sec-3">
      <title>3. Accessing s(CASP) from Prolog</title>
      <p>Currently, s(CASP) programs can be embedded into Prolog using two directives that create
a block. The opening directive changes the Prolog operator tables and activates rules for
term_expansion/2 that validate the s(CASP) terms and does some simple transformations
that avoid conflicts with Prolog. The closing directive reverts the changes to the operator table,
compiles the loaded terms into the internal s(CASP) representation and creates wrappers that
make the exported s(CASP) predicates accessible as normal Prolog predicates from the enclosing
Prolog module.
:- begin_scasp(Unit, Exports).
&lt;sCASP program&gt;
:- end_scasp.</p>
      <p>
        We give a minimal example below.
:- use_module(library(scasp/embed)).
:- begin_scasp(qp, [p/1]).
p(X) :- not q(X).
p(
        <xref ref-type="bibr" rid="ref1">1</xref>
        ).
q(X) :- not p(X).
:- end_scasp.
      </p>
      <p>After loading this Prolog program, p/1 is made available as a normal Prolog predicate as
shown in figure 1. Calling this predicate makes the bindings available as normal Prolog bindings
while alternative solutions are made available through Prolog backtracking. The model and
justification are stored in backtrackable global variables as provided by b_setval/2 in
SWIProlog. By using backtrackable global variables we can provide access to these extensions to
the normal Prolog answer (only the bindings) while full sharing of variables and constraints are
retained.</p>
      <p>The model is represented as a list of Prolog terms, where each term may be nested as
not(Term) (not provable), -(Term) (false) or not(-(Term)) (not provable that Term is
false). The justification is a tree represented as a term -(Term, ListOfChildren). Here
ListOfChildren is the empty list for facts and a list of -/2 terms otherwise. In addition to the
negation symbols, terms may be nested in one of the terms below.</p>
      <p>• proved(Term) Term was already proved.
• chs(Term) Term is assumed because it appears in a loop with an even number of
negations
• assume(Term) Marks the loop detected by chs(Term)</p>
      <p>The model is made available by scasp_model/1 and the justification tree using
scasp_justification/2 , where the second argument is an option list that may be used to adjust
the level of detail in the tree.</p>
      <p>The SWI-Prolog toplevel displays answers to a query as a valid Prolog body term. For
traditional Prolog this is a conjunction of unification (=/2) calls that establish the binding or
true if no variables are bound. If some of the variables are subject to constraints the constraints
are reified using copy_term/3 and added as goals to the answer, for example, using clp(fd) we
may get:
?- A #&gt; 2, A #&lt; 5.</p>
      <p>A in 3..4.</p>
      <p>To accommodate CHR, which has a notion of a global constraint store, SWI-Prolog provides
a hook that can be used to add additional goals to the answer. This hook is used to make the
model and justification visible in the toplevel. This is not entirely satisfactory because the
printed output is no longer valid Prolog input that has the same efect as the original query.</p>
    </sec>
    <sec id="sec-4">
      <title>4. Some notes on portability</title>
      <p>The current codebase is SWI-Prolog specific. While many Prolog implementations have some
way to embed code by temporarily changing the operator table and doing macro expansion,
there is no standard. The same applies for integrating the model and justification into the Prolog
REPL loop.</p>
      <p>Ideally the data representations such as the program representation going into the s(CASP)
compilation phase, the s(CASP) intermediate program representation, the model and justification
are synchronised between (Prolog) s(CASP) implementations. That allows for sharing the
program transformation and solver code as well as code using the output of the embedded
s(CASP) system.</p>
    </sec>
    <sec id="sec-5">
      <title>5. Discussion and future work</title>
      <p>
        When porting s(CASP) to SWI-Prolog we set ourselves the following goals: (
        <xref ref-type="bibr" rid="ref1">1</xref>
        ) cleanup of the
code base to improve maintainability and modularity and (
        <xref ref-type="bibr" rid="ref2">2</xref>
        ) allow using s(CASP) as embedded
language and library from Prolog. This is work in progress. The current implementation
can handle one or more s(CASP) programs embedded in one or more Prolog modules. The
implementation is thread-safe.
      </p>
      <p>The current implementation has no interface for composing s(CASP) programs dynamically.
This is a vital feature for Inductive Logic Programming (ILP) as well as for assembling a suitable
program based on currently known case data.</p>
      <p>Another approach to execute a goal using s(CASP) semantics would be to automatically
re-interpret a Prolog program under s(CASP) semantics. One way to do that would be to
declare predicates as s(CASP) predicates using e.g., :- scasp p/1, q/2.. Alternatively we
could define a predicate scasp(Goal). Both approaches would need to establish the reachable
program, verify it satisfies the limitations imposed by s(CASP), compile the program to the
s(CASP) intermediate representation and run the s(CASP) solver. This approach provides a
trivial API for manipulating the s(CASP) program using assert/1 and retract/1. It is not clear
how s(CASP) global constraints fit into this picture.</p>
      <p>We also plan to support embedding s(CASP) into SWISH, providing an online environment
for teaching and exchanging ideas.</p>
    </sec>
  </body>
  <back>
    <ref-list>
      <ref id="ref1">
        <mixed-citation>
          [1]
          <string-name>
            <given-names>V. W.</given-names>
            <surname>Marek</surname>
          </string-name>
          ,
          <string-name>
            <given-names>M.</given-names>
            <surname>Truszczynski</surname>
          </string-name>
          ,
          <article-title>Stable models and an alternative logic programming paradigm, in:</article-title>
          <string-name>
            <surname>K. R. Apt</surname>
            ,
            <given-names>V. W.</given-names>
          </string-name>
          <string-name>
            <surname>Marek</surname>
            ,
            <given-names>M.</given-names>
          </string-name>
          <string-name>
            <surname>Truszczynski</surname>
          </string-name>
          , D. S. Warren (Eds.),
          <source>The Logic Programming Paradigm - A 25-Year Perspective, Artificial Intelligence</source>
          , Springer,
          <year>1999</year>
          , pp.
          <fpage>375</fpage>
          -
          <lpage>398</lpage>
          . URL: https://doi.org/10.1007/978-3-
          <fpage>642</fpage>
          -60085-2_
          <fpage>17</fpage>
          . doi:
          <volume>10</volume>
          .1007/ 978-3-
          <fpage>642</fpage>
          -60085-2\_
          <fpage>17</fpage>
          .
        </mixed-citation>
      </ref>
      <ref id="ref2">
        <mixed-citation>
          [2]
          <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>
          , in: F.
          <string-name>
            <surname>Ricca</surname>
            ,
            <given-names>A.</given-names>
          </string-name>
          <string-name>
            <surname>Russo</surname>
            ,
            <given-names>S.</given-names>
          </string-name>
          <string-name>
            <surname>Greco</surname>
            ,
            <given-names>N.</given-names>
          </string-name>
          <string-name>
            <surname>Leone</surname>
            ,
            <given-names>A.</given-names>
          </string-name>
          <string-name>
            <surname>Artikis</surname>
            , G. Friedrich,
            <given-names>P.</given-names>
          </string-name>
          <string-name>
            <surname>Fodor</surname>
            ,
            <given-names>A.</given-names>
          </string-name>
          <string-name>
            <surname>Kimmig</surname>
            ,
            <given-names>F. A.</given-names>
          </string-name>
          <string-name>
            <surname>Lisi</surname>
            ,
            <given-names>M.</given-names>
          </string-name>
          <string-name>
            <surname>Maratea</surname>
            ,
            <given-names>A.</given-names>
          </string-name>
          <string-name>
            <surname>Mileo</surname>
            ,
            <given-names>F.</given-names>
          </string-name>
          <string-name>
            <surname>Riguzzi</surname>
          </string-name>
          (Eds.),
          <source>Proceedings 36th International Conference on Logic Programming (Technical Communications)</source>
          ,
          <source>ICLP Technical Communications</source>
          <year>2020</year>
          ,
          <article-title>(Technical Communications) UNICAL, Rende (CS), Italy</article-title>
          ,
          <fpage>18</fpage>
          -24th
          <source>September</source>
          <year>2020</year>
          , volume
          <volume>325</volume>
          <source>of EPTCS</source>
          ,
          <year>2020</year>
          , pp.
          <fpage>59</fpage>
          -
          <lpage>72</lpage>
          . URL: https://doi.org/10.4204/EPTCS.325.12. doi:
          <volume>10</volume>
          .4204/EPTCS.325.12.
        </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>E.</given-names>
            <surname>Salazar</surname>
          </string-name>
          ,
          <string-name>
            <given-names>K.</given-names>
            <surname>Marple</surname>
          </string-name>
          , G. Gupta,
          <article-title>Constraint answer set programming without grounding</article-title>
          ,
          <source>Theory Pract. Log. Program</source>
          .
          <volume>18</volume>
          (
          <year>2018</year>
          )
          <fpage>337</fpage>
          -
          <lpage>354</lpage>
          . URL: https://doi.org/10. 1017/S1471068418000285. doi:
          <volume>10</volume>
          .1017/S1471068418000285.
        </mixed-citation>
      </ref>
      <ref id="ref4">
        <mixed-citation>
          [4]
          <string-name>
            <given-names>G.</given-names>
            <surname>Gupta</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>
            <given-names>Z.</given-names>
            <surname>Chen</surname>
          </string-name>
          ,
          <string-name>
            <given-names>F.</given-names>
            <surname>Shakerin</surname>
          </string-name>
          ,
          <article-title>A case for query-driven predicate answer set programming</article-title>
          , in: G. Reger, D. Traytel (Eds.),
          <source>ARCADE</source>
          <year>2017</year>
          ,
          <source>1st International Workshop on Automated Reasoning: Challenges</source>
          , Applications, Directions, Exemplary Achievements, Gothenburg, Sweden,
          <source>6th August</source>
          <year>2017</year>
          , volume
          <volume>51</volume>
          of EPiC Series in Computing, EasyChair,
          <year>2017</year>
          , pp.
          <fpage>64</fpage>
          -
          <lpage>68</lpage>
          . URL: https://doi.org/10.29007/ngm2. doi:
          <volume>10</volume>
          .29007/ngm2.
        </mixed-citation>
      </ref>
    </ref-list>
  </back>
</article>