Prolog 规则不断返回 false

问题描述 投票:0回答:0

我真的是 Prolog 的新手,我正在做这个项目。我有关于英国君主制的数据,我正在研究一个“继承人”规则,该规则可以找到王位继承人或告诉我我的输入是否是有效的王位继承人。

我有关于英国君主制的数据:

male(georgeIII).
male(georgeIV).
male(frederick).
male(williamIV).
male(edward).
male(ernest_augustus).
male(augustus).
male(adolphus).
male(george1).
male(francis).
male(albert).
male(edwardVII).
male(alfred).
male(leopold).
male(albert_victor).
male(georgeV).
male(edwardVIII).
male(georgeVI).
male(henry).
male(george2).
male(john).


female(charlotte).
female(mary).
female(caroline).
female(charlotte1).
female(victoria).
female(victoriaI).
female(augusta).
female(mary_adelaide).
female(mary1).
female(victora1).
female(alexandra).
female(alice).
female(helena).
female(beatrice).
female(louise_victoria).
female(maud).
female(mary2).


born(georgeIII, 1738).
born(georgeIV, 1762).
born(frederick, 1763).
born(williamIV, 1765).
born(edward, 1767).
born(ernest_augustus, 1771).
born(augustus, 1773).
born(adolphus, 1774).
born(george1, 1819).
born(albert, 1819).
born(francis, 1837).
born(edwardVII, 1841).
born(alfred, 1844).
born(leopold, 1853).
born(albert_victor, 1864).
born(georgeV, 1865).
born(edwardVIII, 1894).
born(georgeVI, 1895).
born(henry, 1900).
born(george2, 1902).
born(john, 1905).


born(charlotte, 1744).
born(caroline, 1768).
born(mary, 1776).
born(victoria, 1786).
born(charlotte1, 1796).
born(augusta, 1797).
born(victoriaI, 1819).
born(victora1, 1819).
born(mary_adelaide, 1833).
born(alice, 1843).
born(alexandra, 1844).
born(helena, 1844).
born(beatrice, 1857).
born(mary1, 1867).
born(louise_victoria, 1867).
born(maud, 1869).
born(mary2, 1897).


parents(edwardVII, alexandra, albert_victor).
parents(edwardVII, alexandra, georgeV).
parents(edwardVII, alexandra, louise_victoria).
parents(edwardVII, alexandra, maud).
parents(albert, victoriaI, victoria1).
parents(albert, victoriaI, edwardVII).
parents(albert, victoriaI, alice).
parents(albert, victoriaI, alfred).
parents(albert, victoriaI, helena).
parents(albert, victoriaI, arthur).
parents(albert, victoriaI, leopold).
parents(albert, victoriaI, beatrice).
parents(francis, mary_adelaide, mary1).
parents(edward, victoria, victoriaI).
parents(georgeIV, caroline, charlotte1).
parents(georgeIII, charlotte, georgeIV).
parents(georgeIII, charlotte, frederick).
parents(georgeIII, charlotte, williamIV).
parents(georgeIII, charlotte, edward).
parents(georgeIII, charlotte, ernest_augustus).
parents(georgeIII, charlotte, augustus).
parents(georgeIII, charlotte, adolphus).
parents(georgeIII, charlotte, mary).
parents(adolphus, augusta, george1).
parents(adolphus, augusta, mary_adelaide).
parents(georgeV, mary1, edwardVIII).
parents(georgeV, mary1, georgeVI).
parents(georgeV, mary1, mary2).
parents(georgeV, mary1, henry).
parents(georgeV, mary1, george2).
parents(georgeV, mary1, john).


year(1780).

我正在研究一个名为“继承人”的 Prolog 规则,其定义如下:

heir(<valid heir>, <heirs parent>, <sequence law type>, <gender law type>)

关于君主制的性别和顺序规则如下:

