<?xml version="1.0" encoding="UTF-8"?>
<TEI xml:space="preserve" xmlns="http://www.tei-c.org/ns/1.0" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xsi:schemaLocation="http://www.tei-c.org/ns/1.0 https://raw.githubusercontent.com/kermitt2/grobid/master/grobid-home/schemas/xsd/Grobid.xsd"
 xmlns:xlink="http://www.w3.org/1999/xlink">
	<teiHeader xml:lang="en">
		<fileDesc>
			<titleStmt>
				<title level="a" type="main">s(CASP) for SWI-Prolog</title>
			</titleStmt>
			<publicationStmt>
				<publisher/>
				<availability status="unknown"><licence/></availability>
			</publicationStmt>
			<sourceDesc>
				<biblStruct>
					<analytic>
						<author>
							<persName><forename type="first">Jan</forename><surname>Wielemaker</surname></persName>
							<affiliation key="aff0">
								<orgName type="institution">SWI-Prolog Solutions b.v</orgName>
								<address>
									<settlement>Amsterdam</settlement>
									<country key="NL">The Netherlands</country>
								</address>
							</affiliation>
							<affiliation key="aff1">
								<orgName type="institution">Vrije Universiteit Amsterdam</orgName>
								<address>
									<addrLine>De Boelelaan 1105</addrLine>
									<postCode>1081 HV</postCode>
									<settlement>Amsterdam</settlement>
									<country key="NL">The Netherlands</country>
								</address>
							</affiliation>
						</author>
						<author>
							<persName><forename type="first">Joaquín</forename><surname>Arias</surname></persName>
							<affiliation key="aff2">
								<orgName type="institution">Universidad Rey Juan Carlos</orgName>
								<address>
									<addrLine>Calle Tulipán s/n</addrLine>
									<postCode>28933</postCode>
									<settlement>Móstoles (Madrid)</settlement>
									<country key="ES">Spain</country>
								</address>
							</affiliation>
						</author>
						<author>
							<persName><forename type="first">Gopal</forename><surname>Gupta</surname></persName>
							<affiliation key="aff3">
								<orgName type="institution">The University of Texas at Dallas</orgName>
								<address>
									<settlement>Dallas</settlement>
									<country key="US">USA</country>
								</address>
							</affiliation>
						</author>
						<title level="a" type="main">s(CASP) for SWI-Prolog</title>
					</analytic>
					<monogr>
						<idno type="ISSN">1613-0073</idno>
					</monogr>
					<idno type="MD5">CF4ACC68060CB208DE7FC54FAF7E98E3</idno>
				</biblStruct>
			</sourceDesc>
		</fileDesc>
		<encodingDesc>
			<appInfo>
				<application version="0.7.2" ident="GROBID" when="2023-03-24T04:54+0000">
					<desc>GROBID - A machine learning software for extracting information from scholarly documents</desc>
					<ref target="https://github.com/kermitt2/grobid"/>
				</application>
			</appInfo>
		</encodingDesc>
		<profileDesc>
			<textClass>
				<keywords>
					<term>s(CASP)</term>
					<term>answer set programming</term>
					<term>multi-paradigm</term>
					<term>Prolog</term>
				</keywords>
			</textClass>
			<abstract>
<div xmlns="http://www.tei-c.org/ns/1.0"><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.</p><p>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></div>
			</abstract>
		</profileDesc>
	</teiHeader>
	<text xml:lang="en">
		<body>
