I am trying to do simple two number multiplication in Prolog. When I initially launch Prolog without loading any file, I am able to type in:
X is 2 * 2.
And I get 4 as the answer. But when I launch Prolog by opening my file with my code, the above statement does not work anymore. It says:
ERROR: toplevel: Undefined procedure: (*)/2 (DWIM could not correct goal)
And when I load my code I get the following:
Warning: The predicates below are not defined. If these are defined
Warning: at runtime using assert/1, use :- dynamic Name/Arity.
Warning:
Warning: */2, which is referenced by
Warning: c:/filepath/project.pl:98:21: 1-st clause of multiply/3
Why does the multiplication work when I don't load my code, but not work when I load my code? This is part of the code I am using for multiplication:
:-dynamic(multiply/3).
multiply(X, Y, Z) :- Z is X * Y.
The two lines above are the only mentions of multiply in my code.
I have spent a couple hours searching for answers and failed. Maybe my keywords I use to search are not specific enough, but it's difficult to find Prolog answers as it is. Am I doing something wrong? This issue is the only thing preventing me from finishing my project, which is too long to be of good use if posted.
Part of our project was to modify code given by the Professor. The Prof included the following line that was not being used anywhere:
:-op(150,xfx,is).
When removed, the 'is' in:
X is 2 * 2.
works and displays the correct answer.
Related
I write a predicate for calculating Circle Area by EClipse-clp.
circle(R) :- S is 3.14*R^2, write(S).
it has following Error:
calling an undefined procedure circle(R) :- S is 3.14 * R^2, write(S) in module eclipse
This is really very basic. You have to distinguish
Definitions - this is your program code, consisting of predicate definitions, which in turn are made up of facts and rules. Logically, this is where you define what is true.
Queries - this is where you call your program. Logically, you ask whether something is true, and how.
Definitions are usually loaded/compiled from a source file. Queries are usually typed into the query prompt. You seem to be typing a definition into the query prompt.
NB: Just to be clear, my motivation for the question below is to learn my way around Prolog and SWI-Prolog, not to get past a particular error message. In fact, I already know one way to get past this error. My question asks about whether several other alternatives are also possible.
An exercise in my Prolog textbook asks one to describe the outcome one should expect from several queries, assuming one has consulted the following knowledgebase beforehand:
x(a).
z(b).
z(c).
z(d).
w(V) :- x(V).
w(V) :- y(V).
w(V) :- z(V).
On SWI-Prolog, at least, most of these queries fail, because SWI-Prolog intreprets y as undefined.
From the solutions to the exercises at the end of the book I can tell that this is not the authors' intended outcome. Maybe there's a Prolog implementation for which the exercise would turn as the solution presents it.
Be that as it may, I'd like to learn about good ways to work around the problem.
Consider, for example, the query w(x).. the book's solution claims that the query w(x). should evaluate to false.. In fact, this is what happens:
?- w(x).
ERROR: w/1: Undefined procedure: y/1
Exception: (7) y(x) ?
(At this point, SWI-Prolog is expecting me to enter some letter indicating how to respond to the exception. More about this later.)
I'm looking for ways to either turn the interaction above to
?- w(x).
false.
?-
...or at least for a suitable <ONE-LETTER RESPONSE> I could give to SWI-Prolog so that it arrives at the conclusion false. IOW, so that
?- w(x).
ERROR: w/1: Undefined procedure: y/1
Exception: (7) y(x) ? <ONE-LETTER RESPONSE>
false.
?-
I know of at least one answer to my question, namely simply to delete or comment out the line:
w(V) :- y(V).
I would like to know of other possible solutions, such as, for example, the "suitable " I alluded to earlier.
Another possibility would be some SWI-Prolog global configuration that would result in the above interaction to change to
?- w(x).
false.
?-
A third possibility would be to "define" y in some minimal way. The only way I can come up with is by adding the fact
y(dummy).
to the knowledgebase. Is there a more minimal way to define y, one that does not require introducing an extraneous constant into the domain of discourse?
(This is not specific to SWI)
The first Prolog systems back in the 1970s actually behaved in the way you describe. Soon it became apparent that this is a frequent source of errors. Simple misspellings remained undetected for too long. Current implementations produce a clean existence error. This is standard behaviour since 1995.
However, you can go back into the olden tymes with the ISO Prolog flag unknown which has three values error (default), fail, and warning.
?- inex.
ERROR: Undefined procedure: inex/0 (DWIM could not correct goal)
?- set_prolog_flag(unknown, fail).
Warning: Using a non-error value for unknown in the global module
Warning: causes most of the development environment to stop working.
Warning: Please use :- dynamic or limit usage of unknown to a module.
Warning: See http://www.swi-prolog.org/howto/database.html
true.
?- inex.
false.
?- set_prolog_flag(unknown, warning).
Warning: Using a non-error value for unknown in the global module
Warning: causes most of the development environment to stop working.
Warning: Please use :- dynamic or limit usage of unknown to a module.
Warning: See http://www.swi-prolog.org/howto/database.html
true.
?- inex.
Warning: toplevel: Undefined procedure: inex/0 (DWIM could not correct goal)
false.
As you can read above, SWI proposes to use a dynamic declaration in stead - which in turn has its own problems... It is much better to declare instead:
:- discontiguous(y/1).
An undefined procedure error raises an exception so if you want the exception to be raised because you don't want to change y/1 predicate (delete or define it) you need to catch the exception and then return false like this:
x(a).
z(b).
z(c).
z(d).
w(V) :- x(V).
w(V) :- catch(y(V), error(Err,_Context),my_handler(Err)).
w(V) :- z(V).
my_handler(Err):- write(Err),fail.
Example:
?- w(x).
existence_error(procedure,y/1)
false.
I am trying to translate a simple Turbo Prolog problem to Visual Prolog 7.1
The original Turbo Prolog code is the following.
DOMAINS
s=string sl=s* sll=sl*
PREDICATES
select(sl,s,sl)
solve(sll)
CLAUSES
select([A|B],A,B).
select([A|B],C,[A|D]):- select(B,C,D).
solve([["Anna",A,A],["Kate",Vp,Vt], ["Natasha",Np,"green"]]):-
select(["white","green","blue"],A,ColPl),
select(["white","blue"],A,[Vt]), Vt<>"white",
select(ColPl,Vp,[Np]), Vp<>"white", Np<>"green".
And its resulting list is outputted with solve(Out) with provides a correct result to the Turbo Prolog console.
When trying to translate this to Visual Prolog, I get error c502 in line 33.
implement main
open core
constants
className = "main".
classVersion = "".
domains
s=string.
sl=s*.
sll=sl*.
%
class predicates
select:(sl,s,sl) nondeterm anyflow.
solve:(sll) nondeterm anyflow.
%
clauses
%
select([A|B],A,B).
select([A|B],C,[A|D]):- select(B,C,D).
%
solve([["Anna",A,A],["Kate",Vp,Vt],["Natasha",Np,"green"]]):-
select(["white","green","blue"],A,ColPl),
select(["white","blue"],A,[Vt]), Vt<>"white",
select(ColPl,Vp,[Np]), Vp<>"white", Np<>"green".
clauses
classInfo(className, classVersion).
clauses
run():-
console::init(),
%ERROR AFTER THIS LINE
stdIO::writef("%", solve(Out)),fail().
end implement main
goal
mainExe::run(main::run).
What I get from this error is that solve(Out) does not give anything to print. What I do not know is how to change the code to produce something to print.
I am a beginner in Prolog and I cannot figure out how to fix this problem and Google is not much of a help either, this seems to be very obscure problem.
Thank you!
I'm not familiar with Visual Prolog, but could you rewrite the offending line as:
solve(Out), stdIO::writef("%", Out),fail().
and try again?
Remember that predicates are not functions like in other programming languages; they do not have a return value.
EDIT to answer comment: a procedure predicate should succeed exactly one time. Here, main is calling your solve function which my fail or succeeds several time. To ensure that, you can try to wrap the call to solve into another predicate:
wrap_solve(S) :- solve(S), !.
wrap_solve([]).
The cut after the call to solve should ensure that you get only one solution if it succeeds.
If there's no solution (i.e., the call to solve fails), then the second clause will give a default value (an empty list in that case).
In main, you should call wrap_solve instead of solve.
I'm trying to write hasRepetition that 'recives' a list and returns true if and only if it has repetitions in it.
I wrote : hasRepetition([Head|Tail]) :- member(Head,Tail);hasRepetition(Tail).
7 ?- hasRepetition([1,1]).
ERROR: toplevel: Undefined procedure: hasRepetition/1 (DWIM could not correct goal)
Your procedure definition is fine.
I guess you forgot to consult your code, i.e. consult(name). where name is the name of the file that has your code.
I think #gusbro already give you the answer (+1). But please note that the procedure you wrote its' very inefficient.
First, memberchk/2 instead of member should be used. Then, put a cut before the disjunction. Otherwise, your code will be very slow, if called on backtracking (for instance, inside a findall...)
I am just starting to use Prolog, and already I've run into problem with a seemingly simple example. Here is my .pl file:
hacker(P) :- mountaindew(P), doesntsleep(P).
hacker(P) :- writesgoodcode(P).
writesgoodcode(jeff).
Then, after I load the program into swipl, I test it with this line at the prompt
writesgoodcode(jeff).
I thought it would display true, but I get this error:
?- hacker(jeff).
ERROR: hacker/1: Undefined procedure: mountaindew/1
Exception: (7) hacker(jeff) ?
This program works fine, but this doesn't solve my problems:
hacker(P) :- writesgoodcode(P).
writesgoodcode(jeff).
$ swipl -s dumb.pl
% dumb.pl compiled 0.00 sec, 1,112 bytes
?- hacker(jeff).
true.
Can anyone explain why my original program doesn't work? From my understanding, Prolog should "skip" the first statement since it doesn't have enough information, and check the next line. It does have enough info for that second line, and thus it should evaluate true. Any help or a point in the right direction would be great. Thanks.
As the error message says, you have an undefined procedure mountaindew/1. To make your code return true, your options are:
Define this predicate
Declare that this predicate is dynamic: dynamic(mountaindew/1)
Declare that all unknown predicates should fail (not recommended): set_prolog_flag(unknown, fail)
you could also change the order of the predicates (cannot be done always ofc)
but mostly what Kaarel said.
in the end there is not really a point in writing something that will always fail, even if you are still developing the code
This works but as I am a beginner I can't say why. The word "uninstantiated" may apply. Despite not knowing why it works, I think it's helpful to show one way that works.
hacker(P) :- mountaindew(P), doesntsleep(P).
hacker(P) :- writesgoodcode(P).
mountaindew(john).
doesntsleep(john).
writesgoodcode(jeff).