| Did you know ... | Search Documentation: |
| library(prolog_auto_table): Automatic tabling |
This library provides the XSB compatible directive auto_table/0,
which tables enough predicates of the file in which it appears to make
sure that every cycle in the static call graph of the file contains a
tabled predicate. It uses library(prolog_call_cycles) to
find the recursion in the static call graph of the file. In addition, auto_table_learn/3
learns which predicates to table and in which mode by running goals.
For each strongly connected component of the call graph, the analysis selects a set of predicates to table such that removing these predicates breaks all cycles (a feedback vertex set). Among the possible sets it chooses the one with the lowest estimated number of tabled variants. This estimate is based on how the predicates are called inside the component: calls with fresh variables or ground arguments create few variants, while calls with arguments that are built from data, notably accumulators, create many. Left recursion (recursion where no call to a non-built-in predicate precedes the recursive call) is always reported, as it is guaranteed to loop under Prolog's SLD resolution.
Like XSB, a predicate is also tabled if it is called through tnot/1.
A tabled goal that waits for an incomplete table suspends by
capturing its continuation using shift/1.
It cannot do so through findall/3
and the other predicates that collect all solutions, aggregate_all/3
or a predicate defined in C (see the barrier key of call_graph_sccs/2).
Recursion through such a predicate is therefore not tabled: if its call
passes a proper subterm or a computed value of the head it is assumed to
terminate, otherwise nothing in the strongly connected component is
tabled and auto_table/0
prints a warning.
The analysis is static. Calls through meta-calls that cannot be resolved at compile time are not seen. Tabling guarantees termination only if the number of call variants and answers is finite.
all (default) or left. If left,
only left recursive cycles are broken.true (default false), assume that directly
recursive calls on a proper subterm of an argument of the head
terminate. This is true if the argument is sufficiently instantiated
when the predicate is called. Without this option, this is only assumed
if a mode declaration says the argument is input (+ or ++).
Mode declarations are given using mode/1
or using PlDoc structured comments, e.g. ‘%! len(+List, -Len)‘.true (default), print a message for each predicate that
is tabled.When the file is reloaded, the predicates tabled by the previous analysis are untabled before the new analysis is performed.
:- mode declarations avoid tabling directly
recursive predicates whose recursion decreases an input argument. The
XSB dialect maps XSB :- mode declarations to mode/1.file(File), module(Module) or goal(Goal).
The latter analyses the predicates reachable from Goal. The options files(FileOrFiles)
and module(ModuleOrModules) restrict these to the
predicates defined in the given files or modules (see call_graph/2).
Analysis is a dict with the keys below. Options
are those of
auto_table/1
plus the options of call_graph/2.
PI-Reason for each predicate that must be tabled,
where Reason is one of tnot or cycle.members, tabled (predicates in the SCC
that were already tabled), selected (predicates selected
for tabling), cost (estimated cost of the selection),
left_recursion (list of left recursion dicts, see
left_recursion/3), terminating
(directly recursive predicates that are not tabled because their
recursion is known to terminate), barriers (edges through a
barrier for suspending a tabled goal that are not known to terminate; if
this is not empty, nothing is selected) and cuts (if the
SCC contains tabled predicates, the edges of the SCC whose call can be
pruned by a cut, which raises an error if the call is to an incomplete
table; see the pruned key of call_graph_sccs/2).auto_table_learn/2 prints the result. Options:
user.library(time)
is not available, which is the case in single threaded versions and
WASM.infinite.true (default), run the second phase (see below).time (CPU
time), memory (table space) or balanced
(default, the sum of both relative to the initial configuration).[variant,subsumptive].true, leave the program tabled according to the best
configuration. Default false, restoring the original
tabling.
The second phase searches for the best configuration, a mapping from
predicates to variant, subsumptive or not
tabled. It starts with the predicates that were already tabled plus the
selected predicates of the first phase. Each configuration is evaluated
by running Goals with fresh tables, measuring CPU time and
table space. A configuration is rejected if a goal does not complete or
produces different answers than the initial configuration. If the
initial configuration fails, e.g., because a cut prunes a call to an
incomplete table, the search starts from the original tabling. If that
fails too, the best configuration is the original tabling. The search
moves are: untable all tabled predicates that are not involved in any
observed loop (if this fails, try halves), untable a single predicate,
switch between variant and subsumptive tabling, and table a predicate
that is often called with the same arguments.
Result is a dict with the keys below.
Loop-Barrier for the observed loops that pass a
barrier for suspending a tabled goal, such as findall/3
or a predicate defined in C (see shift/1).
Tabling cannot break these loops, so their predicates are not selected.pi, calls, variants,
args (number of distinct values per argument), tabled
(whether the predicate was already tabled) and loops
(number of loop detections).Goal-Status, where Status is the result
of running the goal: true, false, time_limit_exceeded,
inference_limit_exceeded or an error.PI-Mode, where Mode is variant or subsumptive.auto_table_stack as a term
s(Depth, Index, HeadCopy, Parent, Frame) or [],
where Frame is the frame that runs the ancestor. The learning state is
in the global variable auto_table_learn. We must access it
using
nb_getval/2 such that
we can update it destructively.
A loop that passes a barrier for suspending a tabled goal, such as findall/3, cannot be broken by tabling. It is recorded separately (see shift_barrier/2).