从 Prolog 查询结果中删除重复项(使用 Pie 的 Visual Prolog)

Removing duplicates from Prolog query results (Visual Prolog using Pie)

我的家谱如下:

Dasarath - Kousalya
         |
       Raam         -        Sita
                    |
                Lava, Kusa

我的事实如下:

male("Dasarath").
male("Raam").
male("Lava").
male("Kusa").

female("Kousalya").
female("Sita").

child("Raam", "Dasarath").
child("Raam", "Kousalya").
child("Lava", "Raam").
child("Lava", "Sita").
child("Kusa", "Raam").
child("Kusa", "Sita").

我有一个定义父级的规则。

parent(ParentName, ChildName) :- child(ChildName, ParentName).

还有定义父亲和母亲的规则。

father(FatherName, ChildName) :- parent(FatherName, ChildName), male(FatherName).
mother(MotherName, ChildName) :- parent(MotherName, ChildName), female(MotherName).

当我为兄弟定义规则时,问题来了

brother(BrotherName, PersonName) :- father(ParentName, BrotherName), father(ParentName, PersonName),
                                      male(BrotherName), not BrotherName = PersonName.

然后我使用查询

执行它
brother(BrotherName, PersonName)

产生的结果为

brother(BrotherName, PersonName)
BROTHERNAME = "Lava".
PERSONNAME = "Kusa".

BROTHERNAME = "Kusa".
PERSONNAME = "Lava".

2 Solutions

我被要求只为这个查询提供一个结果。我无法搜索正确的问题。如果有解决方案,请提供一个或告诉我为什么 Prolog 会这样工作。

您可以通过比较它们的名称来省略其中一个结果:

如果两个人都是男性并且有同一个父亲并且第一个的名字大于第二个的名字:

brother(BrotherName, PersonName) :- father(ParentName, BrotherName), father(ParentName, PersonName),
                                      male(BrotherName), BrotherName > PersonName.