Courses/CS 460/Fall 2005/SolveAll/original SolveAll

From CSWiki

Jump to: navigation, search
%%%%%%%%%%%%%%%%%%%%%%%%%%% Chapter 12 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 

declare

% This is the Solve operation, which returns a lazy list of solutions
% to a relational program.  The list is ordered according to a
% depth-first traversal.  Solve is written using the computation 
% space operations of the Space module.  

fun {Solve Script}
   {SolStep {Space.new Script} nil} 
end
fun {SolStep S Rest}
   case {Space.ask S} of
      failed then Rest  
   [] succeeded then {Space.merge S}|Rest
   [] alternatives(N) then {SolLoop S 1 N Rest}
   end
end 
fun lazy {SolLoop S I N Rest}
   if I > N then Rest
   elseif I == N then
      {Space.commit S I}
      {SolStep S Rest}
   else Right C in
      Right = {SolLoop S I+1 N Rest}
      C = {Space.clone S}
      {Space.commit C I}
      {SolStep C Right}
   end
end
fun {SolveOne F}
   case {Solve F} of
      nil then nil
   [] Sol | _ then Sol 
   end
end 
fun {TouchAll L}
   case L of
      nil then skip
   [] _ | Rest then {TouchAll Rest _}
   end
   L
end
fun {SolveAll F} {TouchAll {Solve F}} end
%%%%%%%%%%%%%%%%%%%%%%%%%%% Digits %%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 

fun {Digit} 
   choice 0 [] 1 [] 2 [] 3 [] 4 [] 5 [] 6 [] 7 [] 8 [] 9 end
end
{Browse {SolveAll Digit}}