1

我使用约束处理规则在 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]).

我在这个程序中添加了一个 simpagation 规则,但它仍然导致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]).

是否可以重新编写推理规则,使它们不会产生无限递归?

4

1 回答 1

3

请阅读可用的 CHR 文献以获取有关 CHR 此类方面的更多详细信息。

例如,CHR 编程技巧包含在Programming Hints中:

  1. 设置语义

CHR 系统允许存在相同的约束,即具有相同函子、数量和参数的多个约束。对于大多数约束求解器来说,这是不可取的:它会影响效率并可能会终止。因此,应添加适当的 simpagation 规则,其形式为:constraint \ constraint <=> true

因此,如果您添加 CHR simpagation 规则,您的示例将按预期工作:

means(A,B) \ means(A,B) <=> true.

示例查询和结果:

?- 表示([3,is,equal,to,4],[3,and,4,are,equal])。
表示([3, and, 4, are, equal], [3, is, equal, to, 4]),
表示([3,等于,等于,4],[3,和,4,等于])。
于 2016-09-11T17:23:50.403 回答