我使用Constraint Handling Rules在SWI-Prolog中编写了一组简单的约束。它使用两个相对简单的推理规则:
%If A means B, then B means A.
means(A,B) ==> means(B,A).
%If A means B and A means C, then B means C.
means(A,B),means(A,C) ==> means(B,C).
我希望means([3,is,equal,to,4],[3,equals,4])
为true
,但它似乎会导致无限递归:
:- use_module(library(chr)).
:- chr_constraint means/2.
:- initialization(main).
means([A,equals,B],[A,'=',B]).
means([A,is,equal,to,B],[A,'=',B]).
means([A,equals,B],[A,and,B,are,equal]).
%These are the rules of inference for this program.
%If A means B, then B means A.
means(A,B) ==> means(B,A).
%If A means B and A means C, then B means C.
means(A,B),means(A,C) ==> means(B,C).
main :-
%This part works as expected. X = [3,'=',4].
means([3,is,equal,to,4],X),writeln(X),
%This statement should be true, so why does it produce an infinite recursion?
means([3,is,equal,to,4],[3,and,4,are,equal]).
我在此程序中添加了一个简单规则,但它仍然导致Out of local stack
错误:
:- use_module(library(chr)).
:- chr_constraint means/2.
:- initialization(main).
%These are the rules of inference for this program.
%If A means B, then B means A.
means(A,B) ==> means(B,A).
%If A means B and A means C, then B means C.
means(A,B),means(A,C) ==> means(B,C).
means(A,B) \ means(A,B) <=> true.
means(A,A) <=> true.
means([A,equals,B],[A,'=',B]).
means([A,is,equal,to,B],[A,'=',B]).
means([A,equals,B],[A,and,B,are,equal]).
main :-
%This part works as expected. X = [3,'=',4].
means([3,is,equal,to,4],X),writeln(X),
%This statement should be true, so why does it produce an infinite recursion?
means([3,is,equal,to,4],[3,and,4,are,equal]).
是否可以重写推理规则,以便它们不会产生无限递归?
答案 0 :(得分:3)
请阅读可用的CHR文献,了解有关CHR的这些方面的更多详细信息。
例如,Tips for CHR programming包含在编程提示:
中
- 设置语义
醇>CHR系统允许存在相同的约束,即具有多个约束 同样的算符,arity和论点。对于大多数约束求解器,这是不可取的:它会影响 效率和可能终止。因此应该添加适当的简化规则 表格:
constraint \ constraint <=> true
因此,如果添加CHR simpagation规则,您的示例将按预期工作:
<强> means(A,B) \ means(A,B) <=> true.
强>
示例查询和结果:
?- means([3,is,equal,to,4],[3,and,4,are,equal]). means([3, and, 4, are, equal], [3, is, equal, to, 4]), means([3, is, equal, to, 4], [3, and, 4, are, equal]).