错误:超出本地堆栈

时间:2014-01-29 18:47:43

标签: prolog

Hi Every Body我在尝试运行此序言时出现此问题“ERROR:Out of local stack”

这是代码

:- redefine_system_predicate(write(_)). :- redefine_system_predicate(readln(_)).

write(S) :- is_list(S) -> format('~s', [S]) ; format('~w', [S]). write(A,B,C,D) :- maplist(write, [A,B,C,D]). write(A,B,C) :- maplist(write, [A,B,C]). readchar(S) :- get(C), atom_codes(S, [C]). readln(A) :- system:readln(L), atomic_list_concat(L,' ',A).

go :- write("What is the patient's name? "), readln(Patient), hypothesis(Patient,Disease), write(Patient,"probably has ",Disease,"."),nl.

go :- write("Sorry, I don't seem to be able to"),nl, write("diagnose the disease."),nl.

symptom(Patient,fever) :- write("Does ",Patient," have a fever (y/n) ?"), response(Reply), Reply='y'.

symptom(Patient,rash) :- write("Does ",Patient," have a rash (y/n) ?"), response(Reply), Reply='y'.

symptom(Patient,headache) :- write("Does ",Patient," have a headache (y/n) ?"), response(Reply), Reply='y'.

symptom(Patient,runny_nose) :- write("Does ",Patient," have a runny_nose (y/n) ?"), response(Reply), Reply='y'.

symptom(Patient,conjunctivitis) :- write("Does ",Patient," have a conjunctivitis (y/n) ?"), response(Reply), Reply='y'.

symptom(Patient,cough) :- write("Does ",Patient," have a cough (y/n) ?"), response(Reply), Reply='y'.

symptom(Patient,body_ache) :- write("Does ",Patient," have a body_ache (y/n) ?"), response(Reply), Reply='y'.

symptom(Patient,chills) :- write("Does ",Patient," have a chills (y/n) ?"), response(Reply), Reply='y'.

symptom(Patient,sore_throat) :- write("Does ",Patient," have a sore_throat (y/n) ?"), response(Reply), Reply='y'.

symptom(Patient,sneezing) :- write("Does ",Patient," have a sneezing (y/n) ?"), response(Reply), Reply='y'.

symptom(Patient,swollen_glands) :- write("Does ",Patient," have a swollen_glands (y/n) ?"), response(Reply), Reply='y'.

hypothesis(Patient,measles) :- symptom(Patient,fever), symptom(Patient,cough), symptom(Patient,conjunctivitis), symptom(Patient,runny_nose), symptom(Patient,rash).

hypothesis(Patient,german_measles) :- symptom(Patient,fever), symptom(Patient,headache), symptom(Patient,runny_nose), symptom(Patient,rash).

hypothesis(Patient,flu) :- symptom(Patient,fever), symptom(Patient,headache), symptom(Patient,body_ache), symptom(Patient,conjunctivitis), symptom(Patient,chills), symptom(Patient,sore_throat), symptom(Patient,runny_nose), symptom(Patient,cough).

hypothesis(Patient,common_cold) :- symptom(Patient,headache), symptom(Patient,sneezing), symptom(Patient,sore_throat), symptom(Patient,runny_nose), symptom(Patient,chills).

hypothesis(Patient,mumps) :- symptom(Patient,fever), symptom(Patient,swollen_glands).

hypothesis(Patient,chicken_pox) :- symptom(Patient,fever), symptom(Patient,chills), symptom(Patient,body_ache), symptom(Patient,rash).

hypothesis(Patient,measles) :- symptom(Patient,cough), symptom(Patient,sneezing), symptom(Patient,runny_nose).

response(Reply) :- readchar(Reply), write(Reply),nl.

有什么建议吗?

3 个答案:

答案 0 :(得分:2)

你有一个循环:

readln(A) :- system:readln(L), atomic_list_concat(L,' ',A).

测试

1 ?- [user].
|: :- redefine_system_predicate(readln(_)).
|: readln(A) :- system:readln(L), atomic_list_concat(L,' ',A).
% user://1 compiled 0.03 sec, 2 clauses
true.

2 ?- readln(X).
ERROR: Out of local stack
3 ?- 

