CSC529 Spring 2003 - Homework assignment 2 (HW2)  -  (updated 2/11/03)
Total Points: 5
Due date: see course web page
General Instructions:   All solutions should be implemented in Prolog for this assignment and you should test them to verify their correctness. Turn in a paper listing of your solution in Prolog and a paper listing (such as screen shots) demonstrating that each problem solution works on a variety of test cases; staple all papers together and write your name on the first page. Your Prolog code should be formatted, documented, and indented so that it is readable. Also be prepared to send me the file containing your solution in case I wish to perform further testing on it to verify its correctness. (Do NOT send the file unless asked.) You should work alone on this assignment. In other words, do not get or give help and do not look at nor employ solutions that anyone else in the universe has ever done before!


1. (3 points) Modify the program that you wrote for (3) of HW 1 so that the KB does not initially contain facts about specific patients such as their symptoms or if they were in an accident. Instead, the modified program should allow a user to enter information about one patient through a dialogue and then the system will write its diagnosis (flu, cold, pneumonia, or broken ribs) and prescription (aspirin, etc.) for that patient.  (Hint: The program will use Prolog's assert predicate to add the information given by the user to the KB.) The program should write out the full diagnosis and full prescription without additional action by the user (such as having the user type ";" after each answer).  Here is a sample dialogue (the system's output is shown in italics):

Welcome to the free medical advice (MAD) program.
Enter name of patient:
sneezy
Enter symptom (or hit return if no more symptoms):
fever
Enter symptom (or hit return if no more symptoms):
chest pain
Enter symptom (or hit return if no more symptoms):
Was patient in accident? (yes or no):
no
Patient: sneezy
Diagnosis: pneumonia
Prescribe: aspirin, antibiotic
Thank you for consulting MAD.

Final note: here is the only medical knowledge that should be encoded in your KB: Prescribe aspirin for fever, antibiotic for pneumonia, soup for flu or a cold, and pain killer for broken ribs. A patient with a fever and aches has flu. A patient with a fever and runny nose has a cold. A patient with chest pain and fever has pneumonia. A patient with chest pain and who was in an accident has broken ribs.

Additional information (added 2/11/03):

i. If your program gets an error message something like "No permission to modify static procedure" because the program tried to assert a predicate foobar, you need to include a directive at the top of your program declaring the predicate to be a dynamic predicate.  For example, if your predicate is called 'symptom' and it takes 2 arguments, you would need the following directive:

:- dynamic symptom/2.

ii. Instead of having the user hit return to end entering symptoms, you can have the user enter a special symbol.
iii. You may require the user to start the program by entering a special query, e.g.
    ?- start.
    Welcome to the ...
iv. If your program needs to handle multiple word input such as 'chest pain', you may require the user to enclose the input in single quotes.  Also, you may require the user to enter a period to end the input.


2.  Define a Prolog predicate called memberall(X, L) that succeeds if and only if X is contained at some level of nesting in list L. For example, your procedure should return the following answers:
?- memberall( b, [a,b,c]).
yes
?- memberall( [b], [a,b,c]).
no
?- memberall( c, [a, b, [c]).
yes.
?- memberall( b, [a, [[b], c]).
yes.
?- memberall( [b], [a, [[b], c]).
yes.
?- memberall( [a], [a, [[b], c]).
no.



3. Define a Prolog predicate called increasing(L), where L is a list of integers, that succeeds if and only if the elements of L are in strictly increasing order. (By definition, if L is empty then this predicate succeeds.)  For example:
?- increasing( [1, 3, 8]).
yes
?- increasing( [ ]).
yes
?- increasing( [ 1, 3, 3]).
no.
?- increasing( [ 1, 8, 3] ).
no.