Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/69.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Prolog调用、退出和重做与指定规则不匹配_Prolog_Rules - Fatal编程技术网

Prolog调用、退出和重做与指定规则不匹配

Prolog调用、退出和重做与指定规则不匹配,prolog,rules,Prolog,Rules,我在追踪序言时遇到了一些问题。跟踪日志如下所示: parent_of(X,Y). Call: (6) parent_of(_G2780, _G2781) ? creep Exit: (6) parent_of(warren, jerry) ? creep X = warren Y = jerry ; Redo: (6) parent_of(_G2780, _G2781) ? creep Exit: (6) parent_of(maryalice, jerry) ? creep X = marya

我在追踪序言时遇到了一些问题。跟踪日志如下所示:

parent_of(X,Y).
Call: (6) parent_of(_G2780, _G2781) ? creep
Exit: (6) parent_of(warren, jerry) ? creep
X = warren
Y = jerry ;
Redo: (6) parent_of(_G2780, _G2781) ? creep
Exit: (6) parent_of(maryalice, jerry) ? creep
X = maryalice
Y = jerry ;
Redo: (6) parent_of(_G2780, _G2781) ? creep
Call: (7) brother(_G2865, _G2781) ? creep
Exit: (7) brother(jerry, kather) ? creep
Call: (7) father(_G2780, jerry) ? creep
Call: (8) parent_of(_G2780, jerry) ? creep
Exit: (8) parent_of(warren, jerry) ? creep
Call: (8) male(warren) ? creep
Exit: (8) male(warren) ? creep
Exit: (7) father(warren, jerry) ? creep
Exit: (6) parent_of(warren, kather) ? creep
male(jerry).
male(stuart).
male(warren).
male(peter).
female(kather).
female(maryalice).
female(ann)
brother(jerry,stuart).
brother(jerry,kather).
brother(peter, warren).
sister(ann, maryalice).
sister(kather,jerry).
parent_of(warren,jerry).
parent_of(maryalice,jerry).

parent_of(X,Z):- brother(Y,Z),(father(X,Y);mother(X,Y)).
father(X,Y) :-  parent_of(X,Y), male(X).
mother(X,Y) :-  parent_of(X,Y), female(X).
然而,我声明我的事实和规则如下:

parent_of(X,Y).
Call: (6) parent_of(_G2780, _G2781) ? creep
Exit: (6) parent_of(warren, jerry) ? creep
X = warren
Y = jerry ;
Redo: (6) parent_of(_G2780, _G2781) ? creep
Exit: (6) parent_of(maryalice, jerry) ? creep
X = maryalice
Y = jerry ;
Redo: (6) parent_of(_G2780, _G2781) ? creep
Call: (7) brother(_G2865, _G2781) ? creep
Exit: (7) brother(jerry, kather) ? creep
Call: (7) father(_G2780, jerry) ? creep
Call: (8) parent_of(_G2780, jerry) ? creep
Exit: (8) parent_of(warren, jerry) ? creep
Call: (8) male(warren) ? creep
Exit: (8) male(warren) ? creep
Exit: (7) father(warren, jerry) ? creep
Exit: (6) parent_of(warren, kather) ? creep
male(jerry).
male(stuart).
male(warren).
male(peter).
female(kather).
female(maryalice).
female(ann)
brother(jerry,stuart).
brother(jerry,kather).
brother(peter, warren).
sister(ann, maryalice).
sister(kather,jerry).
parent_of(warren,jerry).
parent_of(maryalice,jerry).

parent_of(X,Z):- brother(Y,Z),(father(X,Y);mother(X,Y)).
father(X,Y) :-  parent_of(X,Y), male(X).
mother(X,Y) :-  parent_of(X,Y), female(X).
我想第一个电话应该处决兄弟和父母?为什么它直接给了我出口,而沃伦是杰瑞的父母


提前谢谢

Prolog按照断言的顺序查找事实和规则。由于在规则父元素/2之前断言了一些事实父元素/2,因此首先会找到这些事实。您没有显示完整的跟踪,但在找到事实后,它会跟踪到规则:

[trace]  ?- parent_of(X,Y).
   Call: (7) parent_of(_G1353, _G1354) ? creep
   Exit: (7) parent_of(warren, jerry) ? creep
X = warren,
Y = jerry ;
   Redo: (7) parent_of(_G1353, _G1354) ? creep
   Exit: (7) parent_of(maryalice, jerry) ? creep