另一个问题,但没有导致stackoverflow ... 如果您使用最近的SWI-Prolog(版本> = 7.0),请注意您对字符串'type'的测试:双引号文字不再是列表。

?- is_list("123").
false.

答案 1 :(得分:1)

“超出本地堆栈”意味着由于以下某些原因,您没有剩余内存来计算结果:

  • 通常在某处有无限循环。在递归循环中缺少基本案例。
  • 你工作的名单太大了。
  • 有很多回溯,翻译无法处理。

我认为第一个适用于readln(A) :- system:readln(L), atomic_list_concat(L,' ',A).

无论如何,如果您需要更多内存,可以使用SWI-Prolog中的set_prolog_stack/2扩展堆栈。

答案 2 :(得分:0)

我通过使用新版本的SWI-Prolog解决问题并删除write和readln语句,代码如下所示。

:- redefine_system_predicate(write(_)). :- redefine_system_predicate(readln(_)).

write(A,B,C,D) :- maplist(write, [A,B,C,D]). write(A,B,C) :- maplist(write, [A,B,C]). readchar(S) :- get(C), atom_codes(S, [C]).

go :- write("What is the patient\'s name? "), readln(Patient), hypothesis(Patient,Disease), write(Patient,"probably has ",Disease,"."),nl.

go :- write("Sorry, I don't seem to be able to"),nl, write("diagnose the disease."),nl.

symptom(Patient,fever) :- write("Does ",Patient," have a fever (y/n) ?"), response(Reply), Reply='y'.

symptom(Patient,rash) :- write("Does ",Patient," have a rash (y/n) ?"), response(Reply), Reply='y'.

symptom(Patient,headache) :- write("Does ",Patient," have a headache (y/n) ?"), response(Reply), Reply='y'.

symptom(Patient,runny_nose) :- write("Does ",Patient," have a runny_nose (y/n) ?"), response(Reply), Reply='y'.

symptom(Patient,conjunctivitis) :- write("Does ",Patient," have a conjunctivitis (y/n) ?"), response(Reply), Reply='y'.

symptom(Patient,cough) :- write("Does ",Patient," have a cough (y/n) ?"), response(Reply), Reply='y'.

symptom(Patient,body_ache) :- write("Does ",Patient," have a body_ache (y/n) ?"), response(Reply), Reply='y'.

symptom(Patient,chills) :- write("Does ",Patient," have a chills (y/n) ?"), response(Reply), Reply='y'.

symptom(Patient,sore_throat) :- write("Does ",Patient," have a sore_throat (y/n) ?"), response(Reply), Reply='y'.

symptom(Patient,sneezing) :- write("Does ",Patient," have a sneezing (y/n) ?"), response(Reply), Reply='y'.

symptom(Patient,swollen_glands) :- write("Does ",Patient," have a swollen_glands (y/n) ?"), response(Reply), Reply='y'.

hypothesis(Patient,measles) :- symptom(Patient,fever), symptom(Patient,cough), symptom(Patient,conjunctivitis), symptom(Patient,runny_nose), symptom(Patient,rash).

hypothesis(Patient,german_measles) :- symptom(Patient,fever), symptom(Patient,headache), symptom(Patient,runny_nose), symptom(Patient,rash).

hypothesis(Patient,flu) :- symptom(Patient,fever), symptom(Patient,headache), symptom(Patient,body_ache), symptom(Patient,conjunctivitis), symptom(Patient,chills), symptom(Patient,sore_throat), symptom(Patient,runny_nose), symptom(Patient,cough).

hypothesis(Patient,common_cold) :- symptom(Patient,headache), symptom(Patient,sneezing), symptom(Patient,sore_throat), symptom(Patient,runny_nose), symptom(Patient,chills).

hypothesis(Patient,mumps) :- symptom(Patient,fever), symptom(Patient,swollen_glands).

hypothesis(Patient,chicken_pox) :- symptom(Patient,fever), symptom(Patient,chills), symptom(Patient,body_ache), symptom(Patient,rash).

hypothesis(Patient,measles) :- symptom(Patient,cough), symptom(Patient,sneezing), symptom(Patient,runny_nose).

response(Reply) :- readchar(Reply), write(Reply),nl.