Chap11a: assertz

Prolog has two database manipulation commands: assertz and retract. Let’s see how these are used. Suppose we start with an empty database. So if we give the command:

?- listing.

then Prolog will simply respond true; the listing (of course) is empty.

Suppose we now give this command:

?- assertz(happy(mia)).

This succeeds ( assertz/1 commands always succeed). But what is important is not that it succeeds, but the side-effect it has on the database. For if we now give the command

?- listing.

We get the fact. That is, the database is no longer empty: it now contains the fact we asserted.

Suppose we then made four more assert commands:

?- assertz(happy(vincent)). ?- assertz(happy(marcellus)). ?- assertz(happy(butch)). ?- assertz(happy(vincent)).

and then ask for a listing:

?- listing.

All the facts we asserted are now in the knowledge base. Note that happy(vincent) is in the knowledge base twice. As we asserted it twice, this seems sensible.