Exercise of family tree

Asked

Viewed 9,446 times

8

I have to create an uncle relationship according to the next family tree: Árvore Genealógica

I already have the following code:

mãe(ana, eva).
mãe(eva, noé).
mãe(bia, raí).
mãe(bia, clô).
mãe(bia, ary).
mãe(lia, gal).
pai(ivo, eva).
pai(raí, noé).
pai(gil, raí).
pai(gil, clô).
pai(gil, ary).
pai(ary, gal).
mulher(ana).
mulher(eva).
mulher(bia).
mulher(clô).
mulher(lia).
mulher(gal).
homem(ivo).
homem(raí).
homem(noé).
homem(gil).
homem(ary).
gerou(ana, eva).
gerou(ivo, eva).
gerou(eva, noé).
gerou(raí, noé).
gerou(bia, raí).
gerou(bia, clô).
gerou(bia, ary).
gerou(gil, raí).
gerou(gil, clô).
gerou(gil, ary).
gerou(ary, gal).
gerou(lia, gal).
avô(X, Y) :- pai(X, Z), pai(Z, Y); pai(X, Z), mãe(Z, Y).
avó(X, Y) :- mãe(X, Z), mãe(Z, Y); mãe(X, Z), pai(Z, Y).


filho(X, Y) :- gerou(Y, X), homem(X).
filha(X, Y) :- gerou(Y, X), mulher(X).

/* irmãos(raí, clô).
irmãos(clô, raí).
irmãos(ary, clô).
irmãos(ary, raí).
irmãos(clô, ary).
irmãos(raí, ary). */ 

In the exercise I’m doing, you don’t ask me to create the sibling relationship, but I can use it to create the uncle relationship? Otherwise, what relationships should I use? Thanks and forgiveness if I haven’t expressed myself correctly.

  • 1

    I don’t know Prolog. About the sibling relationship is automatic, when sharing father, mother or both. If there is a brother relationship with the parent of a child, then he is an uncle

  • Well thought about the sibling relationship, could have created a rule to define it.

2 answers

7


If two people have the same mother, then they are brothers. The easiest way would be to create the relationship brother and sister:

irmao(X,Y) :- gerou(Z,X), gerou(Z,Y), homem(X)
irma(X,Y) :- gerou(Z,X), gerou(Z,Y), mulher(X)

Finally, the relationship uncle would be:

tio(X,Y) :- irmao(X,Z), pai(Z,Y); irma(Z, X), mae(Z, Y)

Now if creating the sibling relationship is not allowed, it is possible to resolve with the rule below:

tio(X,Y) :- homem(X), gerou(M, X), gerou(M, I), pai(I,Y); 
            homem(X), gerou(M, X), gerou(M, I), mae(I,Y)

1

To make it simple, I’m going to skip the male/female issues.

It is advisable to avoid:

  • all be brothers to themselves
  • parents are their children’s uncles

To that end I added a condition X ≠ Y in this case coded as X \== Y

irmao(X,Y) :- gerou(PAI,X), gerou(PAI,Y), X \== Y.    
tio(T,Y)   :- gerou(AVO, T), gerou(AVO, PAI), gerou(PAI,Y), T \== PAI. 

Similarly we can remove many redundant elementary facts from joining two more rules:

mulher(A):-  mae(A,_).
homem(A) :- pai(A,_).

Browser other questions tagged

You are not signed in. Login or sign up in order to post.