Why does not it run the program in SWI-prolog - prolog

Why doesn't it launch even the most basic program, what am I doing wrong? (In the online version prolog - SWISH, everything works great).
enter image description here

Prolog queries must end in a period:
?- task.

Related

Prolog existence_error procedure in basic example

I'm trying to learn the basics of Prolog and keep running into a existence_error with the following code.
comes_after(m1, m2).
comes_after(m2, m3).
comes_after(m3, m4).
comes_after(m4, m5).
comes_after(m5, m6).
does_come_after(X, Y) :- comes_after(X, Y).
does_come_after(X, Z) :- comes_after(X, Y), does_come_after(Y, Z).
When executing a query such as does_come_after(m1, m3) I keep getting the following error.
uncaught exception: error(existence_error(procedure,comes_after/0),does_come_after/0)
Here's a screenshot showing the error:
Prolog Error
What am I doing wrong, and what should I keep in mind to avoid these errors in the future? Thanks in advance.
The error message tells you that Prolog expects a predicate comes_after/0, but none is found. Further, this problem arises when being called from a predicate does_come_after/0. Now, your definitions all use arity 2. Thus comes_after/2 and does_come_after/2. So what the system expects cannot happen.
And if it does, this must be related to your installation. You have 1.4.5 which is the most recent version, 1.4.4 the still current stable.
It is thus possible that you have another, older, system installed which interferes by providing an incompatible pl2wam compiler. To verify this, say which pl2wam or pl2wam --version.
In particular, versions from 1.3 or even 1.2 may produce such results. There is no version checking for this in GNU.
To ensure that I get always the right version, I say:
export PATH=/opt/gupu/gprolog-1.4.5/bin:${PATH}
Unfortunately, this is a problem with version 1.4.5.
Instead of downgrading, fortunately, there is a trick that you can do:
Instead of using consult(file_name) inside gprolog, you can run this command on your terminal (outside gprolog)
gplc file_name.pl
it will output an executable that you can run by
./file_name
it should solve your existence error problem.

Stop Prolog program with predicate

I am looking for something like the predicate halt/0 or abort/0.
I do not want to use abort/0 because of the % Execution aborted message at the console and I do not want to use halt/0 because it terminates the Sicstus Prolog program.
Is there any other predicates that can be used to stop the program execution?
I already checked the documentation for this topic and I do not seem to find the result I want to see.
Maybe throw an exception with throw/1?
– mat
I managed to achieve what I wanted using that predicate.
Thank you mat.

Prolog helloWorld not working

Silly question, but I'm trying to get this simple Prolog program working. I have written other small Prolog programs with no problem, but this one is giving me trouble:
test :- write 'test1234 test1234',nl,halt.
That's it. The file is saved as adventure1.pl. It is loaded into SWI-Prolog on Ubuntu with the command line option:
prolog -s adventure1.pl
When loaded into the Prolog interpreter I enter the following:
start.
However, Prolog says undefined procedure: test/0 (DWIM could not correct goal). What is the error here, is it somthing really simple. I wrote this because I have example programs that use a predecate named start which displays text, yet I can't even get this to work.
write 'test1234 test1234'
is a syntax error, as SWI-Prolog clearly indicates:
ERROR: /tmp/adventure1.pl:2:10: Syntax error: Operator expected
It should be
write('test1234 test1234')
(Of course, that won't solve the problem of start not working, because you've defined test.)

How to run prolog code?

I am working on family tree in prolog. I don't have any idea how to compile and run this program. Please give me some basic steps to run it.
Assuming you are using SWI-Prolog
Step 1: Put your dictionary into a text file. Here's an example dictionary:
dog(rover).
dog(felix).
dog(benny).
Step 2: Title your dictionary "something.pl" -- I called this one dogs.pl.
Step 3: Open up SWI-Prolog from the command line. In linux, I use the command swipl at the command line. Once SWI-Prolog starts, you will see a command line that looks like ?-
Step 4: In SWI-Prolog, load your dictionary by using the consult command like so:
?- consult('dogs.pl').
Step 5: Now that your dictionary is loaded, you can use it. Here's an example using our test dictionary about dogs:
?- dog(rover).
true.
dog(X).
X = rover ;
X = felix ;
X = benny .
That should pretty much do it as far as getting your prolog programs to load and run.
Finally, here's a link for how others run Prolog:
Adventures in Prolog
When you have finished your code, do the following steps to run your code:
Step 1: open your prolog terminal.(See pic1)
Step 2: click "File" on the left of the top, then choose "Consult" to open the prolog file(your code file).(See pic2)
Step 3: type in your function name and parameters.
Well, that would depend entirely on your Prolog implementation.
The language is one thing but how to compile or run your code is a different issue.
For example, Visual Prolog uses a key sequence within the IDE, CTRL-SHIFT-B, to build the code, or ALT-F5 to run the code. You need to find the equivalent way of doing the same thing in whatever Prolog implementation you're using (or at least let us know).
There's no official standard for the Prolog built-in predicates that compile and load a source file. The most common ones are consult(File), reconsult(File), and load_files(Files, Options). The shortcut [File| Files] is also often available. You will need to consult the documentation of the Prolog system you're using. Be aware that even for the common ones above, the semantics often differ from system to system.
If you are using terminal or cmd
Navigate to the folder where you saved your kB
Run it as script using the following command
swipl -s file.pl

Prolog Code - No idea what it does

I was wondering what this code does:
:- set_prolog_flag(toplevel_print_options,
[quoted(true), portray(true), attributes(portray), max_depth(100)]).
I have seen it in some of the sample codes my prof has posted on his website but I have no clue what it does. Thanks for your help in advance.
I believe it might have something to do with making program output more formatted (and thus, more readable or accessible.)
See this article: "Help... Prolog writes [x, y, z|...], I want the whole answer".
Basically, in the case of your code's settings... it looks like the code is just setting some formatting for output. The max_depth setting means that anything that is nested more than (100, in your case,) levels will then be written as .... The rest of the options just enable normal output.

Resources