Tabling with Sound Answer Subsumption
Abstract
Tabling is a powerful resolution mechanism for logic programs that captures their least fixed point semantics more faithfully than plain Prolog. In many tabling applications, we are not interested in the set of all answers to a goal, but only require an aggregation of those answers. Several works have studied efficient techniques, such as latticebased answer subsumption and modedirected tabling, to do so for various forms of aggregation.
While much attention has been paid to expressivity and efficient implementation of the different approaches, soundness has not been considered. This paper shows that the different implementations indeed fail to produce least fixed points for some programs. As a remedy, we provide a formal framework that generalises the existing approaches and we establish a soundness criterion that explains for which programs the approach is sound.
This article is under consideration for acceptance in TPLP.
Vandenbroucke et al.]
Alexander Vandenbroucke
KU Leuven, Belgium
(email: alexander.vandenbroucke@kuleuven.be)
and Maciej Piróg
KU Leuven, Belgium
(email: maciej.pirog@kuleuven.be)
and Benoit Desouter
Ghent University, Belgium
(email: benoit.desouter@ugent.be)
and Tom Schrijvers
KU Leuven, Belgium
(email: tom.schrijvers@kuleuven.be)
KEYWORDS: tabling, answer subsumption, lattice, partial order, modedirected tabling, denotational semantics, Prolog
1 Introduction
Tabling considerably improves the declarativity and expressiveness of the Prolog language. It removes the sensitivity of SLD resolution to rule and goal ordering, allowing a larger class of programs to terminate. As an added bonus, the memoisation of the tabling mechanism may significantly improve run time performance in exchange for increased memory usage. Tabling has been implemented in a few wellknown Prolog systems, such as XSB [Swift and Warren 2010, Swift and Warren 2012],Yap [Santos Costa et al. 2012], Ciao [Chico de Guzmán et al. 2008] and BProlog [Zhou 2012], and has been successfully applied in various domains.
Much research effort has been devoted to improving the performance of tabling for various specialised usecases [Swift 1999, Ramakrishna et al. 1997, Zhou and Dovier 2011]. This paper is concerned with one such fairly broad class of usecases: we are not directly interested in all the answers to a tabledpredicate query, but instead wish to aggregate these answers somehow. The following shortestpath example illustrates this usecase.
query(X,Y,MinDist) : findall(Dist,p(X,Y,Dist),List), min_list(List,MinDist). : table p/3. p(X,Y,1) : e(X,Y). p(X,Y,D) : p(X,Z,D1), p(Z,Y,D2), D is D1 + D2. e(a,b). e(b,c). e(a,c).\@endparenvThe query ? query(a,c,D). computes the distance D of the shortest path from a to c by first computing the set of distances of all paths and then selecting the smallest value from this set. Unfortunately, when the graph is cyclic, the set of distances is infinite and the query never returns, even though the infinite set has a welldefined minimal value.
Various tabling extensions (know collectively as answer subsumption: modedirected tabling [Guo and Gupta 2004, Guo and Gupta 2008, Zhou et al. 2010, Santos and Rocha 2013], partial order answer subsumption and lattice answer subsumption [Swift and Warren 2012]), have come up with ways to integrate the aggregation into the tabled resolution. This way answers are incrementally aggregated and the tabling may converge more quickly to the desired results. For instance, the shortestpath program can be written with modedirected tabling as:
: table p(+,+,min). p(X,Y,1) : e(X,Y). p(X,Y,D) : p(X,Z,D1), p(Z,Y,D2), D is D1 + D2. e(a,b). e(b,c). e(a,c).\@endparenvHere the query ? p(a,c,D). yields only the shortest distance. It does so by greedily throwing away nonoptimal intermediate results and in this way only considers finitely many paths, even if the graph is cyclic. In summary, this approach makes tabling (sometimes infinitely) more efficient for our aggregation usecase.
Unfortunately, none of the existing implementations that we are aware of is generally sound. Consider the following pure logic program.
p(0). p(1). p(2) : p(X), X = 1. p(3) : p(X), X = 0.\@endparenvThe query ? p(X). has a finite set of solutions, , the largest of which is p(3). However XSB, Yap and BProlog all yield different (invalid) solutions when answer subsumption is used to obtain the maximal value. Both XSB and BProlog yield X = 2, with a maximum lattice and max table mode respectively. Yap (also with max table mode) yields X = 0; X = 1; X = 2, every solution except the right one.^{1}^{1}1The batch scheduling used by Yap returns any answer as soon as it is found.
Clearly, these results are unsound. This example is not the only erroneous one; we can easily construct more erroneous scenarios with other supported forms of aggregation. Hence, we must conclude that answer subsumption is in general not a semanticspreserving optimisation. Yet, as far as we know, the existing literature does not offer any guidance on when the feature can be relied upon. In fact, to our knowledge, its semantics have not been formally discussed before.
This paper fills the semantic gap of answer subsumption with the help of lattice theory. We show how the existing implementations fit into this semantic framework and derive a sufficient condition for semantics preservation that allows answer subsumption to be safely used.
2 Background: Tabling Semantics
Because the operational semantics of tabling is rather complex and different systems vary in subtle ways, we make a simplifying assumption and assume that tabling systems implement Lloyd’s least fixedpoint semantics [Lloyd 1984] for definite logic programs, that is, that tabling is a sound program optimisation with no impact on the denotation of a program. This semantics conveniently abstracts from lowlevel aspects such as clause and goal ordering and the specific clause scheduling algorithm used by the Prolog engine.
2.1 Least FixedPoint Semantics
First, we need the notion of a Herbrand base: the set of all possible (ground) atoms that occur in a logic program. More formally, let be an alphabet, and be a logic program over , then the Herbrand base is the set of all ground atoms over . For example, the Herbrand base of the shortest path program (without query/3,+/2) is:
A (Herbrand) interpretation is a set . Intuitively, it contains atoms in the Herbrand base that are true: .
Finally, define the operator such that, given an interpretation , the value is the interpretation that immediately follows from by any of the program rules:
(0) 
This operator is called the immediate consequence operator. Its least fixedpoint with respect to subsetinclusion (), denoted , defines the semantics of the program , and is also known as the least Herbrand model. It is the interpretation that contains those and only those atoms that follow from the program and that are not selfsupported.
Example 1
Consider the following program :
p(a). p(b). q(c).q(X) : p(X).\@endparenvIts Herbrand base is . Its fixedpoint semantics is:
Observe that this is exactly the set of atoms that follow from the program.
2.2 Existence and Computability of the Least Herbrand Model
The least fixedpoint semantics is not necessarily welldefined: it is not immediate that the least fixedpoint actually exists. Moreover, if it exists, it may not actually be constructively computable.
Fortunately, there is no reason for concern: by appeal to a wellknown theorem from lattice theory, we can easily establish the welldefinedness. A complete lattice is a partially ordered set (poset) such that every has a least upper bound , i.e.:
We do indeed have a lattice structure at hand: the power set of the Herbrand base is a complete lattice. In fact, any power set is a complete lattice. Moreover, it is quite easy to see that if is a definite logic program (i.e., contains no negations), then is monotone with respect to this lattice. It follows that exists, ensuring that the semantics is welldefined for every definite program , by the following theorem:
Theorem 2.1 (Knaster–Tarski)
Let be a complete lattice, and let be a monotone function. Then, has a least fixed point, denoted .
Moreover, the operator is continuous, which means that for all ascending chains with , it is the case that . Then, Kleene’s fixedpoint theorem gives us a constructive way of obtaining :
The least fixedpoint can therefore be obtained in a bottomup fashion by iterating from the empty set onward. Operationally, tabling usually interleaves a topdown goaldirected strategy with bottomup iteration. The bottomup strategy always computes the entire least Herbrand model, even when only a small portion of it may be required to prove a particular query. The topdown part of tabling avoids computing irrelevant atoms as much as possible, making inference feasible.
2.3 Stratification
Unfortunately, the operator is not monotone for programs containing more advanced constructs, such as negation. Therefore Lloyd’s semantics as described above is not suitable for capturing the semantics of such programs. In the case of negation, this problem is solved by partitioning the clauses of a program into an ordered set of strata based on their interdependence. This procedure is called stratification [Apt et al. 1988]. Then, the semantics for each stratum is computed based on the semantics of the lower strata, with no relation to the higher strata. To make this more concrete, suppose a ground program admits a stratification , with the nonempty and pairwise disjoint, then:
where should be understood as a set of facts. If a program admits a stratification where all negated calls are to predicates defined in lower strata, the obvious extension of the operator to include negation is guaranteed to be monotone. The semantics of is then given by .
3 Answer Subsumption Approaches
In this section, we propose a denotational semantics for tabling with answer subsumption. Broadly speaking, we modify the semantics for stratified programs as described in the previous section in two respects. First, our semantics includes new answers that may emerge from the programdefined rules of subsumption, which are not necessarily logical consequences of the same program without answer subsumption. We obtain this by extending the operator. Secondly, we perform the actual subsumption, that is, we remove the subsumed answers. Stratification, as discussed in Section 3.4, is used to control the order in which these two steps are invoked.
In a bit more detail, the semantics of a stratum is given by the extended immediate consequence operator, which we call , and a function that aggregates the answers using a lattice . A consequence of this specification is that an aggregation naturally ignores all operational aspects of the program P. That is to say, given two structurally distinct programs and whose least fixedpoint semantics coincide, that is, , it follows that , i.e. their aggregates coincide as well.
Obviously, the existing systems do not implement answer subsumption as a single postprocessing function. Instead, they execute it repeatedly during the bottomup phase of the computation, which sometimes makes them deviate from the intended semantics, as exemplified in the introduction. We formalise and deal with this in Section 4.
For now, we assume that the program has only one stratum. Towards the end of the section, we show how to assemble the semantics of programs with any number of strata.
3.1 ModeDirected Tabling
Modedirected tabling is a convenient aggregation approach supported by ALSProlog, BProlog and Yap where the arguments of a tabled predicate are annotated with one of a range of aggregation modes. Yap provides the largest range of possible modes: index, first, last, min, max and all. The answers are grouped by distinct values for the index arguments; the remaining arguments are aggregated according to their mode – the mode names should be selfexplanatory.
Based on our assumptions so far, we can immediately disqualify the existing implementations of the three modes first, last and sum. The reason is that the semantics of the existing implementations is inherently sensitive to the program structure. Consider the two programs below:
Clearly , however yields p(1) as an answer for ? p(X). while yields p(2). The opposite happens with the last mode. The next programs illustrate the problem of the sum mode:
Again the least fixedpoint semantics of both programs coincides: . However they produce the following results in Yap:
Yap produces the result p(1) twice the first time the query is called. Any subsequent query is answered with p(2). In other words, not only are the results of and not consistent, the results for are not internally consistent either.
In the rest of this paper we disregard these three modes. As their implementations are so obviously sensitive to the program structure, we do not see a good way to reconcile them with our semanticsoriented postprocessing specification for aggregation. In fact, in our opinion these modes are best avoided in highlevel logic programs.
3.2 LatticeBased Approaches
The remaining three modes, min, max and all, share one notable property: they are all based on a joinsemilattice structure defined on (subsets of) , the set of all ground terms over the alphabet of . A joinsemilattice is a poset such that every finite subset has a least upper bound in , which we denote, as in the case of complete lattices, . For example, the set of natural numbers with standard order is a joinsemilattice (with ), but it is not a complete lattice. Different modes define the following joinsemilattices:

