Previous Up Next

12.2  Altering programs at run time

The Prolog database can be updated during the execution of a program. ECLiPSe allows the user to modify procedures dynamically by adding new clauses via assert/1 and by removing some clauses via retract/1.

These predicates operate on dynamic procedures; if it is required that the definition of a procedure be altered through assertion and retraction, the procedure should therefore first be declared dynamic (see the previous section). The effect of assert/1 and retract/1 on static procedures is explained below.

The effect of the goal

assert(ProcClause)

where ProcClause1 is a clause of the procedure Proc, is as follows.

  1. If Proc has not been previously defined, the assertion raises an exception, however the default handler for this exception just declares the given procedure silently as dynamic and executes the assertion.
  2. If Proc is already defined as a dynamic procedure, the assertion adds ProcClause to the database after any clauses already existing for Proc.
  3. If Proc is already defined as a static procedure, then the assertion raises an exception.

The goal

retract(Clause)

will unify Clause with a clause on the dynamic database and remove it. If Clause does not specify a dynamic procedure, an exception is raised.

ECLiPSe’s dynamic database features the so-called logical update semantics. This means that any change in the database that occurs as a result of executing one of the built-ins of the abolish, assert or retract family affects only those goals that start executing afterwards. For every call to a dynamic procedure, the procedure is virtually frozen at call time.


1
It should be remembered that because of the definition of the syntax of a term, to assert a procedure of the form p :- q,r it is necessary to enclose it in parentheses: assert((p:-q,r)).

Previous Up Next