家谱 - 有两个以上孩子的家庭

时间:2017-04-10 10:05:51

标签: prolog rules rule

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, _, _).

大家好!我的prolog任务需要帮助。我有几个家庭,第一个变量是husbend,第二个是妻子,第三个是孩子。 我需要创建一个规则,它将输出有3个或更多孩子的家庭。我创建了一个和两个孩子的输出规则,而不是尝试在第三个规则中排除它们。但我得到了:

?- moreTwoChildrenMan(X).
false.

我一直在努力完成我自己的任务好几天,但我没有得到任何结果。 有什么想法怎么做?

1 个答案:

答案 0 :(得分:0)

moreTwoChildrenMan/1谓词的问题在于您错误地使用了Prolog的backtracking

目标否定运算符\+强制Prolog的执行机制搜索上一个目标的替代解决方案,并且在moreTwoChildrenMan/1的代码中oneChildrenMan(X)之前没有目标。这就是谓词失败的原因。

但是,如果您只是在目标family(X, _, _)之前移动目标oneChildrenMan(X),它会为您提供所需的结果,因为现在执行将回溯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