Solve family tree with PROLOG

Asked

Viewed 1,254 times

0

I’m having a hard time making relationships. Follows 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, siblings, brother, full siblings, father-in-law, uncle, grandson, ancestor relationship.

Father and mother I’m doing this way:

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

But after returning the mother or father, there is a return false.

Brothers I did it this way:

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

But in this case only brother returns from father. I need a rule to return brother with at least one parent relationship.

1 answer

1

Running the first query got the following answer:

?- pai(jurandir,willian).
false.

?- pai(willian,jurandir).
true .

?- pai(willian,Y).

Y = jurandir .

Let’s understand what’s going on before we approach the problem itself by seeing the trace: inserir a descrição da imagem aqui

Maybe you just need to make your own rules. I would do: pai(X,Y) :- homem(X), progenitor(X,Y).

It reads, "X is the father of Y if X is a man and X is the father of Y"

To find the brother, your rule only needs an addition:

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. 

Conducting the consultations:

?- irmao(helio,jurandir).
true .

?- irmao(henrique,livia).
true .

Adding the second fact to test half brother:

progenitor(Guline, Your mother).

?- irmao(mateus,isabela).
true .

I suggest you make that kind of composition of rules to find uncles, cousins, etcetera.

Before we proceed, 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).

Quite ordinary, isn’t it? A knowledge base containing two facts and two rules, but the definition of digeringo/2 is recursive and occurs on both sides of the second rule. Crucially, however, there is a stop point (a base case) for the number of calls. This point is given by the eating/1 rule, which occurs on both sides of the two rules

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

So, and 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.

inserir a descrição da imagem aqui

Finally, returning to your problem, we will find an ascendant(ancestor).

?- ascendente(jose,helio).
true .

?- ascendente(jose,fabiano).
true .

If you want a good material in Portuguese, look for Eloi Favero

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

Browser other questions tagged

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