Solve a family tree with PROLOG

0

I have a hard time creating some relationships. Here is the table:

homem(jose).
homem(ananias).
homem(helio).
homem(jurandir).
homem(valdir).
homem(delio).
homem(fabiano).
homem(willian).
homem(diego).
homem(mateus).
homem(henrique).
mulher(generosa).
mulher(tereza).
mulher(maria).
mulher(nilzete).
mulher(ildete).
mulher(jaqueline).
mulher(gislaine).
mulher(jaqueline).
mulher(livia).
mulher(isabela).
mulher(maria_e).
casado(jose, generosa).
casado(ananias, tereza).
casado(delio, maria).
casado(jurandir, nilzete).
casado(delio, ildete).
casado(willian, jaqueline).
casado(diego, gislaine).
progenitor(jose, helio).
progenitor(generosa, helio).
progenitor(jose, jurandir).
progenitor(generosa, jurandir).
progenitor(ananias, nilzete).
progenitor(tereza, nilzete).
progenitor(ananias, valdir).
progenitor(tereza, valdir).
progenitor(ananias, ildete).
progenitor(tereza, ildete).
progenitor(helio, fabiano).
progenitor(maria, fabiano).
progenitor(jurandir, willian).
progenitor(nilzete, willian).
progenitor(jurandir, gislaine).
progenitor(nilzete, gislaine).
progenitor(delio, jaqueline).
progenitor(ildete, jaqueline).
progenitor(willian, henrique).
progenitor(jaqueline, henrique).
progenitor(willian, livia).
progenitor(jaqueline, livia).
progenitor(diego, isabela).
progenitor(gislaine, isabela).
progenitor(diego, maria_e).
progenitor(gislaine, maria_e).
progenitor(fabiano, mateus).

I need the following relationship rule: Father, mother, paternal grandmother, brothers, brother, full brothers, father-in-law, uncle, grandson, ancestor relationship.

Father and mother I'm doing it this way:

pai(X,Y) :- homem(Y), progenitor(Y,X).
mae(X,Y) :- mulher(Y), progenitor(Y,X).

But after returning to the parent, it has a false return.

Brothers I did this:

irmao(X,Y) :- progenitor(HOMEM,X), progenitor(HOMEM,Y), X \== Y. 

But in this case, only the parent returns sibling. I need a rule to return a brother with at least one parent-to-parent relationship.

    
asked by anonymous 10.04.2018 / 22:50

1 answer

0

Running the first query I got the following response:

?- pai(jurandir,willian).
false.

?- pai(willian,jurandir).
true .

?- pai(willian,Y).

Y = jurandir .

Let's understand what's happening before we approach the problem itself by seeing the trace:

Maybeyoujustneedtoorganizeyourrules.Iwoulddo:pai(X,Y):-homem(X),progenitor(X,Y).

  

Weread:"X is the father of Y if X is man and X is the parent of Y"

To find your brother, your rule needs only one add-on:

irmao(X,Y):-homem(X), pai(Z,X), pai(Z,Y),X\=Y.
irmao(X,Y):-homem(X), mae(Z,X), mae(Z,Y),X\=Y. 

Performing the queries:

?- irmao(helio,jurandir).
true .

?- irmao(henrique,livia).
true .

Adding the second fact to test half brother:

  

parent (gislaine, mateus).

?- irmao(mateus,isabela).
true .

I suggest you make this kind of rule-making to find uncles, cousins, and so on.

Before proceeding, consider the following example:

digerindo(X,Y) :- comeu(X,Y).
digerindo(X,Y) :- comeu(X,Z), digerindo(Z,Y).

comeu(mosquito,sangue(joão)).
comeu(sapo,mosquito).
comeu(cegonha,sapo).
Pretty ordinary, huh? A knowledge base containing two facts and two rules, but the definition of digering / 2 is recursive and occurs on both sides of the second rule. Crucially, however, there is a stopping point (a base case) for the number of calls. This point is given by the ateu / 1 rule, which occurs on both sides of the two rules

The first rule (which is not recursive, or as we usually call it, the base clause) simply says that: if X has just eaten Y, then X is not digesting Y.

So, what about the second rule, the recursive rule? this one says that: if X has eaten Z and Z is digesting Y, then X is digesting Y as well. Therefore, Prolog concludes by inference that the query is true.

Finally, returning to your problem, we find an ascendant.

?- ascendente(jose,helio).
true .

?- ascendente(jose,fabiano).
true .
  

If you want good stuff in Portuguese, search for Eloi Favero

     

Source: BLACKBURN, Patrick; BOS, Johannes; STRIEGNITZ, Kristina. Learn   prolog now !. London: College Publications, 2006.

    
12.04.2018 / 23:29