Re: [eclipse-clp-users] On implementation of post_goal and EC_resume

From: Joachim Schimpf <joachim.schimpf_at_infotech.monash.edu.au>
Date: Tue, 23 Jun 2009 00:24:53 +1000
Zhang, Y wrote:
> 
> We are working on answering a sequence of goals incrementally.
> We noticed eclipse provides "post_goal" in its interface for C++.
> I'd like to know how eclipse process the continuously added goals 
> (incrementally)?

Yes, it's incremental.  As long as your posted goals succeed,
additional posted goals get appended to the resolvent (so they
form a logical conjunction), you can imagine something like:

?- get_posted_goals(G1), call(G1), get_posted_goals(G2), call(G2), ...

You can find some further discussion here
http://eclipse-clp.org/archive/eclipse-users/0704.html
http://eclipse-clp.org/doc/bips/kernel/externals/yield-2.html


> 
> I also have a programming problem. I write this piece of code:
> 
> ---
> 	...
>     ec_exec_string("compile('test.pl')",0);
>  
>     post_goal(term(EC_functor("q",1),X));
>     if (EC_resume(Start)==EC_succeed){
>         ((EC_word)X).is_atom(&r1);
>         cout << "succeed" << " X=" << r1.Name() << endl;
>     }
>     else cout << "failed q(X)" << endl;
>  
>     post_goal(term(EC_functor("q", 2), X, Y));
>     if (EC_resume(Start)==EC_succeed) {                                   
>       cout << "succeed"; 
>       if (EC_succeed == ((EC_word)X).is_atom(&r1))  
>         cout << " X=" << r1.Name() << " ";
>     
>       if (EC_succeed == ((EC_word)Y).is_atom(&r2)) {
>         cout << " Y=" << r2.Name() << endl;
>       }
>       else cout << "bad Y " << endl;
>     }
>     else
>         cout << "failed q(X,Y)" << endl;
> 	...
> ---
> --- test.pl ---
> q(a).
> q(b).
> q(b,c).
> --- end of test.pl ---
> 
> I got this output
> 
> --
> succeed X=a
> succeed X=b bad Y 
> succeed X=b
> succeed X=basf Y=c
> ---
> 
> In fact, I expect line 2 to be "succeed X=b Y=c". Any comments on
> what's wrong about the program? Thank you.

In the following I have marked the "current execution point" with a ^.
You first post p(X):

?- p(X).
    ^
after resuming, you get the first solution X=a, so effectively the
resolvent now looks like:

?- p(a).
        ^
now you post q(a,Y):

?- p(a), q(a,Y).
          ^
but after resuming, p(a,Y) fails, which means we backtrack to p(X)
which succeeds with the second solution X=b:

?- p(b).
        ^
But you see that the posted q(a,Y) goal was lost during backtracking
because it was posted only conditionally for the first solution of p(X).

To achieve the result you expected, you have to post both goals together
at the beginning, and only resume once.


-- Joachim
Received on Mon Jun 22 2009 - 14:25:00 CEST

This archive was generated by hypermail 2.2.0 : Thu Feb 02 2012 - 02:31:58 CET