Expert system for a family tree written in Prolog.
% Rules % A is child of B (father) and C (mother) child(A, B, C) :- son(A, B, C) ; daughter(A, B, C). % A is father/mother/parent of B father(A, B) :- son(B, A, _). mother(A, B) :- daughter(B, _, A). parent(A, B) :- father(A, B) ; mother(A, B). % A is grandfather/grandmother/grandparent of B grandfather(A, B) :- father(A, C) , parent(C, B). grandmother(A, B) :- mother(A, C) , parent(C, B). grandparent(A, B) :- grandfather(A, B) ; grandmother(A, B). % A is grandparent/grandchild of B grandson(A, B) :- grandfather(B, A). granddaughter(A, B) :- grandmother(B, A). grandchild(A, B) :- grandparent(B, A). % A is ancestor/descendent/vertical related of B ancestor(A, B) :- parent(A, B) ; (parent(A,C) , ancestor(C, B)). descendent(A, B) :- ancestor(B, A). % A is sibling/brother/sister of B brother(A, B) :- son(A, C, D) , child(B, C, D) , A \= B. sister(A, B) :- daughter(A, C, D) , child(B, C, D) , A \= B. sibling(A, B) :- brother(A, B) ; sister(A, B). % A is nephew/niece/nibling of B nephew(A, B) :- son(A, C, D) , (sibling(C, B) ; sibling(D, B)). niece(A, B) :- daughter(A, C, D) , (sibling(C, B) ; sibling(D, B)). nibling(A, B) :- nephew(A, B) ; niece(A, B). % A is uncle/aunt/pibling of B uncle(A, B) :- nephew(B, A). aunt(A, B) :- niece(B, A). pibling(A, B) :- nibling(B, A). % A is cousin of B cousin(A, B) :- nibling(A, C) , parent(C, B). % A is vertical/horizontal relative of B v_relative(A, B) :- ancestor(A, B) ; descendent(A, B). h_relative(A, B) :- sibling(A, B); nibling(A, B) ; pibling(A, B) ; cousin(A, B). % A is relative of B relative(A, B) :- v_relative(A, B) ; h_relative(A, B). % ----------------------------------------------------------------------------- % Facts son(jaime, tywin, joanna). son(tyrion, tywin, joanna). son(joffrey, jaime, cersei). son(tommen, jaime, cersei). daughter(cersei, tywin, joanna). daughter(myrcella, jaime, cersei). % ----------------------------------------------------------------------------- % Queries % relative(jaime, joffrey).