Prolog Undefined procedure and Operator priority clash error? - prolog

just recently started learning Prolog, and I have NO idea why I'm getting this error.I can't understand what can i do .
This is the 'code' :
?- assert(likes(a , beaf)).
?- assert(likes(b , nuduls)).
?- assert(likes(b , hotdog)).
?- assert(likes(c , sandwicth)).
?- assert(likes(c , bargar).
?- assert(likes(d , juice)).
?- assert(likes(d , chicken_curry)).
?- assert(likes(d , nudules)).
?- assert(likes(e , brade)).
?- assert(likes(e , butter)).
?- assert(likes(e , bargar)).
?- friend(e,d).
?- friend(d,a).
?- friend(d,b).
?- friend(d ,c).
?- highlyrecommended(X,L):-friend(X,Y),likes(Y,L).
?- recommended(X,L):-friend(X,Z),friend(Z,Y),likes(Y,L).
?- Is d is the friend of e ?
?- Y
?- Is e is the friend of c ?
?- Y
?-Who are the friend of d?
a , b , e
true.
This all error
2 ?- ERROR: Undefined procedure: (?-)/1
ERROR: ?- is the Prolog prompt
ERROR: See FAQ at http://www.swi-prolog.org/FAQ/ToplevelMode.txt
3 ?- | ERROR: Undefined procedure: (?-)/1
ERROR: ?- is the Prolog prompt
ERROR: See FAQ at http://www.swi-prolog.org/FAQ/ToplevelMode.txt
4 ?- ERROR: Undefined procedure: (?-)/1
ERROR: ?- is the Prolog prompt
ERROR: See FAQ at http://www.swi-prolog.org/FAQ/ToplevelMode.txt
5 ?- | ERROR: Undefined procedure: (?-)/1
ERROR: ?- is the Prolog prompt
ERROR: See FAQ at http://www.swi-prolog.org/FAQ/ToplevelMode.txt
6 ?- ERROR: Syntax error: Operator expected
ERROR: ?- assert(likes(c , bargar)
ERROR: ** here **
ERROR: .
6 ?- | ERROR: Undefined procedure: (?-)/1
ERROR: ?- is the Prolog prompt
ERROR: See FAQ at http://www.swi-prolog.org/FAQ/ToplevelMode.txt
7 ?- ERROR: Undefined procedure: (?-)/1
ERROR: ?- is the Prolog prompt
ERROR: See FAQ at http://www.swi-prolog.org/FAQ/ToplevelMode.txt
8 ?- ERROR: Undefined procedure: (?-)/1
ERROR: ?- is the Prolog prompt
ERROR: See FAQ at http://www.swi-prolog.org/FAQ/ToplevelMode.txt
9 ?- | ERROR: Undefined procedure: (?-)/1
ERROR: ?- is the Prolog prompt
ERROR: See FAQ at http://www.swi-prolog.org/FAQ/ToplevelMode.txt
10 ?- ERROR: Undefined procedure: (?-)/1
ERROR: ?- is the Prolog prompt
ERROR: See FAQ at http://www.swi-prolog.org/FAQ/ToplevelMode.txt
11 ?- ERROR: Undefined procedure: (?-)/1
ERROR: ?- is the Prolog prompt
ERROR: See FAQ at http://www.swi-prolog.org/FAQ/ToplevelMode.txt
12 ?- | ERROR: Undefined procedure: (?-)/1
ERROR: ?- is the Prolog prompt
ERROR: See FAQ at http://www.swi-prolog.org/FAQ/ToplevelMode.txt
13 ?- ERROR: Undefined procedure: (?-)/1
ERROR: ?- is the Prolog prompt
ERROR: See FAQ at http://www.swi-prolog.org/FAQ/ToplevelMode.txt
14 ?- ERROR: Undefined procedure: (?-)/1
ERROR: ?- is the Prolog prompt
ERROR: See FAQ at http://www.swi-prolog.org/FAQ/ToplevelMode.txt
15 ?- ERROR: Undefined procedure: (?-)/1
ERROR: ?- is the Prolog prompt
ERROR: See FAQ at http://www.swi-prolog.org/FAQ/ToplevelMode.txt
16 ?- | | | | ERROR: Syntax error: Operator priority clash
ERROR: ?-
ERROR: ** here **
ERROR: highlyrecommended(X,L):-friend(X,Y),likes(Y,L) .
16 ?- ERROR: Syntax error: Operator priority clash
ERROR: ?-
ERROR: ** here **
ERROR: recommended(X,L):-friend(X,Z),friend(Z,Y),likes(Y,L) .
Any help please?
Thanks.

The ?- prompt
The ?- that you see is just a prompt. Python's interpreter prints >>>, your shell prints $, and so forth—it's not part of the syntax of the language, it's simply a convention adopted over the years by the language and its users. It's very convenient here on S.O. to have a separate notation for interactive queries because it gives you a visual clue as to where you should enter what you're seeing.
The upshot is that you never need to give Prolog ?-, just other humans (for documentation or copy/pasting into emails or S.O. questions).
If we apply this advice, your code turns into this:
assert(likes(a , beaf)).
assert(likes(b , nuduls)).
assert(likes(b , hotdog)).
assert(likes(c , sandwicth)).
assert(likes(c , bargar).
assert(likes(d , juice)).
assert(likes(d , chicken_curry)).
assert(likes(d , nudules)).
assert(likes(e , brade)).
assert(likes(e , butter)).
assert(likes(e , bargar)).
friend(e,d).
friend(d,a).
friend(d,b).
friend(d ,c).
highlyrecommended(X,L) :- friend(X,Y),likes(Y,L).
recommended(X,L) :- friend(X,Z),friend(Z,Y),likes(Y,L).
Using assert/1
Prolog is unique among programming languages in that it has a built-in database. Most of the time, the database is fairly static, meaning the database usually stays the same during any particular execution. However, the you can modify the fact database at runtime with asserta/1 and assertz/1, which insert new facts at the top or bottom of the database respectively, and retract/1 and retractall/1 which remove a fact or family of facts.
There is no ISO predicate assert/1. You have to decide if you want your new fact to be considered first or last.
Regardless, you do not need to assert anything in this program, because you're not changing the database from within the body of a rule. So you can just remove the assert wrapper you have, changing your code into this:
likes(a , beaf).
likes(b , nuduls).
likes(b , hotdog).
likes(c , sandwicth).
likes(c , bargar.
likes(d , juice).
likes(d , chicken_curry).
likes(d , nudules).
likes(e , brade).
likes(e , butter).
likes(e , bargar).
friend(e,d).
friend(d,a).
friend(d,b).
friend(d ,c).
While we're at it, let's fix your spelling.
likes(a, beef).
likes(b, noodles).
likes(b, hotdog).
likes(c, sandwich).
likes(c, burger).
likes(d, juice).
likes(d, chicken_curry).
likes(d, noodles).
likes(e, bread).
likes(e, butter).
likes(e, burger).
highly_recommended(X,L) :- friend(X,Y), likes(Y,L).
recommended(X,L) :- friend(X,Z), friend(Z,Y), likes(Y,L).
Much better.
Consulting a file
In most languages with an interpreter, the language works the same way whether through the interpreter or through a file. Prolog differs from other languages in this respect. Prolog has two "modes" for handling the input: a mode used by consulting, which defines facts and rules, and a mode used for querying, which is what you get when you run Prolog.
Take all the code above, properly cleaned up, and stick it in a file named "first.pl". Then run Prolog and at the prompt, type in [first]. and hit enter. This is what you should see:
$ swipl
Welcome to SWI-Prolog (Multi-threaded, 64 bits, Version 7.1.1)
Copyright (c) 1990-2013 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.
For help, use ?- help(Topic). or ?- apropos(Word).
?- [foo].
% foo compiled 0.00 sec, 18 clauses
true.
You are only typing two words in here: swipl and [foo]. Type them in exactly! Now you've consulted your first file and you can run queries.
Querying
The last five lines of your source are questions in English. They do not belong in your input file. You must write them again as queries to Prolog. Here's how we do them:
Is d is the friend of e?
?- friend(e,d).
true.
Once again, all you'll type in here is friend(e,d). and press enter. Try it!
Is e is the friend of c?
?- friend(c,e).
false.
Note that you have the wrong expected value here.
Who are the friends of d?
?- friend(d,X).
X = a ;
X = b ;
X = c.
Note that again, you have the wrong expected answer e instead of c. This time, you're going to type in friend(d,X). and press enter, then just typing a ; will be enough to get Prolog to give you the next answer until it runs out.
Conclusion
Whatever resource you're trying to learn from, you need to slow down and maybe get your hands on another book. Prolog is far too different for you to try and blast through. Be diligent and read carefully. This question is sort of pre-basic, which is why it is so highly downvoted. We're happy to help you learn, but you have to do your share. You can't expect to get this kind of answer for every little speedbump.

Related

How to use assert and retract in SWI-Prolog

In SWI-Prolog, if I use assert and retract at the prompt, I get
?- assert(at(1)).
true.
?- retract(at(1)).
true.
However, if I put these statements into a program file called "test" as
assert(at(1)).
retract(at(1)).
and run SWI-Prolog as
> swipl
?- [test].
I get
ERROR: /....../test:2:
No permission to modify static procedure `retract/1'
true.
What does this mean and how should I deal with it?
Put statements within a predicate, e.g.:
:- dynamic at/1.
test_assert :-
assert(at(1)).
test_retract :-
retract(at(1)).
Load the program, and then run:
?- test_assert.
true.
?- at(X).
X = 1.
?- test_retract.
true.
?- at(X).
false.
The prompt and source code files are different environments with slightly different behaviours. It's like the difference between calling Python len(x) in the repl and writing function len(x): in Python source code - you would be overriding the builtin len() with your own one. Python lets you do that, SWI Prolog also does but not easily.
When you type them at the prompt, you call the existing predicate assert/1 and actually do insert the fact at(1). into the database. When you type retract/1 you actually do retract the fact at(1) from the database.
In a fresh prompt, try ?- listing(at). and get an error, then ?- assert(at(1)). then do the listing again and see the fact, then retract it and try the listing and see only the remains of the dynamic declaration and the fact is gone.
When you put them in a source code file, you would be trying to override the existing builtin predicates with your new ones. Your new ones say "assert/1 is a predicate which succeeds when its arugment unifies with at(1)" and "retract/1 is a predicate which succeeds when its arugment unifies with at(1)".
That is, they don't do any asserting or retracting or database changes.
In your test file, put this:
:- redefine_system_predicate(assert(_)).
:- redefine_system_predicate(retract(_)).
assert(at(1)) :- true.
retract(at(1)) :- true.
Then save and consult it:
?- [testing].
true.
?- listing(at). % <-- your code ran, but did not insert `at(1)`.
ERROR: procedure `at' does not exist (DWIM could not correct goal)
^ Exception: (13) setup_call_catcher_cleanup(system:true, prolog_listing:listing_(user:at, []), _19450, prolog_listing:close_sources) ? abort
% Execution Aborted
?- assert(P). % <-- it's now behaving
P = at(1). % <-- like any other predicate.

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.

Unable to use user-defined operator in Prolog

I've been stuck for quite some time on this issue, my Prolog program has the following line within its operator definitions:
:- op(100, xfx, [has,gives,'does not',eats,lays,isa]).
and then this fact:
fact :: X isa animal :-
member(X, [cheetah,tiger,giraffe,zebra,ostrich,penguin, albatross]).
When I try to use the operator It says it is undefined and I just do not understand why.
?- peter isa tiger.
ERROR: [Thread pdt_console_client_0_Default Process] toplevel: Undefined
procedure: (isa)/2 (DWIM could not correct goal)
Sorry if its something silly (which it probably is) but I am new to Prolog. Any help is much appreciated.
It is working. See for yourself:
stefan#stefan-Lenovo-G510 ~ $ swipl
Welcome to SWI-Prolog (Multi-threaded, 64 bits, Version 7.3.12)
% ...
?- op(100, xfx, [has,gives,'does not',eats,lays,isa]).
true.
?- Term = (peter isa tiger).
Term = peter isa tiger.
The error message that you got ...
procedure: (isa)/2 (DWIM could not correct goal)
... says that too!
Is it clearer now?

SWI-Prolog - Fail to Assert

I define an operator as follows:
:- op(500, xfx, =>).
When I try something like:
assert(a => b).
Prolog raises an error that says 'No permission to modify static_procedure (=>)/2'.
Any solution?
As a security, you have to warn SWI that you are going to modify a predicate at runtime:
:- dynamic (=>)/2.
put at the top of the file should do it.
You must have meant another symbol in place of (=>)/2. Probably (->)/2 which is a control construct that cannot be modified.
Welcome to SWI-Prolog (Multi-threaded, 32 bits, Version 6.1.3-116-gf1c7e06)
...
?- asserta((a -> b)).
ERROR: asserta/1: No permission to modify static procedure `(->)/2'
ERROR: Defined at /opt/gupu/pl-devel/lib/swipl-6.1.3/boot/init.pl:194
?- op(500, xfx, =>).
true.
?- asserta(a => b).
true.

Constraint Programming library or syntax issue in SWI-Prolog

I'm just trying to figure out constraint programming in SWI-Prolog, looking at this tutorial : http://en.wikibooks.org/wiki/Prolog/Constraint_Logic_Programming
However I seem to be falling at the first hurdle.
?- use_module(library(clpfd)).
true.
?- X #> Y, X in 1..3, Y=2.
ERROR: Syntax error: Operator expected
ERROR: X
ERROR: ** here **
ERROR: #> Y, X in 1..3, Y=2 .
?-
What's going wrong here? I seem to have included the library, but the first example line from the tutorial throws a syntax error.
All the tutorials I can find seem to use operators like #=, #< etc. But my SWI-Prolog baulks at them. Are they an extra syntax which comes with that constraint library? (And am I failing to load it?)
Or am I misreading the tutorial examples?
Update : Trying to understand things from Horsh's reply below. I can get this to work if I use the library and run the line in the interactive terminal. But if I try to import the library and use these operators in a source file, then it throws the error again. What am I not understanding?
Update 2 :
OK. If, in my source file, I invoke the library and then write a rule which contains a #>. Then I try to consult it from the command-line. It will throw an error and the #> syntax is un-recognised. If import the library to the command line before trying to consult the program, it works. Can this be right?
Building on Horsh's answer, you should be importing the library in your source code, remembering to put ?- at the beginning of the line like so:
?- use_module(library(clpfd)).
The ?- tells SWI-Prolog to execute the line as if it were typed into the interpreter directly, instead of trying to declare it as a predicate in your program.
Don't be concerned about SWI-Prolog importing the library more than once, it knows to check if the library was modified and only reloads it if the library was changed since the last time it was loaded.
For anyone else that finds this in the future, if you want to import a library in an SWI-Prolog source file, the following will also work:
:- use_module(library(clpfd)).
Note the :- and not ?-.
The is all in the manual here and there.
?- [library(clpfd)].
% library(error) compiled into error 0.00 sec, 10,128 bytes
% library(apply) compiled into apply 0.00 sec, 16,840 bytes
% library(assoc) compiled into assoc 0.00 sec, 13,132 bytes
% library(lists) compiled into lists 0.00 sec, 14,332 bytes
% library(pairs) compiled into pairs 0.00 sec, 5,372 bytes
% library(clpfd) compiled into clpfd 0.05 sec, 392,604 bytes
true.
?- X #> Y, X in 1..3, Y=2.
X = 3,
Y = 2.

Resources