reach(X,Y):-edge(X,Y).
reach(X,Y):-reach(X,Z),edge(Z,Y).
where the predicate edge defines a relation and reach
defines the transitive closure of the relation. Without tabling, a
query like reach(X,Y) would fall into an infinite loop. Consider
another example:
fib(0, 1).
fib(1, 1).
fib(N,F):-N>1,
N1 is N-1,
N2 is N-2,
fib(N1,F1),
fib(N2,F2),
F is F1+F2.
A query fib(N,X), where N is an integer, will not fall
into an infinite loop, but will spawn The main idea of tabling is to memorize the answers to some calls, called tabled calls, and use the answers to resolve subsequent variant calls. In B-Prolog, tabled predicates are declared explicitly by declarations in the following form:
:-table P1/N1,...,Pk/Nk
where each Pi (i=1,...,k) is a predicate symbol and Ni is an integer that denotes the arity of Pi. To declare all the predicates in a Program as tabled, add the following line to the beginning of the program:
:-table_all.