Get simple Prolog example to work - prolog

I am trying to run a simple gprolog to run on my Linux machine, GNU Prolog was installed from the Ubuntu Software Center.
From the GNU Prolog Intro I got the following example, stored in HelloWorld.pl.
parent(hank,ben).
parent(hank,denise).
parent(irene,ben).
parent(irene,denise).
parent(alice,carl).
parent(ben,carl).
parent(denise,frank).
parent(denise,gary).
parent(earl,frank).
parent(earl,gary).
grandparent(X,Z):-parent(X,Y),parent(Y,Z).
ancestor(X,Y):-parent(X,Y).
ancestor(X,Y):-parent(Z,Y),ancestor(X,Z).
I start gprolog, enter [HelloProlog]. and get the following error:
| ?- [HelloProlog].
uncaught exception: error(instantiation_error,consult/1)
Even if I do not load the code from a file but run it interactively I get an error:
uwe#z11:~/desktop$ gprolog
GNU Prolog 1.3.0
By Daniel Diaz
Copyright (C) 1999-2007 Daniel Diaz
| ?- parent(Luke,Anakin).
uncaught exception: error(existence_error(procedure,parent/2),top_level/0)
| ?-
Is my installation broken or what am I doing wrong?

In Prolog, variables start with an upper case letter (or with an underscore), hence the instantiation error you got with the goal [HelloProlog]. Simply use instead ['HelloProlog']. I.e. represent the file path as a Prolog atom, which require single quotes when they start with an upper case letter.
The existence error you got is simply due to querying a predicate that it's not defined. You need to load the HelloWorld.pl file first.

Related

Prolog: Print to swi prolog console immediately I consult a file

I have a simple prolog program:
write_manual:-
write('------------------------------'),
write('USAGE MANUAL'),
write('find_course. - List all the available courses'),
write('------------------------------').
% execute this and output this right away when I open the program in the console
Does anyone know to achieve this? I'd like to print a simple help manual before the program starts. Currently, the swi prolog console (on windows 10) shows the ?- prompt and requires me to
manually call the predicate. I'm using SWI-Prolog (threaded, 64 bits, version 8.0.0).
This is a comment posted in a question because it doesn't format correctly in a comment.
Running on Windows 10 with SWI-Prolog (threaded, 64 bits, version 8.1.21)
:- initialization main.
main :-
write_manual.
write_manual :-
format('------------------------------~n',[]),
format('USAGE MANUAL~n',[]),
format('find_course. - List all the available courses~n',[]),
format('------------------------------~n',[]).
Start SWI-Prolog
?- consult("C:/Prolog/SO_question_160.pl").
------------------------------
USAGE MANUAL
find_course. - List all the available courses
------------------------------
true.
?-
Note: This was not setup to start from a Windows command line script because the title of the question reads I consult a file.

Prolog what's the difference between \+ and \=

What is the difference \= and \+?
because
?- 15\=14.
?- \+ 15=14.<--- this gives an error while the above does not.
Why?Aren't they the same?
Edit: here's the error:
Compiling the file:
D:\Program Files\Strawberry Prolog Beta\Games\WarCraft.pro
Warning 4: The string \+ is not an operator. (line 1, before the first clause)
Error 16: Instead of the integer 15 what is expected here is something like an infix operator or a full stop. (line 1, before the first clause)
1 error, 1 warning.
Also I'm using Strawberry prolog I also tried it on SWI prolog still the same.
I think you are putting queries into Prolog source files. That is not where they should go:
predicate definitions go into Prolog source files
queries are typed into the interactive Prolog toplevel
Try running the SWI-Prolog program without an input file. You should get a window with some informational messages about the SWI-Prolog version and then a prompt ?-. That is the toplevel. Try typing your query there. All queries should go there.
I don't know about Strawberry Prolog, but I suspect it's the same there.

My simple prolog code is throwing an syntax error: operator expected