min defines the joinsemilattice where is the lexicographical ordering on terms (=</2). The least upper bound is the minimum.

max defines the joinsemilattice where is the inverse of . The least upper bound is the maximum.

all defines the joinsemilattice where is the set of all finite subsets of . Existing implementations represent sets as lists of terms, which are themselves terms.
The two additional aggregation approaches, offered by XSB, are also based on joinsemilattices:

XSB generalises the above modes to userdefined joinsemilattices with the
lattice(/3) mode that is parameterised in a binary join operator. For instance, we can define the min mode as lattice(min/3). 
XSB also provides a second userdefinable mode po(/2) in terms of a partial order on . This partial order induces a joinsemilattice where .
Therefore, in what follows, we only have to deal with lattices that are essentially subsets of , considerably simplifying the formulae.
As we can always reorder arguments and combine multiple joinsemilattices into their product joinsemilattice, we assume, without loss of generality, that only the final argument of a predicate is an output tabling mode. That is, all ground atoms have the shape where is the name of some predicate, is a vector of input arguments and is the value of the output parameter. We make the simplifying assumption that all predicates are tabled. If a predicate has only input arguments (like tabling without answer subsumption), a (constant) dummy output can always be added.
Modedirected tabling groups atoms for a predicate by distinct values for the input arguments and aggregates the values of the output argument into a single term. Therefore, we model a table of aggregated answers by a function (where is the set of predicate names in ) that maps a pair of a predicate name and inputs to a single aggregated output. The set of aggregate answers is the set of all terms, on which a special element is grafted, to indicate the lack of an answer. We extend the chosen order on terms such that is an (adjoined) bottom element, that is, . For legibility, we will also sometimes refer to by . The lattice structure induces a joinsemilattice structure on the set of tables, where is the pointwise order:
This lattice structure allows us to aggregate over multiple tables, by aggregating the answers pointwise:
By storing each individual element of into a table and then aggregating over tables we obtain the semantics for mode directed tabling:
Let and be defined as:
Thus, the function turns an atom into a singleton table, and maps a table to the set of its true atoms.
To compute the set of all true atoms of a program , we need to consider the consequence of joining two elements of a semilattice in addition to regular logical consequences. This is because, for arbitrary lattices, the result of a join can be distinct from any of its arguments, and thus produce new facts. We define a new immediate consequence operator , which extends the regular operator with answers obtained by joins. Formally, we define as follows, where denotes the set of all finite subsets of a set :
(0) 
One can show that is continuous, hence monotone. In fact, for linear orders (such as min and max), behaves exactly like . Again, we consider the least fixedpoint of to be the set of all the answers that can be obtained by the logical rules and the ‘lattice rules’.
The next step is to discard the subsumed answers by applying the join operator on the set of answers. Thus, the set of all true atoms of the program using modedirected tabling is given by:
(0) 
Obviously, when is infinite, the least upper bound above does not necessarily exist. That is why to give a full denotational semantics of answer subsumption in the next subsection, we model tables in a more abstract way as complete lattices. Now, to provide some intuition, we give an example in which the least upper bound exists.
Example 2
Consider the example from the introduction, rewritten using XSB’s lattice answer subsumption for the sake of variety:
: table p(lattice(_,_,min/3)). : table e/3. p(X,Y,1) : e(X,Y,nt). p(X,Y,D) : p(X,Z,D1), p(Z,Y,D2), D is D1 + D2 e(a,b,nt). e(b,c,nt). e(a,c,nt). min(X,Y,Z) : Z is min(X,Y).\@endparenvNote that we have additionally tabled e/3 and added a dummy output parameter (nt stands for not tabled), as described above. The don’t cares (_) in the tabling directive indicate that they are not part of the lattice. In Yap’s terminology: they use the index tabling mode. The least fixedpoint semantics of this program, that is , is given by the following set:
The complete lattice on the final argument of p is , the reversed standard order. The least upper bound in this lattice is the usual infimum on natural numbers.
Interpreted by this lattice, the semantics is
Only the atoms representing the shortest paths are retained, as expected.
Example 3
This example illustrates why we need to extend the operator to include the results of the lattice operations, that is, why we need the operator. Consider the lattice , with and , which we use in the following program:
lub(a,b,c). lub(a,c,c). lub(a,d,d). lub(b,a,c). lub(b,c,c). lub(b,d,d). lub(c,d,d). lub(X,X,X). : table p(lattice(lub/3)). p(a). p(b). p(d) : p(c).\@endparenv
The regular immediate consequence gives us , which means that . The atom does not follow from the logical inference, but from the lattice’s join operator. It is included in the overall answer thanks to the postprocessing step, but its logical consequences are not. With the operator we have , and so , which is the intended semantics.
3.3 Answer Subsumption for Arbitrary Lattices
Even though at any point of computation each table is finite, it is potentially infinite when a program produces infinitely many answers. Thus, to give a denotational semantics for answer subsumption, a joinsemilattice on terms is not enough, as we need least upper bounds of infinite sets, i.e. a complete lattice structure. For example, the most natural candidate for the types of values in the case of the all mode is , the complete lattice of all subsets of , which cannot be modelled by (finite) terms. In general, every semilattice can be extended to a complete lattice via MacNeille (?) completion.
Thus, we do not impose any order on the set , and the type of the table becomes for a complete lattice . For each predicate , we also need two bottompreserving abstraction and representation functions: and respectively. We require to be a retraction of , that is, . Since we want the two functions to preserve bottoms, the least element of denotes ‘no value’. With this, we give new definitions of and , appropriately adding abstraction and representation, where is the least element of :
3.4 Lattice Semantics for Stratified Programs
For general programs, we use stratification to distinguish between predicates that imply and are implied by tabled values. We define the depends on relation as follows: for any two predicates and , it is the case that if and only if there exists a clause p(...):...,q(...),.... We say that and are in the same stratum if and , where is the reflexive and transitive closure of . Put differently, a stratum is a strongly connected component of the dependency graph defined by . The relation induces a partial ordering on the set of all strata , that is, for , it is the case that if and only if there exists and such that .
A stratum forms a logical unit to which the least fixed point semantics and aggregation are applied in turn: For each stratum , we can define its semantics as follows: , where and is the set restricted to the predicates in the stratum , while should be understood as a set of facts. Informally, this means that to give a semantics for a stratum, we first compute the semantics of the strata below, use the results as a set of facts added to the part of the program in the current stratum, compute the fixed point, and finally perform the aggregation step using the join operator. There are always finitely many strata, so is welldefined. The semantics of the program is then the aggregation of the sum of the interpretations of all the strata, that is, .
Stratification ensures that the answers for a predicate are always aggregated before they are used by another predicate, unless there is a cyclic dependency between them. for example, consider the following variation on the shortest path program:
: table p(index,index,min). e(1,2). e(2,3). e(1,3). p(X,Y,1) : e(X,Y) p(X,Y,D) : p(X,Z,D1),p(Z,Y,D2), D is D1 + D2. s(X,Y,D) : p(X,Y,D).\@endparenv
Without stratification, the semantics is given by which contains s(1,3,2), as a consequence of p(1,3,2) before aggregation. However, if we stratify the program as discussed above, the rules for p end up in a lower stratum than s. Therefore, the results for p will be aggregated by min, before any consequence is derived from it. Since p(1,3,2) is subsumed, s(1,3,2) is no longer derived.
When two predicates are interdependent (and therefore in the same stratum), but only one of them is tabled, the answers for the untabled predicate are not subsumed. Stratification then gives a result that appears inconsistent:
: table even(min). even(0). even(X) : odd(Y), Y is X  1. odd(X) : even(Y), Y is X  1.\@endparenvOur semantics interprets this program as the set
While seems equally reasonable. Because we treat subsumption as a postprocessing step per stratum, which means that interdependent predicates are resolved as if no answers were subsumed. Subsumption only affects predicates in the strata above. For instance, assume we add the following (nontabled) predicate to the program:
also_odd(X) : even(Y), Y is X  1.\@endparenvIt is in a different stratum than and , so its semantics depends on the semantics of after the subsumption step. This means that the semantics together with the predicate is given as:
Here also_odd behaves like the alternative suggested for odd above. Importantly, programs like the one above do not satisfy our correctness condition for the greedy strategy given in Section 4.
4 Generalised Answer Subsumption Semantics
The previous section specifies the semantics of tabling with latticebased answer subsumption in terms of a postprocessing aggregation. However, the existing implementations do not actually first compute the least Herbrand model. Instead, they greedily aggregate intermediate results during SLDresolution. This makes it feasible to, for instance, compute the shortest path in a cyclic graph in a finite amount of time, as well as generally improving efficiency. However, as the examples in the introduction acutely demonstrate, this greedy strategy is not always valid. This section characterises the greedy strategy as a form of generalised semantics for logic programs and considers its correctness with respect to our postulated specification.
4.1 Generalised Semantics
Again, we assume that we work in a single stratum. We can capture alternate greedy strategies as generalised semantics of in terms of structures , where:

