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

Far*_*Far 4 procedure prolog undefined

这些是我写的事实

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).
Run Code Online (Sandbox Code Playgroud)

规则

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

classmate(s1,s2,C):-
  enrolled(s1,_,_,C,Z),
   enrolled(s2,_,_,C,Z).
Run Code Online (Sandbox Code Playgroud)

但是,当我运行一个查询,该查询用id教std时20110303,就会出现此错误。我已经检查了所有类型的错误。从语法和逻辑上讲这是正确的,但仍然表示未定义的过程

?- debug.
   true.

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

Guy*_*der 6

得到错误

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

ERROR: Undefined procedure: teaches/2 (DWIM could not correct goal)
Run Code Online (Sandbox Code Playgroud)

咨询加载

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

C:/Users/Eric/Documents/Prolog/soQuestion_4.pl
Run Code Online (Sandbox Code Playgroud)

如果我在口译员中使用 consult

?- consult("C:/Users/Eric/Documents/Prolog/soQuestion_4.pl").
Run Code Online (Sandbox Code Playgroud)

然后运行查询

ERROR: Undefined procedure: teaches/2 (DWIM could not correct goal)
Run Code Online (Sandbox Code Playgroud)

我得到正确的结果

C:/Users/Eric/Documents/Prolog/soQuestion_4.pl
Run Code Online (Sandbox Code Playgroud)

使用清单

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

如果我在使用teaches咨询之前使用清单来检查谓词,则会得到:

?- consult("C:/Users/Eric/Documents/Prolog/soQuestion_4.pl").
Run Code Online (Sandbox Code Playgroud)

然后如果我用咨询加载谓词

?- teaches(D,20110303).
Run Code Online (Sandbox Code Playgroud)

并检查清单,我看到谓词

D = ahmed ;
false.
Run Code Online (Sandbox Code Playgroud)