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 is faster than the mechanisms described above, but can only be used to store simple status information like counters, etc.
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)
. The predicate abolish/2
conforms to the Edinburgh standard, while abolish/1
is ISO compliant.
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.
recorda(Key, Value, _)
.
recordz(Key, Value, _)
.
recorded(Key, Value, _)
.
:- 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) ). |
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'. (24) 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.
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:
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.