Skip to main content

Implementing Diagnosis Problem in Prolog

In this post, we implement a medical diagnosis expert system in Prolog. The program asks the user a series of yes/no questions about a patient’s symptoms and attempts to diagnose a disease based on the answers. This is a classic demonstration of how Prolog’s pattern matching and backtracking can model the reasoning process of a rule-based expert system.

How Does the Diagnosis Work?

The Prolog program defines a hypothesis predicate that maps a combination of symptoms to a disease. A symptom predicate interactively queries the user for each symptom. If all required symptoms are confirmed, Prolog unifies the disease variable and reports the diagnosis. If no hypothesis can be satisfied, a fallback clause prints a failure message.

In this example, the only disease defined is common cold, which requires the patient to have both a headache and sneezing. You can easily extend this to cover more diseases by adding more hypothesis and symptom clauses.


Complete Code

% ============================================================
% Diagnosis Problem in Prolog
% An interactive expert system that diagnoses disease based
% on symptom queries answered by the user (y/n).
% ============================================================

predicates
  hypothesis(string, string)  % hypothesis(PatientName, Disease)
  symptom(string, string)     % symptom(PatientName, SymptomName)
  response(char)              % reads a single character response
  go                          % entry point

clauses

  % Entry point: ask for patient name, attempt diagnosis
  go :-
    write("What is the patient's name?"),
    readln(Patient),
    hypothesis(Patient, Disease),
    write(Patient, " probably has ", Disease, "."), nl.

  % Fallback: if no hypothesis was satisfied
  go :-
    write("Sorry, diagnosis not possible."), nl.

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

  % Symptom: sneezing
  symptom(Patient, sneezing) :-
    write("Is ", Patient, " sneezing?(y/n)"),
    response(Reply),
    Reply = 'y'.

  % Hypothesis: common cold requires headache AND sneezing
  hypothesis(Patient, commoncold) :-
    symptom(Patient, headache),
    symptom(Patient, sneezing).

  % response/1: reads a character and echoes it
  response(Reply) :-
    readchar(Reply),
    write(Reply), nl.

Explanation of the Code

  1. go/0 — Entry Point — The top-level predicate. It reads the patient’s name using readln/1, then calls hypothesis/2 to attempt a diagnosis. If a matching hypothesis is found, the disease is printed. If no hypothesis succeeds, Prolog backtracks to the second go clause and prints a failure message.
  2. symptom/2 — Interactive Query — Each symptom clause writes a yes/no question to the screen, reads a single character using readchar/1, and succeeds only if the user types y. If the user types anything else, the clause fails, causing Prolog to abandon the current hypothesis branch.
  3. hypothesis/2 — Disease Rule — Defines which combination of symptoms leads to a diagnosis. hypothesis(Patient, commoncold) succeeds only if both symptom(Patient, headache) and symptom(Patient, sneezing) succeed. You can add more hypotheses for different diseases (e.g., flu, allergy) simply by adding new clauses.
  4. response/1 — Character Reader — A helper predicate that reads one character from stdin and echoes it back to the screen. Using readchar (rather than readln) means the program responds immediately without requiring the user to press Enter after each answer.

Sample Run 1 — Successful Diagnosis

Goal: go
  What is the patient's name? abc
  Does abc have headache?(y/n) y
  Is abc sneezing?(y/n) y
  abc probably has commoncold.

Explanation: The user enters patient name abc. Prolog attempts hypothesis(abc, commoncold), which requires both symptoms to be confirmed. The user answers y to both questions, so both symptom clauses succeed. Prolog unifies Disease = commoncold and prints the diagnosis.

Sample Run 2 — Diagnosis Not Possible

Goal: go
  What is the patient's name? pqr
  Does pqr have headache?(y/n) n
  Sorry, diagnosis not possible.

Explanation: The user answers n to the headache question. The symptom(pqr, headache) clause fails immediately. Since hypothesis(pqr, commoncold) requires headache as its first sub-goal, the entire hypothesis fails. No other hypothesis clauses exist, so Prolog backtracks to the second go clause and prints the fallback message.


See Also

Conclusion

This diagnosis program is a minimal but complete expert system skeleton. Prolog’s built-in backtracking does the heavy lifting: you simply define disease rules as logical clauses, and Prolog automatically tries each hypothesis in order, abandoning branches where symptoms are denied. To extend this system, add more symptom clauses for new symptoms (e.g., fever, cough) and more hypothesis clauses for new diseases (e.g., flu requires headache, fever, and cough). Prolog will evaluate all hypotheses automatically. For a more complex interactive Prolog example, see the Monkey-Banana Problem, which demonstrates multi-step goal chaining.

No Comments yet!

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.