Dynamic Predicates

All predicates in Prolog fall into one of two categories: static or dynamic. Dynamic predicates can be modified when a program is running; in contrast, static predicates can be modified only by reloading or by abolish/[1,2].

If a predicate is first defined by being loaded from a file, it is static by default. Sometimes, however, it is necessary to add (assert), remove (retract), or inspect (using clause/[2,3]) clauses for a predicate while a program is running. In order to do that, you must declare the predicate to be dynamic. A predicate can be made dynamic by specifically declaring it to be so, as described below, or by using one of the assertion predicates. For a list of the assertion predicates, and for more information on using them, refer to ref-mdb-dsp.

To make a predicate dynamic, you insert in the file containing the predicate a line, which declares the predicate to be dynamic. The format of the line is

     :- dynamic name/arity.

So, for example, the following declarations make the named predicates dynamic.

     :- dynamic exchange_rate/3, spouse_of/2, gravitational_constant/1.


  1. The :- symbol must appear at the beginning of any line with a dynamic declaration, as shown above.
  2. Dynamic declarations can only appear in files; dynamic/1 cannot be called as a predicate.
  3. The line that declares a predicate to be dynamic must occur before any definition of the predicate itself in the file.