Re: n-queens version in c

From: Joachim Schimpf <j.schimpf_at_icparc.ic.ac.uk>
Date: Wed 18 Aug 2004 09:51:37 AM GMT
Message-ID: <412326A9.8090504@icparc.ic.ac.uk>
masdeval wrote:
> 
> This code really needs some effort to read but i did it only to
> test the interface. Unfortunately the application i have worked
> needs to use Eclipse exactly like that, i.e., creating terms
> variables to all predicates, concatenating  each other with ,/2 ,
> declaring all logic variables, and so on.

ECLiPSe gives you a high-level programming language to make things
easier for you (there are other constraint packages where you have to
do all the programming in C++, which is less elegant, but ok if the
package is designed for this purpose).

But when you use C++ to construct an ECLiPSe program, you are really
trying very hard to combine the disadvantages of both approaches!
So I would really urge you to think again - you will save yourself
a lot of time and effort.

There is no reason why you would want to transfer the static part of
your application (in this case the code of the queens_lists/2 predicate)
through the C++ interface in this way. Just put the ECLiPSe code in a
separate file and post a compile/1 goal from the C++ interface.

Once you have compiled the ECLiPSe code, you can run it by posting different
calls to queens_lists/2 (goals). This makes sense because you probably want
to pass parameters (e.g. N) from C++ to ECLiPSe, and results (e.g. Board)
back from ECLiPSe to C++.

As a general rule, the goals you post via the C++ interface should be the
same that you would type into the ECLiPSe-prompt in an interactive session.

So your C++ code would simply be


     EC_init();

     post_goal(term(EC_functor("compile",1), "queens.ecl"));
     if(EC_resume() != EC_succeed)
     {
         printf("Compilation problem");
	exit(-1);
     }

     EC_ref aux;
     aux = newvar();
     post_goal(term(EC_functor("queens_lists",2),4,aux));
     if(EC_resume() == EC_succeed)
         printf(" Success ");


and the queens-code is in a separate file queens.ecl.

If you follow this advice, you can ignore the rest of this mail :-)


>  
> The code in C below try to translate the code in eclipse. When it
> runs there are no error message but neither the "Success" (last line) 
> message
> is printed, so the EC_resume() returns fail.

I can see the following mistakes:


>    //fromto(Board, [Q1|Cols], Cols, [])
>    EC_word term2 = term(EC_functor(" fromto ",4), Board , list( Q1 , 
> list( Cols ,nil()) ) , Cols , nil() );

The list [Q1|Cols] must be constructed simply as list(Q1, Cols).
What you have constructed is [Q1,Cols].


> 
>    //foreach(Q2, Cols), param(Q1), count(Dist,1,_)
>    EC_word term3 = term(EC_functor(" , ", 2), term(EC_functor(" foreach 
> ", 2), Q2 , Cols ), term(EC_functor(" param ", 1), Q1 ) );
>    EC_word term4 = term(EC_functor(" , ", 2), term3, term(EC_functor(" 
> count ", 3), Dist , 1 , Underline ));


When constructing a comma-sequence like  a,b,c,d  you have to be aware that the
comma is right-associative i.e.  a,(b,(c,d))  so you need to write something like

EC_word sequence =
     term(EC_functor(",", 2),
         a,
         term(EC_functor(",", 2),
             b,
             term(EC_functor(",", 2),
                  c,
                  d)));

Also, make sure you don't have extra spaces in your functor-strings!



>    post_goal( term( EC_functor(" assert ",1), term(EC_functor(" :- 
> ",2),  term(EC_functor(" queens_lists ", 2), N, Board ), body )));

Don't use assert/1, use compile_term/1.



-- 
  Joachim Schimpf              /             phone: +44 20 7594 8187
  IC-Parc                     /      mailto:J.Schimpf@imperial.ac.uk
  Imperial College London    /    http://www.icparc.ic.ac.uk/eclipse
Received on Wed Aug 18 10:53:53 2004

This archive was generated by hypermail 2.1.8 : Wed 16 Nov 2005 06:07:30 PM GMT GMT