next up previous contents
Next: What You Should Up: Changing the Program Previous: Do Not Do

Sometimes You have To!

There are one or two occasions when you might want to use these predicates. The main one is when you have definitely proved that something is the case. That is, there is no way in which some statement (added to the program as a clause) can be false. Sometimes, of course, a program is supposed to modify the Prolog database. For example, consult/1 and reconsult/1.

Often, we do not want to modify the program itself ---rather, we want to change the data the program accesses. There is a facility in Edinburgh Prolog known as the recorded database. This is a way of storing Prolog terms under a key. Such terms are hidden from the listing/0 program. The predicates that access this recorded database are:

These can be used to squirrel away information to be used by the program itself. An example is the predicate random/2:

random(Range,Num):-    				             % to choose random number in range

recorded(seed,Seed,Ref), % get seed from database

erase(Ref), % delete old value of seed

Num is (Seed mod Range) + 1, % fit seed into range

NewSeed is (125*Seed+1) mod 4093, % calculate new value

record(seed,NewSeed,_Ref). % and assert it into database [-5pt]

This shows how we can maintain information about the seed used to generate the next pseudo-random number. Note that, unless we want to delete an entry (using erase/1) we usually use an anonymous variable for the record reference.

Using this family of predicates is more elegant (and sometimes more efficient) but suffers from the same problems as the assert family.

Paul Brna
Mon May 24 20:14:48 BST 1999