【问题标题】:swi-prolog predicate doesn't workswi-prolog 谓词不起作用
【发布时间】:2012-04-06 06:06:18
【问题描述】:

我在 swi-prolog 中有一个非常简单的程序,我在其中定义了家庭关系的所有谓词。我还定义了一些事实(基于圣经的亚伯拉罕家族)。 所以我这样定义了一个谓词“mother_in_law”:

mother_in_law(X,Y):- female(X),mother(X,Z),married(Z,Y).

还有一些事实:

female(sarah).
mother(sarah,isaac).
married(rebekah,isaac).

所以现在,当我输入程序时

?- mother_in_law(sarah,X).

它应该只给我 X=rebekah。但它给了我一些不符合我定义的条件的 X 其他值。

谁能告诉我我做错了什么?

非常感谢

我添加了整个文本部分:

father(X,Y). % Define Predicates%
mother(X,Y).
married(X,Y).
male(X).
female(X).
parent(X,Y).
diff(X,Y).
male_cousin(X,Y).
brother_in_law(X,Y).
mother_in_law(X,Y).
cousin_three(X,Y).
cousin(X,Y).
sib(X,Y).

%Define Facts%
male(terah). male(abram). male(nahor). male(haran). male(isaac).
male(lot). male(moab). male(ben_ami). male(bethuel). male(laban).
male(esau). male(jacob). male(reuben).
female(sarah). female(milcah). female(lot_daughter_1). female(lot_daughter_2).
female(rebekah). female(leah). female(dinah).
father(terah,abram). father(terah,nahor).  father(terah,haran).
father(abram,isaac).  father(haran,lot). father(nahor,bethuel).
father(bethuel,laban). father(laban,leah). father(isaac,jacob).
father(isaac,esau).
father(jacob,reuben).
mother(sarah,isaac). mother(milcah, bethuel). mother(rebekah,jacob).
mother(rebekah,esau). mother(leah,reuben).
mother(lot_daughter_1,moab). mother(lot_daughter_2,ben_ami).
married(abram,sarah). married(nahor,milcah).
married(isaac,rebekah). married(jacob,leah).
married(sarah,abram). married(milcah,nahor).
married(rebekah,isaac). married(leah,jacob).



%Define complicated predicates%
    parent(X,Y) :- father(X,Y).
    parent(X,Y) :- mother(X,Y).
    diff(X,Y) :- not(X=Y).
    sib(X,Y):- parent(Z,X), parent(Z,Y), diff(X,Y).  %define sibling %
    mother_in_law(X,Y):- female(X),mother(X,Z),married(Z,Y). %question 1%
    male_cousin(X,Y):- male(X),father(W,X),parent(Z,Y),sib(W,Z).    %question 2%
    cousin(X,Y):- parent(Z,X), parent(W,Y), sib(W,Z).       %question 3%
    cousin_three(X,Y):- parent(Z,X),parent(G,Z),parent(H,Y),parent(K,H),cousin(G,K).
    brother_in_law(X,Y):- male(X),married(X,Z),sib(Y,Z). %question 5-X is married to Y's sibling%
    brother_in_law(X,Y):- male(X),sib(X,Z),married(Y,Z).  %X's sibling is married to Y%
    brother_in_law(X,Y):- male(X),married(X,W),married(Y,Z),sib(W,Z).  % Y is married to the sibling of X's spouse%

【问题讨论】:

    标签: prolog


    【解决方案1】:

    将以下内容添加到您的事实中。然后就可以了。

    married(isaac,rebekah).
    

    更好的方法可能是让已婚关系本身处理已婚(A,B)与已婚(B,A)相同的事实

    【讨论】:

    • 谢谢。实际上我按照你说的做了,但它仍然给了其他一些废话,比如 jacob 是 sarah 的女婿(?!?!)......所以我仍然不知道问题出在哪里。
    • 你也可以发布其余的事实吗?那我也可以试试
    【解决方案2】:

    您的解决方案的问题是由于以下事实

    mother_in_law(X,Y).
    

    声明任何人都是其他人的岳母。

    放下

    father(X,Y). % Define Predicates%
    mother(X,Y).
    married(X,Y).
    male(X).
    female(X).
    parent(X,Y).
    diff(X,Y).
    male_cousin(X,Y).
    brother_in_law(X,Y).
    mother_in_law(X,Y).
    cousin_three(X,Y).
    cousin(X,Y).
    sib(X,Y). 
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-08-21
      • 1970-01-01
      • 2011-11-11
      • 1970-01-01
      相关资源
      最近更新 更多