简体   繁体   English

写作 Prolog 事实和规则

[英]Writing Prolog facts and rules

I have a Prolog program containing some facts and rules about a drug called Diclogenta eye drops.我有一个 Prolog 程序,其中包含有关一种名为 Diclogenta 滴眼液的药物的一些事实和规则。 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.我想创建一个程序,用户在其中输入药物名称 diclogenta_eye_drops,该程序会返回该药物的副作用。

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.所以我写了一个规则,允许我输入药物名称,以便它返回“Diclogenta Eye drops”药物不会降低免疫反应、升高眼压和白内障形成。

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.但就在输入药物名称并按回车键后,我的控制台返回 false。 What am I doing wrong?我究竟做错了什么?

read(X) does not read a string, it reads a Prolog term - Prolog source code. read(X)不读取字符串,它读取 Prolog 术语 - Prolog 源代码。

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.没有引号,它是一个 Prolog 原子,而不是一个字符串。 They are similar but not the same.它们相似但不相同。

To read a string you could use SWI Prolog's read_line_to_string:要读取字符串,您可以使用 SWI Prolog 的 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? ".正如其他评论所说,这不足以修复您的代码,但这是我对您问题的回答“在输入药物名称并按回车键后,我的控制台返回 false。我做错了什么? ”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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