如何在Prolog中应用全称量词?

5
假设您有一个疾病诊断的Prolog程序,该程序从许多疾病和症状之间的关系开始:
causes_of(symptom1, Disease) :-
    Disease = disease1;
    Disease = disease2.
causes_of(symptom2, Disease) :-
    Disease = disease2;
    Disease = disease3.
causes_of(symptom3, Disease) :-
    Disease = disease4.

has_symptom(person1, symptom1).
has_symptom(person1, symptom2).

我该如何创建一个名为“has_disease(Person, Disease)”的规则,以便在人患有该疾病的所有症状时返回true?使用上面的示例,下面是一种可能的输出结果:
has_disease(person1, Disease).
   Disease = disease2.
2个回答

6

嗯,可能有更简单的方法来完成这个任务,因为我的Prolog技能最多只能算得上初级水平。

has_disease(Person, Disease) :- atom(Disease),
    findall(Symptom, has_symptom(Person, Symptom), PersonSymptoms),
    findall(DSymptom, causes_of(DSymptom, Disease), DiseaseSymptoms),
    subset(DiseaseSymptoms, PersonSymptoms).

has_diseases(Person, Diseases) :-
    findall(Disease, (causes_of(_, Disease), has_disease(Person, Disease)), DiseaseList),
    setof(Disease, member(Disease, DiseaseList), Diseases).

被称为如下:
?- has_diseases(person1, D).
D = [disease1, disease2, disease3].

findall/3谓词首先用于查找一个人拥有的所有症状,然后再次用于查找一种疾病拥有的所有症状,最后快速检查疾病的症状是否是该人的子集。

我编写has_disease/2谓词的方式防止其提供疾病列表。因此,我创建了has_diseases/2,它对任何可以找到的疾病执行另一个findall,并使用has_disease/2作为检查。最后使用setof/3调用来获取疾病列表上唯一的结果并按顺序排列以方便使用。

NB. has_disease/2原语中的atom/1仅是为了确保不会传递变量作为Disease,因为在这种情况下它不起作用,至少对我来说是这样。


1
您需要一种查询所有症状的方法,例如使用 findall(S,cause_of(S,disease),SS),其中SS将是该疾病的症状列表。

1
你能举个例子说明我该如何做吗? - Thiago Padilha

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接