假设您有一个疾病诊断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.
嗯,可能有一个更简单的方法来做到这一点,因为我的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
,它使用has_disease/2
作为检查,对它能找到的任何疾病执行另一个findall
。最后使用setof/3
调用来获得疾病列表上的唯一结果,并为方便而订购。
NB。has_disease/2
原语上的atom/1
只是为了确保变量没有传递给Disease
,因为在这种情况下它不起作用,至少对我来说不起作用。
您将需要一种查询所有症状的方法,例如使用findall(S,cause_of(S,disease),SS)
,其中SS
将是该疾病的症状列表。