我正在尝试编写一个在SWI-Prolog中在运行时生成新约束的程序.is_true([A,means,B])旨在在运行时生成另一个约束:
:- use_module(library(chr)).
:- chr_constraint is_true/1.
is_true([A,means,B]) ==> (is_true(A) ==> is_true(B),writeln('asserted')).
is_true([[A,is,true],means,[A,is,not,false]]).
is_true([something,is,true]).
Run Code Online (Sandbox Code Playgroud)
但是当我输入这些查询时,is_true约束似乎没有效果.is_true([something, is, not, false])不归还true:
?- is_true([something,is,true]).
true .
?- is_true([something,is,not,false]).
is_true([something, is, not, false]).
Run Code Online (Sandbox Code Playgroud)
在控制台中断言约束似乎没有任何影响:
?- asserta(is_true(A>B)==>(is_true(B<A),writeln("asserted"))).
true.
?- is_true(4>3).
is_true(4>3).
Run Code Online (Sandbox Code Playgroud)
还有另一种在运行时定义新CHR约束的方法吗?
我正在仔细研究约束处理规则(CHR),看看我是否能理解它们(从某种意义上说,这里计算的是什么以及经典逻辑甚至线性逻辑如何适合于此)并可能应用它们。
Thom Frühwirth 2009 年的书讨论了 CHR 的原则,但实施当然可能有所不同。
在这种情况下,我使用CHR的SWI Prolog 实现。
如果我理解得很好:
作为练习,使用欧几里德算法计算 GCD 并保留操作日志的最简单程序:
% Constraint `logg(Ti,Msg)` retains the log message `Msg` at step `Ti`
% (which increases monotonically)
% Constraint `gcdpool(X)` denotes a "GCD pool member". At each step, we want
% to compute the GCD of all the X for which …Run Code Online (Sandbox Code Playgroud)