Search code examples
prologrulesrule

Family trees - familes with more than two children


family(family_member(oleg,   barmin, birth_date(6, 1, 98), false), family_member(kate,   barmin, birth_date(1, 10, 97), true),
children(family_member(vasy,     barmin, birth_date(5, 12, 07), true))).

family(family_member(artem,  kudinov, birth_date(6, 1, 57), true), family_member(ann,    kudinov, birth_date(1, 10, 67), false),
children(family_member(julia,    barmin, birth_date(5, 12, 88), false))).

family(family_member(kola,   avramov, birth_date(6, 1, 57), false), family_member(nastya,    avramov, birth_date(1, 10, 67), false),
children(family_member(masha,    avramov, birth_date(5, 12, 88), false),family_member(fedya,     avramov, birth_date(5, 12, 88), false))).

family(family_member(ivan,   petrov, birth_date(6, 1, 57), true), family_member(daria,   petrov, birth_date(1, 10, 67), false),
children(family_member(a00000000,    petrov, birth_date(5, 12, 88), false),family_member(warihaerh,  petrov, birth_date(5, 12, 88), false),
family_member(b00000000,     petrov, birth_date(5, 12, 88), false))).

family(family_member(ivan,   ivanov, birth_date(6, 1, 57), true), family_member(daria,   ivanov, birth_date(1, 10, 67), false),
children(family_member(a00000000,    ivanov, birth_date(5, 12, 88), false),family_member(warihaerh,  ivanov, birth_date(5, 12, 88), false),
    family_member(orihthth,  ivanov, birth_date(5, 12, 88), false), family_member(shgsgh,    ivanov, birth_date(5, 12, 88), false))).


familyWifeWorkFalse(Y) :-
    Y = family_member(_, _, _, false),
    family(_, family_member(_, _, _, false), _).

oneChildrenMan(X) :-
    family(X, _, children(_)).

twoChildrenMan(X) :-
    family(X, _, children(_, _)).

moreTwoChildrenMan(X) :-
    \+oneChildrenMan(X),
    \+twoChildrenMan(X),
    family(X, _, _).

Hello everyone! I need help with my prolog task. I have several families, where first variable is a husbend, second one is a wifes and third one is children. I need to create a rule, which will output families with 3 and more children. I have created rules with output of one and two children and than try to exclude them in the third rule. But i got:

?- moreTwoChildrenMan(X).
false.

I have been tring to complete the task my self for several days, but i didnt get any result. Is there any idea how to do this?


Solution

  • The issue with your moreTwoChildrenMan/1 predicate is that you use Prolog's backtracking incorrectly.

    The goal negation operator \+ forces the execution mechanism of Prolog search for alternative solutions for the previous goal, and in the code of moreTwoChildrenMan/1 there is no goal prior to oneChildrenMan(X). This is why the predicate fails.

    But if you just move the goal family(X, _, _) before the goal oneChildrenMan(X) it will give you the desired result, since now the execution will backtrack on solutions for family(X, _, _):

    moreTwoChildrenMan(X) :-
            family(X, _, _),        % find some family member X
            \+ oneChildrenMan(X),   % filter out those with one child
            \+ twoChildrenMan(X).   % filter out those with two children