2017-08-12 82 views
4

我正試圖編寫一個程序,該程序在運行時在SWI-Prolog中生成新約束。 is_true([A,means,B])意在運行時產生另一個約束:在運行時定義CHR約束

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

但是,當我鍵入這些查詢,該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]). 

斷言在控制檯的約束似乎沒有任何效果,或者:

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

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

有另一種方式在運行時定義新CHR限制?

回答

0

可以通過定義一個is_true/2謂詞來解決此問題。該謂詞可以在運行時使用assertz/1謂詞進行更改。這不是一個理想的解決方案,但它適用於這種特殊情況。

現在我可以寫這樣的程序:

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

,並在運行時以這種方式添加新的約束:

̀?- 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).