1 May 201016 May 2010
1 May 201016 May 2010
1 May 201016 May 2010

Querying over disjunctive ASP with functions is a highly undecidable task in general. In this paper we focus on disjunctive logic programs with stratified negation and functions under the stable model semantics (). We show that query answering in this setting is decidable, if the query is finitely recursive (). Our proof yields also an effective method for query evaluation. It is done by extending the magic set technique to . We show that the magic-set rewritten program is query equivalent to the original one (under both brave and cautious reasoning). Moreover, we prove that the rewritten program is also finitely ground, implying that it is decidable. Importantly, finitely ground programs are evaluable using existing ASP solvers, making the class of queries usable in practice.


Disjunctive ASP with Functions: Decidable Queries and Effective Computation    ] Disjunctive ASP with Functions:
Decidable Queries and Effective Computationthanks: This research has been partly supported by Regione Calabria and EU under POR Calabria FESR 2007-2013 within the PIA project of DLVSYSTEM s.r.l., and by MIUR under the PRIN project LoDeN.

Department of Mathematics, University of Calabria
87036 Rende (CS), Italy


8 February 2010

nswer set programming, decidability, magic sets, disjunctive logic programs

1 Introduction

Answer Set Programming (ASP), Logic Programming (LP) under the answer set or stable model semantics, has established itself as a convenient and effective method for declarative knowledge representation and reasoning over the course of the last 20 years [Baral (2003), Gelfond and Lifschitz (1991)]. A major reason for the success of ASP has been the availability of implemented and efficient systems, which allowed for the paradigm to be usable in practice.

This work is about ASP with stratified negation and functions under the stable model semantics (). Dealing with the introduction of function symbols in the language of ASP has been the topic of several works in the literature [Bonatti (2002), Bonatti (2004), Baselice et al. (2009), Calimeri et al. (2009), Syrjänen (2001), Gebser et al. (2007), Calimeri et al. (2008a), Lierler and Lifschitz (2009), Simkus and Eiter (2007), Eiter and Simkus (2009), Lin and Wang (2008), Cabalar (2008)]. They have been motivated by overcoming the major limitation of ASP systems with respect to traditional LP systems, which is the possibility of representing only a finite set of individuals by means of constant symbols. Most of the approaches treat function symbols in the traditional logic programming way, that is by considering the Herbrand universe. A few other works treat function symbols in a way which is closer to classical logic (see, e.g., [Cabalar (2008)]). The fundamental problem with admitting function symbols in ASP is that the common inference tasks become undecidable. The identification of expressive decidable classes of ASP programs with functions is therefore an important task, and has been addressed in several works (see Section 6).

Here, we follow the traditional logic programming approach, and study the rich language of finitely recursive (), showing that it is still decidable. In fact, our work links two relevant classes of ASP with functions: finitely recursive and finitely ground programs. We extend a magic set method for programs with disjunctions and stratified negation to deal with functions and specialize it for finitely recursive queries. We show that the transformed program is query equivalent to the original one and that it belongs to the class of finitely ground programs. Finitely ground programs have been shown to be decidable and therefore it follows that queries are decidable, too. Importantly, by DLV-Complex [Calimeri et al. (2008b)] there is a system which supports query answering on finitely ground programs, so the magic set method serves also as a means for effectively evaluating queries. We also show that programs are maximally expressive, in the sense that each computable function can be represented. In total, programs and queries are an appealing formalism, since they are decidable, a computational system exists, they provide a rich knowledge-modeling language, including disjunction and stratified negation, and they can express any computable function.

Summarizing, the main contributions of the paper are the following:

  • We prove that queries are decidable under both brave and cautious reasoning.

  • We show that the restrictions which guarantee the decidability of queries do not limit their expressiveness. Indeed, we demonstrate that any computable function can be expressed by an program.

  • We provide an effective implementation method for queries, making reasoning over programs feasible in practice. In particular,

    • We design a magic-set rewriting technique for queries. The technique is based on a particular sideways information passing strategy (SIPS) which exploits the structure of queries, and guarantees that the rewritten program has a specific shape.

    • We show that the magic-set rewritten program is query equivalent to the original one (under both brave and cautious reasoning).

    • We prove that the rewritten program is finitely ground, implying that it is computable [Calimeri et al. (2008a)]. Importantly, finitely ground programs are evaluable using the existing ASP solver DLV-Complex [Calimeri et al. (2008b)], making queries usable in practice.

2 Preliminaries

