src/Tools/isac/Interpret/appl.sml
author Walther Neuper <wneuper@ist.tugraz.at>
Tue, 18 Oct 2016 12:05:03 +0200
changeset 59252 7d3dbc1171ff
parent 59250 727dff4f6b2c
child 59253 f0bb15a046ae
permissions -rw-r--r--
back-track after desing error in previous changeset

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