[top] [up] [next]

Prolog database views


Trinc-Prolog supports two different kinds of database views, the immediate view and the database update view. These views affect the behavior of the processor when it searches a match for a goal. The results of the predicates assertz/1, retract/1 and abolish/1 are affected by the current database view.

The immediate view

All the changes to the database are immediately visible if the immediate view is chosen. The following example illustrates this, suppose the database contains the following clauses:

  dynamic(a/1).
  a(1).
  a(2).

  test( X ) :-
    a(X),
    retract( a(2) ),
    fail.

The main goal to prove is test(E), the first time the goal a(X) is executed both a(1) and a(2) are still present, a match is found with a(1). The retract/1 operation then retracts a(2) from the database. The next goal starts the failing of the clause, the goal retract/1 also fails. A redo of a(X) is started but because the clause a(2) was removed no alternative is found and the predicate test/1 fails.
The database was changed between the first time a(X) was visited and the second time, the change was immediately visible. Historically this was the way that many prolog processors handled changing predicates.

The database update view

The ISO prolog standard defines that not the immediate view but the database update view defines the correct behavior of a prolog processor. The general rule for this view is that the set of clauses relevant to an active goal is not altered in any way by any other predicate as compared to the first time it was activated.
For the example above this would mean that a(2) would still be found, even if retract( a(2) ) would have executed successfully.

Suppose the database would contain the following predicates:

  dynamic( a/1).
  a(1).
  a(2).

  test( X ) :-
    a(X),
    retract( a(2) ),
    a(X),
    fail.

The first time a(X) is executed the database still contains a(1) and a(2), a match with a(1) is found. The a(2) predicate is retracted from the database by retract/1. The database now contains a(1), the second goal a(X) is called and a match is found with a(1), then the failing of the goal starts. Redo of the last a(X) fails, retract/1 also fails and then a redo is attempted of the first a(X). This redo succeeds because a match is found with a(2).

The database view is the default view used by Trinc-Prolog. The immediate view is the view that looks and feels the most 'natural' for many prolog programmers. To make a prolog program independent of a current view there are the following rules:

  1. asserta/1 may be used without restrictions.

  2. Never use retract/1 or assertz/1 on a predicate which is active except to retract already used clauses.

  3. Never use abolish/1 on a predicate which is active.

The database view is the default view used by Trinc-Prolog and there is no difference in behavior between instances of classes and predicates in any of the two views.

[top] [up] [next]

 

info@trinc-prolog.com