
prolog_auto_table.pl -- Automatic tablingThis 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.
auto_table is det
auto_table(+Options) is detall (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.
auto_table_file(+File, +Module, +Options) is det[private]
auto_table_analysis(:Spec, -Analysis:dict, +Options) is detfile(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).
tnot_called(+Graph, -PIs) is det[private]
analyse_scc(+Options, +TNot, +ModeDB, +SCC, -Result) is det[private]
barrier_edge(+Edge) is semidet[private]
progress_edge(+Edge) is semidet[private]
illegal_cuts(+SCC, +Tabled, +TNot, +Selected, -Cuts) is det[private]
terminating_edge(+Structural, +ModeDB, +Edge) is semidet[private]+ or ++) by a mode
declaration (see mode_db/3). If Structural is true, any
directly recursive call that decreases an argument is considered
terminating.
mode_terminating(+ModeDB, +Edge) is semidet[private]
mode_db(+Structural, +SCCs, -ModeDB) is det[private]+, - or ?. Mode declarations are taken
from the first of these sources that provides them:
mode(Head). This also covers XSB :- mode declarations
(see library(dialect/xsb)).
xref_modes(+PIs, -Pairs) is det[private]
node_costs(+Nodes, +Edges, -Costs) is det[private]c(Variants, NotExported, Arity). Variants estimates the
number of tabled variants if this node is tabled, based on the
argument abstraction of the calls to the node. The other two
fields implement XSB's tie breaking: prefer exported predicates
and predicates with a low arity.
feedback_vertex_set(+Nodes, +Edges, +Costs, -FVS, -Cost) is det[private]
greedy_fvs(+Graph, +Costs, -FVS) is det[private]
reduce_graph(+Graph0, -Graph) is det[private]
find_cycle(+Graph, -Cycle) is semidet[private]
auto_table_learn(:Goals, +Options) is det
auto_table_learn(:Goals, -Result:dict, +Options) is detauto_table_learn/2 prints the result. Options:
user.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.
candidate_predicates(+Options, -PIs) is det[private]
discover(+Goals, +Candidates, -Discovery, +Options) is det[private]
ancestor_variant(+Stack, +I, +Head, +MaxCheck, -Path, -Frame) is semidet[private]
frame_barrier(+Frame, +AncFrame, -Barrier) is semidet[private]shift_barrier, such as findall/3, or a
predicate defined in C that called Prolog. Barrier is its
predicate indicator.
deep_loop(+Stack, +I, +State, +Loops)[private]
canonical_cycle(+PIs, -Cycle) is det[private]
run_goal(+Limits, :Goal, -Status) is det[private]
limits(+Options, -Limits) is det[private]limits(Time, Inferences) holding the limits for a
single run of a goal. Each is a number or infinite.
call_with_limits(+Limits, :Goal)[private]time_limit_exceeded or inference_limit_exceeded if
a limit is exceeded.
scale_limits(+Limits0, +Run, -Limits) is det[private]
loop_predicates(+Loops, +Candidates, +State, -Preds) is det[private]
hot_predicates(+State, +N, -Hot) is det[private]
learned_selection(+Discovery, -Selected) is det[private]
original_tabling(+Candidates, -Original, -Fixed) is det[private]PI-t(Mode, Extra) for the tabled
candidates we may change, where Mode is variant or
subsumptive and Extra is a list of additional table options
(incremental, shared, ...). Fixed is a list of tabled candidates
we do not change: predicates with mode-directed tabling.
search(+Goals, +Result0, +Original, +Fixed, -Result, +Options)[private]
run_search(+Goals, +Initial, +Result0, +Fixed, +State, -Search)[private]
original_config(+State, -Config) is det[private]
inverse_moves(+Move, +OldConfig, -Inverse) is det[private]
accept(+Objective, +New, +Current, +Initial) is semidet[private]
better(+Objective, +New, +Current, +Initial) is semidet[private]
moves(+Config, +Ctx, -Moves) is det[private]
variant_order(+PIs, +Result, -Ordered) is det[private]
table_space(-Count, -Bytes) is det[private]
apply_config(+Config, +State) is det[private]
directives(+Config, -Directives) is det[private]The following predicates are exported, but not or incorrectly documented.
auto_table(Arg1)
auto_table_learn(Arg1, Arg2, Arg3)