Prolog 和逻辑难题
Prolog and Logic Puzzles
我似乎对 Prolog 中事实的统一有疑问,但无法确认。一切看起来 应该 工作,我已经查找了使用 Prolog 解决逻辑难题的示例,但没有实际效果,鉴于 Prolog 的相对稀有性。
这个是一个额外的学分分配,所以我不确定它是否有效,但我真的很困惑如何从这里开始
% Names
name(teo).
name(mira).
name(bruno).
name(igor).
%Food
food(sandwich).
food(pie).
food(hamburger).
food(pizza).
%Hobby
hobby(crosswords).
hobby(writing).
hobby(reading).
hobby(photography).
%Shirt Colour
shirt(white).
shirt(yellow).
shirt(blue).
shirt(red).
%Other
girl(mira).
student((Name, Food, Hobby, Shirt)) :-
name(Name), food(Food), hobby(Hobby), shirt(Shirt).
solution(L):-
length(L,4),
L= [student(teo, sandwich,_,_),_,_,_],
member(student(mira, pite, crosswords,_),L),
member(student(girl(GirlName),_,_,white),L),
member(student(bruno,_,_,yellow),L),
member(student(_,hamburger,writing,_),
L= [_, student(_,pie,_,_),_,_],
next(student(_,pie,_,_), student(teo,_,_,_), L),
next(student(bruno,_,_,_), student(_,pizza,_,_), L),
next(student(_,_,_,white), student(_,pizza,_,_), L),
member(student(igor,_,reading,_),L),
next(student(_,_,_,blue), student(girl(GirlName),_,_,_), L).
next(A, B, Ls) :- append(_, [A,B|_], Ls).
next(A, B, Ls) :- append(_, [B,A|_], Ls).
问题在于它不会将 solution(L)
视为谓词或规则,而只是一段文本,因此我什至无法测试它是否正确。我最感兴趣的是为什么它甚至无法运行。
起初,我认为这个 girl/1
是许多人所说的罪魁祸首。但即使删除所有此类事件,您的定义仍然会失败(并且在修复该语法错误之后)。这是 solution(L)
仍然失败的负责部分:
:- op(950, fy, *).
*(_).
solution(L) :-
* length(L,4),
L= [student(_/*teo*/, sandwich,_,_),_,_,_],
member(student(_/*mira*/, pite, _/*crosswords*/,_),L),
* member(student(girl(GirlName),_,_,white),L),
* member(student(bruno,_,_,yellow),L),
member(student(_,hamburger,_/*writing*/,_),L),
L= [_, student(_,pie,_,_)|_/*[_,_]*/],
* next(student(_,pie,_,_), student(teo,_,_,_), L),
next(_/*student(bruno,_,_,_)*/, student(_,pizza,_,_), L),
* next(student(_,_,_,white), student(_,pizza,_,_), L),
* member(student(igor,_,reading,_),L),
* next(student(_,_,_,blue), student(girl(GirlName),_,_,_), L).
next(A, B, Ls) :- append(_, [A,B|_], Ls).
next(A, B, Ls) :- append(_, [B,A|_], Ls).
所有的名字都无关紧要,就像他们的爱好一样。真正重要的是食物!
你只有四个位置,但总共有五种食物(三明治、馅饼、肉馅饼、汉堡、比萨)- 只给我其中一种!
这是 Prolog 中的优点:您可以进行此类概括以获得明确的诊断。
我似乎对 Prolog 中事实的统一有疑问,但无法确认。一切看起来 应该 工作,我已经查找了使用 Prolog 解决逻辑难题的示例,但没有实际效果,鉴于 Prolog 的相对稀有性。
这个是一个额外的学分分配,所以我不确定它是否有效,但我真的很困惑如何从这里开始
% Names
name(teo).
name(mira).
name(bruno).
name(igor).
%Food
food(sandwich).
food(pie).
food(hamburger).
food(pizza).
%Hobby
hobby(crosswords).
hobby(writing).
hobby(reading).
hobby(photography).
%Shirt Colour
shirt(white).
shirt(yellow).
shirt(blue).
shirt(red).
%Other
girl(mira).
student((Name, Food, Hobby, Shirt)) :-
name(Name), food(Food), hobby(Hobby), shirt(Shirt).
solution(L):-
length(L,4),
L= [student(teo, sandwich,_,_),_,_,_],
member(student(mira, pite, crosswords,_),L),
member(student(girl(GirlName),_,_,white),L),
member(student(bruno,_,_,yellow),L),
member(student(_,hamburger,writing,_),
L= [_, student(_,pie,_,_),_,_],
next(student(_,pie,_,_), student(teo,_,_,_), L),
next(student(bruno,_,_,_), student(_,pizza,_,_), L),
next(student(_,_,_,white), student(_,pizza,_,_), L),
member(student(igor,_,reading,_),L),
next(student(_,_,_,blue), student(girl(GirlName),_,_,_), L).
next(A, B, Ls) :- append(_, [A,B|_], Ls).
next(A, B, Ls) :- append(_, [B,A|_], Ls).
问题在于它不会将 solution(L)
视为谓词或规则,而只是一段文本,因此我什至无法测试它是否正确。我最感兴趣的是为什么它甚至无法运行。
起初,我认为这个 girl/1
是许多人所说的罪魁祸首。但即使删除所有此类事件,您的定义仍然会失败(并且在修复该语法错误之后)。这是 solution(L)
仍然失败的负责部分:
:- op(950, fy, *). *(_). solution(L) :- *length(L,4), L= [student(_/*teo*/, sandwich,_,_),_,_,_], member(student(_/*mira*/, pite, _/*crosswords*/,_),L), *member(student(girl(GirlName),_,_,white),L), *member(student(bruno,_,_,yellow),L), member(student(_,hamburger,_/*writing*/,_),L), L= [_, student(_,pie,_,_)|_/*[_,_]*/], *next(student(_,pie,_,_), student(teo,_,_,_), L), next(_/*student(bruno,_,_,_)*/, student(_,pizza,_,_), L), *next(student(_,_,_,white), student(_,pizza,_,_), L), *member(student(igor,_,reading,_),L), *next(student(_,_,_,blue), student(girl(GirlName),_,_,_), L). next(A, B, Ls) :- append(_, [A,B|_], Ls). next(A, B, Ls) :- append(_, [B,A|_], Ls).
所有的名字都无关紧要,就像他们的爱好一样。真正重要的是食物!
你只有四个位置,但总共有五种食物(三明治、馅饼、肉馅饼、汉堡、比萨)- 只给我其中一种!
这是 Prolog 中的优点:您可以进行此类概括以获得明确的诊断。