尝试用prolog解决拼图任务,遇到了一些问题。
1002 Stack Overflow. Re-configure with Setup if necessary.
所以,我试图在设置和运行程序中再次增加堆栈大小。但它会导致另一个错误:Syntax error on line...
错误行是谓词中带有操作符"not"的行。
domains
age = integer
childname,ffood,fear = string
child = child(childname,age,ffood,fear)
children = child*
predicates
solve
name(child,childname)
fear(child,fear)
age(child,age)
ffood(child,ffood)
keys(children)
solution(children)
elder(child)
member(children,child)
structure(children)
clauses
member([X|_],X).
member([_|SP],X):-member(SP,X).
name(child(A,_,_,_),A).
age(child(_,A,_,_),A).
ffood(child(_,_,A,_),A).
fear(child(_,_,_,A),A).
structure([child("Dima",_,_,_),child("Kate",_,_,_),child("Misha",_,_,_),child("Sveta",_,_,_),child("Ura",_,_,_)]).
elder(child(_,A,_,_)):-A=7;A=8.
solve:-structure(Children),keys(Children),solution(Children).
keys(Struct):-
member(Struct,child(_,4,_,_)),
member(Struct,child(_,5,_,_)),
member(Struct,child(_,6,_,_),
member(Struct,child(_,7,_,_),
member(Struct,child(_,8,_,_)),
member(Struct,child(_,_,"Banana",_),
member(Struct,child(_,_,"Icecream",_),
member(Struct,child(_,_,"Pizza",_),
member(Struct,child(_,_,"Pasta",_),
member(Struct,child(_,_,"Chocolate",_),
member(Struct,child(_,_,_,"Thunderstorm"),
member(Struct,child(_,_,_,"Spiders"),
member(Struct,child(_,_,_,"Ghosts"),
member(Struct,child(_,_,_,"Dogs"),
member(Struct,child(_,_,_,"Darkness"),
member(Struct,Child1),
name(Child1,"Kate"),
elder(Child1),
not(fear(Child1,"Darkness")),
not(ffood(Child1,"Chocolate")),
member(Struct,Child2),
name(Child2,"Sveta"),
elder(Child2),
not(fear(Child2,"Darkness")),
not(ffood(Child2,"Chocolate")),
ffood(Child2,"Pizza"),
not(fear(Child2,"Spiders")),
member(Struct,Child3),
age(Child3,5),
fear(Child3,"Ghosts"),
member(Struct,Child4),
age(Child4,6),
fear(Child4,"Thunderstorm"),
not(ffood(Child4,"Chocolate")),
not(ffood(Child4,"Pasta")),
member(Struct,Child5),
age(Child5,4),
ffood(Child5,"Banana"),
member(Struct,Child6),
age(Child6,8),
not(fear(Child6,"Dogs")),
member(Struct,Child7),
name(Child7,"Dima"),
not(age(Child7,5)),
not(fear(Child7,"Darkness")),
not(fear(Child7,"Spiders")),
not(ffood(Child7,"Banana")),
member(Struct,Child8),
name(Child8,"Misha"),
not(fear(Child8,"Darkness")),
not(fear(Child8,"Spiders")),
not(ffood(Child8,"Banana")).
solution (Children):-
write ("Solve:"), write (Children).
goal
solve.
发现这个开场白的行为很奇怪…也许有人也有同样的问题?
我尝试用SWI-Prolog重新格式化你的代码:
keys(Struct):-
member(Struct,child(_,4,_,_)),
member(Struct,child(_,5,_,_)),
member(Struct,child(_,6,_,_),
member(Struct,child(_,7,_,_),
member(Struct,child(_,8,_,_)),
member(Struct,child(_,_,"Banana",_),
...
似乎你少了一些括号…在明显的更正之后,我得到
?- solve.
Solve:[child(Dima,6,Icecream,Thunderstorm),child(Kate,8,Pasta,Spiders),child(Misha,5,Chocolate,Ghosts),child(Sveta,7,Pizza,Dogs),child(Ura,4,Banana,Darkness)]
true .