Prolog existence_error procedure in basic example - prolog

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.

Related

Dealing with dynamic predicates that cause compilation failures

I am attempting to run an example GNU Prolog program used as an example during my course work. The code is pulled directly from https://www.cpp.edu/~jrfisher/www/prolog_tutorial/2_17pl.txt and was shown working at one point by my professor.
However, when I run the provided example code, I get the following compilation warning:
| ?- consult('C:/Users/Chase/Desktop/Prolog files/newAnimal.pro').
compiling C:/Users/Chase/Desktop/Prolog files/newAnimal.pro for byte code...
C:/Users/Chase/Desktop/Prolog files/newAnimal.pro:74:12: syntax error: . or operator expected after expression
1 error(s)
compilation failed
The line that is keeping the program from compiling correctly is:
:- dynamic yes/1,no/1.
Which I read up on here: https://www.swi-prolog.org/pldoc/man?predicate=dynamic/1
However, despite attempting to rewrite and reformat the section, I could still not get it to compile.
Any help on why the provided code may not be running?
I am using a Windows GUI GNU Prolog console V1.4.5
The ISO Prolog standard doesn't require dynamic(or multifile or discontiguous) to be declared as an operator. A few systems do it (e.g. SWI-Prolog like you mentioned) but not GNU Prolog. Thus, to ensure code portability, avoid using dynamic as an operator. Write instead:
:- dynamic(yes/1).
:- dynamic(no/1).
Or:
:- dynamic((yes/1, no/1)).
Or:
:- dynamic([yes/1, no/1]).
These are the standard conforming alternatives for declaring multiple predicates as dynamic.
Also, GNU Prolog have a fine manual (part of its installation) which you should refer to when using GNU Prolog.

prolog sort/4 undefined procedure built in

I am trying to use a supposedly built in procedure in SWI-Prolog.
The procedure in question is sort/4.
The thing is, when I try to use it in my program I get the following error:
ERROR: Undefined procedure: sort/4
ERROR: However, there are definitions for:
ERROR: msort/2
ERROR: sort/2
I don't understand because the documentation says its built-in, but Prolog doesn't know it. I really need to use this procedure
Would you know how to fix this issue ? I thought it was a matter of library and tried to add ":- use_module(library(lists))." at the beginning of my code but the situation remains the same.
Hope you can help me, have a good day
The sort/4 predicate as documented here is relatively new. If you get the latest stable or development version of SWI-Prolog it should be available. It is a built-in, not a part of a library.

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.)

Applying predicates on a list in Prolog

I am a newbie in SWI-Prolog (5.10.5 running on win 7).
There are 3 files in an application that I am learning about:
a.pl and b.pl and c.pl
a.pl uses the predicate foldl/4, which seems to be in apply.pl.
c.pl, uses the following:
:- ensure_loaded('a.pl').
:- ensure_loaded('b.pl').
and then calls some predicates in these files.
When I run c.pl, I get exception that foldl/4 is not defined. I tried adding the following to a.pl:
:- use_module(library(apply)).
and
:- [library(apply)].
but no use.
Could you help?
Thanks in advance
Suzi
I'm running 5.10.4 on Ubuntu, and my version does not have apply:foldl, though it does have most of the other predicates in apply. The online source shows that foldl was only added on 2012-06-05, but versions 5.10.X are over 2 years old.
So it looks like the online docs are for much newer versions, which you'd need to upgrade to to have this predicate.

Saving GProlog database

In my program I have a dynamic clauses, they works fine, but when I closing my program, they are disappears.
I've tryed that
saveState :-
write_pl_state_file('backup.dat').
loadState :-
file_exists('backup.dat'),
read_pl_state_file('backup.dat'); !.
but this is not works.
Is there a way to save this databse to a file?
The predicates write_pl_state_file/1 and read_pl_state_file/1 are connected with the information/state that affects parsing of terms, i.e. operator definitions, character conversion Prolog flags, etc.
So that is part of your solution (perhaps), but more fundamentally you wish to save the dynamic clause definitions, probably in a form that allows you to reinstate them by consulting a file.
The predicate listing/0 does something like this, but it displays the dynamic clauses to the "console", not to a file. Probably you want to use the underlying predicate portray_clause/2, which does allow redirecting output to a file (stream).
The author Daniel Diaz noted a slight change (adding a newline to end of output) for portray_clause/2 in recent release notes for version 1.4.0, so you may want to make sure you've got the latest version for the sake of legibility.
Added:
It appears that starting with version 1.3.2 GNU Prolog supports sending listing/0 output to the current stream (rather than just to the console as in 1.3.1 and earlier).
Here's a test case:
| ?- assertz(whoami(i)).
| ?- assertz(whoami(me)).
| ?- assertz(whoami(myself)).
which creates three clauses (facts) for a dynamic predicate whoami/1.
I then created a file myClauses.pl with the following query:
| ?- open('myClauses.pl',write,S), set_output(S), listing, close(S).
Once the stream S is closed, current output is reset to the console.
You will find that the file myClauses.pl contains a blank line followed by the three clauses, so that the source code is in proper form to be consulted. However I'm having a problem with the consult/1 predicate (and its File -> Consult... menu equivalent) in my newly installed GNU Prolog 1.4.0 under Windows. The compilation works from the command line and produces a byte-code file that load/1 can correctly handle in the console, so there's some small problem in how things are set up. I'll post a further note when I get that squared away, having sent in a bug report. I've not tried it yet under Linux.
You can use current_predicate/1 or predicate_property/2 to access predicates, and clause/2 to access the clauses for a predicate.
Then you can write a save utility by using that information.

Resources