Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/actionscript-3/6.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 - Fatal编程技术网

Prolog中的动态谓词

Prolog中的动态谓词,prolog,Prolog,我已经开始使用Prolog了,我在使用动态谓词时遇到了这个问题——我没有得到正确的结果 这是我的数据库: :- dynamic mother/2. mother(X,Y). grandemother(X,Y) :- mother(X,A), mother(A,Y). 以下是我得到的一些结果: 1 ?- assert(mother(alice,lise)). true. 2 ?- assert(mother(lise,kate)). true. 3 ?- grandem

我已经开始使用Prolog了,我在使用动态谓词时遇到了这个问题——我没有得到正确的结果

这是我的数据库:

:- dynamic mother/2.

mother(X,Y).

grandemother(X,Y) :- 
    mother(X,A),
    mother(A,Y).
以下是我得到的一些结果:

1 ?- assert(mother(alice,lise)).
true.

2 ?- assert(mother(lise,kate)).
true.

3 ?- grandemother(alice,X). % should only give X = kate.
true ;
X = lise ;
X = kate ;
true ;
X = kate.

4 ?- grandemother(alice,lise). % should only give false.
true ;
true ;
true ;
false.

5 ?- grandemother(X,kate). % should only give X = alice.
true ;
true ;
X = alice ;
X = alice ;
X = lise.

我真的不知道问题在哪里,有什么想法吗?

正如@Louger在他的评论中所说的,问题是你的母亲(X,Y)。,直接在动态声明之后

为了准确地分析发生了什么,我将看看
祖母(爱丽丝,X)
(在你断言
母亲(爱丽丝,莉丝)
母亲(莉丝,凯特)
):

因此,
grandmother(alice,X)
无需绑定X即可成功

我们再次问,这次:

mother(A,X) unifies with mother(alice,lise). (The second mother/2 fact.)
祖母(alice,X)
成功地将X绑定到lise。再问一次

mother(A,X) unifies with mother(lise,kate).
X是凯特。再次

mother(A,X) cannot be unified any more. Backtrack further...
mother(alice,A) unifies with mother(alice,lise).
mother(lise,X) unifies with mother(X,Y). X is unbound.
mother(lise,X) unifies with mother(lise,kate).
X再次被解除绑定,因此我们只得到
true
。再次

mother(A,X) cannot be unified any more. Backtrack further...
mother(alice,A) unifies with mother(alice,lise).
mother(lise,X) unifies with mother(X,Y). X is unbound.
mother(lise,X) unifies with mother(lise,kate).
X又是凯特。还有吗

mother(lise,X) cannot be unified any more. Backtrack further...
mother(alice,A) cannot be unified any more. Backtrack further...
No more backtracking to be done, so there are no more results.
所以我们没有得到更多的结果


正如@潜伏者所指出的,解决办法是移除
母亲(X,Y)。
,这样就不会发生这种不受约束的行为。

母亲(X,Y)的目的是什么。?也就是说,
X
Y
的母亲,不管
X
Y
是什么,因为它们是变量。我知道我可以把它们作为事实
母亲(alice,lise)
,等等,但我想用断言谓词进行实例化。我的意思是字面上的
母亲(X,Y)。
,您的
dynamic/1
呼叫后的线路。这是一个“事实”,表明任何人都是其他人的母亲。