Next: Indexing databases
Up: Built-In Predicates
Previous: Grammar rule interface
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 it's
derivates become part of the program these predicates compile the term
given to them. R etract/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( +Functor, +Arity)
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.
- 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 facilates 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)
Allfacts 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.
- 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.
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.
- 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, atom or arithmetic integer
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, _, 0),
Goal,
flag(succeeds_n_times, N, N+1),
fail ; flag(succeeds_n_times, Times, Times).
Next: Indexing databases
Up: Built-In Predicates
Previous: Grammar rule interface
Passani Luca
Tue Nov 14 08:58:33 MET 1995