Family relations

Family relations are, a good way to show the strengths of Prolog's way of representing facts and rules for searching solutions.

Here is a small example of such a program. The relations like sons, sister etc. can be handled like facts in Prolog. Facts and rules are specified in the clauses section of a Visual Prolog program:

CLAUSES
  son("John", "Dan").
  sister("Mary","Suzan").
  brother("Harold", "Larry").
  married("John", "Mary").
  married("Larry", "Sue").

Note, that you also have to make some definitions ("declarations") of predicates, to make the program work, this is done in a PREDICATES section of the program

PREDICATES
  son(string,string).

etc.

This small example can lead us to some more powerful abstractions in our family relations program.

father(A,B):-son(B,A).

and

grandfather(A,B):-father(A,C), father(C,B).

Father is of course the inverse of son.

We can construct more complex relations, for instance, sister_in_law:

sister_in_law(A,B):-married(A,C), sister(C,B).

This leads us though to a problem, because a sister in law can also be the spouse of your brother. This is not really a problem, because if Prolog doesn’t find solutions for a predicate in its first clause, it tries again with the next. Therefore, we can just add another clause

sister_in_law(A,B):-brother(A,C),

married(C,B).

If the first one doesn’t succeed, Prolog will backtrack and try the next. If you try the goal

GOAL
  sister_in_law("John",Z).
Prolog will use the first predicate and answer 

Z="Suzan".

If we try

GOAL
  sister_in_law("Harold",Z).

the first clause can’t succeed, because Harold isn't married, so the predicate 'married' will not succeed. But the next one can, because Harold has a brother, Larry, that is married to Sue.

The list of predicates could be continued. Try to implement uncle, nephew, stepson and others, and try to input your own family.

FAMILY.PRJ