is a complete lattice,

is a function that ‘embeds’ terms in the lattice,

(a generalised immediate consequence operator) is monotone,

is generated by , which means that for every , there exists an such that .
Note that in general we do not need a counterpart of the function: in the postprocessing semantics given in Section 3, we need to define the operator and move between strata. Here, we use this semantics to capture correctness within a single stratum, and the immediate consequence operator is not defined, but it is given. This allows us to generalise the whole table to a lattice, which simplifies the generalised semantics.
The generalised semantics of the program is given by , which exists due to the Knaster–Tarski theorem. It is easy to verify that the regular least fixedpoint semantics is a valid instance of this generalised semantics. Also note that the generalised semantics does not depend on or the fact that is generated by . We need these facts in a moment to establish a correctness criterion.
One obvious instantiation of this semantics is with the operator defined in Section 3.3, where is the singleton function:
We say that an instance of the generalised semantics is a correct implementation strategy only if yields the same result as the postprocessing semantics defined in Section 3. More formally, a generalised semantics is correct if and only if
(0) 
where, for convenience, we define, for any lattice , set , and function , the function as follows:
Intuitively, it means that evaluating the whole program with no answer subsumption and then obtaining the final result using ’s join operation on the answers is the same as computing every step with the lattice (which is usually more efficient). The following theorem gives sufficient conditions for the equation ( ‣ 4.1) to hold.
Theorem 4.1 (FixedPoint Fusion [Backhouse 2000])
Let and be posets. Let and be two functions with least fixed points. Let be a function. Assume the following:

