2015-04-27 76 views
1

您好我是新來的序言和我試圖創建一個基本的家譜,這樣太不公平了我有這樣的:序言未捕獲的異常錯誤

/* 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. 

不過,我試圖做到這一點: 定義(添加到數據庫中)如果X是某人的父親,則返回「是」(或「真」)的規則稱爲is_male(X)。
請注意,如果系統找到「真實」答案並且不存在更多真實答案,系統將返回「是」。系統將返回「真實?」如果它找到了「真實」的答案,並且還有可能進一步匹配。在 這種情況下,如果你輸入「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. 

但是當我運行它,它給了我這樣的代碼: 未捕獲的異常:錯誤(existence_error(程序,is_male/1),TOP_LEVEL/0)

+0

歡迎來到SO!由於您是新手,您可能需要查看此[鏈接](http://meta.stackexchange.com/a/5235/187716)。 – fferri

回答

3

錯誤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, _). 

當心,這個規則是不夠。 您可能想要擴展它,因爲您的KB中可能有男性,但不是其他人的父親。

一旦你完成了is_male/1的定義,你可以通過調用is_male(p)而不是檢查事實male(p)來確定一個人的性別。 有些人因事實male(p)是多餘的,因爲他們也是別人的父親。你應該按照要求刪除那些冗餘的事實。

+0

謝謝!有效!是的,我現在明白我錯在哪裏。 – jrsk8chivas

相關問題