CAPITULO II: DESARROLLO DEL TRABAJO
3. PROPUESTAS PARA LA IMPLEMENTACIÓN DEL
3.6 Ubicación de las cámaras en los planos
3.6.1 Semisótano
In Section 3.2 we introduced a new transformation from logic programsP to TRSs RP and
showed that to prove the termination of a class of queries forP, it is sufficient to analyze the termination behavior ofRP. Our criterion to prove termination of logic programs was
summarized in Corollary 3.18.
The transformation itself is trivial to automate and as shown in Section 3.3, existing systems implementing the DP method can easily be adapted to prove termination of in- finitary constructor rewriting. The missing part in the automation is the generation of a
7If (
D, R, π) results from the transformation of a logic program, then for (π(D), π(R), id) it is even sound to apply the existing DP processors for innermost rewriting [GTS05a, GTSF06]. These processors are usually more powerful than those for ordinary rewriting. The framework presented in [GTS05a] even supports constructor rewriting.
suitable argument filter from the user input, cf. Task (a) in Section 3.3. In this section, after presenting the general algorithm to refine argument filters, we introduce suitable heuristics. Finally, we extend the general algorithm for the refinement of argument filters by integrating a mode analysis based on argument filters. This allows us to handle logic programs where a predicate is used with several different modes (i.e., where different oc- currences of the same predicate have different input and output positions). The usefulness of the different heuristics from this section and the power of our mode analysis will be evaluated empirically in Section 3.6.
Refinement Algorithm for Argument Filters
In our approach of Corollary 3.18, the user supplies an initial argument filter π to describe the set of queries whose termination should be proved. There are two issues with this approach. First, while argument filters provide the user with a more expressive tool to characterize classes of queries, termination problems are often rather posed in the form of a moding function for compatibility reasons. Fortunately, it is straightforward to extract an appropriate initial argument filter from such a moding function m: we define π(p) = {i | m(p, i) = in} for all p ∈ ∆ and π(f/n) = {1, . . . , n} for all function symbols f /n∈ Σ.
Second, and less trivially, the variable condition V(π(r)) ⊆ V(π(`)) for all rules ` → r∈ DP (RP)∪ RP does not necessarily hold for the argument filter π. Thus, a refinement
π0 of π must be found such that the variable condition holds for π0. Then, our method from Corollary 3.18 can be applied.
Unfortunately, there are often many refinements π0 of a given filter π such that the variable condition holds. The right choice of π0 is crucial for the success of the termination analysis. As already mentioned in Example 3.19, the argument filter that simply filters away all arguments of all function symbols in the TRS, i.e., that has π0(f ) = ∅ for all f ∈ ΣP, is a refinement of every argument filter π and it obviously satisfies the variable
condition. But of course, only termination of trivial logic programs can be shown when using this refinement π0.
Example 3.27. We consider the logic program of Example 3.1. As shown in Example 3.8, the following rule results (among others) from the translation of the logic program.
pin(f(X), g(Y ))→ u1(pin(f(X), f(Z)), X, Y ) (2)
Suppose that we want to prove termination of all queries p(t1, t2) where both t1 and t2
are (finite) ground terms. This corresponds to the moding m(p, 1) = m(p, 2) = in, i.e., to the initial argument filter π with π(p) ={1, 2}.
to prove termination, we now have to find a refinement π0 of π such that π0(DP (RP)) and
π0(RP) satisfy the variable condition and such that there is no infinite (DP (RP),RP, π0)-
chain.
Let us first try to define π0 = π. Then π0 does not filter away any arguments. Thus, π0(pin) ={1, 2}, π0(u1) ={1, 2, 3}, and π0(f) = π0(g) = {1}. But then clearly, the variable
condition does not hold as Z occurs in π0(r) but not in π0(`) if ` → r is Rule (2) above. So we have to choose a different refinement π0. There remain three choices how we can refine π to π0 in order to filter away the variable Z in the right-hand side of Rule (2): we can filter away the first argument of f by defining π0(
f) = ∅, we can filter away pin’s
second argument by defining π(pin) ={1}, or we can filter away the first argument of u1
by defining π(u1) ={2, 3}.
The decision which of the three choices above should be taken must be done by a suitable heuristic. The following definition gives a formalization for such heuristics. Here we assume that the choice only depends on the term t containing a variable that leads to a violation of the variable condition and on the position pos of the variable. Then a refinement heuristic ρ is a function such that ρ(t, pos) returns a function symbol f /n and an argument position i ∈ {1, . . . , n} such that filtering away the i-th argument of f would erase the position pos in the term t. For instance, if t is the right-hand side u1(pin(f(X), f(Z)), X, Y ) of Rule (2) and pos is the position of the variable Z in this term
(i.e., pos = 121), then ρ(t, pos) can be either (f, 1), (pin, 2), or (u1, 1).
Definition 3.28 (Refinement Heuristic). A refinement heuristic is a partial mapping ρ :T (ΣP,V) × N∗ → ΣP× N such that whenever ρ(t, pos) = (f, i), then there is a position
pos0 with pos0i being a prefix of pos and root (t|pos0) = f .
Given a TRS RP resulting from the transformation of a logic program P and a refine-
ment heuristic ρ, Algorithm 1 computes a refinement π0 of a given argument filter π such that the variable condition holds for DP (RP) and RP.
Termination of this algorithm is obvious asRPis finite and each change of the argument
filter in Step 2.2 reduces the number of unfiltered arguments. Note also that ρ(r, pos) is always defined since pos is never the top position ε. The reason is that the TRS RP is
non-collapsing (i.e., it has no right-hand side consisting just of a variable). The algorithm is correct as it only terminates if the variable condition holds for every dependency pair and every rule.
Note that if π0(F ) = π0(f ) for every defined function symbol f and if we do not filter away the first argument position of the function symbols uc,i, i.e., 1 ∈ π0(uc,i), then
the satisfaction of the variable condition for RP implies that the variable condition for
DP (RP) holds as well. Thus, for heuristics that guarantee the above properties, we only
Input: argument filter π, refinement heuristic ρ, TRS RP
Output: refined argument filter π0 such that π0(DP (RP)) and π0(RP) satisfy the
variable condition 1. π0 := π
2. If there is a rule `→ r from DP (RP)∪ RP
and a position pos with r|pos ∈ V(π0(r))\ V(π0(`)), then:
2.1. Let (f, i) be the result of ρ(r, pos), i.e., (f, i) := ρ(r, pos). 2.2. Modify π0 by removing i from π0(f ), i.e., π0(f ) := π0(f )\
{i}.
For all other symbols from ΣP, π0 remains unchanged.
2.3. Go back to Step 2.
Algorithm 1: General Refinement Algorithm
Simple Refinement Heuristics
The following definition introduces two simple possible refinement heuristics. If a term t has a position pos with a variable that violates the variable condition, then these heuristics filter away the respective argument position of the innermost resp. the outermost function symbol above the variable.
Definition 3.29 (Innermost/Outermost Refinement Heuristic). Let t be a term and let “pos i” resp. “i pos” be a position in t. The innermost refinement heuristic ρim is defined
as follows:
ρim(t, pos i) = (root (t|pos), i)
The outermost refinement heuristic ρom is defined as follows:
ρom(t, i pos) = (root (t), i)
So if t is again the termu1(pin(f(X), f(Z)), X, Y ), then the innermost refinement heuris-
tic would result in ρim(t, 121) = (f, 1) and the outermost refinement heuristic gives
ρom(t, 121) = (u1, 1).
Both heuristics defined above are simple but problematic, as shown in Example 3.30. Filtering the innermost function symbol often results in the removal of an argument position that is relevant for termination of another rule. Filtering the outermost function symbol excludes the possibility of filtering the arguments of function symbols from the signature Σ of the original logic program. Moreover, the outermost heuristic also often removes the first argument of some uc,i-symbol. Afterwards, a successful termination
Example 3.30. Consider again the logic program of Example 3.1 which was transformed into the following TRS in Example 3.8.
pin(X, X)→ pout(X, X) (1)
pin(f(X), g(Y ))→ u1(pin(f(X), f(Z)), X, Y ) (2)
u1(pout(f(X), f(Z)), X, Y )→ u2(pin(Z,g(Y )), X, Y, Z) (3)
u2(pout(Z,g(Y )), X, Y, Z)→ pout(f(X), g(Y )) (4)
As shown in Example 3.15 we obtain the following dependency pairs for the above rules.
Pin(f(X), g(Y ))→ Pin(f(X), f(Z)) (5)
Pin(f(X), g(Y ))→ U1(pin(f(X), f(Z)), X, Y ) (6)
U1(pout(f(X), f(Z)), X, Y )→ Pin(Z,g(Y )) (7)
U1(pout(f(X), f(Z)), X, Y )→ U2(pin(Z,g(Y )), X, Y, Z) (8)
As in Example 3.27 we want to prove termination ofp(t1, t2) for all ground terms t1 and
t2. Hence, we start with the argument filter π that does not filter away any arguments,
i.e., π(f /n) = {1, . . . , n} for all f ∈ ΣP. We will now illustrate Algorithm 1 using our
two heuristics.
Using the innermost refinement heuristic ρim in the algorithm, for the second DP (6)
we get ρim(U1(pin(f(X), f(Z)), X, Y ), 121) = (f, 1). This requires us to filter away the only
argument of f, i.e., π0(
f) = ∅. Now Z is contained in the right-hand side of the third DP (7), but not in the filtered left-hand side anymore. Thus, we now have to filter away the first argument of Pin, i.e., π0(Pin) = {2}. Due to the DP (6), we now also have to
remove the second argument X of U1, i.e., π0(U1) = {1, 3}. Consequently, we lose the
information about finiteness of p’s first argument and therefore cannot show termination of the program anymore. More precisely, there is an infinite (DP (RP),RP, π0)-chain
consisting of the dependency pairs (6) and (7) using a substitution that instantiates the variables X and Z by the infinite term f(f(. . .)). This is indeed a chain since all infinite terms are filtered away by the refined argument filter π0. Hence, the termination proof fails.
Using the outermost refinement heuristic ρom instead, for the second DP (6) we get
ρom(U1(pin(f(X), f(Z)), X, Y ), 121) = (U1, 1), i.e., π0(U1) = {2, 3}. Considering the third
DP (7) we have to filter away the first argument of Pin, i.e., π0(Pin) = {2}. Due to the
DP (6), we now also have to remove the second argument ofU1, i.e., π0(U1) = {3}. So we
obtain the same infinite chain as above since we lose the information about finiteness of p’s first argument. Hence, we again cannot show termination.
A slightly improved version of the outermost refinement heuristic can be achieved by disallowing the filtering of the first arguments of the symbols uc,i and Uc,i.
Definition 3.31 (Improved Outermost Refinement Heuristic). Let t be a term and pos be a position in t. The improved outermost refinement heuristic ρom0 is defined as:
ρom0(t, i pos) =
ρom0(t|i, pos) if i = 1 and either root (t) = uc,i or root (t) = Uc,i
(root (t), i) otherwise
Example 3.32. Reconsider Example 3.30. Using Algorithm 1 with the improved outer- most refinement heuristic, for the second rule (2) we get ρom0(u1(pin(f(X), f(Z)), X, Y ), 121)
= ρom0(pin(f(X), f(Z)), 21) = (pin, 2) requiring us to filter away the second argument of
pin, i.e., π0(pin) = {1}. Consequently, the algorithm filters away the third arguments
of both u1 and u2, i.e., π0(u1) = {1, 2} and π0(u2) = {1, 2, 4}. Now the variable con-
dition holds for RP. Therefore, by defining π0(Pin) = π0(pin), π0(u1) = π0(U1), and
π0(u2) = π0(U2), the variable condition also holds for DP (RP). (As mentioned above,
by filtering tuple symbols F in the same way as the original symbols f and by ensuring 1 ∈ π0(uc,i), it suffices to check the variable condition only for the rules RP and not for
the dependency pairs DP (RP).) This argument filter corresponds to the one chosen in
Example 3.19 and as shown in Section 3.3 one can now easily prove →-termination.∞
Type-Based Refinement Heuristic
The improved outermost heuristic from Section 3.4 only filters symbols of the form pin,
pout, Pin, and Pout. Therefore, the generated argument filters are similar to modings.
However, there are cases where one needs to filter function symbols from the original logic program, too. In this section we show how to obtain a more powerful refinement heuristic using information from inferred types.
There are many approaches to (direct) termination analysis of logic programs that use type information in order to guess suitable “norms” or “ranking functions”, e.g., [BCF92, BCG+07, DDF93, MKS96]. In contrast to most of these approaches, we do not
consider typed logic programs, but untyped ones and we use types only as a basis for a heuristic to prove termination of the transformed TRS. To our knowledge, this is the first time that types are considered in the transformational approach to termination analysis of logic programs.
transformation of Definition 3.7 are:
pin(X, X)→ pout(X, X) (1)
pin(f(X), g(Y ))→ u1(pin(f(X), f(Z)), X, Y ) (2)
u1(pout(f(X), f(Z)), X, Y )→ u2(pin(Z,g(W )), X, Y, Z) (10)
u2(pout(Z,g(W )), X, Y, Z) → pout(f(X), g(Y )) (11)
Using the improved outermost refinement heuristic ρom0 we start off as in Example 3.32
and obtain π0(pin) ={1}, π0(u1) ={1, 2}, and π0(u2) = {1, 2, 4}. However, due to the last
rule (11) we now get ρom0(pout(f(X), g(Y )), 21) = (pout, 2), i.e., π0(pout) ={1}. Considering
the third rule (10), we have to filterpin once more and obtain π0(pin) = ∅. So we again
lose the information about finiteness ofp’s first argument and cannot show termination. Similar to Example 3.30, the innermost refinement heuristic which filters away the only argument off also fails for this program.
So in the example above, neither the innermost nor the (improved) outermost refine- ment heuristic succeed. We therefore propose a better heuristic which is like the innermost refinement heuristic, but which avoids the filtering of certain arguments of original func- tion symbols from the logic program. Close inspection of the cases where filtering such function symbols is required reveals that it is not advisable to filter away “reflexive” arguments. Here, we call an argument position i of a function symbol f reflexive (or “recursive”), if the arguments on position i have the same “type” as the whole term f (. . .) itself, cf. [Wal94]. A type assignment associates a predicate p/n with an n-tuple of types for its arguments and, similarly, a function f /n with an (n + 1)-tuple where the last element specifies the result type of f .
Definition 3.34 (Types). Let Θ be a set of types (i.e., a set of names). A type assign- ment τ over a signature (Σ, ∆) and a set of types Θ is a mapping τ : Σ∪ ∆ → Θ∗ such that τ (p/n)∈ Θn for all p/n∈ ∆ and τ(f/n) ∈ Θn+1 for all f /n∈ Σ. Let f/n ∈ Σ be a
function symbol and τ be a type assignment with τ (f ) = (θ1, . . . , θn, θn+1). Then the set
of reflexive positions of f /n is Reflexiveτ(f /n) ={i | 1 ≤ i ≤ n and θi = θn+1}.
To infer a suitable type assignment for a logic program, we use the following simple algorithm. However, since we only use types as a heuristic to find suitable argument filters, any other type assignment would also yield a correct method for termination analysis. In other words, the choice of the type assignment only influences the power of our method, not its soundness. So unlike [BCG+07], the correctness of our approach does not depend on the logic program or the query being well-typed. More sophisticated type inference algorithms were presented in [BGV05, CP98, GP02, JB92, Lu00, VB02], for example.
In our simple type inference algorithm, we define ' as the reflexive and transitive closure of the following “similarity” relation on the argument positions: Two argument
positions of (possibly different) function or predicate symbols are “similar” if there exists a program clause such that the argument positions are occupied by identical variables. Moreover, if a term f (. . .) occurs in the i-th position of a function or predicate symbol p, then the argument position of f ’s result is similar to the i-th argument position of p. (For a function symbol f /n we also consider the argument position n + 1 which stands for the result of the function.) After having computed the relation ', we then use a type assignment which corresponds to the equivalence classes imposed by'. So our simple type inference algorithm is related to sharing analysis [BDB+96, CF99, LS02], i.e., the program
analysis that aims at detecting program variables that in some program execution might be bound to terms having a common variable.
Example 3.35. As an example, we compute a suitable type assignment for the logic program from Example 3.3:
p(X, X).
p(f(X), g(Y )) ← p(f(X), f(Z)), p(Z, g(W )).
Letpi denote the i-th argument position ofp, etc. Then due to the first clause we obtain
p1 ' p2, since both argument positions are occupied by the variable X. Moreover, since
Z occurs both in the first argument positions off and p in the second clause, we also have p1 ' f1. Finally, since an f-term occurs in the first and second argument of p and since
a g-term occurs in the second argument of p we also have f2 ' p1 ' p2 and g2 ' p2. In
other words, the relation' imposes the two equivalence classes {p1,p2,f1,f2,g2} and {g1}.
Hence, we compute a type assignment with two types a and b where a and b correspond to{p1,p2,f1,f2,g2} and {g1}, respectively. Thus, the type assignment is defined as τ(p) =
τ (f) = (a, a) and τ (g) = (b, a).
Note that the first argument of f has the same type a as its result and hence, this argument position is reflexive. On the other hand, the first argument of g has a differ- ent type than its result and is therefore not reflexive. Hence, Reflexiveτ(f) = {1} and Reflexiveτ(g) = ∅.
Now we can define the following heuristic based on type assignments. It is like the innermost refinement heuristic of Definition 3.29, but now reflexive arguments of function symbols from Σ (i.e., from the original logic program) are not filtered away.
Definition 3.36 (Type-based Refinement Heuristic). Let t be a term, let “pos i” be a position in t, and let τ be a type assignment. The type-based refinement heuristic ρτ
tb is
defined as follows:
ρτtb(t, pos i) = (
(root (t|pos), i) if root (t|pos) /∈ Σ or i /∈ Reflexiveτ(root (t|pos))
ρτ
Note that the heuristic ρτ
tb never filters away the first argument of a symbol uc,i or Uc,i
from the TRSs DP (RP) and RP. Therefore, as mentioned above, we only have to check
the variable condition for the rules of RP, but not for the dependency pairs.
Example 3.37. We continue with the logic program from Example 3.3 and use the type assignment computed in Example 3.35 above. The rules after the transformation of Definition 3.7 are the following, cf. Example 3.33.
pin(X, X)→ pout(X, X) (1)
pin(f(X), g(Y ))→ u1(pin(f(X), f(Z)), X, Y ) (2)
u1(pout(f(X), f(Z)), X, Y )→ u2(pin(Z,g(W )), X, Y, Z) (10)
u2(pout(Z,g(W )), X, Y, Z) → pout(f(X), g(Y )) (11)
Due to the occurrence of Z in the right-hand side of the second rule (2), we compute:
ρτ
tb(u1(pin(f(X), f(Z)), X, Y ), 121)
= ρτ
tb(u1(pin(f(X), f(Z)), X, Y ), 12) as f ∈ Σ and 1 ∈ Reflexiveτ(f)
= (pin, 2) as pin 6∈ Σ
Thus, we filter away the second argument of pin, i.e., π0(pin) = {1}. Consequently, we
obtain π0(u1) ={1, 2} and π0(u2) = {1, 2, 4}.
Considering the fourth rule (11) we compute:
ρτtb(pout(f(X), g(Y )), 21)
= (g, 1) as 16∈ Reflexiveτ(g) Thus, we filter away the only argument ofg, i.e., π0(
g) = ∅. By filtering the tuple symbols in the same way as the corresponding “lower-case” symbols, now the variable condition holds for RP and therefore also for DP (RP). Indeed, this is the argument filter chosen
in Example 3.19. With this filter, one can easily prove termination of the program, cf. Section 3.3.
For the above example, it is sufficient only to avoid the filtering of reflexive positions. However, in general one should also avoid the filtering of all “unbounded” argument positions. An argument position of type θ is “unbounded” if it may contain subterms