12.1.1 Reading in Programs

In fact, you already know a way of telling Prolog to read in predicate definitions that are stored in a file. Right! [FileName1,FileName2]. You have been using queries of that form all the time to tell Prolog to consult files. By putting

:- [FileName1,FileName2].

at the top of a file, you can tell Prolog to consult the files in the square brackets before reading in the rest of the file.

So, suppose that you keep all predicate definitions that have to do with basic list processing, such as append, member, reverse etc., in a file called listpredicates.pl. If you want to use them, you just put

:- [listpredicates].

at the top of the file you want to use them in. Prolog will consult listpredicates, when reading in that file, so that all predicate definitions in listpredicates become available.

On encountering something of the form :- [file,anotherfile], Prolog just goes ahead and consults the files without checking whether the file really needs to be consulted. If, for example, the predicate definitions provided by one of the files are already available, because it already was consulted once, Prolog still consults it again, overwriting the definitions in the database. The inbuilt predicate ensure_loaded/1 behaves a bit more clever in this case and it is what you should usually use to load predicate definitions given in some other file into your program. ensure_loaded basically works as follows: On encountering the following directive

:- ensure_loaded([listpredicates]).

Prolog checks whether the file listpredicates.pl has already been loaded. If not, Prolog loads it. If it already is loaded in, Prolog checks whether it has changed since last loading it and if that is the case, Prolog loads it, if not, it doesn't do anything and goes on processing the program.


Patrick Blackburn, Johan Bos and Kristina Striegnitz
Version 1.2.5 (20030212)