Family tree - cousin relationships with repeated names












1














I am trying to get my cousin relationship to work accurately. For example when I do the query: cousins(X,ron). It returns: X = nancy;X = nancy;X = jeff;X = jeff;X = david;X = thomas.
I believe these are the correct cousins but I don't want to return a name more than once. How can I fix this? And this is the one relationship that is difficult for me to decipher if these are actually cousins or not.



parent(A,B) :- mother(A,B); father(A,B).
grandparent(C,E) :- parent(C,D),parent(D,E).
grandmother(C,E) :- mother(C,D),parent(D,E).
grandfather(C,E) :- father(C,D),parent(D,E).
siblings(B,G) :- parent(P,B), parent(P,G), B=G.
cousins(X,Y) :- parent(A,X), parent(B,Y), siblings(A,B), X=Y.

mother(mary,sue).
mother(mary,bill).
mother(sue,nancy).
mother(sue,jeff).
mother(jane,ron).
mother(nancy,alice2).
mother(gail,jane).
mother(gail,elaine).
mother(laura,alice).
mother(elaine,david).
mother(sarah,frank).
mother(elaine,thomas).

father(john,sue).
father(john,bill).
father(bob,nancy).
father(bob,jeff).
father(bill,ron).
father(charlie,alice2).
father(david,charlie).
father(carl,jane).
father(peter,laura).
father(frank,thomas).
father(frank,david).
father(thomas,alice).
father(henry,frank).

(ancestor(A,D):-parent(A,D)).
(ancestor(A,D):-(parent(P,D),ancestor(A,P))).









share|improve this question
























  • instead of = try @<
    – CapelliC
    Nov 12 '18 at 17:04










  • no that did not work...it made the siblings relationship invalid
    – Royale_w_cheese
    Nov 12 '18 at 17:11








  • 1




    i'll try that in SWI prolog. Sometimes SWISH does weird things
    – Royale_w_cheese
    Nov 12 '18 at 17:19










  • so it did not work In SWI prolog either. and my original code is returning every cousin name twice in SWI prolog.
    – Royale_w_cheese
    Nov 12 '18 at 17:26






  • 1




    When you have a rule like, parent(A,B) :- mother(A,B); father(A,B)., you're going to get possibly two logically distinct solutions to a query like parent(P, john) if they have two parents. If you want your rules to be as general as possible (not lose solutions) and not use a cut (!) or once/1, there's not much you can do about it as there's no further logical reduction to be done or tighter conditions to check. Logically speaking, there are two solutions. In those cases, you can let your predicate find all the solutions then use something like setof/3 to get a unique collection.
    – lurker
    Nov 13 '18 at 13:12
















1














I am trying to get my cousin relationship to work accurately. For example when I do the query: cousins(X,ron). It returns: X = nancy;X = nancy;X = jeff;X = jeff;X = david;X = thomas.
I believe these are the correct cousins but I don't want to return a name more than once. How can I fix this? And this is the one relationship that is difficult for me to decipher if these are actually cousins or not.



parent(A,B) :- mother(A,B); father(A,B).
grandparent(C,E) :- parent(C,D),parent(D,E).
grandmother(C,E) :- mother(C,D),parent(D,E).
grandfather(C,E) :- father(C,D),parent(D,E).
siblings(B,G) :- parent(P,B), parent(P,G), B=G.
cousins(X,Y) :- parent(A,X), parent(B,Y), siblings(A,B), X=Y.

mother(mary,sue).
mother(mary,bill).
mother(sue,nancy).
mother(sue,jeff).
mother(jane,ron).
mother(nancy,alice2).
mother(gail,jane).
mother(gail,elaine).
mother(laura,alice).
mother(elaine,david).
mother(sarah,frank).
mother(elaine,thomas).

father(john,sue).
father(john,bill).
father(bob,nancy).
father(bob,jeff).
father(bill,ron).
father(charlie,alice2).
father(david,charlie).
father(carl,jane).
father(peter,laura).
father(frank,thomas).
father(frank,david).
father(thomas,alice).
father(henry,frank).

(ancestor(A,D):-parent(A,D)).
(ancestor(A,D):-(parent(P,D),ancestor(A,P))).









