In Prolog we talk usually about universal and existential termination. Universal termination being the more relevant. A goal G_0
terminates universally, iff G_0, false
fails finitely. So let s consider the following failure-slice of your program:
:- p(f(X),f(f(Y)),f(f(b))), false.
p(f(a),f(X),f(f(X))) :- false.
p(f(a),f(a),f(f(X))) :- false.
p(X,f(f(Y)),X) :- false, p(Y,f(f(Y)),f(f(X))).
p(f(X),Y,Z) :- p(X,f(Y),f(f(Z))), false.
If this failure slice does not terminate, then also the entire program does not terminate. This is one of the desirable properties of pure Prolog programs.
显然,<代码>Y和Z
对终止都没有任何影响。 两者(在这一失败的案例中)都是中性的终止。 因此,如果方案受到约束(明确和地面),仍然首先导致终止方案。 仅作一推,第一个<代码>f(X)被删除,一个(别名)变量仍保持不变。 因此,这项方案(有问题)并不普遍终止。 iii
Now for existential termination. That is, while the program does not terminate universally it may still find a solution. Think of a query like
?- length(L,N).
L = [], N = 0
; L = [_A], N = 1
; L = [_A,_B], N = 2
; L = [_A,_B,_C], N = 3
; ... .
?- length(L,N), false.
loops.
which does not terminate universally, but still produces useful answers and thus terminates existentially.
传统的终止往往有捷径: 如果没有解决办法,方案不会普遍终止,那么,方案也不存在成因终止。 这将是一场快枪! 这里的情况并非如此。 一种解决办法是:
?- p(f(f(a)),f(f(b)),f(f(b))).
true
; false.
并且还有很多。 因此,该方案是否将找到解决办法,仍然开放。
现在,这是一件奇怪的事情: 第3条没有目的(非终止除外)。 我注意到这一点,增加了一个外围参数,使循环深化:
% p([n],_,_,_).
p([a],f(a),f(X),f(f(X))).
p([b],f(a),f(a),f(f(X))).
p([c|L],X,f(f(Y)),X) :- p(L,Y,f(f(Y)),f(f(X))).
p([d|L],f(X),Y,Z) :- p(L,X,f(Y),f(f(Z))).
?- length(L,N), p(L, A1,A2,f(f(b))).
L = "a", N = 1, A1 = f(a), A2 = f(b)
; L = "b", N = 1, A1 = f(a), A2 = f(a)
; L = "da", N = 2, A1 = f(f(a)), A2 = f(f(b))
; L = "db", N = 2, A1 = f(f(a)), A2 = a
; L = "dda", N = 3, A1 = f(f(f(a))), A2 = f(f(f(b)))
; L = "ddda", N = 4, A1 = f(f(f(f(a)))), A2 = f(f(f(f(b))))
; L = "dddda", N = 5, A1 = f(f(f(f(f(a))))), A2 = f(f(f(f(f(b)))))
; L = "ddddda", N = 6, A1 = f(f(f(f(f(f(a)))))), A2 = f(f(f(f(f(f(b))))))
; L = "dddddda", N = 7, A1 = f(f(f(f(f(f(f(a))))))), A2 = f(f(f(f(f(f(f(b)))))))
; ... .
So when using the third rule initially once, there are no solutions to be found, and the program will not terminate. Thus proving that this part will find no solutions is tantamount to proving that the goal p(X,f(f(X)),f(f(f(f(b)))))
has no solution.
I have not done this part, but it seems that the argument can be narrowed down to the observation that the first argument will always be of the form f*a
, but the second of the form f*b
and thus there cannot be a solution.