I am new to Prolog. Trying to run this code but gives - ERROR: Undefined procedure: teaches / 2 (DWIM cannot fix target)
These are the facts that I wrote
instructor(ahmed,mohammed, cs101,01). instructor(sara,salah,cs101,02). instructor(maryam,faisal,cs101,03). instructor(ali,hassan,cs311,01). enrolled(201110202,huda,issa,cs101,01). enrolled(20110303,mona,amer,cs101,01). enrolled(20115566,amal,omar,cs101,01). enrolled(20118899,ahmed,hassan,cs101,01).
rules
teaches(D,S):-
instructor(D,_,C,Z),
enrolled(S,_,_,C,Z).
classmate(s1,s2,C):-
enrolled(s1,_,_,C,Z),
enrolled(s2,_,_,C,Z).
But when I run a query that teaches std with id 20110303
it gives this error. I have tested it for all kinds of errors. It is syntactically and logically correct, but still says undefined procedure
?- debug.
true.
[debug] ?- teaches(D,20110303).
ERROR: Undefined procedure: teaches/2 (DWIM could not correct goal)
source to share
Getting error
Using SWI-PROLOG I get the same error if I use editor
to enter facts and rules and then in Prolog interpreter
runs a query for example.
ERROR: Undefined procedure: teaches/2 (DWIM could not correct goal)
Download using help
Now my facts and rules are in a file named
C:/Users/Eric/Documents/Prolog/soQuestion_4.pl
and if in the interpreter I use consult
?- consult("C:/Users/Eric/Documents/Prolog/soQuestion_4.pl").
then run query
?- teaches(D,20110303).
I am getting correct result
D = ahmed ; false.
Using the listing
One way to check if a predicate is loaded is to use listing
.
If I use the listing to check the predicate teaches
before loading it with help, I get:
?- listing(teaches). ERROR: prolog_stack([frame(12,call(system:throw/1),throw(error(existence_error(procedure,teaches),context(toplevel,'DWIM could not correct goal')))),frame(11,clause(<clause>(000000000518AD30),62),'$dwim':dwim_existence_error(error,user:teaches)),frame(8,clause(<clause>(0000000005008E40),24),prolog_listing:listing(user:teaches)),frame(7,clause(<clause>(0000000005154870),3),'$toplevel':toplevel_call(user:listing(teaches)))]): procedure `teaches' does not exist (DWIM could not correct goal)
then if i load predicates consult
?- consult("C:/Users/Eric/Documents/Prolog/soQuestion_4.pl"). true.
and check with listing, I see predicate
?- listing(teaches). teaches(A, B) :- instructor(A, _, C, D), enrolled(B, _, _, C, D).
source to share
- Let's say your file name is question.pl.
If you are on Mac or Ubuntu
-
Just open a terminal
-
go to the directory where you saved the file
-
Enter this command [question].
-
Now run your query (D, 20110303).
If you are in windows it is very easy just to double click on the saved file, it will open a terminal, you can call your request from that terminal.
- *
source to share