. or operator expected after expression in GNU Prolog - prolog

I have written a simple program to find largest of two number in prolog
domains
x,y,z=integer
predicates
bigger(x,y,z)
clauses
bigger(X,Y,Z):-
X>Y,Z=X.
bigger(X,Y,Z):-
X<Y,Z=Y.
goal
bigger(5,7,X).
but whenever I run the program I get the error:
compiling /home/prabin/large_number.pl for byte code...
/home/prabin/large_number.pl:2:5: syntax error: . or operator expected after expression
/home/prabin/large_number.pl:11:5: syntax error: . or operator expected after expression
2 error(s)
compilation failed
Why is it so?

Your source code is for Visual Prolog (or the older TurboProlog). Delete:
domains
x,y,z=integer
predicates
bigger(x,y,z)
clauses
and
goal
bigger(5,7,X).
leaving only
bigger(X,Y,Z):-
X>Y,Z=X.
bigger(X,Y,Z):-
X<Y,Z=Y.
After saving the fixed code, you can type the goal at the top-level interpreter:
| ?- bigger(5,7,X).
X = 7
yes

Related

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

syntax error: . or operator expected after expression 1 error(s) compilation failed

Domains
namelist=integer*;
Predicates
member12(namelist,namelist,namelist);
Clauses
member12([],L,L).
member12([X|L1],L2,[X|L3]):-
member12(L1,L2,L3).
Well, seems like you didn't provide the entire code, and considering your error, I'd say it's in your Goal part.
You probably have this line:
consult('D:\\Prolog\\list.pl')
And the error message states a ". or operator" is missing
consult('D:\\Prolog\\list.pl').
% here _______________________↑

Get simple Prolog example to work

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.

Swi prolog Error: Showing true but not false

Just installed SWI-Prolog on my W8 machine and it's throwing an error.
Here's the error:
ERROR: toplevel: Undefined procedure: mark/0 (DWIM could not correct goal)
Let's say my prolog source file contains one fact:
Prolog source...
it_is_monday. //The fact
So I compile the buffer, and get:
.../documents/prolog/prologSource compiled 0.00 sec, 2 clauses
Now when I input
it_is_monday.
The output as expected is true. However when I input say,
some_other_statement.
I get the error posted above as opposed to false. Can anyone tell me what's going on here?
Solution: Different prolog version.
There's a standard Prolog flag, unknown, that is set by default in SWI-Prolog and other modern Prolog compilers to error, meaning that an attempt to call an unknown predicate will result in an exception. This flag can be set (using the standard set_prolog_flag/2 predicate) instead to fail to get the behavior that you seem to be expecting but that's not advisable as it may make debugging harder. E.g. a simply typo in a predicate name will result in a failure which, in a complex program, could be difficult to trace down while a predicate existence error would pinpoint the culprit at the spot.
You get the error
ERROR: toplevel: Undefined procedure: mark/0 (DWIM could not correct goal)
because you haven't defined the procedure you tried to execute. (that's why it says undefined)
If you define it, by editing your .pl file and writing
some_other_statement.
and you run it again, you'll get
1 ?- some_other_statement.
true.
In Prolog you need to define every procedure you want to execute.
When you try to execute an undefined procedure, the name of the procedure will show up in the error. So, if you haven't defined some_other_statement., the error will be:
2 ?- some_other_statement.
ERROR: toplevel: Undefined procedure: some_other_statement/0 (DWIM could not correct goal)
notice that some_other_statement/0 is being shown on the error that I got.
EDIT : If you wanted to get a false message, you would have to define something like some_other_statement(1). and then execute a query like some_other_statement(12).
2 ?- some_other_statement(12).
false.
if you want to receive false from that you can add the directive
:- dynamic(some_other_statement/0).
at the beggining of the file, so when you execute the query
?- some_other_statement.
you will get false

Resources