Defining CHR constraints at runtime

空扰寡人 提交于 2021-02-07 14:50:09

问题


I'm trying to write a program that generates new constraints at runtime in SWI-Prolog. is_true([A,means,B]) is intended to generate another constraint at runtime:

:- 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]).

But when I type these queries, the is_true constraint seems to have no effect. is_true([something, is, not, false]) does not return true:

?- is_true([something,is,true]).
true .

?- is_true([something,is,not,false]).
is_true([something, is, not, false]).

Asserting a constraint in the console seems to have no effect, either:

?- asserta(is_true(A>B)==>(is_true(B<A),writeln("asserted"))).
true.

?- is_true(4>3).
is_true(4>3).

Is there another way to define new CHR constraints at runtime?


回答1:


It is possible to work around this problem by defining an is_true/2 predicate. This predicate can be changed at runtime using the assertz/1 predicate. This isn't an ideal solution, but it works in this particular case.

Now I can write the program like this:

:- use_module(library(chr)).
:- chr_constraint is_true/1.

is_true(A) ==> is_true(A,B) | is_true(B).
is_true([A,means,B]) ==> assertz(is_true(A,B)).
is_true([],[]).

and add new constraints at runtime in this way:

̀?- is_true([[A,implies,B],means,[A,means,B]]).
is_true([[A, implies, B], means, [A, means, B]]).

?- is_true([A>B,implies,B<A]).
is_true([A>B, means, B<A]),
is_true([A>B, implies, B<A]).

?- is_true(A>B).
is_true(B<A),
is_true(A>B).

Instead, it might be more useful to write a "meta-interpreter" for CHR:

:- initialization(main).
:- set_prolog_flag('double_quotes','chars').
:- use_module(library(chr)).
:- chr_constraint is_true/1.

is_true(X) \ is_true(X) <=> true.
is_true(X ==> Y),is_true(X1) ==> copy_term((X -> Y),(X2 -> Y1)),(X2=X1,(is_true(X1)->is_true(Y1));X2\=X1),writeln(Y).
is_true((X;Y)) ==> is_true(X);is_true(Y).
is_true((X,Y)) ==> is_true(X),is_true(Y).
is_true(is_true(X)) ==> is_true(X).
is_true(X),is_true(\+X) ==> false.


main :-
    is_true(is_person(A)==>is_mammal(A)),
    is_true(is_mammal(A)==>is_animal(A)),
    is_true(is_person(sue)),is_true(is_person(bob)).


来源:https://stackoverflow.com/questions/45655705/defining-chr-constraints-at-runtime

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!