format: text

text:
# Termination Proofs for Logic Programs with TablingThis article is a collection and integration of a number of results that appeared—sometimes in weaker forms—in the conference papers [27] and [28].

## 1 Introduction

Tabled logic programming  is receiving increasing attention in the Logic Programming community. It avoids many of the shortcomings of SLD(NF) execution and provides a more flexible and often extremely efficient execution mechanism for logic programs. Furthermore, tabled execution of logic programs terminates more often than execution based on SLD-resolution. In particular, all programs that terminate under SLD also terminate under tabled execution. So, if a program can be proven to terminate under SLD-resolution (by one of the existing automated techniques surveyed in ), then the program will trivially also terminate under SLG-resolution, the resolution principle of tabling; see . But, since there are SLG-terminating programs which are not SLD-terminating, more effective proof techniques need to and can be found.

The idea underlying *tabling* is quite simple. Essentially, under a tabled execution mechanism, answers for selected tabled atoms as well as these atoms are stored in a table. When an identical (up to renaming of variables) such atom is recursively called, the selected atom is not resolved against program clauses; instead, all corresponding answers computed so far are looked up in the table and the corresponding answer substitutions are applied to the atom. This process is repeated for all subsequent computed answer substitutions that correspond to the atom.

We study *universal* termination of *definite* tabled logic programs executed under SLG-resolution using a fixed *left-to-right selection rule* (we drop the “S” in SLD and SLG whenever we refer to the left-to-right selection rule). We introduce a first basic notion of termination under tabled execution, called quasi-termination. Quasi-termination captures the property that, under an LD-computation, a given atomic query leads to only finitely many different non-variant calls to tabled predicates and there is no infinite derivation consisting of queries with only selected non-tabled atoms. In a broader context, the notion of quasi-termination and techniques for proving it are of independent interest; they can be used to e.g. ensure termination of off-line specialisation of logic programs, whether tabled or not; see . However, the notion of quasi-termination only partially corresponds to our intuitive notion of a “terminating computation”. This is because an atom can have infinitely many computed answers (which does not have to lead to infinitely many new calls). Therefore, we also introduce the stronger notion of LG-termination. A program $`P`$ LG-terminates w.r.t. a given atomic query iff $`P`$ quasi-terminates w.r.t. the query and the set of all computed answers for calls in the LD-computation of the query is finite.

We present *sufficient conditions* for these two notions of termination under tabled execution: namely, quasi-acceptability for quasi-termination and LG-acceptability for LG-termination. We show that these conditions are also *necessary* in case the set of tabled predicates is *well-chosen*; see Section 5. Our termination conditions are adapted from the acceptability notion for LD-termination defined in , and not from the more “standard” definition of acceptability by Apt and Pedreschi in . The reason for this choice is that the quasi-termination as well as the LG-termination property of a tabled program and query is *not* closed under substitution. The acceptability notion in  is expressed in terms of ground instances of clauses and its associated notion of LD-termination is expressed in terms of the set of all queries that are bounded under the given level mapping. Such sets are closed under substitution. Because quasi-termination and LG-termination lack invariance under substitution, we use a stronger notion of acceptability, capable of treating *any* set of queries.

Besides a characterisation of the two notions of universal termination under tabled execution, we also give *modular termination conditions*, i.e., conditions on two programs $`P`$ and $`R`$, where $`P`$ extends $`R`$, ensuring termination of the union $`PR`$. Such modular proofs were already motivated in the literature in the context of termination under SLD-resolution (see for instance ). Indeed, for programming in the large, it is important to have modular termination proofs, i.e., proofs that are capable of combining termination proofs of separate programs to obtain termination proofs of combined programs.

Finally, we present easy to *automate*, sufficient conditions for quasi-termination and LG-termination. To this end, we use mode information: we consider simply moded, well-moded programs and queries. We point out how these termination conditions could be automated, by extending the recently developed, constraint-based, automatic termination analysis for SLD-resolution of .

All the above mentioned results are developed and presented for a *mixed tabled/non-tabled* execution mechanism. This means that, in the execution, only a subset of the predicates (specified by the programmer) will be tabled, while standard LD-resolution steps are applied to all others. In Section 3, we discuss the benefits of having such a mixed execution mechanism. This focus on mixed execution considerably strengthens our results. In particular, our results both introduce new termination conditions for (fully) tabled logic programs, and at the same time generalize existing termination conditions for LD-resolution. Of course, this choice also makes the results more technically involved.

The rest of the article is structured as follows. In Section 2, we define some preliminary concepts, in particular the notion of finitely partitioning level mapping, which plays a central role in our termination conditions. Next, in Section 3, we recall the execution mechanism of LG-resolution, the tabled-based resolution strategy used in this article. We first present examples from context-free grammar recognition and parsing which motivate the need to freely mix untabled and tabled execution and then we formally define the resolution principle of tabling, called SLG-resolution. Next, in Section 4, two notions of termination of LG-resolution are introduced: quasi-termination and the stronger notion of LG-termination. We also define a transformation on programs which reduces the problem of proving LG-termination to the problem of proving quasi-termination. In Section 5, sufficient (and also necessary in case the tabling is well-chosen) conditions for the two notions of termination are given: the condition of quasi-acceptability for quasi-termination (Subsection 5.1) and the condition of LG-acceptability for LG-termination (Subsection 5.2). Modular termination conditions, i.e., conditions that are capable of combining termination proofs of separate programs to obtain termination proofs of combined programs, are given in Section 6: in Subsection 6.1 for quasi-termination, and in Subsection 6.2 for LG-termination. In Subsection 6.3, more detailed modular termination conditions for quasi-termination are given, which also provide an incremental construction of an appropriate level mapping. Finally, in Section 7, we investigate conditions for termination of LG-resolution which are easy to automate. In particular, our eventual goal is to extend the constraint-based automatic approach towards LD-termination of , in order to prove termination of tabled logic programs in an automatic way. Our extension is restricted to the class of simply moded, well-moded programs and queries, which we recall from . Only quasi-termination is considered in Section 7; the results for LG-termination carry over in the same way. We end with some concluding remarks, a discussion on related work and with some topics for future research.

## 2 Preliminaries

We assume familiarity with the basic concepts of logic programming; see . Throughout the article, $`P`$ will denote a definite logic program. By $`Pred_P`$, $`Fun_P`$ and $`Const_P`$ we denote the set of predicate, function and constant symbols occurring in $`P`$. We assume that these sets are finite. By $`Def_P`$ we denote the set of predicates defined in $`P`$ (i.e., predicates occurring in the head of a clause of $`P`$). By $`Rec_P`$, resp. $`NRec_P`$, we denote the set of (directly or indirectly) recursive, resp. non-recursive, predicates of the program $`P`$ (so $`NRec_P=Pred_PRec_P`$). If $`A=p(t_1,\mathrm{},t_n)`$, then we denote by $`Rel(A)`$ the predicate symbol $`p`$ of $`A`$; i.e., $`Rel(A)=p`$. We call $`A=p(t_1,\mathrm{},t_n)`$ a $`p`$-atom.

The extended Herbrand Universe, $`U_P^E`$, and the extended Herbrand Base, $`B_P^E`$, associated with a program $`P`$, were introduced in . They are defined as follows. Let $`Term_P`$ and $`Atom_P`$ denote the set of respectively all terms and atoms that can be constructed from the alphabet underlying $`P`$. The variant relation, denoted $``$, defines an equivalence. $`U_P^E`$ and $`B_P^E`$ are respectively the quotient sets $`Term_P/`$ and $`Atom_P/`$. For any term $`t`$ (or atom $`A`$), we denote its class in $`U_P^E`$ ($`B_P^E`$) as $`\stackrel{~}{t}`$ ($`\stackrel{~}{A}`$). However, when no confusion is possible, we omit the tildes. For $`\mathrm{\Pi }Pred_P`$, we denote with $`B_\mathrm{\Pi }^E`$ the subset of $`B_P^E`$ consisting of (equivalence classes of) atoms based on the predicate symbols of $`\mathrm{\Pi }`$. So $`B_P^E`$ can be seen as an abbreviation of $`B_{Pred_P}^E`$.

Let $`P`$ be a program and $`p,qPred_P`$. We say that $`p`$ refers to $`q`$ in $`P`$ iff there is a clause in $`P`$ with $`p`$ in the head and $`q`$ occurring in the body. We say that $`p`$ depends on $`q`$ in $`P`$, and write $`pq`$, iff $`(p,q)`$ is in the reflexive, transitive closure of the relation refers to. Note that, by definition, each predicate depends on itself. We write $`pq`$ iff $`pq`$, $`qp`$ ($`p`$ and $`q`$ are mutually recursive or $`p=q`$). The dependency graph $`G_P`$ of a program $`P`$ is a graph where the nodes are labeled with the predicates of $`Pred_P`$. There is a directed arc from $`p`$ to $`q`$ in $`G_P`$ iff $`p`$ refers to $`q`$. A program $`P`$ extends a program $`R`$ iff no predicate defined in $`P`$ occurs in $`R`$.

As mentioned and used in the introduction, in analogy with , we will refer to SLD-derivations (see ) following the left-to-right selection rule as LD-derivations. Other concepts adopt this naming accordingly.

###### Definition 1 (call set associated to $`S`$)

Let $`P`$ be a program and $`SB_P^E`$. By $`Call(P,S)`$ we denote the subset of $`B_P^E`$ such that $`BCall(P,S)`$ whenever a representant of $`B`$ is a selected atom in an LD-derivation for some $`P\{A\}`$, with $`\stackrel{~}{A}S`$.

Throughout the article we assume that in any derivation of a query w.r.t. a program, representants of equivalence classes are systematically provided with fresh variables, to avoid the necessity of renaming apart. In the sequel, we abbreviate most general unifier with $`mgu`$ and LD-computed answer substitution with $`cas`$.

The concepts defined in the following Definitions 2, 3 and 4, will be used in the proofs of some theorems and propositions of this article.

###### Definition 2 (direct descendant)

Let $`P`$ be a program and $`\stackrel{~}{A},\stackrel{~}{B}B_P^E`$. We call $`\stackrel{~}{B}`$ a direct descendant of $`\stackrel{~}{A}`$ iff there exists a clause $`HB_1,\mathrm{},B_n`$ in $`P`$ such that $`mgu(A,H)=\theta `$ exists and, there is an $`i[1,n]`$ such that there is an LD-refutation for $`(B_1,\mathrm{},B_{i1})\theta `$ with $`cas`$ $`\theta _{i1}`$ and $`BB_i\theta \theta _{i1}`$.

###### Definition 3 (directed subsequence of an LD-derivation)

Let $`P`$ be a program and $`\stackrel{~}{A}B_P^E`$. Let $`A=G_0,`$ $`G_1,\mathrm{}`$ be an LD-derivation of $`A`$ in $`P`$. A subsequence $`G_{i_0},G_{i_1},\mathrm{}`$, with $`G_{i_j}=A_{i_j},𝒜_{i_j}`$, is called a directed subsequence iff for all $`j0`$, $`\stackrel{~}{A}_{i_{j+1}}`$ is a direct descendant of $`\stackrel{~}{A}_{i_j}`$ in the LD-derivation.

###### Definition 4 (call graph associated to $`S`$)

Let $`P`$ be a program and $`SB_P^E`$. The call graph $`Call`$-$`Gr(P,S)`$ associated to $`P`$ and $`S`$ is a graph such that:

* its set of nodes is $`Call(P,S)`$,
* there exists a directed arc from $`\stackrel{~}{A}`$ to $`\stackrel{~}{B}`$ iff $`\stackrel{~}{B}`$ is a direct descendant of $`\stackrel{~}{A}`$.

We recall the definitions of norm and level mapping, which are useful in the context of termination analysis (see  for a survey on termination analyses for (S)LD-resolution).

###### Definition 5 (norm)

A norm is a function $`.:U_P^E\mathrm{}`$.

###### Definition 6 (level mapping)

A level mapping is a function $`|.|:B_P^E\mathrm{}`$.

A level mapping or norm is said to be trivial if it is the constant $`0`$-mapping.

Our termination conditions are based on the following concept of a finitely partitioning level mapping.

###### Definition 7 (finitely partitioning level mapping)

Let $`P`$ be a program and $`CB_P^E`$. A level mapping $`|.|`$ is finitely partitioning on $`C`$ iff for all $`n\mathrm{}:\mathrm{}(|.|^1(n)C)<\mathrm{}`$, where $`\mathrm{}`$ is the cardinality function.

So, a level mapping $`|.|`$ is finitely partitioning on $`CB_P^E`$ if it does not map an infinite set of atoms of $`C`$ to the same natural number. That is, $`|.|`$ partitions $`C`$ into finite subsets. In particular, we have that every level mapping is finitely partitioning on a finite set $`C`$.

## 3 Tabling in Logic Programs

Our experience is that tabled execution is used *selectively* in practice. Thus, before formally defining the resolution principle of tabling, called SLG-resolution, we first present some examples which motivate the need to freely mix LD-resolution and tabled execution.

### 3.1 Mixing Tabled and LD Execution: Motivating Examples

It has long been noted in the literature , that tabled evaluation can be used for context-free grammar recognition and parsing: tabling eliminates redundancy and handles grammars that would otherwise infinitely loop under Prolog-style execution (e.g. left-recursive ones). The following program, where all predicates are tabled, provides such an example.

