Re: Delayed goals

From: Joachim Schimpf <j.schimpf_at_icparc.ic.ac.uk>
Date: Wed 16 Feb 2005 02:14:16 PM GMT
Message-ID: <42135538.2030105@icparc.ic.ac.uk>
Tom Crowley wrote:
> Can you give me some advice on the following....?
> 
> :- use_module(library(fd)).
> :- use_module(library(lists)).
> 
> roman(N, Solns) :-
>      dim(Square, [N,N]),
>      Square[1..N,1..N] :: 1..N,
>      (for(I,1,N), param(Square,N) do
>          Row is Square[I,1..N],
>          alldifferent(Row),
>          Col is Square[1..N,I],
>          alldifferent(Col)
>      ),
>      Vars is Square[1..N,1..N],
>      flatten(Vars, List),
>      findall(_, labeling(List), As),
>      length(As, Solns).
> 
> My program above solves the general problem of roman squares and
> produces the correct answer for all tested 1..5 size squares.
> However, when run Eclipse also indicates 'There are [X] delayed
> goals', where X is proportional to  the size of the square.
> I tried labeling results individually but these appear blank.
> 
> I have printed out Square, Row, Col, Vars and List as each is
> created and all seem to be working over the same uninstantiated
> variables, which coupled  with the fact that the solutions
> returned are correct indicate that the constraints are working
> as intended. Is there any reason why the constraints are being
> adhered to but remain delayed after all solutions have been found?

For the same reason why X is uninstantiated at the end of the query

?- findall(X, (X=3;X=4), L).
X = X
L = [3, 4]
Yes (0.00s cpu)

i.e. after a findall everything is exactly as it was before the
findall (except that the result list has been computed, of course).
Everything that happened inside the findall is undone.

In your case, this means that the labeling is undone and you are
in the same situation that you had immediatedly after constraint
setup, i.e. you have uninstantiated variables, and constraints
between them (manifested as delayed goals). The constraints only
become redundant (and the delayed goals disappear) when the
variables are instantiated and the constraints are known to be
true.

You could restructure your code as follows:

% model the problem, i.e. set up variables and constraints
model(N, List) :-
      dim(Square, [N,N]),
      Square[1..N,1..N] :: 1..N,
      (for(I,1,N), param(Square,N) do
          Row is Square[I,1..N],
          alldifferent(Row),
          Col is Square[1..N,I],
          alldifferent(Col)
      ),
      Vars is Square[1..N,1..N],
      flatten(Vars, List).

% model and solve by doing search,
% nondeterministally compute all solutions
solve(N, List) :-
      model(N, List),
      labeling(List).

% compute and count all solutions
all(N, Solns) :-
      findall(_, solve(N, _), As),
      length(As, Solns).


-- 
  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 Feb 16 14:15:10 2005

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