Consult not working as expected

I want to separate my programming into different files. File basic.pl has only isa(dog,animal). File specific.pl has only consult(basic). When I run specific.pl and then query isa(dog,animal)., I get “ERROR: Unknown procedure: isa/2 (DWIM could not correct goal)”. Is this a problem with the way Visual Studio runs SWI-Prolog or have I made an error?

What does that mean?


Quick guess without knowing more is that SWI-Prolog is not set to the correct directory.

Use the predicate pwd/0 to see the current directory.

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

See this for an example that changes the directory using working_directory/2 before loading the code and calling a predicate.

I guess you just put the consult(basic). in specific.pl.

specific.pl

consult(basic).

This is wrong because it just defined a fact.

You should do this instead:

specific.pl.

:- consult(basic).

Thanks @EricGT . By “run” I mean right-click in Visual Studio and select “load document”, which starts the SWI-Prolog interpreter. I am in the correct directory. Yes, specific.pl just consults basic.pl as a test to make sure that its contents are active/valid. They are not. I can’t figure out why.

Do you mean Visual Studio Code - VSCode ?

Sorry, yes, Visual Studio Code. I’ve confirmed the same behavior with SWI-Prolog. Do I need to define isa/2 in some way before using it like isa(dog,animal). ?

I don’t use consult in source code files I use use_module/2. This means that all my source files are modules. See: Importing Predicates into a Module.

A better means than use_module is to use autoload but I have not started using that, my bad.

Let me see if I have a reply here that shows using modules, otherwise I will just make it for you. This will take a few minutes.

Thanks. No rush.

Am I doing anything wrong? Do you see the same behavior?

Obviously if it is not working. I seriously doubt you have found a bug. What you are doing has been the same for many, many years.

No, you did not provide a minimal working example. I can only guess at what you are doing.

If you have multiple files you might already start using modules.

file basic.pl:

:- module(basic, [isa/2]).

isa(dog, animal).
% more facts

file specific.pl:

:- use_module(basic).
?- [specific].
true.

?- isa(X, Y).
X = dog,
Y = animal.

The library file is named my_lib.pl and the app file is named my_app.pl and a test case is in my_app.plt.

This will use modules with autoload.

Years ago Prolog did not use modules and would use include/1 and odites when working with source code in multiple files. If you see that in source code it is very old Prolog. After modules were created use_module/1,2 became the norm. The current best practice with SWI-Prolog is to use autoload.

Directory: C:/Users/Groot
File: my_lib.pl

:- module(my_lib,
    [
        isa/2
    ]).

% Taxonomy
% Domain - Kingdom - Phylum - Class - Order - Family - Genus - Species

isa('Animalia','Eukaryote').
isa('Chordata','Animalia').
isa('Mammalia','Chordata').
isa('Carnivora','Mammalia').
isa('Canidae','Carnivora').
isa('Canis','Canidae').
isa('C.lupus','Canis').

isa('Plantae','Eukaryote').
isa('Magnoliophyta','Plantae').
isa('Magnoliopsida','Magnoliophyta').
isa('Fabales','Magnoliopsida').
isa('Fabaceae','Fabales').
isa('Pisum','Fabaceae').
isa('P.sativum','Pisum').

Directory: C:/Users/Groot
File: my_app.pl

:- module(my_app,
    [
        subclass/2
    ]).

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

:- load_test_files([]).
:- autoload(my_lib,[isa/2]).

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

subclass(Lower,Higher) :-
    isa(Lower,Higher).

Directory: C:/Users/Groot
File: my_app.plt

% Note: No module declaration for plt files.

:- begin_tests(my_app).

:- autoload(my_app).

test(count, Count == 14 ) :-
    findall(_,subclass(_,_),Items),
    length(Items,Count).

:- end_tests(my_app).

While I have heard that the SWI-Prolog top level can be run within VSCode I run SWI-Prolog top level outside of VSCode, just a personal preference.

To start the SWI-Prolog toplevel on Windows using the command line.

C:\Users\Groot>"C:\Program Files\swipl\bin\swipl-win.exe" --win_app

image

A more convenient way is to just pin a copy of the SWI-Prolog icon from the start menu into the task bar. Then it is always just a mouse click away.

Example run

Welcome to SWI-Prolog (threaded, 64 bits, version 8.5.5)

?- working_directory(_,'C:/Users/Groot').
true.

?- [my_app].
true.

?- run_tests.
% PL-Unit: my_app . done
% test passed
true.

?- subclass(Lower,Higher).
Lower = 'Animalia',
Higher = 'Eukaryote' ;  % Pressed space bar to see next result.
Lower = 'Chordata',
Higher = 'Animalia' ;  % Pressed space bar to see next result.
Lower = 'Mammalia',
Higher = 'Chordata' .  % Pressed enter to end seeing next result.

?- 

That should get you going.

As I do not run SWI-Prolog top level within VSCode I can not help with that. If you can get this working as demonstrated then you can try doing similar with VSCode.

Thank you @Boris & @EricGT !