Joey Deguzman
Joey Deguzman

Reputation: 113

How do I run a program like this in GNU-Prolog?

Every time I would try to compile it, the compilation would always seem to fail. I am very new to the language and have discovered this program while looking for examples of programs that use Prolog but I am at a loss on how to run it. The reason for using this program fragment as an example is that I would like to make a program of my own that will be able to let the user know what sickness they have based on the symptoms that the user entered.

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

the program would detect an error at very first lines and I'm not sure why.

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.

Upvotes: 0

Views: 253

Answers (1)

Paulo Moura
Paulo Moura

Reputation: 18663

Your code seems to be TurboProlog or Visual Prolog code. Start by deleting the code starting with domains up to clauses. You will also need to replace the calls to readchar/1 and readln/1 predicates with calls to standard Prolog predicates such as read/1 or read_term/3. In the particular case, of readchar/1, and only for running under GNU Prolog, you can define it as:

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

Some other Prolog systems provide a readchar functionality but there's no standard. The main difference of these predicates compared with the standard get_char/1 predicate is not requiring a return/enter when used at the top-level.

Also, replace all calls to write with arity greater then 1 with a sequence of calls to the standard write/1 predicate and replace in those calls the double quotes with single quotes.

Upvotes: 2

Related Questions