<div xmlns="http://www.tei-c.org/ns/1.0"><head n="1.">Introduction</head><p>Answer Set Programming (ASP) <ref type="bibr" target="#b0">[1]</ref> is nowadays an established paradigm in the logic programming community. Traditionally, ASP notably targets difficult search problems. ASP solvers normally ground the input program and then use a SAT solver to find stable models. 1 Disadvantages of this technique is that not all programs have a finite grounding and that it is hard to generate a human understandable justification for an answer <ref type="bibr" target="#b1">[2]</ref>. In contrast, s(CASP) <ref type="bibr" target="#b2">[3]</ref> is goal directed (like Prolog) and does not ground the program. As a result it can integrate Constraint Logic Programming (CLP) that allows, for example, to express 𝑋 &gt; 3 without any further knowledge about 𝑋. In addition, the solver produces a stack that represents how each atom or its negation has been derived. This stack is input to the justification generation. s(CASP) is implemented as a meta interpreter in Prolog.</p><p>While s(CASP) is, in its current implementation, not particularly suitable for hard problems, it is suitable for automation of reasoning tasks normally carried out by humans such as reasoning</p></div>
<div xmlns="http://www.tei-c.org/ns/1.0"><head>GDE'21: ICLP'21 (Virtual) Workshop on Goal-directed Execution of Answer Set Programs, September 20, 2021</head><p>jan@swi-prolog.com (J. Wielemaker); joaquin.arias@urjc.es (J. Arias); gupta@utdallas.edu (G. Gupta) 0000-0001-5574-5673 (J. Wielemaker); 0000-0003-4148-311X (J. Arias); 0000-0001-9727-0362 (G. <ref type="bibr">Gupta)</ref> in legal or medical domains. s(CASP) is claimed to be suitable for Commonsense Reasoning. <ref type="foot" target="#foot_0">2</ref>As the family of ASP languages is purely declarative without any side effects 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></div>
<div xmlns="http://www.tei-c.org/ns/1.0"><head n="2.">Porting s(CASP) to SWI-Prolog</head><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 <ref type="bibr" target="#b3">[4]</ref>. 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. <ref type="bibr" target="#b2">[3]</ref> We ported s(CASP) to SWI-Prolog. The aim of this project is twofold: (1) 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 (2) provide s(CASP) as an embedded language in Prolog in addition to the stand-alone executable. With (1) we aim at a modular and industrial strength implementation of s(CASP). With (2) 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. <ref type="foot" target="#foot_1">3</ref> 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>?-p(X). sCASP model: [p(X),not q(X)], sCASP justification query p(X) not q(X) chs(p(X)) ∧  </p></div>
<div xmlns="http://www.tei-c.org/ns/1.0"><head n="3.">Accessing s(CASP) from Prolog</head><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.</p><p>:-begin_scasp(Unit, Exports). &lt;sCASP program&gt; :-end_scasp.</p></div>
<div xmlns="http://www.tei-c.org/ns/1.0"><head>We give a minimal example below.</head><p>:-use_module(library(scasp/embed)). :-begin_scasp(qp, [p/1]). p(X) :-not q(X). p(1). 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 <ref type="figure" target="#fig_1">1</ref>. 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 SWI-Prolog. 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.</p><p>• 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:</p><formula xml:id="formula_0">?-A #&gt; 2, A #&lt; 5. A in 3..4.</formula><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 effect as the original query.</p></div>
<div xmlns="http://www.tei-c.org/ns/1.0"><head n="4.">Some notes on portability</head><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></div>
<div xmlns="http://www.tei-c.org/ns/1.0"><head n="5.">Discussion and future work</head><p>When porting s(CASP) to SWI-Prolog we set ourselves the following goals: (1) cleanup of the code base to improve maintainability and modularity and (2) 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></div><figure xmlns="http://www.tei-c.org/ns/1.0" xml:id="fig_1"><head>Figure 1 :</head><label>1</label><figDesc>Figure 1: Running an s(CASP) query as a normal Prolog query</figDesc></figure>
			<note xmlns="http://www.tei-c.org/ns/1.0" place="foot" n="2" xml:id="foot_0">See https://personal.utdallas.edu/~gupta/csg/</note>
			<note xmlns="http://www.tei-c.org/ns/1.0" place="foot" n="3" xml:id="foot_1">s(ASP) allows for predicate names that start with an underscore, as in _p(X). This can be handled in SWI-Prolog, but not in standard ISO Prolog.</note>
		</body>
		<back>
			<div type="references">

				<listBibl>

<biblStruct xml:id="b0">
	<analytic>
		<title level="a" type="main">Stable models and an alternative logic programming paradigm</title>
		<author>
			<persName><forename type="first">V</forename><forename type="middle">W</forename><surname>Marek</surname></persName>
		</author>
		<author>
			<persName><forename type="first">M</forename><surname>Truszczynski</surname></persName>
		</author>
		<idno type="DOI">10.1007/978-3-642-60085-2_17</idno>
		<idno>doi:</idno>
		<ptr target="10.1007/978-3-642-60085-2\_17" />
	</analytic>
	<monogr>
		<title level="m">The Logic Programming Paradigm -A 25-Year Perspective, Artificial Intelligence</title>
				<editor>
			<persName><forename type="first">K</forename><forename type="middle">R</forename><surname>Apt</surname></persName>
		</editor>
		<editor>
			<persName><forename type="first">V</forename><forename type="middle">W</forename><surname>Marek</surname></persName>
		</editor>
		<editor>
			<persName><forename type="first">M</forename><surname>Truszczynski</surname></persName>
		</editor>
		<editor>
			<persName><forename type="first">D</forename><forename type="middle">S</forename><surname>Warren</surname></persName>
		</editor>
		<imprint>
			<publisher>Springer</publisher>
			<date type="published" when="1999">1999</date>
			<biblScope unit="page" from="375" to="398" />
		</imprint>
	</monogr>
</biblStruct>