In this section, we recall the basics of ASP with function symbols, and the decidable classes of finitely ground [Calimeri et al. (2008a)] and finitely recursive programs [Baselice et al. (2009)].

2.1 ASP Syntax and Semantics

A term is either a variable or a functional term. A functional term is of the form , where is a function symbol (functor) of arity , and are terms111We also use Prolog-like square-bracketed list notation as in [Calimeri et al. (2008a)].. A functional term with arity 0 is a constant. If is a predicate of arity , and are terms, then is an atom222We use the notation for a sequence of terms, for referring to atoms as .. A literal is either an atom (a positive literal), or an atom preceded by the negation as failure symbol (a negative literal). A rule is of the form

where are atoms and . The disjunction is the head of , while the conjunction is the body of . Moreover, denotes the set of head atoms, while denotes the set of body literals. We also use and for denoting the set of atoms appearing in positive and negative body literals, respectively, and for the set . A rule is normal (or disjunction-free) if , positive (or negation-free) if , a fact if both , and no variable appears in .

A program is a finite set of rules; if all the rules in it are positive (resp. normal), then is a positive (resp. normal) program. In addition, is function-free if each functional term appearing in is a constant. Stratified programs constitute another interesting class of programs. A predicate appearing in the head of a rule depends on each predicate such that an atom belongs to ; if belongs to , depends on positively, otherwise negatively. A program is stratified if there is no cycle of dependencies involving a negative dependency. In this paper we focus on the class of stratified programs.

Given a predicate , a defining rule for is a rule such that some atom belongs to . If all defining rules of a predicate are facts, then is an  predicate; otherwise is an  predicate333 and stand for Extensional Database and Intensional Database, respectively.. Given a program , the set of rules having some IDB predicate in head is denoted by , while denotes the remaining rules, that is, . In addition, the set of all facts of is denoted by .

The set of terms constructible by combining functors appearing in a program is the universe of and is denoted by , while the set of ground atoms constructible from predicates in with elements of is the base of , denoted by . We call a term (atom, rule, or program) ground if it does not contain any variable. A ground atom (resp. a ground rule ) is an instance of an atom (resp. of a rule ) if there is a substitution from the variables in (resp. in ) to such that (resp. ). Given a program , denotes the set of all the instances of the rules in .

An interpretation for a program is a subset of . A positive ground literal is true w.r.t. an interpretation if ; otherwise, it is false. A negative ground literal is true w.r.t.  if and only if is false w.r.t. . The body of a ground rule is true w.r.t.  if and only if all the body literals of are true w.r.t. , that is, if and only if and . An interpretation satisfies a ground rule if at least one atom in is true w.r.t.  whenever the body of is true w.r.t. . An interpretation is a model of a program if satisfies all the rules in .

Given an interpretation for a program , the reduct of w.r.t. , denoted , is obtained by deleting from all the rules with , and then by removing all the negative literals from the remaining rules. The semantics of a program is then given by the set of the stable models of , where an interpretation is a stable model for if and only if is a subset-minimal model of .

Given a program and a query (a ground atom)444More complex queries can still be expressed using appropriate rules. We assume that each functor appearing in also appears in ; if this is not the case, then we can add to a fact (where is a predicate that occurs neither in nor ) and are the arguments of ., cautiously (resp. bravely) entails , denoted (resp. ) if and only if for all (resp. some) . Two programs and are cautious-equivalent (resp. brave-equivalent) w.r.t. a query , denoted by (resp. ), whenever iff (resp.  iff ).

2.2 Finitely Ground Programs

The class of finitely ground () programs [Calimeri et al. (2008a)] constitutes a natural formalization of programs which can be finitely evaluated bottom-up. We recall the key concepts, and refer to [Calimeri et al. (2008a)] for details and examples.

The dependency graph of a program is a directed graph having a node for each IDB predicate of , and an edge if there is a rule such that occurs in and occurs in 555 In literature, is also referred as positive dependencies graph.. A component of is then a set of predicates which are strongly connected in .

The component graph of , denoted , is a labelled directed graph having a node for each component of , an edge if there is a rule such that a predicate occurs in and a predicate occurs in , and an edge if (a) is not an edge of , and (b) there is a rule such that a predicate occurs in and a predicate occurs in . A path in a component graph is weak if at least one of its edges is labelled with “”, otherwise it is strong.

A component ordering is a total ordering of all the components of such that, for any , with , both there is no strong path from to in , and if there is a weak path from to , then there must be a weak path also from to . A module of a program is the set of rules defining predicates in , excluding those that define also some other predicate belonging to a lower component in , that is, a component with .

