2015-04-04 71 views
1

以下是適用於大學(不是作業,而是教學作品)。我現在無法從我的導師那裏得到幫助(中期休息),所以我儘管我會告訴你很好的人,看看我出了什麼問題。計劃是創建一個非常基本的反向鏈接推理引擎。我這樣做了(按照提供的說明),但現在我們也必須製作一個證明樹。下面是我的代碼:Prolog BACKWARD CHAINING INFERENCE引擎

%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 

我的猜測是我錯過了一些明顯的東西,但我無法弄清楚什麼。

+2

當我嘗試將代碼加載到GNU Prolog中時,出現一些錯誤。例如,'is_true(C,C <= ProofTreeA)'有語法錯誤。你也在謂詞中調用'is_true',在某些地方只有一個參數。 – lurker 2015-04-04 12:20:48

+0

也,': - op(800,fxf,<=)。「這很有趣 – CapelliC 2015-04-04 16:03:58

+0

謝謝。我的兩個錯誤是CapelliC和@lurker說的。需要有xfx(不是fxf錯字),我只用一個參數調用is_true。現在很好地工作,非常感謝 – JamesWat 2015-04-05 02:25:00

回答

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未定義的錯誤,因爲 編譯策略在那裏:一個er無條件,無條款 全部。

再見