Before we discuss query pack execution in detail, note the following two points: (1) during the pack execution, the pruning of a branch must survive backtracking; (2) when executing a pack we are not interested in any variable instantiations, just in whether a member of the pack succeeds or not. In our previous description we were interested in the binding to the variable I. Since each branch can bind I to only one value - the query number - we collect these values in practice by a side effect denoted in Section 3.2 by report_success.
The starting point for the query pack execution mechanism is the usual Prolog execution of a query given a Prolog program . By backtracking Prolog will generate all the solutions for by giving the possible instantiations such that succeeds in .
A query pack consists of a conjunction of literals and a set of alternatives, where each alternative is again a query pack. Note that leaves are query packs with an empty set of alternatives. For each query pack , denotes the conjunction and denotes the set of alternatives. A set of queries is then represented by a so-called root query pack. For every query pack , there is a path of query packs starting from the root query pack and ending at the query pack itself, namely , , ..., , . The query packs in this path are the predecessors of . Every query pack has a set of dependent queries, . Let , , ..., , be the path to , then , ..., , is a path from to a leaf . Note that are actually the members of the query pack as described earlier.
Execution of a root query pack aims at finding out which queries of the set succeed. If a query pack is executed as if the ors were usual disjunctions, backtracking occurs over queries that have already succeeded and too many successes are detected. To avoid this, it should be the case that as soon as a query succeeds, the corresponding part of the query pack should no longer be considered during backtracking. Our approach realises this by reporting success of queries (and of query packs) to predecessors in the query pack. A (non-root) query pack can be safely removed if all the queries that depend on it (i.e., all the queries in ) succeeded once. For a leaf (empty set of children), success of is sufficient to remove it. For a non-leaf , we wait until all the dependent queries report success or equivalently until all the query packs in report success.
At the start of the evaluation of a root query pack, the set of children for every query pack in it contains all the alternatives in the given query pack. During the execution, query packs can be removed from children sets and thus the values of the change accordingly. When due to backtracking a query pack is executed again, it might be the case that fewer alternatives have to be considered.
The execution of a query pack is defined by the algorithm (Figure 2) which imposes additional control on the usual Prolog execution.
The usual Prolog execution and backtracking behaviour is modelled by the while loop (line 1) which generates all possible solutions for the conjunction in the query pack. If no more solutions are found, fail is returned and backtracking will occur at the level of the calling query pack.
The additional control manages the . For each solution , the necessary children of will be executed. It is important to notice that the initial set of children of a query pack is changed destructively during the execution of this algorithm. Firstly, when a leaf is reached, success is returned (line 8) and the corresponding child is removed from the query pack (line 6). Secondly, when a query pack that initially had several children, finally ends up with an empty set of children (line 6), also this query pack is removed (line 8). The fact that children are destructively removed, implies that when due to backtracking the same query pack is executed again for a different , not all of the alternatives that were initially there, have to be executed any more. Moreover, by returning success the backtracking over the current query pack conjunction is stopped: all branches have reported success.