src/Pure/search.ML
changeset 1588 fff3738830f5
child 2143 093bbe6d333b
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/Pure/search.ML	Mon Mar 18 13:42:35 1996 +0100
     1.3 @@ -0,0 +1,251 @@
     1.4 +(*  Title: 	search
     1.5 +    ID:         $Id$
     1.6 +    Author: 	Lawrence C Paulson and Norbert Voelker
     1.7 +
     1.8 +Search tacticals
     1.9 +*)
    1.10 +
    1.11 +signature SEARCH =
    1.12 +  sig
    1.13 +  val trace_DEPTH_FIRST	: bool ref
    1.14 +  val DEPTH_FIRST	: (thm -> bool) -> tactic -> tactic
    1.15 +  val DEPTH_SOLVE	: tactic -> tactic
    1.16 +  val DEPTH_SOLVE_1	: tactic -> tactic
    1.17 +  val ITER_DEEPEN	: (thm->bool) -> (int->tactic) -> tactic
    1.18 +  val THEN_ITER_DEEPEN	: tactic -> (thm->bool) -> (int->tactic) -> tactic
    1.19 +
    1.20 +  val has_fewer_prems	: int -> thm -> bool   
    1.21 +  val IF_UNSOLVED	: tactic -> tactic
    1.22 +  val trace_BEST_FIRST	: bool ref
    1.23 +  val BEST_FIRST	: (thm -> bool) * (thm -> int) -> tactic -> tactic
    1.24 +  val THEN_BEST_FIRST	: tactic -> (thm->bool) * (thm->int) -> tactic
    1.25 +			  -> tactic
    1.26 +  val trace_ASTAR	: bool ref
    1.27 +  val ASTAR	        : (thm -> bool) * (int->thm->int) -> tactic -> tactic
    1.28 +  val THEN_ASTAR	: tactic -> (thm->bool) * (int->thm->int) -> tactic
    1.29 +			  -> tactic
    1.30 +  val BREADTH_FIRST	: (thm -> bool) -> tactic -> tactic
    1.31 +  end;
    1.32 +
    1.33 +structure Search : SEARCH = 
    1.34 +struct
    1.35 +
    1.36 +(**** Depth-first search ****)
    1.37 +
    1.38 +val trace_DEPTH_FIRST = ref false;
    1.39 +
    1.40 +(*Searches until "satp" reports proof tree as satisfied.
    1.41 +  Suppresses duplicate solutions to minimize search space.*)
    1.42 +fun DEPTH_FIRST satp tac = 
    1.43 + let val tac = tracify trace_DEPTH_FIRST tac
    1.44 +     fun depth used [] = None
    1.45 +       | depth used (q::qs) =
    1.46 +	  case Sequence.pull q of
    1.47 +	      None         => depth used qs
    1.48 +	    | Some(st,stq) => 
    1.49 +		if satp st andalso not (gen_mem eq_thm (st, used))
    1.50 +		then Some(st, Sequence.seqof
    1.51 +			         (fn()=> depth (st::used) (stq::qs)))
    1.52 +		else depth used (tac st :: stq :: qs)
    1.53 +  in  traced_tac (fn st => depth [] ([Sequence.single st]))  end;
    1.54 +
    1.55 +
    1.56 +
    1.57 +(*Predicate: Does the rule have fewer than n premises?*)
    1.58 +fun has_fewer_prems n rule = (nprems_of rule < n);
    1.59 +
    1.60 +(*Apply a tactic if subgoals remain, else do nothing.*)
    1.61 +val IF_UNSOLVED = COND (has_fewer_prems 1) all_tac;
    1.62 +
    1.63 +(*Tactical to reduce the number of premises by 1.
    1.64 +  If no subgoals then it must fail! *)
    1.65 +fun DEPTH_SOLVE_1 tac = STATE
    1.66 + (fn st => 
    1.67 +    (case nprems_of st of
    1.68 +	0 => no_tac
    1.69 +      | n => DEPTH_FIRST (has_fewer_prems n) tac));
    1.70 +
    1.71 +(*Uses depth-first search to solve ALL subgoals*)
    1.72 +val DEPTH_SOLVE = DEPTH_FIRST (has_fewer_prems 1);
    1.73 +
    1.74 +
    1.75 +
    1.76 +(**** Iterative deepening ****)
    1.77 +
    1.78 +fun has_vars (Var _) = true
    1.79 +  | has_vars (Abs (_,_,t)) = has_vars t
    1.80 +  | has_vars (f$t) = has_vars f orelse has_vars t
    1.81 +  | has_vars _ = false;
    1.82 +
    1.83 +(*Counting of primitive inferences is APPROXIMATE, as the step tactic
    1.84 +  may perform >1 inference*)
    1.85 +
    1.86 +(*Pruning of rigid ancestor to prevent backtracking*)
    1.87 +fun prune (new as (k', np':int, rgd', stq), qs) = 
    1.88 +    let fun prune_aux (qs, []) = new::qs
    1.89 +          | prune_aux (qs, (k,np,rgd,q)::rqs) =
    1.90 +	      if np'+1 = np andalso rgd then
    1.91 +		  (if !trace_DEPTH_FIRST then
    1.92 +		       writeln ("Pruning " ^ 
    1.93 +				string_of_int (1+length rqs) ^ " levels")
    1.94 +		   else ();
    1.95 +		   (*Use OLD k: zero-cost solution; see Stickel, p 365*)
    1.96 +		   (k, np', rgd', stq) :: qs)
    1.97 +	      else prune_aux ((k,np,rgd,q)::qs, rqs)
    1.98 +        fun take ([], rqs) = ([], rqs)
    1.99 +	  | take (arg as ((k,np,rgd,stq)::qs, rqs)) = 
   1.100 +	        if np' < np then take (qs, (k,np,rgd,stq)::rqs)
   1.101 +		            else arg
   1.102 +    in  prune_aux (take (qs, []))  end;
   1.103 +
   1.104 +
   1.105 +(*Depth-first iterative deepening search for a state that satisfies satp
   1.106 +  tactic tac0 sets up the initial goal queue, while tac1 searches it.
   1.107 +  The solution sequence is redundant: the cutoff heuristic makes it impossible
   1.108 +  to suppress solutions arising from earlier searches, as the accumulated cost
   1.109 +  (k) can be wrong.*)
   1.110 +fun THEN_ITER_DEEPEN tac0 satp tac1 = traced_tac (fn st => 
   1.111 + let val countr = ref 0
   1.112 +     and tf = tracify trace_DEPTH_FIRST (tac1 1)
   1.113 +     and qs0 = tac0 st
   1.114 +     (*bnd = depth bound; inc = estimate of increment required next*)
   1.115 +     fun depth (bnd,inc) [] = 
   1.116 +	     (writeln (string_of_int (!countr) ^ 
   1.117 +		       " inferences so far.  Searching to depth " ^ 
   1.118 +		       string_of_int bnd);
   1.119 +	      (*larger increments make it run slower for the hard problems*)
   1.120 +	      depth (bnd+inc, 10)) [(0, 1, false, qs0)]
   1.121 +       | depth (bnd,inc) ((k,np,rgd,q)::qs) =
   1.122 +	  if k>=bnd then depth (bnd,inc) qs
   1.123 +          else
   1.124 +	  case (countr := !countr+1;
   1.125 +		if !trace_DEPTH_FIRST then
   1.126 +		    writeln (string_of_int np ^ 
   1.127 +			     implode (map (fn _ => "*") qs))
   1.128 +		else ();
   1.129 +		Sequence.pull q) of
   1.130 +	     None         => depth (bnd,inc) qs
   1.131 +	   | Some(st,stq) => 
   1.132 +	       if satp st	(*solution!*)
   1.133 +	       then Some(st, Sequence.seqof
   1.134 +			 (fn()=> depth (bnd,inc) ((k,np,rgd,stq)::qs)))
   1.135 +
   1.136 +	       else 
   1.137 +               let val np' = nprems_of st
   1.138 +		     (*rgd' calculation assumes tactic operates on subgoal 1*)
   1.139 +                   val rgd' = not (has_vars (hd (prems_of st)))
   1.140 +                   val k' = k+np'-np+1  (*difference in # of subgoals, +1*)
   1.141 +               in  if k'+np' >= bnd 
   1.142 +		   then depth (bnd, min [inc, k'+np'+1-bnd]) qs
   1.143 +		   else if np' < np (*solved a subgoal; prune rigid ancestors*)
   1.144 +                   then depth (bnd,inc) 
   1.145 +		         (prune ((k', np', rgd', tf st), (k,np,rgd,stq) :: qs))
   1.146 +	           else depth (bnd,inc) ((k', np', rgd', tf st) :: 
   1.147 +					 (k,np,rgd,stq) :: qs)
   1.148 +	       end
   1.149 +  in depth (0,5) [] end);
   1.150 +
   1.151 +val ITER_DEEPEN = THEN_ITER_DEEPEN all_tac;
   1.152 +
   1.153 +
   1.154 +(*** Best-first search ***)
   1.155 +
   1.156 +val trace_BEST_FIRST = ref false;
   1.157 +
   1.158 +(*Insertion into priority queue of states *)
   1.159 +fun insert (nth: int*thm, []) = [nth]
   1.160 +  | insert ((m,th), (n,th')::nths) = 
   1.161 +      if  n<m then (n,th') :: insert ((m,th), nths)
   1.162 +      else if  n=m andalso eq_thm(th,th')
   1.163 +              then (n,th')::nths
   1.164 +              else (m,th)::(n,th')::nths;
   1.165 +
   1.166 +(*For creating output sequence*)
   1.167 +fun some_of_list []     = None
   1.168 +  | some_of_list (x::l) = Some (x, Sequence.seqof (fn () => some_of_list l));
   1.169 +
   1.170 +
   1.171 +(*Best-first search for a state that satisfies satp (incl initial state)
   1.172 +  Function sizef estimates size of problem remaining (smaller means better).
   1.173 +  tactic tac0 sets up the initial priority queue, while tac1 searches it. *)
   1.174 +fun THEN_BEST_FIRST tac0 (satp, sizef) tac1 = 
   1.175 +  let val tac = tracify trace_BEST_FIRST tac1
   1.176 +      fun pairsize th = (sizef th, th);
   1.177 +      fun bfs (news,nprfs) =
   1.178 +	   (case  partition satp news  of
   1.179 +		([],nonsats) => next(foldr insert
   1.180 +					(map pairsize nonsats, nprfs)) 
   1.181 +	      | (sats,_)  => some_of_list sats)
   1.182 +      and next [] = None
   1.183 +        | next ((n,prf)::nprfs) =
   1.184 +	    (if !trace_BEST_FIRST 
   1.185 +	       then writeln("state size = " ^ string_of_int n ^ 
   1.186 +		         "  queue length =" ^ string_of_int (length nprfs))  
   1.187 +               else ();
   1.188 +	     bfs (Sequence.list_of_s (tac prf), nprfs))
   1.189 +      fun btac st = bfs (Sequence.list_of_s (tac0 st),  [])
   1.190 +  in traced_tac btac end;
   1.191 +
   1.192 +(*Ordinary best-first search, with no initial tactic*)
   1.193 +val BEST_FIRST = THEN_BEST_FIRST all_tac;
   1.194 +
   1.195 +(*Breadth-first search to satisfy satpred (including initial state) 
   1.196 +  SLOW -- SHOULD NOT USE APPEND!*)
   1.197 +fun BREADTH_FIRST satpred tac = 
   1.198 +  let val tacf = Sequence.list_of_s o tac;
   1.199 +      fun bfs prfs =
   1.200 +	 (case  partition satpred prfs  of
   1.201 +	      ([],[]) => []
   1.202 +	    | ([],nonsats) => 
   1.203 +		  (prs("breadth=" ^ string_of_int(length nonsats) ^ "\n");
   1.204 +		   bfs (flat (map tacf nonsats)))
   1.205 +	    | (sats,_)  => sats)
   1.206 +  in (fn st => Sequence.s_of_list (bfs [st])) end;
   1.207 +
   1.208 +
   1.209 +(*  Author: 	Norbert Voelker, FernUniversitaet Hagen
   1.210 +    Remarks:    Implementation of A*-like proof procedure by modification
   1.211 +		of the existing code for BEST_FIRST and best_tac so that the 
   1.212 +		current level of search is taken into account.
   1.213 +*)		
   1.214 +
   1.215 +(*Insertion into priority queue of states, marked with level *)
   1.216 +fun insert_with_level (lnth: int*int*thm, []) = [lnth]
   1.217 +  | insert_with_level ((l,m,th), (l',n,th')::nths) = 
   1.218 +      if  n<m then (l',n,th') :: insert_with_level ((l,m,th), nths)
   1.219 +      else if  n=m andalso eq_thm(th,th')
   1.220 +              then (l',n,th')::nths
   1.221 +              else (l,m,th)::(l',n,th')::nths;
   1.222 +
   1.223 +(*For creating output sequence*)
   1.224 +fun some_of_list []     = None
   1.225 +  | some_of_list (x::l) = Some (x, Sequence.seqof (fn () => some_of_list l));
   1.226 +
   1.227 +val trace_ASTAR = ref false; 
   1.228 +
   1.229 +fun THEN_ASTAR tac0 (satp, costf) tac1 = 
   1.230 +  let val tf = tracify trace_ASTAR tac1;   
   1.231 +      fun bfs (news,nprfs,level) =
   1.232 +      let fun cost thm = (level, costf level thm, thm)
   1.233 +      in (case  partition satp news  of
   1.234 +            ([],nonsats) 
   1.235 +		 => next (foldr insert_with_level (map cost nonsats, nprfs))
   1.236 +          | (sats,_)  => some_of_list sats)
   1.237 +      end and    
   1.238 +      next []  = None
   1.239 +        | next ((level,n,prf)::nprfs)  =
   1.240 +            (if !trace_ASTAR 
   1.241 +               then writeln("level = " ^ string_of_int level ^
   1.242 +			 "  cost = " ^ string_of_int n ^ 
   1.243 +                         "  queue length =" ^ string_of_int (length nprfs))  
   1.244 +               else ();
   1.245 +             bfs (Sequence.list_of_s (tf prf), nprfs,level+1))
   1.246 +      fun tf st = bfs (Sequence.list_of_s (tac0 st), [], 0)
   1.247 +  in traced_tac tf end;
   1.248 +
   1.249 +(*Ordinary ASTAR, with no initial tactic*)
   1.250 +val ASTAR = THEN_ASTAR all_tac;
   1.251 +
   1.252 +end;
   1.253 +
   1.254 +open Search;