It is the case that .

The function has an upper Galois adjoint, that is, there exists a function such that for all and .
Then, .
The equation ( ‣ 4.1) is clearly an instance of this theorem’s conclusion, with , and . Yet, the theorem only applies if we can show that (a) , and that (b) has an upper Galois adjoint. Fortunately, (b) readily follows from the fact that is continuous, and that every continuous function has an upper Galois adjoint [Backhouse 2000].
Intuitively the remaining condition (a) means that should preserve immediate consequences. In other words, subsumption of a conventional immediate consequence should be the generalised immediate consequence of .
4.2 Greedy Strategy
For better performance, practical implementations of tabling with answer subsumption use a greedy strategy, which means that they remove subsumed answers as soon as possible, and not as a single postprocessing step. We can try to express it as the following instance of the generalised semantics, in terms of the functions defined in Section 3.3:
The function extracts the set of true ground atoms from a table, which is a subset of the Herbrand base , so we can indeed express the ‘greedy’ immediate consequence operator in terms of the standard immediate consequence operator , subsumption and extraction . In fact this definition is quite convenient, because tabled Prolog systems readily provide an efficient implementation of .
The question remains if the function has a fixed point. Luckily, given a tuple , where is generated by , the condition (a) is enough for to be monotone (this is where the assumption that generates comes in handy):
Theorem 4.2
Let be as above. If (the condition (a)), then is monotone.
Additionally, it is the case that . Thus, to show correctness of a program under the greedy semantics, we need only show the following:
(0) 
Example 4
Reconsider the invalid program from the introduction. We show that the condition ‣ 4.2 does not hold for this program under semantics where and is the partial order on terms. By means of the following counter example: A simple calculation shows that .
This example also explains the odd behaviour of XSB, Yap and BProlog: p(0) is subsumed by p(1), therefore the body of the third rule in the program cannot be satisfied and p(3) (the correct answer) is never produced.
Interestingly, when we change the last rule to p(3) : p(0), the result of the query ?p(X). changes once more in all systems, although logically both rules should behave identically. Furthermore, different implementations handle this rule differently. For instance, XSB reasonably disallows calls where latticemode arguments are not free, and the latter rule therefore produces an error message. While Yap instead produces X=3, because its batchedmode evaluation immediately derives p(1) from the fact p(1).
Example 5
Now, consider the shortest path program from Example 3.2 under semantics and . We prove the correctness condition by proving that and . Then by antisymmetry of , the correctness condition holds.
(direction) Because is monotone, and for this case is deflative, i.e. for all , we are done with this direction.
(direction) A p(x,y,d)atom is either introduced by an edge e(x,y,nt) or by two other atoms p(x,z,d1) and p(z,y,d2). In the former case , in the latter case . It is easy to see that if an atom p(x,y,1) is introduced by , it is also introduced by . In the latter case, and are at least as large as their corresponding entries in the table produced by . Hence must be at least as large as any corresponding distance in the set produced by . Hence the infimum of s produced by must be at least as large as any corresponding distance. Finally this means that the entry in the table produced by must be at least as large as the entry in the table produced by .
5 Related Work
As far as we know, “output” subsumption for tabling was first proposed by Van Hentenryck et al. (?) in the context of abstract interpretation.
Tabling Modes for Dynamic Programming
In dynamic programming, an optimal solution to a problem is defined in terms of the optimal solutions of smaller subproblems. This intuition is in fact captured by the correctness condition (Equation ‣ 4.2): it states that the solution found by examining all subsolutions, is equal to examining only the optimal solutions. This is good news, because it implies correctness for dynamic programming.
We have already discussed the tabling modes of Yap [Santos and Rocha 2013] and XSB [Swift and Warren 2012] at length in Section 3.1. XSB’s lattice based answer subsumption is more suitable for implementing techniques that require more general lattices than simple minimum and maximum, such as abstract interpretation.
Guo and Gupta (?; ?) implemented 5 tabling modes in ALSProlog with the aim of simplifying and accelerating dynamic programming. These modes are + (indexed), â(only the first answer is retained), min (minimum), max (maximum) and last (the last answer is retained). The correspondence to Yap’s tabling modes is obvious, but there is no equivalent for Yap’s all and sum modes. Answers are grouped by distinct values for the + arguments and the remaining arguments are aggregated based on their mode.
Zhou et al. (?) added tabling to BProlog with the same purpose in mind. Consequently, the tabling modes they support are identical, except they do not implement a last mode. Instead they support cardinality constraints, which limit the answers that are stored in the table to the first optimal answers for some positive integer . Also supported is an nt (nottabled) tabling mode, which is used to pass around global constants efficiently. From the perspective of the tabling system nt arguments do not exist, and thus are never stored in the table.
Haskell
Vandenbroucke et al. (?) have added lattice answer subsumption to their tabling implementation in Haskell. It is based on the effect handlers approach.
Abstract interpretation
Our approach bears a strong resemblance to abstract interpretation [Cousot and Cousot 1992, Abramsky and Hankin 1987]. Unlike answer subsumption, abstract interpretation admits approximate solutions, implying a weaker correctness condition where equality is replaced by an order relation.
Matroids and Greedoids
Other set theoretic structures (besides lattices) such as matroids [Oxley 1992] and greedoids [Korte et al. 1991], have been developed to analyse greedy algorithms and show their optimality. As answer subsumption is essentially a greedy strategy, we plan to reexamine answer subsumption in this new context in the future.
6 Conclusion and Future Work
In many instances of tabling only the optimal answers to a query are relevant. To improve performance over a naive generateandaggregate approach, various forms of answer subsumption that greedily combine these answers have been developed in the literature. However, their semantics has never been described formally. An operational understanding is always an option in this case, and although often useful, it is a far cry from the declarative ideal that tabling promises.
We define a highlevel semantics for answer subsumption based on lattice theory. Then we generalise it to establish a correctness condition indicating when it is safe to use (greedy) answer subsumption. We show several examples where the existing implementations of answer subsumption fail that condition and derive an erroneous result.
This condition is sufficient, but not necessary: there may still exist programs that do not satisfy the condition, for which the greedy strategy nevertheless delivers correct results. Since we have not run across any noncontrived examples of such programs, we contend that this apparent lack of necessity is an artefact of our rather coarse semantics, which we intend to refine in future work.
The verification of correctness does constitute a nontrivial effort. Hence, manually proving the correctness condition for realistically sized programs could be unfeasible in practice. Ideally we would have an automated analysis that warns the programmer if it fails to establish the correctness condition. This is future work.
Acknowledgements
We would like to thank Bart Demoen for enlightening discussions during the preparation of this paper.
References
 Abramsky and Hankin 1987 Abramsky, S. and Hankin, C. 1987. Abstract Interpretation of declarative languages. Vol. 1. Ellis Horwood, Chapter An introduction to abstract interpretation, 63–102.
 Apt et al. 1988 Apt, K. R., Blair, H. A., and Walker, A. 1988. Towards a Theory of Declarative Knowledge. Morgan Kaufmann.
 Backhouse 2000 Backhouse, R. C. 2000. Galois connections and fixed point calculus. In Algebraic and Coalgebraic Methods in the Mathematics of Program Construction, International Summer School and Workshop, Oxford, UK, April 1014, 2000, Revised Lectures, R. C. Backhouse, R. L. Crole, and J. Gibbons, Eds. LNCS, vol. 2297. Springer, 89–148.
 Chico de Guzmán et al. 2008 Chico de Guzmán, P., Carro, M., Hermenegildo, M. V., Silva, C., and Rocha, R. 2008. An improved continuation callbased implementation of tabling. In Practical Aspects of Declarative Languages, 10th International Symposium. LNCS, vol. 4902. Springer, 197–213.
 Cousot and Cousot 1992 Cousot, P. and Cousot, R. 1992. Abstract interpretation and application to logic programs. The Journal of Logic Programming 13, 2â3, 103 – 179.
 Guo and Gupta 2004 Guo, H.F. and Gupta, G. 2004. Simplifying dynamic programming via tabling. In Practical Aspects of Declarative Languages. LNCS, vol. 3057. Springer, 163–177.
 Guo and Gupta 2008 Guo, H.F. and Gupta, G. 2008. Simplifying dynamic programming via modedirected tabling. Software: Practice and Experience 38, 1, 75–94.
 Korte et al. 1991 Korte, B., Lovász, L., and Schrader, R. 1991. Greedoids, algorithms and combinatorics, vol. 4.
 Lloyd 1984 Lloyd, J. W. 1984. Foundations of Logic Programming. SpringerVerlag, New York.
 MacNeille 1937 MacNeille, H. M. 1937. Partially ordered sets. Transactions of the American Mathematical Society, 416–460.
 Oxley 1992 Oxley, J. G. 1992. Matroid theory. Oxford University Press.
 Ramakrishna et al. 1997 Ramakrishna, Y. S., Ramakrishnan, C. R., Ramakrishnan, I. V., Smolka, S. A., Swift, T., and Warren, D. S. 1997. Computer Aided Verification: 9th International Conference, Haifa, Israel, June 22–25, 1997 Proceedings. Springer, 143–154.
 Santos and Rocha 2013 Santos, J. and Rocha, R. 2013. On the efficient implementation of modedirected tabling. In Practical Aspects of Declarative Languages. LNCS, vol. 7752. Springer, 141–156.
 Santos Costa et al. 2012 Santos Costa, V., Rocha, R., and Damas, L. 2012. The YAP Prolog system. Theory and Practice of Logic Programming 12, 12, 5–34.
 Swift 1999 Swift, T. 1999. Tabling for nonmonotonic programming. Annals of Mathematics and Artificial Intelligence 25, 34, 201–240.
 Swift and Warren 2010 Swift, T. and Warren, D. 2010. Tabling with answer subsumption: Implementation, applications performance. 300–312.
 Swift and Warren 2012 Swift, T. and Warren, D. S. 2012. XSB: Extending Prolog with tabled logic programming. Theory and Practice of Logic Programming 12, 12 (Jan.), 157–187.
 Van Hentenryck et al. 1993 Van Hentenryck, P., Degimbe, O., Charlier, B. L., and Michel, L. 1993. Abstract interpretation of Prolog based on OLDT resolution. Tech. rep., Providence, RI, USA.
 Vandenbroucke et al. 2016 Vandenbroucke, A., Schrijvers, T., and Piessens, F. 2016. Fixing nondeterminism. In Proceedings of the 27th symposium on Implementation and Application of Functional Languages 2015.
 Zhou 2012 Zhou, N.F. 2012. The language features and architecture of BProlog. Theory and Practice of Logic Programming 12, 12, 189–218.
 Zhou and Dovier 2011 Zhou, N. F. and Dovier, A. 2011. A tabled prolog program for solving sokoban. In 2011 IEEE 23rd International Conference on Tools with Artificial Intelligence. 896–897.
 Zhou et al. 2010 Zhou, N.F., Kameya, Y., and Sato, T. 2010. Modedirected tabling for dynamic programming, machine learning, and constraint solving. In 22nd International Conference on Tools with Artificial Intelligence (ICTAI), 2010. Vol. 2. 213–218.
