Module tabling

This module enhances aggregates by memorization. The table/1 directive has two effects. First of all a tabled predicate call is materialized into a table by the given aggregate. This means for example that duplicates are removed. Second, the materialized table is memorized so that recurrent calls do not re-evaluate the tabled predicate.

Example:
:- table concat/3.
concat([], X, X).
concat([X|Y], Z, [X|T]) :- concat(Y, Z, T).
?- concat(X, Y, [1,2,3]).
X = [],
Y = [1,2,3] ;
X = [1],
Y = [2,3]

The table/1 directive accepts both a predicate indicators and a callable. If a predicate indicator is specified the given aggregate will be the empty aggregate nil/0. If a callable is specified the arguments of the callable specify the given aggregate. Multiple aggregate specifications will be automatically combined by the aggregate pairing operator (',')/2.

Example:
:- table path(_,_,min).
path(X, X, 0).
path(X, Y, N) :- edge(X, Z), path(Z, Y, M), N is M+1.
?- path(a, e, X).
X = 2
?- path(a, e, 1).
No

The memorization stores the variant keys from the tabled predicate calls. Recursive tabled predicate calls are allowed and when completed extend the memorization store. The memorization store can be queried by the predicate current_table/2. Variant keys are not checked whether they subsume, so that different modes result in new variant keys.

The following tabling predicates are provided:

table P, ..:
The predicate sets the predicate P to tabled.The predicate sets the predicate P to tabled. The predicate can be specified via a predicate indicator or a callable. The result is grouped by the witnesses. The following aggregates are recognized:
_: The argument is not aggregated.
sum: The result is the sum of the argument.
mul: The result is the product of the argument.
min: The result is the minimum of the argument.
max: The result is the maximum of the argument.
first(C): The result is the C first of the argument.
last(C): The result is the C last of the argument.
reduce(I,A): The result is the I and A reduct of the argument.
sys_sorter P, ..:
The predicate sets the predicate P to tabled.The predicate sets the predicate P to tabled. The predicate can be specified via a predicate indicator or a callable. The result is sorted by the witnesses.
current_table(V, R):
The predicate succeeds in V with the cached variant keys and in R with the cache clause reference.
retract_table(V):
The predicate succeeds with and removes the cached variant keys that match V.
retractall_table(V):
The predicate succeeds and removes all the cached variant keys that match V.

The following tabling predicate properties are provided:

sys_tabled:
The property indicates that the given predicate is tabled. The value can be changed by the end-user.

Comments