projog

3.95. dynamic/1 - indicates that a user defined predicate is dynamic.

If a user defined predicate is dynamic then it can be updated after it is first defined. It can subsequently have clauses removed using retract/1 and retractall/1 or new clauses added using asserta(X), assertz(X) and consult/1.

Examples

?- dynamic(true/0).

Cannot replace already defined built-in predicate: true/0

?- dynamic(is/2).

Cannot replace already defined built-in predicate: is/2

?- dynamic(test/2).

yes

?- write_to_file('dynamic1.tmp', 'test(a,1). test(b,2). test(c,3). test(d,4). test(e,5). testRule(X) :- test(X, Y), Y mod 2 =:= 0.').

yes

?- consult('dynamic1.tmp').

yes

?- testRule(X).
X = b

yes;
X = d

yes;

no

?- write_to_file('dynamic2.tmp', 'test(f,6). test(g,7). test(h,8).').

yes

?- consult('dynamic2.tmp').

yes

?- testRule(X).
X = b

yes;
X = d

yes;
X = f

yes;
X = h

yes

?- assertz(test(x,180)).

yes

?- asserta(test(y,42)).

yes

?- testRule(X).
X = y

yes;
X = b

yes;
X = d

yes;
X = f

yes;
X = h

yes;
X = x

yes

Not OK to call dynamic/1 on a predicate that has already been defined and is not marked as dynamic.

?- write_to_file('dynamic3.tmp', 'not_dynamic(1,2,3).').

yes

?- consult('dynamic3.tmp').

yes

?- dynamic(not_dynamic/3).

Predicate has already been defined and is not dynamic: not_dynamic/3

OK to call dynamic/1 on a predicate that has already been marked as dynamic.

?- dynamic(test/2).

yes

write_to_file(Filename, Contents) :-
open(Filename, write, Z),
set_output(Z),
writef(Contents),
close(Z),
set_output('user_output').