【问题标题】:prolog uncaught exception errorprolog未捕获的异常错误
【发布时间】:2015-04-27 06:13:07
【问题描述】:

您好,我是 prolog 的新手,我正在尝试创建一个基本的家谱,所以我有这个:

/* Database for family. It consists of facts and rules. */

/* Facts */
female(ella).
female(jodi). 
female(sonya).
male(arnold).
male(chris).
male(louis).
male(mark).
father_of(arnold, chris). /* arnold is the father of chris */
father_of(louis, mark).
father_of(mark, arnold).    
mother_of(ella, sonya).
mother_of(jodi, ella).
mother_of(jodi, mark).

/* Rules */
grandmother_of(X, Z) :- 
    mother_of(X, Y), 
    (mother_of(Y, Z); father_of(Y, Z)).

familyquestions :-      
grandmother_of(X, arnold),
write('The grandmother of Arnold is '), write(X), nl,
father_of(Y, mark),
write(Y), write(' is the father of Mark'), nl, nl.

但是,我试图这样做: 定义(添加到数据库中)一个名为 is_male(X) 的规则,如果 X 是某人的父亲,则返回“yes”(或“true”)。
请注意,如果系统找到“真实”答案并且不存在更多真实答案,则系统将返回“是”。系统将返回“true ?”如果它找到一个“真实”的答案并且可能还有更多的匹配项。在 在这种情况下,如果您键入“enter”,它将返回“yes”并停止。如果您键入“;”,它将继续搜索进一步的答案。 1.2 定义一个名为 is_female(X) 的规则,如果 X 是某人的母亲,则返回“yes”或“true”。
1.3 删除(注释掉)您已添加到数据库中的非必要事实(已被新规则 is_male/1 和 is_female/1 涵盖)。

所以我添加了这个

/* 家庭数据库。它由事实和规则组成。 */

/* Facts */
female(ella).
female(jodi). 
female(sonya).
male(arnold).
male(chris).
male(louis).
male(mark).
father_of(arnold, chris). /* arnold is the father of chris */
father_of(louis, mark).
father_of(mark, arnold).    
mother_of(ella, sonya).
mother_of(jodi, ella).
mother_of(jodi, mark).

/* Rules */
grandmother_of(X, Z) :- 
    mother_of(X, Y), 
    (mother_of(Y, Z); father_of(Y, Z)).

 is_male(X, Y) :-
        father_of(X, Y).

 is_female(X, Y) :-
          mother_of(X, Y).

familyquestions :-      
grandmother_of(X, arnold),
write('The grandmother of Arnold is '), write(X), nl,
father_of(Y, mark),
write(Y), write(' is the father of Mark'), nl, nl.

但是当我运行它时,它给了我以下代码: 未捕获的异常:error(existence_error(procedure,is_male/1),top_level/0)

【问题讨论】:

  • 欢迎来到 SO!由于您是新人,您可能需要查看此link

标签: prolog


【解决方案1】:

错误error(existence_error(procedure,is_male/1),top_level/0) 表示您正在调用一元谓词is_male,即您正在调用is_male(X) 或类似的东西。但是您只定义了一个二元谓词is_male(谓词的元数通过将/N 附加到N-ary 谓词的名称来表示)。

也许您想将is_male 定义为:

is_male(X) :- father_of(X, _).

请注意,此规则不够。 您可能想要扩展它,因为您的知识库中可能有男性,但不是其他人的父亲。

完成is_male/1 的定义后,您可以通过调用is_male(p) 而不是检查事实male(p) 来确定一个人p 的性别。 对于某些人来说,male(p) 是多余的,因为他们也是其他人的父亲。您应该按照要求删除那些多余的事实。

【讨论】:

  • 谢谢!有效!是的,我现在明白我哪里出错了。
猜你喜欢
  • 2014-08-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-07-07
  • 2020-05-30
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多