Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Defining CHR constraints at runtime

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?

like image 799
Anderson Green Avatar asked Nov 08 '22 18:11

Anderson Green


1 Answers

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)).
like image 180
Anderson Green Avatar answered Nov 28 '22 11:11

Anderson Green