2013-08-17 107 views
2

我目前正在嘗試學習一點Prolog。作爲一個練習我試圖解決以下幾個謎語:序言 - 試圖解決文本謎題

這些規則給出:

*Every person that has neither a car nor a plane, has a bike. 
*Every person that doesn't have a plane but has a bike, has a car 
*Every person that doesn't have a plane but has a car, has a truck 
*Every person that doesn't have a truck but has a boat, doesn't have a plane 
*Every person that doesn't have a boat but has a plane, doesn't have a car 

現在有四個人:

*Person1 doesn't have a car but has a boat 
*Person2 doesn't have a boat but has a plane 
*Person3 doesn't have a plane but has a bike 
*Person4 doesn't have a bike but has a car 

哪個人沒有一輛卡車?

我想出來的,到目前爲止是這樣的:

doesnthave(car,pa). 
has(boat,pa). 
doesnthave(boat,pb). 
has(plane,pb). 
doesnthave(plane,pc). 
has(bike,pc). 
doesnthave(bike,pd). 
has(car,pd). 

has(bike,X) :- doesnthave(car,X),doesnthave(plane,X). 
has(car,X) :- doesnthave(plane,X),has(bike,X). 
has(truck,X) :- doesnthave(plane,X),has(car,X). 
doesnthave(plane,X) :- doesnthave(truck,X),has(boat,X). 
doesnthave(car,X) :- doesnthave(boat,X),has(plane,X). 

現在,這似乎並不足夠。或者這不是在prolog中解決像這樣的難題的方法嗎?

編輯:看起來前兩個陳述是矛盾的。它們一起產生:每個既沒有汽車也沒有飛機的人擁有一輛汽車。我不確定是否有明智的解決方案。

回答

3

不知道這個解決方案,但知識的簡單的表示一定幫助:

has(car, pa, n). has(boat, pa, y). 
has(boat, pb, n). has(plane, pb, y). 
has(plane, pc, n). has(bike, pc, y). 
has(bike, pd, n). has(car, pd, y). 

has(bike, X, y) :- has(car, X, n), has(plane, X, n). 
has(car, X, y) :- has(plane, X, n), has(bike, X, y). 
has(truck, X, y) :- has(plane, X, n), has(car, X, y). 
has(plane, X, n) :- has(truck, X, n), has(boat, X, y). 
has(car, X, n) :- has(boat, X, n), has(plane, X, y). 

現在我們可以查詢有什麼所有制(即注意這是一個一對多的關係)

?- setof((P,T,R), has(T,P,R), L), maplist(writeln, L). 
pa,boat,y 
pa,car,n 
pb,boat,n 
pb,car,n 
pb,plane,y 
pc,bike,y 
pc,car,y 
pc,plane,n 
pc,truck,y 
pd,bike,n 
pd,car,y 
L = [ (pa, boat, y), (pa, car, n), (pb, boat, n), (pb, car, n), (pb, plane, y), (pc, bike, y), (pc, car, y), (pc, ..., ...), (..., ...)|...]. 

注意我放置P erson之前T運輸,然後我們可以目測檢查結果...

似乎t他的解決方案是一個三重...

+0

事實證明,前兩個陳述存在矛盾。 他們一起屈服:每個既沒有汽車也沒有飛機的人擁有一輛汽車。 – tschaei