Prolog BACKWARD CHAINING推理引擎

时间:2015-04-04 02:30:30

标签: prolog

以下是针对大学的(不是作业,而是辅导作品)。此刻(中期休息时间),我无法得到导师的帮助,所以我会告诉你很好的人,看看我哪里出错了。计划是创建一个非常基本的反向链接推理引擎。我这样做(按照提供的说明),但现在我们必须制作一个证明树。以下是我的代码:

%operator rules
:- op(800, fx, if).
:- op(700, xfx, then).
:- op(300, xfy, or).
:- op(200, xfy, and).

:- op(800, fx, fact).
:- op(800, fxf, <=).

% BACKWARD CHAINING INFERENCE ENGINE with proof tree
is_true(P, P) :-
   fact P.
is_true(C, C <= ProofTreeA) :-
   if A then C, is_true(A, ProofTreeA).
is_true(P1 and P2, ProofTree1 and ProofTree2) :-
   is_true(P1), is_true(P2), ProofTree1, ProofTree2.
is_true(P1 or P2, ProofTree1) :- is_true(P1), ProofTree1.
is_true(P1 or P2, ProofTree2) :- is_true(P2), ProofTree2.

% production rules
if covering_scales then family_fish.
if covering_skin then family_mammal.
if family_mammal and size_large then species_whale.
if family_mammal and size_small then species_seal.
if family_fish and size_large then species_tuna.
if family_fish and size_small then species_sardine.

现在在我查阅文件之后我要声明以下内容:

asserta(fact covering_scales).
asserta(fact size_large).

然后我输入以下查询:

is_true(species_tuna, P).

但我得到的是以下错误消息:

uncaught exception: error(existence_error(procedure,is_true/2),top_level/0

我的猜测是我错过了一些显而易见的东西,但我无法弄清楚是什么。

1 个答案:

答案 0 :(得分:1)

我想以下规则:

is_true(P1 and P2, ProofTree1 and ProofTree2) :-
   is_true(P1), is_true(P2), ProofTree1, ProofTree2.
is_true(P1 or P2, ProofTree1) :- is_true(P1), ProofTree1.
is_true(P1 or P2, ProofTree2) :- is_true(P2), ProofTree2.

应更正为:

is_true(P1 and P2, ProofTree1 and ProofTree2) :-
   is_true(P1, ProofTree1), is_true(P2, ProofTree2).
is_true(P1 or _, ProofTree1) :- is_true(P1, ProofTree1).
is_true(_ or P2, ProofTree2) :- is_true(P2, ProofTree2).

使用op / 3修复程序,您会得到以下结果:

?- asserta(fact covering_scales).
Yes
?- asserta(fact size_large).
Yes
?- is_true(species_tuna, P).
P = (species_tuna<=(family_fish<=covering_scales)and size_large) ;
No

你在GNU-Prolog中遇到了一个is_true / 2未定义的错误,因为 汇编政策是:一个错误的条款,没有条款 所有

再见

相关问题