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

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

这些是我写的事实

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

规则

teaches(D,S):-
   instructor(D,_,C,Z),
   enrolled(S,_,_,C,Z).

classmate(s1,s2,C):-
  enrolled(s1,_,_,C,Z),
   enrolled(s2,_,_,C,Z).

但是当我 运行 查询 who teaches std with id 20110303 时,它给出了这个错误。我已经检查过它是否存在各种错误。它在语法和逻辑上都是正确的,但仍然说 undefined procedure

?- debug.
   true.

[debug]  ?-  teaches(D,20110303).
ERROR: Undefined procedure: teaches/2 (DWIM could not correct goal)

获取错误

使用 SWI-PROLOG 如果我使用 editor 输入事实和规则然后在 Prolog interpreter 运行 查询中,我会得到同样的错误,例如

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

正在加载咨询

现在我的事实和规则在一个名为

的文件中
C:/Users/Eric/Documents/Prolog/soQuestion_4.pl

如果在解释器中我使用 consult

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

然后运行查询

?- teaches(D,20110303).

我得到了正确的结果

D = ahmed ;
false.

使用列表

检查谓词是否已加载的一种方法是使用 listing.

如果我在使用 consult 加载谓词之前使用列表检查谓词 teaches,我会得到:

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

然后如果我使用 consult

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

并检查列表我看到谓词

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

假设,您的文件名为 question.pl。如果您在 Mac 或 Ubuntu

  1. 只要打开你的终端
  2. 转到您保存文件的目录
  3. 输入此命令[问题]。
  4. 现在 运行 您的查询教导 (D,20110303)。

如果您在 windows,这非常简单,只需双击您保存的文件,它将打开终端,您可以从该终端调用您的查询。