As the title suggest, my prolog code is throwing a syntax error. Im not sure what Im doing wrong. Im using Swi for my IDE and I tried playing with it to fix the problem, but to no avail.
heres my simple prolog code with error
?-
| male(bob)
| male(jeff)
|
| female(jane)
| female(erica)
|
| father(bob,jane)
| mother(erica, jane)
|
| ?-mother(erica,X).
ERROR: Syntax error: Operator expected
ERROR: male(bob)
ERROR: ** here **
ERROR:
male(jeff)
female(jane)
female(erica)
father(bob,jane)
mother(erica, jane)
?-mother(erica,X) .
There are two phases of Prolog development: Writing the program and interacting with it in the Prolog shell. These two phases are separate. You don't write your program in the shell, at least not directly.
Save your facts in a file called family.pl (with a dot . at the end of each fact!), then start the Prolog shell. In the shell, you can load the program using
?- consult(family).
or
?- consult('family.pl').
Note that in the first case you leave off the .pl extension, but in the second case, if you do use the extension, you should use single quotes (') around the file name.
Now you can run your query:
?- mother(erica, X).
X = jane.
There are some other ways to load files, such as putting the file name between square brackets [] instead of using consult, or (for many Prolog systems) simply adding the file name on the command line.
Statements in prolog end with a dot:
male(jeff).
female(jane).
female(erica).
father(bob,jane).
mother(erica, jane).

Prolog, getting right answer in terminal, but wrong answer when running a program

Typing "prolog" in terminal gets:
GNU Prolog 1.3.0
By Daniel Diaz
Copyright (C) 1999-2007 Daniel Diaz
| ?-
Typing:
| ?- member(2, [1,2,3]).
Gets:
true ?
Then pressing enter gets:
yes
Typing:
| ?- member(4, [1,2,3]).
gets:
no
When i write a file; test.pl consisting of this:
:- member(4, [1,2,3]), nl, halt.
And then write in the terminal:
| ?- [test2].
I get:
compiling /path/test.pl for byte code...
/path/test.pl:1: warning: unknown directive (',')/2 - maybe use initialization/1 - directive ignored
/path/test.pl compiled, 1 lines read - 139 bytes written, 11 ms
yes
Shouldnt the answer here be no? What am i doing wrong. Also, how would you do this in prolog:
if (testInPrologTerminal(member(4, [1,2,3])) { do this; }
I.e, i want to send queries to the prolog top level, and get an answer
When you type the query member(2, [1,2,3]), GNU Prolog prompts you for a possible additional solution (hence the true ? prompt) as only by backtracking (and looking to the last element in the list, 3) it could check for it. When you press enter, you're telling the top-level interpreter that you are satisfied with the current solution (the element 2 in the list second position). The second query, member(4, [1,2,3]), have no solutions so you get a no.
To execute a query when a file is loaded, the standard and portable way of doing it, is to use the standard initialization/1 directive. In this case, you would write:
:- initialization((member(4, [1,2,3]), nl, halt)).
Note the ()'s surrounding the query, otherwise you may get a warning about an unknown initialization/3 standard, built-in, control construct. If you have more complex queries to be executed when a file is loaded, then define a predicate that makes the queries a call this predicate from the initialization/1 directive. For example:
main :-
( member(4, [1,2,3]) ->
write('Query succeeded!'), nl
; write('Query failed!'), nl
).
:- initialization(main).
Writing arbitrary queries as directives in a source file is legacy practice and thus accepted by several Prolog implementations but using the initialization/1 directive is the more clean, standard, and portable alternative.

Do SWI-Prolog files need start and end characters?

I'm new to SWI-Prolog and am trying some tutorials. Every file I try to load through the command line, however, gets 2 error messages - one at the start (Operator expected) and one at the end (Unexpected end of file). Files are saved in the same directory as the one I'm working in.
For example, I have this file saved as kb2.pl
listensToMusic(mia).
happy(yolanda).
playsAirGuitar(mia) :- listensToMusic(mia).
playsAirGuitar(yolanda) :- listensToMusic(yolanda).
listensToMusic(yolanda):- happy(yolanda).
From the start of opening Prolog (through /opt/local/bin/swipl), my command line looks like this:
% library(swi_hooks) compiled into pce_swi_hooks 0.00 sec, 3,928 bytes
Welcome to SWI-Prolog (Multi-threaded, 64 bits, Version 5.10.2)
Copyright (c) 1990-2010 University of Amsterdam, VU Amsterdam
SWI-Prolog comes with ABSOLUTELY NO WARRANTY. This is free software,
and you are welcome to redistribute it under certain conditions.
Please visit http://www.swi-prolog.org for details.
help, use ?- help(Topic). or ?- apropos(Word).
?- [kb2].
ERROR: /Users/name/Desktop/kb2.pl:1:0: Syntax error: Operator expected
ERROR: /Users/name/Desktop/kb2.pl:10:52: Syntax error: Unexpected end of file
% kb2 compiled 0.00 sec, 2,584 bytes
true.
?-
When I ask for the listing of the compiled file, I get:
\happy(yolanda).
\playsAirGuitar(mia) :- listensToMusic(mia).
\playsAirGuitar(yolanda) :- listensToMusic(yolanda).
true.
So it's cutting out the first and last lines of the file.
I've searched for these error messages online and found a lot of useful hints about formatting for Prolog, but none that address this situation. Is there some sort of special character or formatting that should be used for the start and end of a file for Prolog?
Which operating system are you on? Are you sure the file is ASCII (and not UTF-16)? Does it have the native line ending for your platform? Can you post the whole file (raw) so people can check it?
Many Prolog systems, such as SWI-Prolog, Jekejeke Prolog, allow to detect a BOM as the first character of a text file. The BOM is some white space, that indicates the encoding of the text file:
254 255 UTF-16BE
255 254 UTF-16LE
239 187 191 UTF-8
So there is no need to restrict yourself to ASCII. Many text editors will place the BOM for you. And SWI-Prolog, Jekejeke Prolog, by default probe the BOM.

Resources