A CLP( ) program is divided into static rules, which do not change,
and dynamic rules, which allow the rulebase to be modified via assert/1
and retract/1 as well as by
consulting .
As mentioned above, static rules/code cannot span more than one chunk.
bsphackfloatpenalty -Mii
floatpenalty-Miii
parmoderrfloatpenalty@
nextcurrboxfreelist
currbox-2floatpenalty@ fltovf
currboxtempboxa
tempboxa
@floatesphackbsphackfloatpenalty -Mii
floatpenalty-Miii
parmoderrfloatpenalty@
nextcurrboxfreelist
currbox-3floatpenalty@ fltovf
currboxtempboxa
tempboxa
@floatesphack
Dynamic code on the other hand can be defined anywhere and dynamic
rules can be added by asserting them during execution
or by consulting a program file, which behaves as if those definitions
were asserted.
The only requirement for rules intended to be dynamic is that the
particular predicate name has
to be pre-declared using dynamic/2 which ensures that
all uses of this predicate are now dynamic, e.g. ?- dynamic(foo, 2).
The first argument is the name of the predicate and the second is its
arity
.
Every dynamic declaration has to occur before any use of a dynamic predicate
is made (including rule, assert and retract), otherwise
an error is generated with any of the preceeding system predicates and
any use of that predicate is assumed to be static.
bsphackfloatpenalty -Mii
floatpenalty-Miii
parmoderrfloatpenalty@
nextcurrboxfreelist
currbox-4floatpenalty@ fltovf
currboxtempboxa
tempboxa
@floatesphack
Declaring a predicate to be dynamic allows the use of rule/2
to inspect the rulebase,
assert/1 to add new rules and retract/1 to delete rules.
The operational semantics of the
assert, rule and retract family of system
predicates is that any modifications to the rulebase
occur immediately and are immediately available for use.
This is called the immediate update view [17].
Consider the following example:
?- dynamic(p,0).
p :- assert(p), fail.
This will cause the goal ``?- p.'' to succeed. Apart from the dynamic declaration and the immediate update semantics, there is no difference between static and dynamic code and they may be used interchangeably, e.g. both can be listed with ls/1. Dynamic code is also compiled but is generally not as efficient as static code and also less deterministic. Also note that the semantics of assert, rule and retract are an enhancement of that in PROLOG (see Section 3.4.2).