Search code examples
visual-prolog

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


I have a family tree like the one below:

Dasarath - Kousalya
         |
       Raam         -        Sita
                    |
                Lava, Kusa

And I have my facts as:

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").

And I have a rule to define parent.

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

And rules for defining father and mother too.

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

Now comes the problem when I define rule for brother as

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

and I execute it using the query

brother(BrotherName, PersonName)

which produces the result as

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

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

2 Solutions

I have been asked to bring only one result for this query. I am unable to search for right question. If there is a solution, please provide one or give me the reason why Prolog works like this.


Solution

  • You can omit one of the results by comparing their names:

    Two person are bothers if they are both males and have the same father and the first one's name is greater than the second one's name:

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