0

我正在尝试根据老歌做家庭作业,我是我自己的爷爷。

所以,我已经开始为谁是儿子、女儿、父亲、父亲_in_law 等定义规则。

但是,我的规则/事实的顺序一定有问题,因为每次加载它时都会出现以下错误:

GNU Prolog 1.3.1
作者 Daniel Diaz 版权所有 (C) 1999-2009
Daniel Diaz | ?- [爷爷]。正在编译 /home/nfs/student/USER/cs4700/grandpa.pl
以获取字节码...
/home/nfs/student/USER/cs4700/grandpa.pl:119:警告:不连续谓词 child/2 - 子句被忽略
/home /nfs/student/USER/cs4700/grandpa.pl:120:警告:不连续谓词 child/2 - 忽略子句
/home/nfs/student/USER/cs4700/grandpa.pl:121:警告:不连续谓词 child/2 -子句被忽略
/home/nfs/student/USER/cs4700/grandpa.pl:122:警告:不连续谓词 child/2 - 子句被忽略
/home/nfs/student/USER/cs4700/grandpa.pl:123:警告:不连续谓词child/2 - 子句被忽略
/home/nfs/student/USER/cs4700/grandpa.pl:124:警告:不连续谓词 child/2 - 忽略子句
/home/nfs/student/USER/cs4700/grandpa.pl:125:警告:不连续谓词儿子/ 2 - 忽略子句
/home/nfs/student/USER/cs4700/grandpa.pl:126:警告:不连续谓词儿子/2 - 忽略子句
/home/nfs/student/USER/cs4700/grandpa.pl:127:警告:不连续谓词son/2 - 忽略子句
/home/nfs/student/USER/cs4700/grandpa.pl:128:警告:不连续谓词son/2 - 忽略子句
/home/nfs/student/USER/cs4700/grandpa.pl: 129:警告:不连续谓词 son/2 - 忽略子句
/home/nfs/student/USER/cs4700/grandpa.pl:130:警告:不连续谓词女儿/2 - 忽略子句
/home/nfs/student/USER/cs4700/grandpa.pl:131:警告:不连续谓词已婚/2 - 子句已忽略
/home/nfs/student/USER/cs4700/grandpa.pl:132:警告:不连续谓词已婚/ 2 - 子句忽略
/home/nfs/student/USER/cs4700/grandpa.pl:133:警告:不连续谓词已婚/2 - 子句忽略
/home/nfs/student/USER/cs4700/grandpa.pl:134:警告:不连续谓词已婚/2 - 子句被忽略
/home/nfs/student/USER/cs4700/grandpa.pl:135:警告:不连续谓词 son_in_law/2 - 子句被忽略
/home/nfs/student/USER/cs4700/grandpa.pl: 136:警告:不连续谓词father_in_law/2 - 子句被忽略
/home/nfs/student/USER/cs4700/grandpa.pl:137:警告:不连续谓词父亲/2 - 忽略子句
/home/nfs/student/USER/cs4700/grandpa.pl:138:警告:不连续谓词父亲/ 2 - 忽略子句
/home/nfs/student/USER/cs4700/grandpa.pl:139:警告:不连续谓词父亲/2 - 忽略子句
/home/nfs/student/USER/cs4700/grandpa.pl:140:警告:不连续谓词母亲/2 - 忽略子句
/home/nfs/student/USER/cs4700/grandpa.pl:141:警告:不连续谓词母亲/2 - 忽略子句
/home/nfs/student/USER/cs4700/grandpa.pl: 142:警告:不连续的谓词母亲/ 2 - 子句被忽略
/home/nfs/student/USER/cs4700/grandpa.pl:143:警告:不连续谓词 step_mother/2 - 忽略子句
/home/nfs/student/USER/cs4700/grandpa.pl:144:警告:不连续谓词 Brother_in_law/ 2 - 忽略子句
/home/nfs/student/USER/cs4700/grandpa.pl:145:警告:不连续谓词 Brother_in_law/2 - 忽略子句
/home/nfs/student/USER/cs4700/grandpa.pl:146:警告:不连续谓词 uncle/2 - 子句忽略
/home/nfs/student/USER/cs4700/grandpa.pl:147:警告:不连续谓词 step_daughter/2 - 子句忽略
/home/nfs/student/USER/cs4700/grandpa.pl 已编译,读取 149 行 - 写入 8389 字节,44 毫秒

到目前为止,我的代码是:

child(X,Y):-
   son(Y,X).

child(X,Y):-
   daughter(Y,X).

parent(X,Y):-
   father(X,Y).

