2
votes

Lets say I have a file main.pl with the following content

/* I tried these one at a time, not all at once... */
[externalFile].
['externalFile'].
['externalFile.pl'].
['./externalFile.pl']./*this was an answer to a similar SO question*/
consult(externalFile).
/*... etc ...*/

now lets use it...

$ swipl -l externalFile.pl
/* 12 second loading delay */
/* everything works perfect! */
$ swipl -l main.pl
/* 0 second loading delay */
/* let's try anything from externalFile... */
ERROR: toplevel: Undefined procedure: [...]
/* main.pl might as well be empty */
?- consult(externalFile).
/* 0 second loading delay */
true.
/* true? oh my bad! Lets try again... */
ERROR: toplevel: Undefined procedure: [...]
?- [externalFile].
/* 12 second loading delay... seems better... */
true.
/* been lied to before... */
/* and yet every thing works perfect now! */

note: consult(externalFile). only has no effect when consult(externalFile). is in main.pl. Otherwise, consult(externalFile). does succeed in importing the file while in a terminal session.

So how do I write source code without resorting to having to copy and paste my whole program into the terminal (or just have all my program source code have a file size of 15+ MEGA bytes worth of manual inclusions!) if the terminal is the only place where import commands will work?


note: I have also checked the line separator encoding (dos vs windows) (this was the answer to another question)

note: independent clauses defined directly within main.pl work fine

1
You should look at the Prolog documentation. [] in this context is same as consult(). These are directives. Directives require :-. So, for example, :- [externalFile]. or :- consult(externalFile)..lurker
@lurker why not writing as an answer, seems complete enough and very helpful :) ?coder

1 Answers

4
votes

Operations such as consult are referred to in Prolog as directives. Prolog directives are recognized by Prolog differently depending upon whether you are using the interactive environment, or you are executing a directive from a Prolog source file.

In the interactive environment, a directive is just typed in at the ?- prompt:

?- consult('myfile').

When you consult a file, Prolog sees the file as a series of assertions of facts and rules. However, if Prolog sees the :- operator, it treats the line as a directive in the file. So from a file, you must use that operator:

:- consult('myfile').

The brackets [] are sort of a short hand for consult(). You are giving Prolog a list of files to consult. The above would be equivalent to:

?- [myfile].

And

:- [myfile].

There are also different forms that are acceptable. A consult argument must be an atom. If you have a file called myfile.pl, it can be consulted as:

:- [myfile].
:- ['myfile'].
:- ['myfile.pl'].

And similarly:

:- consult(myfile).
:- consult('myfile').
:- consult('myfile.pl').

consult(myfile.pl) or [myfile.pl] will result in an error since the, without the quotes, myfile.pl will not be seen as a single atom.

The list ([]) form of consult will accept multiple files:

:- [myfile, anotherfile].

To address your comment:

consult(externalFile). only has no effect when consult(externalFile). is in main.pl.

When you put consult(externalFile). on a line by itself in a file (like main.pl) that you consult, you are telling Prolog that you wish to assert the term (fact in this case), consult(externalFile) which consists of the functor consult and has one atomic argument, externalFile. So it actually did do something, just not what you were expecting.