How can I load a base of knowledge with SWI under iMAC?

Hi everyone,

I am working under iMAC and I launch SWI from a terminal windows with the command open SWIProlog.app !

Once upon into SWI, how can I load a base of Knowledge?

Thanks for an advice

Stephane

The general answer is here: SWI-Prolog -- Manual

The easiest way is to just execute the top-level goal
[path/to/my/file] % notice that the .pl isn’t needed or even desirable.
which is a short-hand for consult(path/to/my/file).
You can also use ensure_loaded/1 and – if the knowledge base is in a module – use_module/1 or use_module/2. You probably don’t owant to use include/1.

1 Like

Thank you very much for you quick answer Peter.
I apologize but I am still a stranger in the SWI-World.
Here what I am trying to do:
My knowledge base is in a file called template.pl.
I launch my SWI from a terminal window with the command “open SWI.app”.
Once upon in SWI I try to do what you have explained: “?- ensure_loaded/1 (temp.pl).” but it doesn’t work.
What I am doing wrong?

Thank you for your help.

Stephane

At the :- prompt, enter:
ensure_loaded(temp).

If this fails because temp.pl isn’t in your current working directory, you can always give a full path, e.g. ensure_loaded('/path/to/temp'). (The .pl extension is optional and generally not given.)

My knowledge base base1.pl is in the same directory as the SWI application.
That’s the result of my command/

?- ensure_loaded(base1.pl).
ERROR: Type error: dict' expected, found base1’ (an atom)
ERROR: In:
ERROR: [13] throw(error(type_error(dict,base1),_33146))
ERROR: [10] ‘’(user:user: …)
ERROR: [9]
ERROR:
ERROR: Note: some frames are missing due to last-call optimization.
ERROR: Re-run your program in debug mode (:- debug.) to get more detail.

If you have a special character (such as “.”) in the name, you need to quote it:

?- ensure_loaded('base1.pl').

If you want to understand your error message: base1.pl is not a single thing – it’s three: “base1”, “.”, “pl”. The “.” has the special meaning of selecting an item from a dict. So, the error message “Type error: `dict’ expected, found `base1’ (an atom)” means that “base” isn’t a dict (it’s an atom).

In the directive ensure_loaded/1, the “/1” means it expects a single argument. The argument typically is an atom: you can ensure it’s an atom by quoting it with single quotes. As a convenience, ensure_loaded/1 also allows you to have names without quotes that contain "/"s … this is actually a compound term because the “/” is an operator – unlike in regular programming languages, an operator doesn’t do anything; it’s merely a bit of syntactic sugar that allows you to write path/to/file instead of '/'('/'(path,to),file) (where path, to, file are all atoms – they don’t need quotes because the don’t contain any special characters.

(I hope I haven’t over-explained and caused you more confusion.)

1 Like

Hello Peter,

thank you very much for your advices: I progress :-).

Now I have the following error message/
ERROR: /Users/stephanehetzel/Desktop/Prolog/base1.pl:13:6: Syntax error: Unexpected end of file

Where can I find in the manual the part which describes how the knowledge bases shall be structured ?

Regards

I’m not sure how I could help you without getting into a full Prolog tutorial. Basically, Prolog “knowledge bases” are just Prolog programs; there’s no strong distinction.

If you post your code, we can probably help you. (I think Discourse allows attaching a file to a post)

Hi Peter,

I can’t join my file to this message.

But here the content of the file:
man(john) .
woman(frida) .
woman(nelly) .
duck(donald) .
loves(john,frida) .
loves(frida,donald) .
loves(nelly,donald) .
loves(donald,nelly) .
sad(X) :− loves(X,Y) , loves(Y,Z) .

Can you give me perhaps the name of a Prolog editor?

Thanks

Regards

The file looks fine, although the last line’s :- seems to have been modified to use a Unicode em-dash (or maybe en-dash) instead of an ordinary minus sign. The last line should be:

sad(X) :- loves(X,Y) , loves(Y,Z) .

I’ve attached the corrected file.

If you load this file, you should get a warning: “Singleton variables: [Z]”.sad.pl (179 Bytes)

As to editor - I use Emacs. SWI-Prolog has its own editor. I think that some people VScode. See also Program Development Tools

Great it works!
Thanks a lot for your help and your patience.

Stephane

Hi Peter,

I have tried to use the integrated editor of SWI without success.

Here the error message that I have received.

?- edit("base1.pl").
% Waiting for editor ... 
% Editor returned failure; skipped make/0 to reload files
true.

What shall I do?

Stephane

I see. A mac. You need to install Xquartz to get the development tools working. This should popup a new window.

Hi jan,

Thank you for your answer.

But now I have another problem: when I try to execute the command edit(“base1.pl”), it closes my Prolog windows!
I have installed Xquartz.
Shall I do something else after have installed Xquartz?

Thanks

Stephane

Now it may get hard :frowning: Which versions of SWI-Prolog, MacOS and XQuartz? You can try to run Prolog in a terminal to get some better feedback. Normally open the MacOS terminal and run this command. If you did not install Prolog you’ll find the same under /Volumes

 /Applications/SWI-Prolog.app/Contents/MacOS/swipl

Now try

?- emacs.

If it complains something like “[PCE fatal: @display/display: Failed to connect …” your XQuartz is probably not running. If something else, please include the error.

Hi jan,

here what I have done:
open “SWI-Prolog.app/Contents/MacOS/swipl”

?- emacs.
see the screenshot that I have joined to the post.

Tha

nks
Stephane

The error message basically says that your X11 (Xquartz) “shared object” file is too old.

You have some options (you might find more possibilities by searching for something like “macos old xquartz” and including the version number you get from uname -a:

  1. Upgrade your system somehow to get a newer version of /opt/X11/lib/libfreetype.6.dylib (and probably others). (You might be able to just get a newer version of Xquartz, which has the X11 libraries)

  2. Create symlinks to fool SWI-Prolog into thinking that it has a newer version of the library (which might or might not work, depending on what has changed between versions). I’ve done this with Linux and SunOS (which depend on naming conventions for tracking version), but never for MacOS, so I don’t know if it’ll work (which might use a different mechanism for tracking library versions) … e.g., something like this (and be sure to keep a note of what you did, so that you can undo it in future):

      ( cd /opt/X11/lib/ ;
        sudo ln -s libfreetype.6.dylib libfreetype.7.dylib )
  1. Rebuild SWI-Prolog for your system (there are instructions for downloading and building at the SWI-Prolog website, but I don’t know how well those instructions work with an old iMac).

  2. Find a sufficiently old version of SWI-Prolog that works with your system.

Good luck!

(Personally, I don’t use the edit/1 predict; I run my source editor in one window and my editor in another; but I’m old-fashioned.)

1 Like

Hello Peter, hello Jan,

A great thank you for your support.

a last question : where can I find the reference off all command in SWI for example know how to exit from the trace mode, how to clear the screen and so on.

Thanks

The manual is quite extensive; for debugging, here’s a link: SWI-Prolog -- Manual