Using Prolog to Analyze your Family Tree

I teach a little of Prolog in both Artificial Intelligence and in Programming Languages. Prolog is a language for entering facts, and letting the system deduce new facts based on those. A standard example to learn what the language can do is to practice on a fake family tree. But this could be done on your real family tree too.

I wrote a quick Prolog Exporter for the upcoming Gramps 4.2 to test out how this would work on a good size family tree. In these examples, I use GNU Prolog. The “rules” look like:

grandparent(X, Y) :- parent(X, Z), parent(Z, Y).
ancestor(X, Y) :- parent(X, Y).
ancestor(X, Y) :- parent(X, Z), ancestor(Z, Y).
sibling(X, Y) :- parent(Z, X), parent(Z,Y), X \= Y.

This says that “a person X is grandparent of Y if  X is a parent of Z, who is a parent of  Y.” And “X is an ancestor of Y if X is a parent of Y, or if X is a parent of Z, and Z is an ancestor of Y.” And “X is a sibling of Y if Z is a parent of X and Y and X is not the same as Y.”

The data look like:

data(bi0828, 'Isard, Ann').
is_alive(bi0828, 'False').
data(bi0000, 'Gilliam, Elizabeth').
is_alive(bi0000, 'False').
data(bi0665, 'Sutherland, Margaret').

In Prolog, lowercase words are values, and uppercase words are variables. Prolog tries to find matches for any variables in what you type. You can ask questions this way:

data(GID, 'Sutherland, Margaret').

which says “what is Margaret’s gramps id?” and Prolog will respond with GID = bi0665.

Now, it gets interesting when you start using functions such as “ancestor” on the whole database. For example, you could ask for all of Margaret’s ancestors:

ancestor(Who, bi0665).

and Prolog will find them all. Or find all of the relatives of Margaret’s who are still alive:

ancestor(Who, bi0665), is_alive(Who, ‘True’).

You can do that fairly easily in Gramps with filters. But some queries would be more difficult to construct in Gramps. For example, could you write a filter that finds all the people who have a sibling named “David”? Here is the query in Prolog:

sibling(X, Y), data(Y, Z), sub_atom(Z, Before, Length, After, 'David').

 

Here is a short video (3 minutes) demonstrating the code running:

 

Hope you found that interesting,

-Doug

4 Comments

  • Sebastian

    Wow! Looks awesome! Nice job. When and where can we download the plugin? Looking forward to test it out myself!

  • Carl-Erik Kopseng

    Yeah, joining Sebastian as the crowd that wants to know where this functionality is hidden 🙂 Is this prolog export plugin downloadable somewhere?

  • Doug Blank

    Gramps 4.2 is now released, and you’ll find the Prolog Exporter in the Addons (menu -> Preferences -> Addons).

  • Sebastian

    Hi Doug,

    awesome work! Can you please tell if it is possible to export a prolog file via the command line?

    Like “gramps –quiet -O “mydatabase” -a report -p prolog”?

    Thank you very much for your (all) commitment to this awesome Genealogy software!

Join the Conversation!