X = maryalice,
Y = jerry ;
   Redo: (7) parent_of(_G1353, _G1354) ? creep
   Call: (8) brother(_G1444, _G1354) ? creep
   Exit: (8) brother(jerry, stuart) ? creep
   Call: (8) father(_G1353, jerry) ? creep
   Call: (9) parent_of(_G1353, jerry) ? creep
   Exit: (9) parent_of(warren, jerry) ? creep
   Call: (9) male(warren) ? creep
   Exit: (9) male(warren) ? creep
   Exit: (8) father(warren, jerry) ? creep
   Exit: (7) parent_of(warren, stuart) ? creep
X = warren,
Y = stuart ;
   Redo: (9) parent_of(_G1353, jerry) ? creep
   Exit: (9) parent_of(maryalice, jerry) ? creep
   Call: (9) male(maryalice) ? creep
   Fail: (9) male(maryalice) ? creep
   Redo: (9) parent_of(_G1353, jerry) ? creep
   Call: (10) brother(_G1444, jerry) ? creep
   Fail: (10) brother(_G1444, jerry) ? creep
   Fail: (9) parent_of(_G1353, jerry) ? creep
   Fail: (8) father(_G1353, jerry) ? creep
   Redo: (7) parent_of(_G1353, stuart) ? creep
   Call: (8) mother(_G1353, jerry) ? creep
   Call: (9) parent_of(_G1353, jerry) ? creep
   Exit: (9) parent_of(warren, jerry) ? creep
   Call: (9) female(warren) ? creep
   Fail: (9) female(warren) ? creep
   Redo: (9) parent_of(_G1353, jerry) ? creep
   Exit: (9) parent_of(maryalice, jerry) ? creep
   Call: (9) female(maryalice) ? creep
   Exit: (9) female(maryalice) ? creep
   Exit: (8) mother(maryalice, jerry) ? creep
   Exit: (7) parent_of(maryalice, stuart) ? creep
X = maryalice,
Y = stuart
...
但是,我建议您将规则名称与事实名称区分开来。所以我会做一些类似的事情:

parent(warren,jerry).
parent(maryalice,jerry).
然后:

但也要注意多余的事实。看起来你的父母关系可能已经被涉及父亲/2和母亲/2的事实所定义。所以不清楚你为什么需要父母/2的事实

请注意,您的代码在此处有语法错误:

female(ann)
brother(jerry,stuart).
您可能忽略了错误。但是如果没有周期,Prolog将忽略上述两个事实,然后给出您看到的结果。
另一方面,您的代码有很多循环逻辑,这是不好的。/2的父\u依赖于父/2和母/2,然后父/2和母/2调用/2的父\u。这是将事实与谓词分开的另一个重要原因。

我明白了,但在X=maryalice Y=jerry之后;对,第三个我得到X=warren Y=kather。我不知道为什么它跳过了斯图尔特,因为事实证明,斯图尔特是在凯瑟面前宣布的。让我来更新跟踪日志。@Fyndy您首先要了解的事实是warren&jerry和maryalice&jerry,它按照预期顺序首先找到了这两个。下一个被发现的是warren&stuart使用规则。我不知道你说的是什么意思。看我的踪迹。我直接运行了你的代码。因此,如果您要让warren&kather为您的第三个解决方案提供帮助,那么您没有运行您发布的代码。我试图通过阅读2780来跟踪它,因为2780是2781的父代。2865是2781的兄弟。但在出口处,我怎么知道2865是杰瑞,2781是凯瑟?我更新了我的跟踪日志,并且已经重新运行了几次,但我仍然得到了kather@Fyndy这些数字只是与匿名变量名相对应,在实例化之前它们只是变量。所以这些数字并不直接对应于你的原子。它们只是变量用来区分它们的任意数字。你用的是什么序言?SWI?还有别的吗?什么版本?我完全按照您在SWI中的问题中显示的方式运行代码,并且得到了我在答案中显示的结果。因此,请仔细检查您实际使用的代码。@Fyndy我想我可能知道问题所在。你有一个语法错误,你把femaleann的句点去掉了。因此,Prolog不承认这一事实,也不承认下面的事实,斯图亚特的兄弟。。我在运行您的代码时已修复了该错误,因此我没有观察到与您相同的结果。您有语法错误。在femaleann之后你需要一段时间。