Prolog helloWorld not working - prolog

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

Related

Prolog procedure does not exist (cant run rules in prolog)

I am new to Prolog and using the SWISH SWI online PROLOG website. https://swish.swi-prolog.org/
I am trying a really basic program:
a('jae').
b('lii').
c('jackson').
happy(A):-sings(A).
happy(B):-dances(B).
goToPlay(C):-free(C).
trying to run happy(jae). gives the following error
procedure `sings(A)' does not exist
Reachable from:
happy(A)
Please help me solve this.
If submit the goal
happy(jae).
Then rule
happy(A) :- sings(A).
applies, which means that the next goal to be solved is
sings(A).
with A = jae.
Unfortunately the one-argument predicate sings/1 is nowhere to be found, so you get the same error as for a missing procedure in another programming language.
You have to define sings/1.

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.

Prolog ERROR: Undefined procedure: transpose/2 (DWIM could not correct goal)

I have just started with Prolog for one of my classes so it's quite basic. I just had a doubt that does Prolog not have any inbuilt functions that can be used directly or should we always be writing a set of facts and rules first and then use them? I was trying to run this function:
?-transpose([[1,2,3],[4,5,6],[7,8,9]],Ts).
Which gave me this error:
ERROR: Undefined procedure: transpose/2 (DWIM could not correct goal)
So, basically, my question is, do I need to write how the transpose would work? I thought it was an inbuilt function ready to use.
P.S.: I am using: SWI-Prolog (threaded, 64 bits, version 7.6.0-rc1)
Of course it has quite a few built-in predicates (listed in http://www.swi-prolog.org/pldoc/man?section=builtin) but transpose is not one of them. If it's "for one of my classes so it's quite basic", most likely you are expected to write transpose yourself, especially if it's your first Prolog class.
There is one in clfpd library, see How to use predicate transpose in SWI-Prolog?.

How to call facts and rules from another file

Case: I have a file, solution.pl, where I want to use the rules of another file (this is the definition of the program, not up to me, but I think it's somewhat common).
So I'm using Ciao Prolog and the syntax seems to be ensure_loaded('c:/Path').
but when I try to use the rules of the other file, it tells me he doesn't know what rule I'm trying to use.
On the other hand, if I write on the console that same lane, and then query again, it does work.
So I was wondering maybe the syntax for doing it on the console and on a file are different??
Could someone tell me a step by step so I can figure it out if I'm missing something? What I'm doing right now is loading the program as I would do with smaller programs, then try to query rules of the file included in the ensure_loaded command. And as I said, it does seem to work if write the command outside of the program.
in solution.pl, try
:- ensure_loaded('c:/Path').
or, if the source in c:/Path has a module directive - that is, it begins with :- module(name, [exported/arity, ...]).
:- use_module('c:/Path').

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.

Resources