A.40 library(prolog_auto_table): Automatic tabling
All Application Manual Name SummaryHelp

  • Documentation
    • Reference manual
      • The SWI-Prolog library
        • library(prolog_auto_table): Automatic tabling
          • auto_table/0
          • auto_table/1
          • auto_table_analysis/3
          • auto_table_learn/2
          • auto_table_learn/3
          • observe_call/3
    • Packages
Availability::- use_module(library(prolog_auto_table)).(can be autoloaded)
Source[det]auto_table_learn(:Goals, +Options)
[det]auto_table_learn(:Goals, -Result:dict, +Options)
Run Goals (a goal or list of goals) to learn which predicates must be tabled. The first phase runs Goals without (additional) tabling while observing the recursion: a call that is a variant of one of its ancestors is a loop. Such a call is recorded and fails, so the run continues. This also finds recursion that is not visible to the static analysis, such as recursion through meta-calls or recursion in the data interpreted by a meta-interpreter. The observed loops are combined into a graph for which we compute a feedback vertex set using the observed number of call variants as cost.

auto_table_learn/2 prints the result. Options:

modules(+Modules)
Observe the predicates of the given modules. Default are all modules of class user.
predicates(+PIs)
Observe the given predicates.
time_limit(+Seconds)
Time limit for each run of a goal. Default 60. This option is ignored if library(time) is not available, which is the case in single threaded versions and WASM.
inference_limit(+Count)
Inference limit for each run of a goal. Default infinite.
max_depth(+Depth)
Treat recursion deeper than Depth as a loop. Default 10,000.
ancestor_depth(+Depth)
Only check the nearest Depth ancestors for variants. Default 1,000.
hot(+Count)
Report statistics for the Count predicates with the most call variants. Default 10.
search(+Bool)
If true (default), run the second phase (see below).
objective(+Objective)
What to optimise in the second phase. One of time (CPU time), memory (table space) or balanced (default, the sum of both relative to the initial configuration).
max_runs(+Count)
Maximum number of configurations to try. Default 50.
modes(+Modes)
Tabling modes to try. Default [variant,subsumptive].
apply(+Bool)
If true, leave the program tabled according to the best configuration. Default false, restoring the original tabling.
output(+File)
Write the table directives for the best configuration to File.

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.

selected
List of predicates that must be tabled to break all observed loops.
loops
List of observed loops. Each loop is a list of predicate indicators.
barrier_loops
List of 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.
predicates
List of dicts with statistics for each predicate that is involved in a loop. Keys are pi, calls, variants, args (number of distinct values per argument), tabled (whether the predicate was already tabled) and loops (number of loop detections).
goals
List of Goal-Status, where Status is the result of running the goal: true, false, time_limit_exceeded, inference_limit_exceeded or an error.
hot
Statistics for the predicates with the most call variants.
config
The best configuration found by the second phase, a list of PI-Mode, where Mode is variant or subsumptive.
runs
List of dicts describing each configuration that was tried.
directives
List of table directives for the best configuration.