src/Tools/isac/Interpret/appl.sml
author Walther Neuper <neuper@ist.tugraz.at>
Sun, 15 May 2011 11:32:41 +0200
branchdecompose-isar
changeset 41992 1ada058e92bc
parent 41983 4c49adbfadab
child 41993 2301fe2b9f9c
permissions -rw-r--r--
intermed. ctxt ..: checked all occurrences of ProofContext.init_global
     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 script-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)) = Appl (Init_Proof' (ct', spec))
   225 
   226   | applicable_in (p,p_) pt Model_Problem = 
   227       if not (is_pblobj (get_obj I pt p)) orelse p_ = Res
   228       then Notappl ((tac2str Model_Problem) ^ " not for pos " ^ (pos'2str (p,p_)))
   229       else 
   230         let 
   231           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 
   236   | applicable_in (p,p_) pt (Refine_Tacitly pI) = 
   237       if not (is_pblobj (get_obj I pt p)) orelse p_ = Res
   238       then Notappl ((tac2str (Refine_Tacitly pI)) ^ " not for pos " ^ (pos'2str (p,p_)))
   239       else 
   240         let 
   241           val (PblObj {origin = (oris, (dI',_,_),_), ...}) = get_obj I pt p;
   242           val opt = refine_ori oris pI;
   243         in case opt of
   244 	           SOME pblID => 
   245 	             Appl (Refine_Tacitly' (pI, pblID, 
   246 				         e_domID, e_metID, [](*filled in specify*)))
   247 	         | NONE => Notappl ((tac2str (Refine_Tacitly pI)) ^ " not applicable")
   248         end
   249 
   250   | applicable_in (p,p_) pt (Refine_Problem pI) = 
   251   if not (is_pblobj (get_obj I pt p)) orelse p_ = Res                  
   252     then Notappl ((tac2str (Refine_Problem pI))^
   253 	   " not for pos "^(pos'2str (p,p_)))
   254   else
   255     let val (PblObj {origin=(_,(dI,_,_),_),spec=(dI',_,_),
   256 		     probl=itms, ...}) = get_obj I pt p;
   257 	val thy = if dI' = e_domID then dI else dI';
   258 	val rfopt = refine_pbl (assoc_thy thy) pI itms;
   259     in case rfopt of
   260 	   NONE => Notappl ((tac2str (Refine_Problem pI))^" not applicable")
   261 	 | SOME (rf as (pI',_)) =>
   262 (* val SOME (rf as (pI',_)) = rfopt;
   263    *)
   264 	   if pI' = pI
   265 	   then Notappl ((tac2str (Refine_Problem pI))^" not applicable")
   266 	   else Appl (Refine_Problem' rf)
   267     end
   268 
   269   (*the specify-tacs have cterm' instead term: 
   270    parse+error here!!!: see appl_add*)  
   271   | applicable_in (p,p_) pt (Add_Given ct') = 
   272   if not (is_pblobj (get_obj I pt p)) orelse p_ = Res                  
   273     then Notappl ((tac2str (Add_Given ct'))^
   274 	   " not for pos "^(pos'2str (p,p_)))
   275   else Appl (Add_Given' (ct', [(*filled in specify_additem*)]))
   276   (*Add_.. should reject (dsc //) (see fmz=[] in sqrt*)
   277 
   278   | applicable_in (p,p_) pt (Del_Given ct') =
   279   if not (is_pblobj (get_obj I pt p)) orelse p_ = Res                  
   280     then Notappl ((tac2str (Del_Given ct'))^
   281 	   " not for pos "^(pos'2str (p,p_)))
   282   else Appl (Del_Given' ct')
   283 
   284   | applicable_in (p,p_) pt (Add_Find ct') =                   
   285   if not (is_pblobj (get_obj I pt p)) orelse p_ = Res                  
   286     then Notappl ((tac2str (Add_Find ct'))^
   287 	   " not for pos "^(pos'2str (p,p_)))
   288   else Appl (Add_Find' (ct', [(*filled in specify_additem*)]))
   289 
   290   | applicable_in (p,p_) pt (Del_Find ct') =
   291   if not (is_pblobj (get_obj I pt p)) orelse p_ = Res                  
   292     then Notappl ((tac2str (Del_Find ct'))^
   293 	   " not for pos "^(pos'2str (p,p_)))
   294   else Appl (Del_Find' ct')
   295 
   296   | applicable_in (p,p_) pt (Add_Relation ct') =               
   297   if not (is_pblobj (get_obj I pt p)) orelse p_ = Res                  
   298     then Notappl ((tac2str (Add_Relation ct'))^
   299 	   " not for pos "^(pos'2str (p,p_)))
   300   else Appl (Add_Relation' (ct', [(*filled in specify_additem*)]))
   301 
   302   | applicable_in (p,p_) pt (Del_Relation ct') =
   303   if not (is_pblobj (get_obj I pt p)) orelse p_ = Res                  
   304     then Notappl ((tac2str (Del_Relation ct'))^
   305 	   " not for pos "^(pos'2str (p,p_)))
   306   else Appl (Del_Relation' ct')
   307 
   308   | applicable_in (p,p_) pt (Specify_Theory dI) =              
   309   if not (is_pblobj (get_obj I pt p)) orelse p_ = Res                  
   310     then Notappl ((tac2str (Specify_Theory dI))^
   311 	   " not for pos "^(pos'2str (p,p_)))
   312   else Appl (Specify_Theory' dI)
   313 (* val (p,p_) = p; val Specify_Problem pID = m;
   314    val Specify_Problem pID = m;
   315    *)
   316   | applicable_in (p,p_) pt (Specify_Problem pID) = 
   317   if not (is_pblobj (get_obj I pt p)) orelse p_ = Res                  
   318     then Notappl ((tac2str (Specify_Problem pID))^
   319 	   " not for pos "^(pos'2str (p,p_)))
   320   else
   321     let val (PblObj {origin=(oris,(dI,pI,_),_),spec=(dI',pI',_),
   322 		     probl=itms, ...}) = get_obj I pt p;
   323 	val thy = assoc_thy (if dI' = e_domID then dI else dI');
   324         val {ppc,where_,prls,...} = get_pbt pID;
   325 	val pbl = if pI'=e_pblID andalso pI=e_pblID
   326 		  then (false, (init_pbl ppc, []))
   327 		  else match_itms_oris thy itms (ppc,where_,prls) oris;
   328     in Appl (Specify_Problem' (pID, pbl)) end
   329 (* val Specify_Method mID = nxt; val (p,p_) = p; 
   330    *)
   331   | applicable_in (p,p_) pt (Specify_Method mID) =              
   332   if not (is_pblobj (get_obj I pt p)) orelse p_ = Res               
   333     then Notappl ((tac2str (Specify_Method mID))^
   334 	   " not for pos "^(pos'2str (p,p_)))
   335   else Appl (Specify_Method' (mID,[(*filled in specify*)],
   336 			      [(*filled in specify*)]))
   337 
   338   | applicable_in (p,p_) pt (Apply_Method mI) =                
   339       if not (is_pblobj (get_obj I pt p)) orelse p_ = Res                  
   340       then Notappl ((tac2str (Apply_Method mI)) ^ " not for pos " ^ (pos'2str (p,p_)))
   341       else
   342         let
   343           val (PblObj{origin = (_, (dI, pI, _), _), probl, ctxt, ...}) = get_obj I pt p;
   344           val {where_, ...} = get_pbt pI
   345           val pres = map (mk_env probl |> subst_atomic) where_
   346           val ctxt = 
   347             if is_e_ctxt ctxt
   348             then assoc_thy dI |> ProofContext.init_global |> insert_assumptions pres
   349             else ctxt
   350     (*TODO.WN110416 here do evalprecond according to fun check_preconds'
   351       and then decide on Notappl/Appl accordingly once more.
   352       Implement after all tests are running, since this changes
   353       overall system behavior*)
   354   in Appl (Apply_Method' (mI, NONE, e_istate (*filled in solve*), ctxt)) end
   355 
   356   | applicable_in (p,p_) pt (Check_Postcond pI) =
   357   if member op = [Pbl,Met] p_                  
   358     then Notappl ((tac2str (Check_Postcond pI)) ^
   359 	   " not for pos "^(pos'2str (p,p_)))
   360   else Appl (Check_Postcond' 
   361 		 (pI,(e_term,[(*asm in solve*)])))
   362   (* in solve -"-     ^^^^^^ gets returnvalue of scr*)
   363 
   364   (*these are always applicable*)
   365   | applicable_in (p,p_) _ (Take str) = Appl (Take' (str2term str))
   366   | applicable_in (p,p_) _ (Free_Solve) = Appl (Free_Solve')
   367 
   368 (* val m as Rewrite_Inst (subs, thm') = m;
   369    *)
   370   | applicable_in (p,p_) pt (m as Rewrite_Inst (subs, thm')) = 
   371   if member op = [Pbl,Met] p_ 
   372     then Notappl ((tac2str m)^" not for pos "^(pos'2str (p,p_)))
   373   else
   374   let 
   375     val pp = par_pblobj pt p;
   376     val thy' = (get_obj g_domID pt pp):theory';
   377     val thy = assoc_thy thy';
   378     val {rew_ord'=ro',erls=erls,...} = 
   379       get_met (get_obj g_metID pt pp);
   380     val (f,p) = case p_ of (*p 12.4.00 unnecessary*)
   381               Frm => (get_obj g_form pt p, p)
   382 	    | Res => ((fst o (get_obj g_result pt)) p, lev_on p)
   383 	    | _ => error ("applicable_in: call by "^
   384 				(pos'2str (p,p_)));
   385   in 
   386     let val subst = subs2subst thy subs;
   387 	val subs' = subst2subs' subst;
   388     in case rewrite_inst_ thy (assoc_rew_ord ro') erls
   389 			 (*put_asm*)false subst (assoc_thm' thy thm') f of
   390       SOME (f',asm) => Appl (
   391 	  Rewrite_Inst' (thy',ro',erls,(*put_asm*)false,subst,thm',
   392       (*term_of o the o (parse (assoc_thy thy'))*) f,
   393        (*(term_of o the o (parse (assoc_thy thy'))*) (f',
   394 	(*map (term_of o the o (parse (assoc_thy thy')))*) asm)))
   395     | NONE => Notappl ((fst thm')^" not applicable") end
   396   handle _ => Notappl ("syntax error in "^(subs2str subs)) end
   397 
   398 (* val ((p,p_), pt, m as Rewrite thm') = (p, pt, m);
   399    val ((p,p_), pt, m as Rewrite thm') = (pos, pt, tac);
   400    *)
   401 | applicable_in (p,p_) pt (m as Rewrite thm') = 
   402   if member op = [Pbl,Met] p_ 
   403     then Notappl ((tac2str m)^" not for pos "^(pos'2str (p,p_)))
   404   else
   405   let val (msg,thy',ro,rls',(*put_asm*)_)= from_pblobj_or_detail_thm thm' p pt;
   406     val thy = assoc_thy thy';
   407     val f = case p_ of
   408               Frm => get_obj g_form pt p
   409 	    | Res => (fst o (get_obj g_result pt)) p
   410 	    | _ => error ("applicable_in Rewrite: call by "^
   411 				(pos'2str (p,p_)));
   412   in if msg = "OK" 
   413      then
   414       ((*tracing("### applicable_in rls'= "^rls');*)
   415        (* val SOME (f',asm)=rewrite thy' ro (id_rls rls') put_asm thm' f;
   416 	  *)
   417        case rewrite_ thy (assoc_rew_ord ro) 
   418 		     rls' false (assoc_thm' thy thm') f of
   419        SOME (f',asm) => Appl (
   420 	   Rewrite' (thy',ro,rls',(*put_asm*)false,thm', f, (f', asm)))
   421      | NONE => Notappl ("'"^(fst thm')^"' not applicable") )
   422      else Notappl msg
   423   end
   424 
   425 | applicable_in (p,p_) pt (m as Rewrite_Asm thm') = 
   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',erls=erls,...} = 
   434       get_met (get_obj g_metID pt pp);
   435     (*val put_asm = true;*)
   436     val (f,p) = case p_ of  (*p 12.4.00 unnecessary*)
   437               Frm => (get_obj g_form pt p, p)
   438 	    | Res => ((fst o (get_obj g_result pt)) p, lev_on p)
   439 	    | _ => error ("applicable_in: call by "^
   440 				(pos'2str (p,p_)));
   441   in case rewrite_ thy (assoc_rew_ord ro') erls 
   442 		   (*put_asm*)false (assoc_thm' thy thm') f of
   443        SOME (f',asm) => Appl (
   444 	   Rewrite' (thy',ro',erls,(*put_asm*)false,thm', f, (f', asm)))
   445      | NONE => Notappl ("'"^(fst thm')^"' not applicable") end
   446 
   447   | applicable_in (p,p_) pt (m as Detail_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',...} = get_met (get_obj g_metID pt pp);
   456     val f = case p_ of Frm => get_obj g_form pt p
   457 		     | Res => (fst o (get_obj g_result pt)) p
   458 		     | _ => error ("applicable_in: call by "^
   459 					 (pos'2str (p,p_)));
   460   in 
   461       let val subst = subs2subst thy subs
   462 	  val subs' = subst2subs' subst
   463       in case rewrite_set_inst_ thy false subst (assoc_rls rls) f of
   464       SOME (f',asm) => Appl (
   465 	  Detail_Set_Inst' (thy',false,subst,assoc_rls rls, f, (f', asm)))
   466     | NONE => Notappl (rls^" not applicable") end
   467   handle _ => Notappl ("syntax error in "^(subs2str subs)) end
   468 
   469   | applicable_in (p,p_) pt (m as Rewrite_Set_Inst (subs, rls)) = 
   470   if member op = [Pbl,Met] p_ 
   471     then Notappl ((tac2str m)^" not for pos "^(pos'2str (p,p_)))
   472   else
   473   let 
   474     val pp = par_pblobj pt p;
   475     val thy' = (get_obj g_domID pt pp):theory';
   476     val thy = assoc_thy thy';
   477     val {rew_ord'=ro',(*asm_rls=asm_rls,*)...} = 
   478       get_met (get_obj g_metID pt pp);
   479     val (f,p) = case p_ of  (*p 12.4.00 unnecessary*)
   480               Frm => (get_obj g_form pt p, p)
   481 	    | Res => ((fst o (get_obj g_result pt)) p, lev_on p)
   482 	    | _ => error ("applicable_in: call by "^
   483 				(pos'2str (p,p_)));
   484   in 
   485     let val subst = subs2subst thy subs;
   486 	val subs' = subst2subs' subst;
   487     in case rewrite_set_inst_ thy (*put_asm*)false subst (assoc_rls rls) f of
   488       SOME (f',asm) => Appl (
   489 	  Rewrite_Set_Inst' (thy',(*put_asm*)false,subst,assoc_rls rls, f, (f', asm)))
   490     | NONE => Notappl (rls^" not applicable") end
   491   handle _ => Notappl ("syntax error in "^(subs2str subs)) end
   492 
   493   | applicable_in (p,p_) pt (m as Rewrite_Set rls) = 
   494   if member op = [Pbl,Met] p_ 
   495     then Notappl ((tac2str m)^" not for pos "^(pos'2str (p,p_)))
   496   else
   497   let 
   498     val pp = par_pblobj pt p; 
   499     val thy' = (get_obj g_domID pt pp):theory';
   500     val (f,p) = case p_ of  (*p 12.4.00 unnecessary*)
   501               Frm => (get_obj g_form pt p, p)
   502 	    | Res => ((fst o (get_obj g_result pt)) p, lev_on p)
   503 	    | _ => error ("applicable_in: call by "^
   504 				(pos'2str (p,p_)));
   505   in case rewrite_set_ (assoc_thy thy') false (assoc_rls rls) f of
   506        SOME (f',asm) => 
   507 	((*tracing("#.# applicable_in Rewrite_Set,2f'= "^f');*)
   508 	 Appl (Rewrite_Set' (thy',(*put_asm*)false,assoc_rls rls, f, (f', asm)))
   509 	 )
   510      | NONE => Notappl (rls^" not applicable") end
   511 
   512   | applicable_in (p,p_) pt (m as Detail_Set rls) =
   513     if member op = [Pbl,Met] p_ 
   514     then Notappl ((tac2str m)^" not for pos "^(pos'2str (p,p_)))
   515     else
   516 	let val pp = par_pblobj pt p 
   517 	    val thy' = (get_obj g_domID pt pp):theory'
   518 	    val f = case p_ of
   519 			Frm => get_obj g_form pt p
   520 		      | Res => (fst o (get_obj g_result pt)) p
   521 		      | _ => error ("applicable_in: call by "^
   522 					  (pos'2str (p,p_)));
   523 	in case rewrite_set_ (assoc_thy thy') false (assoc_rls rls) f of
   524 	       SOME (f',asm) => 
   525 	       Appl (Detail_Set' (thy',false,assoc_rls rls, f, (f',asm)))
   526 	     | NONE => Notappl (rls^" not applicable") end
   527 
   528 
   529   | applicable_in p pt (End_Ruleset) = 
   530   error ("applicable_in: not impl. for "^
   531 	       (tac2str End_Ruleset))
   532 
   533 (* val ((p,p_), pt, (m as Calculate op_)) = (p, pt, m);
   534    *)
   535 | applicable_in (p,p_) pt (m as Calculate op_) = 
   536   if member op = [Pbl,Met] p_
   537     then Notappl ((tac2str m)^" not for pos "^(pos'2str (p,p_)))
   538   else
   539   let 
   540     val (msg,thy',isa_fn) = from_pblobj_or_detail_calc op_ p pt;
   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   in if msg = "OK" then
   545 	 case calculate_ (assoc_thy thy') isa_fn f of
   546 	     SOME (f', (id, thm)) => 
   547 	     Appl (Calculate' (thy',op_, f, (f', (id, string_of_thmI thm))))
   548 	   | NONE => Notappl ("'calculate "^op_^"' not applicable") 
   549      else Notappl msg
   550   end
   551 
   552 (*Substitute combines two different kind of "substitution":
   553   (1) subst_atomic: for ?a..?z
   554   (2) Pattern.match: for solving equational systems 
   555       (which raises exn for ?a..?z)*)
   556   | applicable_in (p,p_) pt (m as Substitute sube) = 
   557   if member op = [Pbl,Met] p_ 
   558   then Notappl ((tac2str m)^" not for pos "^(pos'2str (p,p_)))
   559   else let val pp = par_pblobj pt p
   560 	   val thy = assoc_thy (get_obj g_domID pt pp)
   561 	   val f = case p_ of
   562 		       Frm => get_obj g_form pt p
   563 		     | Res => (fst o (get_obj g_result pt)) p
   564 	   val {rew_ord',erls,...} = get_met (get_obj g_metID pt pp)
   565 	   val subte = sube2subte sube
   566 	   val subst = sube2subst thy sube
   567        in if foldl and_ (true, map contains_Var subte)
   568 	  (*1*)
   569 	  then let val f' = subst_atomic subst f
   570 	       in if f = f' then Notappl (sube2str sube^" not applicable")
   571 		  else Appl (Substitute' (subte, f, f'))
   572 	       end
   573 	  (*2*)
   574 	  else case rewrite_terms_ thy (assoc_rew_ord rew_ord') 
   575 				   erls subte f of
   576 		   SOME (f', _) =>  Appl (Substitute' (subte, f, f'))
   577 		 | NONE => Notappl (sube2str sube^" not applicable")
   578        end
   579 (*-------WN08114 interrupted with error in polyminus.sml "11 = 11"
   580   | applicable_in (p,p_) pt (m as Substitute sube) = 
   581   if member op = [Pbl,Met] p_ 
   582   then Notappl ((tac2str m)^" not for pos "^(pos'2str (p,p_)))
   583   else let val pp = par_pblobj pt p
   584 	   val thy = assoc_thy (get_obj g_domID pt pp)
   585 	   val f = case p_ of
   586 		       Frm => get_obj g_form pt p
   587 		     | Res => (fst o (get_obj g_result pt)) p
   588 	   val {rew_ord',erls,...} = get_met (get_obj g_metID pt pp)
   589 	   val subte = sube2subte sube
   590        in case rewrite_terms_ thy (assoc_rew_ord rew_ord') erls subte f of
   591 	      SOME (f', _) =>  Appl (Substitute' (subte, f, f'))
   592 	    | NONE => Notappl (sube2str sube^" not applicable")
   593        end
   594 ------------------*)
   595 
   596   | applicable_in p pt (Apply_Assumption cts') = 
   597   (error ("applicable_in: not impl. for "^
   598 	       (tac2str (Apply_Assumption cts'))))
   599   
   600   (*'logical' applicability wrt. script in locate: Inconsistent?*)
   601   | applicable_in (p,p_) pt (m as Take ct') = 
   602      if member op = [Pbl,Met] p_ 
   603        then Notappl ((tac2str m)^" not for pos "^(pos'2str (p,p_)))
   604      else
   605        let val thy' = get_obj g_domID pt (par_pblobj pt p);
   606        in (case parseNEW (assoc_thy thy' |> thy2ctxt) ct' of
   607 	       SOME ct => Appl (Take' ct)
   608 	     | NONE => Notappl ("syntax error in "^ct'))
   609        end
   610 
   611   | applicable_in p pt (Take_Inst ct') = 
   612   error ("applicable_in: not impl. for "^
   613 	       (tac2str (Take_Inst ct')))
   614 
   615   | applicable_in p pt (Group (con, ints)) = 
   616   error ("applicable_in: not impl. for "^
   617 	       (tac2str (Group (con, ints))))
   618 
   619   | applicable_in (p,p_) pt (m as Subproblem (domID, pblID)) = 
   620      if member op = [Pbl,Met] p_
   621        then (*maybe Apply_Method has already been done*)
   622 	 case get_obj g_env pt p of
   623 	     SOME is => Appl (Subproblem' ((domID, pblID, e_metID), [], 
   624 					   e_term, [], e_ctxt, subpbl domID pblID))
   625 	   | NONE => Notappl ((tac2str m)^" not for pos "^(pos'2str (p,p_)))
   626      else (*somewhere later in the script*)
   627        Appl (Subproblem' ((domID, pblID, e_metID), [], 
   628 			  e_term, [], e_ctxt, subpbl domID pblID))
   629 
   630   | applicable_in p pt (End_Subproblem) =
   631   error ("applicable_in: not impl. for "^
   632 	       (tac2str (End_Subproblem)))
   633 
   634   | applicable_in p pt (CAScmd ct') = 
   635   error ("applicable_in: not impl. for "^
   636 	       (tac2str (CAScmd ct')))
   637   
   638   | applicable_in p pt (Split_And) = 
   639   error ("applicable_in: not impl. for "^
   640 	       (tac2str (Split_And)))
   641   | applicable_in p pt (Conclude_And) = 
   642   error ("applicable_in: not impl. for "^
   643 	       (tac2str (Conclude_And)))
   644   | applicable_in p pt (Split_Or) = 
   645   error ("applicable_in: not impl. for "^
   646 	       (tac2str (Split_Or)))
   647   | applicable_in p pt (Conclude_Or) = 
   648   error ("applicable_in: not impl. for "^
   649 	       (tac2str (Conclude_Or)))
   650 
   651   | applicable_in (p,p_) pt (Begin_Trans) =
   652     let
   653       val (f,p) = case p_ of   (*p 12.4.00 unnecessary*)
   654 	                             (*_____ implizit Take in gen*)
   655 	Frm => (get_obj g_form pt p, (lev_on o lev_dn) p)
   656       | Res => ((fst o (get_obj g_result pt)) p, (lev_on o lev_dn o lev_on) p)
   657       | _ => error ("applicable_in: call by "^
   658 				(pos'2str (p,p_)));
   659       val thy' = get_obj g_domID pt (par_pblobj pt p);
   660     in (Appl (Begin_Trans' f))
   661       handle _ => error ("applicable_in: Begin_Trans finds \
   662                                \syntaxerror in '"^(term2str f)^"'") end
   663 
   664     (*TODO: check parent branches*)
   665   | applicable_in (p,p_) pt (End_Trans) =
   666     let val thy' = get_obj g_domID pt (par_pblobj pt p);
   667     in if p_ = Res 
   668 	   then Appl (End_Trans' (get_obj g_result pt p))
   669        else Notappl "'End_Trans' is not applicable at \
   670 	\the beginning of a transitive sequence"
   671 	 (*TODO: check parent branches*)
   672     end
   673 
   674   | applicable_in p pt (Begin_Sequ) = 
   675   error ("applicable_in: not impl. for "^
   676 	       (tac2str (Begin_Sequ)))
   677   | applicable_in p pt (End_Sequ) = 
   678   error ("applicable_in: not impl. for "^
   679 	       (tac2str (End_Sequ)))
   680   | applicable_in p pt (Split_Intersect) = 
   681   error ("applicable_in: not impl. for "^
   682 	       (tac2str (Split_Intersect)))
   683   | applicable_in p pt (End_Intersect) = 
   684   error ("applicable_in: not impl. for "^
   685 	       (tac2str (End_Intersect)))
   686 (* val Appl (Check_elementwse'(t1,"Assumptions",t2)) = it;
   687    val (vvv,ppp) = vp;
   688 
   689    val Check_elementwise pred = m;
   690    
   691    val ((p,p_), Check_elementwise pred) = (p, m);
   692    *)
   693   | applicable_in (p,p_) pt (m as Check_elementwise pred) = 
   694   if member op = [Pbl,Met] p_ 
   695     then Notappl ((tac2str m)^" not for pos "^(pos'2str (p,p_)))
   696   else
   697   let 
   698     val pp = par_pblobj pt p; 
   699     val thy' = (get_obj g_domID pt pp):theory';
   700     val thy = assoc_thy thy'
   701     val metID = (get_obj g_metID pt pp)
   702     val {crls,...} =  get_met metID
   703     (*val _=tracing("### applicable_in Check_elementwise: crls= "^crls)
   704     val _=tracing("### applicable_in Check_elementwise: pred= "^pred)*)
   705     (*val erl = the (assoc'(!ruleset',crls))*)
   706     val (f,asm) = case p_ of
   707               Frm => (get_obj g_form pt p , [])
   708 	    | Res => get_obj g_result pt p;
   709     (*val _= tracing("### applicable_in Check_elementwise: f= "^f);*)
   710     val vp = (thy2ctxt thy, pred) |-> parseNEW |> the |> mk_set thy pt p f;
   711     (*val (v,p)=vp;val _=tracing("### applicable_in Check_elementwise: vp= "^
   712 			       pair2str(term2str v,term2str p))*)
   713   in case f of
   714       Const ("List.list.Cons",_) $ _ $ _ =>
   715 	Appl (Check_elementwise'
   716 		  (f, pred, 
   717 		   ((*tracing("### applicable_in Check_elementwise: --> "^
   718 			    (res2str (check_elementwise thy crls f vp)));*)
   719 		   check_elementwise thy crls f vp)))
   720     | Const ("Tools.UniversalList",_) => 
   721       Appl (Check_elementwise' (f, pred, (f,asm)))
   722     | Const ("List.list.Nil",_) => 
   723       (*Notappl "not applicable to empty list" 3.6.03*) 
   724       Appl (Check_elementwise' (f, pred, (f,asm(*[] 11.6.03???*))))
   725     | _ => Notappl ("not applicable: "^(term2str f)^" should be constants")
   726   end
   727 
   728   | applicable_in (p,p_) pt Or_to_List = 
   729   if member op = [Pbl,Met] p_ 
   730     then Notappl ((tac2str Or_to_List)^" not for pos "^(pos'2str (p,p_)))
   731   else
   732   let 
   733     val pp = par_pblobj pt p; 
   734     val thy' = (get_obj g_domID pt pp):theory';
   735     val thy = assoc_thy thy';
   736     val f = case p_ of
   737               Frm => get_obj g_form pt p
   738 	    | Res => (fst o (get_obj g_result pt)) p;
   739   in (let val ls = or2list f
   740       in Appl (Or_to_List' (f, ls)) end) 
   741      handle _ => Notappl ("'Or_to_List' not applicable to "^(term2str f))
   742   end
   743 
   744   | applicable_in p pt (Collect_Trues) = 
   745   error ("applicable_in: not impl. for "^
   746 	       (tac2str (Collect_Trues)))
   747 
   748   | applicable_in p pt (Empty_Tac) = 
   749   Notappl "Empty_Tac is not applicable"
   750 
   751   | applicable_in (p,p_) pt (Tac id) =
   752   let 
   753     val pp = par_pblobj pt p; 
   754     val thy' = (get_obj g_domID pt pp):theory';
   755     val thy = assoc_thy thy';
   756     val f = case p_ of
   757               Frm => get_obj g_form pt p
   758             | Pbl => error "applicable_in (p,Pbl) pt (Tac id): not at Pbl"
   759 	    | Res => (fst o (get_obj g_result pt)) p;
   760   in case id of
   761       "subproblem_equation_dummy" =>
   762 	  if is_expliceq f
   763 	  then Appl (Tac_ (thy, term2str f, id,
   764 			     "subproblem_equation_dummy ("^(term2str f)^")"))
   765 	  else Notappl "applicable only to equations made explicit"
   766     | "solve_equation_dummy" =>
   767 	  let (*val _= tracing("### applicable_in: solve_equation_dummy: f= "
   768 				 ^f);*)
   769 	    val (id',f') = split_dummy (term2str f);
   770 	    (*val _= tracing("### applicable_in: f'= "^f');*)
   771 	    (*val _= (term_of o the o (parse thy)) f';*)
   772 	    (*val _= tracing"### applicable_in: solve_equation_dummy";*)
   773 	  in if id' <> "subproblem_equation_dummy" then Notappl "no subproblem"
   774 	     else if (thy2ctxt thy, f') |-> parseNEW |> the |> is_expliceq
   775 		      then Appl (Tac_ (thy, term2str f, id, "[" ^ f' ^ "]"))
   776 		  else error ("applicable_in: f= " ^ f') end
   777     | _ => Appl (Tac_ (thy, term2str f, id, term2str f)) end
   778 
   779   | applicable_in p pt End_Proof' = Appl End_Proof''
   780 
   781   | applicable_in _ _ m = 
   782   error ("applicable_in called for "^(tac2str m));
   783 
   784 (*WN060614 unused*)
   785 fun tac2tac_ pt p m = 
   786     case applicable_in p pt m of
   787 	Appl (m') => m' 
   788       | Notappl _ => error ("tac2mstp': fails with"^
   789 				  (tac2str m));
   790