users-prolog
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: where fd constraints go


From: Daniel Diaz
Subject: Re: where fd constraints go
Date: Tue, 24 Feb 2004 12:59:37 +0100
User-agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.6) Gecko/20040116

Hi,

Hak-Jin Kim wrote:
Hello,

For fd programming, I wonder where fd constraints for problem representation
go.
According to a document, all constraints are entailed to constraint of the
form, X in r, and the constraint store keeps such constraints.  Then where
original constraints go?  They go to 'global stack'?

They are stored in the "constaint stack" (type statististics. to see its size).

Also I like to put a different type of constraint such as
 'fd_constraint --> call an imperative function'. Here the fd_constraint is
not affected by consistency checking.  What I need is to check the truth
value.  That is, if the antecedent has true, the imperative function in the
consequence should be executed; otherwise, it is ignored.  Can this kind be
constructed in the current gprolog fd constraint system?

Yes, in the current version you need to write a bit of FD language... which is not (yet) documented. So I propose you a fragment of code that you could easily adapt.

The idea is to set a constraint "<your constraint to test> #<=> B" which sets B (a FD boolean variable) which the truth value of "your constraint". Then add a constraint which waits until B is ground to 1) test it and 2) if it is 1 execute your C function.

The file t.pl is the prolog part. The predicate t/0 tells a constraint X+Y#=20 and wants to execute a C code if X#>=5. For this it calls a FD constraint trigger_fct(B) which will call a C function when B is instantiate (this function calls your C code if B==1). Then the predicate t/0 calls a backtrackable predicate q(Y,B) which refines the the domain of X modifying Y. The 1st clause sets the truth value to true, the 2nd leaves it unchanged (undecided), the 3rd sets it to false.

The FD file defines the constraint which simply invokes the C function C_Trigger when B is instantiated.

The C file defines C_Trigger to invoke your code if B==1.

You find these 3 files as attachments.

Compile them with:
$ gplc t.pl t_fd.fd t_c.c

execute ./t and inside the top-level the predicate t/0. Ask for alternative solutions with ;
You should obtain:

$ ./t
GNU Prolog 1.2.18
By Daniel Diaz
Copyright (C) 1999-2004 Daniel Diaz
| ?- t.
prolog: fct not yet triggered
C: test trigger b: 1
C: Trigger Fct
prolog: B:1 fct should have been triggered by C code


true ? ;
prolog: B:_#81(0..1) C code should not yet be executed


true ? ;
C: test trigger b: 0
C: do not Trigger Fct
prolog: B:0 fct should NOT have been triggered by C code


yes

Thirdly, is there any detailed document about the user defined fd constraint
using fd2c?
> Any comment will be helpful to me.  Thanks in advance.

As I said no. But you can look at the code of the FD Built-in predicates in the source distribution. Look at: src/BipsFD/*_fd.fd (for the FD part) and *_c.c for the C part (called by the FD part).

Also have a look at src/Fd2C/FD_SYNTAX (which is intentionnally for me and thus only recall me the BNF syntax of the FD language).


--
===============================================
                 Daniel Diaz
University of Paris 1      INRIA Rocquencourt
75013 Paris FRANCE      78153 Le Chesnay FRANCE
        web: http://pauillac.inria.fr/~diaz
        email: address@hidden

--
Ce message a subi une analyse antivirus par MailScanner ; il est vraisemblablement
sans danger.

t:-
        X + Y #= 20,
        B #<=> X #>= 5,
        trigger_fct(B),
        write('prolog: fct not yet triggered'), nl,
        q(Y,B).

q(Y,B):-
        Y #=< 15,
        format('prolog: B:~w fct should have been triggered by C code~n', [B]).

q(Y,B):-
        Y #>= 10,
        format('prolog: B:~w C code should not yet be executed~n', [B]).

q(Y,B):-
        Y #>= 16,
        format('prolog: B:~w fct should NOT have been triggered by C code~n', 
[B]).


trigger_fct(B) :-
        fd_tell(trigger_fct(B)).
%{
Bool C_Trigger(int b);
%}

trigger_fct(fdv B)
{
 start C_Trigger(val(B))
}
#include <stdio.h>
#include "gprolog.h"

Bool C_Trigger(int b)
{
  printf("C: test trigger b: %d\n", b);
  if (b)
    printf("C: Trigger Fct\n");
  else
    printf("C: do not Trigger Fct\n");

  return TRUE;
}

reply via email to

[Prev in Thread] Current Thread [Next in Thread]