本文介绍了我需要如何订购这些 Prolog 规则才能运行我的程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试根据老歌完成家庭作业,我是我自己的爷爷.

I'm trying to work on a homework assignment based on the old song, I'm my own grandpa.

所以,我开始为儿子、女儿、父亲、父亲_in_law 等定义规则.

So, I've started out defining rules for who a son, daughter, father, father_in_law, etc was.

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

However, something must be wrong with the order of my rules/facts because every time I load it I get the following errors:

GNU Prolog 1.3.1
Daniel Diaz 版权所有 (C) 1999-2009
丹尼尔·迪亚兹 |?- [爷爷].编译/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:警告:不连续的谓词儿子/2- 忽略子句
/home/nfs/student/USER/cs4700/grandpa.pl:128:警告:不连续的谓词儿子/2- 忽略子句
/home/nfs/student/USER/cs4700/grandpa.pl:129:警告:不连续的谓词儿子/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:警告:不连续的谓词parent_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 毫秒

到目前为止我的代码是:

And my code so far is:

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 很陌生,所以我可能只是犯了一些根本性的错误.有人能帮我指出有关这些错误的正确方向吗?

I'm very new to prolog so I'm probably just making some fundamental mistake. Can someone help point me in the right direction concerning these errors?

推荐答案

很久没有使用 Prolog,但我发现 这个.由此我得出结论,您要么需要先使用不连续的东西,要么需要将所有规则组合在一起(即将所有内容与孩子放在一个地方,即混合您的规则和事实).示例:

It was a long time since I used Prolog but I found this. And from that I conclude that you either need to use the discontiguous thingy first or you need to group all your rules together (i.e. having everything with child in one place, i.e. mixing your rules and facts). Example:

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

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

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

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

子(i,f).

child(s1,w).

child(s1,w).

child(s1,i).

child(s1,i).

子(s2,d).

子(s2,f).

孩子(d,w).

这篇关于我需要如何订购这些 Prolog 规则才能运行我的程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-31 12:58