The facts and rules discussed so far are static - that is, they are unchanged throughout the set of queries we run.
For a complex system (e.g. simulations, derivations, etc) it is often useful to be able to add new facts as we recognize them, and make other facts obsolete. For example, if we are simulating a population of animals then we may wish to change the location, health, and behaviour of individuals within the population over time.
Facts which can be altered are termed dynamic facts in prolog, and involve three steps: Identifying that a fact is dynamic, Asserting a new fact, Retracting an obsolete fact.
location(Indiv, Place)
specifies
the current location of an individual, we may wish to alter it over time.
The code to specify this is dynamic looks like
:- dynamic(location/2).
If we had a variety of dynamic facts our initial declaration might look like
:- dynamic(location/2). :- dynamic(health/2). :- dynamic(inventory/2). % etc
assertz(Fact).
is used to assert a new fact during the course of a query,
and put it at the end of the relevant fact list.
e.g. assertz(location(turtle,pond)).
asserta(Fact).
also asserts a new fact, but puts it at the front of the
list of relevant facts.
retract(Fact).
is used to retract an obsolete fact, which is often done
just prior to replacing it with a new one, e.g.
move_individual(Indiv, NewLoc) :- retract(location(Indiv, _)), assertz(location(Indiv, NewLoc)).