$$\{\begin{array}{ccc}expr(Si,So)\hfill & \hfill & expr(Si,S1),S1=[^{}+^{}|S2],term(S2,So)\hfill \\ expr(Si,So)\hfill & \hfill & term(Si,So)\hfill \\ term(Si,So)\hfill & \hfill & term(Si,S1),S1=[^{}^{}|S2],primary(S2,So)\hfill \\ term(Si,So)\hfill & \hfill & primary(Si,So)\hfill \\ primary(Si,So)\hfill & \hfill & Si=[^{}(^{}|S1],expr(S1,S2),S2=[^{})^{}|So]\hfill \\ primary(Si,So)\hfill & \hfill & Si=[I|So],integer(I)\hfill \end{array}$$

This grammar, recognizing arithmetic expressions containing additions and multiplications over the integers, is left recursive—left recursion is used to give the arithmetic operators their proper associativity—and would be non-terminating for Prolog-style execution. Under tabled execution, left recursion is handled correctly. In fact, one only needs to table predicates $`expr/2`$ and $`term/2`$ to get the desired termination behaviour; we can and will safely drop the tabling of $`primary/2`$ in the sequel. However, this integration of non-tabled (LD) and tabled execution is perhaps a trivial one.

To see why a non-trivial mix of tabled with LD execution is desirable in practice, suppose that we want to extend the above recognition grammar to handle exponentiation. The most natural way to do so is to introduce a new nonterminal, named $`factor`$, for handling exponentiation and make it right recursive, since the exponentiation operator is right associative. The resulting grammar is as below where only the predicates $`expr/2`$ and $`term/2`$ are tabled.

$$\{\begin{array}{ccc}expr(Si,So)\hfill & \hfill & expr(Si,S1),S1=[^{}+^{}|S2],term(S2,So)\hfill \\ expr(Si,So)\hfill & \hfill & term(Si,So)\hfill \\ term(Si,So)\hfill & \hfill & term(Si,S1),S1=[^{}^{}|S2],factor(S2,So)\hfill \\ term(Si,So)\hfill & \hfill & factor(Si,So)\hfill \\ factor(Si,So)\hfill & \hfill & primary(Si,S1),S1=[^{}^{}|S2],factor(S2,So)\hfill \\ factor(Si,So)\hfill & \hfill & primary(Si,So)\hfill \\ primary(Si,So)\hfill & \hfill & Si=[^{}(^{}|S1],expr(S1,S2),S2=[^{})^{}|So]\hfill \\ primary(Si,So)\hfill & \hfill & Si=[I|So],integer(I)\hfill \end{array}$$

Note that, at least as far as termination is concerned, there is no need to table the new nonterminal $`factor`$. Indeed, Prolog’s evaluation strategy handles right recursion in grammars finitely. In fact, Prolog-style evaluation of right recursion is more efficient than its tabled-based evaluation: Prolog has linear complexity for a simple right recursive grammar, but with tabling implemented as in XSB the evaluation could be quadratic as calls need to be recorded in the tables using explicit copying. Thus, it is important to allow tabled and non-tabled predicates to be freely intermixed, and be able to choose the strategy that is most efficient for the situation at hand.

By using tabling in context-free grammars, one gets a recognition algorithm that is a variant of Early’s algorithm (also known as active chart recognition algorithm) whose complexity is polynomial in the size of the input expression/string . However, often one wants to construct the parse tree(s) for a given input string. The usual approach is to introduce an extra argument to the nonterminals of the input grammar—representing the portion of the parse tree that each rule generates—and naturally to also add the necessary code that constructs the parse tree. This approach is straightforward, but as noticed by Warren in , using the same program for recognition as well as parsing may be extremely unsatisfactory from a complexity standpoint: in context-free grammars, recognition is polynomial while parsing is exponential, since there can be exponentially many parse trees for a given input string. The obvious solution is to use two interleaved versions of the grammar as in the following program, which recognizes and parses the language $`a^nb`$.

$$\begin{array}{cc}R:\hfill & \{\begin{array}{ccc}s(Si,So)\hfill & \hfill & a(Si,S),S=[b|So]\hfill \\ a(Si,So)\hfill & \hfill & a(Si,S),a(S,So)\hfill \\ a(Si,So)\hfill & \hfill & Si=[a|So]\hfill \end{array}\hfill \\ & \\ P:\hfill & \{\begin{array}{ccc}s(Si,So,PT)\hfill & \hfill & a(Si,S),S=[b|So],PT=spt(PTa,b),a(Si,S,PTa)\hfill \\ a(Si,So,PT)\hfill & \hfill & a(Si,S),a(S,So),PT=apt(PT1,PT2),a(Si,S,PT1),\hfill \\ & & a(S,So,PT2)\hfill \\ a(Si,So,PT)\hfill & \hfill & Si=[a|So],PT=a\hfill \end{array}\hfill \end{array}$$

Note that only $`a/2`$, i.e., the recursive predicate of the ‘recognition’ part, $`R`$, of the program (consisting of predicates $`s/2`$ and $`a/2`$), needs to be tabled. This action allows recognition to terminate and to have polynomial complexity. Furthermore, the recognizer can now be used as a filter for the parsing process in the following way: only after knowing that a particular part of the input belongs to the grammar and having computed the exact substring that each nonterminal spans, do we invoke the parsing routine on the nonterminal to construct its (possibly exponentially many) parse trees. Doing so, avoids e.g. cases where it may take exponential time to fail on an input string that does not belong in the given language: an example for the grammar under consideration is the input string $`a^n`$. On the other hand, tabling the ‘parsing’ part of the program (consisting of predicates $`s/3`$ and $`a/3`$) does not affect the efficiency of the process complexity-wise and incurs a small performance overhead due to the recording of calls and their answers in the tables. Finally, note that the construction is modular in the sense that the ‘parsing’ part of the program, $`P`$, depends on the ‘recognition’ part, $`R`$, but not vice versa; we say that $`P`$ *extends* $`R`$.

### 3.2 SLG-Resolution

In this article, we consider termination of SLG-resolution (see ), using a fixed left-to-right selection rule, for a given set of atomic (top level) queries with atoms in $`SB_P^E`$. We will abbreviate SLG-resolution under the left-to-right selection rule by LG-resolution. For definite programs LG-resolution is similar to OLDT-resolution , modulo the fact that OLDT specifies a more fixed control strategy and uses subsumption checking and term-depth abstraction instead of variant checking. We present a non-constructive definition of SLG-resolution that is sufficient for our purposes, and refer to  for more constructive formulations of (variants) of tabled resolution.

By fixing a tabling for a program $`P`$, we mean choosing a set of predicates of $`P`$ which are tabled. The set of tabled predicates for a given tabling of a program $`P`$ is denoted with $`Tab_P`$. The complement of this set is denoted with $`NTab_P=Pred_PTab_P`$.

###### Definition 8 (pseudo SLG-tree, pseudo LG-tree)

Let $`P`$ be a definite program, $`Tab_PPred_P`$, $``$ a selection rule and $`A`$ an atom. A pseudo SLG-tree w.r.t. $`Tab_P`$ for $`P\{A\}`$ under $``$ is a tree $`\tau _A`$ such that:

1. the nodes of $`\tau _A`$ are labeled with queries along with an indication of the selected atom according to $``$,
2. the root of $`\tau _A`$ is $`A`$,
3. the children of the root $`A`$ are obtained by resolution against all matching program clauses in $`P`$, the arcs are labeled with the corresponding $`mgu`$ used in the resolution step,
4. the children of a non-root node labeled with the query $`𝐐`$ where $`(𝐐)=B`$ are obtained as follows:

1. if $`Rel(B)Tab_P`$, then

    the (possibly infinitely many) children of the node can only be obtained by resolving the selected atom $`B`$ of the node with clauses of the form $`B\theta `$ (not necessarily in $`P`$), the arcs are labeled with the corresponding $`mgu`$ used in the resolution step (i.e., $`\theta `$),
    2. if $`Rel(B)NTab_P`$, then

    the children of the node are obtained by resolution of $`B`$ against all matching program clauses in $`P`$, and the arcs are labeled with the corresponding $`mgu`$ used in the resolution step.

If $``$ is the leftmost selection rule, $`\tau _A`$ is called a pseudo LG-tree w.r.t. $`Tab_P`$ for $`P\{A\}`$.
We say that a pseudo SLG-tree $`\tau _A`$ w.r.t. $`Tab_P`$ for $`P\{A\}`$ is smaller than another pseudo SLG-tree $`\tau _A^{^{}}`$ w.r.t. $`Tab_P`$ for $`P\{A\}`$ iff $`\tau _A^{^{}}`$ can be obtained from $`\tau _A`$ by attaching new sub-branches to nodes in $`\tau _A`$.
A (computed) answer clause of a pseudo SLG-tree $`\tau _A`$ w.r.t. $`Tab_P`$ for $`P\{A\}`$ is a clause of the form $`A\theta `$ where $`\theta `$ is the composition of the substitutions found on a branch of $`\tau _A`$ whose leaf is labeled with the empty query.

Intuitively, a pseudo SLG-tree (in an SLG-forest, see Definition 9 below) represents the tabled computation (w.r.t. $`Tab_P`$) of all answers for a given subquery labeling the root node of the tree. The trees in the above definition are called pseudo SLG-trees because there is no condition yet on which clauses $`B\theta `$ exactly are to be used for resolution in point 4a. These clauses represent the answers found (possibly in another tree of the forest) for the selected tabled atom. This interaction between the trees in an SLG-forest is captured in the following definition.

###### Definition 9 (SLG-forest, LG-forest)

Let $`P`$ be a definite program, $`Tab_PPred_P`$, $``$ be a selection rule and $`T`$ be a (possibly infinite) set of atoms such that no two different atoms in $`T`$ are variants of each other. $``$ is an SLG-forest w.r.t. $`Tab_P`$ for $`P`$ and $`T`$ under $``$ iff $``$ is a set of minimal pseudo SLG-trees $`\{\tau _A|AT\}`$ w.r.t. $`Tab_P`$ where

1. $`\tau _A`$ is a pseudo SLG-tree w.r.t. $`Tab_P`$ for $`P\{A\}`$ under $``$,
2. every selected tabled atom $`B`$ of each node in every $`\tau _A`$ is a variant of an element $`B^{^{}}`$ of $`T`$, such that every clause resolved with $`B`$ is a variant of an answer clause of $`\tau _B^{^{}}`$ and vice versa, for every answer clause of $`\tau _B^{^{}}`$ there is a variant of this answer clause which is resolved with $`B`$.

Let $`S`$ be a set of atoms. An SLG-forest for $`P`$ and $`S`$ w.r.t. $`Tab_P`$ under $``$ is an SLG-forest w.r.t. $`Tab_P`$ for a minimal set $`T`$ with $`\stackrel{~}{S}\stackrel{~}{T}`$. If $`S=\{A\}`$, then we also talk about the SLG-forest for $`P\{A\}`$.
An LG-forest is an SLG-forest containing only pseudo LG-trees.

Point 2 of Definition 9, together with the imposed minimality of trees in a forest, now uniquely determines these trees. So we can henceforth drop the designation “pseudo” and refer to (S)LG-trees in an (S)LG-forest.

Note that, selected atoms which are not tabled (i.e., of predicates belonging to $`NTab_P`$) are resolved against program clauses, as in (S)LD-resolution. So, if $`Tab_P=\mathrm{}`$, the (S)LG-forest of $`P\{A\}`$ consists of one tree: the (S)LD-tree of $`P\{A\}`$.

We use the following small, tabled program to illustrate the notions that we introduced so far. Variations of it will also be used throughout this article to exemplify concepts related to the termination aspects of tabled logic programs.

###### Example 1

The following program $`P`$ computes the paths from a given node to the reachable nodes in a given graph. The graph is represented as a list of terms $`e(n_1,n_2)`$, indicating that there is an edge from node $`n_1`$ to node $`n_2`$; this list is passed as an input argument to predicate $`\mathrm{𝑝𝑎𝑡ℎ}/4`$ and the predicate $`\mathrm{𝑒𝑑𝑔𝑒}/3`$ is used to retrieve edges of the graph with a specific source node.

$$\{\begin{array}{ccc}path(X,Ed,Y,[Y])\hfill & \hfill & edge(X,Ed,Y)\hfill \\ path(X,Ed,Z,[Y|L])\hfill & \hfill & edge(X,Ed,Y),path(Y,Ed,Z,L)\hfill \\ edge(X,[e(X,Y)|L],Y)\hfill & \hfill & \\ edge(X,[e(X_1,X_2)|L],Y)\hfill & \hfill & edge(X,L,Y)\hfill \end{array}$$

Let $`S=\{\mathrm{𝑝𝑎𝑡ℎ}(a,[e(a,b),e(b,a)],Y,L)\}`$ and $`Tab_P=\{\mathrm{𝑝𝑎𝑡ℎ}/4\}`$. Then,

$`Call(P,S)`$ $`=`$ $`\{\mathrm{𝑝𝑎𝑡ℎ}(a,[e(a,b),e(b,a)],Y,L),\mathrm{𝑝𝑎𝑡ℎ}(b,[e(a,b),e(b,a)],Y,L),`$
$`\mathrm{𝑒𝑑𝑔𝑒}(a,[e(a,b),e(b,a)],Y),\mathrm{𝑒𝑑𝑔𝑒}(a,[e(b,a)],Y),\mathrm{𝑒𝑑𝑔𝑒}(a,[],Y),`$
$`\mathrm{𝑒𝑑𝑔𝑒}(b,[e(a,b),e(b,a)],Y),\mathrm{𝑒𝑑𝑔𝑒}(b,[e(b,a)],Y),\mathrm{𝑒𝑑𝑔𝑒}(b,[],Y)\}`$
The LG-forest w.r.t. $`Tab_P`$ for $`P`$ and $`S`$ is shown in Figure 1. Note that there are two LG-trees (only 2 tabled atoms are called), both with finite branches, but both trees have an infinitely branching node. Due to the last argument of the $`\mathrm{𝑝𝑎𝑡ℎ}/4`$ predicate, each of these selected tabled atoms has infinitely many computed answers.

As proven in e.g. \[20, Theorem 2.1\], the set of call patterns and the set of computed answer substitutions are not influenced by tabling. Thus, we can use the notions of call set, $`Call(P,S)`$, and LD-computed answer substitution, $`cas`$, even in the context of SLG-resolution.

The notion of a call graph (Definition 4) has the following particularly interesting property, described in the proposition below, which is useful in the study of termination. We will use this property in the proof of Theorem 5.1, which gives a necessary and sufficient condition for quasi-termination of tabled logic programs.

###### Proposition 1 (call graph: paths and selected atoms)

Let $`P`$ be a program, $`Tab_PPred_P`$ and $`SB_P^E`$. Let $`p`$ be any directed path in $`Call`$-$`Gr(P,S)`$. Then there exists an LG-derivation for some element of $`Call(P,S)`$, such that all the nodes in $`p`$ occur as selected atoms in the derivation.

###### Proof

By definition of $`Call`$-$`Gr(P,S)`$, for every arc from $`\stackrel{~}{A}`$ to $`\stackrel{~}{B}`$ in $`Call`$-$`Gr(P,S)`$, there exists a sequence of consecutive LG-derivation steps, starting from $`A`$ and having a variant of $`B`$ as its selected atom at the end. Because (a variant of) $`B`$ is selected at the end-point, any two such derivation-step sequences, corresponding to consecutive arcs in $`Call`$-$`Gr(P,S)`$, can be composed to form a new sequence of LG-derivation steps. In this sequence, all 3 nodes of the consecutive arcs remain selected atoms in the new sequence of derivation steps. Transitively exploiting the above argument yields the result. ∎

## 4 Two Notions of Termination of Tabled Logic Programs

We start by introducing a first notion of universal termination of tabled logic programs, called *quasi-termination*. A program $`P`$ with a tabling $`Tab_P`$ is said to be quasi-terminating w.r.t. a query $`A`$ iff the LG-forest of $`P\{A\}`$ consists of a finite number of LG-trees which all have finite branches. Quasi-termination captures the property that, under LD-computation, a given atomic query leads to only finitely many different (nonvariant) calls to tabled predicates and there is no infinite derivation consisting of queries with only selected non-tabled atoms. As mentioned in the introduction, techniques for proving quasi-termination can be used to ensure termination of off-line specialisation of logic programs (whether tabled or not). Currently, in all off-line partial evaluation methods for logic programs (e.g. ) termination has to be ensured manually. In the context of off-line partial evaluation, quasi-termination (when tabling the whole set of predicates) is actually *identical* to termination of the partial evaluator; see e.g. the discussion in . Thus, given a technique to establish quasi-termination, one can also establish whether a given binding time annotation will ensure termination or whether further abstraction is called for. This idea has already been successfully applied in the context of functional programming , using the termination criterion of .

Despite its usefulness, the notion of quasi-termination only partially corresponds to our intuitive notion of a terminating execution of a query against a tabled program. This is because this notion only requires that the LG-forest consists of only a finite number of LG-trees, without infinite branches, yet these trees can have infinitely branching nodes. In order to capture this source of non-termination for a tabled computation, we also introduce the stronger notion of LG-termination. A program $`P`$ with a tabling $`Tab_P`$ is said to be LG-terminating w.r.t. a query $`A`$ iff the LG-forest of $`P\{A\}`$ consists of a finite number of finite LG-trees. So, a program $`P`$ is LG-terminating w.r.t. a query $`A`$ iff it is quasi-terminating w.r.t. $`A`$ and all atoms in the call set $`Call(P,\{A\})`$ have only a finite number of computed answers.

In the next two subsections, we formally introduce these two notions of termination of LG-resolution with Tabling, we give examples and discuss some of their properties.

### 4.1 Quasi-Termination

A first basic notion of universal termination under a tabled execution mechanism is quasi-termination (a term borrowed from , defining a similar notion in the context of termination of off-line partial evaluation of functional programs). It is formally defined as follows.

###### Definition 10 (quasi-termination)

Let $`P`$ be a program, $`Tab_PPred_P`$, and $`SB_P^E`$.
$`P`$ quasi-terminates w.r.t. $`Tab_P`$ and $`S`$ iff for all $`A`$ such that $`\stackrel{~}{A}S`$, the LG-forest w.r.t. $`Tab_P`$ for $`P\{A\}`$ consists of a finite number of LG-trees without infinite branches.
Also, $`P`$ quasi-terminates w.r.t. $`S`$ iff $`P`$ quasi-terminates w.r.t. $`Pred_P`$ and $`S`$.

Note that quasi-termination does not require that the LG-trees are finitely branching in their nodes.

###### Example 2

Recall the program $`P`$ and set $`S=\{\mathrm{𝑝𝑎𝑡ℎ}(a,[e(a,b),e(b,a)],Y,L)\}`$ of Example 1. The LG-forest w.r.t. $`Tab_P=\{\mathrm{𝑝𝑎𝑡ℎ}/4\}`$ was shown in Figure 1. $`P`$ quasi-terminates w.r.t. $`Tab_P`$ and $`S`$.

Many works address the problem of termination of logic programs executed under LD-resolution (see  for a survey): A program $`P`$ is said to be LD-terminating w.r.t. a set $`SB_P^E`$ iff for all $`A`$ such that $`\stackrel{~}{A}S`$, the LD-tree of $`P\{A\}`$ is finite. In the next lemma, we show that the notion of LD-termination is stronger than the notion of quasi-termination. Taking Example 2 into account, it then follows that the notion of LD-termination is strictly stronger than the notion of quasi-termination.

###### Lemma 1

Let $`P`$ be a program, $`Tab_PPred_P`$, and $`SB_P^E`$.
If $`P`$ LD-terminates w.r.t. $`S`$, then $`P`$ quasi-terminates w.r.t. $`Tab_P`$ and $`S`$.

###### Proof

Let $`A`$ be an atom such that $`\stackrel{~}{A}S`$. Let $``$ be the LG-forest w.r.t. $`Tab_P`$ for $`P\{A\}`$. If $`P`$ LD-terminates w.r.t. $`S`$, it is easy to see that $`Call(P,\{A\})`$ is finite. Hence, $`Call(P,\{A\})B_{Tab_P}^E`$ is finite, and $``$ consists of a finite number of LG-trees.

Now we prove that no tree in $``$ has an infinite branch. Suppose this is not the case and there is a tree in $``$ with an infinite branch. Let $`H`$ be the leftmost atom of a query labeling a node in this infinite branch. Then, $`H`$ has an infinite LD-derivation (just plug in, for each tabled atom $`G`$ in the infinite branch which is resolved with an answer, the branch of the tree with root $`G`$ which leads to this answer). This gives a contradiction. ∎

Note that by definition, $`P`$ quasi-terminates w.r.t. $`Tab_P=\mathrm{}`$ and $`S`$ iff $`P`$ LD-terminates w.r.t. $`S`$.

Consider next the special case where all predicates occurring in $`P`$ are tabled. If $`Tab_P=Pred_P`$, an LG-tree cannot have infinite branches. So, $`P`$ quasi-terminates w.r.t. a set $`S`$ iff for all $`A`$ such that $`\stackrel{~}{A}S`$, the LG-forest for $`P\{A\}`$ consists of a finite number of LG-trees. The following equivalence holds.

###### Lemma 2

Let $`P`$ be a program, $`Tab_P=Pred_P`$, and $`SB_P^E`$. SW
$`P`$ quasi-terminates w.r.t. $`S`$ iff for all $`A`$ such that $`\stackrel{~}{A}S`$, $`Call(P,\{A\})`$ is finite.

###### Proof

Since $`Tab_P=Pred_P`$, an LG-tree cannot have infinite branches. The equivalence then follows from the fact that for every $`A`$ such that $`\stackrel{~}{A}S`$, $`B`$ is the root of an LG-tree in the LG-forest of $`P\{A\}`$ iff $`\stackrel{~}{B}Call(P,\{A\})`$. ∎

When all predicates are tabled, from the above lemma, it follows that in case the Herbrand Universe $`U_P^E`$ associated to a program $`P`$ is finite, $`P`$ quasi-terminates w.r.t. any set of queries $`S`$.

Lemma 2 does not hold in case that the tabled predicates of a program are a strict subset of the set of predicates occurring in the program. A counterexample for the if-direction is given by the program $`P=\{pq,qp\}`$, the set $`S=\{p\}`$ and the empty set of tabled predicates, $`Tab_P=\mathrm{}`$. The LG-forest consists of one tree, namely the LD-tree of $`P\{p\}`$ (so quasi-termination is the same as LD-termination). $`P`$ does not quasi-terminate w.r.t. $`Tab_P`$ and $`S`$, whereas $`Call(P,\{p\})=\{p,q\}`$ is a finite set. Also the only-if direction of Lemma 2 does not hold in case $`Tab_PPred_P`$. We provide a counterexample.

###### Example 3

Consider the following program $`P`$:

$$\{\begin{array}{ccc}p(a)\hfill & \hfill & \\ p(f(X))\hfill & \hfill & p(X),q(X)\hfill \\ q(X)\hfill & \hfill & \end{array}$$

with set of tabled predicates $`Tab_P=\{p/1\}`$ and $`S=\{p(X)\}`$. The LG-forest is shown in Figure 2.

$`P`$ quasi-terminates w.r.t. $`Tab_P`$ and $`S`$. There is only one LG-tree in the LG-forest for $`P\{p(X)\}`$ without infinite branches. Note that the LG-tree has an infinitely branching node. But the call set $`Call(P,\{p(X)\})=\{p(X),q(a),\mathrm{},q(f^n(a)),\mathrm{}\}`$ is infinite.

Note however that, since quasi-termination requires that there are only finitely many LG-trees in the LG-forest of a query, there can only be a finite number of tabled atoms in the call set of that query. Hence, in general, the following holds.

###### Lemma 3

Let $`P`$ be a program, $`Tab_PPred_P`$ and $`SB_P^E`$.
If $`P`$ quasi-terminates w.r.t. $`Tab_P`$ and $`S`$, then, for all $`A`$ such that $`\stackrel{~}{A}S`$, $`Call(P,\{A\})B_{Tab_P}^E`$ is finite.

###### Proof

The implication follows from the fact that for every $`A`$ such that $`\stackrel{~}{A}S`$, $`B`$ is the root of an LG-tree in the LG-forest w.r.t. $`Tab_P`$ of $`P\{A\}`$ iff $`\stackrel{~}{B}(Call(P,\{A\})B_{Tab_P}^E)\{\stackrel{~}{A}\}`$. ∎

###### Example 4

Recall program $`P`$ and set $`S`$ of Example 3. We already know that $`P`$ quasi-terminates w.r.t. $`Tab_P=\{p/1\}`$ and $`S`$. Indeed, $`Call(P,\{p(X)\})B_{Tab_P}^E=\{p(X)\}`$ is finite.

### 4.2 LG-Termination

As already noted, the notion of quasi-termination only partially corresponds to our intuitive notion of a terminating execution of a query against a tabled program. Therefore, the following stronger notion of LG-termination is introduced.

###### Definition 11 (LG-termination)

Let $`P`$ be a program, $`Tab_PPred_P`$ and $`SB_P^E`$.
$`P`$ LG-terminates w.r.t. $`Tab_P`$ and $`S`$ iff for every atom $`A`$ such that $`\stackrel{~}{A}S`$, the LG-forest w.r.t. $`Tab_P`$ for $`P\{A\}`$ consists of a finite number of finite LG-trees.
Also, $`P`$ LG-terminates w.r.t. $`S`$ iff $`P`$ LG-terminates w.r.t. $`Pred_P`$ and $`S`$.

Note that by definition, $`P`$ LG-terminates w.r.t. $`Tab_P=\mathrm{}`$ and $`S`$ iff $`P`$ LD-terminates w.r.t. $`S`$.

Recall the program $`P`$ and set $`S`$ of Example 1. The LG-forest of $`P`$ and $`S`$ w.r.t. $`Tab_P=\{\mathrm{𝑝𝑎𝑡ℎ}/4\}`$ was shown in Figure 1. Note that there are infinitely branching nodes in the LG-trees. Hence, $`P`$ does not LG-terminate w.r.t. $`Tab_P`$ and $`S`$.

Observe that if the program $`P`$ is called with an acyclic graph as input, we have LG-termination and even LD-termination. The program $`P^{^{}}`$ of the following example is obtained from $`P`$ by removing the last argument of the $`\mathrm{𝑝𝑎𝑡ℎ}/4`$ predicate in which the path is computed; the resulting predicate is named $`\mathrm{𝑟𝑒𝑎𝑐ℎ𝑎𝑏𝑙𝑒}/3`$. When $`P^{^{}}`$ is called with a cyclic graph as input, we have LG-termination (but no LD-termination).

###### Example 5

The following program $`P^{^{}}`$ computes the reachable nodes from a given node in a given graph. As in Example 1, the graph is represented as a list of terms $`e(n_1,n_2)`$, indicating that there is an edge from node $`n_1`$ to node $`n_2`$. Note that, contrary to program $`P`$ of Example 1, $`P^{^{}}`$ does not compute the paths leading from the given node to the reachable nodes.

$$\{\begin{array}{ccc}reachable(X,Ed,Y)\hfill & \hfill & edge(X,Ed,Y)\hfill \\ reachable(X,Ed,Z)\hfill & \hfill & edge(X,Ed,Y),reachable(Y,Ed,Z)\hfill \\ edge(X,[e(X,Y)|L],Y)\hfill & \hfill & \\ edge(X,[e(X_1,X_2)|L],Y)\hfill & \hfill & edge(X,L,Y)\hfill \end{array}$$

Let $`S^{^{}}=\{\mathrm{𝑟𝑒𝑎𝑐ℎ𝑎𝑏𝑙𝑒}(a,[e(a,b),e(b,a)],Y)\}`$ and $`Tab_P^{^{}}=\{\mathrm{𝑟𝑒𝑎𝑐ℎ𝑎𝑏𝑙𝑒}/3\}`$.. Then,

$`Call(P^{^{}},S^{^{}})`$ $`=`$ $`\{\mathrm{𝑟𝑒𝑎𝑐ℎ𝑎𝑏𝑙𝑒}(a,[e(a,b),e(b,a)],Y),\mathrm{𝑟𝑒𝑎𝑐ℎ𝑎𝑏𝑙𝑒}(b,[e(a,b),e(b,a)],Y),`$
$`\mathrm{𝑒𝑑𝑔𝑒}(a,[e(a,b),e(b,a)],Y),\mathrm{𝑒𝑑𝑔𝑒}(a,[e(b,a)],Y),\mathrm{𝑒𝑑𝑔𝑒}(a,[],Y),`$
$`\mathrm{𝑒𝑑𝑔𝑒}(b,[e(a,b),e(b,a)],Y),\mathrm{𝑒𝑑𝑔𝑒}(b,[e(b,a)],Y),\mathrm{𝑒𝑑𝑔𝑒}(b,[],Y)\}`$
The LG-forest w.r.t. $`Tab_P^{^{}}`$ for $`P^{^{}}`$ and $`S^{^{}}`$ is shown in Figure 3. Note that there are 2 LG-trees (only 2 tabled atoms are called), both with finite branches and finitely branching nodes (the selected tabled atoms have a finite number of computed answers). $`P^{^{}}`$ LG-terminates w.r.t. $`Tab_P^{^{}}`$ and $`S^{^{}}`$. Observe that $`P^{^{}}`$ does not LD-terminate w.r.t. $`S^{^{}}`$.

As illustrated by the above examples, the notion of LG-termination is strictly stronger than the notion of quasi-termination. Also, LD-termination implies (and is strictly stronger than) LG-termination.

###### Lemma 4

Let $`P`$ be a program, $`Tab_PPred_P`$, and $`SB_P^E`$.
If $`P`$ LG-terminates w.r.t. $`Tab_P`$ and $`S`$, then $`P`$ quasi-terminates w.r.t. $`Tab_P`$ and $`S`$.
If $`P`$ LD-terminates w.r.t. $`S`$, then $`P`$ LG-terminates w.r.t. $`Tab_P`$ and $`S`$.

###### Proof

The first statement is trivial by definition. For the second statement, this is a corollary of the following Proposition 2 with $`Tab_1=\mathrm{}`$ and $`Tab_2=Tab_P`$. ∎

Note that, if a program quasi-terminates w.r.t. a tabling and a set $`S`$ and the program does not LG-terminate w.r.t. that tabling and $`S`$, then there does not exist a tabling such that the program LG-terminates w.r.t. that tabling and $`S`$. This is proven in the following lemma.

###### Lemma 5

Let $`P`$ be a program and $`SB_P^E`$ a set of queries. Suppose there exists a tabling $`Tab_P^{}Pred_P`$ such that $`P`$ quasi-terminates w.r.t. $`Tab_P^{}`$ and $`S`$ and $`P`$ does not LG-terminate w.r.t. $`Tab_P^{}`$ and $`S`$.
Then for all tablings $`Tab_PPred_P`$, $`P`$ does not LG-terminate w.r.t. $`Tab_P`$ and $`S`$.

###### Proof

Let $`Tab_P^{}Pred_P`$ be such that $`P`$ quasi-terminates w.r.t. $`Tab_P^{}`$ and $`S`$ and $`P`$ does not LG-terminate w.r.t. $`Tab_P^{}`$ and $`S`$. Then, there exists a predicate $`pTab_P^{}Rec_P`$ such that there is a $`p`$-atom in $`Call(P,S)`$ which has infinitely may different (nonvariant) computed answers. Since tabling does not influence the set of call patterns nor the set of computed answer substitutions (see e.g. \[20, Theorem 2.1\]), there cannot exist a tabling such that $`P`$ LG-terminates w.r.t. that tabling and the set $`S`$. ∎

Consider two tablings $`Tab_1,Tab_2Pred_P`$ for a program $`P`$. Suppose $`Tab_1Tab_2`$ (hence $`NTab_1NTab_2`$). The next proposition studies the relationship between the LG-termination of $`P`$ w.r.t. these two tablings.

###### Proposition 2

Let $`P`$ be a program. Let $`Pred_P=Tab_1NTab_1`$ and $`Pred_P=Tab_2NTab_2`$. Suppose $`Tab_1Tab_2`$. Let $`SB_P^E`$.
If $`P`$ $`LG`$-terminates w.r.t. $`Tab_1`$ and $`S`$, then $`P`$ $`LG`$-terminates w.r.t. $`Tab_2`$ and $`S`$.

###### Proof

Let $`A`$ be an atom such that $`\stackrel{~}{A}S`$. Let $`_1`$ be the LG-forest w.r.t. $`Tab_1`$ of $`P\{A\}`$ and let $`_2`$ be the LG-forest w.r.t. $`Tab_2`$ of $`P\{A\}`$. We know that $`_1`$ consists of a finite number of finite LG-trees. So, $`\mathrm{}Call(P,\{A\})<\mathrm{}`$, hence, $`\mathrm{}(Call(P,\{A\})B_{Tab_2}^E)<\mathrm{}`$ and $`_2`$ consists of a finite number of LG-trees. We prove that the LG-trees of $`_2`$ are finite. Since each LG-tree in $`_2`$ can be extended to obtain an LG-tree in $`_1`$, this follows from the finiteness of the LG-trees in $`_1`$. ∎

Note that this proposition does not hold for quasi-termination as is shown in the following example.

###### Example 6

Recall the program $`P`$ and set $`S=\{p(X)\}`$ of Example 3. Let $`Tab_1=\{p/1\}`$ (as in Example 3) and $`Tab_2=\{p/1,q/1\}`$. Then, $`P`$ quasi-terminates w.r.t. $`Tab_1`$ and $`S`$ (the LG-forest in this case was shown in Figure 2). But, as is shown in Figure 4, $`P`$ does not quasi-terminate w.r.t. $`Tab_2`$ and $`S`$.

### 4.3 Characterization of LG-termination through quasi-termination

We now relate the notions of quasi-termination and LG-termination in a more detailed way. By definition, quasi-termination only corresponds to part of the LG-termination notion; it fails to capture non-termination caused by an infinitely branching node in an LG-tree. Note that if an LG-forest contains a tree with an infinitely branching node, then there is an LG-tree in the forest which is infinitely branching in a node which contains a query with a selected atom which is tabled and recursive. This observation leads to the following lemma. We denote the set of tabled, recursive predicates in a program $`P`$ with $`TR_P`$:

$$TR_P=Tab_PRec_P.$$

###### Lemma 6

Let $`P`$ be a program, $`Tab_PPred_P`$, and $`SB_P^E`$.
$`P`$ LG-terminates w.r.t. $`Tab_P`$ and $`S`$ iff $`P`$ quasi-terminates w.r.t. $`Tab_P`$ and $`S`$ and for all $`A`$ such that $`\stackrel{~}{A}S`$, the set of LD-computed answers for atoms in $`Call(P,\{A\})B_{TR_P}^E`$ is finite.

###### Proof

$`:`$ Suppose $`P`$ LG-terminates w.r.t. $`Tab_P`$ and $`S`$. Then $`P`$ quasi-terminates w.r.t. $`Tab_P`$ and $`S`$. It is easy to see that, since for every $`A`$ such that $`\stackrel{~}{A}S`$ the LG-forest for $`P\{A\}`$ consists of a finite number of finite trees, the set of computed answers for atoms in $`Call(P,\{A\})`$ is finite.

$`:`$ Suppose that $`P`$ quasi-terminates w.r.t. $`Tab_P`$ and $`S`$ and for all $`A`$ such that $`\stackrel{~}{A}S`$ the set of LD-computed answers for atoms in $`Call(P,\{A\})B_{TR_P}^E`$ is finite. We prove that $`P`$ LG-terminates w.r.t. $`Tab_P`$ and $`S`$. Let $`A`$ be an atom such that $`\stackrel{~}{A}S`$. We already know that the LG-forest $``$ of $`P\{A\}`$ consists of a finite number of LG-trees without infinite branches. We prove by contradiction that these LG-trees are finitely branching. Suppose there is an LG-tree in $``$ which is infinitely branching. Then, there is an LG-tree in $``$ with an infinitely branching node, which contains a query which has a tabled, recursive atom at the leftmost position. That is, there is an atom in $`Call(P,\{A\})B_{TR_P}^E`$ which has infinitely many computed answers. This gives a contradiction. ∎

It follows from the proof of Lemma 6 that, if $`P`$ LG-terminates w.r.t. $`Tab_P`$ and $`S`$, the set of computed answers for atoms in $`Call(P,\{A\})`$ is finite for all $`A`$ such that $`\stackrel{~}{A}S`$.

Based on the observation in Lemma 6, we next define a transformation on programs, called the answer-transformation, such that LG-termination of a program $`P`$ is equivalent to the quasi-termination of the program $`P^a`$ obtained by applying the answer-transformation on $`P`$.

###### Definition 12 (a(nswer)-transformation)

Let $`P`$ be a program and $`Tab_PPred_P`$. The a-transformation on $`P`$ and $`Tab_P`$ is defined as follows:

* For a clause $`C=HB_1,\mathrm{},B_n`$ in $`P`$, we define

$$C^a=HB_1,B_1^{},\mathrm{},B_n,B_n^{}$$

with $`B_i^{}`$ defined as follows (suppose $`B_i=p(t_1,\mathrm{},t_n)`$):

if $`pTab_P`$ and $`pRel(H)`$ then $`B_i^{}=p^a(t_1,\mathrm{},t_n)`$, where $`p^a/n`$ is a new predicate, else $`B_i^{}=\mathrm{}`$.

Let $`TR_P^a=\{p^a/n|p/nTR_P\}`$ (recall that $`TR_P=Tab_PRec_P`$).
* For the program $`P`$, we define

$$P^a=\{C^a|CP\}\{p^a(X_1,\mathrm{},X_n)|p^a/nTR_P^a\}.$$
* The set of tabled predicates of the program $`P^a`$ is defined as

$$Tab_{P^a}=Tab_PTR_P^a.$$

###### Example 7

Let $`P`$ be the program of Example 1, with $`Tab_P=\{\mathrm{𝑝𝑎𝑡ℎ}/4\}`$. The a-transformation, $`P^a`$, of $`P`$ is the following program:

$$\{\begin{array}{ccc}path(X,Ed,Y,[Y])\hfill & \hfill & edge(X,Ed,Y)\hfill \\ path(X,Ed,Z,[Y|L])\hfill & \hfill & edge(X,Ed,Y),path(Y,Ed,Z,L),\hfill \\ & & path^a(Y,Ed,Z,L)\hfill \\ edge(X,[e(X,Y)|L],Y)\hfill & \hfill & \\ edge(X,[e(X_1,X_2)|L],Y)\hfill & \hfill & edge(X,L,Y)\hfill \\ path^a(X,Ed,Y,L)\hfill & \hfill & \end{array}$$

with $`Tab_{P^a}=\{\mathrm{𝑝𝑎𝑡ℎ}/4,\mathrm{𝑝𝑎𝑡ℎ}^a/4\}`$.

It is easy to see that $`Call(P,S)=Call(P^a,S)B_P^E`$. Also, if we denote with $`cas(P,\{p(\overline{t})\})`$ the set of computed answer substitutions for $`p(\overline{t})`$ in $`P`$, then $`cas(P,\{p(\overline{t})\})`$ $`=cas(P^a,\{p(\overline{t})\})`$ for all $`p(\overline{t})B_P^E`$. It is important to note that, if we have a query $`p(\overline{t})B_{TR_P}^E`$ to the program $`P`$, then $`p(\overline{t})\sigma `$ is a computed answer if $`p^a(\overline{t})\sigma `$ $`Call(P^a,\{p(\overline{t})\})`$. This is in fact the main purpose of the transformation.

###### Theorem 4.1 (characterisation of LG-termination in terms of quasi-termination)

Let $`P`$ be a program, $`Tab_PPred_P`$ and $`SB_P^E`$.
$`P`$ LG-terminates w.r.t. $`Tab_P`$ and $`S`$ iff $`P^a`$ quasi-terminates w.r.t. $`Tab_{P^a}`$ and $`S`$.

###### Proof

$`:`$ Suppose $`P^a`$ is quasi-terminating w.r.t. $`Tab_{P^a}`$ and $`S`$. Let $`A`$ be an atom such that $`\stackrel{~}{A}S`$. Let $``$ be the LG-forest w.r.t. $`Tab_P`$ of $`P\{A\}`$. We prove that $``$ consists of a finite number of finite LG-trees.
We know that the LG-forest $`^a`$ w.r.t. $`Tab_{P^a}`$ of $`P^a\{A\}`$ is a finite set of LG-trees, without infinite branches. It is easy to see that hence, $``$ consists also of a finite number of trees without infinite branches. We prove that the LG-trees in $``$ are finitely branching. Suppose this is not the case, i.e. there is an LG-tree in $``$ which is infinitely branching. Then, there is an LG-tree $`T`$ in $``$ which is infinitely branching in a non-root node, which is a query with leftmost atom $`p(t_1,\mathrm{},t_n)`$, with $`pTR_P`$, which is directly descending from an atom $`q(s_1,\mathrm{},s_m)`$, with $`pq`$, via a recursive clause $`C=q(u_1,\mathrm{},u_m)\mathrm{},p(v_1,\mathrm{},v_n),\mathrm{}`$. Let $`T^a`$ be the LG-tree in $`^a`$ corresponding to $`T`$. Note that the clause $`C^a`$ instead of $`C`$ is used in $`T^a`$. Because of this, the atom to the right of $`p(t_1,\mathrm{},t_n)`$ in the infinitely branching node is $`p^a(t_1,\mathrm{},t_n)`$. Thus, $`^a`$ consists of a infinite number of LG-trees (there are an infinite number of LG-trees with predicate $`p^a`$ in the root). This gives a contradiction.
$`:`$ Suppose $`P`$ is LG-terminating w.r.t. $`Tab_P`$ and $`S`$. Let $`A`$ be an atom such that $`\stackrel{~}{A}S`$. Let $``$ be the LG-forest w.r.t. $`Tab_P`$ of $`P\{A\}`$. Then, $``$ consists of a finite number of finite LG-trees. Let $`^a`$ be the LG-forest w.r.t. $`Tab_{P^a}`$ of $`P^a\{A\}`$. By definition of the $`a`$-transformation, we see that $`^a`$ also consists of a finite number of finite LG-trees. Hence, $`P^a`$ quasi-terminates (and even LG-terminates) w.r.t. $`Tab_{P^a}`$ and $`S`$. ∎

###### Example 8 (Example 7 continued)

The LG-forest w.r.t. $`Tab_P`$ of $`P`$ and
$`\{path(a,[e(a,b),e(b,a)],Y,L)\}`$ was shown in Figure 1. Note that the trees are infinitely branching and hence $`P`$ does not LG-terminate w.r.t. $`Tab_P`$ and $`\{\mathrm{𝑝𝑎𝑡ℎ}(a,[e(a,b),e(b,a)],Y,L)\}`$.
In Figure 5, the LG-forest of the program $`P^a`$ and $`\{\mathrm{𝑝𝑎𝑡ℎ}(a,[e(a,b),e(b,a)],Y,L)\}`$ w.r.t. $`Tab_{P^a}`$ is shown. Note that there are infinitely many LG-trees in the forest; $`P^a`$ does not quasi-terminate w.r.t. $`Tab_{P^a}`$ and $`\{\mathrm{𝑝𝑎𝑡ℎ}(a,[e(a,b),e(b,a)],Y,L)\}`$.

## 5 Conditions for Termination of Tabled Logic Programs

In this section, we give sufficient conditions for the notions of quasi-termination and LG-termination. We prove that these conditions are also necessary in case the tabling satisfies the property of being well-chosen. First, we want to note that the termination conditions are adapted from the acceptability notion for LD-termination defined in , and not from the more “standard” definition of acceptability by Apt and Pedreschi in . The reason for this choice is that the quasi-termination as well as the LG-termination property of a tabled program and query is *not* closed under substitution. To see this, consider the following example from .

###### Example 9

Let $`p/2`$ be a tabled predicate defined by the following clause.

$$p(f(X),Y)p(X,Y)$$

Then, the query $`p(X,Y)`$ terminates while $`p(X,X)`$ does not.

The acceptability notion in  is expressed in terms of ground instances of clauses and its associated notion of LD-termination is expressed in terms of the set of all queries that are bounded under the given level mapping. Such sets are closed under substitution. Because quasi-termination lacks invariance under substitution, we need a stronger notion of acceptability, capable of treating *any* set of queries.

We next introduce the notion of well-chosen tabling w.r.t. a program. If the tabling is well-chosen, we are able to give a necessary and sufficient condition for quasi-termination and for LG-termination. If the tabling is not well-chosen, the condition is still sufficient.

We first introduce some notation. Let $`P`$ be a program and let $`G_P`$ be the dependency graph of the predicates of $`P`$. For a tabling $`Tab_P`$ for $`P`$ and predicates $`p,qNTab_P`$ with $`pq`$, let $`C_1(p,q),`$ $`C_2(p,q)`$ and $`C_3(p,q)`$ denote the following disjoint cases:

No cycle of directed arcs in $`G_P`$ containing $`p`$ and $`q`$ contains a predicate from $`Tab_P`$.

All cycles of directed arcs in $`G_P`$ containing $`p`$ and $`q`$ contain at least one predicate from $`Tab_P`$.

There is a cycle of directed arcs in $`G_P`$ containing $`p`$ and $`q`$ which contains no predicate from $`Tab_P`$ and there is a cycle of directed arcs in $`G_P`$ containing $`p`$ and $`q`$ which contains a predicate from $`Tab_P`$.

Note that $`C_1(p,q),`$ $`C_2(p,q)`$ and $`C_3(p,q)`$ depend on the program $`P`$ (more precisely on the dependency graph $`G_P`$ of $`P`$) and on the tabling $`Tab_P`$ for $`P`$. When referring to one of these three cases, it will always be clear from the context which program and tabling are under consideration. Given a program $`P`$ and tabling $`Tab_P`$, for all predicates $`p,qNTab_P`$ with $`pq`$, exactly one of the cases $`C_1(p,q),`$ $`C_2(p,q)`$ or $`C_3(p,q)`$ holds.

###### Example 10

Consider the following three propositional programs $`P`$, $`P^{^{}}`$ and $`P^{^{\prime \prime }}`$:

$$\begin{array}{ccccc}P:\{\begin{array}{ccc}a\hfill & \hfill & b\hfill \\ b\hfill & \hfill & c\hfill \\ c\hfill & \hfill & b\hfill \end{array}\hfill & & P^{^{}}:\{\begin{array}{ccc}a\hfill & \hfill & b\hfill \\ b\hfill & \hfill & c\hfill \\ c\hfill & \hfill & a\hfill \end{array}\hfill & & P^{^{\prime \prime }}:\{\begin{array}{ccc}a\hfill & \hfill & b\hfill \\ b\hfill & \hfill & c\hfill \\ c\hfill & \hfill & a\hfill \\ c\hfill & \hfill & b\hfill \end{array}\hfill \end{array}$$

with $`Tab_P=Tab_P^{^{}}=Tab_{P^{^{\prime \prime }}}=\{a/0\}`$.

For the program $`P`$, we have that $`C_1(b,c)`$ holds. For the program $`P^{^{}}`$, we have that $`C_2(b,c)`$ holds. For the program $`P^{^{\prime \prime }}`$, we have that $`C_3(b,c)`$ holds.

We next define the notion of well-chosen tabling w.r.t. a program $`P`$. A tabling for $`P`$ is well-chosen w.r.t. $`P`$ if it is such that the third case $`C_3`$ never occurs.

###### Definition 13 (well-chosen tabling (w.r.t. a program))

Let $`P`$ be a program. The tabling $`Tab_P`$ is called well-chosen w.r.t. the program $`P`$ iff for every $`p,qNTab_P`$ such that $`pq`$, either $`C_1(p,q)`$ or $`C_2(p,q)`$ holds.

Note that in case $`Tab_P`$ is well-chosen w.r.t. $`P`$, we have that if $`p,q,rNTab_P`$ and $`pqr`$ and $`C_1(p,q)`$ (resp. $`C_2(p,q)`$) holds, then $`C_1(q,r)`$ (resp. $`C_2(q,r)`$) holds. In the special case that $`NTab_P\{pPred_P|p`$ is a non-recursive or only directly recursive predicate$`\}`$ or that $`NTab_P=\mathrm{}`$ (i.e. $`Tab_P=Pred_P`$), the tabling $`Tab_P`$ is well-chosen w.r.t. $`P`$.

###### Example 11

Recall the programs $`P`$, $`P^{^{}}`$ and $`P^{^{\prime \prime }}`$ of Example 10. The tabling $`\{a/0\}`$ is well-chosen w.r.t. $`P`$ and $`P^{^{}}`$, but not w.r.t. $`P^{^{\prime \prime }}`$.

### 5.1 Quasi-Termination

We now introduce the notion of quasi-acceptability, in general a sufficient condition for quasi-termination. In case the tabling is well-chosen, quasi-acceptability is also a necessary condition for quasi-termination.

###### Definition 14 (quasi-acceptability)

Let $`P`$ be a program, $`Tab_PPred_P`$, and $`SB_P^E`$. $`P`$ is quasi-acceptable w.r.t. $`Tab_P`$ and $`S`$ iff there is a level mapping $`|.|`$ on $`B_P^E`$ such that for all $`A`$ such that $`\stackrel{~}{A}S`$, $`|.|`$ is finitely partitioning on $`Call(P,\{A\})B_{Tab_P}^E`$ and such that

* for every atom $`A`$ such that $`\stackrel{~}{A}Call(P,S)`$,
* for every clause $`HB_1,\mathrm{},B_n`$ in $`P`$, such that $`mgu(A,H)=\theta `$ exists,
* for every $`1in`$,
* for every $`cas`$ $`\theta _{i1}`$ for $`(B_1,\mathrm{},B_{i1})\theta `$:

$$\begin{array}{cc}|A||B_i\theta \theta _{i1}|\hfill & \\ \text{and}\hfill & \\ |A|>|B_i\theta \theta _{i1}|\hfill & \text{if}Rel(A)Rel(B_i)NTab_P\text{and}\hfill \\ & C_2(Rel(A),Rel(B_i))\text{does not hold}.\hfill \end{array}$$

###### Theorem 5.1 ((necessary and) sufficient condition for quasi-termination)

Let $`P`$ be a program, $`Tab_PPred_P`$ and $`SB_P^E`$.
If $`P`$ is quasi-acceptable w.r.t. $`Tab_P`$ and $`S`$, then $`P`$ quasi-terminates w.r.t. $`Tab_P`$ and $`S`$.
If the tabling $`Tab_P`$ is well-chosen w.r.t. $`P`$, then also the converse holds, i.e. $`P`$ is quasi-acceptable w.r.t. $`Tab_P`$ and $`S`$ iff $`P`$ quasi-terminates w.r.t. $`Tab_P`$ and $`S`$.

###### Proof

$`:`$ Suppose that $`P`$ is quasi-acceptable w.r.t. $`Tab_P`$, $`S`$ and a level mapping $`|.|`$. We prove that $`P`$ quasi-terminates w.r.t. $`Tab_P`$ and $`S`$. Let $`A`$ be an atom such that $`\stackrel{~}{A}S`$, let $``$ be the LG-forest w.r.t. $`Tab_P`$ of $`P\{A\}`$.

* $``$ consists of a finite number of LG-trees, i.e. $`\mathrm{}(Call(P,\{A\})B_{Tab_P}^E)<\mathrm{}`$.

Due to the quasi-acceptability condition, any call in $`Call(P,\{A\})`$ directly descending from $`A`$, say $`B`$, is such that $`|A||B|`$. The same holds recursively for the atoms descending from $`B`$. Thus, the level mapping of any call, recursively descending from $`A`$, is smaller than or equal to $`|A|\mathrm{}`$. Since $`|.|`$ is finitely partitioning on $`Call(P,\{A\})B_{Tab_P}^E`$, we have that: $`\mathrm{}(_{n|A|}|.|^1(n)Call(P,\{A\})B_{Tab_P}^E)<\mathrm{}`$. Hence, $`\mathrm{}(Call(P,\{A\})B_{Tab_P}^E)<\mathrm{}`$, i.e. $``$ consists of a finite number of trees.
* The LG-trees in $``$ have finite branches.

Suppose there is a tree in $``$ with an infinite branch. This infinite branch contains an infinite directed subsequence $`G_0,G_1,\mathrm{}`$. It is easy to see that the leftmost atoms in the nodes of this infinite directed subsequence all are $`NTab_P`$-atoms (because $`Tab_P`$-atoms are resolved using answers). There is a $`n\mathrm{}`$, such that each $`G_i`$, $`in`$, has as leftmost atom $`A_i`$ and for all $`in`$, $`Rel(A_i)Rel(A_{i+1})`$ and $`C_2(Rel(A_i),Rel(A_{i+1}))`$ does not hold. Because of the quasi-acceptability condition, $`|A_i|>|A_{i+1}|`$, for all $`in`$. This gives a contradiction.

$`:`$ Suppose that the tabling $`Tab_P`$ is well-chosen w.r.t. $`P`$ and suppose that $`P`$ quasi-terminates w.r.t. $`S`$. We have to construct a level mapping $`|.|`$ such that $`P`$ is quasi-acceptable w.r.t. $`Tab_P`$, $`S`$ and this level mapping $`|.|`$. We will only define $`|.|`$ on elements of $`Call(P,S)`$. On elements of the complement of $`Call(P,S)`$ in $`B_P^E`$, $`|.|`$ can be assigned any value, as these elements do not turn up in the quasi-acceptability condition.
In order to define $`|.|`$ on $`Call(P,S)`$, consider the $`Call`$-$`Gr(P,S)`$-graph (Definition 4). Consider a strongly connected component $`C`$ in $`Call`$-$`Gr(P,S)`$.
Then, there is at least one $`Tab_P`$-atom in $`C`$. To see this, suppose this is not the case. Consider a cyclic path $`p`$ in $`C`$. This consists only of $`NTab_P`$-atoms. But then, because of Proposition 1, there is an infinite branch in a tree of the LG-forest of an element of $`S`$. This gives a contradiction.
Also, there is only a finite number of $`Tab_P`$-atoms in $`C`$. To see this, suppose this is not the case. Then there is an infinitely long path $`p`$ through infinitely many $`Tab_P`$-atoms of $`C`$. Because of Proposition 1, there is an infinite number of $`Tab_P`$-atoms selected in a derivation of an element of $`S`$, i.e. there are infinitely many trees in the LG-forest of that element of $`S`$. This gives a contradiction.
For every two non-tabled atoms, say $`p(\overline{t})`$ and $`q(\overline{s})`$, in $`C`$ (note that thus $`pq`$), $`C_1(p,q)`$ does not hold (since there is at least one $`Tab_P`$-atom in $`C`$). Thus, since the tabling is well-chosen, $`C_2(p,q)`$ holds.
Define $`\overline{CG}`$ as the graph obtained from $`Call`$-$`Gr(P,S)`$ by replacing any strongly connected component by a single contracting node and replacing any arc from $`Call`$-$`Gr(P,S)`$ pointing to (resp. from) any node in that strongly connected component by an arc to (resp. from) that contracting node. $`\overline{CG}`$ does not have any (non-trivial) strongly connected components. Moreover, any strongly connected component from $`Call`$-$`Gr(P,S)`$ that was collapsed into a contracting node of $`\overline{CG}`$ necessarily contains at least one and at most a finite number of $`Tab_P`$-atoms.
Note now that each path in $`\overline{CG}`$ which is not cyclic (there are only trivial cycles in $`\overline{CG}`$) is finite. This also follows directly from Proposition 1.
Note also that it is possible that $`\overline{CG}`$ has an infinitely branching (possibly contracting) node. Let $`A`$ be an atom in that infinitely branching node. It follows from Lemma 3 that, because $`P`$ quasi-terminates w.r.t. $`S`$, $`\mathrm{}(\{B|B`$ is a descendant of $`A`$ in $`\overline{CG}\}`$ $``$ $`B_{Tab_P}^E)<`$ $`\mathrm{}`$.
We now construct $`\overline{\overline{CG}}`$ from $`\overline{CG}`$ starting from the top nodes $`N_1`$ downwards as follows:

* replace all direct descendants of $`N_1`$ in $`\overline{CG}`$ different from $`N_1`$, by a single contracting node $`N_2`$;
* replace any arc from $`\overline{CG}`$ pointing to (resp. from) any node in that (possibly infinite) set of direct descendants by an arc to (resp. from) that contracting node $`N_2`$;
* repeat this for the nodes $`N_2`$.

This process stops because, as we already noted, each path in $`\overline{CG}`$ which is not cyclic is finite. It is easy to see that $`\overline{\overline{CG}}`$ is a graph in which each node has at most one direct descendant different from itself. Also, each node in $`\overline{\overline{CG}}`$ consists of a (possibly infinite) set of nodes of $`Call`$-$`Graph(P,S)`$ which contains only finitely many $`Tab_P`$-atoms.
We define the level mapping $`|.|`$ as follows. Consider the layers of $`\overline{\overline{CG}}`$ (there are only a finite number of layers). Let layer-0 be the set of leaves in $`\overline{\overline{CG}}`$. We assign to these nodes a number in $`\mathrm{}`$, such that all nodes get a different number. Then, we move up to the next layer in $`\overline{\overline{CG}}`$. This layer, layer-1, consists of all nodes $`N`$ such that the path starting from $`N`$ has length 1. We assign to each such node $`N`$ a natural number, such that the number assigned to $`N`$ is strictly larger than the number assigned to its descendant (in the previous step). We continue this process layer by layer. The value of the level mapping $`|.|`$ on elements of $`Call(P,S)`$ is defined as follows: all calls contained in the node $`N`$ receive the number assigned to the node $`N`$.
We prove that $`P`$ is quasi-acceptable w.r.t. $`Tab_P`$, $`S`$ and this level mapping $`|.|`$.

* for every $`AS`$, $`|.|`$ is finitely partitioning on $`B_{Tab_P}^ECall(P,\{A\})`$.

Note that $`|.|`$ is even finitely partitioning on $`B_{Tab_P}^ECall(P,S)`$. This is because each (contracting) node of $`\overline{\overline{CG}}`$ contains only a finite number of $`Tab_P`$-atoms and because of the construction of $`|.|`$.
* Let $`A`$ be an atom such that $`\stackrel{~}{A}Call(P,S)`$, let $`HB_1,\mathrm{},B_n`$ be a clause in $`P`$, such that $`mgu(A,H)=\theta `$ exists, let $`\theta _{i1}`$ be a $`cas`$ for $`(B_1,\mathrm{},B_{i1})\theta `$:

+ then $`|A||B_i\theta \theta _{i1}|`$.

    This is because there is a directed arc from $`A`$ to $`B_i\theta \theta _{i1}`$ in $`Call`$-$`Graph(P,S)`$ and because of the construction of $`|.|`$.
    + then $`|A|>|B_i\theta \theta _{i1}|`$ if $`Rel(A)Rel(B_i)`$ $`NTab_P`$ and $`C_2(Rel(A),Rel(B_i))`$ does not hold (i.e. $`C_1(Rel(A),Rel(B_i))`$ holds).

    There is a directed arc in $`Call`$-$`Graph(P,S)`$ from $`A`$ to $`B_i\theta \theta _{i1}`$. Note that $`A`$ and $`B_i\theta \theta _{i1}`$ do not belong to the same strongly connected component of $`Call`$-$`Graph(P,S)`$. This is because $`C_1(Rel(A),Rel(B_i))`$ holds. So, $`A`$ and $`B_i\theta \theta _{i1}`$ belong to a different layer and $`B_i\theta \theta _{i1}`$ is a direct descendant of $`A`$. Hence, because of the construction of $`|.|`$, $`|A|>|B_i\theta \theta _{i1}|`$. ∎

###### Example 12

Recall the programs $`P`$ and $`P^{^{}}`$ with $`Tab_P=Tab_P^{^{}}=\{a/0\}`$ of Example 10. Let $`S=\{a\}`$. The LG-forests for $`P\{a\}`$ and $`P^{^{}}\{a\}`$ are shown in Figure 6.

$`P`$ does not quasi-terminate w.r.t. $`\{a/0\}`$ and $`S`$, whereas $`P^{^{}}`$ quasi-terminates w.r.t. $`\{a/0\}`$ and $`S`$.
This can be proven by Theorem 5.1. Recall from Example 11 that for both programs, the tablings are well-chosen. Also note that, because the programs are propositional, every level mapping is finitely partitioning on the whole Herbrand base.
Let’s first consider program $`P`$. Recall that for this program and tabling $`\{a/0\}`$ the condition $`C_1(b,c)`$ holds. Note that there is no level mapping $`|.|`$ such that $`|b|>|c|`$ and $`|c|>|b|`$ holds. Hence, the condition in Theorem 5.1 can not be satisfied and $`P`$ does not quasi-terminate w.r.t. $`\{a/0\}`$ and $`S`$.
Consider next program $`P^{^{}}`$. Recall that for this program and tabling $`\{a/0\}`$ the condition $`C_2(b,c)`$ holds. Let $`|.|`$ be the following level mapping $`|a|=|b|=|c|=0`$. With this level mapping, $`P^{^{}}`$ satisfies the condition of Theorem 5.1 and hence, $`P^{^{}}`$ quasi-terminates w.r.t. $`\{a/0\}`$ and $`S`$.

The quasi-acceptability condition is necessary only in case the tabling is well-chosen. We next give an example of a program $`P`$, a tabling $`Tab_P`$ which is not well-chosen w.r.t. $`P`$, and a set of queries $`S`$, such that $`P`$ quasi-terminates w.r.t. $`Tab_P`$ and $`S`$, but $`P`$ is not quasi-acceptable w.r.t. $`Tab_P`$ and $`S`$.

###### Example 13

Let $`P`$ be the following program:

$$\{\begin{array}{ccc}p(X)\hfill & \hfill & q(X)\hfill \\ q(X)\hfill & \hfill & r(X)\hfill \\ r(s(X))\hfill & \hfill & q(X)\hfill \\ r(X)\hfill & \hfill & p(X)\hfill \end{array}$$

with tabling $`Tab_P=\{p/1\}`$. Notice that $`Tab_P`$ is not well-chosen w.r.t. $`P`$. Let $`S=\{p(0)\}`$. $`P`$ quasi-terminates w.r.t. $`Tab_P`$ and $`S`$. We show that $`P`$ is not quasi-acceptable w.r.t. $`Tab_P`$ and $`S`$. Suppose that there exists a level mapping $`|.|`$ such that $`P`$ is quasi-acceptable w.r.t. $`Tab_P`$, $`S`$ and this level mapping $`|.|`$ (we prove a contradiction). Then, for this level mapping, the following inequalities must hold: $`|p(0)||q(0)|`$, $`|q(0)|>|r(0)|`$ (since $`C_3(q,r)`$ holds, and so $`C_2(q,r)`$ does not hold), and $`|r(0)||p(0)|`$. Hence, $`|p(0)|>|p(0)|`$ must hold, but this gives a contradiction.

### 5.2 LG-Termination

In analogy to quasi-termination, we now present a necessary and sufficient condition for LG-termination in case the tabling is well-chosen. In case the tabling is not well-chosen, the condition is still sufficient.

Note that Theorem 4.1 already provides us with a characterisation of LG-termination of a program in terms of quasi-termination. That is, to prove the LG-termination of $`P`$ w.r.t. $`Tab_P`$ and $`S`$, it suffices to prove the quasi-termination of $`P^a`$, the a-transformation of the program $`P`$, w.r.t. $`Tab_{P^a}`$ and $`S`$. To prove quasi-termination, we can use the results of Subsection 5.1. Namely, it is sufficient (and also necessary in case the tabling is well-chosen<sup>1</sup><sup>1</sup>1 Note that if $`Tab_P`$ is well-chosen w.r.t. $`P`$, then also $`Tab_{P^a}`$ is well-chosen w.r.t. $`P^a`$.) to prove the quasi-acceptability of $`P^a`$ w.r.t. $`Tab_{P^a}`$ and $`S`$. However, the condition of quasi-acceptability on $`P^a`$ can be weakened; i.e. some of the decreases “$`|A||B_i\theta \theta _{i1}|`$” need not be checked because they can always be fulfilled. In particular, we only have to require the non-strict decrease for recursive, tabled body atoms $`B_i`$ (to obtain an LG-forest with only finitely many LG-trees) or for body atoms $`B_i`$ of the form $`p^a(t_1,\mathrm{},t_n)`$ (to obtain LG-trees which are finitely branching); the conditions on non-tabled predicates remain the same. The following notion of LG-acceptability gives this optimised condition for LG-termination of a program.

###### Definition 15 (LG-acceptability)

Let $`P`$ be a program, $`Tab_PPred_P`$ and $`SB_P^E`$. $`P`$ is LG-acceptable w.r.t. $`Tab_P`$ and $`S`$ iff
there is a level mapping $`|.|`$ on $`B_{P^a}^E`$ such that for all $`A`$ such that $`\stackrel{~}{A}S`$, $`|.|`$ is finitely partitioning on $`Call(P^a,\{A\})B_{TR_PTR_P^a}^E`$, and such that

* for every atom $`A`$ such that $`\stackrel{~}{A}Call(P^a,S)`$,
* for every clause $`HB_1,\mathrm{},B_n`$ in $`P^a`$, such that $`mgu(A,H)=\theta `$ exists,
* for every $`B_i`$ such that $`Rel(B_i)Rel(H)`$ or $`Rel(B_i)TR_P^a`$,
* for every $`cas`$ $`\theta _{i1}`$ in $`P^a`$ for $`(B_1,\mathrm{},B_{i1})\theta `$:

$$\begin{array}{cc}|A||B_i\theta \theta _{i1}|\hfill & \\ \text{and}\hfill & \\ |A|>|B_i\theta \theta _{i1}|\hfill & \text{if}Rel(A)Rel(B_i)NTab_P\text{and}\hfill \\ & C_2(Rel(A),Rel(B_i))\text{does not hold}.\hfill \end{array}$$

###### Theorem 5.2 ((necessary and) sufficient condition for LG-termination)

Let $`P`$ be a program, $`Tab_PPred_P`$ and $`SB_P^E`$.
If $`P`$ is LG-acceptable w.r.t. $`Tab_P`$ and $`S`$, then $`P`$ LG-terminates w.r.t. $`Tab_P`$ and $`S`$.
If the tabling $`Tab_P`$ is well-chosen w.r.t. $`P`$, then also the converse holds, i.e. $`P`$ is LG-acceptable w.r.t. $`Tab_P`$ and $`S`$ iff $`P`$ LG-terminates w.r.t. $`Tab_P`$ and $`S`$.

###### Proof

$`:`$ Suppose that $`P`$ is LG-acceptable w.r.t. $`Tab_P`$ and $`S`$. We prove that $`P`$ LG-terminates w.r.t. $`Tab_P`$ and $`S`$.
Let $`A`$ be an atom such that $`\stackrel{~}{A}S`$. Let $``$ be the LG-forest w.r.t. $`Tab_P`$ of $`P\{A\}`$. We prove that $``$ consists of a finite number of finite LG-trees.

* The LG-trees in $``$ are finitely branching.

Suppose this is not the case, i.e. there is an LG-tree in $``$ which is infinitely branching. Then, there is an LG-tree $`T`$ in $``$ which is infinitely branching in a non-root node, which is a query with leftmost atom $`p(t_1,\mathrm{},t_n)`$, with $`pTR_P`$, which is directly descending from an atom $`q(s_1,\mathrm{},s_m)`$, with $`pq`$, via a recursive clause $`C=q(u_1,\mathrm{},u_m)\mathrm{},p(v_1,\mathrm{},v_n),\mathrm{}`$. Now, consider the LG-forest $`^a`$ of $`P^a\{A\}`$. Let $`T^a`$ be the LG-tree in $`^a`$ corresponding to $`T`$. Note that the clause $`C^a`$ instead of $`C`$ is used in $`T^a`$. Because of this, the atom on the right of $`p(t_1,\mathrm{},t_n)`$ in the infinitely branching node is $`p^a(t_1,\mathrm{},t_n)`$. Thus, $`^a`$ consists of a infinite number of LG-trees (there are an infinite number of LG-trees with predicate $`p^a`$ in the root). But, all these $`p^a`$-atoms directly descend from the node $`q(s_1,\mathrm{},s_m)`$ via the clause $`C^a`$ in $`P^a`$ and hence, because of the LG-acceptability condition, their value under the level mapping $`|.|`$ is smaller or equal to $`|q(s_1,\mathrm{},s_m)|`$. Because $`|.|`$ is finitely partitioning on $`Call(P^a,\{A\})B_{TR_P^a}^E`$, this gives a contradiction.
* $``$ consists of a finite number of LG-trees, i.e. $`\mathrm{}(Call(P,\{A\})B_{Tab_P}^E)<\mathrm{}`$.

Suppose this is not the case. A first possible reason for an infinite number of LG-trees in $``$ is an infinitely branching LG-tree in $``$. But we already proved that this does not occur. The other possibility is that there exists an infinite LD-derivation of $`A`$ in $`P`$ which contains an infinite directed subsequence, such that this infinite directed subsequence has a tail $`G_n,G_{n+1},\mathrm{}`$ with $`G_i=A_i,𝒜_i`$, $`in`$, such that $`\{\stackrel{~}{A}_i|in\}B_{Tab_P}^E`$ is an infinite set and $`Rel(A_i)Rel(A_{i+1})`$ for all $`in`$. So, $`\{\stackrel{~}{A}_i|in\}B_{TR_P}^E`$. Since $`\stackrel{~}{A}_iCall(P,\{A\})B_{TR_P}^E`$ $``$ $`Call(P^a,\{A\})B_{TR_PTR_P^a}^E`$, and since $`|.|`$ is finitely partitioning on this set and $`|A_i||A_{i+1}|`$ for all $`in`$ (by the LG-acceptability condition), this gives a contradiction.
* The LG-trees in $``$ have finite branches.

The same argumentation as in the proof of Theorem 5.1 can be applied here.

$`:`$ Suppose that the tabling $`Tab_P`$ is well-chosen w.r.t. $`P`$ and suppose that $`P`$ LG-terminates w.r.t. $`Tab_P`$ and $`S`$. We prove that there exists a level mapping $`|.|`$ such that $`P`$ is LG-acceptable w.r.t. $`Tab_P`$, $`S`$ and this level mapping $`|.|`$.
Since $`P`$ LG-terminates w.r.t. $`Tab_P`$ and $`S`$, we know by Theorem 4.1 that $`P^a`$ quasi-terminates w.r.t. $`Tab_{P^a}`$ and $`S`$. Note that, since $`Tab_P`$ is well-chosen w.r.t. $`P`$, $`Tab_{P^a}`$ is well-chosen w.r.t. $`P^a`$. By Theorem 5.1, there exists a level mapping $`|.|`$ such that $`P^a`$ is quasi-acceptable w.r.t. $`Tab_{P^a}`$, $`S`$ and this level mapping $`|.|`$. It is straightforward to verify that $`P`$ is LG-acceptable w.r.t. $`Tab_P`$, $`S`$ and this level mapping $`|.|`$. (Note that, as we already discussed in the beginning of this subsection, the level mapping obtained in this way satisfies more conditions than required by the notion of LG-acceptability.) ∎

###### Example 14

Recall the part $`R`$ of the grammar program (Section 3.1) which recognizes the language $`a^nb`$:

$$\begin{array}{cc}R:\hfill & \{\begin{array}{ccc}s(Si,So)\hfill & \hfill & a(Si,S),S=[b|So]\hfill \\ a(Si,So)\hfill & \hfill & a(Si,S),a(S,So)\hfill \\ a(Si,So)\hfill & \hfill & Si=[a|So]\hfill \end{array}\hfill \end{array}$$

with $`Tab_R=\{a/2\}`$. We show that $`R`$ LG-terminates w.r.t. $`\{a/2\}`$ and $`S=\{s(si,So)|si`$ is a ground list consisting of constants $`a,b`$ and $`So`$ is a variable$`\}`$. Consider the a-transformation of $`R`$:

$$\begin{array}{cc}R^a:\hfill & \{\begin{array}{ccc}s(Si,So)\hfill & \hfill & a(Si,S),S=[b|So]\hfill \\ a(Si,So)\hfill & \hfill & a(Si,S),a^a(Si,S),a(S,So),a^a(S,So)\hfill \\ a(Si,So)\hfill & \hfill & Si=[a|So]\hfill \\ a^a(Si,So)\hfill & \hfill & \end{array}\hfill \end{array}$$

with $`Tab_{R^a}=\{a/2,a^a/2\}`$. When applying Theorem 5.2, we only have to consider the second clause of $`R^a`$. Note that, for all $`a(t1,t2)Call(R^a,\{s(si,So)\})`$, $`t1`$ is a sublist of $`si`$ and $`t2`$ is a variable. Also, for all $`a^a(v1,v2)Call(R^a,\{s(si,So)\})`$, $`v1`$ is a sublist of $`si`$ and $`v2`$ is a (strict) sublist of $`v1`$. Let $`|.|`$ be the following level mapping:

$$\begin{array}{cc}|a(t1,t2)|\hfill & =2t1_l\hfill \\ |a^a(v1,v2)|\hfill & =v1_l+v2_l\hfill \end{array}$$

where $`._l`$ is the list-length norm<sup>2</sup><sup>2</sup>2The list-length norm is defined as follows:
$$\{\begin{array}{ccc}[h|t]_l\hfill & =1+t_l\hfill & \\ u_l\hfill & =0\hfill & \text{if}u[h|t].\hfill \end{array}$$
. The level mapping $`|.|`$ is finitely partitioning on the whole set $`Call(R^a,S)B_{\{a/2,a^a/2\}}^E`$. It can be easily verified that $`R`$ and $`S`$, together with $`|.|`$, satisfy the conditions of Theorem 5.2. Hence, $`R`$ LG-terminates w.r.t. $`\{a/2\}`$ and $`S`$.

## 6 Modular Termination Proofs for Tabled Logic Programs

In the context of programming in the large, it is important to be able to obtain modular termination proofs, i.e. proofs built by combining termination proofs of separate components of the program. Starting from the quasi- and LG-acceptability conditions, we present modular proofs for quasi-termination in Subsections 6.1 and 6.3, and for LG-termination in Subsection 6.2. We consider the union $`PR`$ of two programs $`P`$ and $`R`$, where $`P`$ extends<sup>3</sup><sup>3</sup>3Recall that a program $`P`$ extends a program $`R`$ iff no predicate defined in $`P`$ occurs in $`R`$. $`R`$, and we prove the quasi/LG-termination of $`PR`$ by imposing conditions on the two components $`P`$ and $`R`$.

In order to fix a notation, for $`Pred_{PR}=Tab_{PR}NTab_{PR}`$, let

$$\begin{array}{ccc}Tab_P=Tab_{PR}Pred_P\hfill & ,\hfill & NTab_P=NTab_{PR}Pred_P\hfill \\ Tab_R=Tab_{PR}Pred_R\hfill & ,\hfill & NTab_R=NTab_{PR}Pred_R.\hfill \end{array}$$

So the tabling of the union $`PR`$ determines the tabling of the components $`P`$ and $`R`$. Note that $`Tab_P`$ also contains predicates which are tabled in $`PR`$ but defined in $`R`$.

In the following we give modular termination proofs for the union $`PR`$ of two programs $`P`$ and $`R`$ where:

1. $`P`$ extends $`R`$.
2. $`P`$ extends $`R`$ and no defined predicate in $`P`$ is tabled ($`Def_PNTab_P`$).
3. $`P`$ extends $`R`$ and all defined predicates in $`P`$ are tabled ($`Def_PTab_P`$).
4. $`P`$ extends $`R`$ and $`R`$ extends $`P`$.

Note that points 2, 3 and 4 are special cases of the first one. The reason for treating them separately is because they occur quite often in practice and, more importantly, because in these special cases, simpler modular termination conditions can be given.

### 6.1 Modular Conditions for Quasi-Termination

Throughout this subsection, we will consider the following example.

###### Example 15

Consider the following union of programs $`U=TPRP^{^{}}`$ with $`Tab_U=\{path/4\}`$. Let $`S=\{reachable(rome,X)\}`$, then $`U`$ will compute the cities belonging to the same region $`r`$ as $`rome`$ and which are reachable from $`rome`$ making use of the list of connections of the region $`r`$. The program $`P^{^{}}`$ contains facts giving the region to which each city belongs and the list of connections in each region (a connection between city $`c_1`$ and city $`c_2`$ is given by the term $`e(c_1,c_2)`$).

$$\begin{array}{cc}T:\hfill & \{\begin{array}{ccc}reachable(X,Y)\hfill & \hfill & inregion(X,R),connections(R,Ed),\hfill \\ & & path(X,Ed,Y,L)\hfill \end{array}\hfill \\ & \\ P:\hfill & \{\begin{array}{ccc}path(X,Ed,Y,[Y])\hfill & \hfill & edge(X,Ed,Y)\hfill \\ path(X,Ed,Z,[Y|L])\hfill & \hfill & edge(X,Ed,Y),path(Y,Ed,Z,L)\hfill \end{array}\hfill \\ & \\ R:\hfill & \{\begin{array}{ccc}edge(X,[e(X,Y)|L],Y)\hfill & \hfill & \\ edge(X,[e(X_1,X_2)|L],Y)\hfill & \hfill & edge(X,L,Y)\hfill \end{array}\hfill \\ & \\ P^{^{}}:\hfill & \{\begin{array}{ccc}inregion(city,region).\hfill & & \\ \mathrm{}\hfill & & \\ connections(region,list\mathrm{\_}of\mathrm{\_}connections).\hfill & & \\ \mathrm{}\hfill & & \end{array}\hfill \end{array}$$

We will prove that $`U`$ quasi-terminates w.r.t. $`Tab_U`$ and $`S`$. We will do this in a modular way:

* In Example 16, after Proposition 3, we prove that $`U=T(PRP^{^{}})`$ quasi-terminates, given that $`(PRP^{^{}})`$ quasi-terminates.
* In Example 17, after Proposition 4, we prove that $`PR`$ quasi-terminates (recall that $`PR`$ is the program of Example 1 in Subsection 3.2).
* In Example 18, after Proposition 5, we prove that $`(PR)P^{^{}}`$ quasi-terminates, given that $`(PR)`$ and $`P^{^{}}`$ quasi-terminate.

###### Proposition 3

Suppose $`P`$ and $`R`$ are two programs, such that $`P`$ extends $`R`$. Let $`SB_{PR}^E`$. If

* $`R`$ quasi-terminates w.r.t. $`Tab_R`$ and $`Call(PR,S)`$,
* there is a level mapping $`|.|`$ on $`B_P^E`$ such that for all $`A`$ such that $`\stackrel{~}{A}S`$, $`|.|`$ is finitely partitioning on $`Call(PR,\{A\})B_{Tab_P}^E`$, and such that

+ for every atom $`A`$ such that $`\stackrel{~}{A}Call(PR,S)`$,
    + for every clause $`HB_1,\mathrm{},B_n`$ in $`P`$ such that $`mgu(A,H)=\theta `$ exists,
    + for every $`1in`$,
    + for every $`cas`$ $`\theta _{i1}`$ in $`PR`$ for $`(B_1,\mathrm{},B_{i1})\theta `$:
$$\begin{array}{cc}|A||B_i\theta \theta _{i1}|\hfill & \\ \text{and}\hfill & \\ |A|>|B_i\theta \theta _{i1}|\hfill & \text{if}Rel(A)Rel(B_i)NTab_P\text{and}\hfill \\ & C_2(Rel(A),Rel(B_i))\text{does not hold}.\hfill \end{array}$$

then, $`PR`$ quasi-terminates w.r.t. $`Tab_{PR}`$ and $`S`$.

###### Proof

Let $`A`$ be an atom such that $`\stackrel{~}{A}S`$. Let $``$ be the LG-forest w.r.t. $`Tab_{PR}`$ of $`PR\{A\}`$. We prove that $``$ consists of a finite number of LG-trees without infinite branches.
If $`A`$ is defined in $`R`$, this follows directly from the fact that $`P`$ extends $`R`$ and that $`R`$ quasi-terminates w.r.t. $`Tab_R`$ and $`Call(PR,S)`$.
So, suppose $`A`$ is defined in $`P`$. Because of the second condition in the proposition statement, every call directly descending from $`A`$, say $`B`$, is such that $`|B||A|`$. This holds recursively for atoms descending from $`A`$ using clauses of $`P`$. Because $`|.|`$ is finitely partitioning on $`B_{Tab_P}^ECall(PR,\{A\})`$, the set of tabled atoms, descending from $`A`$, using clauses of $`P`$, is finite. For atoms $`C`$, defined in $`R`$ and descending from $`A`$ using clauses of $`P`$, we know that $`\mathrm{}(Call(PR,\{C\})B_{Tab_R}^E)<\mathrm{}`$. So, $`\mathrm{}(Call(PR,\{A\})B_{Tab_{PR}}^E)<\mathrm{}`$.
We now prove that there is no tree in $``$ with an infinite branch. Suppose this is not the case, and there is a tree in $``$ with an infinite branch. Because, $`R`$ quasi-terminates w.r.t. $`Tab_R`$ and $`Call(PR,S)`$, and because $`P`$ extends $`R`$, this infinite branch contains an infinite directed subsequence $`G_0,G_1,\mathrm{}`$, with leftmost atoms $`A_0,A_1,\mathrm{}`$, belonging to $`B_{NTab_PDef_P}^E`$. This infinite directed subsequence has a tail, such that for all $`i`$ such that $`G_i`$ belongs to this tail, $`Rel(A_i)Rel(A_{i+1})`$ and $`C_2(Rel(A_i),Rel(A_{i+1}))`$ does not hold. But because of the condition in the proposition statement, $`|A_i|>|A_{i+1}|`$ and this gives a contradiction. ∎

###### Example 16 (Example 15 continued)

We illustrate the above proposition by proving that $`U=T(PRP^{^{}})`$ quasi-terminates w.r.t. $`Tab_U=\{\mathrm{𝑝𝑎𝑡ℎ}/4\}`$ and $`S=\{\mathrm{𝑟𝑒𝑎𝑐ℎ𝑎𝑏𝑙𝑒}(\mathrm{𝑟𝑜𝑚𝑒},X)\}`$, given that $`PRP^{^{}}`$ quasi-terminates w.r.t. $`\{\mathrm{𝑝𝑎𝑡ℎ}/4\}`$ and $`Call(U,S)`$. The quasi-termination of $`PRP^{^{}}`$ will be shown in the following examples of this subsection.
The trivial level mapping (mapping every atom to $`0`$) satisfies the condition of the proposition; there is no recursive call to a non-tabled predicate in $`T`$ and the set of called $`\mathrm{𝑝𝑎𝑡ℎ}`$-atoms is finite (since the database $`P^{^{}}`$ is finite).

The case of two programs $`P`$ and $`R`$, such that $`P`$ extends $`R`$ and such that no defined predicate in $`P`$ is tabled (mentioned as point 2 in the introduction of Section 6), does not give rise to a simpler modular termination condition than the condition in Proposition 3. We want to note already here that regarding LG-termination, this special case (point 2) will give rise to a simpler modular termination condition than in the general case.

The next proposition considers the special case of two programs $`P`$ and $`R`$, such that $`P`$ extends $`R`$ and such that all the defined predicates in $`P`$ are tabled.

###### Proposition 4

Suppose $`P`$ and $`R`$ are two programs, such that $`P`$ extends $`R`$, and such that $`Def_PTab_P`$. Let $`SB_{PR}^E`$. If

* $`R`$ quasi-terminates w.r.t. $`Tab_R`$ and $`Call(PR,S)`$,
* there is a level mapping $`|.|`$ on $`B_P^E`$ such that for all $`A`$ such that $`\stackrel{~}{A}S`$, $`|.|`$ is finitely partitioning on $`Call(PR,\{A\})B_{Tab_P}^E`$, and such that

+ for every atom $`A`$ such that $`\stackrel{~}{A}Call(PR,S)`$,
    + for every clause $`HB_1,\mathrm{},B_n`$ in $`P`$ such that $`mgu(A,H)=\theta `$ exists,
    + for every $`1in`$,
    + for every $`cas`$ $`\theta _{i1}`$ in $`PR`$ for $`(B_1,\mathrm{},B_{i1})\theta `$:

    $$|A||B\theta \theta _{i1}|$$

then, $`PR`$ quasi-terminates w.r.t. $`Tab_{PR}`$ and $`S`$.

###### Proof

This is a direct corollary of Proposition 3 (every recursive predicate in $`P`$ is defined in $`P`$ and hence tabled). ∎

###### Example 17 (Example 15 continued)

We illustrate the above proposition by proving that $`PR`$ quasi-terminates w.r.t. $`\{\mathrm{𝑝𝑎𝑡ℎ}/4\}`$ and $`Call(U,S)B_{PR}^E`$.

* First we prove that $`R`$ quasi-terminates w.r.t. $`\mathrm{}`$ and $`Call(U,S)B_R^E`$ (or, since there are no tabled atoms in $`R`$, that $`R`$ LD-terminates w.r.t. $`Call(U,S)B_R^E`$). We use Theorem 5.1 and show that $`R`$ is quasi-acceptable w.r.t. $`\mathrm{}`$ and $`Call(U,S)B_R^E`$. Consider the following level mapping:

$$|edge(t_1,t_2,t_3)|=t_2_l$$

It can be easily seen that we have a strict decrease between the head and the body atom of the recursive clause for $`edge`$ in $`R`$. Hence, the quasi-acceptability condition is satisfied.
* The trivial level mapping on $`B_P^E`$ satisfies the second condition in the proposition statement. Indeed, $`\mathrm{𝑝𝑎𝑡ℎ}`$ is tabled so a strict decrease is never required, and the set of called $`\mathrm{𝑝𝑎𝑡ℎ}`$-atoms is finite since the database of facts comprising $`P^{^{}}`$ is finite.

Finally, we consider the case of two programs $`P_1`$ and $`P_2`$ extending each other.

###### Proposition 5

Let $`P_1,P_2`$ be two programs such that $`P_1`$ extends $`P_2`$ and $`P_2`$ extends $`P_1`$. Let $`SB_{P_1P_2}^E`$. If

* $`P_1`$ quasi-terminates w.r.t. $`Tab_{P_1}`$ and $`SB_{P_1}^E`$,
* $`P_2`$ quasi-terminates w.r.t. $`Tab_{P_2}`$ and $`SB_{P_2}^E`$,

then $`P_1P_2`$ quasi-terminates w.r.t. $`Tab_{P_1P_2}`$ and $`S`$.

###### Proof

Because $`P_1`$ extends $`P_2`$ and $`P_2`$ extends $`P_1`$, $`Call(P_1P_2,S)B_{P_i}^E=Call(P_i,SB_{P_i}^E)`$ for $`i=1,2`$. The proposition follows then by definition of quasi-termination. ∎

###### Example 18 (Example 15 continued)

We prove that $`(PR)P^{^{}}`$ quasi-terminates w.r.t. $`\{\mathrm{𝑝𝑎𝑡ℎ}/4\}`$ and $`Call(U,S)B_{PRP^{^{}}}^E`$, given that $`PR`$ quasi-terminates w.r.t. $`\{\mathrm{𝑝𝑎𝑡ℎ}/4\}`$ and $`Call(U,S)B_{PR}^E`$ (which was shown in Example 17) and that $`P^{^{}}`$ quasi-terminates w.r.t. $`\{\mathrm{𝑝𝑎𝑡ℎ}/4\}`$ and $`Call(U,S)B_P^{^{}}^E`$ (which is obvious since it consists of a finite set of facts). We can apply Proposition 5, since $`PR`$ extends $`P^{^{}}`$ and vice versa, $`P^{^{}}`$ extends $`PR`$.

The above modular conditions for the quasi-termination of $`PR`$ are proven to be sufficient, but in many cases they are also necessary. In particular, the modular conditions of Proposition 3 are also necessary for the quasi-termination of $`PR`$ in case the tabling $`Tab_P`$ is well-chosen w.r.t. $`P`$. Because in all cases where $`Def_PTab_P`$, $`Tab_P`$ is well-chosen w.r.t. $`P`$, it follows that the modular conditions of Proposition 4 are necessary in general. Finally, it can be easily seen that the modular conditions of Proposition 5 are also necessary in general.

Note that all the above modular termination conditions prove the quasi-termination of $`PR`$ without constructing a level mapping $`|.|`$ such that $`PR`$ is quasi-acceptable w.r.t. this level mapping. In Subsection 6.3, modular termination conditions for quasi-termination are given which construct (from simpler level mappings) a level mapping such that $`PR`$ is quasi-acceptable w.r.t. this level mapping. This construction will be illustrated in Example 20 on the program $`PR`$ of Example 15 (see also Example 17).

### 6.2 Modular Conditions for LG-Termination

Similarly to the case of quasi-termination, we want to have modular termination proofs for the LG-termination of the union $`PR`$ of two programs $`P`$ and $`R`$, where $`P`$ extends $`R`$. Note that, because of Theorem 4.1 and because $`(PR)^a=`$ $`P^aR^a`$ (if $`P`$ extends $`R`$), we can use the modular proofs for quasi-termination of Subsection 6.1. However, as we already noted in Subsection 5.2, we can give simpler conditions which require less checks of decreases between the levels of successive calls. These conditions are given below.

###### Proposition 6

Let $`P`$ and $`R`$ be two programs, such that $`P`$ extends $`R`$. Let $`SB_{PR}^E`$. If

* $`R`$ LG-terminates w.r.t. $`Tab_R`$ and $`Call(PR,S)`$, and
* there is a level mapping $`|.|`$ on $`B_{P^a}^E`$ such that for all $`A`$ such that $`\stackrel{~}{A}S`$, $`|.|`$ is finitely partitioning on $`Call(P^aR,\{A\})B_{TR_PTR_P^a}^E`$, and such that

+ for every atom $`A`$ such that $`\stackrel{~}{A}Call(P^aR,S)`$,
    + for every clause $`HB_1,\mathrm{},B_n`$ in $`P^a`$ such that $`mgu(A,H)=\theta `$ exists,
    + for every $`B_i`$ such that $`Rel(B_i)Rel(H)`$ or $`Rel(B_i)TR_P^a`$,
    + for every $`cas`$ $`\theta _{i1}`$ in $`P^aR`$ for $`(B_1,\mathrm{},B_{i1})\theta `$:

    $$\begin{array}{cc}|A||B_i\theta \theta _{i1}|\hfill & \\ \text{and}\hfill & \\ |A|>|B_i\theta \theta _{i1}|\hfill & \text{if}Rel(A)Rel(B_i)NTab_P\text{and}\hfill \\ & C_2(Rel(A),Rel(B_i))\text{does not hold}.\hfill \end{array}$$

then $`PR`$ LG-terminates w.r.t. $`Tab_{PR}`$ and $`S`$.

###### Proof

The proof is a simple adaptation of the proof of the if-direction of Theorem 5.2; the adaptation is similar to the adaptation needed to transform the proof of the if-direction of Theorem 5.1 into a proof of Proposition 3. ∎

We next consider three special cases of Proposition 6. In the following proposition we consider the case in which no defined predicate in $`P`$ is tabled.

###### Proposition 7

Let $`P`$ and $`R`$ be two programs, such that $`P`$ extends $`R`$ and such that $`\mathrm{𝐷𝑒𝑓}_P\mathrm{𝑁𝑇𝑎𝑏}_P`$. Let $`SB_{PR}^E`$. If

* $`R`$ LG-terminates w.r.t. $`\mathrm{𝑇𝑎𝑏}_R`$ and $`Call(PR,S)`$,
* there is a level mapping $`|.|`$ on $`B_P^E`$ such that

+ for every atom $`A`$ such that $`\stackrel{~}{A}Call(PR,S)`$,
    + for every clause $`HB_1,\mathrm{},B_n`$ in $`P`$ such that $`mgu(A,H)=\theta `$ exists,
    + for every $`B_i`$ such that $`Rel(B_i)Rel(A)`$,
    + for every $`cas`$ $`\theta _{i1}`$ in $`PR`$ for $`(B_1,\mathrm{},B_{i1})\theta `$:

    $$|A|>|B_i\theta \theta _{i1}|$$

then $`PR`$ LG-terminates w.r.t. $`\mathrm{𝑇𝑎𝑏}_{PR}`$ and $`S`$.

###### Proof

Because no defined predicate in $`P`$ is tabled, $`P^a=P`$. Also, for all $`p,q\mathrm{𝑁𝑇𝑎𝑏}_P\mathrm{𝐷𝑒𝑓}_P`$ with $`pq`$, $`C_1(p,q)`$ holds. The proposition follows then from Proposition 6. ∎

###### Example 19

Recall program $`R`$ of Example 14. Let $`P`$ be the following program which parses the language $`a^nb`$ (see also Subsection 3.1):

$$\begin{array}{cc}P:\hfill & \{\begin{array}{ccc}s(\mathrm{𝑆𝑖},\mathrm{𝑆𝑜},\mathrm{𝑃𝑇})\hfill & \hfill & a(\mathrm{𝑆𝑖},S),S=[b|\mathrm{𝑆𝑜}],\mathrm{𝑃𝑇}=\mathrm{𝑠𝑝𝑡}(\mathrm{𝑃𝑇𝑎},b),a(\mathrm{𝑆𝑖},S,\mathrm{𝑃𝑇𝑎})\hfill \\ a(\mathrm{𝑆𝑖},\mathrm{𝑆𝑜},\mathrm{𝑃𝑇})\hfill & \hfill & a(\mathrm{𝑆𝑖},S),a(S,\mathrm{𝑆𝑜}),\mathrm{𝑃𝑇}=\mathrm{𝑎𝑝𝑡}(\mathit{PT1},\mathit{PT2}),a(\mathrm{𝑆𝑖},S,\mathit{PT1}),\hfill \\ & & a(S,\mathrm{𝑆𝑜},\mathit{PT2})\hfill \\ a(\mathrm{𝑆𝑖},\mathrm{𝑆𝑜},\mathrm{𝑃𝑇})\hfill & \hfill & \mathrm{𝑆𝑖}=[a|\mathrm{𝑆𝑜}],\mathrm{𝑃𝑇}=a\hfill \end{array}\hfill \end{array}$$

As already noted, $`P`$ extends $`R`$. Let $`a/2`$ be the only tabled predicate in $`PR`$; see Subsection 3.1 for why this tabling is sufficient. Let $`S=\{s(\mathrm{𝑠𝑖},\mathrm{𝑆𝑜},\mathrm{𝑃𝑇})|si`$ is a ground list consisting of constants $`a,b`$, and $`So,PT`$ are distinct variables$`\}`$. We show, using Proposition 7, that $`PR`$ LG-terminates w.r.t. $`\{a/2\}`$ and $`S`$.

* $`R`$ LG-terminates w.r.t. $`\{a/2\}`$ and $`Call(PR,S)`$.

Note that, if $`a(t1,t2)Call(PR,s(si,So,PT))`$, then either $`t1`$ is a sublist of $`si`$ and $`t2`$ is a variable, or $`t1`$ and $`t2`$ are both sublists of $`si`$. In Example 14, we proved that $`R`$ LG-terminates w.r.t. this first kind of queries. To prove that $`R`$ LG-terminates w.r.t. the second kind of queries, we can again apply Theorem 5.2. Since the proof is similar to the one given in Example 14, we omit it here.
* Note first that, if $`a(t1,t2,P)Call(PR,\{s(si,So,PT)\})`$, then $`t2`$ is a (strict) sublist of $`t1`$, $`t1`$ is a sublist of $`si`$ and $`P`$ is a variable. Let $`|.|`$ be the following level mapping on $`Call(PR,S)B_{\{a/3\}}^E`$: $`|a(t1,t2,P)|=t1_lt2_l`$. Because of the remark above, $`|.|`$ is well-defined. Note that we only have to consider the recursive clause for $`a/3`$ in the analysis.

+ First consider the fourth body atom in the recursive clause for $`a/3`$. If this clause is called with $`a(ti,to,PT)`$, with $`to`$ a (strict) sublist of $`ti`$, then the fourth body atom is called as $`a(ti,t,PT1)`$ where $`to`$ is a (strict) sublist of $`t`$ and $`t`$ is a (strict) sublist of $`ti`$. Hence,

    $$|a(ti,to,PT)|=ti_lto_l>ti_lt_l=|a(ti,t,PT1)|.$$
    + Now consider the last body atom. If the recursive clause is called with $`a(ti,to,PT)`$, with $`to`$ a (strict) sublist of $`ti`$, then the last body atom is called as $`a(t,to,PT2)`$ where $`to`$ is a (strict) sublist of $`t`$ and $`t`$ is a (strict) sublist of $`ti`$. Hence,

    $$|a(ti,to,PT)|=ti_lto_l>t_lto_l=|a(t,to,PT2)|.$$

We conclude that $`PR`$ and $`S`$ satisfy the condition of Proposition 7, so $`PR`$ LG-terminates w.r.t. $`\{a/2\}`$ and $`S`$.

In the next proposition, a modular termination proof for the LG-termination of the union $`PR`$ is given, where $`P`$ extends $`R`$ and all defined predicates in $`P`$ are tabled.

###### Proposition 8

Let $`P`$ and $`R`$ be two programs, such that $`P`$ extends $`R`$ and such that $`\mathrm{𝐷𝑒𝑓}_P\mathrm{𝑇𝑎𝑏}_P`$. Let $`SB_{PR}^E`$. If

* $`R`$ LG-terminates w.r.t. $`Tab_R`$ and $`Call(PR,S)`$, and
* there is a level mapping $`|.|`$ on $`B_{P^a}^E`$ such that for all $`A`$ such that $`\stackrel{~}{A}S`$, $`|.|`$ is finitely partitioning on $`Call(P^aR,\{A\})B_{TR_PTR_P^a}^E`$, and such that

+ for every atom $`A`$ such that $`\stackrel{~}{A}Call(P^aR,S)`$,
    + for every clause $`HB_1,\mathrm{},B_n`$ in $`P^a`$ such that $`mgu(A,H)=\theta `$ exists,
    + for every $`B_i`$ such that $`Rel(B_i)Rel(H)`$ or $`Rel(B_i)TR_P^a`$,
    + for every $`cas`$ $`\theta _{i1}`$ in $`P^aR`$ for $`(B_1,\mathrm{},B_{i1})\theta `$:

    $$|A||B_i\theta \theta _{i1}|$$

then $`PR`$ LG-terminates w.r.t. $`\mathrm{𝑇𝑎𝑏}_{PR}`$ and $`S`$.

###### Proof

This is a direct corollary of Proposition 6 (every recursive predicate in $`P`$ is defined in $`P`$ and hence tabled). ∎

Finally, we consider the case of two programs $`P_1`$ and $`P_2`$ extending each other.

###### Proposition 9

Let $`P_1,P_2`$ be two programs such that $`P_1`$ extends $`P_2`$ and $`P_2`$ extends $`P_1`$. Let $`SB_{P_1P_2}^E`$. If

* $`P_1`$ LG-terminates w.r.t. $`Tab_{P_1}`$ and $`SB_{P_1}^E`$,
* $`P_2`$ LG-terminates w.r.t. $`Tab_{P_2}`$ and $`SB_{P_2}^E`$,

then $`P_1P_2`$ LG-terminates w.r.t. $`Tab_{P_1P_2}`$ and $`S`$.

###### Proof

Because $`P_1`$ extends $`P_2`$ and $`P_2`$ extends $`P_1`$, $`Call(P_1P_2,S)B_{P_i}^E=Call(P_i,SB_{P_i}^E)`$, for $`i=1,2`$. The proposition follows then by definition of LG-termination. ∎

Similar as in the case of quasi-termination, the above modular, sufficient conditions for the LG-termination of $`PR`$ are in many cases also necessary. In particular, the modular conditions of Proposition 6 are also necessary for the LG-termination of $`PR`$ in case the tabling $`Tab_P`$ is well-chosen w.r.t. $`P`$. Because in all cases where $`Def_PNTab_P`$, respectively $`Def_PTab_P`$, $`Tab_P`$ is well-chosen w.r.t. $`P`$, it follows that the modular conditions of Proposition 7, respectively Proposition 8, are necessary in general. Also the modular conditions of Propositions 9 are necessary in general.

### 6.3 Construction of Level Mappings in Modular Termination Proofs

We now take a closer look at the modular termination proofs of the previous subsections. We follow the approach of , where modular proofs for SLD-termination (i.e. termination of SLD-resolution w.r.t. all selection rules) and LD-termination are given. In , (S)LD-termination of a program $`PR`$, where $`P`$ extends $`R`$, is proven by constructing a level mapping $`|.|`$ for $`PR`$ which satisfies some acceptability condition. The level mapping $`|.|`$ is constructed from simpler level mappings for the separate components $`P`$ and $`R`$. Namely, $`|.|`$ is constructed from $`|.|_P`$, $`|.|_R`$ and $`._P`$, where $`|.|_P`$, respectively $`|.|_R`$, is a level mapping for $`P`$, respectively $`R`$, satisfying the acceptability condition, and where $`._P`$ is a level mapping for $`P`$ serving as the connecting part between the two components. The level mapping $`|.|`$ for $`PR`$ is then defined as $`|.|_P+._P`$ on the atoms defined in $`P`$ and as $`|.|_R`$ on the atoms defined in $`R`$. It is proven that such a construction always returns a level mapping satisfying the acceptability condition for the whole program $`PR`$.

We follow the same approach for the case of quasi-termination (we do not consider LG-termination since it can be dealt with in a similar way). In particular, we give modular proofs of the quasi-termination of a program $`PR`$, where $`P`$ extends $`R`$, by constructing a level mapping such that $`PR`$ is quasi-acceptable w.r.t. this level mapping (see Definition 14 and Theorem 5.1). The construction of such a level mapping is done in a way similar to , which we explained above. We first need the following lemma, which gives sufficient, modular conditions on a level mapping in order to be finitely partitioning on some subset of the extended Herbrand base. We use a slightly more general definition of a level mapping, namely, a level mapping is a mapping from a *subset* of the extended Herbrand base to the natural numbers.

###### Lemma 7

1. Let $`P`$ be a program and $`LB_P^E`$. Let $`|.|,.:L\mathrm{}`$ be level mappings. If $`|.|`$ is finitely partitioning on $`CL`$, then $`|.|+.:L\mathrm{}:A(|.|+.)(A)=|A|+A`$ is finitely partitioning on $`C`$.
2. Let $`P_1,P_2`$ be two programs and $`L_1B_{P_1}^E`$, $`L_2B_{P_2}^E`$. Let $`|.|_1:L_1\mathrm{}`$ and $`|.|_2:L_2\mathrm{}`$ be level mappings. If $`|.|_1`$, respectively $`|.|_2`$, is finitely partitioning on $`C_1L_1`$, respectively $`C_2L_2`$, then $`m(|.|_1,|.|_2):L_1L_2\mathrm{}:`$

$$Am(|.|_1,|.|_2)(A):=\{\begin{array}{ccc}min(|A|_1,|A|_2)\hfill & ,\hfill & AL_1L_2\hfill \\ |A|_1\hfill & ,\hfill & AL_1L_2\hfill \\ |A|_2\hfill & ,\hfill & AL_2L_1\hfill \end{array}$$

is finitely partitioning on $`C_1C_2`$.

###### Proof

1. Let $`n\mathrm{}`$. We prove that $`\mathrm{}((|.|+.)^1(n)C)<\mathrm{}`$.

$$\begin{array}{ccc}(|.|+.)^1(n)C\hfill & =\hfill & \{AC|(|.|+.)(A)=n\}\hfill \\ & \hfill & \{AC||A|n\}\hfill \\ & =\hfill & _{0mn}\{AC||A|=m\}\hfill \end{array}$$

and this last set is finite.
2. Let $`n\mathrm{}`$. We prove that $`\mathrm{}(m(|.|_1,|.|_2)^1(n)`$ $``$ $`(C_1C_2))<`$ $`\mathrm{}`$.

$$\begin{array}{ccc}m(|.|_1,|.|_2)^1(n)(C_1C_2)\hfill & =\hfill & \{AC_1C_2|m(|.|_1,|.|_2)(A)=n\}\hfill \\ & =\hfill & \{AC_1C_2||A|_1=n\}\hfill \\ & & \{AC_2C_2||A|_2=n\}\hfill \\ & & \{AC_1C_2|min(|A|_1,|A|_2)=n\}\hfill \end{array}$$

It is obvious that the first two sets in the union are finite ($`|.|_1`$, resp. $`|.|_2`$, is finitely partitioning on $`C_1`$, resp. $`C_2`$). The set $`\{AC_1C_2|min(|A|_1,|A|_2)`$ $`=n\}`$ is finite, because it is a subset of the finite set $`\{AC_1C_2||A|_1=n\}`$ $``$ $`\{AC_1C_2||A|_2=n\}`$.

We next give a modular termination condition for the quasi-termination of $`PR`$ where $`P`$ extends $`R`$, by constructing a level mapping, from simpler ones, such that $`PR`$ is quasi-acceptable w.r.t. this level mapping. Notice that we consider the same case as in Proposition 3.

###### Proposition 10

Let $`P`$ and $`R`$ be two programs such that $`P`$ extends $`R`$ and let $`SB_{PR}^E`$. If

1. $`R`$ is quasi-acceptable w.r.t. $`Tab_R`$, $`Call(PR,S)`$ and the level mapping $`|.|_R`$, defined on $`B_R^E`$ and finitely partitioning on $`Call(PR,S)B_{Tab_R}^E`$,
2. there is a level mapping $`|.|_P`$ defined on $`B_P^EB_R^E`$ and finitely partitioning on $`Call(PR,S)B_{Tab_PTab_R}^E`$ such that

* for every atom $`A`$ such that $`\stackrel{~}{A}Call(PR,S)`$,
    * for every clause $`HB_1,\mathrm{},B_n`$ in $`P`$, such that $`mgu(A,H)=\theta `$ exists,
    * for every $`1in`$ such that $`B_iB_P^EB_R^E`$,
    * for every $`cas`$ $`\theta _{i1}`$ in $`PR`$ for $`(B_1,\mathrm{},B_{i1})\theta `$:

    $$\begin{array}{cc}|A|_P|B_i\theta \theta _{i1}|_P\hfill & \\ \text{and}\hfill & \\ |A|_P>|B_i\theta \theta _{i1}|_P\hfill & \text{if}Rel(A)Rel(B_i)NTab_P\text{and}\hfill \\ & C_2(Rel(A),Rel(B_i))\text{does not hold}.\hfill \end{array}$$
3. there exists a level mapping $`._P`$ on $`B_P^EB_R^E`$ such that

* for every atom $`A`$ such that $`\stackrel{~}{A}Call(PR,S)`$,
    * for every clause $`HB_1,\mathrm{},B_n`$ in $`P`$, such that $`mgu(A,H)=\theta `$ exists,
    * for every $`1in`$,
    * for every $`cas`$ $`\theta _{i1}`$ in $`PR`$ for $`(B_1,\mathrm{},B_{i1})\theta `$:
$$A_P\{\begin{array}{ccc}B_i\theta \theta _{i1}_P\hfill & ,\hfill & B_i\theta \theta _{i1}B_P^EB_R^E\hfill \\ |B_i\theta \theta _{i1}|_R\hfill & ,\hfill & B_i\theta \theta _{i1}B_R^E\hfill \end{array}$$

then, the following level mapping $`|.|`$, defined on $`B_{PR}^E`$, is finitely partitioning on $`Call(PR,S)B_{Tab_{PR}}^E`$:

$$|A|=\{\begin{array}{cc}|A|_P+A_P\hfill & \text{if}AB_P^EB_R^E,\hfill \\ |A|_R\hfill & \text{if}AB_R^E,\hfill \end{array}$$

and $`PR`$ is quasi-acceptable w.r.t. $`Tab_{PR}`$, $`S`$, and the level mapping $`|.|`$. Hence, $`PR`$ quasi-terminates w.r.t. $`Tab_{PR}`$ and $`S`$.

###### Proof

Because of Lemma 7(1,2), the level mapping $`|.|`$ is finitely partitioning on $`Call(PR,S)B_{Tab_{PR}}^E`$. We prove that $`PR`$ is quasi-acceptable w.r.t. $`Tab_{PR}`$, $`S`$ and the level mapping $`|.|`$ (see Definition 14).
Let $`A`$ be an atom such that $`\stackrel{~}{A}Call(PR,S)`$. Let $`HB_1,\mathrm{},B_n`$ be a clause of $`PR`$ such that $`mgu(A,H)=\theta `$ exists. Let $`\theta _{i1}`$ be a $`cas`$ in $`PR`$ for $`(B_1,\mathrm{},B_{i1})\theta `$. There are two cases to consider:

* $`A`$ is defined in $`R`$, $`|A|=|A|_R`$.

Then, because of condition 1 in the proposition statement, $`|A|_R|B_i\theta \theta _{i1}|_R`$ (note that because $`P`$ extends $`R`$, for a clause $`HB_1,\mathrm{},B_n`$ in $`R`$ and $`mgu(A,H)=\theta `$, a $`cas`$ for $`(B_1,\mathrm{},B_{i1})\theta `$ in $`PR`$ is the same as a $`cas`$ for $`(B_1,\mathrm{},B_{i1})\theta `$ in $`R`$ only). Since $`A,B_i\theta \theta _{i1}`$ $`B_R^E`$, $`|A|=|A|_R`$ $`|B_i\theta \theta _{i1}|_R=|B_i\theta \theta _{i1}|`$. In case $`Rel(A)Rel(B_i)NTab_R`$ and $`C_2(Rel(A),Rel(B_i))`$ does not hold, $`|A|=|A|_R>`$ $`|B_i\theta \theta _{i1}|_R=|B_i\theta \theta _{i1}|`$.
* $`A`$ is defined in $`P`$, $`|A|=|A|_P+A_P`$.

+ $`B_iB_R^E`$, $`|B_i\theta \theta _{i1}|=|B_i\theta \theta _{i1}|_R`$.

    Because of condition 3 in the proposition statement, $`A_P`$ $``$ $`|B_i\theta \theta _{i1}|_R`$. Hence, $`|A|=`$ $`|A|_P+A_P`$ $``$ $`|B_i\theta \theta _{i1}|_R`$ $`=|B_i\theta \theta _{i1}|`$.

    Note that in this case we always have that $`Rel(A)\simeq ̸Rel(B_i)`$ (because $`P`$ extends $`R`$).
    + $`B_iB_P^EB_R^E`$, $`|B_i\theta \theta _{i1}|=`$ $`|B_i\theta \theta _{i1}|_P+`$ $`B_i\theta \theta _{i1}_P`$.

    Because of condition 2 in the proposition statement, $`|A|_P`$ $``$ $`|B_i\theta \theta _{i1}|_P`$. Also, because of condition 3, $`A_P`$ $``$ $`B_i\theta \theta _{i1}_P`$. Hence, $`|A|_P+`$ $`A_P`$ $``$ $`|B_i\theta \theta _{i1}|_P+`$ $`B_i\theta \theta _{i1}_P`$. In case $`Rel(A)Rel(B_i)NTab_P`$ and $`C_2(Rel(A),Rel(B_i))`$ does not hold, we have that $`|A|_P`$ $`>`$ $`|B_i\theta \theta _{i1}|_P`$, hence $`|A|_P+A_P`$ $`>`$ $`|B_i\theta \theta _{i1}|_P+`$ $`B_i\theta \theta _{i1}_P`$.

In each case, we conclude that $`|A|`$ $``$ $`|B_i\theta \theta _{i1}|`$ and that, in case $`Rel(A)Rel(B_i)NTab_{PR}`$ and $`C_2(Rel(A),Rel(B_i))`$ does not hold, $`|A|`$ $`>`$ $`|B_i\theta \theta _{i1}|`$. ∎

###### Example 20

Recall the program $`PR`$ of Example 15 (see also Examples 17 and 1).

$$\begin{array}{cc}P:\hfill & \{\begin{array}{ccc}path(X,Ed,Y,[Y])\hfill & \hfill & edge(X,Ed,Y)\hfill \\ path(X,Ed,Z,[Y|L])\hfill & \hfill & edge(X,Ed,Y),path(Y,Ed,Z,L)\hfill \end{array}\hfill \\ & \\ R:\hfill & \{\begin{array}{ccc}edge(X,[e(X,Y)|L],Y)\hfill & \hfill & \\ edge(X,[e(X_1,X_2)|L],Y)\hfill & \hfill & edge(X,L,Y)\hfill \end{array}\hfill \end{array}$$

Let $`Tab_{PR}=\{\mathrm{𝑝𝑎𝑡ℎ}/4\}`$ and $`S=\{\mathrm{𝑝𝑎𝑡ℎ}(a,[e(a,b),e(b,a)],Y,L)\}`$. We prove that $`PR`$ quasi-terminates w.r.t. $`\{\mathrm{𝑝𝑎𝑡ℎ}/4\}`$ and $`S`$ using the above proposition. The first two conditions of this proposition were already tackled in Example 17 (using a different set $`S`$; the arguments remain the same however).

1. $`R`$ is quasi-acceptable w.r.t. $`\mathrm{}`$, $`Call(PR,S)`$ and the level mapping $`|.|_R`$:

$$|edge(t_1,t_2,t_3)|_R=t_2_l.$$
2. The trivial level mapping, $`|.|_P`$, on $`B_P^EB_R^E=B_{\{\mathrm{𝑝𝑎𝑡ℎ}\}}^E`$ satisfies the second condition of the proposition; $`\mathrm{𝑝𝑎𝑡ℎ}/4`$ is tabled so a strict decrease is never required and there are a finite number of $`\mathrm{𝑝𝑎𝑡ℎ}`$-atoms in the call set.
3. The following level mapping, $`._P`$, on $`B_P^EB_R^E=B_{\{\mathrm{𝑝𝑎𝑡ℎ}\}}^E`$ satisfies the third condition of the proposition:

$$path(t_1,t_2,t_3,t_4)_P=t_2_l.$$

Hence, the level mapping $`|.|`$, on $`B_{PR}^E`$, is defined as follows:

$$\begin{array}{ccc}|path(t_1,t_2,t_3,t_4)|\hfill & =\hfill & t_2_l\hfill \\ |edge(t_1,t_2,t_3)|\hfill & =\hfill & t_2_l\hfill \end{array}$$

and by the proposition, the program $`PR`$ is quasi-acceptable w.r.t. $`Tab_{PR}=\{\mathrm{𝑝𝑎𝑡ℎ}/4\}`$, $`S=\{\mathrm{𝑝𝑎𝑡ℎ}(a,[e(a,b),e(b,a)],Y,L)\}`$ and the level mapping $`|.|`$.

In  the special case where two programs use disjoint sets of predicates is also considered. We next consider a more general case in which the two programs may use the same predicates but may not define the same predicate; that is, both programs extend each other. This case was already considered in Proposition 5.

###### Proposition 11

Let $`P_1,P_2`$ be programs such that $`P_1`$ extends $`P_2`$ and $`P_2`$ extends $`P_1`$. Let $`SB_{P_1P_2}^E`$. Suppose that

1. $`P_1`$ is quasi-acceptable w.r.t. $`Tab_{P_1}`$, $`SB_{P_1}^E`$ and a level mapping $`|.|_1`$ on $`B_{P_1}^E`$ which is finitely partitioning on $`Call(P_1,SB_{P_1}^E)B_{Tab_{P_1}}^E`$,
2. $`P_2`$ is quasi-acceptable w.r.t. $`Tab_{P_2}`$, $`SB_{P_2}^E`$ and a level mapping $`|.|_2`$ on $`B_{P_2}^E`$ which is finitely partitioning on $`Call(P_2,SB_{P_2}^E)B_{Tab_{P_2}}^E`$,

then $`m(|.|_1,|.|_2)`$ (see Lemma 7, point 2) is a finitely partitioning level mapping on $`Call(P_1P_2,S)B_{Tab_{P_1P_2}}^E`$, and $`P_1P_2`$ is quasi-acceptable w.r.t. $`Tab_{P_1P_2}`$, $`S`$ and $`m(|.|_1,|.|_2)`$. Hence, $`P_1P_2`$ quasi-terminates w.r.t. $`Tab_{P_1P_2}`$ and $`S`$.

###### Proof

Note that because $`P_1`$ extends $`P_2`$ and vice versa, $`Call(P_1P_2,S)B_{P_i}^E`$ $`=`$ $`Call(P_i,SB_{P_i}^E)`$, $`i\{1,2\}`$. By Lemma 7(2), $`m(|.|_1,|.|_2)`$ is finitely partitioning on $`Call(P_1P_2,S)B_{Tab_{P_1P_2}}^E`$. Also, if $`HB_1,\mathrm{},B_n`$ is a clause in $`P_i`$ and $`mgu(A,H)=\theta `$, then a $`cas`$ in $`P_i`$ for $`(B_1,\mathrm{},B_{i1})\theta `$ is a $`cas`$ in $`P_iP_j`$ ($`\{i,j\}=\{1,2\}`$) for $`(B_1,\mathrm{},B_{i1})\theta `$ and vice versa. Then it directly follows that $`P_1P_2`$ is quasi-acceptable w.r.t. $`Tab_{P_1P_2}`$, $`S`$ and $`m(|.|_1,|.|_2)`$. ∎

As was shown in  for the case of (S)LD-termination, the above modular conditions provide us with an incremental, bottom up method for proving termination of tabled logic programs.

## 7 Towards Automated Termination Proofs for Tabled Logic Programs

Having described the basic framework for proving termination of tabled logic programs, in this section we examine issues related to the automation of the termination conditions. We will only consider quasi-termination in this section; the results for LG-termination carry over in the same way. We show how to extend the constraint-based, automatic approach for proving LD-termination of Decorte, De Schreye and Vandecasteele , in order to prove quasi-termination of tabled logic programs in an automatic way. Our results hold for the class of simply moded, well-moded programs and queries.

We first recall the main ideas of . In , a new strategy for automatically proving LD-termination of logic programs w.r.t. sets of queries is developed. A symbolic termination condition is introduced, called *rigid acceptability*, by parametrising the concepts of norm, level mapping and model. The rigid acceptability condition is translated into a system of constraints on the values of the introduced symbols only. A system of constraints identifies sets of suitable norms, level mappings and models which can be used in the termination condition. In other words, if a solution for the constraint system exists, termination can be proved. The solving of constraint sets enables the different components of a termination proof to communicate with one another and to direct the proof towards success (if there is). The method of  is both efficient and precise.

This section<sup>4</sup><sup>4</sup>4For the referees, we want to mention that the material presented in this section is very related to results presented in the article “Termination of simply moded well-typed programs under a tabled execution mechanism” (S. Verbaeten and D. De Schreye), which is submitted to the Journal of Applicable Algebra in Engineering, Communication and Computing (AAECC). There, we present similar results for the special case in which all the predicates of the program are tabled. Here we extend those results to allow a mix of tabled and non-tabled predicates. In spite of the partial overlap with this article, we choose to include this section anyway, because we feel that it is important to explicitly discuss the prospects of automating the results presented in the previous sections. In the article submitted to AAECC, we consider the larger class of simply moded well-typed programs, instead of simply moded well-moded programs that we consider here. With respect to the automation of the method, this larger class adds nothing new, so we do not include these results here. If the referees think this section should not be included, we are willing to remove it. is structured as follows. We first reformulate the quasi-acceptability condition into a condition at the clause level, which is needed in the constraint-based termination analysis framework of . This gives us the rigid quasi-acceptability condition. Then, we recall the symbolic forms for norm, level mapping and model, as introduced in . We introduce the class of simply moded, well-moded programs and queries, for which we translate the rigid quasi-acceptability condition into a system of constraints on the introduced symbols.

### 7.1 Rigid Quasi-Acceptability Condition

In order to prove termination in an automatic, constraint-based way as in , it is important to have a termination condition which is stated at the clause level (and not on sets of calls as the quasi-acceptability condition of Theorem 5.1 is). In most automatic approaches, and in particular in that of , this is obtained by requiring that the level mapping is *rigid* on the call set. A level mapping is rigid on the call set iff the value of an atom in the call set is invariant under substitutions. If a level mapping is rigid on the call set, the atoms in the call set can be considered as ground w.r.t. the level mapping. In this way, the problem of back-propagation of bindings in the calls is dealt with, and this allows the termination condition to be stated at the clause level (see also ).

###### Definition 16 (rigid level mapping)

Let $`P`$ be a program and $`CB_P^E`$. A level mapping $`|.|`$ is rigid on $`C`$ iff for all atoms $`AC`$, for all substitutions $`\psi `$, $`|A|=|A\psi |`$.

The following condition of rigid quasi-acceptability is derived from the quasi-acceptability condition, and will serve as the basis for a constraint-based, symbolic condition for quasi-termination.

###### Proposition 12 (rigid quasi-acceptability condition)

Let $`P`$ be definite program, $`Tab_PPred_P`$ and $`SB_P^E`$ be a set of queries.
If there exists a level mapping $`|.|`$, such that $`|.|`$ is rigid on $`Call(P,S)`$, $`|.|`$ is finitely partitioning on $`Call(P,S)B_{Tab_P}^E`$, and such that

* for every clause $`HB_1,\mathrm{},B_n`$ in $`P`$,
* for every atom $`B_i`$, $`i\{1,..,n\}`$,
* for every substitution $`\psi `$ such that $`PB_1\psi ,\mathrm{},B_{i1}\psi `$:

$$\begin{array}{cc}|H\psi ||B_i\psi |\hfill & \\ \text{and}\hfill & \\ |H\psi |>|B_i\psi |\hfill & \text{if}Rel(H)Rel(B_i)NTab_P\text{and}\hfill \\ & C_2(Rel(A),Rel(B_i))\text{does not hold},\hfill \end{array}$$

then $`P`$ is quasi-acceptable w.r.t. $`Tab_P`$ and $`S`$. Hence, $`P`$ quasi-terminates w.r.t. $`Tab_P`$ and $`S`$.

###### Proof

Suppose the above condition is satisfied for $`P`$. We prove that $`P`$ is quasi-acceptable w.r.t. $`Tab_P`$, $`S`$ and the level mapping $`|.|`$. Let $`A`$ be an atom such that $`\stackrel{~}{A}Call(P,S)`$. Let $`HB_1,\mathrm{},B_n`$ be a clause in $`P`$ such that $`mgu(A,H)=\theta `$ exists. Let $`\theta _{i1}`$ be an LD- computed answer substitution for $`(B_1,\mathrm{},B_{i1})\theta `$. Then, $`PB_1\theta \theta _{i1},\mathrm{},B_{i1}\theta \theta _{i1}`$. We prove that $`|A||B_i\theta \theta _{i1}|`$. By the condition in the proposition, we know that $`|H\theta \theta _{i1}||B\theta \theta _{i1}|`$. Because $`A\theta =H\theta `$, $`|A\theta \theta _{i1}|=|H\theta \theta _{i1}|`$. Now, since $`ACall(P,S)`$ and $`|.|`$ is rigid on $`Call(P,S)`$, $`|A\theta \theta _{i1}|=|A|`$. Thus, $`|A|=|H\theta \theta _{i1}|`$, and therefore $`|A||B_i\theta \theta _{i1}|`$. The proof that $`|A|>|B_i\theta \theta _{i1}|`$ in case $`Rel(A)Rel(B_i)NTab_P`$ and $`C_2(Rel(A),Rel(B_i))`$ does not hold, is analogous. ∎

### 7.2 Symbolising the Concepts of Norm, Level Mapping and Model

We recall the symbolic forms for norms, level mappings and interargument relations (which are abstractions of models), as introduced in . These will form the basis for the symbolic termination condition. The condition will be formulated as a search for suitable values for all introduced symbols. We refer to  for motivation and more details.

We will need the following notions.

###### Definition 17 (functor, predicate and extended predicate coefficients)

The set of functor coefficients, respectively predicate coefficients, respectively extended predicate coefficients associated to a program $`P`$ are the sets of symbols

$$\begin{array}{c}FC(P)=\{f_i|f/nFun_Pi\{0,\mathrm{},n\}\},\hfill \\ PC(P)=\{p_i|p/nPred_Pi\{1,\mathrm{},n\}\},\hfill \\ EC(P)=\{p_i^e|p/nPred_Pi\{0,\mathrm{},n\}\}.\hfill \end{array}$$

Let $`𝒞`$ denote the set of symbols $`FC(P)PC(P)EC(P)`$. The symbol $`L_{<𝒞;+,.;>}`$ denotes the language containing the symbols in the set $`𝒞`$ as constants, the infix functor $`+/2`$, the infix functor $`./2`$, the relation symbol $`/2`$ and the set of variables in the first order language of the program $`P`$. Terms in that language are defined in the usual way. The relation symbols $`=/2`$ and $`</2`$ are defined in terms of $`/2`$ as usual and considered as additional primitive predicates. We denote the set of all possible atoms in that language by $`S_{<𝒞;+,.;>}`$. We call such atoms symbolic expressions. The set of all logical formulae over $`S_{<𝒞;+,.;>}`$ is denoted as $`F_{<𝒞;+,.;>}`$. Such formulae are called symbolic formulae. By natural formulae, we denote formulae in $`F_{<\{0,1\};+,.;>}`$. For a formula $`F`$, $`F`$ denotes the universal closure over the free variables occurring in $`F`$.

We introduce symbolic (semi-linear) norms.

###### Definition 18 (symbolic norm $`.^s`$)

Let $`FC(P)`$ be a set of functor coefficients.

$$\begin{array}{ccccc}.^s:\hfill & Term_P& \hfill & S_{<𝒞;+,.;>}& \\ & t& \hfill & f_0+_{i=1}^nf_it_i^s& \text{ if }t=f(t_1,\mathrm{},t_n),n>0,\hfill \\ & t& \hfill & 0& \text{ if }t=cConst_P,\hfill \\ & t& \hfill & X& \text{ if }t=X\text{ is a variable}.\hfill \end{array}$$

Note that the symbolic norm of a variable is the variable itself. So, the symbolic norm includes information about the instantiation level of the term. That is, the symbolic norm takes into account those parts of the concrete term whose size may still change under instantiation.

In the same way, we can define a symbolic level mapping by symbolising its coefficients.

###### Definition 19 (symbolic level mapping $`|.|^s`$)

Let $`PC(P)`$ be a set of predicate coefficients and $`.^s`$ a symbolic norm.

$$\begin{array}{cccc}|.|^s:\hfill & Atom_P& \hfill & S_{<𝒞;+,.;>}\hfill \\ & p(t_1,\mathrm{},t_n)& \hfill & _{i=1}^np_it_i^s.\hfill \end{array}$$

Finally, we want to abstract the notion of model. Norms allow to abstract models by specifying relations which hold between the size of certain arguments of their member atoms. This leads to the notion of interargument relation.

###### Definition 20 ((valid) interargument relation)

Let $`p/nPred_P`$.
An interargument relation for $`p/n`$ is a relation $`R^{p/n}\mathrm{}^n`$.
An interargument relation $`R^{p/n}`$ for $`p/n`$ is a valid interargument relation w.r.t. a norm $`.`$ iff $`p(t_1,\mathrm{},t_n)Atom_P`$: if $`Pp(t_1,\mathrm{},t_n)`$, then $`(t_1,\mathrm{},t_n)`$ $`R^{p/n}`$.

As in , we will allow interargument relations which express an inequality relation: $`R^{p/n}=\{(x_1,\mathrm{},x_n)|_{iI_p}k_ix_i_{jO_p}k_jx_j+k_0\}`$, with $`k_i\mathrm{}`$, $`i\{0,1,\mathrm{},n\}`$, depending on $`p/n`$, $`I_pO_p\{1,\mathrm{},n\}`$ and $`I_pO_p=\mathrm{}`$. The sets $`I_p`$ and $`O_p`$ are assumed fixed for each predicate. In , it is argued that these sets can best be seen as some kind of a generalisation of the sets of input and output arguments. In the following subsection, we will assume a fixed mode for each predicate, and then the set $`I_p`$, respectively $`O_p`$, will be taken as the set of input, respectively output, positions of the predicate $`p`$.

Finally, we can abstract success sets by abstracting interargument relations.

###### Definition 21 (symbolic size expression $`𝒜^s`$)

Let $`EC(P)`$ be a set of extended predicate coefficients and $`.^s`$ a symbolic norm.

$$\begin{array}{cccc}𝒜^s:\hfill & Atom_P& \hfill & S_{<𝒞;+;>}\hfill \\ & t_1=t_2& \hfill & t_1^s=t_2^s,\hfill \\ & p(t_1,\mathrm{},t_n)& \hfill & _{iI_p}p_i^et_i^s_{jO_p}p_j^et_j^s+p_0^e,\text{ where }p=.\hfill \end{array}$$

###### Definition 22 (symbol mapping)

A symbol mapping is a mapping $`s:𝒞\mathrm{}`$.

Expressions involving only symbols from $`𝒞`$ are mapped into the natural numbers by substituting the symbols by their mapped value. With abuse of notation, if $`F`$ is a symbolic formula and $`s`$ a symbol mapping, we denote the associated natural formula as $`s(F)`$.

Each symbol mapping induces in a natural way a norm, level mapping and interargument relations. More precisely, the symbol mapping $`s`$ induces the following norm $`._s`$:

$$\{\begin{array}{cc}X_s=c_s=0\hfill & \text{if}X\text{is a variable},cConst_P,\hfill \\ f(t_1,\mathrm{},t_n)_s=s(f_0)+_{i=1}^ns(f_i)t_i_s,\hfill & \end{array}$$

level mapping $`|.|_s`$:

$$|p(t_1,\mathrm{},t_n)|_s=\underset{i=1}{\overset{n}{}}s(p_i)t_i_s,$$

and interargument relations $`R_s^{p/n}`$:

$$\{(t_1_s),\mathrm{},t_n_s)|t_1,\mathrm{},t_nTerm_P\text{and}s(𝒜^s(p(t_1,\mathrm{},t_n)))\text{ holds }\}.$$

Our aim is to formulate the rigid quasi-acceptability condition in a constraint-based way. In particular this means that we have to find syntactical conditions on a symbol mapping $`s`$ such that

* $`|.|_s`$ is rigid on $`Call(P,S)`$, and
* $`|.|_s`$ is finitely partitioning on $`Call(P,S)B_{Tab_P}^E`$.

As shown in e.g. , a level mapping $`|.|_s`$ is rigid on the call set if it does not take into account too many argument positions of predicates and functors in its linear combination<sup>5</sup><sup>5</sup>5More precisely, $`s`$ is equal to $`0`$ on all functor and predicate coefficients for which there is an atom in the call set which contains a variable on such a position. For a more formal discussion on this topic, we refer to .. On the other hand, a level mapping $`|.|_s`$ is finitely partitioning on $`Call(P,S)B_{Tab_P}^E`$, if enough argument positions are taken into account in its linear combination<sup>6</sup><sup>6</sup>6That is, the symbol mapping $`s`$ is different from $`0`$ on enough functor and predicate coefficients. For a more formal discussion on this topic, we again refer to .. We will show that, for the class of simply moded, well-moded programs and queries, we are able to combine these two—at first sight contradictory— conditions. We first introduce the class of simply moded, well-moded programs and queries and then formulate, for this class, the symbolic termination condition.

### 7.3 The Class of Simply Moded, Well-Moded Programs and Queries

###### Definition 23 (mode for a predicate)

Let $`p`$ be an $`n`$-ary predicate symbol. A mode for $`p`$ is a function $`m_p:`$ $`\{1,\mathrm{},n\}`$ $`\{In,Out\}`$. If $`m_p(i)=In`$ (respectively $`Out`$), then we say that $`i`$ is an input (respectively output) position of $`p`$ (w.r.t. $`m_p`$).

We assume that each predicate symbol has a unique mode. For predicates that have multiple modes, we can assume that a straightforward renaming process taking each mode into account has already been performed. In examples, we will write the mode $`m_p`$ for the predicate $`p`$ as follows: $`p(m_p(1),\mathrm{},m_p(n))`$. Given a predicate $`pPred_P`$ with mode $`m_p`$, we denote by $`I_p=\{i|m_p(i)=In\}`$ the set of input positions of $`p`$ according to $`m_p`$, and by $`O_p=\{i|m_p(i)=Out\}`$ the set of output positions of $`p`$ according to $`m_p`$.

We recall the notion of well-modedness (see e.g. ). For simplifying the notation, when writing an atom as $`p(𝐮,𝐯)`$, we assume that $`𝐮`$ is the sequence of terms filling in the input positions of $`p`$ and $`𝐯`$ is the sequence of terms filling in the output positions of $`p`$. For a term $`t`$, we denote by $`Var(t)`$ the set of variables occurring in $`t`$. Similar notation is used for sequences of terms.

###### Definition 24 (well-modedness)

A clause $`p_0(𝐭_\mathrm{𝟎},𝐬_{𝐧+\mathrm{𝟏}})`$ $`p_1(𝐬_\mathrm{𝟏},𝐭_\mathrm{𝟏}),\mathrm{},p_n(𝐬_𝐧,𝐭_𝐧)`$ is called well-moded iff for $`i[1,n+1]`$:

$$Var(𝐬_𝐢)\underset{j=0}{\overset{i1}{}}Var(𝐭_𝐣).$$

A program is called well-moded iff every clause of it is well-moded.
A query $`p_1(𝐬_\mathrm{𝟏},𝐭_\mathrm{𝟏}),\mathrm{},p_n(𝐬_𝐧,𝐭_𝐧)`$ is well-moded iff the clause $`p`$ $`p_1(𝐬_\mathrm{𝟏},`$ $`𝐭_\mathrm{𝟏}),`$ $`\mathrm{},`$ $`p_n(𝐬_𝐧,𝐭_𝐧)`$ is well-moded, where $`p`$ is any zero-ary predicate symbol.

An example of a well-moded program and query is given in the next subsection (Example 21). In  the persistence of the notion of well-modedness was proven; i.e. an LD-resolvent of a well-moded query and a well-moded clause that is variable-disjoint with it, is well-moded. Note that in a well-moded query, the terms that occur in the input positions of the leftmost atom are all ground. Hence, as a consequence of the persistence of well-modedness, we have that well-moded programs are data driven; i.e. all atoms selected in an LD-derivation of a well-moded query in a well-moded program contain ground terms in their input positions.

Next, we introduce the notion of simply-modedness . A family (or multiset) of terms is called linear iff every variable occurs at most once in it.

###### Definition 25 (simply modedness)

A clause $`p_0(𝐬_\mathrm{𝟎},𝐭_{𝐧+\mathrm{𝟏}})`$ $`p_1(𝐬_\mathrm{𝟏},𝐭_\mathrm{𝟏}),\mathrm{},p_n(𝐬_𝐧,𝐭_𝐧)`$ is called simply moded iff $`𝐭_\mathrm{𝟏},\mathrm{},𝐭_𝐧`$ is a linear family of variables and for $`i[1,n]`$:

$$Var(𝐭_𝐢)(\underset{j=0}{\overset{i}{}}Var(𝐬_𝐣))=\mathrm{}.$$

A program is called simply moded iff every clause of it is simply moded.
A query $`p_1(𝐬_\mathrm{𝟏},𝐭_\mathrm{𝟏}),\mathrm{},p_n(𝐬_𝐧,𝐭_𝐧)`$ is simply moded iff the clause $`p`$ $`p_1(𝐬_\mathrm{𝟏},𝐭_\mathrm{𝟏}),`$ $`\mathrm{},`$ $`p_n(𝐬_𝐧,𝐭_𝐧)`$ is simply moded, where $`p`$ is any zero-ary predicate symbol.

The program and query of Example 21 of the next subsection, are simply moded. The notion of simply modedness is persistent ; i.e. an LD-resolvent of a simply moded query and a simply moded clause that is variable-disjoint with it, is simply moded. An atom is called input/output disjoint if the family of terms occurring in its input positions has no variable in common with the family of terms occurring in its output positions. As a consequence of the persistence of simply modedness, we have that all atoms selected in an LD-derivation of a simply moded query in a simply moded program are input/output disjoint and such that each of the output positions is filled in by a distinct variable.

In , it is argued that most programs are simply moded, and that often non-simply moded programs can be naturally transformed into simply moded ones. In , the class of simply moded, well-moded programs and queries is shown to be unification-free, that is, in the execution, unification can be replaced by iterated matching.

We want to note that our results for the class of simply moded well-moded programs and queries carry over to the bigger class of simply moded well-typed programs and queries such that the heads of the program clauses are input safe. We refer to , where it is shown why the results also hold for these programs and queries. We also refer to , where this class of programs and queries is introduced and shown to be unification-free.

### 7.4 The Symbolic Condition for Quasi-Termination

We will now show how the rigid quasi-acceptability condition (Proposition 12) is translated into a symbolic termination condition. The symbolic condition is a system of constraints on the introduced symbols for norm, level mapping and interargument relations (i.e. the functor, predicate and extended predicate coefficients). We first need the following concepts.

###### Definition 26 (measuring only/all input)

Let $`CB_P^E`$. Let $`|.|_s`$ be a level mapping induced by the symbol mapping $`s`$.
We say that $`|.|_s`$ measures only input positions in $`C`$ iff

* for every predicate $`p/n`$ occurring in $`C`$: if $`iO_p`$, then $`s(p_i)=0`$,

We say that $`|.|_s`$ measures all input positions in $`C`$ iff

* for every predicate $`p/n`$ occurring in $`C`$: if $`iI_p`$, then $`s(p_i)0`$, and
* for every functor $`f/m`$, $`m>0`$, occurring in an input position of an atom in $`C`$: $`s(f_i)0`$ for all $`i\{0,\mathrm{},m\}`$.

The next lemma follows from the fact that, for a well-moded program $`P`$ and a set $`S`$ of well-moded queries, the input positions of atoms in the call set $`Call(P,S)`$ are ground.

###### Lemma 8

Let $`P`$ be a well-moded program and $`SB_P^E`$ be a set of well-moded queries. Let $`|.|_s`$ be a level mapping which measures only input positions in $`Call(P,S)`$. Then, $`|.|_s`$ is rigid on $`Call(P,S)`$.

The following lemma is a corollary of the fact that, for a simply moded program $`P`$ and a set $`S`$ of simply moded queries, the output positions of atoms in the call set $`Call(P,S)`$ consist of distinct variables.

###### Lemma 9

Let $`P`$ be a simply moded program and $`SB_P^E`$ be a set of simply moded queries. Let $`|.|_s`$ be a level mapping which measures all input positions in $`CCall(P,S)`$. Then, $`|.|_s`$ is finitely partitioning on $`C`$.

In the following proposition from , a condition on a symbol mapping $`s`$ is given which ensures that the interargument relations induced by $`s`$ are valid w.r.t. the norm induced by $`s`$. We include its proof since it is essential for understanding the proposition.

###### Proposition 13

Let $`P`$ be a program and $`s`$ a symbol mapping on $`𝒞`$. If for each clause $`HB_1,\mathrm{},B_nP`$ it holds that

$$s(:[𝒜^s(B_1)\mathrm{}𝒜^s(B_n)𝒜^s(H)])$$

then for all $`p/n`$, $`R_s^{p/n}`$ is valid w.r.t. $`._s`$.

###### Proof

The union of the relations

$$R_s^{p/n}=\{(||t_1||_s),\mathrm{},||t_n||_s)|t_1,\mathrm{},t_nTerm_P\text{ and }s(𝒜^s(p(t_1,\mathrm{},t_n)))\text{ holds }\}$$

$`p/nP`$, define an interpretation of $`P`$ on the domain $`\mathrm{}`$. The condition expresses that for this interpretation, $`T_P(I)I`$ holds. Thus, the interpretation is a model and therefore each $`R_s^{p/n}`$ is a valid interargument relation. ∎

Finally, we can formulate the rigid quasi-acceptability condition in a constraint-based way. We use Lemma 8 and Lemma 9, and thus restrict our results to simply moded well-moded programs and queries.

###### Proposition 14

Let $`P`$ be a simply moded well-moded program. Let $`SB_P^E`$ be a set of simply moded well-moded queries. Let $`Tab_PPred_P`$ be a tabling for $`P`$. $`P`$ quasi-terminates w.r.t. $`Tab_P`$ and $`S`$ if
there exists a symbol mapping $`s`$ such that

1. $`|.|_s`$ measures only input positions in $`Call(P,S)`$:

* for every predicate $`p/n`$ occurring in $`Call(P,S)`$:

    if $`iO_p`$, then $`s(p_i)=0`$,
2. $`|.|_s`$ measures all input positions in $`Call(P,S)B_{Tab_P}^E`$:

* for every predicate $`p/n`$ occurring in $`Call(P,S)B_{Tab_P}^E`$:

    if $`iI_p`$, then $`s(p_i)0`$, and
    * for every functor $`f/m`$, $`m>0`$, occurring in an input position of an atom in $`Call(P,S)B_{Tab_P}^E`$:

    $`s(f_i)0`$ for all $`i\{0,\mathrm{},m\}`$,
3. all interargument relations induced by $`s`$ are valid w.r.t. the norm $`._s`$ induced by $`s`$:

$`HB_1,\mathrm{},B_nP`$:

$$s(:[𝒜^s(B_1)\mathrm{}𝒜^s(B_n)𝒜^s(H)]),$$
4. the quasi-acceptability condition w.r.t. the norm, level mapping and interargument relations induced by $`s`$ must hold:

$`HB_1,\mathrm{},B_nP`$, $`B_i,i\{1,\mathrm{},n\}`$:

$$s(:[𝒜^s(B_1)\mathrm{}𝒜^s(B_{i1})|H|^s|B_i|^s])$$

and if $`Rel(H)Rel(B_i)NTab_P`$ and $`C_2(Rel(H),Rel(B_i))`$ does not hold, then

$$s(:[𝒜^s(B_1)\mathrm{}𝒜^s(B_{i1})|H|^s>|B_i|^s]).$$

###### Proof

This symbolic condition for quasi-termination is derived from the rigid quasi-acceptability condition in a way analogous to the derivation of the symbolic condition for LD-termination from the rigid acceptability condition (see ). In order for this article to be self-contained, we include the proof. Suppose that there exists a symbol mapping $`s`$ satisfying the above condition. We prove that $`P`$ is rigid quasi-acceptable w.r.t. $`Tab_P`$ and $`S`$, and hence that $`P`$ quasi-terminates w.r.t. $`Tab_P`$ and $`S`$.

We propose as a level mapping the level mapping $`|.|_s`$ induced by $`s`$ (based on the norm $`._s`$ induced by $`s`$). Because $`|.|_s`$ measures only input positions in $`Call(P,S)`$, we have by Lemma 8 (and by the fact that $`P`$ and $`S`$ are well-moded) that $`|.|_s`$ is rigid on $`Call(P,S)`$. Also, because $`|.|_s`$ measures all input positions in $`Call(P,S)B_{Tab_P}^E`$, we have by Lemma 9 (and by the fact that $`P`$ and $`S`$ are simply moded) that $`|.|_s`$ is finitely partitioning on $`Call(P,S)B_{Tab_P}^E`$.

Take any clause $`HB_1,\mathrm{},B_n`$ in $`P`$, and any body atom $`B_i`$, $`i\{1,\mathrm{},n\}`$. Let $`\psi `$ be a substitution such that $`PB_1\psi ,\mathrm{},B_{i1}\psi `$. We prove that $`|H\psi |_s|B_i\psi |_s`$ (the proof that $`|H\psi |_s>|B_i\psi |_s`$ in case $`Rel(H)Rel(B_i)NTab_P`$ and $`C_2(Rel(H),Rel(B_i))`$ does not hold, is analogous). Condition 4 of this proposition holds for any instantiation of it, so

$$s(:[𝒜^s(B_1\psi )\mathrm{}𝒜^s(B_{i1}\psi )|H\psi |^s|B_i\psi |^s])()$$

holds. Now, we prove that for any $`1ji1`$, $`s(:𝒜^s(B_j\psi ))`$ holds. By Proposition 13 and condition 3 of this proposition, we have that, for all $`p/n`$, $`R_s^{p/n}`$ is valid w.r.t. $`._s`$. Then, since for all $`1ji1`$, $`PB_j\psi `$ holds, we have that $`s(:𝒜^s(B_j\psi ))`$ holds. So, by $`()`$ we conclude that $`s(:|H\psi |^s|B_i\psi |^s)`$ holds, which implies that $`|H\psi |_s|B_i\psi |_s`$ holds. ∎

Given a program $`P`$, a set of atoms $`S`$, and a tabling $`Tab_P`$, we can set up a symbolic condition for quasi-termination using the above proposition. By solving the generated constraints, we get a demand-driven solution for all the concepts involved in the termination analysis (norm, level mapping and model). More precisely, if a norm, level mapping and interargument relations of the given generic forms exist such that the program can be proven to quasi-terminate, then our generated set of constraints has these required instances of the generic forms as a solution.

###### Example 21

Let $`P`$ be the following program, computing the paths from a given node to the reachable nodes in a given cyclic graph:

$$\{\begin{array}{ccc}edge(a,b)\hfill & \hfill & \\ edge(b,a)\hfill & \hfill & \\ path(X,Y,[Y])\hfill & \hfill & edge(X,Y)\hfill \\ path(X,Y,[Z|L])\hfill & \hfill & edge(X,Z),path(Z,Y,L)\hfill \end{array}$$

Let $`Tab_P=\{path/3\}`$ and $`S=\{path(a,Y,L)\}`$. Then, $`P`$ quasi-terminates w.r.t. $`Tab_P`$ and $`S`$. We consider the following modes: $`edge(In,Out),path(In,Out,Out)`$. Then the program $`P`$ and query $`S`$ are simply moded and well-moded.

We prove, using the constraint-based approach of Proposition 14, that $`P`$ quasi-terminates w.r.t. $`Tab_P`$ and $`S`$. We set up the constraints:

1. First for the output positions of the predicates:

$$s(edge_2)=0,s(path_2)=0,s(path_3)=0.$$
2. For the input position of $`path/3`$:

$$s(path_1)0.$$
3. We only need a linear size expression for the $`edge/2`$ predicate. Its two clauses both give rise to the following constraint:

$$s(edge_1^e)0s(edge_2^e)0+s(edge_0^e).$$

After simplification, we get: $`s(edge_0^e)=0`$.
4. The non-recursive clause for $`path/2`$ gives rise to the following constraint:

$$s(X:[path_1Xedge_1X]).$$

This constraint has to hold for all possible values (in $`\mathrm{}`$) for $`X`$. Hence, we derive the following constraint on the symbols $`path_1`$ and $`edge_1`$:

$$s(path_1)s(edge_1).$$

The recursive clause gives rise to the following two constraints:

$$\begin{array}{c}s(X:[path_1Xedge_1X]),\hfill \\ s(X,Z:[edge_1^eXedge_2^eZpath_1Xpath_1Z]).\hfill \end{array}$$

The first constraint is the same as the one for the non-recursive clause and reduces to $`s(path_1)s(edge_1)`$. We refer to  for a general methodology for solving constraints as generated by Proposition 14. Such constraints involve two types of variables: the symbolic coefficients for which we aim to fix a symbol mapping and the universally quantified variables, which express that the derived conditions should hold for any value of these; the point is to eliminate the latter variables. We briefly explain how the second constraint reduces to a system of constraints on the symbolic coefficients only. We first rewrite the second constraint into the following equivalent form:

$$s(X,Z:[edge_1^eXedge_2^eZ0path_1Xpath_1Z0]).$$

Then the idea is to derive the right hand side as a positive linear combination of the assumption in the left hand side. We do this by subtracting the left hand side of the implication from the right hand side, and by requiring that the resulting coefficients of the variables are greater than or equal to $`0`$. Doing so, we obtain the following constraints:

$$s(path_1)s(edge_1^e)0,s(edge_2^e)s(path_1)0.$$

One solution to this system of constraints is ($``$ stands for the list constructor):

$$\begin{array}{c}s(_0)=s(_1)=s(_2)=0,\hfill \\ s(edge_1)=1,s(edge_2)=0,\hfill \\ s(path_1)=1,s(path_2)=s(path_3)=0,\hfill \\ s(edge_1^e)=s(edge_2^e)=1,s(edge_0^e)=0.\hfill \end{array}$$

This gives us the following concrete norm and level mapping:

$$\begin{array}{c}t_s=0tU_P^E(._s\text{is the trivial norm}),\hfill \\ |edge(t_1,t_2)|_s=t_1_s,\hfill \\ |path(t_1,t_2,t_3)|_s=t_1_s.\hfill \end{array}$$

The interargument relation for $`\mathrm{𝑒𝑑𝑔𝑒}(t_\mathit{1},t_\mathit{2})`$ is $`t_1_st_2_s`$.

The rigid quasi-acceptability condition is satisfied using these concrete norm, level mapping and valid interargument relation. Hence, we have proven that $`P`$ quasi-terminates w.r.t. $`\{\mathrm{𝑝𝑎𝑡ℎ}/2\}`$ and $`S`$.

## 8 Conclusions, Related Work and Topics for Future Research

In this article we studied termination of tabled logic programs. We introduced two notions of universal termination under a tabled execution mechanism: quasi-termination and (the stronger notion of) LG-termination. We presented sufficient conditions (which are also necessary in case the tabling is well-chosen) for quasi-termination and LG-termination: namely quasi-acceptability and LG-acceptability. We extended the applicability by presenting modular termination conditions, i.e. conditions ensuring termination of the union $`PR`$ of two programs $`P`$ and $`R`$, where $`P`$ extends $`R`$. Finally, we investigated the problem of automatically proving quasi-termination and LG-termination. We showed that for simply moded, well-moded programs, a sufficient condition for quasi-termination and LG-termination can be given, which is formulated fully at the clause level. We pointed out how these sufficient conditions can be automated by extending the constraint-based, automatic approach towards LD-termination of .

Since all programs that terminate under LD-resolution, are quasi-terminating and LG-terminating as well, verification of termination under LD-resolution using an existing automated termination analysis (such as those surveyed in e.g. ) is a sufficient proof of the program’s quasi-termination and LG-termination. However, since there are quasi-terminating and LG-terminating programs, which are not LD-terminating, better proof techniques can and should be found. There are only relatively few works studying termination under a tabled execution mechanism. In , the special case where all predicates of the program are tabled is considered and the two notions of universal termination of a tabled logic program w.r.t. a set of queries is introduced and characterised. In , in the context of well-moded programs, Plümer presents a sufficient condition for the bounded term-size property of programs, which implies LG-termination. Holst, in , provides another sufficient condition for quasi-termination in the context of functional programming.

Our modular conditions for termination of tabled logic programs and more precisely the modular conditions which incrementally construct a level mapping for the whole program, are inspired by the modular conditions for (S)LD-resolution as given by Apt and Pedreschi in . More specifically, in , the notions of semi-recurrent program (for SLD-resolution) and of semi-acceptable program (for LD-resolution) are introduced, and modular termination proofs are presented which are based on these notions.

In the constraint-based approach towards quasi- and LG-termination, we used mode information in the presentation of the sufficient conditions. In a recent article, , Etalle et al study how mode information can be used for characterizing properties of LD-termination. They define and study the class of well-terminating programs, i.e., programs for which all well-moded queries have finite LD-derivations. They introduce the notion of well-acceptability which is based on the concept of moded level mapping; that is, a level mapping which only measures input positions. It is then shown that for well-moded programs, well-acceptability implies well-termination. Furthermore, it is proven that for simply moded well-moded programs, the notions of well-acceptability and well-termination are equivalent.

A topic for future research is to extend our results to *normal* logic programs executed under such a mixed tabled/non-tabled execution. Another topic, with an arguably more practical flavour, is to investigate how the termination conditions presented here can form the basis of a compiler that automatically decides on—or at least guides a programmer in choosing—a tabling (i.e. a set of tabled predicates) for an input program such that quasi-termination of the program is ensured. We plan to implement the constraint-based technique for automatically proving quasi-termination and LG-termination (note that a prototype implementation for automatically proving LD-termination  exists). Also, it remains to be studied how our results can be extended to automatically prove quasi-termination and LG-termination for larger classes of programs and queries (i.e. for programs and queries which are not simply moded, well-moded).