next up previous contents
Next: Dynamically Loaded Fact Files Up: Handling Large Fact Files Previous: Handling Large Fact Files

Compiling Fact Files

Certain applications of XSB require the use of large predicates defined exclusively by ground facts. These can be thought of as ``database'' relations. Predicates defined by a few hundreds of facts can simply be compiled and used like all other predicates. XSB, by default, indexes all compiled predicates on the first argument, using the main functor symbol. This means that a call to a predicate which is bound on the first argument will quickly select only those facts that match on that first argument. This entirely avoids looking at any clause that doesn't match. This can have a large effect on execution times. For example, assume that p(X,Y) is a predicate defined by facts and true of all pairs <X,Y> such that 1 <= X <= 20, 1 <= Y <= 20. Assume it is compiled (using defaults). Then the goal:

	| ?- p(1,X),p(X,Y).
will make 20 indexed lookups (for the second call to p/2). The goal
	| ?- p(1,X),p(Y,X).
will, for each of the 20 values for X, backtrack through all 400 tuples to find the 20 that match. This is because p/2 by default is indexed on the first argument, and not the second. The first query is, in this case, about 5 times faster than the second, and this performance difference is entirely due to indexing.

XSB allows the user to declare that the index is to be constructed for some argument position other than the first. One can add to the program file an index declaration. For example:

:- index p/2-2.

p(1,1).
p(1,2).
p(1,3).
p(1,4).
...

When this file is compiled, the first line declares that the p/2 predicate should be compiled with its index on the second argument. Compiled data can be indexed on only one argument (unless a more sophisticated indexing strategy is chosen.)


next up previous contents
Next: Dynamically Loaded Fact Files Up: Handling Large Fact Files Previous: Handling Large Fact Files
David S. Warren
1999-07-31