Database predicates

Prolog programs may make use of a database to store clauses and terms. The rich structure of clauses and terms are far more expressive than the flat records of relational databases or even the more complex tree-like structures encoded in JSON and XML.

The power of the database provided in Arity/Prolog32 enables applications to be constructed that would be much more difficult to construct than in more conventional languages.

Five related sets of predicates are provided by Arity/Prolog32 for storing and indexing any Prolog term in the database:

Prolog clauses for interpretation

Interpreted code is stored in the database as Prolog terms which have the form of clauses. Typically, interpreted code is consulted at program initialization and not further modified, but support is provided for a program to be modified while running and to perform reflection. Best practice calls for a clear separation of code and data, unless there is a compelling reason. Therefore, if your program creates data structures that need to be stored and used during execution you should prefer using other means of term storage than as Prolog clauses.

Sequential storage of terms

Terms may be stored in a sequential order associated with a database key. Each term receives a unique database reference number that can be used to access the term. The database reference number can also be used to directly access the previous or next term in the key if one exists.

B-trees

Terms are stored in the database in a B-tree indexed by keys which can be any ground Prolog term. The keys are maintained in a sorted order that your program can specify. Because the keys are sorted, searching for terms can be more efficient than a sequential search through an un-ordered list. Terms within a range of keys can be retrieved in the sorted order or in reverse sorted order.

Hash tables

Like B-trees, hash tables are used to create an index on terms in the database through keys. Unlike B-trees, keys in a hash table are not ordered and do not have to be ground. In fact, only the principal functor of the key will be used as the hash index.

Orphan records

An orphan record is not stored directly in any of the above three mechnisms. Specialized indexing that store references in graph-like structures may be built with orphans.

Arity/Prolog32's database resides in virtual memory that may be up to 2Gb in size. A database is divided into 4 Kb pages. An application may access up to four databases at the same time using workspace predicates.

Arity/Prolog32 tries to store terms with the same key on the same page and uses other strategies to improve locality of reference. This is done to improve performance when a database is larger than can be stored in physical memory. The judicious use of database worlds and partitions can also significantly improve performance of some database-bound applications.

Predicates for encoding and decoding the internal representation of terms used by the database are described in the Term stucture conversions section.