next up previous contents
Next: Sometimes You have Up: Changing the Program Previous: Changing the Program

Do Not Do It!

The Prolog database is the set of clauses loaded into Prolog via consult/1 or reconsult/1 (these predicates can also be used at run-time so they are subject to the same strictures as the rest described below).

If, during run-time, a new clause is introduced into the Prolog database then this can change the behaviour of the program as, often, the program's meaning changes.

The predicates that we refer to are as follows:

Note that all the predicates except retract/1 are determinate. They are not resatisfiable. The predicate abolish/2 has mode abolish(+,+) while the predicate retract/1 can be used with mode retract(-). This latter predicate can therefore be used to `wipe out' a complete program as in:

 
?- retract(X),fail.
 [-5pt]

This will fail with the side-effect of removing all the clauses loaded. We can remove just some clauses as in:
 
?- retract(foo(1,X)).
 [-5pt]

will remove all clauses whose heads unify with foo(1,X).

Note that to add a clause which is also a rule you will need to write assert((a:- b)) and not assert(a:-b). See chapter 10 for an explanation.

Together, these predicates can be used to implement global flags and a form of global variable. This almost always makes it harder to understand individual parts of the program ---let alone the disastrous effect such changes have on the declarative reading of programs.

All these predicates are side-effecting. Therefore, backtracking will not undo these side-effects. For example, if assert/1 is used to maintain a database of results found so far then, on backtracking, Prolog will not remove these results.

Further, the program becomes sensitive to interupts. It has been known for someone to abort a program (using ^C and then a for abort) between the asserting of a new clause and the retracting of an old clause ---leaving an unexpected old clause around which interfered badly with the subsequent execution of the program.

If a problem seems to require the use of assert/1 then, usually, there is another way of doing things.



next up previous contents
Next: Sometimes You have Up: Changing the Program Previous: Changing the Program



Paul Brna
Mon May 24 20:14:48 BST 1999