next up previous contents index
Next: Update view Up: Built-in predicates Previous: DCG Grammar rules   Contents   Index

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 assert1 or one of its derivatives become part of the program these predicates compile the term given to them. retract1 and retractall1 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 dynamic1.

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.

abolish1: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, abolish1 can only be applied to dynamic procedures. This is odd, as for dealing with dynamic procedures there is already retract1 and retractall1. The abolish1 predicate has been introduced in DEC-10 Prolog precisely for dealing with static procedures. In SWI-Prolog, abolish1 works on static procedures, unless the prolog flag iso is set to true.

It is adviced to use retractall1 for erasing all clauses of a dynamic predicate. abolish2+Name, +Arity Same as abolish(Name/Arity). The predicate abolish2 conforms to the Edinburgh standard, while abolish1 is ISO compliant. redefine_system_predicate1+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. retract1+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. retractall1+Head All facts or clauses in the database for which the head unifies with Head are removed. assert1+Term Assert a fact or clause in the database. Term is asserted as the last fact or clause of the corresponding predicate. asserta1+Term Equivalent to assert1, but Term is asserted as first clause or fact of the predicate. assertz1+Term Equivalent to assert1. assert2+Term, -Reference Equivalent to assert1, but Reference is unified with a unique reference to the asserted clause. This key can later be used with clause3 or erase1. asserta2+Term, -Reference Equivalent to assert2, but Term is asserted as first clause or fact of the predicate. assertz2+Term, -Reference Equivalent to assert2. recorda3+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 erase1). recorda2+Key, +Term Equivalent to recorda(Key, Value, _). recordz3+Key, +Term, -Reference Equivalent to recorda3, but puts the Term at the tail of the terms recorded under Key. recordz2+Key, +Term Equivalent to recordz(Key, Value, _). recorded3+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. recorded2+Key, -Value Equivalent to recorded(Key, Value, _). erase1+Reference Erase a record or clause from the database. Reference is an integer returned by recorda3 or recorded3, clause3, assert2, asserta2 or assertz2. 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.The 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. flag3+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. flag3 is a very fast mechanism for storing simple facts in the database. Example:


\begin{code}
:- module_transparent succeeds_n_times/2.
\par succeeds_n_times(Goa...
...eds_n_times, N, N+1),
fail
; flag(succeeds_n_times, Times, Old)
).
\end{code}



Subsections
next up previous contents index
Next: Update view Up: Built-in predicates Previous: DCG Grammar rules   Contents   Index
Dr. Richard Botting 2001-12-12