Indexing (in Prolog) is an optimization technique that reduces the search space of the predicates without altering the Prolog semantics.
In the most general case, predicate clauses are tryed on backtracking one after the other, in sequential order. We can call this list of clauses a try-list. Indexing is based on removing clauses that are known to fail without any observable output (no side-effects) from the try-list. A typical implementation introduces tests before the actual predicate execution to discriminate among a collection of precomputed specialized try-lists. In the best case, this technique can obtain try-lists with 0 or 1 elements for calls.
Currently, the Ciao engine implements a limited but fast 1st-argument-1st-level indexing. The indexer package provides more powerful indexing schemes. It lets you pick different combinations of arguments to index on. E.g., it will let you index on the first and third argument or the second and the third argument of a predicate.
The selection of the try-list is based on computing a hash value for the terms (or part of them) to be indexed upon. Given this, the optimization pays off only when the amount of clashing that your original predicate causes without indexing superseeds the cost of the hashing function. Such amount of course depends on the number and form of the facts in your predicate, and the calling modes.
Despite this, the package implements some indexing schemes with low overhead.
Provides an efficient way to calculate an integer HashValue for a ground Term.
N is a hashing index for T.
Declares an indexing scheme for a predicate. Each spec declares an indexing on a combination of the arguments. Indexing will be performed using any of the specs in IndexSpecs (being thus interpreted as an or).
You should use a * in an argument position if you wish to hash on the entire term in that argument. If a + is used only one level of the term in the argument is used for hashing. An i is used to indicate that argument is already an integer, and therefore its own value will be used for hashing. The argspec ? simply indicates not to use the argument for indexing.
For example, the index specification:
:- index foo(+,?,*,i), foo(?,?,?,i).declares indexing for foo/4 either on a combination of the first, third, and fourht arguments, or only on the last argument, which is an integer. In the first case, only the principal functor of the first argument will be used for hashing; the third argument will be used in its entirety.
The argspec n is a pragmatic extension and can not be used in conjunction with the other specifiers aside from ?. It stands for "nonvar" and implies that the argument will not be used for hashing, since only ground terms can effectively be used in hashing. Thus, it can not be used in combination with other specifiers within a particular index specification. It is often the fastest thing to use.
indexspecs(Spec) :- indexspec(Spec). indexspecs((Spec,Specs)) :- indexspec(Spec), indexspecs(Specs).
indexspec(Spec) :- Spec=..[_F|Args], list(Args,argspec).
IndexSpecs is an index specification.
argspec(+). argspec(*). argspec(i). argspec(n). argspec(?).
Spec is an argument hash specification.