src/Tools/isac/Interpret/solve.sml
author Walther Neuper <wneuper@ist.tugraz.at>
Wed, 21 Dec 2016 08:57:47 +0100
changeset 59272 1d3ef477d9c8
parent 59271 7a02202e4577
child 59273 2ba35efb07b7
permissions -rw-r--r--
added structure Applicable INTERMEDIATELY

Note: introduction of structure Ctree : CALC_TREE is in the works,
but this is persuasive such, that "open Ctree" seems useful,
but the latter seems to require a structure to localise it.
     1 (* Title:  solve an example by interpreting a method's script
     2    Author: Walther Neuper 1999
     3    (c) copyright due to lincense terms.
     4 1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890
     5         10        20        30        40        50        60        70        80         90      100
     6 *)
     7 
     8 fun safe (ScrState (_,_,_,_,s,_)) = s
     9   | safe (RrlsState _) = Safe;
    10 
    11 type mstID = string;
    12 type tac'_ = mstID * tac; (*DG <-> ME*)
    13 val e_tac'_ = ("Empty_Tac", Empty_Tac):tac'_;
    14 
    15 fun mk_tac'_   m = case m of
    16   Init_Proof (ppc, spec)    => ("Init_Proof", Init_Proof (ppc, spec )) 
    17 | Model_Problem             => ("Model_Problem", Model_Problem)
    18 | Refine_Tacitly pblID      => ("Refine_Tacitly", Refine_Tacitly pblID)
    19 | Refine_Problem pblID      => ("Refine_Problem", Refine_Problem pblID)
    20 | Add_Given cterm'          => ("Add_Given", Add_Given cterm') 
    21 | Del_Given cterm'          => ("Del_Given", Del_Given cterm') 
    22 | Add_Find cterm'           => ("Add_Find", Add_Find cterm') 
    23 | Del_Find cterm'           => ("Del_Find", Del_Find cterm') 
    24 | Add_Relation cterm'       => ("Add_Relation", Add_Relation cterm') 
    25 | Del_Relation cterm'       => ("Del_Relation", Del_Relation cterm') 
    26 
    27 | Specify_Theory domID	    => ("Specify_Theory", Specify_Theory domID) 
    28 | Specify_Problem pblID     => ("Specify_Problem", Specify_Problem pblID)
    29 | Specify_Method metID	    => ("Specify_Method", Specify_Method metID) 
    30 | Apply_Method metID	    => ("Apply_Method", Apply_Method metID) 
    31 | Check_Postcond pblID	    => ("Check_Postcond", Check_Postcond pblID)
    32 | Free_Solve                => ("Free_Solve",Free_Solve)
    33 		    
    34 | Rewrite_Inst (subs, thm') => ("Rewrite_Inst", Rewrite_Inst (subs, thm')) 
    35 | Rewrite thm'		    => ("Rewrite", Rewrite thm') 
    36 | Rewrite_Asm thm'	    => ("Rewrite_Asm", Rewrite_Asm thm') 
    37 | Rewrite_Set_Inst (subs, rls')
    38                => ("Rewrite_Set_Inst", Rewrite_Set_Inst (subs, rls')) 
    39 | Rewrite_Set rls'          => ("Rewrite_Set", Rewrite_Set rls') 
    40 | End_Ruleset		    => ("End_Ruleset", End_Ruleset)
    41 
    42 | End_Detail                => ("End_Detail", End_Detail)
    43 | Detail_Set rls'           => ("Detail_Set", Detail_Set rls')
    44 | Detail_Set_Inst (s, rls') => ("Detail_Set_Inst", Detail_Set_Inst (s, rls'))
    45 
    46 | Calculate op_             => ("Calculate", Calculate op_)
    47 | Substitute sube           => ("Substitute", Substitute sube) 
    48 | Apply_Assumption cts'	    => ("Apply_Assumption", Apply_Assumption cts')
    49 
    50 | Take cterm'               => ("Take", Take cterm') 
    51 | Take_Inst cterm'          => ("Take_Inst", Take_Inst cterm') 
    52 | Group (con, ints) 	    => ("Group", Group (con, ints)) 
    53 | Subproblem (domID, pblID) => ("Subproblem", Subproblem (domID, pblID)) 
    54 (*
    55 | Subproblem_Full(spec,cts')=> ("Subproblem_Full", Subproblem_Full(spec,cts')) 
    56 *)
    57 | End_Subproblem            => ("End_Subproblem",End_Subproblem)
    58 | CAScmd cterm'		    => ("CAScmd", CAScmd cterm')
    59 			    
    60 | Split_And                 => ("Split_And", Split_And) 
    61 | Conclude_And		    => ("Conclude_And", Conclude_And) 
    62 | Split_Or                  => ("Split_Or", Split_Or) 
    63 | Conclude_Or		    => ("Conclude_Or", Conclude_Or) 
    64 | Begin_Trans               => ("Begin_Trans", Begin_Trans) 
    65 | End_Trans		    => ("End_Trans", End_Trans) 
    66 | Begin_Sequ                => ("Begin_Sequ", Begin_Sequ) 
    67 | End_Sequ                  => ("End_Sequ", Begin_Sequ) 
    68 | Split_Intersect           => ("Split_Intersect", Split_Intersect) 
    69 | End_Intersect		    => ("End_Intersect", End_Intersect) 
    70 | Check_elementwise cterm'  => ("Check_elementwise", Check_elementwise cterm')
    71 | Or_to_List                => ("Or_to_List", Or_to_List) 
    72 | Collect_Trues	            => ("Collect_Results", Collect_Trues) 
    73 			    
    74 | Empty_Tac               => ("Empty_Tac",Empty_Tac)
    75 | Tac string              => ("Tac",Tac string)
    76 | User                      => ("User",User)
    77 | End_Proof'                => ("End_Proof'",End_Proof'); 
    78 
    79 (*Detail*)
    80 val empty_tac'_ = (mk_tac'_ Empty_Tac):tac'_;
    81 
    82 fun mk_tac ((_,m):tac'_) = m; 
    83 fun mk_mstID ((mI,_):tac'_) = mI;
    84 
    85 fun tac'_2str ((ID,ms):tac'_) = ID ^ (tac2str ms);
    86 (* TODO: tac2str, tac'_2str NOT tested *)
    87 
    88 
    89 
    90 type squ = ptree; (* TODO: safe etc. *)
    91 
    92 (*13.9.02--------------
    93 type ctr = (loc * pos) list;
    94 val ops = [("PLUS","Groups.plus_class.plus"),("MINUS","Groups.minus_class.minus"),("TIMES","Groups.times_class.times"),
    95 	   ("cancel","cancel"),("pow","pow"),("sqrt","sqrt")];
    96 ML {* 
    97 @{term "PLUS"};   (*Free ("PLUS", "'a") : term*)
    98 @{term "plus"};   (*Const ("Groups.plus_class.plus", "'a => 'a => 'a")*)
    99 @{term "MINUS"};  (*Free ("MINUS", "'a")*)
   100 @{term "minus"};  (*Const ("Groups.minus_class.minus", "'a => 'a => 'a")*)
   101 @{term "TIMES"};  (*Free ("TIMES", "'a")*)
   102 @{term "times"};  (*Const ("Groups.times_class.times", "'a => 'a => 'a")*)
   103 @{term "CANCEL"}; (*Free ("CANCEL", "'a")*)
   104 @{term "cancel"}; (*Free ("cancel", "'a")*)
   105 @{term "POWER"};  (*Free ("POWER", "'a")*)
   106 @{term "pow"};    (*Free ("pow", "'a")*)
   107 @{term "SQRT"};   (*Free ("SQRT", "'a")*)
   108 @{term "sqrt"};   (*Const ("NthRoot.sqrt", "RealDef.real => RealDef.real")*)
   109 *}
   110 fun op_intern op_ =
   111   case assoc (ops, op_) of
   112     SOME op' => op' | NONE => error ("op_intern: no op= "^op_);
   113 -----------------------*)
   114 
   115 
   116 
   117 (* use"ME/solve.sml";
   118    use"solve.sml";
   119 
   120 val ttt = (Thm.term_of o the o (parse thy))"Substitute [(bdv,x)] g";
   121 val ttt = (Thm.term_of o the o (parse thy))"Rewrite thmid True g";
   122 
   123   Const ("Script.Rewrite'_Inst",_) $ sub $ Free (thm',_) $ Const (pa,_) $ f'
   124    *)
   125 
   126 
   127 
   128 val specsteps = ["Init_Proof","Refine_Tacitly","Refine_Problem",
   129 		 "Model_Problem",(*"Match_Problem",*)
   130 		 "Add_Given","Del_Given","Add_Find","Del_Find",
   131 		 "Add_Relation","Del_Relation",
   132 		 "Specify_Theory","Specify_Problem","Specify_Method"];
   133 
   134 "-----------------------------------------------------------------------";
   135 
   136 
   137 fun step2taci ((tac_, _, pt, p, _) : Lucin.step) = (*FIXXME.040312: redesign step*)
   138     (Lucin.tac_2tac tac_, tac_, (p, get_loc pt p)): Generate.taci;
   139 
   140 
   141 (*FIXME.WN050821 compare solve ... nxt_solv*)
   142 (* val ("Apply_Method",Apply_Method' (mI,_))=(mI,m);
   143    val (("Apply_Method",Apply_Method' (mI,_,_)),pt, pos as (p,_))=(m,pt, pos);
   144    *)
   145 fun solve ("Apply_Method", m as Apply_Method' (mI, _, _, _)) (pt:ptree, (pos as (p,_))) =
   146       let val {srls, ...} = Specify.get_met mI;
   147         val PblObj {meth=itms, ...} = get_obj I pt p;
   148         val thy' = get_obj g_domID pt p;
   149         val thy = assoc_thy thy';
   150         val (is as ScrState (env,_,_,_,_,_), ctxt, sc) = Lucin.init_scrstate thy itms mI;
   151         val ini = Lucin.init_form thy sc env;
   152         val p = lev_dn p;
   153       in 
   154         case ini of
   155 	        SOME t =>
   156             let val (pos,c,_,pt) = 
   157 		          Generate.generate1 thy (Apply_Method' (mI, SOME t, is, ctxt))
   158 			        (is, ctxt) (lev_on p, Frm)(*implicit Take*) pt;
   159 	          in ("ok",([(Apply_Method mI, Apply_Method' (mI, SOME t, is, ctxt), 
   160 		          ((lev_on p, Frm), (is, ctxt)))], c, (pt, pos)) : Chead.calcstate') 
   161 	          end	      
   162 	      | NONE => (*execute the first tac in the Script, compare solve m*)
   163 	          let
   164               val (m', (is', ctxt'), _) = Lucin.next_tac (thy', srls) (pt, (p, Res)) sc (is, ctxt);
   165 	            val d = e_rls (*FIXME: get simplifier from domID*);
   166 	          in 
   167 	            case Lucin.locate_gen (thy',srls) m' (pt,(p, Res)) (sc,d) (is', ctxt') of 
   168 		            Lucin.Steps (is'', ss as (m'',f',pt',p',c')::_) =>
   169 		              ("ok", (map step2taci ss, c', (pt',p')))
   170 		          | NotLocatable =>  
   171 		              let val (p,ps,f,pt) = Generate.generate_hard (assoc_thy "Isac") m (p,Frm) pt;
   172 		              in 
   173 		                ("not-found-in-script",([(Lucin.tac_2tac m, m, (pos, (is, ctxt)))], ps, (pt,p))) 
   174 		              end
   175 	          end
   176       end
   177 
   178   | solve ("Free_Solve", Free_Solve')  (pt,po as (p,_)) =
   179       let (*val _=tracing"###solve Free_Solve";*)
   180         val p' = lev_dn_ (p,Res);
   181         val pt = update_metID pt (par_pblobj pt p) e_metID;
   182       in ("ok", ([(Empty_Tac, Empty_Tac_, (po, (Uistate, e_ctxt)))], [], (pt,p')))
   183       end
   184 
   185   | solve ("Check_Postcond", Check_Postcond' (pI,_)) (pt,(pos as (p,p_))) =
   186       let
   187         val pp = par_pblobj pt p
   188         val asm = 
   189           (case get_obj g_tac pt p of
   190 		         Check_elementwise _ => (*collects and instantiates asms*)
   191 		           (snd o (get_obj g_result pt)) p
   192 		       | _ => get_assumptions_ pt (p,p_))
   193 	        handle _ => [] (*WN.27.5.03 asms in subpbls not completely clear*)
   194         val metID = get_obj g_metID pt pp;
   195         val {srls=srls,scr=sc,...} = Specify.get_met metID;
   196         val loc as (ScrState (E,l,a,_,_,b), ctxt) = get_loc pt (p,p_); 
   197         val thy' = get_obj g_domID pt pp;
   198         val thy = assoc_thy thy';
   199         val (_,_,(scval,scsaf)) = Lucin.next_tac (thy',srls) (pt,(p,p_)) sc loc;
   200       in 
   201         if pp = [] 
   202         then
   203 	        let 
   204             val is = ScrState (E,l,a,scval,scsaf,b)
   205 	          val tac_ = Check_Postcond' (pI, (scval, asm))
   206 	          val (pos,ps,f,pt) = Generate.generate1 thy tac_ (is, ctxt) (pp,Res) pt;
   207 	        in ("ok", ([(Check_Postcond pI, tac_, ((pp,Res),(is, ctxt)))], ps,(pt,pos))) end
   208         else
   209           let (*resume script of parpbl, transfer value of subpbl-script*)
   210             val ppp = par_pblobj pt (lev_up p);
   211 	          val thy' = get_obj g_domID pt ppp;
   212             val thy = assoc_thy thy';
   213 	          val metID = get_obj g_metID pt ppp;
   214             val sc = (#scr o Specify.get_met) metID;
   215             val (ScrState (E,l,a,_,_,b), ctxt') = get_loc pt (pp(*!/p/*),Frm); 
   216 	          val ctxt'' = from_subpbl_to_caller ctxt scval ctxt'
   217             val ((p,p_),ps,f,pt) = 
   218 	            Generate.generate1 thy (Check_Postcond' (pI, (scval, asm)))
   219 		            (ScrState (E,l,a,scval,scsaf,b), ctxt'') (pp,Res) pt;
   220        in ("ok", ([(Check_Postcond pI, Check_Postcond'(pI,(scval, asm)),
   221 	         ((pp,Res), (ScrState (E,l,a,scval,scsaf,b), ctxt'')))],ps,(pt,(p,p_))))
   222 	     end
   223      end
   224 
   225   | solve (_,End_Proof'') (pt, (p,p_)) =
   226       ("end-proof",
   227        ([(Empty_Tac,Empty_Tac_,(([],Res),(Uistate, e_ctxt)))],[],(pt,(p,p_))))
   228 
   229 (*-----------vvvvvvvvvvv could be done by generate1 ?!?*)
   230   | solve (_,End_Detail' t) (pt, (p,p_)) =
   231       let
   232         val pr as (p',_) = (lev_up p, Res)
   233 	      val pp = par_pblobj pt p
   234 	      val r = (fst o (get_obj g_result pt)) p' 
   235 	      (*Rewrite_Set* done at Detail_Set*: this result is already in ptree*)
   236 	      val thy' = get_obj g_domID pt pp
   237 	      val (srls, is, sc) = Lucin.from_pblobj' thy' pr pt
   238 	      val (tac_,is',_) = Lucin.next_tac (thy',srls) (pt,pr) sc is
   239       in ("ok", ([(End_Detail, End_Detail' t , 
   240 	      ((p,p_), get_loc pt (p,p_)))], [], (pt,pr)))
   241       end
   242 
   243   | solve (mI,m) (pt, po as (p,p_)) =
   244       if e_metID = get_obj g_metID pt (par_pblobj pt p)(*29.8.02: could be detail, too !!*)
   245       then
   246         let
   247           val ctxt = get_ctxt pt po
   248           val ((p,p_),ps,f,pt) = 
   249 		        Generate.generate1 (assoc_thy (get_obj g_domID pt (par_pblobj pt p))) 
   250 			        m (e_istate, ctxt) (p,p_) pt;
   251 	      in ("no-method-specified", (*Free_Solve*)
   252 	        ([(Empty_Tac,Empty_Tac_, ((p,p_),(Uistate, ctxt)))], ps, (pt,(p,p_))))
   253         end
   254       else
   255 	      let 
   256 	        val thy' = get_obj g_domID pt (par_pblobj pt p);
   257 	        val (srls, is, sc) = Lucin.from_pblobj_or_detail' thy' (p,p_) pt;
   258 		      val d = e_rls; (*FIXME.WN0108: canon.simplifier for domain is missing: generate from domID?*)
   259 	      in
   260           case Lucin.locate_gen (thy',srls) m  (pt,(p,p_)) (sc,d) is of 
   261 	          Lucin.Steps (is', ss as (m',f',pt',p',c')::_) =>
   262 	            let 
   263 	              (*27.8.02:next_tac may change to other branches in pt FIXXXXME*)
   264 	            in ("ok", (map step2taci ss, c', (pt',p'))) end
   265 	        | NotLocatable =>  
   266 	            let val (p,ps,f,pt) = 
   267 		            Generate.generate_hard (assoc_thy "Isac") m (p,p_) pt;
   268 	            in ("not-found-in-script",
   269 		            ([(Lucin.tac_2tac m, m, (po,is))], ps, (pt,p)))
   270               end
   271 	      end;
   272 
   273 (*FIXME.WN050821 compare fun solve ... fun nxt_solv*)
   274 (* nxt_solv (Apply_Method'     vvv FIXME: get args in applicable_in *)
   275 fun nxt_solv (Apply_Method' (mI,_,_,_)) _ (pt:ptree, pos as (p,_)) =
   276       let
   277         val {srls,ppc,...} = Specify.get_met mI;
   278         val PblObj{meth=itms,origin=(oris,_,_),probl, ...} = get_obj I pt p;
   279         val itms = if itms <> [] then itms else Chead.complete_metitms oris probl [] ppc
   280         val thy' = get_obj g_domID pt p;
   281         val thy = assoc_thy thy';
   282         val (is as ScrState (env,_,_,_,_,_), ctxt, scr) = Lucin.init_scrstate thy itms mI;
   283         val ini = Lucin.init_form thy scr env;
   284       in 
   285         case ini of
   286           SOME t =>
   287             let
   288               val pos = ((lev_on o lev_dn) p, Frm)
   289 	            val tac_ = Apply_Method' (mI, SOME t, is, ctxt);
   290 	            val (pos,c,_,pt) = (*implicit Take*)
   291 	              Generate.generate1 thy tac_ (is, ctxt) pos pt
   292             in ([(Apply_Method mI, tac_, (pos, (is, ctxt)))], c, (pt, pos)) : Chead.calcstate' end
   293         | NONE =>
   294             let
   295               val pt = update_env pt (fst pos) (SOME (is, ctxt))
   296 	            val (tacis, c, ptp) = nxt_solve_ (pt, pos)
   297             in (tacis @ 
   298 	              [(Apply_Method mI, Apply_Method' (mI, NONE, e_istate, ctxt), (pos, (is, ctxt)))],
   299 	             c, ptp)
   300             end
   301       end
   302 
   303     (*TODO.WN050913 remove unnecessary code below*)
   304   | nxt_solv (Check_Postcond' (pI,_)) _ (pt, pos as (p,p_))  =
   305       let
   306         val pp = par_pblobj pt p
   307         val asm =
   308           (case get_obj g_tac pt p of
   309 		         Check_elementwise _ => (*collects and instantiates asms*)
   310 		           (snd o (get_obj g_result pt)) p
   311 		       | _ => get_assumptions_ pt (p,p_))
   312 	        handle _ => [] (*FIXME.WN030527 asms in subpbls not completely clear*)
   313         val metID = get_obj g_metID pt pp;
   314         val {srls=srls,scr=sc,...} = Specify.get_met metID;
   315         val loc as (ScrState (E,l,a,_,_,b), ctxt) = get_loc pt (p,p_); 
   316         val thy' = get_obj g_domID pt pp;
   317         val thy = assoc_thy thy';
   318         val (_,_,(scval,scsaf)) = Lucin.next_tac (thy',srls) (pt,(p,p_)) sc loc;
   319       in
   320         if pp = []
   321         then 
   322 	        let
   323             val is = ScrState (E,l,a,scval,scsaf,b)
   324 	          val tac_ = Check_Postcond'(pI,(scval, asm))
   325 	          val ((p,p_),ps,f,pt) = 
   326 		          Generate.generate1 thy tac_ (is, ctxt) (pp,Res) pt;
   327 	        in ([(Check_Postcond pI, tac_, ((pp,Res), (is, ctxt)))],ps,(pt, (p,p_))) end
   328         else
   329           let (*resume script of parpbl, transfer value of subpbl-script*)
   330             val ppp = par_pblobj pt (lev_up p);
   331 	          val thy' = get_obj g_domID pt ppp;
   332             val thy = assoc_thy thy';
   333 	          val metID = get_obj g_metID pt ppp;
   334 	          val {scr,...} = Specify.get_met metID;
   335             val (ScrState (E,l,a,_,_,b), ctxt') = get_loc pt (pp(*!/p/*),Frm)
   336 	          val ctxt'' = from_subpbl_to_caller ctxt scval ctxt'
   337             val tac_ = Check_Postcond' (pI, (scval, asm))
   338 	          val is = ScrState (E,l,a,scval,scsaf,b)
   339             val ((p,p_),ps,f,pt) = Generate.generate1 thy tac_ (is, ctxt'') (pp, Res) pt;
   340           in ([(Check_Postcond pI, tac_, ((pp, Res), (is, ctxt'')))], ps, (pt, (p,p_))) end
   341       end
   342 
   343   | nxt_solv (End_Proof'') _ ptp = ([], [], ptp)
   344 
   345   | nxt_solv tac_ is (pt, pos as (p,p_)) =
   346       let
   347         val pos =
   348           case pos of
   349 		        (p, Met) => ((lev_on o lev_dn) p, Frm)(*begin script*)
   350 		      | (p, Res) => (lev_on p,Res) (*somewhere in script*)
   351 		      | _ => pos
   352 	      val (pos',c,_,pt) = Generate.generate1 (assoc_thy "Isac") tac_ is pos pt;
   353       in ([(Lucin.tac_2tac tac_, tac_, (pos,is))], c, (pt, pos')) end
   354 
   355 (* find the next tac from the script, nxt_solv will update the ptree *)
   356 and nxt_solve_ (ptp as (pt, pos as (p,p_))) =
   357       if e_metID = get_obj g_metID pt (par_pblobj pt p)
   358       then ([], [], (pt, (p, p_))) : Chead.calcstate'
   359       else 
   360         let 
   361           val thy' = get_obj g_domID pt (par_pblobj pt p);
   362 	        val (srls, is, sc) = Lucin.from_pblobj_or_detail' thy' (p,p_) pt;
   363 	        val (tac_,is,(t,_)) = Lucin.next_tac (thy',srls) (pt,pos) sc is;
   364 	        (*TODO here ^^^  return finished/helpless/ok !*)
   365 	      in case tac_ of
   366 		         End_Detail' _ => ([(End_Detail, End_Detail' (t,[(*FIXME.040215*)]), 
   367 				       (pos, is))], [], (pt, pos))
   368 	         | _ => nxt_solv tac_ is ptp end;
   369 
   370 (* says how may steps of a calculation should be done by "fun autocalc" *)
   371 (*FIXXXME040624: does NOT match interfaces/ITOCalc.java
   372   TODO.WN0512 redesign togehter with autocalc ?*)
   373 datatype auto = 
   374   Step of int      (*1 do #int steps (may stop in model/specify)
   375                        IS VERY INEFFICIENT IN MODEL/SPECIY                    *)
   376 | CompleteModel    (*2 complete modeling
   377                        if model complete, finish specifying                   *)
   378 | CompleteCalcHead (*3 complete model/specify in one go                       *)
   379 | CompleteToSubpbl (*4 stop at the next begin of a subproblem,
   380                        if none, complete the actual (sub)problem              *)
   381 | CompleteSubpbl   (*5 complete the actual (sub)problem (incl.ev.subproblems) *)
   382 | CompleteCalc;    (*6 complete the calculation as a whole                    *)
   383 
   384 fun autoord (Step _ ) = 1
   385   | autoord CompleteModel = 2
   386   | autoord CompleteCalcHead = 3
   387   | autoord CompleteToSubpbl = 4
   388   | autoord CompleteSubpbl = 5
   389   | autoord CompleteCalc = 6;
   390 
   391 fun complete_solve auto c (ptp as (_, p as (_,p_)): ptree * pos') =
   392   if p = ([], Res)
   393   then ("end-of-calculation", [], ptp)
   394   else
   395     if member op = [Pbl,Met] p_
   396     then
   397       let
   398         val ptp = Chead.all_modspec ptp
   399 	      val (_, c', ptp) = all_solve auto c ptp
   400 	    in complete_solve auto (c @ c') ptp end
   401     else
   402       case nxt_solve_ ptp of
   403 	      ((Subproblem _, tac_, (_, is))::_, c', ptp') =>
   404 	        if autoord auto < 5
   405           then ("ok", c @ c', ptp)
   406 	        else
   407             let
   408               val ptp = Chead.all_modspec ptp'
   409 	           val (_, c'', ptp) = all_solve auto (c @ c') ptp
   410 	         in complete_solve auto (c @ c'@ c'') ptp end
   411 	    | ((Check_Postcond _, tac_, (_, is))::_, c', ptp' as (_, p')) =>
   412 	        if autoord auto < 6 orelse p' = ([],Res)
   413           then ("ok", c @ c', ptp')
   414 	        else complete_solve auto (c @ c') ptp'
   415 	    | ((End_Detail, _, _)::_, c', ptp') => 
   416 	        if autoord auto < 6
   417           then ("ok", c @ c', ptp')
   418 	        else complete_solve auto (c @ c') ptp'
   419 	    | (_, c', ptp') => complete_solve auto (c @ c') ptp'
   420 
   421 and all_solve auto c (ptp as (pt, pos as (p,_)): ptree * pos') = 
   422   let
   423     val (_,_,mI) = get_obj g_spec pt p
   424     val ctxt = get_ctxt pt pos
   425     val (_, c', ptp) = nxt_solv (Apply_Method' (mI, NONE, e_istate, ctxt)) (e_istate, ctxt) ptp
   426   in complete_solve auto (c @ c') ptp end;
   427 
   428 (* aux.fun for detailrls with Rrls, reverse rewriting *)
   429 fun rul_terms_2nds _ nds t [] = nds
   430   | rul_terms_2nds thy nds t ((rule, res as (t', _)) :: rts) =
   431     (append_atomic [] (e_istate, e_ctxt) t (rule2tac thy [] rule) res Complete EmptyPtree) ::
   432     (rul_terms_2nds thy nds t' rts);
   433 
   434 (* detail steps done internally by Rewrite_Set* into ctree by use of a script *)
   435 fun detailrls pt (pos as (p,p_):pos') = 
   436   let
   437     val t = get_obj g_form pt p
   438 	  val tac = get_obj g_tac pt p
   439 	  val rls = (assoc_rls o rls_of) tac
   440     val ctxt = get_ctxt pt pos
   441   in
   442     case rls of
   443 	    Rrls {scr = Rfuns {init_state,...},...} => 
   444 	      let
   445           val (_,_,_,rul_terms) = init_state t
   446 	        val newnds = rul_terms_2nds (Proof_Context.theory_of ctxt) [] t rul_terms
   447 	        val pt''' = ins_chn newnds pt p 
   448 	      in ("detailrls", pt''', (p @ [length newnds], Res):pos') end
   449 	  | _ =>
   450 	      let
   451           val is = Generate.init_istate tac t
   452 	        (*TODO.WN060602 ScrState (["(t_, Problem (Isac,[equation,univar]))"]
   453 				    is wrong for simpl, but working ?!? *)
   454 	        val tac_ = Apply_Method' (e_metID(*WN0402: see generate1 !?!*), SOME t, is, ctxt)
   455 	        val pos' = ((lev_on o lev_dn) p, Frm)
   456 	        val thy = assoc_thy "Isac"
   457 	        val (_,_,_,pt') = (*implicit Take*)Generate.generate1 thy tac_ (is, ctxt) pos' pt
   458 	        val (_,_,(pt'',_)) = complete_solve CompleteSubpbl [] (pt',pos')
   459 	        val newnds = children (get_nd pt'' p)
   460 	        val pt''' = ins_chn newnds pt p 
   461 	        (*complete_solve cuts branches after*)
   462 	     in ("detailrls", pt''', (p @ [length newnds], Res):pos') end
   463   end;
   464 
   465 
   466 
   467 (* val(mI,m)=m;val ppp=p;(*!!!*)val(p,p_)=pos;val(_,pt,_)=ppp(*!!!*);
   468    get_form ((mI,m):tac'_) ((p,p_):pos') ppp;
   469    *)
   470 fun get_form ((mI,m):tac'_) ((p,p_):pos') pt = 
   471   case Applicable.applicable_in (p,p_) pt m of
   472     Chead.Notappl e => Generate.Error' e
   473   | Chead.Appl m => 
   474       (* val Appl m=applicable_in (p,p_) pt m;
   475          *)
   476       if member op = specsteps mI
   477 	then let val (_,_,f,_,_,_) = Chead.specify m (p,p_) [] pt
   478 	     in f end
   479       else let val (*_,_,f,_,_,_*)_ = solve (mI,m) (pt,(p,p_))
   480 	   in (*f*) Generate.EmptyMout end;
   481