Prolog割算子行为
我有以下条款:Prolog割算子行为,prolog,behavior,clause,prolog-cut,Prolog,Behavior,Clause,Prolog Cut,我有以下条款: a(1). a(2). b(a). c(A,B,C) :- a(A),d(B,C). c(A,B,C) :- b(A),d(B,C). d(B,C) :- a(B),!,a(C). d(B,_) :- b(B). 当我运行查询c(X,Y,Z)时,答案是: X = 1, Y = 1, Z = 1 ; X = 1, Y = 1, Z = 2 ; X = 2, Y = 1, Z = 1 ; X = 2, Y = 1, Z = 2 ; X = a, Y = 1, Z = 1 ; X =
a(1).
a(2).
b(a).
c(A,B,C) :- a(A),d(B,C).
c(A,B,C) :- b(A),d(B,C).
d(B,C) :- a(B),!,a(C).
d(B,_) :- b(B).
当我运行查询c(X,Y,Z)时,答案是:
X = 1, Y = 1, Z = 1 ;
X = 1, Y = 1, Z = 2 ;
X = 2, Y = 1, Z = 1 ;
X = 2, Y = 1, Z = 2 ;
X = a, Y = 1, Z = 1 ;
X = a, Y = 1, Z = 2.
所以基本上,切割操作符(在这里d(B,C):-a(B),!,a(C)忽略了最近的选择点,也就是说,它不会进一步搜索d()和a()。我认为,削减忽略了所有以前的选择点,不会做任何回溯
有人能解释一下确切的行为吗?为什么我错了?因为我没有立即理解你对切割操作的解释,所以我查看了你的代码。我的阅读大致如下:
1. Kills off the parent choice-point
2. Commits to all the choices made going through the rule
在以下情况下为真:c(A,B,c)
和a(a)
d(B,C)
- 或
和b(A)
d(b,C)
在d(B,C)
时为真,但仅适用于您遇到的第一个a(B)
,不要在下面查找任何其他a(B)
定义d(B,C)
我希望这至少有一点帮助。因为我没有立即理解您对剪切操作的解释,所以我查看了您的代码。我的阅读大致如下:
1. Kills off the parent choice-point
2. Commits to all the choices made going through the rule
在以下情况下为真:c(A,B,c)
和a(a)
d(B,C)
- 或
和b(A)
d(b,C)
在d(B,C)
时为真,但仅适用于您遇到的第一个a(B)
,不要在下面查找任何其他a(B)
定义d(B,C)
我希望这至少有点帮助。我读了一些书,剪辑如下:
1. Kills off the parent choice-point
2. Commits to all the choices made going through the rule
因此:
1. d(B,_) :- b(B). is not explored
2. B in d(B,C) :- a(B),!,a(C). is irrevocably bound to 1.
我读了一些书,剪辑如下:
1. Kills off the parent choice-point
2. Commits to all the choices made going through the rule
因此:
1. d(B,_) :- b(B). is not explored
2. B in d(B,C) :- a(B),!,a(C). is irrevocably bound to 1.