Standard Prolog contains four built-in predicates which may modify the database:
A problem arises if one of these predicates is activated during a query. Since this potentially changes the clauses that are available to be chosen at a given choice point.
The standard adopts the ``logical'' database update viewpoint. In this view retracted clauses remain as choice points for the query but not the appended ones. Thus a query of the form.
setof(X, foo(X), S)where foo/1 does something like
will evaluate the setof/3 for the value of the database of foo/1 as it is at the beginning of the query, ignoring the added clauses.
Compare the two predicates go/0 and go1/0
Since in practice this may be counter intuitive it is as well to observe the following rules, (taken from the book of Deransart, Ed-Dbali and Cervoni.
Since for some the logical update view is somewhat unexpected. It is worth giving some justification for the choice made by the ISO working group. There are two principal reasons for choosing the logical view:
To see why it is easier to reason about a program when the logical update view is in force. Observe that the search tree has the property that when a node is first reached it is possible to determine the number of children that the node has. This is not possible using the immediate view since any (dynamic) predicate might acquire additional clauses. This raises the question of whether a node that is closed (meaning all its children have been visited) can possible be reopened by an assert. Worse what if a retract cuts off the very branch which is currently being explored?
For more on the semantics of updates to the Prolog database see:
E. Boerger, B. Demoen `A framework to specify database update views for Prolog' Proceedings of the Third International Symposium on Programming Language Implementation and Logic Programming Passau, August 1991, pp. 147-158