Chap11b: retract

The database manipulations we have been making have changed the meaning of the predicate happy/1 . More generally, database manipulation commands give us the ability to change the meaning of predicates while we are running programs. Predicates whose definitions change during run-time are called dynamic predicates, as opposed to the static predicates that we have previously dealt with. Most Prolog interpreters insist that we explicitly declare the predicates that we wish to be dynamic. We will soon examine an example involving dynamic predicates, but let’s first complete our discussion of the database manipulation commands.

Now that we know how to assert new information into the database, we should also learn how to remove information when we no longer need it. There is an inverse predicate to assertz/1 , namely retract/1 . For example, if we carry straight on from the previous example by giving the command:

:- dynamic happy/1. happy(mia). happy(vincent). happy(marcellus). happy(butch). happy(vincent). ?- retract(happy(marcellus)).

and then list the database, we get:

?- listing.

That is, the fact happy(marcellus) has been removed.

Suppose we go on further, and say

?- once(retract(happy(vincent))).

and then ask for a listing. We get:

?- listing.

Note that the first occurrence of happy(vincent) , and only the first occurrence, was removed.

To remove all of our assertions contributing to the definition of the predicate happy/1 we can use a variable:

?- retract(happy(X)).

A listing reveals that the database is now empty.

?- listing.