如何在GNU-Prolog中运行这样的程序?

时间:2018-12-07 14:04:41

标签: prolog

每次我尝试编译它时,编译似乎总是失败。我是该语言的新手,在寻找使用Prolog的程序示例时发现了该程序,但是我对如何运行它感到迷惑。使用该程序片段作为示例的原因是,我想制作一个自己的程序,该程序能够根据用户输入的症状让用户知道他们患了什么病。

domains
disease,indication = symbol.
Patient,name = string.
predicates
hypothesis(string,disease).
symptom(name,indication).
response(char).
go.
clauses

程序会在第一行检测到错误,但我不确定为什么。

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.

1 个答案:

答案 0 :(得分:2)

您的代码似乎是TurboProlog或Visual Prolog代码。首先删除从domainsclauses的代码。您还需要用对标准Prolog谓词(例如readchar/1readln/1的调用来替换对read/1read_term/3谓词的调用。在特定情况下,readchar/1仅在GNU Prolog下运行时,可以将其定义为:

    readchar(Char) :-
        get_key(Code), char_code(Char, Code), nl.

其他一些Prolog系统提供了readchar功能,但没有标准。这些谓词与标准get_char/1谓词的主要区别在于,在顶层使用谓词时不需要返回/输入。

另外,将对write的所有大于等于1的调用替换为对标准write/1谓词的一系列调用,并在这些调用中将双引号替换为单引号。