Title: Removing duplicates from Prolog query results (Visual Prolog using Pie)
Tags: visual-prolog
Question: 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.

Here is the accepted answer: 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 &gt; PersonName.
Comment for this answer: Thanks abbaasi... You nailed it :)


{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 1, 'CommentCount': 0, 'ContentLicense': 'CC BY-SA 3.0', 'CreationDate': '2015:00:03/28/15', 'FavoriteCount': None, 'PostTypeId': 1, 'Tags': '<visual-prolog>', 'Title': 'Removing duplicates from Prolog query results (Visual Prolog using Pie)', 'ViewCount': 1207, 'id': 29314534, 'user_AboutMe': '<p>Working on Rails...</p>\n', 'user_AccountId': 1649751.0, 'user_DisplayName': 'SriramK89', 'user_Reputation': 428.0, 'user_Views': 26.0, 'user_id': 1520572.0}}