Prolog 8.3.2 GUI can not find [likes] source file

Hello, I’m very new to Prolog. I installed SWI-Prolog 8.3.2 for Windows 32 bits without any installation options changed from defaults. Installation is completed. I run program by click on SWI-Prolog icon on Windows Start menu. I try to load likes.pl as described in Tutorials from https://www.swi-prolog.org/pldoc/man?section=quickstart. It seems prolog can not find likes.pl and show error messages.

What can I do to solve this problem ?

However, If I run SWI-Prolog (Console) version. It can find and load likes.pl without any errors.

Thank you.

I think that you have to provide the complete directory specification within square braces (which is a shortcut for the consult command). As an alternative you can load the file by going to the File menu and choose Consult.
Cheers/JC

1 Like

Thanks for your reply. I test your suggestion by type [“c:/Program Files/swipl/demo/likes”]. It works by replies “true.”.

But Console version work fine just type [likes], Does it mean, by default, swipl_win.exe and swipl.exe have different directory for configuration ? and How can I know what is that directory.

When you run swipl_win or swipl try this:

?- pwd.
%<it will print directory here>
3 Likes

In Console version, It is % c:/program files/swipl/

In GUI version, It is % c:/users/sam/documents/prolog/ (sam is user name.) And after I changed directory by type cd("c:/program files/swipl/"). Type [likes]. can work now. :smiley: Thank you.

This guide me next question, How to auto config default directory since start swipl_win.exe ?

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.

Since the projects start by editing the Prolog source code in the editor at the top of the source I add a comment with the consult, e.g.

% consult("C:/Users/Eric/Documents/Projects/Prolog/SO_question_100.pl").

Also 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

best reply ever

1 Like

You can use the [ Settings / USer init file… ] menu.
Beware that the editor window could be rather slow to open, and it probably will issue
a message box, asking for creation of the the init.pl in the appropriate folder - the path changed recently - that could be hidden behind - for instance - a full screen browser window.

When you have the init.pl file open - presumably blank - you can set an initialization path be means of a directive, like

:- cd('C:\\Users\\Carlo\\tex2swi').
:- maplist(edit, ['tex2swi.pl','gen_swinb.pl','parse_tex.pl','tex2swi.plt']).

Next time you will start Prolog it will attempt to change to such directory.
You can put in your init.pl a lot of utilities, for instance it’s not difficult to reload a bunch of files - your preferred ‘workspace’, as you can see above.

2 Likes

One way to avoid depending on the working directory for loading likes.pl is this

?- [swi(demo/likes)].

If you want to change the start dir, I guess the best is to edit the Windows shortcut. Remove --win-app, which clauses the system to create prolog in the standard document folder and go there. Next set the start folder as you please.

Once upon a time you could double click a *.pl file to open it in Prolog (and Prolog would go to the directory of this file). I don’t know whether this still works. Don’t have a Windows machine around … What should work is to drop a *.pl files onto the Prolog desktop shortcut.

1 Like

This works on Windows 10

Welcome to SWI-Prolog (threaded, 64 bits, version 8.3.0-172-g5e86d749d)
SWI-Prolog comes with ABSOLUTELY NO WARRANTY. This is free software.
Please run ?- license. for legal details.

For online help and background, visit https://www.swi-prolog.org
For built-in help, use ?- help(Topic). or ?- apropos(Word).

?- [swi(demo/likes)].
true.

?- listing(likes).
likes(sam, Food) :-
    indian(Food),
    mild(Food).
likes(sam, Food) :-
    chinese(Food).
likes(sam, Food) :-
    italian(Food).
likes(sam, chips).

true.

?- likes(sam,Food).
Food = dahl ;
Food = tandoori ;
Food = kurma ;
Food = chow_mein ;
Food = chop_suey ;
Food = sweet_and_sour ;
Food = pizza ;
Food = spaghetti ;
Food = chips.

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

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

Notice that the working directory is different than the location for likes, i.e. C:\Program Files\swipl\demo\likes.pl



On my Windows 10 system is

C:\Users\Eric\Documents\Projects\Prolog\swi-discourse_045.pl

% consult("C:/Users/Eric/Documents/Projects/Prolog/swi-discourse_045.pl").

% https://swi-prolog.discourse.group/t/prolog-8-3-2-gui-can-not-find-likes-source-file/2542

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

test :-
    format('~w World!',['Hello,']).

Double clicking the file in File Explorer

image

Running test without fist using consult because the file was consulted via the double click action.

?- test.
Hello, World!
true.

Notice the title of the window is SWI-Prolog -- c:/Users/Eric/Documents/Projects/Prolog/swi-discourse_045.pl

Notice that in File Explorer when the file association .pl file type is set to SWI-Prolog the .pl files will have an SWI-Prolog icon, i.e. image


This occurs because I have the file association for .pl set to C:\Program Files\swipl\bin\swipl-win.exe

See: Set or Change File Associations & Extensions in Windows 10



Dropping a SWI-Prolog source code file on to the SWI-Prolog desktop shortcut works in Windows and will consult the source code and set the working directory.

The easiest way to create a desktop shout cut is to use File Explorer to find the SWI-Prolog executable C:\Program Files\swipl\bin\swipl-win.exe then right click and drag and drop it onto the desktop and select Create Shortcut here.

1 Like

That totally works still for me (I’m on windows 10 like @EricGT). I hope it doesn’t change in the future because it’s very useful. For example, I have a project load file in all my projects that a) starts the documentation browser, b) opens select project files in the IDE and c) consults the project’s “entry point” file. That way I can start working on a project by double-clicking the load file (or navigating to the load file by keyboard and pressing enter, to protect my wrists).

Please don’t change this :slight_smile:

(I think this works on linux also, except of course if you consult the load file from a terminal it will open your default editor in the terminal. Which can be confusing if the project load file attempts to open multiple source files at once, especially if your default editor doesn’t have tabs. Should be even more fun when the default editor is the default editor) (ed).