【问题标题】:Prolog - Reduce the knowledge base by deductionProlog - 通过演绎减少知识库
【发布时间】:2018-04-16 19:08:51
【问题描述】:

我需要创建一个规则来搜索与 my_rule 匹配的事实。这些事实将用于改变知识库。 (my_rule (Conclusion, Premise))。

我有这个知识库开始:

:- dynamic( is/2 ).

is( m1, house ).
is( m1, thing ).
is( m2, house ).
is( m2, thing ).
is( m3, niche ).
is( m3, house ).
is( m3, thing ).
is( m4, car ).
is( m4, mobile ).
is( m4, house ).
is( m4, thing ).

my_rule( is( X, thing ), is( X, house ) ).
my_rule( is( X, house ), is( X, niche ) ).

找到规则后,代码将搜索数据库中是否存在结论及其前提。

我不知道如何做到这一点,是的,这是一个家庭作业。 我只想有人指出从哪里开始。

谢谢。

【问题讨论】:

  • 你到底想做什么?您是否想根据是否可以用my_rule/2 推断出is/2 事实的数量(使用retract/1)?将您的事实输入 Prolog 并使用查询 my_rule(Fact1, Fact2), call(Fact1), call(Fact2)。如果my_rule 建立的两个事实都在您的数据库中,则此操作成功。因此,如果它确实成功了,您可以撤回 Fact1Fact2,因为其中一个可以从另一个推导出来。
  • @lurker 是的,这就是练习的目的。谢谢你的回答。

标签: prolog prolog-directive-dynamic


【解决方案1】:

首先,您需要选择一个不同的谓词名称,因为 is/2 是计算算术表达式的内置函数,例如

?- X is 3+2.
X = 5.

如果您尝试查阅源文件,您的代码会导致以下错误:

?- [myfile].
ERROR: /home/someuser/code/myfile.pl:1:
        dynamic/1: No permission to modify static procedure `(is)/2'

让我们将其重命名为is_a/2。然后你的代码看起来像:

:- dynamic( is_a/2 ).

is_a(m1, house).
is_a(m1, thing).
is_a(m2, house).
is_a(m2, thing).
is_a(m3, niche).
is_a(m3, house).
is_a(m3, thing).
is_a(m4, car).
is_a(m4, mobile).
is_a(m4, house).
is_a(m4, thing).

然后您可以定义一个谓词来描述成对的结论和前提,如下所示:

conclusion_premise(is_a(X, thing), is_a(X, house)).
conclusion_premise(is_a(X, house), is_a(X, niche)).

在此基础上,您可以定义 my_rule/2 来描述 CP 必须是相应的一对结论和前提,然后将两者称为目标:

my_rule(C,P) :-
   conclusion_premise(C,P),
   call(C),
   call(P).

现在您可以查询my_rule/2 以搜索相应的结论-前提对:

?- my_rule(Conclusion,Premise).
Conclusion = is_a(m1, thing),
Premise = is_a(m1, house) ;
Conclusion = is_a(m2, thing),
Premise = is_a(m2, house) ;
Conclusion = is_a(m3, thing),
Premise = is_a(m3, house) ;
Conclusion = is_a(m4, thing),
Premise = is_a(m4, house) ;
Conclusion = is_a(m3, house),
Premise = is_a(m3, niche) ;
false.

【讨论】:

  • 感谢您的回答。我的代码最初是法语的,我翻译了它,之后没有运行它。很抱歉。
  • 您的代码正是我想要的。谢谢一百万!
猜你喜欢
  • 1970-01-01
  • 2022-09-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-10-11
  • 1970-01-01
相关资源
最近更新 更多