<!DOCTYPE article PUBLIC "-//NLM//DTD JATS (Z39.96) Journal Archiving and Interchange DTD v1.0 20120330//EN" "JATS-archivearticle1.dtd">
<article xmlns:xlink="http://www.w3.org/1999/xlink">
  <front>
    <journal-meta />
    <article-meta>
      <title-group>
        <article-title>A compiler for strati ed Datalog programs: preliminary results?</article-title>
      </title-group>
      <contrib-group>
        <contrib contrib-type="author">
          <string-name>Bernardo Cuteri</string-name>
          <xref ref-type="aff" rid="aff0">0</xref>
        </contrib>
        <contrib contrib-type="author">
          <string-name>Francesco Ricca</string-name>
          <email>riccag@mat.unical.it</email>
          <xref ref-type="aff" rid="aff0">0</xref>
        </contrib>
        <aff id="aff0">
          <label>0</label>
          <institution>DeMaCS, Universita della Calabria</institution>
          ,
          <addr-line>Rende (CS)</addr-line>
          ,
          <country country="IT">Italy</country>
        </aff>
      </contrib-group>
      <abstract>
        <p>Deductive databases originated from the con uence between logic programming and databases. The core language of deductive databases is Datalog, which has recently found a renewed interest and new applications in several real-world problems. The evaluation of Datalog is traditionally implemented in monolithic systems that are generalpurpose in the sense that they are able to process an entire class of programs. In this paper, we follow a di erent approach; we present a tool that is able to compile a given (non-ground) Datalog program, possibly with strati ed negation, into a problem-speci c executable implementation. Preliminary results show the performance bene ts that can be obtained by a compilation-based approach.</p>
      </abstract>
      <kwd-group>
        <kwd>Compilation</kwd>
        <kwd>Strati ed programs</kwd>
        <kwd>Deductive databases</kwd>
      </kwd-group>
    </article-meta>
  </front>
  <body>
    <sec id="sec-1">
      <title>Introduction</title>
      <p>
        Deductive databases allow to manipulate data declaratively by means of logic
programming. The target language of this work is Datalog [
        <xref ref-type="bibr" rid="ref12 ref4">4, 12</xref>
        ]: a simple,
yet exible logic programming language which is at the core of most deductive
database systems. Datalog is su ciently expressive to model many practical
problems, and it recently found new applications in a variety of emerging
domains such as data integration, information extraction, networking, program
analysis, security, and cloud computing [
        <xref ref-type="bibr" rid="ref6">6</xref>
        ]. The evaluation of Datalog programs
is traditionally performed by systems that are general-purpose in the sense that
they are able to process an entire class of programs. In this paper, we follow a
different approach; we consider a compilation strategy for the evaluation of Datalog
programs. Moreover, we consider an extension of Datalog that includes strati ed
negation and allows the representation of a striclty larger class of problems. This
language is also called strati ed Datalog.
      </p>
      <p>
        A Datalog program with strati ed negation [
        <xref ref-type="bibr" rid="ref4">4</xref>
        ] P is a set of rules of the
form Head : Body, where Body is a conjunction of possibly negated literals,
and Head is an atom. A literal is either a positive or a negative atom, while
an atom is either a propositional variable or a n-ary predicate p with a list of
terms t1; :::; tn. A term is either a constant or a variable. If a variable appears
in the head of a rule or in a negative literal, it must then appear also in some
positive literal of the same rule (safety), and recursion trough negation is not
allowed (strati cation). Programs are virtually split into two distinct parts: the
intensional part (i.e. the set of possibly non-ground rules) that describes the
problem, and the extensional part (i.e. the set of ground facts) that represent
an instance of the problem. As an example, the following program models the
well-known Reachability problem:
reaches(X,Y) :- edge(X, Y).
      </p>
      <p>reaches(X,Y) :- edge(X,Z), reaches(Z,Y).</p>
      <p>Facts of the form edge(i,j) for each arc (i,j) model the input graph. It is
custom in the logic programming community to refer to the instensional part as
the encoding, and to the extensional part as the instance. In applications, it is
common to use the same uniform encoding several times with di erent instances.</p>
      <p>A general-purpose system (often needlessly) processes the same encoding
