O (1) search

I want to find the existence of a term in my current prolog program as quickly as possible, without a prolog engine intersecting all terms until it reaches an existing term.

I did not find any evidence of this. But I assume that this

animal(lion). animal(zebra). ... % thousands of other animals ... animal(tiger). 

The swi-prolog engine will have to go through thousands of animals trying to team up with a tiger to confirm that the animal (tiger) is in my prologue database.

In other languages, I believe that HashSet will solve this problem by allowing you to search for O (1) ... However, I cannot find hash tables or hash tables in the swi-proog documentation.

Is there a swi-prolog library for the hash, or can I somehow create it myself using term_hash \ 2?

Information about the bonus, I most likely will have to look at some dynamically added data, either added to the hashset data structure, or using assertz

+5
source share
2 answers

All serious Prolog systems perform this O (1) hash search automatically and implicitly for you, so you don't have to do it yourself.

It's called argument indexing, and you find it explained in all the good Prolog books. See Also "Just-In-Time JIT Indexing" in later versions of many Prolog systems, including SWI. Indexing applies to dynamically added sentences, and this is one of the reasons why assertz/1 slows down and therefore is not a good choice for data that changes more often than it reads.

You can also easily verify this yourself by creating databases with more and more facts and assuming that the search time remains approximately constant when argument indexing is applied.

+6
source

If the built-in indexing of the first argument is not enough (note that some Prolog systems also provide indexing with several arguments), depending on the system, you can create your own indexing scheme using the built-in or library term of the hash predicate, In the case of ECLiPSe, GNU Prolog, SICStus Prolog, SWI-Prolog, and YAP, view the documentation for the term_hash/4 predicate.

+2
source

Source: https://habr.com/ru/post/1216035/


All Articles