Calculate Family Relationship from Genealogical Data

后端 未结 6 2167
萌比男神i
萌比男神i 2021-01-30 11:36

I would like to be able to calculate the family relationship between two individuals in a family tree, given the following data schema (simplified from my actual data schema, on

6条回答
  •  你的背包
    2021-01-30 12:29

    As strange as it might sound PROLOG seems to be the thing you're looking for. Given following ad-hoc program (http://www.pastey.net/117134 better colouring)

    female(alice).
    female(eve).
    female(kate).
    
    male(bob).
    male(carlos).
    male(dave).
    
    % mother(_mother, _child).
    mother(alice, bob).
    mother(kate, alice).
    
    % father(_father, _child)
    father(carlos, bob).
    
    child(C, P) :- father(P, C).
    child(C, P) :- mother(P, C).
    
    parent(X, Y) :- mother(X, Y).
    parent(X, Y) :- father(X, Y).
    
    sister(alice, eve).
    sister(eve, alice).
    sister(alice, dave).
    
    brother(dave, alice).
    
    % brother(sibling, sibling)
    sibling(X, Y) :- brother(X, Y).
    sibling(X, Y) :- sister(X, Y).
    
    
    uncle(U, C) :- sibling(U, PARENT),
        child(C, PARENT),
        male(U).
    
    
    relationship(U, C, uncle) :- uncle(U, C).
    relationship(P, C, parent) :- parent(P, C).
    relationship(B, S, brother) :- brother(B, S).
    relationship(G, C, grandparent) :- parent(P, C), parent(G, P).
    
    

    You could ask Prolog interpreter something like that:

    relationship(P1, P2, R).

    with the answers:

    
    P1 = dave, P2 = bob, R = uncle ;
    P1 = alice, P2 = bob, R = parent ;
    P1 = kate, P2 = alice, R = parent ;
    P1 = carlos, P2 = bob, R = parent ;
    P1 = dave, P2 = alice, R = brother ;
    P1 = kate, P2 = bob, R = grandparent ;
    false.
    

    It's a powerful tool, if you know how and when to use it. This seems exactly like a place for Prolog. I know it's not terribly popular, or easy to embed, but the impressive feature of wolphram alpha shown in one of the comments can be coded using nothing more than constructs used above, and this is Prolog 101.

提交回复
热议问题