4.13 Database

SWI-Prolog offers three different database mechanisms. The first one is the common assert/retract mechanism for manipulating the clause database. As facts and clauses asserted using assert/1 or one of its derivatives become part of the program these predicates compile the term given to them. retract/1 and retractall/1 have to unify a term and therefore have to decompile the program. For these reasons the assert/retract mechanism is expensive. On the other hand, once compiled, queries to the database are faster than querying the recorded database discussed below. See also dynamic/1.

The second way of storing arbitrary terms in the database is using the ``recorded database''. In this database terms are associated with a key. A key can be an atom, small integer or term. In the last case only the functor and arity determine the key. Each key has a chain of terms associated with it. New terms can be added either at the head or at the tail of this chain.

The third mechanism is a special purpose one. It associates an integer or atom with a key, which is an atom, integer or term. Each key can only have one atom or integer associated with it.

[ISO]abolish(:PredicateIndicator)
Removes all clauses of a predicate with functor Functor and arity Arity from the database. All predicate attributes (dynamic, multifile, index, etc.) are reset to their defaults. Abolishing an imported predicate only removes the import link; the predicate will keep its old definition in its definition module.

According to the ISO standard, abolish/1 can only be applied to dynamic procedures. This is odd, as for dealing with dynamic procedures there is already retract/1 and retractall/1. The abolish/1 predicate has been introduced in DEC-10 Prolog precisely for dealing with static procedures. In SWI-Prolog, abolish/1 works on static procedures, unless the prolog flag iso is set to true.

It is advised to use retractall/1 for erasing all clauses of a dynamic predicate.

abolish(+Name, +Arity)
Same as abolish(Name/Arity). The predicate abolish/2 conforms to the Edinburgh standard, while abolish/1 is ISO compliant.
redefine_system_predicate(+Head)
This directive may be used both in module user and in normal modules to redefine any system predicate. If the system definition is redefined in module user, the new definition is the default definition for all sub-modules. Otherwise the redefinition is local to the module. The system definition remains in the module system.

Redefining system predicate facilitates the definition of compatibility packages. Use in other context is discouraged.

[ISO]retract(+Term)
When Term is an atom or a term it is unified with the first unifying fact or clause in the database. The fact or clause is removed from the database.
retractall(+Head)
All facts or clauses in the database for which the head unifies with Head are removed.
assert(+Term)
Assert a fact or clause in the database. Term is asserted as the last fact or clause of the corresponding predicate.
[ISO]asserta(+Term)
Equivalent to assert/1, but Term is asserted as first clause or fact of the predicate.
[ISO]assertz(+Term)
Equivalent to assert/1.
assert(+Term, -Reference)
Equivalent to assert/1, but Reference is unified with a unique reference to the asserted clause. This key can later be used with clause/3 or erase/1.
asserta(+Term, -Reference)
Equivalent to assert/2, but Term is asserted as first clause or fact of the predicate.
assertz(+Term, -Reference)
Equivalent to assert/2.
recorda(+Key, +Term, -Reference)
Assert Term in the recorded database under key Key. Key is a small integer (range min_tagged_integer ...max_tagged_integer, atom or compound term. If the key is a compound term, only the name and arity define the key. Reference is unified with a unique reference to the record (see erase/1).
recorda(+Key, +Term)
Equivalent to recorda(Key, Value, _).
recordz(+Key, +Term, -Reference)
Equivalent to recorda/3, but puts the Term at the tail of the terms recorded under Key.
recordz(+Key, +Term)
Equivalent to recordz(Key, Value, _).
recorded(+Key, -Value, -Reference)
Unify Value with the first term recorded under Key which does unify. Reference is unified with the memory location of the record.
recorded(+Key, -Value)
Equivalent to recorded(Key, Value, _).
erase(+Reference)
Erase a record or clause from the database. Reference is an integer returned by recorda/3 or recorded/3, clause/3, assert/2, asserta/2 or assertz/2. Other integers might conflict with the internal consistency of the system. Erase can only be called once on a record or clause. A second call also might conflict with the internal consistency of the system.bugThe system should have a special type for pointers, thus avoiding the Prolog user having to worry about consistency matters. Currently some simple heuristics are used to determine whether a reference is valid.
flag(+Key, -Old, +New)
Key is an atom, integer or term. As with the recorded database, if Key is a term, only the name and arity are used to locate the flag. Unify Old with the old value associated with Key. If the key is used for the first time Old is unified with the integer 0. Then store the value of New, which should be an integer, float, atom or arithmetic expression, under Key. flag/3 is a fast mechanism for storing simple facts in the database. The flag database is shared between threads and updates are atomic, making it suitable for generating unique integer counters.32The flag/3 predicate is not portable. Non-backtrackable global variables (nb_setval/2) and non-backtrackable assignment (nb_setarg/3) are more widely recognised special-purpose alternatives for non-backtrackable and/or global state.

4.13.1 Update view

Traditionally, Prolog systems used the immediate update view: new clauses became visible to predicates backtracking over dynamic predicates immediately and retracted clauses became invisible immediately.

Starting with SWI-Prolog 3.3.0 we adhere the logical update view, where backtrackable predicates that enter the definition of a predicate will not see any changes (either caused by assert/1 or retract/1) to the predicate. This view is the ISO standard, the most commonly used and the most `safe'.33For example, using the immediate update view, no call to a dynamic predicate is deterministic. Logical updates are realised by keeping reference-counts on predicates and generation information on clauses. Each change to the database causes an increment of the generation of the database. Each goal is tagged with the generation in which it was started. Each clause is flagged with the generation it was created as well as the generation it was erased. Only clauses with `created' ... `erased' interval that encloses the generation of the current goal are considered visible.

4.13.2 Indexing databases

By default, SWI-Prolog, as most other implementations, indexes predicates on their first argument. SWI-Prolog allows indexing on other and multiple arguments using the declaration index/1. Dedicated index schemas can be build using term_hash/2 or term_hash/4.

[det]term_hash(+Term, -HashKey)
If Term is a ground term (see ground/1), HashKey is unified with a positive integer value that may be used as a hash-key to the value. If Term is not ground, the predicate leaves HashKey an unbound variable. Hash keys are in the range 0 ... 16,777,215, the maximal integer that can be stored efficiently on both 32 and 64 bit platforms.

This predicate may be used to build hash-tables as well as to exploit argument-indexing to find complex terms more quickly.

The hash-key does not rely on temporary information like addresses of atoms and may be assumed constant over different invocations and versions of SWI-Prolog.34Last change: version 5.6.53 The term_hash/2 predicate is cycle-safe. Hashes for numbers differ between bit and little endian machines.

[det]term_hash(+Term, +Depth, +Range, -HashKey)
As term_hash/2, but only considers Term to the specified Depth. The toplevel term has depth 1, its arguments have depth 2, etc. I.e. Depth = 0 hashes nothing; Depth = 1 hashes atomic values or the functor and arity of a compound term, not its arguments; Depth = 2 also indexes the immediate arguments, etc. Using Depth = -1 makes term_hash/4 behave as term_hash/2, hashing ground terms to the full depth.

HashKey is in the range [0 ...Range-1]. Range must be in the range [1 ... 2147483647]