Multiple Argument Indexing

Author(s): Anil Nair (original work), Tom Howland (, derived the original work), Francisco Bueno (port to Ciao).

This package is an extension of the idea of Prolog indexing, usually performed, in a limited way, on the first argument. This package provides more powerful indexing schemes. It lets you pick different arguments to index on, and provides for 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 indexing is based on computing a hash value for the terms to be indexed upon. Note, however, that the current implementation of the package is done at the source level, so it may sometimes not be as fast as expected. Given this, this version of the package pays off only when the amount of clashing that your original predicate causes without the package superseeds the cost of the hashing function in the package. Such amount of course depends on the number and the form of the facts in your predicate.

Usage and interface

Documentation on exports


Provides an efficient way to calculate an integer HashValue for a ground Term.

Usage 1: hash_term(T,N)

  • Description: N is a hashing index for T.
  • The following properties should hold at call time:
    (term_typing:ground/1)T is currently ground (it contains no variables).
    (term_typing:var/1)N is a free variable.
  • The following properties should hold upon exit:
    (basic_props:int/1)N is an integer.

Usage 2: hash_term(T,N)

  • The following properties should hold at call time:
    (native_props:nonground/1)T is not ground.
    (term_typing:var/1)N is a free variable.
  • The following properties should hold upon exit:
    (term_typing:var/1)N is a free variable.

Documentation on internals


Usage: :- index(IndexSpecs).

  • Description: Declares an indexing scheme for a predicate. All specs of IndexSpecs must be terms for the same 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.

  • The following properties should hold upon exit:
    (indexer_doc:indexspecs/1)IndexSpecs is an index specification.

An index specification is defined as follows:
indexspecs(Spec) :-
indexspecs((Spec,Specs)) :-
indexspec(Spec) :-

Usage: indexspecs(IndexSpecs)

  • Description: IndexSpecs is an index specification.

An argument hash specification is defined as follows:

Usage: argspec(Spec)

  • Description: Spec is an argument hash specification.