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.

:- 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.

:- 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).

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 specializations result in new variant keys.

The following tabling predicates are provided:

table P, ..:
table (P, ..) as O:
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 as/2 construct can be used to specify sort options. The following tabling functions are recognized when a callable is specified:
_: 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.

The sort options are the same as for aggregate/4 and friends. Except tabling, also recognize the following sort option additions:

share(S): Determine the sub goal and result sharing.

The allowed share values are "thread_local", "dynamic" and "group_local". The default value is "thread_local".
current_table(V, S):
The predicate succeeds in V with the cached variant keys and in S with the cache status of the variant key.
The predicate succeeds with and removes the cached variant keys that match V.
The predicate succeeds and removes all the cached variant keys that match V.

The following tabling predicate properties are provided:

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