every time a new instance is processed. By compiling the encoding in a specialized
procedure, one can wire it inside the evaluation procedure so that one does not
have to process it every time. Moreover, specialized evaluation strategies can be
adopted on a per-rule basis possibly increasing evaluation performance.</p>
      <p>
        Datalog represents the kernel sub-language of Answer Set Programming
(ASP) [
        <xref ref-type="bibr" rid="ref2">2</xref>
        ]. Notably, the grounder modules of ASP systems are based on
algorithms for evaluating strati ed Datalog programs [
        <xref ref-type="bibr" rid="ref7">7</xref>
        ], and, basically, any
monolithic ASP system is also an e cient engine for this class of programs.
      </p>
      <p>
        Actually, the idea of compiling Datalog programs is not new [
        <xref ref-type="bibr" rid="ref1 ref4 ref8">1, 4, 8</xref>
        ]; however,
a new complexity-wise optimal compilation-based approach has been recently
proposed in [
        <xref ref-type="bibr" rid="ref11">11</xref>
        ]. There, Liu and Stoller describe a method for transforming
Datalog programs (with rules having at most two literals in the body) into
e cient specialized implementations.
      </p>
      <p>
        We extend the approach in [
        <xref ref-type="bibr" rid="ref11">11</xref>
        ] in order to handle rules with bodies of any
size, strati ed negation and inequalities. Moreover, we implemented a concrete
system entirely developed in C++. In our prototype implementation, both the
compiler and its output (the compiled logic programs) are written in C++.
      </p>
      <p>To assess the k of our tool, we performed an experimental analysis where we
compare our implementation against existing general-purpose systems capable
of handling strati ed logic programs.
2</p>
    </sec>
    <sec id="sec-2">
      <title>Compilation of strati ed logic programs</title>
      <p>
        In this section, we rst overview the evaluation strategy adopted by our system,
and then we present the compilation strategy by means of an example.
Evaluation strategy. Strati ed logic programs are evaluated in our approach
by following the classical bottom-up schema [
        <xref ref-type="bibr" rid="ref12">12</xref>
        ]. Basically, rules are applied
Algorithm 1 Reachability compiled program
1: . . . fData structures initializations and facts readingg
2: while W edge 6= do fEvaluation of rule (1)g
3: edge = W edge:pop()
4: Redge:insert(edge)
5: EdgeZM ap:insertKeyV alue(fedge[
        <xref ref-type="bibr" rid="ref1">1</xref>
        ]g; fedge[0]g)
6: W reaches:insert(fedge[0]; edge[
        <xref ref-type="bibr" rid="ref1">1</xref>
        ]g)
7: end while
8: while W reaches 6= do fEvaluation of rule (2)g
9: reaches = W reaches:pop()
10: Rreaches:insert(reaches)
11: for X : edgeZM ap:at(reaches[0]) do
12: W reaches:insert(fX; reaches[
        <xref ref-type="bibr" rid="ref1">1</xref>
        ]g)
