
This package applies a compiling control technique to implement depth-first iterative deepening (IDDFS) execution [Kor85]. It changes the usual depth-first search rule by iterative-deepening on those predicates specifically marked. This is very useful in search problems as an alternative to, e.g., breadth-first search, when a complete proof procedure is needed (another complete proof procedure is implemented by the bf package(s) for breadth first execution).
When this search rule is used, SLD derivations are explored in a depth-first manner under an explicit cost bound. The cost of a derivation is measured using resource metrics such as call depth or the number of resolution steps. If no solution is found – or if additional solutions are requested via backtracking – the bound is relaxed and the entire goal is re-executed from the initial state.
Although this strategy may appear inefficient because derivations explored under smaller bounds are revisited when the bound is increased, it has been shown that iterative deepening incurs only about b/(b - 1) times as many expansions as the corresponding breadth-first strategy (where b is the branching factor of the SLD tree), while retaining the low memory consumption of depth-first search.
The following directives can be used to control the search:
The parameters for iterative-deepening are an [Opt=Value, ...] list where the options can be:
A predicate F/N executed using the IDDFS search rule will start the execution starting with initial bound init_cutoff, and with cost bound being incremented by the threshold update function. This predicate computes the new threshold using the previous one. It must implement a dilating function i.e., the new cost bound must be greater than the old one. For example, using the call_depth metric, to start with depth 5 and increment by 10 you can write:
:- iterative(p/1,[init_cutoff=5, next_threshold_fun=f]).
f(X,Y) :- Y is X + 10.
or, alternatively (using the hiord package):
:- iterative(p/1,[init_cutoff=5, next_threshold_fun=(_(X,Y):- Y is X + 10)]).
You can also use a fourth parameter to set a limiting depth. Goals that reach the given depth limit simply fail. Thus, with the following directive all goals deeper than 100 will fail:
:- iterative(p/1,[
init_cutoff=5,
next_threshold_fun=(_(X,Y):- Y is X + 10),
max_cost=100
]). This is a simple example using this package:
:- module(example_id, _,[id]).
test(id) :-
idchain(a,d).
test(df) :-
chain(a,d). % loops!
:- iterative(idchain/2, [init_cutoff=3]).
idchain(X,X).
idchain(X,Y) :-
arc(X,Z),
idchain(Z,Y).
chain(X,X).
chain(X,Y) :-
arc(X,Z),
chain(Z,Y).
arc(a,b).
arc(a,d).
arc(b,c).
arc(c,a).
The order of solutions is as follows: first, the shallower ones and then the deeper ones in the resolution tree. Solutions between two cutoffs are given in the usual left-to-right order (i.e., the leftmost branch corresponds to the first unifying clause in the program, as in depth-first search). For example:
:- module(_,_,[id,hiord]).
:- iterative(p/1,[
init_cutoff=0,
next_threshold_fun=(_(X,Y):- Y is X + 1),
max_cost=2 % goals deeper than 2 will fail
]).
% Change the solutions' order to goal p(X).
% :- iterative(p/1,[
% init_cutoff=1,
% next_threshold_fun=(_(X,Y):- Y is X + 3),
% max_cost=2 % goals deeper than 2 will fail
% ]).
p(X) :- q(X).
p(a).
q(X) :- r(X).
q(b).
r(X) :- s(X).
r(c).
s(d).
All predicates specified with the iterative directive will be executed in IDDFS mode for a particular metric and stay in that mode for all recursive calls also compiled for that metric. Otherwise, predicates are called in the default DFS strategy.
It is possible to preserve the iterative-deepening behavior for calls to predicates defined in other modules. These modules should obviously also use this package. In addition, all predicates from such modules should be imported, i.e., the directive :- use_module(module), should be used in this case instead of :- use_module(module,[...]). Otherwise calls to predicates outside the module will be treated in the usual way, i.e., by depth-first execution.