Re: n-queens version in c

From: masdeval <masdeval_at_yahoo.com.br>
Date: Tue 17 Aug 2004 07:54:36 PM GMT
Message-ID: <002401c48495$22a10cf0$2806520a@cbraz>
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.

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 will try to explain the code better so someone who has
experience in use the C++ Eclipse's interface could copy/paste it
and execute. In red are the predicates functors and in blue the
variables.

Thanks again,

Christian


     /* n-queens */

:- lib(ic)
queens_lists(N, Board) :- 
length(Board, N),   Board :: 1..N,  
( fromto(Board, [Q1|Cols], Cols, [])  do   
    ( foreach(Q2, Cols), param(Q1), count(Dist,1,_) do
   Q2 #\= Q1,
   Q2 - Q1 #\= Dist,
   Q1 - Q2 #\= Dist
     )
 ), labeling(Board).  
 
    / * C++ version */ 

  EC_init();
    //Here i create all logic variables like the eclipse code 
  EC_ref Board, N;
  EC_word Q1,Cols,Q2,Dist, Underline;
  Q1 = newvar();
  Cols = newvar();
  Q2 = newvar();
  Dist = newvar();
  Underline = newvar();
 
  post_goal("lib(ic)");

  //length(Board, N), Board :: 1..N
  EC_word term1 = term(EC_functor(",", 2), term(EC_functor("length", 2), Board, N), term(EC_functor("::", 2), Board, term(EC_functor("..", 2),1, N)));
 
   //fromto(Board, [Q1|Cols], Cols, [])
   EC_word term2 = term(EC_functor("fromto",4), Board , list(Q1, list(Cols,nil()) ) , Cols, nil() );

   //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 ));

   //Q2 # \ = Q1, Q2 - Q1 # \ = Dist,Q1 - Q2 # \=  Dist
   EC_word term5 = term(EC_functor(",", 2), term(EC_functor("# \ \=",2) , Q2 , Q1) , term(EC_functor("# \ \=" , 2), term(EC_functor("-", 2) , Q2 , Q1 ), Dist) );
   EC_word term6 = term(EC_functor(",", 2), term5, term(EC_functor("# \ \ =",2), term(EC_functor("-", 2) , Q1 , Q2), Dist ) );

  // second do 
   EC_word term7 = term(EC_functor("do", 2), term4 , term6);

   //first do 
   EC_word term8 = term(EC_functor("do", 2),  term2,  term7);
   
  //labeling
  EC_word term9 = term(EC_functor("labeling", 1), Board);
   
   EC_word term10 = term(EC_functor(",", 2), term1, term8);
   EC_word term11 = term(EC_functor(",", 2), term10, term9 );
 
   EC_word body = term( EC_functor("," , 2), term11, term(EC_functor("writeln",1) , Board ));

  //Submit the goal
   post_goal( term( EC_functor("assert",1), term(EC_functor(":-",2),  term(EC_functor("queens_lists", 2), N, Board), body )));
 
   EC_ref aux;
   aux = newvar();

   //Submit the query
   post_goal(term(EC_functor("queens_lists", 2 ), 4,  aux));
 
   if(EC_resume() == EC_succeed)
     printf(" Success ");

----- Original Message ----- 
From: <wh@icparc.ic.ac.uk>
To: "masdeval" <masdeval@yahoo.com.br>
Cc: <eclipse-users@icparc.ic.ac.uk>
Sent: Tuesday, August 17, 2004 1:50 PM
Subject: Re: [eclipse-users] n-queens version in c


> Hi Christian,
> 
> May I ask why you want to do this?  I haven't looked at why it doesn't work:
> it's extremely difficult to read - I'd hate to try to maintain code like
> this!
> 
> I suggest that if you want to use ECLiPSe's control structures (do loops,
> if-then-else, etc.), write your program in ECLiPSe.  If you want to write
> your program in C++, then use C++'s control structures and just post the
> constraints to ECLiPSe.
> 
> Also, when requesting help, you can help others to help you if you provide
> more of an explanation than just "It didn't work".  In what way didn't it
> work?
> 
> Cheers,
> Warwick
> 
> On Tue, Aug 17, 2004 at 10:34:19AM -0300, masdeval wrote:
> > 
> >     Friends, 
> > 
> >     i'am trying to codify the n-queens problem like it appears in the eclipse home page using the eclipse c++ interface (see below). Someone could see what's wrong? It didn't work but to me is everything correct.
> > 
> >     Thanks, Christian
> > 
> >     /* n-queens */
> >   /*queens_lists(N, Board) :-
> > 
> >  length(Board, N),
> >  Board :: 1..N,
> >  ( fromto(Board, [Q1|Cols], Cols, []) do
> >      ( foreach(Q2, Cols), param(Q1), count(Dist,1,_) do
> >   Q2 #\= Q1,
> >   Q2 - Q1 #\= Dist,
> >   Q1 - Q2 #\= Dist
> >      )
> >  ), labeling(Board).  */
> > 
> > /* C++ version */ 
> > 
> >   EC_ref Board, N;
> >   EC_word Q1,Cols,Q2,Dist, Underline;
> >   Q1 = newvar();
> >   Cols = newvar();
> >   Q2 = newvar();
> >   Dist = newvar();
> >   Underline = newvar();
> > 
> >   EC_word wlist = nil();
> >   wlist = list(Cols,wlist);
> >   wlist = list(Q1,wlist);
> > 
> >   post_goal("lib(ic)");
> >   //length(Board, N), Board :: 1..N,
> >   EC_word term1 = term(EC_functor(",",2), term(EC_functor("length",2),Board,N), term(EC_functor("::",2),Board, term(EC_functor("..",2),1,N)));
> > 
> >   //fromto(Board, [Q1|Cols], Cols, [])
> >   EC_word term2 = term(EC_functor("fromto",4), Board, wlist, Cols, nil());
> > 
> >   //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));
> > 
> >   //Q2 #\= Q1, Q2 - Q1 #\= Dist,Q1 - Q2 #\= Dist
> >   EC_word term5 = term(EC_functor(",",2),term(EC_functor("#\\=",2),Q2,Q1) , term(EC_functor("#\\=",2), term(EC_functor("-",2),Q2,Q1),Dist) );
> >   EC_word term6 = term(EC_functor(",",2),term5, term(EC_functor("#\\=",2), term(EC_functor("-",2),Q1,Q2), Dist ) );
> > 
> >   // second do
> >   EC_word term7 = term(EC_functor("do",2),term4,term6);
> >   //first do
> >   EC_word term8 = term(EC_functor("do",2), term2, term7 );
> >   
> >  //labeling
> >   EC_word term9 = term(EC_functor("labeling",1),Board);
> >   
> >   EC_word term10 = term(EC_functor(",",2),term1,term8);
> >   EC_word term11 = term(EC_functor(",",2),term10, term9 );
> > 
> >   EC_word body = term( EC_functor(",",2), term11, term(EC_functor("writeln",1),Board ));
> >    
> >   post_goal( term( EC_functor("assert",1), term(EC_functor(":-",2),  term(EC_functor("queens_lists",2),N, Board), body )));
> > 
> >   EC_ref aux;
> >   aux = newvar();
> >   post_goal(term(EC_functor("queens_lists",2),4,aux));
> > 
> >   if(EC_resume() == EC_succeed)
> >     printf(" Success ");
> >  
> >
Received on Tue Aug 17 21:55:15 2004

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