src/Tools/isac/Interpret/appl.sml
author Walther Neuper <neuper@ist.tugraz.at>
Sat, 16 Apr 2011 10:19:45 +0200
branchdecompose-isar
changeset 41958 66b31adc80f2
parent 41957 703d656a6291
child 41959 a0d6a7c3e1df
permissions -rw-r--r--
Apply_Method transfers ctxt from specification to interpretation

remark: the precondition (prec) requires special attention:
precs in the local subproblem need also be in the next-outer problem.
They should hold before entering the subproblem (and they should
be in the respective (next-outer) ctxt; but they aren't yet)

So there is the advice for the next changes to take the precs _all_
and transfer them the the (next-outer) ctxt _always_ ---
--- which has to be reversed finally
     1 (* use"ME/appl.sml";
     2    use"appl.sml";
     3 
     4 12345678901234567890123456789012345678901234567890123456789012345678901234567890
     5         10        20        30        40        50        60        70        80
     6 *)
     7 val e_cterm' = empty_cterm';
     8 
     9 
    10 fun rew_info (Rls {erls,rew_ord=(rew_ord',_),calc=ca, ...}) =
    11     (rew_ord':rew_ord',erls,ca)
    12   | rew_info (Seq {erls,rew_ord=(rew_ord',_),calc=ca, ...}) =
    13     (rew_ord',erls,ca)
    14   | rew_info (Rrls {erls,rew_ord=(rew_ord',_),calc=ca, ...}) =
    15     (rew_ord',erls, ca)
    16   | rew_info rls = error ("rew_info called with '"^rls2str rls^"'");
    17 
    18 (*FIXME.3.4.03:re-organize from_pblobj_or_detail_thm after rls' --> rls*)
    19 fun from_pblobj_or_detail_thm thm' p pt = 
    20     let val (pbl,p',rls') = par_pbl_det pt p
    21     in if pbl
    22        then let (*val _= tracing("### from_pblobj_or_detail_thm: pbl=true")*)
    23 	        val thy' = get_obj g_domID pt p'
    24 		val {rew_ord',erls,(*asm_thm,*)...} = 
    25 		    get_met (get_obj g_metID pt p')
    26 		(*val _= tracing("### from_pblobj_or_detail_thm: metID= "^
    27 			       (metID2str(get_obj g_metID pt p')))
    28 		val _= tracing("### from_pblobj_or_detail_thm: erls= "^erls)*)
    29 	    in ("OK",thy',rew_ord',erls,(*put_asm*)false) 
    30 	    end
    31        else ((*tracing("### from_pblobj_or_detail_thm: pbl=false");*)
    32 	     (*case assoc(!ruleset', rls') of  !!!FIXME.3.4.03:re-organize !!!
    33 		NONE => ("unknown ruleset '"^rls'^"'","","",Erls,false)
    34 	      | SOME rls =>*)
    35 		let val thy' = get_obj g_domID pt (par_pblobj pt p)
    36 		    val (rew_ord',erls,(*asm_thm,*)_) = rew_info rls'
    37 		in ("OK",thy',rew_ord',erls,false) end)
    38     end;
    39 (*FIXME.3.4.03:re-organize from_pblobj_or_detail_calc after rls' --> rls*)
    40 fun from_pblobj_or_detail_calc scrop p pt = 
    41 (* val (scrop, p, pt) = (op_, p, pt);
    42    *)
    43     let val (pbl,p',rls') = par_pbl_det pt p
    44     in if pbl
    45        then let val thy' = get_obj g_domID pt p'
    46 		val {calc = scr_isa_fns,...} = 
    47 		    get_met (get_obj g_metID pt p')
    48 		val opt = assoc (scr_isa_fns, scrop)
    49 	    in case opt of
    50 		   SOME isa_fn => ("OK",thy',isa_fn)
    51 		 | NONE => ("applicable_in Calculate: unknown '"^scrop^"'",
    52 			    "",("",e_evalfn)) end
    53        else (*case assoc(!ruleset', rls') of
    54 		NONE => ("unknown ruleset '"^rls'^"'","",("",e_evalfn))
    55 	      | SOME rls => !!!FIXME.3.4.03:re-organize from_pblobj_or_detai*)
    56 		(* val SOME rls = assoc(!ruleset', rls');
    57 		   *)
    58 		let val thy' = get_obj g_domID pt (par_pblobj pt p);
    59 		    val (_,_,(*_,*)scr_isa_fns) = rew_info rls'(*rls*)
    60 		in case assoc (scr_isa_fns, scrop) of
    61 		   SOME isa_fn => ("OK",thy',isa_fn)
    62 		 | NONE => ("applicable_in Calculate: unknown '"^scrop^"'",
    63 			    "",("",e_evalfn)) end
    64     end;
    65 (*------------------------------------------------------------------*)
    66 
    67 val op_and = Const ("op &", [bool, bool] ---> bool);
    68 (*> (cterm_of thy) (op_and $ Free("a",bool) $ Free("b",bool));
    69 val it = "a & b" : cterm
    70 *)
    71 fun mk_and a b = op_and $ a $ b;
    72 (*> (cterm_of thy) 
    73      (mk_and (Free("a",bool)) (Free("b",bool)));
    74 val it = "a & b" : cterm*)
    75 
    76 fun mk_and [] = HOLogic.true_const
    77   | mk_and (t::[]) = t
    78   | mk_and (t::ts) = 
    79     let fun mk t' (t::[]) = op_and $ t' $ t
    80 	  | mk t' (t::ts) = mk (op_and $ t' $ t) ts
    81     in mk t ts end;
    82 (*> val pred = map (term_of o the o (parse thy)) 
    83              ["#0 <= #9 + #4 * x","#0 <= sqrt x + sqrt (#-3 + x)"];
    84 > (cterm_of thy) (mk_and pred);
    85 val it = "#0 <= #9 + #4 * x & #0 <= sqrt x + sqrt (#-3 + x)" : cterm*)
    86 
    87 
    88 
    89 
    90 (*for Check_elementwise in applicable_in: [x=1,..] Assumptions -> (x,0<=x&..)*)
    91 fun mk_set thy pt p (Const ("List.list.Nil",_)) pred = (e_term, [])
    92 
    93   | mk_set thy pt p (Const ("Tools.UniversalList",_)) pred =
    94     (e_term, if pred <> Const ("Script.Assumptions",bool)
    95 	     then [pred] 
    96 	     else get_assumptions_ pt (p,Res))
    97 
    98 (* val pred = (term_of o the o (parse thy)) pred;
    99    val consts as Const ("List.list.Cons",_) $ eq $ _ = ft;
   100    mk_set thy pt p consts pred;
   101    *)
   102   | mk_set thy pt p (consts as Const ("List.list.Cons",_) $ eq $ _) pred =
   103   let val (bdv,_) = HOLogic.dest_eq eq;
   104     val pred = if pred <> Const ("Script.Assumptions",bool)
   105 		 then [pred] 
   106 	       else get_assumptions_ pt (p,Res)
   107   in (bdv, pred) end
   108 
   109   | mk_set thy _ _ l _ = 
   110     error ("check_elementwise: no set " ^ term2str l);
   111 (*> val consts = str2term "[x=#4]";
   112 > val pred = str2term "Assumptions";
   113 > val pt = union_asm pt p 
   114    [("#0 <= sqrt x + sqrt (#5 + x)",[11]),("#0 <= #9 + #4 * x",[22]),
   115    ("#0 <= x ^^^ #2 + #5 * x",[33]),("#0 <= #2 + x",[44])];
   116 > val p = [];
   117 > val (sss,ttt) = mk_set thy pt p consts pred;
   118 > (term2str sss, term2str ttt);
   119 val it = ("x","((#0 <= sqrt x + sqrt (#5 + x) & #0 <= #9 + #4 * x) & ...
   120 
   121  val consts = str2term "UniversalList";
   122  val pred = str2term "Assumptions";
   123 
   124 *)
   125 
   126 
   127 
   128 (*check a list (/set) of constants [c_1,..,c_n] for c_i:set (: in)*)
   129 (* val (erls,consts,(bdv,pred)) = (erl,ft,vp);
   130    val (consts,(bdv,pred)) = (ft,vp);
   131    *)
   132 fun check_elementwise thy erls all_results (bdv, asm) =
   133     let   (*bdv extracted from ~~~~~~~~~~~ in mk_set already*)
   134 	fun check sub =
   135 	    let val inst_ = map (subst_atomic [sub]) asm
   136 	    in case eval__true thy 1 inst_ [] erls of
   137 		   (asm', true) => ([HOLogic.mk_eq sub], asm')
   138 		 | (_, false) => ([],[])
   139 	    end;
   140       (*val _= tracing("### check_elementwise: res= "^(term2str all_results)^
   141 		       ", bdv= "^(term2str bdv)^", asm= "^(terms2str asm));*)
   142 	val c' = isalist2list all_results
   143 	val c'' = map (snd o HOLogic.dest_eq) c' (*assumes [x=1,x=2,..]*)
   144 	val subs = map (pair bdv) c''
   145     in if asm = [] then (all_results, [])
   146        else ((apfst ((list2isalist bool) o flat)) o 
   147 	     (apsnd flat) o split_list o (map check)) subs end;
   148 (* 20.5.03
   149 > val all_results = str2term "[x=a+b,x=b,x=3]";
   150 > val bdv = str2term "x";
   151 > val asm = str2term "(x ~= a) & (x ~= b)";
   152 > val erls = e_rls;
   153 > val (t, ts) = check_elementwise thy erls all_results (bdv, asm);
   154 > term2str t; tracing(terms2str ts);
   155 val it = "[x = a + b, x = b, x = c]" : string
   156 ["a + b ~= a & a + b ~= b","b ~= a & b ~= b","c ~= a & c ~= b"]
   157 ... with appropriate erls this should be:
   158 val it = "[x = a + b,       x = c]" : string
   159 ["b ~= 0 & a ~= 0",         "3 ~= a & 3 ~= b"]
   160                     ////// because b ~= b False*)
   161 
   162 
   163 
   164 (*before 5.03-----
   165 > val ct = "((#0 <= #18 & #0 <= sqrt (#5 + #3) + sqrt (#5 - #3)) &\
   166 	   \ #0 <= #25 + #-1 * #3 ^^^ #2) & #0 <= #4";
   167 > val SOME(ct',_) = rewrite_set "Isac" false "eval_rls" ct;
   168 val ct' = "HOL.True" : cterm'
   169 
   170 > val ct = "((#0 <= #18 & #0 <= sqrt (#5 + #-3) + sqrt (#5 - #-3)) &\
   171 	   \ #0 <= #25 + #-1 * #-3 ^^^ #2) & #0 <= #4";
   172 > val SOME(ct',_) = rewrite_set "Isac"  false "eval_rls" ct;
   173 val ct' = "HOL.True" : cterm'
   174 
   175 
   176 > val const  = (term_of o the o (parse thy)) "(#3::real)";
   177 > val pred' = subst_atomic [(bdv,const)] pred;
   178 
   179 
   180 > val consts = (term_of o the o (parse thy)) "[x = #-3, x = #3]";
   181 > val bdv    = (term_of o the o (parse thy)) "(x::real)";
   182 > val pred   = (term_of o the o (parse thy)) 
   183   "((#0 <= #18 & #0 <= sqrt (#5 + x) + sqrt (#5 - x)) & #0 <= #25 + #-1 * x ^^^ #2) & #0 <= #4";
   184 > val ttt = check_elementwise thy consts (bdv, pred);
   185 > (cterm_of thy) ttt;
   186 val it = "[x = #-3, x = #3]" : cterm
   187 
   188 > val consts = (term_of o the o (parse thy)) "[x = #4]";
   189 > val bdv    = (term_of o the o (parse thy)) "(x::real)";
   190 > val pred   = (term_of o the o (parse thy)) 
   191  "#0 <= sqrt x + sqrt (#5 + x) & #0 <= #9 + #4 * x & #0 <= x ^^^ #2 + #5 * x & #0 <= #2 + x";
   192 > val ttt = check_elementwise thy consts (bdv,pred);
   193 > (cterm_of thy) ttt;
   194 val it = "[x = #4]" : cterm
   195 
   196 > val consts = (term_of o the o (parse thy)) "[x = #-12 // #5]";
   197 > val bdv    = (term_of o the o (parse thy)) "(x::real)";
   198 > val pred   = (term_of o the o (parse thy))
   199  " #0 <= sqrt x + sqrt (#-3 + x) & #0 <= #9 + #4 * x & #0 <= x ^^^ #2 + #-3 * x & #0 <= #6 + x";
   200 > val ttt = check_elementwise thy consts (bdv,pred);
   201 > (cterm_of thy) ttt;
   202 val it = "[]" : cterm*)
   203 
   204 
   205 (* 14.1.01: for Tac-dummies in root-equ only: skip str until "("*)
   206 fun split_dummy str = 
   207 let fun scan s' [] = (implode s', "")
   208       | scan s' (s::ss) = if s=" " then (implode s', implode  ss)
   209 			  else scan (s'@[s]) ss;
   210 in ((scan []) o Symbol.explode) str end;
   211 (* split_dummy "subproblem_equation_dummy (x=-#5//#12)";
   212 val it = ("subproblem_equation_dummy","(x=-#5//#12)") : string * string
   213 > split_dummy "x=-#5//#12";
   214 val it = ("x=-#5//#12","") : string * string*)
   215 
   216 
   217 
   218 
   219 (*.applicability of a tacic wrt. a calc-state (ptree,pos').
   220    additionally used by next_tac in the script-interpreter for sequence-tacs.
   221    tests for applicability are so expensive, that results (rewrites!)
   222    are kept in the return-value of 'type tac_'.
   223 .*)
   224 fun applicable_in (_:pos') _ (Init_Proof (ct', spec)) =
   225   Appl (Init_Proof' (ct', spec))
   226 
   227   | applicable_in (p,p_) pt Model_Problem = 
   228   if not (is_pblobj (get_obj I pt p)) orelse p_ = Res
   229     then Notappl ((tac2str Model_Problem)^
   230 	   " not for pos "^(pos'2str (p,p_)))
   231   else let val (PblObj{origin=(_,(_,pI',_),_),...}) = get_obj I pt p
   232 	   val {ppc,...} = get_pbt pI'
   233 	   val pbl = init_pbl ppc
   234        in Appl (Model_Problem' (pI', pbl, [])) end
   235 (* val Refine_Tacitly pI = m;
   236    *)
   237   | applicable_in (p,p_) pt (Refine_Tacitly pI) = 
   238   if not (is_pblobj (get_obj I pt p)) orelse p_ = Res
   239     then Notappl ((tac2str (Refine_Tacitly pI))^
   240 	   " not for pos "^(pos'2str (p,p_)))
   241   else (* val Refine_Tacitly pI = m;
   242           *)
   243     let val (PblObj {origin = (oris, (dI',_,_),_), ...}) = get_obj I pt p;
   244       val opt = refine_ori oris pI;
   245     in case opt of
   246 	   SOME pblID => 
   247 	   Appl (Refine_Tacitly' (pI, pblID, 
   248 				  e_domID, e_metID, [](*filled in specify*)))
   249 	 | NONE => Notappl ((tac2str (Refine_Tacitly pI))^
   250 			    " not applicable") end
   251 (* val (p,p_) = ip;
   252    val Refine_Problem pI = m;
   253    *)
   254   | applicable_in (p,p_) pt (Refine_Problem pI) = 
   255   if not (is_pblobj (get_obj I pt p)) orelse p_ = Res                  
   256     then Notappl ((tac2str (Refine_Problem pI))^
   257 	   " not for pos "^(pos'2str (p,p_)))
   258   else
   259     let val (PblObj {origin=(_,(dI,_,_),_),spec=(dI',_,_),
   260 		     probl=itms, ...}) = get_obj I pt p;
   261 	val thy = if dI' = e_domID then dI else dI';
   262 	val rfopt = refine_pbl (assoc_thy thy) pI itms;
   263     in case rfopt of
   264 	   NONE => Notappl ((tac2str (Refine_Problem pI))^" not applicable")
   265 	 | SOME (rf as (pI',_)) =>
   266 (* val SOME (rf as (pI',_)) = rfopt;
   267    *)
   268 	   if pI' = pI
   269 	   then Notappl ((tac2str (Refine_Problem pI))^" not applicable")
   270 	   else Appl (Refine_Problem' rf)
   271     end
   272 
   273   (*the specify-tacs have cterm' instead term: 
   274    parse+error here!!!: see appl_add*)  
   275   | applicable_in (p,p_) pt (Add_Given ct') = 
   276   if not (is_pblobj (get_obj I pt p)) orelse p_ = Res                  
   277     then Notappl ((tac2str (Add_Given ct'))^
   278 	   " not for pos "^(pos'2str (p,p_)))
   279   else Appl (Add_Given' (ct', [(*filled in specify_additem*)]))
   280   (*Add_.. should reject (dsc //) (see fmz=[] in sqrt*)
   281 
   282   | applicable_in (p,p_) pt (Del_Given ct') =
   283   if not (is_pblobj (get_obj I pt p)) orelse p_ = Res                  
   284     then Notappl ((tac2str (Del_Given ct'))^
   285 	   " not for pos "^(pos'2str (p,p_)))
   286   else Appl (Del_Given' ct')
   287 
   288   | applicable_in (p,p_) pt (Add_Find ct') =                   
   289   if not (is_pblobj (get_obj I pt p)) orelse p_ = Res                  
   290     then Notappl ((tac2str (Add_Find ct'))^
   291 	   " not for pos "^(pos'2str (p,p_)))
   292   else Appl (Add_Find' (ct', [(*filled in specify_additem*)]))
   293 
   294   | applicable_in (p,p_) pt (Del_Find ct') =
   295   if not (is_pblobj (get_obj I pt p)) orelse p_ = Res                  
   296     then Notappl ((tac2str (Del_Find ct'))^
   297 	   " not for pos "^(pos'2str (p,p_)))
   298   else Appl (Del_Find' ct')
   299 
   300   | applicable_in (p,p_) pt (Add_Relation ct') =               
   301   if not (is_pblobj (get_obj I pt p)) orelse p_ = Res                  
   302     then Notappl ((tac2str (Add_Relation ct'))^
   303 	   " not for pos "^(pos'2str (p,p_)))
   304   else Appl (Add_Relation' (ct', [(*filled in specify_additem*)]))
   305 
   306   | applicable_in (p,p_) pt (Del_Relation ct') =
   307   if not (is_pblobj (get_obj I pt p)) orelse p_ = Res                  
   308     then Notappl ((tac2str (Del_Relation ct'))^
   309 	   " not for pos "^(pos'2str (p,p_)))
   310   else Appl (Del_Relation' ct')
   311 
   312   | applicable_in (p,p_) pt (Specify_Theory dI) =              
   313   if not (is_pblobj (get_obj I pt p)) orelse p_ = Res                  
   314     then Notappl ((tac2str (Specify_Theory dI))^
   315 	   " not for pos "^(pos'2str (p,p_)))
   316   else Appl (Specify_Theory' dI)
   317 (* val (p,p_) = p; val Specify_Problem pID = m;
   318    val Specify_Problem pID = m;
   319    *)
   320   | applicable_in (p,p_) pt (Specify_Problem pID) = 
   321   if not (is_pblobj (get_obj I pt p)) orelse p_ = Res                  
   322     then Notappl ((tac2str (Specify_Problem pID))^
   323 	   " not for pos "^(pos'2str (p,p_)))
   324   else
   325     let val (PblObj {origin=(oris,(dI,pI,_),_),spec=(dI',pI',_),
   326 		     probl=itms, ...}) = get_obj I pt p;
   327 	val thy = assoc_thy (if dI' = e_domID then dI else dI');
   328         val {ppc,where_,prls,...} = get_pbt pID;
   329 	val pbl = if pI'=e_pblID andalso pI=e_pblID
   330 		  then (false, (init_pbl ppc, []))
   331 		  else match_itms_oris thy itms (ppc,where_,prls) oris;
   332     in Appl (Specify_Problem' (pID, pbl)) end
   333 (* val Specify_Method mID = nxt; val (p,p_) = p; 
   334    *)
   335   | applicable_in (p,p_) pt (Specify_Method mID) =              
   336   if not (is_pblobj (get_obj I pt p)) orelse p_ = Res               
   337     then Notappl ((tac2str (Specify_Method mID))^
   338 	   " not for pos "^(pos'2str (p,p_)))
   339   else Appl (Specify_Method' (mID,[(*filled in specify*)],
   340 			      [(*filled in specify*)]))
   341 
   342   | applicable_in (p,p_) pt (Apply_Method mI) =                
   343   if not (is_pblobj (get_obj I pt p)) orelse p_ = Res                  
   344     then Notappl ((tac2str (Apply_Method mI))^
   345 	   " not for pos "^(pos'2str (p,p_)))
   346   else let
   347     val (PblObj{origin = (_, (dI, pI, _), _), probl, ...}) = get_obj I pt p;
   348     val {where_, ...} = get_pbt pI
   349     val pres = map (mk_env probl |> subst_atomic) where_
   350     val ctxt = assoc_thy dI |> ProofContext.init_global 
   351           |> insert_assumptions pres
   352     (*TODO.WN110416 here evalprecond according to fun check_preconds'
   353       and then decide on Notappl/Appl once more.
   354       Implement after all tests are running, since this changes
   355       overall system behavior*)
   356   in Appl (Apply_Method' (mI, NONE, e_istate (*filled in solve*), ctxt)) end
   357 
   358   | applicable_in (p,p_) pt (Check_Postcond pI) =
   359   if member op = [Pbl,Met] p_                  
   360     then Notappl ((tac2str (Check_Postcond pI))^
   361 	   " not for pos "^(pos'2str (p,p_)))
   362   else Appl (Check_Postcond' 
   363 		 (pI,(e_term,[(*asm in solve*)])))
   364   (* in solve -"-     ^^^^^^ gets returnvalue of scr*)
   365 
   366   (*these are always applicable*)
   367   | applicable_in (p,p_) _ (Take str) = Appl (Take' (str2term str))
   368   | applicable_in (p,p_) _ (Free_Solve) = Appl (Free_Solve')
   369 
   370 (* val m as Rewrite_Inst (subs, thm') = m;
   371    *)
   372   | applicable_in (p,p_) pt (m as Rewrite_Inst (subs, thm')) = 
   373   if member op = [Pbl,Met] p_ 
   374     then Notappl ((tac2str m)^" not for pos "^(pos'2str (p,p_)))
   375   else
   376   let 
   377     val pp = par_pblobj pt p;
   378     val thy' = (get_obj g_domID pt pp):theory';
   379     val thy = assoc_thy thy';
   380     val {rew_ord'=ro',erls=erls,...} = 
   381       get_met (get_obj g_metID pt pp);
   382     val (f,p) = case p_ of (*p 12.4.00 unnecessary*)
   383               Frm => (get_obj g_form pt p, p)
   384 	    | Res => ((fst o (get_obj g_result pt)) p, lev_on p)
   385 	    | _ => error ("applicable_in: call by "^
   386 				(pos'2str (p,p_)));
   387   in 
   388     let val subst = subs2subst thy subs;
   389 	val subs' = subst2subs' subst;
   390     in case rewrite_inst_ thy (assoc_rew_ord ro') erls
   391 			 (*put_asm*)false subst (assoc_thm' thy thm') f of
   392       SOME (f',asm) => Appl (
   393 	  Rewrite_Inst' (thy',ro',erls,(*put_asm*)false,subst,thm',
   394       (*term_of o the o (parse (assoc_thy thy'))*) f,
   395        (*(term_of o the o (parse (assoc_thy thy'))*) (f',
   396 	(*map (term_of o the o (parse (assoc_thy thy')))*) asm)))
   397     | NONE => Notappl ((fst thm')^" not applicable") end
   398   handle _ => Notappl ("syntax error in "^(subs2str subs)) end
   399 
   400 (* val ((p,p_), pt, m as Rewrite thm') = (p, pt, m);
   401    val ((p,p_), pt, m as Rewrite thm') = (pos, pt, tac);
   402    *)
   403 | applicable_in (p,p_) pt (m as Rewrite thm') = 
   404   if member op = [Pbl,Met] p_ 
   405     then Notappl ((tac2str m)^" not for pos "^(pos'2str (p,p_)))
   406   else
   407   let val (msg,thy',ro,rls',(*put_asm*)_)= from_pblobj_or_detail_thm thm' p pt;
   408     val thy = assoc_thy thy';
   409     val f = case p_ of
   410               Frm => get_obj g_form pt p
   411 	    | Res => (fst o (get_obj g_result pt)) p
   412 	    | _ => error ("applicable_in Rewrite: call by "^
   413 				(pos'2str (p,p_)));
   414   in if msg = "OK" 
   415      then
   416       ((*tracing("### applicable_in rls'= "^rls');*)
   417        (* val SOME (f',asm)=rewrite thy' ro (id_rls rls') put_asm thm' f;
   418 	  *)
   419        case rewrite_ thy (assoc_rew_ord ro) 
   420 		     rls' false (assoc_thm' thy thm') f of
   421        SOME (f',asm) => Appl (
   422 	   Rewrite' (thy',ro,rls',(*put_asm*)false,thm', f, (f', asm)))
   423      | NONE => Notappl ("'"^(fst thm')^"' not applicable") )
   424      else Notappl msg
   425   end
   426 
   427 | applicable_in (p,p_) pt (m as Rewrite_Asm thm') = 
   428   if member op = [Pbl,Met] p_ 
   429     then Notappl ((tac2str m)^" not for pos "^(pos'2str (p,p_)))
   430   else
   431   let 
   432     val pp = par_pblobj pt p; 
   433     val thy' = (get_obj g_domID pt pp):theory';
   434     val thy = assoc_thy thy';
   435     val {rew_ord'=ro',erls=erls,...} = 
   436       get_met (get_obj g_metID pt pp);
   437     (*val put_asm = true;*)
   438     val (f,p) = case p_ of  (*p 12.4.00 unnecessary*)
   439               Frm => (get_obj g_form pt p, p)
   440 	    | Res => ((fst o (get_obj g_result pt)) p, lev_on p)
   441 	    | _ => error ("applicable_in: call by "^
   442 				(pos'2str (p,p_)));
   443   in case rewrite_ thy (assoc_rew_ord ro') erls 
   444 		   (*put_asm*)false (assoc_thm' thy thm') f of
   445        SOME (f',asm) => Appl (
   446 	   Rewrite' (thy',ro',erls,(*put_asm*)false,thm', f, (f', asm)))
   447      | NONE => Notappl ("'"^(fst thm')^"' not applicable") end
   448 
   449   | applicable_in (p,p_) pt (m as Detail_Set_Inst (subs, rls)) = 
   450   if member op = [Pbl,Met] p_ 
   451     then Notappl ((tac2str m)^" not for pos "^(pos'2str (p,p_)))
   452   else
   453   let 
   454     val pp = par_pblobj pt p;
   455     val thy' = (get_obj g_domID pt pp):theory';
   456     val thy = assoc_thy thy';
   457     val {rew_ord'=ro',...} = get_met (get_obj g_metID pt pp);
   458     val f = case p_ of Frm => get_obj g_form pt p
   459 		     | Res => (fst o (get_obj g_result pt)) p
   460 		     | _ => error ("applicable_in: call by "^
   461 					 (pos'2str (p,p_)));
   462   in 
   463       let val subst = subs2subst thy subs
   464 	  val subs' = subst2subs' subst
   465       in case rewrite_set_inst_ thy false subst (assoc_rls rls) f of
   466       SOME (f',asm) => Appl (
   467 	  Detail_Set_Inst' (thy',false,subst,assoc_rls rls, f, (f', asm)))
   468     | NONE => Notappl (rls^" not applicable") end
   469   handle _ => Notappl ("syntax error in "^(subs2str subs)) end
   470 
   471   | applicable_in (p,p_) pt (m as Rewrite_Set_Inst (subs, rls)) = 
   472   if member op = [Pbl,Met] p_ 
   473     then Notappl ((tac2str m)^" not for pos "^(pos'2str (p,p_)))
   474   else
   475   let 
   476     val pp = par_pblobj pt p;
   477     val thy' = (get_obj g_domID pt pp):theory';
   478     val thy = assoc_thy thy';
   479     val {rew_ord'=ro',(*asm_rls=asm_rls,*)...} = 
   480       get_met (get_obj g_metID pt pp);
   481     val (f,p) = case p_ of  (*p 12.4.00 unnecessary*)
   482               Frm => (get_obj g_form pt p, p)
   483 	    | Res => ((fst o (get_obj g_result pt)) p, lev_on p)
   484 	    | _ => error ("applicable_in: call by "^
   485 				(pos'2str (p,p_)));
   486   in 
   487     let val subst = subs2subst thy subs;
   488 	val subs' = subst2subs' subst;
   489     in case rewrite_set_inst_ thy (*put_asm*)false subst (assoc_rls rls) f of
   490       SOME (f',asm) => Appl (
   491 	  Rewrite_Set_Inst' (thy',(*put_asm*)false,subst,assoc_rls rls, f, (f', asm)))
   492     | NONE => Notappl (rls^" not applicable") end
   493   handle _ => Notappl ("syntax error in "^(subs2str subs)) end
   494 
   495   | applicable_in (p,p_) pt (m as Rewrite_Set rls) = 
   496   if member op = [Pbl,Met] p_ 
   497     then Notappl ((tac2str m)^" not for pos "^(pos'2str (p,p_)))
   498   else
   499   let 
   500     val pp = par_pblobj pt p; 
   501     val thy' = (get_obj g_domID pt pp):theory';
   502     val (f,p) = case p_ of  (*p 12.4.00 unnecessary*)
   503               Frm => (get_obj g_form pt p, p)
   504 	    | Res => ((fst o (get_obj g_result pt)) p, lev_on p)
   505 	    | _ => error ("applicable_in: call by "^
   506 				(pos'2str (p,p_)));
   507   in case rewrite_set_ (assoc_thy thy') false (assoc_rls rls) f of
   508        SOME (f',asm) => 
   509 	((*tracing("#.# applicable_in Rewrite_Set,2f'= "^f');*)
   510 	 Appl (Rewrite_Set' (thy',(*put_asm*)false,assoc_rls rls, f, (f', asm)))
   511 	 )
   512      | NONE => Notappl (rls^" not applicable") end
   513 
   514   | applicable_in (p,p_) pt (m as Detail_Set rls) =
   515     if member op = [Pbl,Met] p_ 
   516     then Notappl ((tac2str m)^" not for pos "^(pos'2str (p,p_)))
   517     else
   518 	let val pp = par_pblobj pt p 
   519 	    val thy' = (get_obj g_domID pt pp):theory'
   520 	    val f = case p_ of
   521 			Frm => get_obj g_form pt p
   522 		      | Res => (fst o (get_obj g_result pt)) p
   523 		      | _ => error ("applicable_in: call by "^
   524 					  (pos'2str (p,p_)));
   525 	in case rewrite_set_ (assoc_thy thy') false (assoc_rls rls) f of
   526 	       SOME (f',asm) => 
   527 	       Appl (Detail_Set' (thy',false,assoc_rls rls, f, (f',asm)))
   528 	     | NONE => Notappl (rls^" not applicable") end
   529 
   530 
   531   | applicable_in p pt (End_Ruleset) = 
   532   error ("applicable_in: not impl. for "^
   533 	       (tac2str End_Ruleset))
   534 
   535 (* val ((p,p_), pt, (m as Calculate op_)) = (p, pt, m);
   536    *)
   537 | applicable_in (p,p_) pt (m as Calculate op_) = 
   538   if member op = [Pbl,Met] p_
   539     then Notappl ((tac2str m)^" not for pos "^(pos'2str (p,p_)))
   540   else
   541   let 
   542     val (msg,thy',isa_fn) = from_pblobj_or_detail_calc op_ p pt;
   543     val f = case p_ of
   544               Frm => get_obj g_form pt p
   545 	    | Res => (fst o (get_obj g_result pt)) p
   546   in if msg = "OK" then
   547 	 case calculate_ (assoc_thy thy') isa_fn f of
   548 	     SOME (f', (id, thm)) => 
   549 	     Appl (Calculate' (thy',op_, f, (f', (id, string_of_thmI thm))))
   550 	   | NONE => Notappl ("'calculate "^op_^"' not applicable") 
   551      else Notappl msg
   552   end
   553 
   554 (*Substitute combines two different kind of "substitution":
   555   (1) subst_atomic: for ?a..?z
   556   (2) Pattern.match: for solving equational systems 
   557       (which raises exn for ?a..?z)*)
   558   | applicable_in (p,p_) pt (m as Substitute sube) = 
   559   if member op = [Pbl,Met] p_ 
   560   then Notappl ((tac2str m)^" not for pos "^(pos'2str (p,p_)))
   561   else let val pp = par_pblobj pt p
   562 	   val thy = assoc_thy (get_obj g_domID pt pp)
   563 	   val f = case p_ of
   564 		       Frm => get_obj g_form pt p
   565 		     | Res => (fst o (get_obj g_result pt)) p
   566 	   val {rew_ord',erls,...} = get_met (get_obj g_metID pt pp)
   567 	   val subte = sube2subte sube
   568 	   val subst = sube2subst thy sube
   569        in if foldl and_ (true, map contains_Var subte)
   570 	  (*1*)
   571 	  then let val f' = subst_atomic subst f
   572 	       in if f = f' then Notappl (sube2str sube^" not applicable")
   573 		  else Appl (Substitute' (subte, f, f'))
   574 	       end
   575 	  (*2*)
   576 	  else case rewrite_terms_ thy (assoc_rew_ord rew_ord') 
   577 				   erls subte f of
   578 		   SOME (f', _) =>  Appl (Substitute' (subte, f, f'))
   579 		 | NONE => Notappl (sube2str sube^" not applicable")
   580        end
   581 (*-------WN08114 interrupted with error in polyminus.sml "11 = 11"
   582   | applicable_in (p,p_) pt (m as Substitute sube) = 
   583   if member op = [Pbl,Met] p_ 
   584   then Notappl ((tac2str m)^" not for pos "^(pos'2str (p,p_)))
   585   else let val pp = par_pblobj pt p
   586 	   val thy = assoc_thy (get_obj g_domID pt pp)
   587 	   val f = case p_ of
   588 		       Frm => get_obj g_form pt p
   589 		     | Res => (fst o (get_obj g_result pt)) p
   590 	   val {rew_ord',erls,...} = get_met (get_obj g_metID pt pp)
   591 	   val subte = sube2subte sube
   592        in case rewrite_terms_ thy (assoc_rew_ord rew_ord') erls subte f of
   593 	      SOME (f', _) =>  Appl (Substitute' (subte, f, f'))
   594 	    | NONE => Notappl (sube2str sube^" not applicable")
   595        end
   596 ------------------*)
   597 
   598   | applicable_in p pt (Apply_Assumption cts') = 
   599   (error ("applicable_in: not impl. for "^
   600 	       (tac2str (Apply_Assumption cts'))))
   601   
   602   (*'logical' applicability wrt. script in locate: Inconsistent?*)
   603   | applicable_in (p,p_) pt (m as Take ct') = 
   604      if member op = [Pbl,Met] p_ 
   605        then Notappl ((tac2str m)^" not for pos "^(pos'2str (p,p_)))
   606      else
   607        let val thy' = get_obj g_domID pt (par_pblobj pt p);
   608        in (case parseNEW (assoc_thy thy' |> thy2ctxt) ct' of
   609 	       SOME ct => Appl (Take' ct)
   610 	     | NONE => Notappl ("syntax error in "^ct'))
   611        end
   612 
   613   | applicable_in p pt (Take_Inst ct') = 
   614   error ("applicable_in: not impl. for "^
   615 	       (tac2str (Take_Inst ct')))
   616 
   617   | applicable_in p pt (Group (con, ints)) = 
   618   error ("applicable_in: not impl. for "^
   619 	       (tac2str (Group (con, ints))))
   620 
   621   | applicable_in (p,p_) pt (m as Subproblem (domID, pblID)) = 
   622      if member op = [Pbl,Met] p_
   623        then (*maybe Apply_Method has already been done*)
   624 	 case get_obj g_env pt p of
   625 	     SOME is => Appl (Subproblem' ((domID, pblID, e_metID), [], 
   626 					   e_term, [], subpbl domID pblID))
   627 	   | NONE => Notappl ((tac2str m)^" not for pos "^(pos'2str (p,p_)))
   628      else (*somewhere later in the script*)
   629        Appl (Subproblem' ((domID, pblID, e_metID), [], 
   630 			  e_term, [], subpbl domID pblID))
   631 
   632   | applicable_in p pt (End_Subproblem) =
   633   error ("applicable_in: not impl. for "^
   634 	       (tac2str (End_Subproblem)))
   635 
   636   | applicable_in p pt (CAScmd ct') = 
   637   error ("applicable_in: not impl. for "^
   638 	       (tac2str (CAScmd ct')))
   639   
   640   | applicable_in p pt (Split_And) = 
   641   error ("applicable_in: not impl. for "^
   642 	       (tac2str (Split_And)))
   643   | applicable_in p pt (Conclude_And) = 
   644   error ("applicable_in: not impl. for "^
   645 	       (tac2str (Conclude_And)))
   646   | applicable_in p pt (Split_Or) = 
   647   error ("applicable_in: not impl. for "^
   648 	       (tac2str (Split_Or)))
   649   | applicable_in p pt (Conclude_Or) = 
   650   error ("applicable_in: not impl. for "^
   651 	       (tac2str (Conclude_Or)))
   652 
   653   | applicable_in (p,p_) pt (Begin_Trans) =
   654     let
   655       val (f,p) = case p_ of   (*p 12.4.00 unnecessary*)
   656 	                             (*_____ implizit Take in gen*)
   657 	Frm => (get_obj g_form pt p, (lev_on o lev_dn) p)
   658       | Res => ((fst o (get_obj g_result pt)) p, (lev_on o lev_dn o lev_on) p)
   659       | _ => error ("applicable_in: call by "^
   660 				(pos'2str (p,p_)));
   661       val thy' = get_obj g_domID pt (par_pblobj pt p);
   662     in (Appl (Begin_Trans' f))
   663       handle _ => error ("applicable_in: Begin_Trans finds \
   664                                \syntaxerror in '"^(term2str f)^"'") end
   665 
   666     (*TODO: check parent branches*)
   667   | applicable_in (p,p_) pt (End_Trans) =
   668     let val thy' = get_obj g_domID pt (par_pblobj pt p);
   669     in if p_ = Res 
   670 	   then Appl (End_Trans' (get_obj g_result pt p))
   671        else Notappl "'End_Trans' is not applicable at \
   672 	\the beginning of a transitive sequence"
   673 	 (*TODO: check parent branches*)
   674     end
   675 
   676   | applicable_in p pt (Begin_Sequ) = 
   677   error ("applicable_in: not impl. for "^
   678 	       (tac2str (Begin_Sequ)))
   679   | applicable_in p pt (End_Sequ) = 
   680   error ("applicable_in: not impl. for "^
   681 	       (tac2str (End_Sequ)))
   682   | applicable_in p pt (Split_Intersect) = 
   683   error ("applicable_in: not impl. for "^
   684 	       (tac2str (Split_Intersect)))
   685   | applicable_in p pt (End_Intersect) = 
   686   error ("applicable_in: not impl. for "^
   687 	       (tac2str (End_Intersect)))
   688 (* val Appl (Check_elementwse'(t1,"Assumptions",t2)) = it;
   689    val (vvv,ppp) = vp;
   690 
   691    val Check_elementwise pred = m;
   692    
   693    val ((p,p_), Check_elementwise pred) = (p, m);
   694    *)
   695   | applicable_in (p,p_) pt (m as Check_elementwise pred) = 
   696   if member op = [Pbl,Met] p_ 
   697     then Notappl ((tac2str m)^" not for pos "^(pos'2str (p,p_)))
   698   else
   699   let 
   700     val pp = par_pblobj pt p; 
   701     val thy' = (get_obj g_domID pt pp):theory';
   702     val thy = assoc_thy thy'
   703     val metID = (get_obj g_metID pt pp)
   704     val {crls,...} =  get_met metID
   705     (*val _=tracing("### applicable_in Check_elementwise: crls= "^crls)
   706     val _=tracing("### applicable_in Check_elementwise: pred= "^pred)*)
   707     (*val erl = the (assoc'(!ruleset',crls))*)
   708     val (f,asm) = case p_ of
   709               Frm => (get_obj g_form pt p , [])
   710 	    | Res => get_obj g_result pt p;
   711     (*val _= tracing("### applicable_in Check_elementwise: f= "^f);*)
   712     val vp = (thy2ctxt thy, pred) |-> parseNEW |> the |> mk_set thy pt p f;
   713     (*val (v,p)=vp;val _=tracing("### applicable_in Check_elementwise: vp= "^
   714 			       pair2str(term2str v,term2str p))*)
   715   in case f of
   716       Const ("List.list.Cons",_) $ _ $ _ =>
   717 	Appl (Check_elementwise'
   718 		  (f, pred, 
   719 		   ((*tracing("### applicable_in Check_elementwise: --> "^
   720 			    (res2str (check_elementwise thy crls f vp)));*)
   721 		   check_elementwise thy crls f vp)))
   722     | Const ("Tools.UniversalList",_) => 
   723       Appl (Check_elementwise' (f, pred, (f,asm)))
   724     | Const ("List.list.Nil",_) => 
   725       (*Notappl "not applicable to empty list" 3.6.03*) 
   726       Appl (Check_elementwise' (f, pred, (f,asm(*[] 11.6.03???*))))
   727     | _ => Notappl ("not applicable: "^(term2str f)^" should be constants")
   728   end
   729 
   730   | applicable_in (p,p_) pt Or_to_List = 
   731   if member op = [Pbl,Met] p_ 
   732     then Notappl ((tac2str Or_to_List)^" not for pos "^(pos'2str (p,p_)))
   733   else
   734   let 
   735     val pp = par_pblobj pt p; 
   736     val thy' = (get_obj g_domID pt pp):theory';
   737     val thy = assoc_thy thy';
   738     val f = case p_ of
   739               Frm => get_obj g_form pt p
   740 	    | Res => (fst o (get_obj g_result pt)) p;
   741   in (let val ls = or2list f
   742       in Appl (Or_to_List' (f, ls)) end) 
   743      handle _ => Notappl ("'Or_to_List' not applicable to "^(term2str f))
   744   end
   745 
   746   | applicable_in p pt (Collect_Trues) = 
   747   error ("applicable_in: not impl. for "^
   748 	       (tac2str (Collect_Trues)))
   749 
   750   | applicable_in p pt (Empty_Tac) = 
   751   Notappl "Empty_Tac is not applicable"
   752 
   753   | applicable_in (p,p_) pt (Tac id) =
   754   let 
   755     val pp = par_pblobj pt p; 
   756     val thy' = (get_obj g_domID pt pp):theory';
   757     val thy = assoc_thy thy';
   758     val f = case p_ of
   759               Frm => get_obj g_form pt p
   760             | Pbl => error "applicable_in (p,Pbl) pt (Tac id): not at Pbl"
   761 	    | Res => (fst o (get_obj g_result pt)) p;
   762   in case id of
   763       "subproblem_equation_dummy" =>
   764 	  if is_expliceq f
   765 	  then Appl (Tac_ (thy, term2str f, id,
   766 			     "subproblem_equation_dummy ("^(term2str f)^")"))
   767 	  else Notappl "applicable only to equations made explicit"
   768     | "solve_equation_dummy" =>
   769 	  let (*val _= tracing("### applicable_in: solve_equation_dummy: f= "
   770 				 ^f);*)
   771 	    val (id',f') = split_dummy (term2str f);
   772 	    (*val _= tracing("### applicable_in: f'= "^f');*)
   773 	    (*val _= (term_of o the o (parse thy)) f';*)
   774 	    (*val _= tracing"### applicable_in: solve_equation_dummy";*)
   775 	  in if id' <> "subproblem_equation_dummy" then Notappl "no subproblem"
   776 	     else if (thy2ctxt thy, f') |-> parseNEW |> the |> is_expliceq
   777 		      then Appl (Tac_ (thy, term2str f, id, "[" ^ f' ^ "]"))
   778 		  else error ("applicable_in: f= " ^ f') end
   779     | _ => Appl (Tac_ (thy, term2str f, id, term2str f)) end
   780 
   781   | applicable_in p pt End_Proof' = Appl End_Proof''
   782 
   783   | applicable_in _ _ m = 
   784   error ("applicable_in called for "^(tac2str m));
   785 
   786 (*WN060614 unused*)
   787 fun tac2tac_ pt p m = 
   788     case applicable_in p pt m of
   789 	Appl (m') => m' 
   790       | Notappl _ => error ("tac2mstp': fails with"^
   791 				  (tac2str m));
   792