Appendix A Proofs and Calcuclations
a.1 continuity of the operator
Let be an chain, that is, . We need to show that . The lefthand side:
(def. of )  
( is cont.)  
(def. of ) 
The righthand side:
(image)  
(def. of )  
(def of ) 
Thus, it is enough to show that if and only if . The direction is trivial. For the direction pick with the lowest such that , which exists, since is finite.
a.2 Proof of Theorem 4.2
Let . Since is generated by , there exist such that and .
=  (def. of )  
=  (assumption)  
(composition preserves monotonicity)  
=  (assumption)  
=  (def. of )  
= 
a.3 Counter Example for Example 4.2
Appendix B Additional Examples
This appendix contains some additional examples that do not fit in the main part of the paper because of the page limit, but which could be useful in understanding the details of our semantics for tabling with answer subsumption.
b.1 The Extended Immediate Consequence Operator
This example illustrates why we need to extend the operator to include the results of the lattice operations, that is, why we need the operator. Consider the lattice , with and , which we use in the following program:
lub(a,b,c). lub(a,c,c). lub(a,d,d). lub(b,a,c). lub(b,c,c). lub(b,d,d). lub(c,d,d). lub(X,X,X). : table p(lattice(lub/3)). p(a). p(b). p(d) : p(c).\@endparenv
The regular immediate consequence gives us , which means that . The atom does not follow from the logical inference, but from the lattice’s join operator. It is included in the overall answer thanks to the postprocessing step, but its logical consequences are not. With the operator we have , and so , which is the intended semantics.
b.2 Circular Dependencies
The following example shows what happens when two predicates are interdependent, but only one of them is tabled:
: table even(min). even(0). even(X) : odd(Y), Y is X  1. odd(X) : even(Y), Y is X  1.\@endparenv
Our semantics interprets this program as the set
One other possible candidate would be . It is because we treat subsumption as a postprocessing step per stratum, which means that interdependent predicates are resolved as if no answers were subsumed. Subsumption affects predicates in the strata above. For instance, assume we add the following (nontabled) predicate to the program:
also_odd(X) : even(Y), Y is X  1.\@endparenv
It is in a different stratum than and , so its semantics depends on the semantics of after the subsumption step. This means that the semantics together with the predicate is given as:
Importantly, programs like the one above do not satisfy our correctness condition for the greedy strategy.
b.3 Example of answer subsumption for arbitrary lattices
Consider the shortest path program, now interpreted in the lattice , the natural numbers extended with infinity with the canonical order, and abstractions and representations , , and , where:
This lattice computes the longest path in the graph, demonstrating that a change in the interpreting lattice can change the result of the program entirely. Now suppose the edge e(c,a,nt) is added to the program, creating a cycle. The least fixedpoint semantics becomes infinite, and contains only paths of infinite length, represented by atoms such as p(a,c,infty). Although such least fixedpoints are not constructively computable in practice, from a theoretical point of view they demonstrate the essence of this approach quite well.