Prev No Next Up Home Keys Figs Search New

Multiple Data Files in SWI-Prolog

Appeared in Volume 10/2, May 1997


timhowe@aol.com
Tim Howe
10th February 1997

I have been creating a database which has two data files for different categories of data. However, the files have some common predicates. When I try to "consult" the second data file, it destroys all the data with the same predicate name that's already loaded. (Apparently SWI-Prolog's "consult" is the same as the standard "reconsult".) I can't find anything in the documentation telling me how to change this behavior.


jan@swi.psy.uva.nl
Jan Wielemaker
11th February 1997

You have a couple of options. One is to use the :- multifile Name/Arity ... to declare your predicates multifile. SWI-Prolog keeps track of which clauses come from which files, and nicely keeps your database up-to-date, except for the order of the clauses defined in different files. The alternative is to load the files into different modules, and write something on top of it:

?- consult(world1:file1),
   consult(world2:file2),
   ....

call_data(D) :-
    data_module(M),
    M:D.

data_module/1 enumerates the various data modules.

Finally, you may decide to write your own predicate for loading a database, based on read and assert. This is sometimes a good idea, as you carry out transformations and check the consistency of the data you are reading. The main code is just:

read_data(File) :-
    open(File, read, Fd),
    read(Fd, First),
    read_data(First, Fd),
    close(Fd).

read_data(end_of_file, _) :- !.
read_data(Term, Fd) :-
    assert(Term),
    read(Fd, Next),<
    read_data(Next, Fd).

This is as efficient as consult/1 in SWI-Prolog, which compiles everything.

The above is applicable for various Prolog implementations, notably Quintus and SICStus.


timhowe@aol.com
Tim Howe
12th February 1997

Is the multifile approach standard? I want to make it as compatible as possible.


jan@swi.psy.uva.nl
Jan Wielemaker
13th February 1997

It is definitely part of Quintus, SICStus, and SWI. It is also part of the ISO standard, see page 219 of 'Prolog: The standard'.

Prev No Next Up Home Keys Figs Search New