Given a rule and a set of ground atoms, an instance of is an -restricted instance of if . The set of all -restricted instances of all the rules of a program is denoted by . Note that, for any , . Intuitively, this identifies those ground instances that may be supported by a given set .

Let be a program, a component in a component ordering , a set of ground rules to be simplified w.r.t. another set of ground rules. Then the simplification of w.r.t.  is obtained from by: deleting each rule such that contains some atom ; eliminating from each remaining rule the atoms in , and each atom such that , with , and there is no rule in with in its head. Assuming that contains all ground instances obtained from the modules preceding , deletes from the rules whose head is certainly already true w.r.t.  or whose body is certainly false w.r.t. , and simplifies the remaining rules by removing from the bodies all literals true w.r.t. . We define now the operator , combining and .

Let be a program, a component in a component ordering , and two sets of ground rules. Then , where is the set of atoms belonging to the head of some rule in . The operator always admit a least fixpoint . We can then define the intelligent instantiation of a program for a component ordering as the last element of the sequence , . is finitely ground () if is finite for every component ordering  for . The main result for this class of programs is that reasoning is effectively computable.

Theorem 2.1

Cautious and brave reasoning over programs are decidable.

2.3 Finitely Recursive Queries

We next provide the definition of finitely recursive queries [Calimeri et al. (2009)] and programs [Baselice et al. (2009)].

Let be a program and a query. The relevant atoms for are: itself, and each atom in , where is such that some atom in is relevant for . Then is finitely recursive on if only a finite number of ground atoms is relevant for , and is finitely recursive if every query is finitely recursive on .

Example 2.2

Consider the query for the following program:

The program cautiously and bravely entails the query. The query is clearly finitely recursive; also the program is finitely recursive.

3 Magic-Set Techniques

The Magic Set method is a strategy for simulating the top-down evaluation of a query by modifying the original program by means of additional rules, which narrow the computation to what is relevant for answering the query. In this section we first recall the magic set technique for disjunctive programs with stratified negation without function symbols, as presented in [Alviano et al. (2009)], we then lift the technique to queries, and formally prove its correctness.

3.1 Magic Sets for Function-Free Programs

The method of [Alviano et al. (2009)]666For a detailed description of the standard technique we refer to [Ullman (1989)]. is structured in three main phases.

(1) Adornment. The key idea is to materialize the binding information for IDB predicates that would be propagated during a top-down computation, like for instance the one adopted by Prolog. According to this kind of evaluation, all the rules such that (where for some substitution ) are considered in a first step. Then the atoms in different from are considered as new queries and the procedure is iterated.

Note that during this process the information about bound (i.e. non-variable) arguments in the query is “passed” to the other atoms in the rule. Moreover, it is assumed that the rule is processed in a certain sequence, and processing an atom may bind some of its arguments for subsequently considered atoms, thus “generating” and “passing” bindings. Therefore, whenever an atom is processed, each of its arguments is considered to be either bound or free.

The specific propagation strategy adopted in a top-down evaluation scheme is called sideways information passing strategy (SIPS), which is just a way of formalizing a partial ordering over the atoms of each rule together with the specification of how the bindings originated and propagate [Beeri and Ramakrishnan (1991), Greco (2003)]. Thus, in this phase, adornments are first created for the query predicate. Then each adorned predicate is used to propagate its information to the other atoms of the rules defining it according to a SIPS, thereby simulating a top-down evaluation. While adorning rules, novel binding information in the form of yet unseen adorned predicates may be generated, which should be used for adorning other rules.

(2) Generation. The adorned rules are then used to generate magic rules defining magic predicates, which represent the atoms relevant for answering the input query. Thus, the bodies of magic rules contain the atoms required for binding the arguments of some atom, following the adopted SIPS.

(3) Modification. Subsequently, magic atoms are added to the bodies of the adorned rules in order to limit the range of the head variables, thus avoiding the inference of facts which are irrelevant for the query. The resulting rules are called modified rules.

The complete rewritten program consists of the magic and modified rules (together with the original EDB). Given a function-free program , a query , and the rewritten program , and are equivalent w.r.t. , i.e., and hold [Alviano et al. (2009)].

3.2 A Rewriting Algorithm for Programs