share|improve this question
























  • instead of = try @<
    – CapelliC
    Nov 12 '18 at 17:04










  • no that did not work...it made the siblings relationship invalid
    – Royale_w_cheese
    Nov 12 '18 at 17:11








  • 1




    i'll try that in SWI prolog. Sometimes SWISH does weird things
    – Royale_w_cheese
    Nov 12 '18 at 17:19










  • so it did not work In SWI prolog either. and my original code is returning every cousin name twice in SWI prolog.
    – Royale_w_cheese
    Nov 12 '18 at 17:26






  • 1




    When you have a rule like, parent(A,B) :- mother(A,B); father(A,B)., you're going to get possibly two logically distinct solutions to a query like parent(P, john) if they have two parents. If you want your rules to be as general as possible (not lose solutions) and not use a cut (!) or once/1, there's not much you can do about it as there's no further logical reduction to be done or tighter conditions to check. Logically speaking, there are two solutions. In those cases, you can let your predicate find all the solutions then use something like setof/3 to get a unique collection.
    – lurker
    Nov 13 '18 at 13:12














1












1








1







I am trying to get my cousin relationship to work accurately. For example when I do the query: cousins(X,ron). It returns: X = nancy;X = nancy;X = jeff;X = jeff;X = david;X = thomas.
I believe these are the correct cousins but I don't want to return a name more than once. How can I fix this? And this is the one relationship that is difficult for me to decipher if these are actually cousins or not.



parent(A,B) :- mother(A,B); father(A,B).
grandparent(C,E) :- parent(C,D),parent(D,E).
grandmother(C,E) :- mother(C,D),parent(D,E).
grandfather(C,E) :- father(C,D),parent(D,E).
siblings(B,G) :- parent(P,B), parent(P,G), B=G.
cousins(X,Y) :- parent(A,X), parent(B,Y), siblings(A,B), X=Y.

mother(mary,sue).
mother(mary,bill).
mother(sue,nancy).
mother(sue,jeff).
mother(jane,ron).
mother(nancy,alice2).
mother(gail,jane).
mother(gail,elaine).
mother(laura,alice).
mother(elaine,david).
mother(sarah,frank).
mother(elaine,thomas).

father(john,sue).
father(john,bill).
father(bob,nancy).
father(bob,jeff).
father(bill,ron).
father(charlie,alice2).
father(david,charlie).
father(carl,jane).
father(peter,laura).
father(frank,thomas).
father(frank,david).
father(thomas,alice).
father(henry,frank).

(ancestor(A,D):-parent(A,D)).
(ancestor(A,D):-(parent(P,D),ancestor(A,P))).









share|improve this question















I am trying to get my cousin relationship to work accurately. For example when I do the query: cousins(X,ron). It returns: X = nancy;X = nancy;X = jeff;X = jeff;X = david;X = thomas.
I believe these are the correct cousins but I don't want to return a name more than once. How can I fix this? And this is the one relationship that is difficult for me to decipher if these are actually cousins or not.



parent(A,B) :- mother(A,B); father(A,B).
grandparent(C,E) :- parent(C,D),parent(D,E).
grandmother(C,E) :- mother(C,D),parent(D,E).
grandfather(C,E) :- father(C,D),parent(D,E).
siblings(B,G) :- parent(P,B), parent(P,G), B=G.
cousins(X,Y) :- parent(A,X), parent(B,Y), siblings(A,B), X=Y.

mother(mary,sue).
mother(mary,bill).
mother(sue,nancy).
mother(sue,jeff).
mother(jane,ron).
mother(nancy,alice2).
mother(gail,jane).
mother(gail,elaine).
mother(laura,alice).
mother(elaine,david).
mother(sarah,frank).
mother(elaine,thomas).

father(john,sue).
father(john,bill).
father(bob,nancy).
father(bob,jeff).
father(bill,ron).
father(charlie,alice2).
father(david,charlie).
father(carl,jane).
father(peter,laura).
father(frank,thomas).
father(frank,david).
father(thomas,alice).
father(henry,frank).

(ancestor(A,D):-parent(A,D)).
(ancestor(A,D):-(parent(P,D),ancestor(A,P))).






prolog family-tree






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 12 '18 at 15:33

























asked Nov 12 '18 at 15:12









Royale_w_cheese

1147




