我是Prolog的新手。试图运行此代码,但给出 - 错误:未定义的过程:teaches / 2(DWIM无法纠正目标)

时间:2017-03-26 09:07:07

标签: prolog undefined procedure

这些是我写的事实

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

但是,当我运行一个查询谁教导标识为20110303的std时,它会出现此错误。我检查了所有类型的错误。它在语法和逻辑上都是正确的,但仍然说未定义的过程

?- debug.
   true.

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

2 个答案:

答案 0 :(得分:3)

获取错误

使用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

如果我使用列表来检查谓词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).

答案 1 :(得分:0)

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

  1. 只需打开终端
  2. 转到保存文件的目录
  3. 输入此命令[问题]。
  4. 现在运行您的查询(D,20110303)。
  5. 如果你在Windows中很简单,只需双击你保存的文件,它就会打开终端,你可以从该终端拨打你的查询。