Our rewriting algorithm exploits the peculiarities of queries, and guarantees that the rewritten program is query equivalent, that it has a particular structure and that it is bottom-up computable. In particular, for a finitely recursive query over an program , the Magic-Set technique can be simplified due to the following observations:

  • For each (sub)query and each rule having an atom , all the variables appearing in appear also in . Indeed, if this is not the case, then an infinite number of ground atoms would be relevant for (the query would not be finitely recursive).777We assume the general case where there is some functor with arity greater than 0. Therefore, each adorned predicate generated in the Adornment phase has all arguments bound.

  • Since all variables of a processed rule are bound by the (sub)query, the body of a magic rule produced in the Generation phase consists only of the magic version of the (sub)query (by properly limiting the adopted SIPS).

We assume the original program has no predicate symbol that begins with the string “”. In the following we will then use for denoting the magic predicate associated with the predicate . So the magic atom associated with will be , in which, by previous considerations, each argument is bound.


   Input: A program , and a query Output: The optimized program . var , : set of predicates;  , : set of rules; begin 01.   := ;   := ;   := ;   := ; 02.  while do 03.      take an element from ; remove from ; add to ; 04.      for each rule and for each atom do 05.            := ; 06.           for each atom do     add to ;     end for 07.           add to ; 08.          for each atom such that is an IDB predicate do 09.              add to ;     add to if ; 10.          end for 11.      end for 12.  end while 13. :=   (); 14. return ; end. \figrule

Figure 1: Magic Set algorithm () for queries.

The algorithm implementing the Magic-Set technique for queries is reported in Figure 1. Given a program and a query , the algorithm outputs a rewritten and optimized program , consisting of a set of modified and magic rules, stored by means of the sets and , respectively (together with the original EDB). The algorithm exploits a set for storing all the predicates to be processed, and a set for storing the predicates already done.

The computation starts by initializing and to the empty set (step 1). Then the magic seed (a fact) is stored in and the predicate is inserted in the set (step 1). The core of the algorithm (steps 2–12) is repeated until the set is empty, i.e., until there is no further predicate to be propagated. In particular, a predicate is moved from to (step 3), and each rule having an atom in the head is considered (note that one rule is processed as often as occurs in its head; steps 4–11). A modified rule is subsequently obtained from by adding an atom (for each atom in the head of ) to its body (steps 5–7). In addition, for each atom in such that is an IDB predicate (steps 8–10), a magic rule is generated (step 9), and the predicate is added to the set if not already processed (i.e., if ; step 9). Note that the magic rule is added also if occurs in the head or in the negative body, since bindings are propagated in a uniform way to all IDB atoms.

Example 3.1

The result of the application of the algorithm to the program and query in Example 2.2 is:

3.3 Query Equivalence Result

We conclude the presentation of the algorithm by formally proving its correctness. This section essentially follows [Alviano et al. (2009)], to which we refer for the details, while here we highlight the necessary considerations for generalizing the results of [Alviano et al. (2009)] to queries, exploiting the considerations described in Section 3.2. Throughout this section, we use the well established notion of unfounded set for disjunctive programs with negation defined in [Leone et al. (1997)]. Since we deal with total interpretations, represented as the set of atoms interpreted as true, the definition of unfounded set can be restated as follows.

Definition 3.2 (Unfounded sets)

Let be an interpretation for a program , and be a set of ground atoms. Then is an unfounded set for w.r.t.  if and only if for each ground rule with , either , or , or , or .

Intuitively, conditions , and check if the rule is satisfied by regardless of the atoms in , while condition assures that the rule can be satisfied by taking the atoms in as false. Therefore, the next theorem immediately follows from the characterization of unfounded sets in [Leone et al. (1997)].

Theorem 3.3

Let be an interpretation for a program . Then, for any stable model of , and for each unfounded set of w.r.t. , holds.

We now prove the correctness of the strategy by showing that it is sound and complete. In both parts of the proof, we exploit the following set of atoms.

Definition 3.4 (Killed atoms)

Given a model for , and a model of , the set of the killed atoms w.r.t.  and is defined as:

Thus, killed atoms are either false instances of some EDB predicate, or false atoms which are relevant for (since a magic atom exists in ). Therefore, we expect that these atoms are also false in any stable model for containing .

Proposition 3.5

Let be a model for , and a model of . Then is an unfounded set for w.r.t. .

We can now prove the soundness of the algorithm.

Lemma 3.6

Let be an query over . Then, for each stable model of , there is a stable model of such that if and only if .


We can show that there is such that . Since belongs either to or to , the claim follows by Proposition 3.5.

