Home Contents Index Summary Previous Next

3.12 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, 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. This mechanism is considerably faster than the assert/retract mechanism as terms are not compiled, but just copied into the heap.

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. It again is considerably faster than the mechanisms described above, but can only be used to store simple status information like counters, etc.

abolish(:PredicateIndicator)
Removes all clauses of a predicate with functor Functor and arity Arity from the database. Unlike version 1.2, 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. For `cleanup' of the dynamic database, one should use retractall/1 rather than abolish/2.

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.

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. (8)

assert(+Term)
Assert a fact or clause in the database. Term is asserted as the last fact or clause of the corresponding predicate.

asserta(+Term)
Equivalent to assert/1, but Term is asserted as first clause or fact of the predicate.

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 an integer, atom or term. 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. (9)

flag(+Key, -Old, +New)
Key is an atom, integer or term. 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 very fast mechanism for storing simple facts in the database. Example:


:- module_transparent succeeds_n_times/2.

succeeds_n_times(Goal, Times) :-
        flag(succeeds_n_times, Old, 0),
        Goal,
        flag(succeeds_n_times, N, N+1),
        fail ; flag(succeeds_n_times, Times, Old).

3.12.1 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.

For advanced database indexing, it defines hash_term/2:

hash_term(+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 succeeds immediately, leaving HashKey an unbound variable.

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 of SWI-Prolog.