首页 > 解决方案 > 我想用 Prolog 证明一些定理,但是,它总是返回“Out of global stack”

问题描述

我正在做证明代数群论的人工智能作业。

该定理可以表示如下:

A1. i(e,X) = X                   (identity)
A2. i(X, e) = X                  (identity)
A3. i(comp(X),X) = e             (complement)
A4. i(X, comp(X)) = e            (complement)
A5. i(X, i(Y,Z)) = i(i(X,Y),Z)   (associativity)

THEOREM: If G is a group such that for every X,
A6. i(X,X) = e,
then G is commutative, i.e., for every X; Y ,
i(X,Y) = i(Y,X):

and the commutative part can be represented as 
A7. i(a, b, c)                          clause derived from negated conclusion
A8. -i(b, a, c)                         clause derived from negated conclusion

我将它们转换为 Prolog 格式,如下所示:

% A7
i(a, b, c).
% A1
i(e, X, X) .
%A2
i(X, e, X).
% A3
i(comp(X), X, e).
% A4
i(X, comp(X), e).
% A51
i(U, Z, W) :- i(X, Y, U), i(Y, Z, V), i(X, V, W).
% A52
i(X, V, W) :- i(X, Y, U), i(Y, Z, V), i(U, Z, W).
% A6
i(X, X, e).

然后我想证明这个定理,所以我在 Prolog 控制台中输入了“i(b,a,c)”,我得到了以下错误消息:

?- i(b,a,c).
ERROR: Out of global-stack.
ERROR: No room for exception term.  Aborting.
ERROR: Out of global-stack.
ERROR: No room for exception term.  Aborting.
ERROR: Out of global-stack.
ERROR: No room for exception term.  Aborting.
% Execution Aborted

请帮帮我,非常感谢!

标签: prologswi-prologtheorem-proving

解决方案


A51 和 A52 子句是左递归的,这会导致堆栈外错误。在 Prolog 中处理左递归的典型解决方案是使用支持制表的系统(例如 XSB、YAP、SWI-Prolog、B-Prolog 或 Ciao)。但是您的代码中还有另一个问题。A3 和 A4 子句可能导致创建循环项。例如,仅加载子句 A3:

?- i(X, X, Y), cyclic_term(X).
X = comp(X),
Y = e.

如果您注释掉 A3 和 A4 子句并在源文件的顶部添加指令:

:- table(i/3).

你会得到:

?- i(b,a,c).
true.

推荐阅读