src/Pure/search.ML
changeset 15531 08c8dad8e399
parent 14160 6750ff1dfc32
child 15570 8d8c70b41bab
equal deleted inserted replaced
15530:6f43714517ee 15531:08c8dad8e399
    56 
    56 
    57 (*Searches until "satp" reports proof tree as satisfied.
    57 (*Searches until "satp" reports proof tree as satisfied.
    58   Suppresses duplicate solutions to minimize search space.*)
    58   Suppresses duplicate solutions to minimize search space.*)
    59 fun DEPTH_FIRST satp tac = 
    59 fun DEPTH_FIRST satp tac = 
    60  let val tac = tracify trace_DEPTH_FIRST tac
    60  let val tac = tracify trace_DEPTH_FIRST tac
    61      fun depth used [] = None
    61      fun depth used [] = NONE
    62        | depth used (q::qs) =
    62        | depth used (q::qs) =
    63 	  case Seq.pull q of
    63 	  case Seq.pull q of
    64 	      None         => depth used qs
    64 	      NONE         => depth used qs
    65 	    | Some(st,stq) => 
    65 	    | SOME(st,stq) => 
    66 		if satp st andalso not (gen_mem eq_thm (st, used))
    66 		if satp st andalso not (gen_mem eq_thm (st, used))
    67 		then Some(st, Seq.make
    67 		then SOME(st, Seq.make
    68 			         (fn()=> depth (st::used) (stq::qs)))
    68 			         (fn()=> depth (st::used) (stq::qs)))
    69 		else depth used (tac st :: stq :: qs)
    69 		else depth used (tac st :: stq :: qs)
    70   in  traced_tac (fn st => depth [] ([Seq.single st]))  end;
    70   in  traced_tac (fn st => depth [] ([Seq.single st]))  end;
    71 
    71 
    72 
    72 
   154 		if !trace_DEPTH_FIRST then
   154 		if !trace_DEPTH_FIRST then
   155 		    tracing (string_of_int np ^ 
   155 		    tracing (string_of_int np ^ 
   156 			     implode (map (fn _ => "*") qs))
   156 			     implode (map (fn _ => "*") qs))
   157 		else ();
   157 		else ();
   158 		Seq.pull q) of
   158 		Seq.pull q) of
   159 	     None         => depth (bnd,inc) qs
   159 	     NONE         => depth (bnd,inc) qs
   160 	   | Some(st,stq) => 
   160 	   | SOME(st,stq) => 
   161 	       if satp st	(*solution!*)
   161 	       if satp st	(*solution!*)
   162 	       then Some(st, Seq.make
   162 	       then SOME(st, Seq.make
   163 			 (fn()=> depth (bnd,inc) ((k,np,rgd,stq)::qs)))
   163 			 (fn()=> depth (bnd,inc) ((k,np,rgd,stq)::qs)))
   164 
   164 
   165 	       else 
   165 	       else 
   166                let val np' = nprems_of st
   166                let val np' = nprems_of st
   167 		     (*rgd' calculation assumes tactic operates on subgoal 1*)
   167 		     (*rgd' calculation assumes tactic operates on subgoal 1*)
   195 (*** Best-first search ***)
   195 (*** Best-first search ***)
   196 
   196 
   197 val trace_BEST_FIRST = ref false;
   197 val trace_BEST_FIRST = ref false;
   198 
   198 
   199 (*For creating output sequence*)
   199 (*For creating output sequence*)
   200 fun some_of_list []     = None
   200 fun some_of_list []     = NONE
   201   | some_of_list (x::l) = Some (x, Seq.make (fn () => some_of_list l));
   201   | some_of_list (x::l) = SOME (x, Seq.make (fn () => some_of_list l));
   202 
   202 
   203 (*Check for and delete duplicate proof states*)
   203 (*Check for and delete duplicate proof states*)
   204 fun deleteAllMin prf heap = 
   204 fun deleteAllMin prf heap = 
   205       if ThmHeap.is_empty heap then heap
   205       if ThmHeap.is_empty heap then heap
   206       else if eq_thm (prf, #2 (ThmHeap.min heap))
   206       else if eq_thm (prf, #2 (ThmHeap.min heap))
   217 	   (case  partition satp news  of
   217 	   (case  partition satp news  of
   218 		([],nonsats) => next(foldr ThmHeap.insert
   218 		([],nonsats) => next(foldr ThmHeap.insert
   219 					(map pairsize nonsats, nprf_heap)) 
   219 					(map pairsize nonsats, nprf_heap)) 
   220 	      | (sats,_)  => some_of_list sats)
   220 	      | (sats,_)  => some_of_list sats)
   221       and next nprf_heap =
   221       and next nprf_heap =
   222             if ThmHeap.is_empty nprf_heap then None
   222             if ThmHeap.is_empty nprf_heap then NONE
   223 	    else 
   223 	    else 
   224 	    let val (n,prf) = ThmHeap.min nprf_heap
   224 	    let val (n,prf) = ThmHeap.min nprf_heap
   225 	    in if !trace_BEST_FIRST 
   225 	    in if !trace_BEST_FIRST 
   226 	       then tracing("state size = " ^ string_of_int n)  
   226 	       then tracing("state size = " ^ string_of_int n)  
   227                else ();
   227                else ();
   264       else if  n=m andalso eq_thm(th,th')
   264       else if  n=m andalso eq_thm(th,th')
   265               then (l',n,th')::nths
   265               then (l',n,th')::nths
   266               else (l,m,th)::(l',n,th')::nths;
   266               else (l,m,th)::(l',n,th')::nths;
   267 
   267 
   268 (*For creating output sequence*)
   268 (*For creating output sequence*)
   269 fun some_of_list []     = None
   269 fun some_of_list []     = NONE
   270   | some_of_list (x::l) = Some (x, Seq.make (fn () => some_of_list l));
   270   | some_of_list (x::l) = SOME (x, Seq.make (fn () => some_of_list l));
   271 
   271 
   272 val trace_ASTAR = ref false; 
   272 val trace_ASTAR = ref false; 
   273 
   273 
   274 fun THEN_ASTAR tac0 (satp, costf) tac1 = 
   274 fun THEN_ASTAR tac0 (satp, costf) tac1 = 
   275   let val tf = tracify trace_ASTAR tac1;   
   275   let val tf = tracify trace_ASTAR tac1;   
   278       in (case  partition satp news  of
   278       in (case  partition satp news  of
   279             ([],nonsats) 
   279             ([],nonsats) 
   280 		 => next (foldr insert_with_level (map cost nonsats, nprfs))
   280 		 => next (foldr insert_with_level (map cost nonsats, nprfs))
   281           | (sats,_)  => some_of_list sats)
   281           | (sats,_)  => some_of_list sats)
   282       end and    
   282       end and    
   283       next []  = None
   283       next []  = NONE
   284         | next ((level,n,prf)::nprfs)  =
   284         | next ((level,n,prf)::nprfs)  =
   285             (if !trace_ASTAR 
   285             (if !trace_ASTAR 
   286                then tracing("level = " ^ string_of_int level ^
   286                then tracing("level = " ^ string_of_int level ^
   287 			 "  cost = " ^ string_of_int n ^ 
   287 			 "  cost = " ^ string_of_int n ^ 
   288                          "  queue length =" ^ string_of_int (length nprfs))  
   288                          "  queue length =" ^ string_of_int (length nprfs))