1147












  • instead of = try @<
    – CapelliC
    Nov 12 '18 at 17:04










  • no that did not work...it made the siblings relationship invalid
    – Royale_w_cheese
    Nov 12 '18 at 17:11








  • 1




    i'll try that in SWI prolog. Sometimes SWISH does weird things
    – Royale_w_cheese
    Nov 12 '18 at 17:19










  • so it did not work In SWI prolog either. and my original code is returning every cousin name twice in SWI prolog.
    – Royale_w_cheese
    Nov 12 '18 at 17:26






  • 1




    When you have a rule like, parent(A,B) :- mother(A,B); father(A,B)., you're going to get possibly two logically distinct solutions to a query like parent(P, john) if they have two parents. If you want your rules to be as general as possible (not lose solutions) and not use a cut (!) or once/1, there's not much you can do about it as there's no further logical reduction to be done or tighter conditions to check. Logically speaking, there are two solutions. In those cases, you can let your predicate find all the solutions then use something like setof/3 to get a unique collection.
    – lurker
    Nov 13 '18 at 13:12


















  • instead of = try @<
    – CapelliC
    Nov 12 '18 at 17:04










  • no that did not work...it made the siblings relationship invalid
    – Royale_w_cheese
    Nov 12 '18 at 17:11








  • 1




    i'll try that in SWI prolog. Sometimes SWISH does weird things
    – Royale_w_cheese
    Nov 12 '18 at 17:19










  • so it did not work In SWI prolog either. and my original code is returning every cousin name twice in SWI prolog.
    – Royale_w_cheese
    Nov 12 '18 at 17:26






  • 1




    When you have a rule like, parent(A,B) :- mother(A,B); father(A,B)., you're going to get possibly two logically distinct solutions to a query like parent(P, john) if they have two parents. If you want your rules to be as general as possible (not lose solutions) and not use a cut (!) or once/1, there's not much you can do about it as there's no further logical reduction to be done or tighter conditions to check. Logically speaking, there are two solutions. In those cases, you can let your predicate find all the solutions then use something like setof/3 to get a unique collection.
    – lurker
    Nov 13 '18 at 13:12
















instead of = try @<
– CapelliC
Nov 12 '18 at 17:04




instead of = try @<
– CapelliC
Nov 12 '18 at 17:04












no that did not work...it made the siblings relationship invalid
– Royale_w_cheese
Nov 12 '18 at 17:11






no that did not work...it made the siblings relationship invalid
– Royale_w_cheese
Nov 12 '18 at 17:11






1




1




i'll try that in SWI prolog. Sometimes SWISH does weird things
– Royale_w_cheese
Nov 12 '18 at 17:19




i'll try that in SWI prolog. Sometimes SWISH does weird things
– Royale_w_cheese
Nov 12 '18 at 17:19












so it did not work In SWI prolog either. and my original code is returning every cousin name twice in SWI prolog.
– Royale_w_cheese
Nov 12 '18 at 17:26




so it did not work In SWI prolog either. and my original code is returning every cousin name twice in SWI prolog.
– Royale_w_cheese
Nov 12 '18 at 17:26




1




1




When you have a rule like, parent(A,B) :- mother(A,B); father(A,B)., you're going to get possibly two logically distinct solutions to a query like parent(P, john) if they have two parents. If you want your rules to be as general as possible (not lose solutions) and not use a cut (!) or once/1, there's not much you can do about it as there's no further logical reduction to be done or tighter conditions to check. Logically speaking, there are two solutions. In those cases, you can let your predicate find all the solutions then use something like setof/3 to get a unique collection.
– lurker
Nov 13 '18 at 13:12




When you have a rule like, parent(A,B) :- mother(A,B); father(A,B)., you're going to get possibly two logically distinct solutions to a query like parent(P, john) if they have two parents. If you want your rules to be as general as possible (not lose solutions) and not use a cut (!) or once/1, there's not much you can do about it as there's no further logical reduction to be done or tighter conditions to check. Logically speaking, there are two solutions. In those cases, you can let your predicate find all the solutions then use something like setof/3 to get a unique collection.
– lurker
Nov 13 '18 at 13:12












0






active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53265023%2ffamily-tree-cousin-relationships-with-repeated-names%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes
















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53265023%2ffamily-tree-cousin-relationships-with-repeated-names%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







這個網誌中的熱門文章

Hercules Kyvelos

Tangent Lines Diagram Along Smooth Curve

Yusuf al-Mu'taman ibn Hud