【问题标题】:Family tree with SWI-Prolog带有 SWI-Prolog 的家谱
【发布时间】:2016-04-17 20:50:23
【问题描述】:

我正在尝试使用最多允许 3 个事实的简单家谱与 Prolog 一起使用,但是我似乎无法将我的妹妹定义为我父母的孩子。这是我写的:

father(dad,me).
mother(mom,me).
siblings(me,sis).

parents(X,Z):-father(X,Z).
parents(Y,Z):-mother(Y,Z).
child(Z,X):-siblings(Z,Z2),parents(X,Z).
child(Z,Y):-siblings(Z,Z2),parents(Y,Z).
child(Z2,X):-siblings(Z,Z2),parents(X,Z).
child(Z2,Y):-siblings(Z,Z2),parents(Y,Z).
son(Z,X):-siblings(Z,Z2),parents(X,Z).
daughter(Z2,X):-siblings(Z,Z2),parents(X,Z).
brother(Z,Z2):-siblings(Z,Z2).
sister(Z2,Z):-siblings(Z,Z2).

当我在 Prolog 中输入father(ZFather,ZChild) 时,它只显示me 作为孩子而不是我的sis。我知道我没有在事实中定义它,但我尝试在规则中使用child(Z2,X)child(Z2,Y) 来定义它,这意味着Z2 是我的sis

我们将不胜感激。

【问题讨论】:

    标签: prolog logic family-tree


    【解决方案1】:

    您的谓词father/2 仅描述了一种解决方案。如果您希望它描述更多但不想添加更多事实,您可以为父亲添加一条规则:

    father(F,C) :-
       dif(X,C),
       siblings(X,C),
       father(F,X).
    

    如果你现在查询谓词:

       ?- father(X,Y).
    X = dad,
    Y = me ? ;
    X = dad,
    Y = sis ? ;
    no
    

    但是,从逻辑上讲,这不是一个非常干净的方法。毕竟,兄弟姐妹有可能只共享同一个母亲(或一般来说:只有一个父母)。最好不要将自己局限于三个事实。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-03-17
      • 1970-01-01
      • 1970-01-01
      • 2017-07-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-06-23
      相关资源
      最近更新 更多