2015-11-05 89 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])). 

而从这个独特的解决方案是不容易确定搜索