2015-11-05 90 views
2

問題是:鹽被偷了!那麼,它被發現的罪魁禍首是卡特彼勒, 比爾蜥蜴或柴郡貓。三是審判和法院作出以下 聲明:將邏輯謎題轉化爲謂詞演算和序言/ dlv

CATERPILLAR: Bill the Lizard ate the salt. 
BILL THE LIZARD: That is true! 
CHESHIRE CAT: I never ate the salt. 

。就這樣,他們中的至少一個撒謊和至少一個說了實話。誰吃了鹽 ?

我知道如果法案是真的,比所有的陳述都是真的,如果柴郡是真的,那麼所有的都是假的,所以它一定是毛毛蟲。

綜觀謂詞演算和編程它,它會是這樣的權利:

suspect(caterpillar). 
suspect(lizard). 
suspect(cat). 

:- suspect(cat), suspect(lizard). 
:- suspect(cat), suspect(caterpillar). 
:- suspect(lizard), suspect(caterpillar). 

%where these imply not more than one of these can be true or returned in our set 

但隨後進一步描述這個謂詞邏輯,我不我會怎樣形容描述或抗辯的他們製作。如果一個陳述是真實的,那麼這意味着其他人可能會失敗。

回答

2

一個關於這個難題好處是,你甚至不需要一階謂詞邏輯來模擬它:它足夠使用命題邏輯,因爲無論是犯罪嫌疑人所在或講真話可以與指示一個布爾變量,而且這些語句本身也只是布爾變量的語句。

因此,考慮使用約束求解器布爾型變量當解決這個任務與Prolog。有關詳細信息,請參閱

這裏是一個樣品溶液,使用SICStus Prolog的或SWI:

?- solution(Pairs). 
Pairs = [caterpillar-1, lizard-0, cat-0]. 

:- use_module(library(clpb)). 

solution(Pairs) :- 
     Suspects = [_Caterpillar,Lizard,Cat], 
     pairs_keys_values(Pairs, [caterpillar,lizard,cat], Suspects), 
     Truths = [CaterpillarTrue,LizardTrue,CatTrue], 
     % exactly one of them ate the salt 
     sat(card([1], Suspects)), 
     % the statements 
     sat(CaterpillarTrue =:= Lizard), 
     sat(LizardTrue =:= Lizard), 
     sat(CatTrue =:= ~Cat), 
     % at least one of them tells the truth: 
     sat(card([1,2,3], Truths)), 
     % at least one of them lies: 
     sat(card([1,2,3], [~CaterpillarTrue,~LizardTrue,~CatTrue])). 

而從這個獨特的解決方案是不容易確定搜索