For proving the completeness of the algorithm we provide a construction for passing from an interpretation for to one for .

Definition 3.7 (Magic variant)

Let be an interpretation for an query over . We define an interpretation for , called the magic variant of w.r.t.  and , as follows:

where is the unique stable model of .

In this definition, we exploit the fact that has a unique and finite stable model for queries (see Lemma 4.2 for a detailed proof). By definition, for a magic variant of an interpretation for , holds. More interesting, the magic variant of a stable model for is in turn a stable model for preserving truth/falsity of . The following formalizes the intuition above.

Lemma 3.8

If is a stable model of an program with a finitely recursive query , then is a stable model of and if and only if .


Consider a modified rule having and :

We can show that

belongs to . Since and , we have , , and . Thus, because is a model of . Moreover, if there is a model of , then is a model for , contradicting the assumption that is a stable model of .

Thus, is a stable model of . Since belongs either to or to , the claim follows by Proposition 3.5.

From the above lemma, together with Lemma 3.6, the correctness of the Magic Set method with respect to query answering directly follows.

Theorem 3.9

If is an query over , then both and hold.

4 Decidability Result

In this section, we prove that queries are decidable. To this end, we link finitely recursive queries to finitely ground programs. More specifically, we show that the Magic-Set rewriting of a finitely recursive query is a finitely ground program, for which querying is known to be decidable.

We first show some properties of the rewritten program due to the particular restrictions applied to the adopted SIPS.

Lemma 4.1

If is an query over , then is stratified.


Each cycle of dependencies in involving predicates of is also present in . Indeed, each magic rule has exactly one magic atom in the head and one in the body, and each modified rule is obtained by adding only magic atoms to the body of a rule belonging to . Since is stratified by assumption, such cycles have no negative dependencies. Any new cycle stems only from magic rules, which are positive.

Now consider the program consisting of the magic rules produced for a finitely recursive query. We can show that this program has a unique and finite stable model, that we will denote .

Lemma 4.2

Let be an query over . Then the program has a unique and finite stable model .


Since is positive and normal, is unique. If we show that contains all and only the relevant atoms for , then we are done because is finitely recursive on . To this end, note that the only fact in is the query seed , and each magic rule in ( a substitution) is such that is relevant for . Indeed, has been produced during the Generation phase involving a rule with and ; since each variable in appears also in , is such that and , i.e., is relevant for .

We can now link queries and finitely ground programs.

Theorem 4.3

Let be an query over . Then is finitely ground.


Let be a component ordering for . Since each cycle of dependencies in involving predicates of is also present in , components with non-magic predicates are disjoint from components with magic predicates. For a component with magic predicates, is a subset of , which is finite by Lemma 4.2.

For a component with a non-magic predicate , we consider a modified rule with an atom :

Thus, the component containing precedes in . Moreover, since is finitely recursive on , each variable appearing in appears also in . Therefore, is finite also in this case.

We are now ready for proving the decidability of brave and cautious reasoning for the class of finitely recursive queries on programs.

Theorem 4.4

Let be an query over . Deciding whether cautiously/bravely entails is computable.


From Theorem 3.9, and hold. Since is finitely ground by Theorem 4.3, decidability follows from Thereom 2.1.

5 Expressiveness Result

In this section, we show that the restrictions which guarantee the decidability of queries do not limit their expressiveness. Indeed, any computable function can be encoded by an program (even without using disjunction and negation). To this end, we show how to encode a deterministic Turing Machine as a positive program with functions and an input string by means of a query. In fact it is well-known that Horn clauses (under the classic first-order semantics) can represent any computable function [Tärnlund (1977)], so we just have to adapt these results for programs and queries.

A Turing Machine with semi-infinite tape is a 5-tuple , where is an alphabet (i.e., a set of symbols), is a set of states, are two distinct states (representing the initial and final states of , respectively), and is a transition function. Given an input string , the initial configuration of is such that the current state is , the tape contains followed by an infinite sequence of blank symbols (a special tape symbol occurring in ; we are assuming does not contain any blank symbol), and the head is over the first symbol of the tape. The other configurations assumed by with input are then obtained by means of the transition function : If and are the current state and symbol, respectively, and , then overwrites with , moves its head according to , and changes its state to . accepts if the final state is reached at some point of the computation.

A configuration of can be encoded by an instance of , where is the current state, the symbol under the head, the list of symbols on the left of the head in reverse order, and a finite list of symbols on the right of the head containing at least all the non-blank symbols. The query representing the initial configuration of with input is