|- Up -||Next >>|
Prolog has four database manipulation commands: assert, retract, asserta, and assertz. Let's see how these are used. Suppose we start with an empty database. So if we give the command:
we simply get a yes; the listing (of course) is empty.
Suppose we now give this command:
It succeeds (
assert commands always succeed). But what is important is not that it succeeds, but the side-effect it has on the database. If we now give the command:
we get the listing:
That is, the database is no longer empty: it now contains the fact we asserted.
Suppose we then made four more assert commands:
Suppose we then ask for a 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.
So far, we have only asserted facts into the database, but we can assert new rules as well. Suppose we want to assert the rule that everyone who is happy is naive. That is, suppose we want to assert that:
naive(X) :- happy(X).
We can do this as follows:
assert( (naive(X) :- happy(X)) ).
Note the syntax of this command: the rule we are asserting is enclosed in a pair of brackets. If we now ask for a listing we get:
Now that we know how to assert new information into the database, we need to know how to remove things form the database when we no longer need them. There is an inverse predicate to
retract. For example, if we go straight on and give the command:
and then list the database we get:
That is, the fact
happy(marcellus) has been removed. Suppose we go on further, and say
and then ask for a listing. We get:
Note that the first occurrence of
happy(vincent) (and only the first occurrence) was removed.
To remove all of our assertions we can use a variable:
X = mia ;
X = butch ;
X = vincent ;
A listing reveals that the database is now empty:
If we want more control over where the asserted material is placed, there are two variants of assert, namely:
assertz. Places asserted material at the end of the database.
asserta. Places asserted material at the beginning of the database.
For example, suppose we start with an empty database, and then we give the following command:
assert( p(b) ), assertz( p(c) ), asserta( p(a) ).
Then a listing reveals that we now have the following database:
Database manipulation is a useful technique. It is especially useful for storing the results to computations, so that if we need to ask the same question in future, we don't need to redo the work: we just look up the asserted fact. This technique is called `memoization', or `caching'.
Here's a simple example. We create an addition table for adding digits by using database manipulation.
D is B+C,
member/2 is the standard membership predicate which tests for membership in a list.)
What does this program do? It takes a list of numbers A, uses
member to select two numbers B and C of this list, and then adds B and C together calling the result D. Now for the important bit. It then asserts the fact that it has discovered (namely that D is the sum of A and B), and then fails. Why do we want it to fail? Because we want to force backtracking! Because it has failed, Prolog will backtrack to
member(C,A) and choose a new value for C, add this new C to B two create a new D, and then assert this new fact. it will then fail again. This repeated failure will force Prolog to find all values for
member(C,A), and add together and assert all possible combinations.
For example, when we give Prolog the command
It will come back and say No. But it's not this response that interests us, its the side-effect on the database that's important. If we now ask for a listing we see that the database now contains
Question: how do we remove all these new facts when we no longer want them? After all, if we simply give the command
Prolog is going to go through all 100 facts and ask us whether we want to remove them! But there's a much simpler way. Use the command
Again, the purpose of the
fail is to force backtracking. Prolog removes the first fact about
sum in the database, and then fails. So it backtracks and removes the next fact about sum. So it backtracks again, removes the third, and so on. Eventually (after it has removed all 100 items) it will fail completely, and say No. But we're not interested in what Prolog says, we're interested in what it does. All we care about is that the database now contains no facts about
To conclude our discussion of database manipulation, a word of warning. Although it can be a useful technique, database manipulation can lead to dirty, hard to understand, code. If you use it heavily in a program with lots of backtracking, understanding what is going on can be a nightmare. It is a non-declarative, non logical, feature of Prolog that should be used cautiously.
|- Up -||Next >>|