13: end for
14: end while
15: while W vertex 6= do fEvaluation of rule (3)g
16: vertex = W vertex:pop()
17: Rvertex:insert(vertex)
18: if not Rreaches:contains(f2; vertex[0]g) then
19: Rnoreaches:insert(fvertex[0]g)
20: end if
21: end while
22: M odel = Redge [ Rreaches [ Rvertex [ Rnoreaches
starting from the known facts to deduce new facts until no new information
can be derived. The notion of dependency graph of the input program is used
both to improve e ciency of the evaluation and to determine a correct order of
evaluation of rules in presence of negation. More in details, given program P the
dependency graph DG =&lt; V; E &gt; of P has a vertex p 2 V for each intensional
predicate p, and a (direct) edge of the form (b; h) 2 E whenever b occurs in
the body and h in the head of a rule of P . Edges are labeled as negative if the
body literal is negated. The dependency graph is, then, partitioned into strongly
connected components (SCC)s. A SCC is a maximal subset of the vertices, such
that every vertex is reachable from every other vertex. We say that a rule r 2 P
de nes a predicate p if p appears in the head of r. For each strongly connected
component (SCC) of DG, the set of rules de ning all the predicates in C is called
the module of C. The dependency graph yields a topological order [C1]; :::; [Cn]
over the SCCs: for each pair (Ci; Cj ) with i &lt; j, there is no path in the
dependency graph from Ci to Cj . Program modules corresponding to components
are evaluated by following a topological order. If two predicates do not belong
to the same SCC it means that they do not depend on each other, thus their
de ning rules can be evaluated separately (possibly increasing evaluation
performance). Since, by de nition, strati ed programs admit no negative edge inside
any SCC (i.e. no loop can contain a negative edge), an evaluation performed
according to a topological order ensures a sound computation of the semantics
of programs with negation. Since the rules of a program module (possibly) need
to be processed several times, the evaluation of modules is optimized employing
the semi-nave evaluation technique [
        <xref ref-type="bibr" rid="ref12">12</xref>
        ]. Basically, at each iteration n, only the
signi cant information derived during iteration n 1 is used.
      </p>
      <p>Compilation by example. The strategy described above is quite standard,
and it is employed also by general-purpose implementations. In our approach the
same strategy is used to produce a specialized implementation. In the following,
we exemplify how the compilation of a program module is done in our system.
Consider once more the program P GR that models the Reachability problem
with an extra rule to derive all vertices that do not reach vertex 2:
(1) reaches(X,Y) :- edge(X, Y).
(2) reaches(X,Y) :- edge(X,Z), reaches(Z,Y).</p>
      <p>(3) noReach(Y) :- vertex(Y), not reaches(2,Y).</p>
      <p>
        The dependency graph analysis basically ensures that the rule (3) is evaluated
after rule (1) and (2). Then , the core part of the compilation process applied to
P GR, in pseudo-code is presented in Algorithm 1. There, W edge, W reaches and
W vertex denote working sets while Redge, Rreaches, Rvertex and Rnoreaches
denote result sets (cfr.[
        <xref ref-type="bibr" rid="ref11">11</xref>
        ]). Working sets and result sets are implemented in
e cient indexed data structures that provide associative access and e cient
insertions and deletions of ground atoms. In the example, we rst loop on the
working set of predicate edge which was previously loaded with facts. In every
iteration, we retrieve and remove an element from the working set (pop) and
we insert it in the result set. At line 5 we insert an edge atom into an auxiliary
map (note that, attributes in atom variables, such as edge, are accessed in the
example by position, where the rst attribute has index 0). Auxiliary maps are
used to index predicate ground atoms that appear in the body of some rule and
might be involved in join operations with other predicates of the same body. In
our example, rule (2) induces an indexing of predicate edge on attribute Z. At
line 6, we derive an instance of reaches because of rule (1) and we insert it in
its working set. After the edge loop completes, we can loop on W reaches and
once again we retrieve and remove an atom from the working set and we insert
it into the result set. At this point (line 11) we loop over joining edge atoms
retrieved from the auxiliary map EdgeZMap. Inside the loop, we generate the
head ground atom and we insert it in Wreaches. In the last loop (line 15), we
iterate over vertex ground atoms and we add into Rnoreaches all vertices that
do not reach vertex 2 because of rule (3). Note that we do not need an extra
auxiliary map for reaches because we can directly use Rreaches instead. Finally,
the model of the program is given by the union of all result sets.
      </p>
      <p>We remark that, there are tasks involved in the evaluation of programs that
are carried out by the compiler and do not appear in the execution code. For
instance, the computation of SCCs and the individuation of which data
structures are needed in the evaluation. Thus, the product of the compilation does
not have to perform such tasks because they are done only once and for all in the
compilation process, while a general-purpose solver has to deal with such tasks
once for every instance. At low level, another advantage, comes from the
possiProblem solutions</p>
      <p>Model 1</p>
      <p>...</p>
      <p>Model n</p>
      <p>Encoding
(problem description)</p>
      <p>Compiler
Compiled program</p>
      <p>Problem instances</p>
      <p>Facts 1</p>
      <p>...</p>
      <p>Facts n
bility to declare data structures as variables and exploit direct access (reducing
pointers dereferencing), which results in faster C++ code.</p>
      <p>Datalog Compiler. The compiler is written in C++ and the output is rst
written in C++ and then compiled into an executable program (using standard
C++ compilers). Figure 1 shows a high-level architecture of Datalog compilation
and evaluation as designed in our system. The resulting executable program can
be run on any instance of the compiled logic program.
3</p>
    </sec>
    <sec id="sec-3">
      <title>Experimental analysis</title>
      <p>
        To assess the potential of our tool, we performed an experimental analysis where
we compared our system against existing general-purpose systems that can
evaluate strati ed logic programs by using bottom-up strategies, namely: Clingo [
        <xref ref-type="bibr" rid="ref5">5</xref>
        ],
DLV [
        <xref ref-type="bibr" rid="ref9">9</xref>
        ] and I-DLV [
        <xref ref-type="bibr" rid="ref3">3</xref>
        ]. Clingo and DLV are two well-known ASP solvers, while
I-DLV is a recently-introduced grounder. Even though the target language of
      </p>
      <p>Compiled</p>
      <p>Clingo</p>
      <p>DLV
I-DLV
5
10
15
20
25
30
35
40
45</p>
      <p>50</p>
      <p>
        Solved Instances
such systems is ASP they can also be considered as rather e cient
implementations for strati ed logic programs. The experimental analysis has been carried
out on benchmarks from OpenRuleBench [
        <xref ref-type="bibr" rid="ref10">10</xref>
        ], which is a well-known suite for
rule engines. The execution times reported for our system include compilation
times to provide a fair comparison with general-purpose tools.
      </p>
      <p>Results are summarized in Table 1 and in Figure 2. By looking at the table,
we observe that our tool solves more instances than any alternative on the
overall, and is the fastest on average in Large Joins and Recursion sets. In Strati ed
negation, our tool is on par with the others in terms of solved instances, but is
slower on average than I-DLV . This might be explained by the fact that there
are still some optimizations, such as join reordering, that general-purpose
systems adopt, but we did not consider yet in our prototype implementation. An
aggregate view on the results is reported in Figure 2 showing that our tool
performs well on the overall. For completeness, we report that compilation required
2.6s on average (over all benchmarks). This performance is acceptable given that
compilation is intended as a one-time process in our approach.
4</p>
    </sec>
    <sec id="sec-4">
      <title>Conclusions and future works</title>
      <p>
        In this paper, we presented a new compiler for strati ed Datalog programs.1 The
prototype takes as input a strati ed Datalog program and generates a specialized
implementation for a given program. Experimental results are very encouraging.
Ongoing work concerns the improvement of data structures generated by our
1 The tool can be downloaded from http://goo.gl/XhZXWh.
compiler and the inclusion of other known optimization techniques used by ASP
grounders [
        <xref ref-type="bibr" rid="ref7">7</xref>
        ]. Since, notably, the grounder modules of ASP systems are based
on algorithms for evaluating strati ed Datalog programs and we are obtaining
promising results for Datalog, we then expect to be able to obtain similar
improvements by applying compilation to the grounding of ASP programs. As for
future works, we aim at applying compilation-based techniques to the
instantiation of ASP programs.
      </p>
    </sec>
  </body>
  <back>
    <ref-list>
      <ref id="ref1">
        <mixed-citation>
          1.
          <string-name>
            <surname>Arni</surname>
            ,
            <given-names>F.</given-names>
          </string-name>
          ,
          <string-name>
            <surname>Ong</surname>
            ,
            <given-names>K.</given-names>
          </string-name>
          ,
          <string-name>
            <surname>Tsur</surname>
            ,
            <given-names>S.</given-names>
          </string-name>
          ,
          <string-name>
            <surname>Wang</surname>
            ,
            <given-names>H.</given-names>
          </string-name>
          ,
          <string-name>
            <surname>Zaniolo</surname>
            ,
            <given-names>C.</given-names>
          </string-name>
          :
          <article-title>The deductive database system LDL++</article-title>
          .
          <source>TPLP 3</source>
          (
          <issue>1</issue>
          ),
          <volume>61</volume>
          {
          <fpage>94</fpage>
          (
          <year>2003</year>
          )
        </mixed-citation>
      </ref>
      <ref id="ref2">
        <mixed-citation>
          2.
          <string-name>
            <surname>Brewka</surname>
            ,
            <given-names>G.</given-names>
          </string-name>
          ,
          <string-name>
            <surname>Eiter</surname>
            ,
            <given-names>T.</given-names>
          </string-name>
          ,
          <string-name>
            <surname>Truszczynski</surname>
            ,
            <given-names>M.</given-names>
          </string-name>
          :
          <article-title>Answer set programming at a glance</article-title>
          .
          <source>Commun. ACM</source>
          <volume>54</volume>
          (
          <issue>12</issue>
          ),
          <volume>92</volume>
          {
          <fpage>103</fpage>
          (
          <year>2011</year>
          )
        </mixed-citation>
      </ref>
      <ref id="ref3">
        <mixed-citation>
          3.
          <string-name>
            <surname>Calimeri</surname>
            ,
            <given-names>F.</given-names>
          </string-name>
          ,
          <string-name>
            <surname>Fusca</surname>
            ,
            <given-names>D.</given-names>
          </string-name>
          ,
          <string-name>
            <surname>Perri</surname>
            ,
            <given-names>S.</given-names>
          </string-name>
          ,
          <string-name>
            <surname>Zangari</surname>
            ,
            <given-names>J.:</given-names>
          </string-name>
          <article-title>I -dlv: The new intelligent grounder of dlv</article-title>
          .
          <source>In: AI*IA. LNCS</source>
          , vol.
          <volume>10037</volume>
          , pp.
          <volume>192</volume>
          {
          <fpage>207</fpage>
          . Springer (
          <year>2016</year>
          )
        </mixed-citation>
      </ref>
      <ref id="ref4">
        <mixed-citation>
          4.
          <string-name>
            <surname>Ceri</surname>
            ,
            <given-names>S.</given-names>
          </string-name>
          ,
          <string-name>
            <surname>Gottlob</surname>
            ,
            <given-names>G.</given-names>
          </string-name>
          ,
          <string-name>
            <surname>Tanca</surname>
            ,
            <given-names>L.</given-names>
          </string-name>
          :
          <article-title>Logic Programming</article-title>
          and Databases. Springer (
          <year>1990</year>
          )
        </mixed-citation>
      </ref>
      <ref id="ref5">
        <mixed-citation>
          5.
          <string-name>
            <surname>Gebser</surname>
            ,
            <given-names>M.</given-names>
          </string-name>
          ,
          <string-name>
            <surname>Kaminski</surname>
            ,
            <given-names>R.</given-names>
          </string-name>
          , Kaufmann,
          <string-name>
            <given-names>B.</given-names>
            ,
            <surname>Ostrowski</surname>
          </string-name>
          ,
          <string-name>
            <given-names>M.</given-names>
            ,
            <surname>Schaub</surname>
          </string-name>
          ,
          <string-name>
            <given-names>T.</given-names>
            ,
            <surname>Wanko</surname>
          </string-name>
          ,
          <string-name>
            <surname>P.</surname>
          </string-name>
          :
          <article-title>Theory solving made easy with clingo 5</article-title>
          . In:
          <article-title>ICLP 2016 TCs</article-title>
          . pp.
          <volume>2</volume>
          :
          <issue>1</issue>
          {2:
          <issue>15</issue>
          (
          <year>2016</year>
          )
        </mixed-citation>
      </ref>
      <ref id="ref6">
        <mixed-citation>
          6.
          <string-name>
            <surname>Huang</surname>
            ,
            <given-names>S.S.</given-names>
          </string-name>
          ,
          <string-name>
            <surname>Green</surname>
            ,
            <given-names>T.J.</given-names>
          </string-name>
          ,
          <string-name>
            <surname>Loo</surname>
            ,
            <given-names>B.T.</given-names>
          </string-name>
          :
          <article-title>Datalog and emerging applications: an interactive tutorial</article-title>
          .
          <source>In: Proceedings of SIGMOD 2011</source>
          . pp.
          <volume>1213</volume>
          {
          <fpage>1216</fpage>
          .
          <string-name>
            <surname>ACM</surname>
          </string-name>
          (
          <year>2011</year>
          )
        </mixed-citation>
      </ref>
      <ref id="ref7">
        <mixed-citation>
          7.
          <string-name>
            <surname>Kaufmann</surname>
            ,
            <given-names>B.</given-names>
          </string-name>
          ,
          <string-name>
            <surname>Leone</surname>
            ,
            <given-names>N.</given-names>
          </string-name>
          ,
          <string-name>
            <surname>Perri</surname>
            ,
            <given-names>S.</given-names>
          </string-name>
          ,
          <string-name>
            <surname>Schaub</surname>
            ,
            <given-names>T.</given-names>
          </string-name>
          :
          <article-title>Grounding and solving in answer set programming</article-title>
          .
          <source>AI Magazine</source>
          <volume>37</volume>
          (
          <issue>3</issue>
          ),
          <volume>25</volume>
          {
          <fpage>32</fpage>
          (
          <year>2016</year>
          )
        </mixed-citation>
      </ref>
      <ref id="ref8">
        <mixed-citation>
          8.
          <string-name>
            <surname>Kifer</surname>
            ,
            <given-names>M.</given-names>
          </string-name>
          ,
          <string-name>
            <surname>Lozinskii</surname>
            ,
            <given-names>E.L.</given-names>
          </string-name>
          :
          <article-title>On compile-time query optimization in deductive databases by means of static ltering</article-title>
          .
          <source>ACM TDS 15(3)</source>
          ,
          <volume>385</volume>
          {
          <fpage>426</fpage>
          (
          <year>1990</year>
          )
        </mixed-citation>
      </ref>
      <ref id="ref9">
        <mixed-citation>
          9.
          <string-name>
            <surname>Leone</surname>
            ,
            <given-names>N.</given-names>
          </string-name>
          ,
          <string-name>
            <surname>Pfeifer</surname>
            ,
            <given-names>G.</given-names>
          </string-name>
          ,
          <string-name>
            <surname>Faber</surname>
            ,
            <given-names>W.</given-names>
          </string-name>
          ,
          <string-name>
            <surname>Eiter</surname>
            ,
            <given-names>T.</given-names>
          </string-name>
          ,
          <string-name>
            <surname>Gottlob</surname>
            ,
            <given-names>G.</given-names>
          </string-name>
          ,
          <string-name>
            <surname>Perri</surname>
            ,
            <given-names>S.</given-names>
          </string-name>
          ,
          <string-name>
            <surname>Scarcello</surname>
            ,
            <given-names>F.</given-names>
          </string-name>
          :
          <article-title>The DLV system for knowledge representation and reasoning</article-title>
          .
          <source>ACM TOCL 7</source>
          (
          <issue>3</issue>
          ),
          <volume>499</volume>
          {
          <fpage>562</fpage>
          (
          <year>2006</year>
          )
        </mixed-citation>
      </ref>
      <ref id="ref10">
        <mixed-citation>
          10.
          <string-name>
            <surname>Liang</surname>
            ,
            <given-names>S.</given-names>
          </string-name>
          ,
          <string-name>
            <surname>Fodor</surname>
            ,
            <given-names>P.</given-names>
          </string-name>
          ,
          <string-name>
            <surname>Wan</surname>
            ,
            <given-names>H.</given-names>
          </string-name>
          ,
          <string-name>
            <surname>Kifer</surname>
            ,
            <given-names>M.</given-names>
          </string-name>
          :
          <article-title>Openrulebench: an analysis of the performance of rule engines</article-title>
          .
          <source>In: Proceedings of WWW 2009</source>
          . pp.
          <volume>601</volume>
          {
          <fpage>610</fpage>
          .
          <string-name>
            <surname>ACM</surname>
          </string-name>
          (
          <year>2009</year>
          )
        </mixed-citation>
      </ref>
      <ref id="ref11">
        <mixed-citation>
          11.
          <string-name>
            <surname>Liu</surname>
            ,
            <given-names>Y.A.</given-names>
          </string-name>
          ,
          <string-name>
            <surname>Stoller</surname>
          </string-name>
          , S.D.:
          <article-title>From datalog rules to e cient programs with time and space guarantees</article-title>
          .
          <source>ACM Trans. Program. Lang. Syst</source>
          .
          <volume>31</volume>
          (
          <issue>6</issue>
          ),
          <volume>21</volume>
          :1{
          <fpage>21</fpage>
          :
          <fpage>38</fpage>
          (
          <year>2009</year>
          )
        </mixed-citation>
      </ref>
      <ref id="ref12">
        <mixed-citation>
          12.
          <string-name>
            <surname>Ullman</surname>
            ,
            <given-names>J.D.</given-names>
          </string-name>
          :
          <article-title>Principles of Database and Knowledge-Base Systems</article-title>
          . Computer Science Press (
          <year>1988</year>
          )
        </mixed-citation>
      </ref>
    </ref-list>
  </back>
</article>