%gender law:
%  strict_agnatic:
%    primary - only males of the parents dynasty may inherit
%    secondary - if no valid primary heirs, only males of parents children may inherit
%  strict_enatic
%    primary - only females of the parents dynasty may inherit
%    secondary - if no valid primary heirs, only females of parents children may inherit
%  agnatic_cognatic
%    primary - males of the parents dynasty may inherit
%    secondary - if no valid primary heirs, females of parents dynasty may inherit
%              - female heirs with male children are preferred
%  enatic_cognatic
%    primary - females of the parents dynasty may inherit
%    secondary - if no valid primary heirs, males of parents dynasty may inherit
%              - male heirs with female children are preferred

%sequence law:
%  primogeniture: oldest eligible successor is heir
%  ultimogenture: youngest eligible successor is heir

这是继承人规则的当前代码:

age(NAME, Z):- born(NAME, X), year(Y), Z is Y-X.
child_of(X, Y):- parents(Y,_,X);parents(_,Y,X).
is_older(A, B):- age(A, AG), age(B, AG0), \+(A = B), AG > AG0.
is_younger(A, B):- age(A, AG), age(B, AG0), \+(A = B), AG < AG0.


elder_child(PAR, CHD0, CHD1):- child_of(CHD0, PAR),child_of(CHD1, PAR),
                CHD0 \= CHD1,is_older(CHD0, CHD1).

eldest_child(PAR, CHD):- child_of(CHD, PAR),\+elder_child(PAR, _, CHD).


younger_child(PAR, CHD0, CHD1):- child_of(CHD0, PAR),child_of(CHD1, PAR),
                CHD0 \= CHD1,is_younger(CHD0, CHD1).


youngest_child(PAR, CHD):- child_of(CHD, PAR),\+younger_child(PAR, _, CHD).

% this is the code I am working on:
heir(HEIR, PARENT, strict_agnatic, primogeniture) :-
    male(HEIR),
    child_of(HEIR, PARENT),
    eldest_child(PARENT, HEIR).

heir(HEIR, PARENT, strict_agnatic, ultimogeniture) :-
    male(HEIR),
    child_of(HEIR, PARENT),
    youngest_child(PARENT, HEIR).

heir(HEIR, PARENT, strict_enatic, primogeniture) :-
    female(HEIR),
    child_of(HEIR, PARENT),
    eldest_child(PARENT, HEIR).

heir(HEIR, PARENT, strict_enatic, ultimogeniture) :-
    female(HEIR),
    child_of(HEIR, PARENT),
    youngest_child(PARENT, HEIR).

heir(HEIR, PARENT, agnatic_cognatic, primogeniture) :-
    male(HEIR),
    child_of(HEIR, PARENT),
    eldest_child(PARENT, HEIR).
heir(HEIR, PARENT, agnatic_cognatic, primogeniture) :-
    female(HEIR),
    child_of(HEIR, PARENT),
    \+ heir(_, PARENT, agnatic_cognatic, primogeniture),
    eldest_child(PARENT, CHILD), male(CHILD).

现在,当我尝试使用输入

heir(X, georgeIII, ultimogeniture, strict_enatic).
对此进行测试时,我得到了
false
而不是
X = mary
。我不知道如何解决这个问题。

我尝试了多个测试用例,但都失败了: 第一行是输入,下一行是预期结果。我一直变得“错误”,它从来没有告诉我

X
应该是什么。

%testcases:
%    heir(georgeIV, georgeIII, primogeniture, agnatic_cognatic).
%    true ?
%
%    heir(georgeIV, georgeIII, ultimogeniture, agnatic_cognatic).
%    no
%
%    heir(X, georgeIII, primogeniture, strict_enatic).
%    X = mary
%        yes
%
%    heir(X, georgeIII, ultimogeniture, strict_enatic).
%    X = mary
%        yes
%
%   (if you comment out mary from female, born and parents):
%    heir(X, georgeIII, ultimogeniture, strict_enatic).
%    X = charlotte1 ? ;
%    X = victoriaI ? ;
%    X = mary_adelaide ? ;
%    no
%
%   heir(X, georgeIII, ultimogeniture, enatic_cognatic).
%   X = adolphus ? ;
%   no
%
%   heir(X, georgeIII, primogeniture, enatic_cognatic).
%   X = georgeIV ? ;
%   no
prolog swi-prolog
© www.soinside.com 2019 - 2024. All rights reserved.