在Prolog中,我如何在族谱上提问?

在Prolog中,我如何在族谱上提问?,prolog,Prolog,我目前正在尝试学习如何使用Prolog。我已经安装了SWI Prolog 6.2.6版 它似乎在运行: ?- 3<4. true. ?- 4<3. false. 但是当我尝试用consult(family)加载它时。我得到: ?- consult(family). Warning: /home/moose/Desktop/family.pl:7: Clauses of father/2 are not together in the source-file Warning

我目前正在尝试学习如何使用Prolog。我已经安装了SWI Prolog 6.2.6版

它似乎在运行:

?- 3<4.
true.

?- 4<3.
false.
但是当我尝试用
consult(family)加载它时。
我得到:

?- consult(family).
Warning: /home/moose/Desktop/family.pl:7:
    Clauses of father/2 are not together in the source-file
Warning: /home/moose/Desktop/family.pl:9:
    Clauses of mother/2 are not together in the source-file
Warning: /home/moose/Desktop/family.pl:11:
    Clauses of father/2 are not together in the source-file
Warning: /home/moose/Desktop/family.pl:12:
    Clauses of mother/2 are not together in the source-file
Warning: /home/moose/Desktop/family.pl:13:
    Clauses of father/2 are not together in the source-file
Warning: /home/moose/Desktop/family.pl:14:
    Clauses of mother/2 are not together in the source-file
% family compiled 0.00 sec, 17 clauses
true.
我不明白这里有什么问题。我发现一些结果提到,
-
不能在标识符中使用,但我没有在标识符中使用
-

问题1:是什么原因导致上面的警告?我怎样才能修好它

但是只有警告,所以我继续

?- female(fabienne).
true.

?- male(fabienne).
false.
好的,这似乎和预期的一样

然后我又加了一句

male(jake).
female(ida).
female(kahlan).
female(luci).
brother(X,Y):-
    male(X),
    (mother(Z,X)=mother(Z,Y);father(Z,X)=father(Z,Y)).
并尝试:

?- brother(jake,ida).
false.
为什么不是这样


问题2:我弟弟的
规则有什么问题?

你的第一个问题得到了回答

至于第二个,你是从功能而不是关系的角度来思考的

mother(Z,X) = mother(Z,Y)
这与说
X=Y
相同,因为它比较两个术语,而不解释它们。如果您希望
Z
成为
X
Y
的母亲,则需要一个连词:

mother(Z, X), mother(Z, Y)

如果将所有父声明和母声明移到一起(不要混合它们),则可以修复警告 在兄弟定义中,您使用=并且应该使用逻辑and,因此使用,(逗号) 你还需要告诉prolog杰克是个男性,因为他无法从这些规则中理解

我将brother定义拆分为2,因为它更清晰,这类似于逻辑OR(第一个定义有效,第二个定义有效)

试运行

brother(X,Y).

为了获得更多的结果,你需要像我为jake做的那样,为这些孩子添加谁是男性或女性

我认为,
必须被理解为
。如果我想让他们有不同的母亲,我会写什么<代码>母亲(Z,X);母亲(Z,Y)?@moose,the
是prolog中的OR。如果你想强制X和Y有不同的母亲,你可以说,母亲(X,Mx),母亲(Y,My),Mx\==My。也就是说,
Mx
X
的母亲,
My
Y
的母亲,
Mx
My
是不同的。啊,现在我明白了。(正确答案接受,评论加1)
father(bob,danna).
father(bob,fabienne).
father(bob,gabrielle).
father(charlie,ida).
father(charlie,jake).
father(edgar,kahlan).
father(hager,luci).

mother(alice,danna).
mother(alice,fabienne).
mother(alice,gabrielle).

mother(danna,ida).
mother(danna,jake).
mother(fabienne,kahlan).
mother(gabrielle,luci).
male(jake).
male(X) :- father(X,_).
female(X) :- mother(X,_).

brother(X,Y):-
    male(X),
    mother(Z,X),mother(Z,Y).
brother(X,Y):-
    male(X),    
    father(Z,X),father(Z,Y).
brother(X,Y).