我如何订购这些Prolog规则以便运行我的程序?

我正在尝试根据老歌做作业,I'm my own grandpa.

所以,我已经开始为儿子,女儿,父亲,父亲 _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:警告:discontiguous predicate child / 2 - ignore ignored / home /nfs/student/USER/cs4700/grandpa.pl:120:警告:discontiguous谓词child / 2 - 子句忽略/home/nfs/student/USER/cs4700/grandpa.pl:121:警告:discontiguous predicate child / 2 - 子句忽略/home/nfs/student/USER/cs4700/grandpa.pl:122:警告:discontiguous谓词child / 2 - 子句忽略/home/nfs/student/USER/cs4700/grandpa.pl:123:警告:discontiguous谓词child / 2 - 忽略/home/nfs/student/USER/cs4700/grandpa.pl:124:警告:discontiguous谓词child / 2 - 子句忽略/home/nfs/student/USER/cs4700/grandpa.pl:125:警告:discontiguous谓词son / 2 - 子句忽略/home/nfs/student/USER/cs4700/grandpa.pl:126:警告:discontiguous谓词son / 2 - 子句忽略/ home / nfs / student / USER / cs4700 / grandpa . pl:127:警告:discontiguous谓词子/ 2 - 子句忽略/ h ome / nfs / student / USER / cs4700 / grandpa.pl:128:警告:discontiguous谓词son / 2 - 子句忽略/home/nfs/student/USER/cs4700/grandpa.pl:129:警告:discontiguous predicate 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 :警告:discontiguous谓词结婚/ 2 - 条款忽略/home/nfs/student/USER/cs4700/grandpa.pl:134:警告:discontiguous谓词结婚/ 2 - 子句忽略/ home / nfs / student / USER / cs4700 / grandpa .pl:135:警告:discontiguous谓词son_in_law / 2 - 子句忽略/home/nfs/student/USER/cs4700/grandpa.pl:136:警告:discontiguous谓词father_in_law / 2 - 子句忽略/ home / nfs / student / USER /cs4700/grandpa.pl:137:警告:discontiguous谓词父/ 2 - 子句忽略/home/nfs/student/USER/cs4700/grandpa.pl:138:警告:discontiguous谓词父/ 2 - 子句忽略/ home / nfs / student / USER / cs4700 /爷爷 . pl:139:警告:discontiguous谓词父/ 2 - 子句忽略/home/nfs/student/USER/cs4700/grandpa.pl:140:警告:discontiguous谓词母亲/ 2 - 子句忽略/ home / nfs / student / USER / cs4700 / grandpa.pl:141:警告:discontiguous谓词母亲/ 2 - 子句忽略/home/nfs/student/USER/cs4700/grandpa.pl:142:警告:discontiguous谓词母亲/ 2 - 子句忽略/ home / nfs / student / USER / cs4700 / grandpa.pl:143:警告:discontiguous谓词step_mother / 2 - 子句忽略/home/nfs/student/USER/cs4700/grandpa.pl:144:警告:discontiguous谓词brother_in_law / 2 - 子句忽略/ home / nfs / student / USER / cs4700 / grandpa.pl:145:警告:discontiguous谓词brother_in_law / 2 - 子句忽略/home/nfs/student/USER/cs4700/grandpa.pl:146:警告:discontiguous谓词u ncle / 2 - 子句忽略/home/nfs/student/USER/cs4700/grandpa.pl:147:警告:discontiguous谓词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很新,所以我可能只是犯了一些根本性的错误 . 有人可以帮我指出这些错误的正确方向吗?

回答(3)

3 years ago

我使用Prolog已经很久了,但我找到了this . 从那里我得出结论,你要么首先要使用不连续的东西,要么你需要将所有的规则组合在一起(即在一个地方将孩子的所有内容都包括在内,即混合你的规则和事实) . 例:

child(X,Y): - 儿子(Y,X) .

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

子(I,F) .

孩子(S1,W) .

孩子(S1,I) .

孩子(S2,d) .

孩子(S2,F) .

孩子(d,W) .

3 years ago

http://www.gprolog.org/manual/gprolog.html#htoc50开始,将它放在代码的顶部:

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

所以你可以压制这个警告 . 您将有一些乐趣从递归和堆栈溢出问题中挖掘出来,但这是编程乐趣的一部分!

p.s.-'指令'比'thingy'更正式,但我喜欢它:-)

3 years ago

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

或者,这对于程序使用是有意义的,您可以将与歌曲叙述相关的事实声明为动态 . Build 与儿子,女儿等相关的 生产环境 规则(即使已婚(X,Y)的问题受到激烈辩论;-)),因此需要断言歌曲的事实 .

另一件事:除非你正在调试/检查你的规则的完整性,否则你不需要拼出所有的事实,只有歌词中的这些事实 . 例如:“我有一个小儿子[来自w]”给你儿子(s1,i)和[暗示歌曲]儿子(s1,w),但你可以让prolog推断男性(s1)或孩子(s1) ,i),如果这对它的断言甚至有用 .

玩得开心 !