11.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.
-
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.
- If Proc is already defined as a dynamic procedure,
the assertion adds ProcClause
to the database after any clauses already existing for Proc.
- 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 builtins 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.