简体   繁体   中英

Writing Prolog facts and rules

I have a Prolog program containing some facts and rules about a drug called Diclogenta eye drops. I wanted to create a program where the user enters the drug name diclogenta_eye_drops and the program returns the side effects of the drug.

So I wrote a rule that allows me to enter the drug name so that it returns 'Diclogenta Eye drops' drug does not reduce immune response, raise intra-ocular pressure and cataract formation.

anti_inflammatory(diclofenac_sodium).
analgesic(diclofenac_sodium).
diclogenta_eye_drops(diclofenac_sodium).
    
diclofenac_sodium(X) :- 
    diclofenac_sodium(diclogenta_eye_drops), 
    does_Not(X,increase_intra_ocular_pressure),
    does_Not(X, cause_cataract_formation),
    does_Not(reduce_immune_response).
    
medicine :-
    write("Enter drug name. Use _ (underscore) instead of space bar."),
    read(X),
    X = "diclogenta_eye_drops",
    diclofenac_sodium(X).

But just after inputting the name of the drug and pressing enter, my console returns false. What am I doing wrong?

read(X) does not read a string, it reads a Prolog term - Prolog source code.

This part:

read(X),
X = "diclogenta_eye_drops"

will only succeed if you enter that string like the source code - in double quotes; try in a toplevel without:

?- "diclogenta_eye_drops" = diclogenta_eye_drops.
false

Without quotes it is a Prolog atom, instead of a string. They are similar but not the same.

To read a string you could use SWI Prolog's read_line_to_string:

?- read_line_to_string(user_input, X), X = "diclogenta_eye_drops".

|: diclogenta_eye_drops     <-- what I typed in, Enter, no dot at the end

X = "diclogenta_eye_drops".     <-- success, no instant "false"

That is not enough to fix your code, as the other comments say, but it is my answer to your question " just after inputting the name of the drug and pressing enter, my console returns false. What am I doing wrong? ".

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM