【问题标题】:I'm new to Prolog. Trying to run this code but gives - ERROR: Undefined procedure: teaches/2 (DWIM could not correct goal)我是 Prolog 的新手。尝试运行此代码但给出 - 错误:未定义的过程:教导/2(DWIM 无法纠正目标)
【发布时间】:2017-08-19 00:17:38
【问题描述】:

这些是我写的事实

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

但是当我运行一个查询谁用 id 20110303 教 std 时,它给出了这个错误。我已经检查过它是否有各种错误。它在语法和逻辑上都是正确的,但仍然是未定义的过程

?- debug.
   true.

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

【问题讨论】:

    标签: prolog undefined procedure


    【解决方案1】:

    假设您的文件名为question.pl。如果您使用的是 Mac 或 Ubuntu

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

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

    【讨论】:

      【解决方案2】:

      得到错误

      使用 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("C:/Users/Eric/Documents/Prolog/soQuestion_4.pl").
      true.
      

      并检查列表我看到谓词

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

      【讨论】:

        猜你喜欢
        • 2023-03-25
        • 1970-01-01
        • 2017-05-02
        • 1970-01-01
        • 2021-04-15
        • 1970-01-01
        • 2019-01-26
        • 2021-12-24
        • 2016-03-27
        相关资源
        最近更新 更多