# How can remove the duplicate search result

**URL:** <https://swi-prolog.discourse.group/t/how-can-remove-the-duplicate-search-result/497>\
**Category:** Help!\
**Created:** [April 6, 2019, 3:00am UTC](https://swi-prolog.discourse.group/t/how-can-remove-the-duplicate-search-result/497 "2019-04-06T03:00:54Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![CHN-STUDENT](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/chn-student/32/166_2.png) [@CHN-STUDENT](https://swi-prolog.discourse.group/u/CHN-STUDENT)\
**Post date:** [April 6, 2019, 3:00am UTC](https://swi-prolog.discourse.group/t/how-can-remove-the-duplicate-search-result/497/1 "2019-04-06T03:00:54Z")

</div>

Hi, I want to use prolog to make One Hundred Years of Solitude 's family tree, and when i try to search result, I find there are the duplicate part? So could you tell me how to fix it? Or could you give me some advice to let it better? Thanks!

My code is here:

```prolog
% One Hundred Years of Solitude 's family tree in Prolog
% Wiki picture: https://en.wikipedia.org/wiki/One_Hundred_Years_of_Solitude#/media/File:One_Hundred_Years_Of_Solitude_Buendia%27s_Family_Tree.svg

% See here:https://codereview.stackexchange.com/questions/143116/family-tree-in-prolog

% predicate(X,Y) means "X is Y 's XXX ."
% eg. father(X,Y) means "X is Y 's father."

% Generation I
man('José Arcadio Buendía').
woman('Úrsula Iguarán').

marry('José Arcadio Buendía','Úrsula Iguarán').

% Generation II
man('José Arcadio').
man('Colonel Aureliano Buendía').
woman('Amaranta').
woman('Remedios Moscote').
woman('Rebeca').
woman('Pilar Ternera').

parent('José Arcadio Buendía','José Arcadio').
parent('José Arcadio Buendía','Colonel Aureliano Buendía').
parent('José Arcadio Buendía','Amaranta').
parent('Úrsula Iguarán','José Arcadio').
parent('Úrsula Iguarán','Colonel Aureliano Buendía').
parent('Úrsula Iguarán','Amaranta').
marry('Colonel Aureliano Buendía','Remedios Moscote').
marry('Rebeca','José Arcadio').
amorousAffairsy('Pilar Ternera','José Arcadio').
amorousAffairsy('Pilar Ternera','Colonel Aureliano Buendía').

% Generation III
woman('Aureliano José').
woman('Arcadio').
man('Santa Sofía de la Piedad').

marry('Santa Sofía de la Piedad','Arcadio').
illegitimateChild('17 sons by unknown women','Colonel Aureliano Buendía').
parent('Pilar Ternera','Arcadio').
parent('José Arcadio Buendía','Arcadio').
% Generation IV
woman('Remedios the Beauty').
man('José Arcadio II').
man('Aureliano II').
woman('Petra Cotes').
woman('Fernanda del Carpio').

marry('Aureliano II','Fernanda del Carpio').
amorousAffairsy('Petra Cotes','Aureliano II').
parent('Santa Sofía de la Piedad','Remedios the Beauty').
parent('Santa Sofía de la Piedad','José Arcadio II').
parent('Santa Sofía de la Piedad','Aureliano II').
parent('Arcadio','Remedios the Beauty').
parent('Arcadio','José Arcadio II').
parent('Arcadio','Aureliano II').
% Generation V
man('Gastón').
man('José Arcadio').
man('Mauricio Babilonia').
woman('Amaranta Úrsula').
woman('Renata Remedios').

marry('Amaranta Úrsula','Gastón').
amorousAffairsy('Renata Remedios','Mauricio Babilonia').
parent('Aureliano II','Amaranta Úrsula').
parent('Aureliano II','José Arcadio').
parent('Aureliano II','Renata Remedios').
parent('Fernanda del Carpio','Amaranta Úrsula').
parent('Fernanda del Carpio','José Arcadio').
parent('Fernanda del Carpio','Renata Remedios').
% Generation VI
man('Aureliano Babilonia').
parent('Mauricio Babilonia','Aureliano Babilonia').
parent('Renata Remedios','Aureliano Babilonia').

amorousAffairsy('Amaranta Úrsula','Aureliano Babilonia').
% Generation VII
man('Aureliano').
parent('Aureliano Babilonia','Aureliano').
parent('Amaranta Úrsula','Aureliano').

% -------- Start defining the relationships -------- 

:- discontiguous man/1, woman/1, marry/2,amorousAffairsy/2.

father(X,Y) :- parent(X,Y), man(X), X\=Y.
mother(X,Y) :- parent(X,Y), woman(X), X\=Y.
%parent(X,Y) :- father(X,Y); mother(X,Y).
parents(X,Y,Z) :- (father(X,Z), mother(Y,Z)); (father(Y,Z), mother(X,Z)), X\=Y, X\=Z,Z\=Y.
marry(X,Y) :- marry(X,Y), marry(Y,X), X\=Y.
amorousAffairsy(X,Y) :- amorousAffairsy(X,Y), amorousAffairsy(Y,X), X\=Y.
child(X,Y) :- father(Y,X); mother(Y,X),X\=Y.
illegitimateChild(X,Y) :- illegitimateChild(X,Y), X\=Y.
son(X,Y) :- child(X,Y),man(X),X\=Y.
daughter(X,Y) :- child(X,Y),woman(X),X\=Y.

grandma(X,Y) :- (mother(X,W), mother(W,Y)); (father(X,W), mother(W,Y)),X\=Y.
grandpa(X,Y) :- (father(X,W), father(W,Y)); (mother(X,W), father(W,Y)),X\=Y.
grandparents(X,Y,Z) :- (grandma(X,Z), grandpa(Y,Z)); (grandma(Y,Z), grandpa(X,Z)), X\=Y, X\=Z,Z\=Y.

siblings(A,B) :- father(F,A), father(F,B), mother(M,A), mother(M,B), A\=B.
uncle(U,N) :- man(U), ((siblings(U,M), father(M,N)) ; (siblings(U,F), father(F,N))),U\=N.
aunt(U,N) :- woman(U), ((siblings(U,M), mother(M,N)) ; (siblings(U,F), mother(F,N))),U\=N.

sister(X,Y) :- siblings(X,Y), woman(X), X\=Y.
brother(X,Y) :- siblings(X,Y), man(X), X\=Y.

ancestor(X,Y):-parent(X,Y),X\=Y.
ancestor(X,Y):-parent(X,Z),ancestor(Z,Y).

% -------- End defining the relationships -------- 

```

 ![image](https://global.discourse-cdn.com/free1/uploads/swiprolog/original/1X/c8d5a9e4915f0506fbb7a5f503ba46ebca6e52c4.png)

---

<div class="post-metadata">

**Author:** ![edom](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/edom/32/522_2.png) [@edom](https://swi-prolog.discourse.group/u/edom)\
**Post date:** [April 6, 2019, 10:35am UTC](https://swi-prolog.discourse.group/t/how-can-remove-the-duplicate-search-result/497/2 "2019-04-06T10:35:50Z")

</div>

It seems that the `aunt/2`

```prolog
aunt(U,N) :- woman(U), ((siblings(U,M), mother(M,N)) ; (siblings(U,F), mother(F,N))),U\=N.

```

should have been

```prolog
aunt(U,N) :- woman(U), siblings(U,M), mother(M,N), U\=N.

```

What is the duplicate `(siblings(U,F), mother(F,N))` alternative for?

Try reading the program as a natural language text:

U is an aunt of N if

1. U is a woman,
2. there is an M such that
  1. U is a sibling of M,
  2. M is the mother of N,

3. U is not N.

Compare that to your original program:

U is an aunt of N if

1. U is a woman,
2. at least one of these is true:
  1. there is an M such that
    1. U is a sibling of M,
    2. M is the mother of N;

  2. there is an F such that
    1. U is a sibling of F,
    2. F is the mother of N.
    3. (But what is this for? This is the same as “there is an M …”, only with M renamed to F?)

3. U is not N.

Thus, your actual problem is not about removing duplicate search result, but about how to state the problem in Prolog. 🙂

---

<div class="post-metadata">

**Author:** ![CHN-STUDENT](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/chn-student/32/166_2.png) [@CHN-STUDENT](https://swi-prolog.discourse.group/u/CHN-STUDENT)\
**Post date:** [April 8, 2019, 12:45pm UTC](https://swi-prolog.discourse.group/t/how-can-remove-the-duplicate-search-result/497/3 "2019-04-08T12:45:57Z")

</div>

@edom  
Sorry, i am wrong when i coding, it means aunt of father’s or mother’s . So it is correct code.

```prolog
aunt(U,N) :- woman(U), ((siblings(U,M), father(M,N)); (siblings(U,F), mother(F,N))), U\=N.

```

But i have another quesion? How to descripte mutual relationship? For exmple. XX is YY 's sister.  
YY is XX’s sister too.

I try to edit my code,here it this:

```prolog

% See here:https://codereview.stackexchange.com/questions/143116/family-tree-in-prolog

% predicate(X,Y) means "X is Y 's XXX ."
% eg. father(X,Y) means "X is Y 's father."

:- encoding(utf8).
:- discontiguous man/1, woman/1, marry/2,amorousAffairsy/2,parent/2,father/2,mother/2,child/2,illegitimateChild/2.

% Generation I
man('José Arcadio Buendía').
woman('Úrsula Iguarán').

marry('José Arcadio Buendía','Úrsula Iguarán').

% Generation II
man('José Arcadio').
man('Colonel Aureliano Buendía').
woman('Amaranta').
woman('Remedios Moscote').
woman('Rebeca').
woman('Pilar Ternera').

parent('José Arcadio Buendía','José Arcadio').
parent('José Arcadio Buendía','Colonel Aureliano Buendía').
parent('José Arcadio Buendía','Amaranta').
parent('Úrsula Iguarán','José Arcadio').
parent('Úrsula Iguarán','Colonel Aureliano Buendía').
parent('Úrsula Iguarán','Amaranta').
marry('Colonel Aureliano Buendía','Remedios Moscote').
marry('Rebeca','José Arcadio').
amorousAffairsy('Pilar Ternera','José Arcadio').
amorousAffairsy('Pilar Ternera','Colonel Aureliano Buendía').

% Generation III
woman('Aureliano José').
woman('Arcadio').
man('Santa Sofía de la Piedad').

marry('Santa Sofía de la Piedad','Arcadio').
illegitimateChild('17 sons by unknown women','Colonel Aureliano Buendía').
parent('Pilar Ternera','Arcadio').
parent('José Arcadio Buendía','Arcadio').
% Generation IV
woman('Remedios the Beauty').
man('José Arcadio II').
man('Aureliano II').
woman('Petra Cotes').
woman('Fernanda del Carpio').

marry('Aureliano II','Fernanda del Carpio').
amorousAffairsy('Petra Cotes','Aureliano II').
parent('Santa Sofía de la Piedad','Remedios the Beauty').
parent('Santa Sofía de la Piedad','José Arcadio II').
parent('Santa Sofía de la Piedad','Aureliano II').
parent('Arcadio','Remedios the Beauty').
parent('Arcadio','José Arcadio II').
parent('Arcadio','Aureliano II').
% Generation V
man('Gastón').
man('José Arcadio').
man('Mauricio Babilonia').
woman('Amaranta Úrsula').
woman('Renata Remedios').

marry('Amaranta Úrsula','Gastón').
amorousAffairsy('Renata Remedios','Mauricio Babilonia').
parent('Aureliano II','Amaranta Úrsula').
parent('Aureliano II','José Arcadio').
parent('Aureliano II','Renata Remedios').
parent('Fernanda del Carpio','Amaranta Úrsula').
parent('Fernanda del Carpio','José Arcadio').
parent('Fernanda del Carpio','Renata Remedios').
% Generation VI
man('Aureliano Babilonia').
parent('Mauricio Babilonia','Aureliano Babilonia').
parent('Renata Remedios','Aureliano Babilonia').

amorousAffairsy('Amaranta Úrsula','Aureliano Babilonia').
% Generation VII
man('Aureliano').
parent('Aureliano Babilonia','Aureliano').
parent('Amaranta Úrsula','Aureliano').

% -------- Start defining the relationships -------- 

father(X,Y) :- parent(X,Y), man(X), X\=Y.
mother(X,Y) :- parent(X,Y), woman(X), X\=Y.
%parent(X,Y) :- father(X,Y); mother(X,Y).
parents(X,Y,Z) :- (father(X,Z), mother(Y,Z)); (father(Y,Z), mother(X,Z)), X\=Y, X\=Z, Z\=Y.
couple(X,Y) :- marry(X,Y), X\=Y. 
couple(Y,X) :- marry(X,Y), X\=Y.
amorousAffairsy(X,Y) :- amorousAffairsy(X,Y), amorousAffairsy(Y,X), X\=Y.
child(X,Y) :- father(Y,X); mother(Y,X), X\=Y.
illegitimateChild(X,Y) :- illegitimateChild(X,Y), X\=Y.
son(X,Y) :- child(X,Y), man(X), X\=Y.
daughter(X,Y) :- child(X,Y), woman(X), X\=Y.

grandma(X,Y) :- (mother(X,W), mother(W,Y)); (father(X,W), mother(W,Y)), X\=Y.
grandpa(X,Y) :- (father(X,W), father(W,Y)); (mother(X,W), father(W,Y)), X\=Y.
grandparents(X,Y,Z) :- (grandma(X,Z), grandpa(Y,Z)); (grandma(Y,Z), grandpa(X,Z)), X\=Y, X\=Z, Z\=Y.

siblings(A,B) :- father(F,A), father(F,B), mother(M,A), mother(M,B), A\=B.
%siblings(B,A) :- father(F,A), father(F,B), mother(M,A), mother(M,B), A\=B.
uncle(U,N) :- man(U), ((siblings(U,M), father(M,N)); (siblings(U,F), mother(F,N))), U\=N.
aunt(U,N) :- woman(U), ((siblings(U,M), father(M,N)); (siblings(U,F), mother(F,N))), U\=N.

sister(X,Y) :- siblings(X,Y), woman(X), woman(Y), X\=Y.
%sister(Y,X) :- siblings(X,Y), woman(X), woman(Y), X\=Y.
brother(X,Y) :- siblings(X,Y), man(X), man(Y), X\=Y.
%brother(Y,X) :- siblings(X,Y), man(X), man(Y), X\=Y.

ancestor(X,Y):- parent(X,Y), X\=Y.
ancestor(X,Y):- parent(X,Z), ancestor(Z,Y).

% -------- End defining the relationships -------- 

```

---

<div class="post-metadata">

**Author:** ![Ian](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/ian/32/1026_2.png) [@Ian](https://swi-prolog.discourse.group/u/Ian)\
**Post date:** [April 8, 2019, 1:30pm UTC](https://swi-prolog.discourse.group/t/how-can-remove-the-duplicate-search-result/497/4 "2019-04-08T13:30:29Z")

</div>

Your current definition of sister represents ‘sisters’ since both arguments have to be woman.  
Did you want sister(X,Y), to mean ‘X is a sister of Y’, in which case, Y could be either a man or a woman.

---

<div class="post-metadata">

**Author:** ![CHN-STUDENT](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/chn-student/32/166_2.png) [@CHN-STUDENT](https://swi-prolog.discourse.group/u/CHN-STUDENT)\
**Post date:** [April 8, 2019, 10:30pm UTC](https://swi-prolog.discourse.group/t/how-can-remove-the-duplicate-search-result/497/5 "2019-04-08T22:30:04Z")

</div>

Thanks. But I mean if i try search sisters(X,Y) on the database, I hope X and Y have mutual relationship.Because X is Y’s sister and Y is X’s sister too. The database will introduce these conclusions based on facts, but not is mutual relationship. And i try to write rule to do this like `sister(Y,X):-sister(X,Y)`, This will bring infinite recursion of the query, the StackOverflow suggest me to use another predicate to write rule.But i am not sure,so could you give me some advice? Thanks !

> <https://stackoverflow.com/questions/14639317/how-to-create-a-rule-that-makes-all-relations-symmetric-in-prolog>

---

<div class="post-metadata">

**Author:** ![peter.ludemann](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/peter.ludemann/32/48_2.png) [@peter.ludemann](https://swi-prolog.discourse.group/u/peter.ludemann)\
**Post date:** [April 8, 2019, 11:15pm UTC](https://swi-prolog.discourse.group/t/how-can-remove-the-duplicate-search-result/497/6 "2019-04-08T23:15:40Z")

</div>

A generic way of removing duplicates from a goal is to replace the call to `goal(..., X)` by:

```prolog
setof(X, goal(..., X), Xs), member(X, Xs).

```

Explanation:  
`setof(X, goal(..., X), Xs)` finds all the `X` values that are a solution to `goal(..., X)` and puts them into the list `Xs` (without duplicates); `member(X, Xs)` gets the results one-by-one on backtracking.

Another way of removing duplicates is to use [sort/2](http://www.swi-prolog.org/pldoc/doc_for?object=sort/2). Or (less efficiently): [list\_to\_set/2](http://www.swi-prolog.org/pldoc/man?predicate=list_to_set/2).

---

<div class="post-metadata">

**Author:** ![jan](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/jan/32/4_2.png) [@jan](https://swi-prolog.discourse.group/u/jan)\
**Post date:** [April 9, 2019, 6:42am UTC](https://swi-prolog.discourse.group/t/how-can-remove-the-duplicate-search-result/497/7 "2019-04-09T06:42:34Z")

</div>

The trick is called _stratification_, i.e. making _layers_. The lowest layer are the facts:

```
sister_fact(a, b).
sister_fact(c, d).
...

sister(X,Y) :- sister(X,Y).
sister(X,Y) :- sister(Y,X).

```

**note** The sister relation is in fact **not symmetric** , Mary is the sister of Bob, but Bob is the brother of Mary.

---

<div class="post-metadata">

**Author:** ![kastiglione](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/kastiglione/32/164_2.png) [@kastiglione](https://swi-prolog.discourse.group/u/kastiglione)\
**Post date:** [April 9, 2019, 3:51pm UTC](https://swi-prolog.discourse.group/t/how-can-remove-the-duplicate-search-result/497/8 "2019-04-09T15:51:20Z")

</div>

Jan, should that be:

```
sister(X,Y) :- sister_fact(X,Y).
sister(X,Y) :- sister_fact(Y,X).
```

---

<div class="post-metadata">

**Author:** ![jan](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/jan/32/4_2.png) [@jan](https://swi-prolog.discourse.group/u/jan)\
**Post date:** [April 9, 2019, 5:20pm UTC](https://swi-prolog.discourse.group/t/how-can-remove-the-duplicate-search-result/497/9 "2019-04-09T17:20:36Z")

</div>

Yes. Sorry

---

<div class="post-metadata">

**Author:** ![CHN-STUDENT](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/chn-student/32/166_2.png) [@CHN-STUDENT](https://swi-prolog.discourse.group/u/CHN-STUDENT)\
**Post date:** [April 9, 2019, 11:50pm UTC](https://swi-prolog.discourse.group/t/how-can-remove-the-duplicate-search-result/497/10 "2019-04-09T23:50:44Z")

</div>

Thanks to all people who is helping me!  
So you suggestion only apply to X,Y who are women,and there have symmetric?  
In fact, i think couple may can use grammar like this because of symmetric. Do you think that my thinking is wrong? What about you? Could you tell me? Thanks!

like this:

```prolog
marry(A,B).
marry(C,D).
....
couple(X,Y) :- marry(Y,X).
couple(X,Y) :- marry(X,Y).

```

---

<div class="post-metadata">

**Author:** ![CHN-STUDENT](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/chn-student/32/166_2.png) [@CHN-STUDENT](https://swi-prolog.discourse.group/u/CHN-STUDENT)\
**Post date:** [April 10, 2019, 12:03am UTC](https://swi-prolog.discourse.group/t/how-can-remove-the-duplicate-search-result/497/11 "2019-04-10T00:03:52Z")

</div>

Thank you,

> Jan said the sister relation is in fact **not symmetric**.

So perhaps add sex fact more useful? like this?

```prolog
sister(X,Y) :- sister_fact(X,Y), woman(X), woman(Y).
sister(X,Y) :- sister_fact(Y,X), woman(X), woman(Y).

```

If X,Y has symmetric’s sister relation, they should be as woman.

---

<div class="post-metadata">

**Author:** ![jan](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/jan/32/4_2.png) [@jan](https://swi-prolog.discourse.group/u/jan)\
**Post date:** [April 10, 2019, 6:59am UTC](https://swi-prolog.discourse.group/t/how-can-remove-the-duplicate-search-result/497/12 "2019-04-10T06:59:15Z")

</div>

X is a sister of Y if X and Y share both parents and X is female. I guess it is your task to write that down in Prolog. Which facts do you need?

---

<div class="post-metadata">

**Author:** ![CHN-STUDENT](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/chn-student/32/166_2.png) [@CHN-STUDENT](https://swi-prolog.discourse.group/u/CHN-STUDENT)\
**Post date:** [April 10, 2019, 11:17am UTC](https://swi-prolog.discourse.group/t/how-can-remove-the-duplicate-search-result/497/13 "2019-04-10T11:17:29Z")

</div>

Thanks. And I think i have no problem about this.

I reorganized the relationship and corrected the problem.

Here are my code:

```prolog
% One Hundred Years of Solitude 's family tree in Prolog
% Wiki picture: https://en.wikipedia.org/wiki/One_Hundred_Years_of_Solitude#/media/File:One_Hundred_Years_Of_Solitude_Buendia%27s_Family_Tree.svg

% See here:https://codereview.stackexchange.com/questions/143116/family-tree-in-prolog

% predicate(X,Y) means "X is Y 's XXX ."
% eg. father(X,Y) means "X is Y 's father."

:- encoding(utf8).
:- discontiguous man/1, woman/1, marry/2,amorousAffairsy/2,parent/2,father/2,mother/2,child/2,illegitimateChild/2.

% Generation I
man('José Arcadio Buendía').
woman('Úrsula Iguarán').

marry('José Arcadio Buendía','Úrsula Iguarán').

% Generation II
man('José Arcadio').
man('Colonel Aureliano Buendía').
woman('Amaranta').
woman('Remedios Moscote').
woman('Rebeca').
woman('Pilar Ternera').

parent('José Arcadio Buendía','José Arcadio').
parent('José Arcadio Buendía','Colonel Aureliano Buendía').
parent('José Arcadio Buendía','Amaranta').
parent('Úrsula Iguarán','José Arcadio').
parent('Úrsula Iguarán','Colonel Aureliano Buendía').
parent('Úrsula Iguarán','Amaranta').
marry('Colonel Aureliano Buendía','Remedios Moscote').
marry('Rebeca','José Arcadio').
amorousAffairsy('Pilar Ternera','José Arcadio').
amorousAffairsy('Pilar Ternera','Colonel Aureliano Buendía').

% Generation III
woman('Aureliano José').
woman('Arcadio').
man('Santa Sofía de la Piedad').

marry('Santa Sofía de la Piedad','Arcadio').
illegitimateChild('17 sons by unknown women','Colonel Aureliano Buendía').
parent('Pilar Ternera','Arcadio').
parent('José Arcadio Buendía','Arcadio').
% Generation IV
woman('Remedios the Beauty').
man('José Arcadio II').
man('Aureliano II').
woman('Petra Cotes').
woman('Fernanda del Carpio').

marry('Aureliano II','Fernanda del Carpio').
amorousAffairsy('Petra Cotes','Aureliano II').
parent('Santa Sofía de la Piedad','Remedios the Beauty').
parent('Santa Sofía de la Piedad','José Arcadio II').
parent('Santa Sofía de la Piedad','Aureliano II').
parent('Arcadio','Remedios the Beauty').
parent('Arcadio','José Arcadio II').
parent('Arcadio','Aureliano II').
% Generation V
man('Gastón').
man('José Arcadio').
man('Mauricio Babilonia').
woman('Amaranta Úrsula').
woman('Renata Remedios').

marry('Amaranta Úrsula','Gastón').
amorousAffairsy('Renata Remedios','Mauricio Babilonia').
parent('Aureliano II','Amaranta Úrsula').
parent('Aureliano II','José Arcadio').
parent('Aureliano II','Renata Remedios').
parent('Fernanda del Carpio','Amaranta Úrsula').
parent('Fernanda del Carpio','José Arcadio').
parent('Fernanda del Carpio','Renata Remedios').
% Generation VI
man('Aureliano Babilonia').
parent('Mauricio Babilonia','Aureliano Babilonia').
parent('Renata Remedios','Aureliano Babilonia').

amorousAffairsy('Amaranta Úrsula','Aureliano Babilonia').
% Generation VII
man('Aureliano').
parent('Aureliano Babilonia','Aureliano').
parent('Amaranta Úrsula','Aureliano').

% -------- Start defining the relationships -------- 

% The couple relation is in fact symmetric.
couple(X,Y) :- marry(X,Y), X\=Y. 
couple(X,Y) :- marry(Y,X), X\=Y.

% The amorous affairsy relation is also in fact symmetric.
illegitimateLover(X,Y) :- amorousAffairsy(X,Y); amorousAffairsy(Y,X), X\=Y.

father(X,Y) :- parent(X,Y), man(X), X\=Y.
mother(X,Y) :- parent(X,Y), woman(X), X\=Y.

% X or Y is Z's parents. X may be Z's father or mother,Y may be Z's mother or father.
parents(X,Y,Z) :- (father(X,Z), mother(Y,Z)); (father(Y,Z), mother(X,Z)), X\=Y, X\=Z, Z\=Y.
parents(X,Y,Z) :- (father(X,Z), mother(Y,Z)); (father(Y,Z), mother(X,Z)), X\=Y, X\=Z, Z\=Y.

% X is Y's child. => Y is X's father or mother.
child(X,Y) :- (father(Y,X); mother(Y,X)), X\=Y.
illegitimateChild(X,Y) :- illegitimateChild(X,Y), X\=Y.
son(X,Y) :- child(X,Y), man(X), X\=Y.
daughter(X,Y) :- child(X,Y), woman(X), X\=Y.

% Grandma or grandpa is someone's mother or father 's mother or father.
grandma(X,Y) :- (mother(X,M), mother(M,Y)); (mother(X,F),father(F,Y)), X\=Y.
grandpa(X,Y) :- (mother(X,M), father(M,Y)); (father(X,F), father(F,Y)), X\=Y.
grandparents(X,Y,Z) :- (grandma(X,Z), grandpa(Y,Z)); (grandma(Y,Z), grandpa(X,Z)), X\=Y, X\=Z, Z\=Y.

% Siblings need having same father and mother.
siblings(A,B) :- father(F,A), father(F,B), mother(M,A), mother(M,B), A\=B.

% Uncle or aunt is someone's mother or father 's siblings.
uncle(U,N) :- man(U), ((siblings(U,F), father(F,N)); (siblings(U,M), mother(M,N))), U\=N.
aunt(A,N) :- woman(A), ((siblings(A,F), father(F,N)); (siblings(A,F), mother(F,N))), A\=N.

% Y's sister or brother is woman or man. 
sister(X,Y) :- siblings(X,Y), woman(X), X\=Y.
brother(X,Y) :- siblings(X,Y), man(X), X\=Y.

% Ancestor may be someone's parent or they are someone's parent's ancestor.
ancestor(X,Y):- parent(X,Y), X\=Y.
ancestor(X,Y):- parent(X,Z), ancestor(Z,Y).

% -------- End defining the relationships -------- 

```