parent(X,Y):-
   mother(X,Y).

son(X,Y):-
    child(X,Y),
    male(X).

daughter(X,Y):-
    child(X,Y),
    female(X).

son_in_law(X,Y):-
    child(X,Z),
    not(child(X,Y)),
    married(Z,Y),
    male(X).

step_daughter(X,Y):-
    child(X,Z),
    married(Z,Y),
    not(child(X,Y)),
    female(X).

brother(X,Y):-
    sibling(X,Y),
    male(X).

brother_in_law(X,Y):-
    parent(Z,X),
    parent(Z,Y),
    not(sibling(X,Y)),
    male(X).

sibling(X,Y):-
    parent(Z,X),
    parent(Z,Y).

sister(X,Y):-
    sibling(X,Y),
    female(X).

father(X,Y):-
    parent(X,Y),
    male(X).

father_in_law(X,Y):-
    child(X,Z),
    married(Y,Z),
    not(child(X,Y)),
    male(X).

mother(X,Y):-
    parent(X,Y),
    female(X).

step_parent(X,Y):-
    married(X,Z),
    parent(Z,Y),
    not(parent(X,Y)).

step_father(X,Y):-
    step_parent(X,Y),
    male(X).

step_mother(X,Y):-
    step_parent(X,Y),
    female(X).

grandparent(X,Y):-
    parent(X,Z),
    parent(Z,Y).

grandmother(X,Y):-
    grandparent(X,Y),
    female(X).

grandfather(X,Y):-
    grandparent(X,Y),
    male(X).

grandchild(X,Y):-
    child(X,Z),
    child(Z,Y).

married(X,Y):-
    wife(X,Y),
    female(X).

married(X,Y):-
    husband(X,Y),
    male(X).

uncle(X,Y):-
    sibling(X,Z),
    parent(Z,Y),
    male(X).

aunt(X,Y):-
    sibling(X,Z),
    parent(Z,Y),
    female(X).

male(i).
male(f).
male(s1).
male(s2).
female(w).
female(d).
child(i,f).
child(s1,w).
child(s1,i).
child(s2,d).
child(s2,f).
child(d,w).
son(i,f).
son(s1,w).
son(s1,i).
son(s2,d).
son(s2,f).
daughter(d,w).
married(i,w).
married(w,i).
married(f,d).
married(d,f).
son_in_law(f,i).
father_in_law(i,f).
father(f,i).
father(i,s1).
father(f,s2).
mother(w,s1).
mother(w,d).
mother(d,s2).
step_mother(d,i).
brother_in_law(f,s1).
brother_in_law(s1,f).
uncle(s1,i).
step_daughter(d,i).

我对prolog很陌生,所以我可能只是犯了一些根本性的错误。有人可以帮我指出有关这些错误的正确方向吗?

4

3 回答 3

2

自从我使用 Prolog 已经很长时间了,但我发现了这个。由此我得出的结论是,您要么需要首先使用不连续的事物,要么需要将所有规则组合在一起(即,将所有内容与孩子放在一个地方,即混合您的规则和事实)。例子:

孩子(X,Y):-儿子(Y,X)。

孩子(X,Y):-女儿(Y,X)。

孩子(i,f)。

孩子(s1,w)。

孩子(s1,i)。

孩子(s2,d)。

孩子(s2,f)。

孩子(d,w)。

于 2009-09-15T03:54:55.143 回答
1

http://www.gprolog.org/manual/gprolog.html#htoc50把它放在代码的顶部:

discontiguous([child, son, daughter, married, etc])

所以你可以抑制这个警告。你会从递归和堆栈溢出问题中挖掘出一些乐趣,但这是编程乐趣的一部分!

ps-“directive”比“thingy”更正式一点,但我喜欢它:-)

于 2009-09-15T04:33:17.093 回答
1

您可以重新组织规则或使用上面提示的不连续指令。

或者,这对于程序的使用是有意义的,您可以将与歌曲叙述相关的事实声明为动态的。建立了与儿子、女儿等相关的制作规则(即使已婚(X,Y)的问题引起了激烈的争论;-)),因此需要断言歌曲的事实。

另一件事:除非您正在调试/检查规则的合理性,否则您不需要拼出所有事实,只需从歌词中拼出这些。例如:“I had a baby son [from w]”给你儿子(s1,i)和[歌曲暗示]儿子(s1,w),但你可以让prolog推断出男性(s1)或孩子(s1 , i),如果这对其断言有用的话。

玩得开心 !

于 2009-09-15T04:59:55.107 回答