<biblStruct xml:id="b1">
	<analytic>
		<title level="a" type="main">Justifications for goal-directed constraint answer set programming</title>
		<author>
			<persName><forename type="first">J</forename><surname>Arias</surname></persName>
		</author>
		<author>
			<persName><forename type="first">M</forename><surname>Carro</surname></persName>
		</author>
		<author>
			<persName><forename type="first">Z</forename><surname>Chen</surname></persName>
		</author>
		<author>
			<persName><forename type="first">G</forename><surname>Gupta</surname></persName>
		</author>
		<idno type="DOI">10.4204/EPTCS.325.12</idno>
		<ptr target="https://doi.org/10.4204/EPTCS.325.12.doi:10.4204/EPTCS.325.12" />
	</analytic>
	<monogr>
		<title level="m">Proceedings 36th International Conference on Logic Programming (Technical Communications), ICLP Technical Communications 2020</title>
				<editor>
			<persName><forename type="first">F</forename><surname>Ricca</surname></persName>
		</editor>
		<editor>
			<persName><forename type="first">A</forename><surname>Russo</surname></persName>
		</editor>
		<editor>
			<persName><forename type="first">S</forename><surname>Greco</surname></persName>
		</editor>
		<editor>
			<persName><forename type="first">N</forename><surname>Leone</surname></persName>
		</editor>
		<editor>
			<persName><forename type="first">A</forename><surname>Artikis</surname></persName>
		</editor>
		<editor>
			<persName><forename type="first">G</forename><surname>Friedrich</surname></persName>
		</editor>
		<editor>
			<persName><forename type="first">P</forename><surname>Fodor</surname></persName>
		</editor>
		<editor>
			<persName><forename type="first">A</forename><surname>Kimmig</surname></persName>
		</editor>
		<editor>
			<persName><forename type="first">F</forename><forename type="middle">A</forename><surname>Lisi</surname></persName>
		</editor>
		<editor>
			<persName><forename type="first">M</forename><surname>Maratea</surname></persName>
		</editor>
		<editor>
			<persName><forename type="first">A</forename><surname>Mileo</surname></persName>
		</editor>
		<editor>
			<persName><forename type="first">F</forename><surname>Riguzzi</surname></persName>
		</editor>
		<meeting>36th International Conference on Logic Programming (Technical Communications), ICLP Technical Communications 2020<address><addrLine>Rende (CS), Italy</addrLine></address></meeting>
		<imprint>
			<publisher>EPTCS</publisher>
			<date type="published" when="2020-09-24">18-24th September 2020. 2020</date>
			<biblScope unit="volume">325</biblScope>
			<biblScope unit="page" from="59" to="72" />
		</imprint>
	</monogr>
	<note>Technical Communications) UNICAL</note>
</biblStruct>

<biblStruct xml:id="b2">
	<analytic>
		<title level="a" type="main">Constraint answer set programming without grounding</title>
		<author>
			<persName><forename type="first">J</forename><surname>Arias</surname></persName>
		</author>
		<author>
			<persName><forename type="first">M</forename><surname>Carro</surname></persName>
		</author>
		<author>
			<persName><forename type="first">E</forename><surname>Salazar</surname></persName>
		</author>
		<author>
			<persName><forename type="first">K</forename><surname>Marple</surname></persName>
		</author>
		<author>
			<persName><forename type="first">G</forename><surname>Gupta</surname></persName>
		</author>
		<idno type="DOI">10.1017/S1471068418000285</idno>
		<ptr target="https://doi.org/10.1017/S1471068418000285.doi:10.1017/S1471068418000285" />
	</analytic>
	<monogr>
		<title level="j">Theory Pract. Log. Program</title>
		<imprint>
			<biblScope unit="volume">18</biblScope>
			<biblScope unit="page" from="337" to="354" />
			<date type="published" when="2018">2018</date>
		</imprint>
	</monogr>
</biblStruct>

<biblStruct xml:id="b3">
	<analytic>
		<title level="a" type="main">A case for query-driven predicate answer set programming</title>
		<author>
			<persName><forename type="first">G</forename><surname>Gupta</surname></persName>
		</author>
		<author>
			<persName><forename type="first">E</forename><surname>Salazar</surname></persName>
		</author>
		<author>
			<persName><forename type="first">K</forename><surname>Marple</surname></persName>
		</author>
		<author>
			<persName><forename type="first">Z</forename><surname>Chen</surname></persName>
		</author>
		<author>
			<persName><forename type="first">F</forename><surname>Shakerin</surname></persName>
		</author>
		<idno type="DOI">10.29007/ngm2</idno>
		<ptr target="https://doi.org/10.29007/ngm2.doi:10.29007/ngm2" />
	</analytic>
	<monogr>
		<title level="m">ARCADE 2017, 1st International Workshop on Automated Reasoning: Challenges, Applications, Directions, Exemplary Achievements</title>
		<title level="s">EPiC Series in Computing</title>
		<editor>
			<persName><forename type="first">G</forename><surname>Reger</surname></persName>
		</editor>
		<editor>
			<persName><forename type="first">D</forename><surname>Traytel</surname></persName>
		</editor>
		<meeting><address><addrLine>Gothenburg, Sweden</addrLine></address></meeting>
		<imprint>
			<publisher>EasyChair</publisher>
			<date type="published" when="2017-08-06">6th August 2017. 2017</date>
			<biblScope unit="volume">51</biblScope>
			<biblScope unit="page" from="64" to="68" />
		</imprint>
	</monogr>
</biblStruct>

				</listBibl>
			</div>
		</back>
	</text>
</TEI>
