next up previous contents
Next: Interactive Program Development Up: Interlude: Practical Matters Previous: Exiting and Leaving

Loading Files

A program should normally be considered as a sequence of files. Consequently, it is usually necessary for Prolog to read in one or more files at the beginning of a session.

The standard command is

 ?-  consult(filename).

where ``filename'' is some legal unix filename. Because some legal unix filenames contain characters that Prolog may find syntactically illegal it is often necessary to `protect' the filename using single quotes. Here are some arbitrary examples:

 ?-  consult(foo).

?- consult('/u/ai/s2/ai2/aifoo/program').

?- consult('').

It is also possible to consult a set of files as in:

 ?-  consult([foo,baz,'']).

There is a shorthand for the command consult which can be confusing. The abbreviation overloads the symbols associated with list notation. The command consult(foo) can be abbreviated to [foo] and the command consult([foo,baz]) can be rewritten [foo,baz]. There is quite a subtle syntax error that can cause difficulties when the file you want to read in needs to be protected with single quotes. Consider:
 ?-  ['].


Prolog is still waiting for the closing single quote. All you have to do is type in the closing single quote and then the ]. and press Return. Prolog will produce an error message because you have asked to load a very strangely named file.

Another error is to use double quotes instead of single quotes.

 ?-  [""].

{ERROR: absolute_file_name(102,_45) - invalid file spec}

This weird error will not be explained here ---just note that double quotes have a special interpretation in Prolog which results in the above command being interpreted as the desire to consult three files: 102, 111 and 111. Can you guess the meaning of double quotes?

Each syntactically correct clause that is found on reading the file will be loaded. On encountering a syntactically incorrect clause then an error message will be printed. We now illustrate some common syntax errors together with the error messages generated. You will notice that the error messages can be quite obscure.

 foo (X). 		% space between functor and left bracket

** bracket follows expression **


** here **

( X ) .

 fooX). 		% missing left bracket

** operator expected after expression **


** here **

 foo(X. 		% missing right bracket

** , or ) expected in arguments **

foo ( X

** here **

 foo(X Y). 		% missing argument separator

** variable follows expression **

foo ( X

** here **

 foo([a,b). 		% missing right square bracket

** , or ] expected in list **

foo ( [ a , b

** here **

 foo(a) 		% missing `.'


** atom follows expression **

foo ( a )

** here **

foo ( b ) .

 foo(a), 		% used `,' for `.'


{ERROR: (,)/2 - attempt to redefine built_in predicate}

This latter error message is caused because the input is equivalent the the logical statement foo(a) foo(b) which is not in Horn clause form and therefore not legal Prolog. Here is another related error:
 foo;- baz. 		% ; instead of :

{ERROR: (;)/2 - attempt to redefine built_in predicate}

We suggest that, if you have made a syntax error and pressed Return (so you cannot delete the error) then type in `.' followed by Return. This will probably generate a syntax error and you can try again. Of course, there are situations for which this will not work: you cannot use this method to get out of the problem with:

 ?-  ['].


or the equivalent problem with double quotes.

Now SICStus does one nice thing: consult(foo) will first try to find a file ``''. If it does not find one, it will look for ``foo''.

next up previous contents
Next: Interactive Program Development Up: Interlude: Practical Matters Previous: Exiting and Leaving

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