0
votes

I'm new to prolog I'm trying to write a family tree I got the code to get parent,mother,father,sister,brother,etc.. I am trying to write code to get great-great-grandmother/s

I have this code to get great-grandmother/s :

great_grandmother(X,Y) :- parent(X,V), parent(V,W), parent(W,Y), female(X).

however I tried many times to get the great-great-grandmother/s but it is not working out. How would I modify my code to get the great-great-grandmother/s?

and also when I run my code on swish.swi I get all the correct great grandmothers but the program keeps running and it gives me false. Is there anyway to fix it based on my code to get great grandmothers?

1
On the one hand you say: it is not working out on the other hand you say that you get all the correct grandmothers. That Prolog appends at the end ; false. is OK. - false
well I get the great grandmothers, but I want to get the great great grandmothers if that makes sense. and can you tell me why at the end it says false? - addicted_programmer
You need to give use more information. Like the facts for parent/2. So far, we can only guess. But my guess is that you do not have sufficiently many persons. Otherwise how do you query great great grandmothers directly? It's all guesses, you need to provide more information. And simply ignore the ; false. - false
Any chance you could edit your question and provide a minimal reproducible example? - Enigmativity

1 Answers

0
votes

Your code is based on the approach that a great-grandmother is essentially a mother of a parent of a parent. In the same way, a great-great-grandmother would be a mother of a parent of a parent of a parent:

great_great_grandmother(X,Z) :- parent(X,V), parent(V,W), parent(W,Y), parent(Y,Z), female(X).

To break down this logic, you could limit everything to pairs of binary relations, such that a grandparent is a parent of a parent, a great-grandparent is a parent of a grandparent, etc., as in:

parent(X,Y) :- child(Y,X).
grandparent(X,Y) :- parent(X,Y), parent(Y,Z).
great_grandparent :- parent(X,Y), grand_parent(Y,Z).
great_great_grandparent(X,Y) :- parent(X,Y), great_grandparent(Y,Z).

And then a great-great-grandmother is just a female great-great-grandparent:

great_great_grandmother(X,Y) :- great_great_grandparent(X,Y), female(X).

As for why you are not getting solutions for your query, it all depends on what is in your facts. Either your great_great_grandparent predicate is ill formed, or there are no entities by that definition in your model, or both.