next up previous contents index
Next: 11.6.4 Answer Combination Up: 11.6.3 Answer Projection Previous: 11.6.3 Answer Projection   Contents   Index

11.6.3.0.1 Example

Take for example a predicate p/1 with a less than or equal constraint leq/2 on variables and integers. The predicate p/1 has local variables, but when p returns we are not interested in any constraints involving local variables. Hence we project on the argument of p/1 with a project constraint as follows:

:- import memberchk/2 from lists.

:- import merge_answer_store/1, 
          get_chr_store/1,
          set_chr_store/1,
          get_chr_answer_store/2
   from chr.

:- table tabled_p/2.

:- constraints leq/2, project/1.

... /* other CHR rules */
project(L) \ leq(X,Y) <=>
        ( var(X), \+ memberchk(X,L) 
        ; var(Y), \+ memberchk(Y,L)
        ) | true.
       
project(_) <=> true. 
        
p(X) :-
        tabled_p(X1,AnswerStore),
        merge_answer_store(AnswerStore),
        X1 = X.

tabled_p(X,AnswerStore) :-
        get_chr_store(CallStore),
        set_chr_store(_EmptyStore)
        orig_p(X),
        project([X]),
        get_chr_answer_store(chrmod,AnswerStore),
        set_chr_store(CallStore).
        
orig_p(X) :-
   ... /* original body of p/1 */.

The example in the following subsection shows projection in a full application.



Terrance Swift 2007-10-06