- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在阅读 Raymond Smullyan 的“ mock 一只知更鸟”。书中有一个谜题是这样的:
Any resemblance between the Seville of this story and the famous Seville of Spain (which in fact there isn't) is purely coincidental. In this mythical town of Seville, the male inhabitants wear wigs on those and only those days when they feel like it. No two inhabitants behave alike on all days; that is, given any two male inhabitants, there is at least one day on which one of them wears a wig and the other doesn't. Given any male inhabitants X and Y, inhabitant Y is said to be a follower of X ifY wears a wig on all days that X does. Also, given any inhabitants X, Y, and Z, inhabitant Z is said to be a follower of X and Y if Z wears a wig on all days that X and Y both do.
Five of the inhabitants are named Alfredo, Bernardo, Ben- ito, Roberto, and Ramano. The following facts are known about them:
Fact 1.. Bernardo and Benito are opposite in their wig-wear- ing habits; that is, on any given day, one of them wears a wig and the other one doesn't.
Fact 2: Roberto and Ramano are likewise opposites.
Fact 3: Ramano wears a wig on those and only those days when Alfredo and Benito both wear one.
Seville has exactly one barber, and the following facts are known about him:
Fact 4: Bernardo is a follower of Alfredo and the barber.
Fact 5: Given any male inhabitant X, if Bernardo is a fol- lower of Alfredo and X, then the barber is a follower of X alone.
Alfredo wears only black wigs; Bernardo wears only white wigs; Benito wears only gray wigs; Roberto wears only red wigs; and Ramano wears only brown wigs.
One Easter morning, the barber was seen wearing a wig. What color was he wearing?
isOpposite( bernardo, benito ).
isOpposite( benito , bernardo ).
isOpposite( roberto , ramano ).
isOpposite( ramano , roberto ).
wears( alfredo , black ).
wears( bernardo, white ).
wears( benito , gray ).
wears( roberto , red ).
wears( ramano , brown ).
whatWearsTheBarber( WigColor ) :-
member( Barber, [ alfredo, benito, bernardo, roberto, ramano ] ),
wears( Barber, WigColor ).
Step 1: First, we prove that Roberto is a follower of the barber.
Well, consider any day on which the barber wears a wig. Either Alfredo wears a wig on that day or he doesn't. Suppose Alfredo does. Then Bernardo also wears a wig on that day, because Bernardo is a follower of Alfredo and the barber. So Benito can't wear a wig on that day, because he is opposite to Bernardo. Then Ramano can't wear a wig on that day, because he wears wigs only on those days when Alfredo and Benito both do, and Benito doesn't have one on this day. Since Ramano doesn't wear a wig on this day, then Roberto must, because Roberto is opposite to Ramano. This proves that on any day on which the barber wears a wig, if Alfredo also does, then so does Roberto.
Now, what about a day on which the barber wears a wig but Alfredo doesn't? Well, since Alfredo doesn't, then it cer- tainly is not the case that Alfredo and Benito both do; hence Ramano doesn't, by Fact 3, and therefore Roberto does, by Fact 2. So Roberto wears a wig on any day that the barber does and Alfredo doesn't-indeed, he wears a wig on all days that Alfredo doesn't, regardless of the barber. This proves that on any day on which the barber wears a wig, Roberto also does, regardless of whether Alfredo does or does not wear a wig on that day. So Roberto is indeed a follower of the barber.
最佳答案
编辑 2:由于@killy9999 发布了书中的部分解决方案,我决定重写我的 Prolog 以反射(reflect) Smullyan 的推理。原始部分解决方案保留在下面。
首先是一些基本结构
person(alfredo).
person(benito).
person(roberto).
person(ramano).
person(bernardo).
day([_Alfredo,_Benito,_Bernardo,_Roberto,_Romano]).
% barber(alfredo). % Follows from Fact 4.
barber(benito).
% barber(bernardo). % Follows from Fact 4.
barber(roberto).
barber(romano).
wearsWig(alfredo,[1,_X,_Y,_Z,_W]).
wearsWig(benito,[_X,1,_Y,_Z,_W]).
wearsWig(bernardo,[_X,_Y,1,_Z,_W]).
wearsWig(roberto,[_X,_Y,_Z,1,_W]).
wearsWig(romano,[_X,_Y,_Z,_W,1]).
noWig(alfredo,[0,_X,_Y,_Z,_W]).
noWig(benito,[_X,0,_Y,_Z,_W]).
noWig(bernardo,[_X,_Y,0,_Z,_W]).
noWig(roberto,[_X,_Y,_Z,0,_W]).
noWig(romano,[_X,_Y,_Z,_W,0]).
consistent2(_D,[]).
consistent2(D,[(X,Y)|Os]):-wearsWig(X,D),noWig(Y,D),consistent2(D,Os).
consistent2(D,[(X,Y)|Os]):-noWig(X,D),wearsWig(Y,D),consistent2(D,Os).
consistent3(O,G):-consistent3(O,_D,G).
consistent3(_O,_D,[]).
consistent3(O,D,[(X,Y,Z)|Gs]):-
wearsWig(X,D),wearsWig(Y,D),wearsWig(Z,D),
consistent2(D,O),consistent3(O,D,Gs).
consistent3(O,D,[(_X,Y,_Z)|Gs]):-
noWig(Y,D),consistent2(D,O),consistent3(O,D,Gs).
consistent3(O,D,[(_X,_Y,Z)|Gs]):-
noWig(Z,D),consistent2(D,O),consistent3(O,D,Gs).
fact3(D):-wearsWig(romano,D),wearsWig(alfredo,D),wearsWig(benito,D).
fact3(D):-noWig(alfredo,D),noWig(romano,D).
fact3(D):-noWig(benito,D),noWig(romano,D).
?- person(Barber),barber(Barber),
O = [(benito,bernardo),(roberto,romano)],
G = [(bernardo,alfredo,Barber),(romano,alfredo,benito)],
consistent3(O,D,G),fact3(D),
wearsWig(Barber,D),noWig(roberto,D).
false.
?- person(Barber)barber(Barber),
O = [(benito,bernardo),(roberto,romano)],
G = [(bernardo,alfredo,Barber),(romano,alfredo,benito)],
consistent3(O,D,G),fact3(D),
wearsWig(alfredo,D),wearsWig(roberto,D),noWig(bernardo,D).
false.
consistent4(_D,_Barber,[]).
consistent4(D,Barber,[X|Xs]):-
wearsWig(X,D1),wearsWig(alfredo,D1),
noWig(bernardo,D1),consistent4(D,Barber,Xs).
consistent4(D,Barber,[X|Xs]):-
wearsWig(X,D),wearsWig(alfredo,D),
wearsWig(bernardo,D),wearsWig(Barber,D),
consistent4(D,Barber,Xs).
wears(alfredo, black).
wears(bernardo, white).
wears(benito, gray).
wears(roberto, red).
wears(ramano, brown).
whatWearsTheBarber(WigColor):-
person(Barber),
day(Easter),
barber(Barber),
wearsWig(Barber,Easter),
fact3(Easter),
G=[(bernardo,alfredo,Barber),(romano,alfredo,benito)],
O=[(benito,bernardo),(roberto,romano)],
consistent2(Easter,O),
consistent3(O,D,G),
X=[alfredo,benito,bernardo,roberto,romano],
consistent4(D,Barber,X),
wears(Barber, WigColor).
?- findall(WigColor,whatWearsTheBarber(WigColor),B),list_to_set(B,R).
B = [red, red, red, red, red, red, red, red, red|...],
R = [red].
[ [WearsWig,IsBarber], ... , [WearsWig,IsBarber] ]
seville(S) :-
S=[Benito,Bernardo,Roberto,Ramano,Alfredo],
opposite(Benito,Bernardo),
opposite(Roberto,Ramano),
fact3(Ramano,Alfredo,Benito),
fact4(Bernardo,Alfredo),
noBarber(Bernardo),noBarber(Alfredo),
onlyOneBarberWearsWig(S).
noWig([0,_X]).
wearsWig([1,_X]).
isBarber([_X,1]).
noBarber([_X,0]).
opposite(X,Y):-noWig(X),wearsWig(Y).
opposite(X,Y):-noWig(Y),wearsWig(X).
fact3(X,Y,Z):-wearsWig(X),wearsWig(Y),wearsWig(Z).
fact3(X,Y,_Z):-noWig(X),noWig(Y).
fact3(X,_Y,Z):-noWig(X),noWig(Z).
fact4(X,Y):-wearsWig(X),wearsWig(Y),wearsWig(Z),isBarber(Z).
fact4(_X,Y):-noWig(Y).
onlyOneBarberWearsWig([X|Xs]):-isBarber(X),wearsWig(X),noBarbers(Xs).
onlyOneBarberWearsWig([X|Xs]):-noBarber(X),onlyOneBarberWearsWig(Xs).
noBarbers([]).
noBarbers([X|Xs]):-noBarber(X),noBarbers(Xs).
barbersWigColor([_X,_Y,_Z,_U,Alfredo],black):-isBarber(Alfredo).
barbersWigColor([_X,Bernardo,_Y,_Z,_U],white):-isBarber(Bernardo).
barbersWigColor([Benito,_X,_Y,_Z,_U],gray):-isBarber(Benito).
barbersWigColor([_X,_Y,Roberto,_Z,_U],red):-isBarber(Roberto).
barbersWigColor([_X,_Y,_Z,Ramano,_U],brown):-isBarber(Ramano).
whatWearsTheBarber(Color):-seville(X),barbersWigColor(X,Color).
?- seville(X).
X = [[0, 0], [1, 0], [1, 1], [0, 0], [0, 0]] ;
X = [[0, 0], [1, 0], [1, 1], [0, 0], [1, 0]] ;
X = [[0, 0], [1, 0], [1, 1], [0, 0], [0, 0]] ;
X = [[1, 1], [0, 0], [1, 0], [0, 0], [0, 0]] ;
X = [[1, 0], [0, 0], [1, 1], [0, 0], [0, 0]] ;
false.
?- whatWearsTheBarber(Color).
Color = red ;
Color = red ;
Color = red ;
Color = gray ;
Color = red ;
false.
关于prolog - "Who is the barber"Prolog 中的逻辑谜题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10268086/
我正在学习序言。 在我看来,prolog 的规则(关系和简单的事实)是“肯定的”——他们说的是或可能是真的。 向 prolog 程序添加新的此类规则只会增加“正面”知识。它不能添加“负面”事实来说明某
希望你一切都好。我是 prolog 的新手,我在编写代码时遇到问题。这段代码的目的很简单。它将列表中的每个元素添加到最后一个。我可以用 Java 做的事情是: static void add(
在closed-world assumption下, what is not currently known to be true, is false Prolog 的语义通常被称为遵循封闭世界假设,
我正在 Prolog (swi-prolog) 中做我的第一步,但无法解决以下问题:如何将存在量化的规则包含在我的事实中;具体来说,我如何包含句子“每个人都是某人的 friend ”\forall x
我知道如何以过程方式(即,在 C++、Java 等中)对 BST 执行范围查询,但我发现很难转换为 Prolog 语言。 程序的方式应该是这样的: http://www.geeksforgeeks.o
Prolog 中是否有(相对)当前最佳实践的引用资料?一本适合没有学习过逻辑编程或“Prolog 的工艺”等高级文本的商业 Prolog 开发人员? 有很多通用教程,但我能找到的关于最佳实践的唯一一个
这是CFG: S -> T | V T -> UU U -> aUb | ab V -> aVb | aWb W -> bWa | ba 所以这将接受某种形式的: {a^n b^n a^m b^m |
我目前有以下问题,我想用 Prolog 解决。这是一个简单的例子,很容易在 Java/C/whatever 中解决。我的问题是,我认为与 Java 的思想联系太紧密,无法以利用 Prolog 逻辑能力
就目前而言,这个问题不适合我们的问答形式。我们希望答案得到事实、引用资料或专业知识的支持,但这个问题可能会引发辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visit the
我无法理解差异列表,尤其是在这个谓词中: palindrome(A, A). palindrome([_|A], A). palindrome([C|A], D) :- palindrome(A
(这不是一个类(class)作业问题。只是我自己的个人学习。) 我正在尝试在 Prolog 中进行练习以从列表中删除元素。这是我的代码: deleteall([],X,[]). deleteall([
我最近试图了解 Prolog,它似乎可以很好地映射到很多领域,但我无法弄清楚它可能不擅长什么。 那么它有什么不好的(除了需要实时/无 gc 性能的东西)? 最佳答案 我同意你的一般评估,即 Prolo
我正在组装一个简单的元解释器,它输出证明的步骤。我无法将证明步骤作为输出参数。我的谓词 explain1 以我想要的详细形式返回证明,但不是作为输出参数。我的谓词 explain2 将证明作为输出参数
hi(g,plus(A,B),int) :- hi(g,A,int),hi(g,B,int),!. 在上面的语句中 '!' 是什么意思?在声明的末尾签名吗? 最佳答案 那是 cut operator
有没有一种简单的方法可以让 prolog 中的查询只返回每个结果一次? 例如我正在尝试类似的东西: deadly(Xn) :- scary(X), Xn is X - 1, Xp is X + 1,
我正在尝试学习 Prolog。这是我使用这种语言的第一步。作为练习,我想编写可以识别一些扑克手牌的程序(同花顺、同花顺、满屋等)。 我正在 Prolog 中寻找良好的卡片表示。我需要有可能检查一张卡片
我刚刚被介绍到 Prolog 并且正在尝试编写一个谓词来查找整数列表的最大值。我需要写一个从头开始比较,另一个从结尾比较。到目前为止,我有: max2([],R). max2([X|Xs], R):-
我试图在Prolog中编写谓词palindrome/1,当且仅当其列表输入包含回文列表时才为true。 例如: ?- palindrome([1,2,3,4,5,4,3,2,1]). 是真的。 有什么
我正在尝试编写一个程序,该程序将两个列表作为输入并检查适当的子集。我开始于: proper([A],[]). proper([],[A]). proper([A|T1],[A|T2]) :- prop
我是 Prolog 的新手,我正在使用 SWI-Prolog v6.6 在 *.pl 中存储断言文件。 :- dynamic fact/2. assert(fact(fact1,fact2)). 使用
我是一名优秀的程序员,十分优秀!