Prolog 8.3.2 GUI can not find [likes] source file

While that is actually a valid next question IMHO it is the wrong next question to ask. The reason is that if you want to do more than just simple learning with Prolog you will start to do many different projects. If all of those projects are in the same directory it can cause problems down the road.

As I primarily use SWI-Prolog on Windows here are some of the things that will help you.

  1. The directory swipl is started from becomes the working directory.
C:\Users\Eric>swipl
Welcome to SWI-Prolog (threaded, 64 bits, version 8.3.0-172-g5e86d749d)
...
1 ?- pwd.
% c:/users/eric/
true.

2 ?- halt.

C:\Users\Eric>cd documents\prolog

C:\Users\Eric\Documents\Prolog>swipl
Welcome to SWI-Prolog (threaded, 64 bits, version 8.3.0-172-g5e86d749d)
...
1 ?- pwd
.
% c:/users/eric/documents/prolog/
true.
  1. Use working_directory/2 to change the working directory with in SWI-Prolog.
C:\Users\Eric>swipl
Welcome to SWI-Prolog (threaded, 64 bits, version 8.3.0-172-g5e86d749d)
...
1 ?- working_directory(D,D).
D = 'c:/users/eric/'.

2 ?- working_directory(D,'c:/users/eric/documents/prolog').
D = 'c:/users/eric/'.

3 ?- pwd.
% c:/users/eric/documents/prolog/
true.

4 ?- working_directory(D,D).
D = 'c:/users/eric/documents/prolog/'.

5 ?-
  1. Prolog source code is loaded using consult/1, or [ ]. See: Getting started quickly

Note: Prolog expects path separators as / and not \ as Windows uses.

C:\Users\Eric>swipl
Welcome to SWI-Prolog (threaded, 64 bits, version 8.3.0-172-g5e86d749d)
...
1 ?- pwd.
% c:/users/eric/
true.

2 ?- consult('C:/Users/Eric/Documents/Projects/Prolog/simple_test.pl').
true.

3 ?- ['C:/Users/Eric/Documents/Projects/Prolog/simple_test.pl'].
true.

4 ?- consult('C:\Users\Eric\Documents\Projects\Prolog\simple_test.pl').
ERROR: Syntax error: Illegal \u or \U sequence
ERROR: consult('C:
ERROR: ** here **
ERROR: \Users\Eric\Documents\Projects\Prolog\simple_test.pl') .
4 ?-
  1. If consult/1 is given a relative path it will use the working directory.
Welcome to SWI-Prolog (threaded, 64 bits, version 8.3.0-172-g5e86d749d)
...
1 ?- pwd.
% c:/users/eric/documents/projects/prolog/
true.

2 ?- consult('simple_test.pl').
true.

3 ?- ['simple_test.pl'].
true.
  1. working_directory/2 can be put in a directive so it is executed when the source code is loaded. This can be used to change the working directory automatically each time the source code is loaded. This is useful if the code is doing I/O to files and the file location is static and known ahead of time, e.g. developing with library(persistency).

File: C:/Users/Eric/Documents/Projects/Prolog/swi-discourse_046.pl

:- working_directory(_,'C:\\Users\\Eric\\Documents\\Projects\\Prolog').
Welcome to SWI-Prolog (threaded, 64 bits, version 8.3.0-172-g5e86d749d)
...
?- working_directory(D,D).
D = 'c:/users/eric/documents/prolog/'.

?- consult("C:/Users/Eric/Documents/Projects/Prolog/swi-discourse_046.pl").
true.

?- working_directory(D,D).
D = 'c:/users/eric/documents/projects/prolog/'.


How I quickly get started with my projects

I use Visual Studio Code as my primary editor.

When working with projects I start by editing the Prolog source code in the editor and at the top of the source I add a section like the following that sets the working directory for Prolog when the code is consulted by SWI-Prolog, e.g.

% -----------------------------------------------------------------------------

% DELETE THIS SECTION BEFORE POSTING

:- if(current_prolog_flag(windows,true)).
:- working_directory(_,'C:/Users/Eric/Documents/Projects/SWI-Prolog/SO_question_100').
:- elif(current_prolog_flag(unix,true)).
:- working_directory(_,'/mnt/c/Users/Eric/Documents/Projects/SWI-Prolog/SO_question_100').
:- endif.

% [SO_question_100].

% ----------------------------------------------------------------------------

The comment includes changing the directory in both a Windows flavor and a Linux flavor as I occasionally switch to working with code on Linux via WSL 2 running a Linux distro.

One common mistake I started to make when using the above was that I would put the module definition after the comment, the module definition has to be the first line of Prolog code, e.g.

:- module(SO_question_100,[]).

% -----------------------------------------------------------------------------

% DELETE THIS SECTION BEFORE POSTING
...
% ----------------------------------------------------------------------------

Since the projects start by opening Visual Studio Code and Visual Studio Code keeps track of projects, by right clicking on the Visual Studio Code icon in the Windows task bar

I can quickly select either a simple project as a single file or a larger project as a folder which starts Visual Studio Code, loads the files or directories and files and then opens the files where they were last left. At the top of the main source code file is a consult comment which I copy and paste into SWI-Prolog once I start it.

With Visual Studio Code I mainly use two plugins for Prolog source code editing. (ref)


Bonus section.

If you start to use SWI-Prolog often and work on Windows you will eventually find that also working with SWI-Prolog on Linux is desired. Take a look at WSL 2 as this will allow you to work with Linux and Windows on the same machine at the same time and share the files on both OS at the same time. Files on Linux can be seen with File Explorer and files on Windows can be seen from Linux as a mount. WSL 2 offers so much more than just file sharing, you can even start an X windows session in Windows and do the work in Linux while keeping the files on Windows.

5 Likes