Subject: Re: indexing capabilty of swi-pl

From: Jan Wielemaker <[email-masked]>
Date: Sun, 28 Nov 1999 14:54:43 +0100

> 
> On Sat, 30 Oct 1999, Jan Wielemaker wrote:
> 
> > > * Wich of this methods do you think is faster to access a dictionary,
> > >    entry(KeyValue,....KeyInfos....)   (about 5.000 entries):
> > >           - put entries as they are in the internal DB of Prolog (with
> > >             KeyValue as first argument, relaying on SWI-PL first arg
> > >             indexing;
> > >           - put entries in a B-tree managed by Prolog.
> > > 
> > This depends on the number of clauses, but using large sets the choice
> > is always a hash-table.  Try to avoid as much as possible duplicates
> > and don't use a `catch-all' clause (using a variable) at the end.
> > 5,000 entries is just peanuts.  We've played with tables more than
> > 10 times as big without any trouble.
> > 
> 
> Can someone mail a small example program showing how to use a hash
> table in SWI Prolog? From the manual, you use hash_term(Term,Key)
> to attribute keys to entries; but I do not understand whether you
> have to do something special to retrieve them afterwards.

Hash-tables are used internally by Prolog to do first-argument indexing
on predicates having lots of clauses.  hash_term/2 is designed to make
it possible to hash on more then just the atomic object or functor of
the first argument.  The idea is that if we want to define a/3, where
argument 1 and 2 should be indexed is replaced by a/4 in this way:

assert_a3(A,B,C) :-
	hash_term(A-B, Hash),
	assert(a(Hash, A, B, C).

a3(A,B,C) :-
	hash_term(A-B, Hash),
	a(Hash, A, B, C).

See also SWI-Prologs :- index(Head). extension.

	Regards --- Jan