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.
Following the Edinburgh tradition, SWI-Prolog provides database keys to clauses and records in the recorded database. As of 5.9.10, these keys are represented by non-textual atoms (`blobs', see section 9.4.6), which makes accessing the database through references safe.
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.
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, _)
.current_key(Key), recorded(Key, Value, Reference)
recorded(Key, Value, _)
.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.
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 built using term_hash/2 or term_hash/4.
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 big and little endian machines.
HashKey is in the range [0 ...Range-1]. Range must be in the range [1 ... 2147483647]
This predicate raises an exeption when trying to compute the hash on a cyclic term or attributed term. Attributed terms are not handled because subsumes_chk/2 is not considered well defined for attributed terms. Cyclic terms are not supported because this would require establishing a canonical cycle. I.e., given A=[a|A] and B=[a,a|B], A and B should produce the same hash. This is not (yet) implemented.
This hash was developed for lookup of solutions to a goal stored in a table. By using a cryptographic hash, heuristic algorithms can often ignore the possibility of hash-colisions and thus avoid storing the goal-term itself as well as testing using =@=/2.