Search code examples
prologundefinedprocedure

I'm new to Prolog. Trying to run this code but gives - ERROR: Undefined procedure: teaches/2 (DWIM could not correct goal)


These are the facts I have written

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

The 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 who teaches std with id 20110303, it gives this error. I have checked it for all kind 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)

Solution

  • Getting the error

    Using SWI-PROLOG I get the same error if I use an editor to enter the facts and rules then in the Prolog interpreter run the query, e.g.

    ERROR: Undefined procedure: teaches/2 (DWIM could not correct goal)
    

    Loading with consult

    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 the query

    ?- teaches(D,20110303).
    

    I get the correct result

    D = ahmed ;
    false.
    

    Using listing

    One way to check if a predicate is loaded is to use listing.

    If I use listing to check the predicate teaches before loading it with consult 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 the predicates with consult

    ?- consult("C:/Users/Eric/Documents/Prolog/soQuestion_4.pl").
    true.
    

    and check with listing I see the predicate

    ?- listing(teaches).
    teaches(A, B) :-
            instructor(A, _, C, D),
            enrolled(B, _, _, C, D).