【问题标题】:Prolog: facts and rules序言:事实和规则
【发布时间】:2019-05-10 23:08:36
【问题描述】:

我有以下情况,我要做一个谓词,代表一个医生就诊一个病人,但是不起作用。

doctor(adele).
doctor(inez).
doctor(elin).

patient(aurora).
patient(mandy).
patient(roan).

doctor_attends_patient(doc,pat):-doctor(doc),patient(pat).

查询时返回false。

doctor_attends_patient(adele,roan).
false

【问题讨论】:

  • 变量在 Prolog 中必须大写。这是第 1 页类型的东西,请重新阅读您的介绍材料。

标签: list prolog rules fact


【解决方案1】:

作为was said before谓词可以读作

to_prove(This) :- need_to_prove(This) , and_also(That).

所以

doctor_attends_patient(doc, pat) :- doctor(doc), patient(pat).

表示证明

doctor_attends_patient(doc, pat)

Prolog 需要证明

                                    doctor(doc), patient(pat).

这两个子目标都是基础,不包含任何逻辑Variables,因此要证明它们必须匹配一些事实 在我们的知识库中。但我们只有

doctor(adele).
doctor(inez).
doctor(elin).

patient(aurora).
patient(mandy).
patient(roan).

我们没有doctor(doc)patient(pat) 作为陈述的事实。

doctor(Doc) 另一方面,匹配 any 所述的doctor/1 事实,因为Doc 是一个能够假定任何值的逻辑变量。

由于Daniel Lyons 指向in the comments,Prolog 中的变量名大写

您现在可以修复谓词定义。


关于您的查询,

doctor_attends_patient(adele,roan)

也不匹配您知识库中的任何事实或规则头。您拥有的唯一具有远程匹配头的规则是

doctor_attends_patient(doc, pat) :- doctor(doc), patient(pat).

但是复合词

doctor_attends_patient( adele, roan)

匹配复合词

doctor_attends_patient( doc  , pat )

因为尽管doctor_attends_patient 这两个术语的函子 确实匹配,并且它们的arities2 也匹配,但没有一个括号内的 arguments 匹配。特别是,

    adele = doc

失败了,也失败了

    roan = pat

如果尝试也会失败。

但如果你使用的是变量,那么

    adele = Doc , roan = Pat

会改为成功,导致替换Doc = adele , Pat = roan。由于规则的头部也会匹配,作为一个整体,

doctor_attends_patient(adele,roan)
doctor_attends_patient( Doc , Pat)

规则的主体将被输入,Prolog 将尝试证明生成的子目标将其中的任何变量替换为成功替换时的值。

您现在可以修复谓词定义。

【讨论】:

    猜你喜欢
    • 2022-10-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-09-22
    • 1970-01-01
    相关资源
    最近更新 更多