src/Tools/isac/ProgLang/rewrite.sml
author Walther Neuper <neuper@ist.tugraz.at>
Mon, 21 Feb 2011 19:40:36 +0100
branchdecompose-isar
changeset 40836 69364e021751
parent 38058 ad0485155c0e
child 41899 d837e83a4835
permissions -rw-r--r--
part.update Isabelle2011

works neither on 2009-2 nor on 2011
     1 (* isac's rewriter
     2    (c) Walther Neuper 2000
     3 
     4 use"ProgLang/rewrite.sml"; 
     5 use"rewrite.sml";
     6 *)
     7 
     8 
     9 exception NO_REWRITE;
    10 exception STOP_REW_SUB; (*WN050820 quick and dirty*)
    11 
    12 (*17.6.00: rewrite by going down the term with rew_sub*)
    13 (* val (thy, i, bdv, tless, rls, put_asm, thm, ct) =
    14        (thy, 1, []:(Term.term * Term.term) list, rew_ord, erls, bool,thm,term);
    15    *)
    16 fun rewrite__ thy i bdv tless rls put_asm thm ct =
    17   (let
    18     val (t',asms,lrd,rew) = 
    19 	rew_sub thy i bdv tless rls put_asm [(*root of the term*)]
    20 		(((inst_bdv bdv) o norm o #prop o rep_thm) thm) ct;
    21   in if rew then SOME (t', distinct asms)
    22      else NONE end)
    23 (* one rewrite (possibly conditional, ordered) EXOR exn EXOR go into subterms *)
    24 and rew_sub thy i bdv tless rls put_asm lrd r t = 
    25   ((*tracing ("@@@ rew_sub begin: t = "^(term2str t));*)
    26     let                  (* copy from Pure/thm.ML: fun rewritec *)
    27      val (lhs, rhs) = (HOLogic.dest_eq o HOLogic.dest_Trueprop
    28                        o Logic.strip_imp_concl) r;
    29      val r' = Envir.subst_term (Pattern.match thy (lhs, t) 
    30 					      (Vartab.empty, Vartab.empty)) r;
    31      val p' = map HOLogic.dest_Trueprop ((fst o Logic.strip_prems) 
    32                                              (Logic.count_prems r', [], r'));
    33      val t' = (snd o HOLogic.dest_eq o HOLogic.dest_Trueprop 
    34                o Logic.strip_imp_concl) r';
    35      (*val _= tracing("@@@ rew_sub match: t'= "^(term2str t'));*)
    36      val _= if ! trace_rewrite andalso i < ! depth andalso p' <> []
    37 	    then tracing((idt"#"(i+1))^" eval asms: "^(term2str r')) else();
    38      val (t'',p'') =                                   (*conditional rewriting*)
    39 	 let val (simpl_p', nofalse) = eval__true thy (i+1) p' bdv rls 	     
    40 	 in if nofalse
    41 	    then (if ! trace_rewrite andalso i < ! depth andalso p' <> []
    42 		  then tracing((idt"#"(i+1))^" asms accepted: "^(terms2str p')^
    43 			       "   stored: "^(terms2str simpl_p'))
    44 		  else(); (t',simpl_p'))             (* uncond.rew. from above*)
    45 	    else 
    46 		(if ! trace_rewrite andalso i < ! depth 
    47 		 then tracing((idt"#"(i+1))^" asms false: "^(terms2str p')) 
    48 		 else(); raise STOP_REW_SUB (*dont go into subterms of cond*))
    49 	 end
    50    in if perm lhs rhs andalso not (tless bdv (t',t))       (*ordered rewriting*)
    51 	then (if ! trace_rewrite andalso i < ! depth 
    52 	      then tracing((idt"#"i)^" not: \""^
    53 	      (term2str t)^"\" > \""^
    54 	      (term2str t')^"\"") else (); 
    55 	      raise NO_REWRITE )
    56 	else ((*tracing("##@ rew_sub: (t''= "^(term2str t'')^
    57 		      ", p'' ="^(terms2str p'')^", true)");*)
    58 	      (t'',p'',[],true))
    59    end
    60    ) handle _ (*NO_REWRITE WN050820 causes diff.behav. in tests + MATCH!*) => 
    61      ((*tracing ("@@@ rew_sub gosub: t = "^(term2str t));*)
    62       case t of
    63 	Const(s,T) => (Const(s,T),[],lrd,false)
    64       | Free(s,T) => (Free(s,T),[],lrd,false)
    65       | Var(n,T) => (Var(n,T),[],lrd,false)
    66       | Bound i => (Bound i,[],lrd,false)
    67       | Abs(s,T,body) => 
    68 	  let val (t', asms, lrd, rew) = 
    69 		  rew_sub thy i bdv tless rls put_asm (lrd@[D]) r body
    70 	  in (Abs(s,T,t'), asms, [], rew) end
    71       | t1 $ t2 => 
    72 	  let val (t2', asm2, lrd, rew2) = 
    73 		  rew_sub thy i bdv tless rls put_asm (lrd@[R]) r t2
    74 	  in if rew2 then (t1 $ t2', asm2, lrd, true)
    75 	     else let val (t1', asm1, lrd, rew1) = 
    76 	       rew_sub thy i bdv tless rls put_asm (lrd@[L]) r t1
    77 		  in if rew1 then (t1' $ t2, asm1, lrd, true)
    78 		     else (t1 $ t2,[], lrd, false) end
    79 	  end)
    80 (* simplify asumptions until one evaluates to false, store intermined's (x~=0)*)
    81 and eval__true thy i asms bdv rls =
    82   if asms = [HOLogic.true_const] orelse asms = [] then ([], true)
    83   (* this allows to check Rrls with prepat = ([HOLogic.true_const], pat) *)
    84   else if asms = [HOLogic.false_const] then ([], false)
    85   else let                            
    86       fun chk indets [] = (indets, true)(*return asms<>True until false*)
    87 	| chk indets (a::asms) =
    88 	  (case rewrite__set_ thy (i+1) false bdv rls a of
    89 	      NONE => (chk (indets @ [a]) asms)
    90 	    | SOME (t, a') =>
    91 	      if t = HOLogic.true_const 
    92 	      then (chk (indets @ a') asms)
    93 	      else if t = HOLogic.false_const then ([], false)
    94 	      (*asm false .. thm not applied ^^^; continue until False vvv*)
    95 	      else chk (indets @ [t] @ a') asms);
    96   in chk [] asms end
    97 (* rewrite with a rule set, which must not be the empty Erls *)
    98 and rewrite__set_ _ _ __ Erls t = 
    99     error("rewrite__set_ called with 'Erls' for '"^term2str t^"'")
   100   (* rewrite with a 'reverse rule set' implemented by ML code *)
   101   | rewrite__set_ thy i _ _ (rrls as Rrls _) t =
   102     let val _= if ! trace_rewrite andalso i < ! depth 
   103 	       then tracing ((idt"#"i)^" rls: "^(id_rls rrls)^" on: "^
   104 			     (term2str t)) else ()
   105 	val (t', asm, rew) = app_rev thy (i+1) rrls t
   106     in if rew then SOME (t', distinct asm)
   107        else NONE end
   108   (* rewrite with a rule set containing Thms or Calculations *)
   109   | rewrite__set_ thy i put_asm bdv rls ct =
   110 (* val (thy, i, put_asm, bdv, rls, ct) = (thy, 1, bool, [], rls, term);
   111    *)
   112   let
   113     datatype switch = Appl | Noap;
   114     fun rew_once ruls asm ct Noap [] = (ct,asm)
   115       | rew_once ruls asm ct Appl [] = 
   116 	(case rls of Rls _ => rew_once ruls asm ct Noap ruls
   117 		   | Seq _ => (ct,asm))
   118       | rew_once ruls asm ct apno (rul::thms) =
   119 (* val (ruls, asm, ct, apno, (rul::thms)) = (ruls, [], ct, Noap, ruls);
   120    val Thm (thmid, thm) = rul;
   121    *)
   122       case rul of
   123 	Thm (thmid, thm) =>
   124 	  (if !trace_rewrite andalso i < ! depth 
   125 	   then tracing((idt"#"(i+1))^" try thm: "^thmid) else ();
   126 	   case rewrite__ thy (i+1) bdv ((snd o #rew_ord o rep_rls) rls)
   127 	     ((#erls o rep_rls) rls) put_asm thm ct of
   128 	     NONE => rew_once ruls asm ct apno thms
   129 	   | SOME (ct',asm') => (if ! trace_rewrite andalso i < ! depth 
   130 	     then tracing((idt"="(i+1))^" rewrites to: "^
   131 			  (term2str ct')) else ();
   132 	       rew_once ruls (union (op =) asm asm') ct' Appl (rul::thms)))
   133       | Calc (cc as (op_,_)) => 
   134 	  (let val _= if !trace_rewrite andalso i < ! depth then
   135 		      tracing((idt"#"(i+1))^" try calc: "^op_^"'") else ();
   136 	     val ct = uminus_to_string ct
   137 	   in case get_calculation_ thy cc ct of
   138 	     NONE => rew_once ruls asm ct apno thms
   139 	   | SOME (thmid, thm') => 
   140 	       let 
   141 		 val pairopt = 
   142 		   rewrite__ thy (i+1) bdv ((snd o #rew_ord o rep_rls) rls)
   143 		   ((#erls o rep_rls) rls) put_asm thm' ct;
   144 		 val _ = if pairopt <> NONE then () 
   145 			 else error("rewrite_set_, rewrite_ \""^
   146 			 (string_of_thmI thm')^"\" "^(term2str ct)^" = NONE")
   147 		 val _ = if ! trace_rewrite andalso i < ! depth 
   148 			   then tracing((idt"="(i+1))^" calc. to: "^
   149 					(term2str ((fst o the) pairopt)))
   150 			 else()
   151 	       in rew_once ruls asm ((fst o the) pairopt) Appl (rul::thms) end
   152 	   end)
   153       | Cal1 (cc as (op_,_)) => 
   154 	  (let val _= if !trace_rewrite andalso i < ! depth then
   155 		      tracing((idt"#"(i+1))^" try cal1: "^op_^"'") else ();
   156 	     val ct = uminus_to_string ct
   157 	   in case get_calculation1_ thy cc ct of
   158 	     NONE => (ct, asm)
   159 	   | SOME (thmid, thm') =>
   160 	       let 
   161 		 val pairopt = 
   162 		   rewrite__ thy (i+1) bdv ((snd o #rew_ord o rep_rls) rls)
   163 		   ((#erls o rep_rls) rls) put_asm thm' ct;
   164 		 val _ = if pairopt <> NONE then () 
   165 			 else error("rewrite_set_, rewrite_ \""^
   166 			 (string_of_thmI thm')^"\" "^(term2str ct)^" = NONE")
   167 		 val _ = if ! trace_rewrite andalso i < ! depth 
   168 			   then tracing((idt"="(i+1))^" cal1. to: "^
   169 					(term2str ((fst o the) pairopt)))
   170 			 else()
   171 	       in the pairopt end
   172 	   end)
   173       | Rls_ rls' => 
   174 	(case rewrite__set_ thy (i+1) put_asm bdv rls' ct of
   175 	     SOME (t',asm') => rew_once ruls (union (op =) asm asm') t' Appl thms
   176 	   | NONE => rew_once ruls asm ct apno thms);
   177 
   178     val ruls = (#rules o rep_rls) rls;
   179     val _= if ! trace_rewrite andalso i < ! depth 
   180 	   then tracing ((idt"#"i)^" rls: "^(id_rls rls)^" on: "^
   181 			 (term2str ct)) else ()
   182     val (ct',asm') = rew_once ruls [] ct Noap ruls;
   183   in if ct = ct' then NONE else SOME (ct', distinct asm') end
   184 
   185 and app_rev thy i rrls t = 
   186     let (* check a (precond, pattern) of a rev-set; stops with 1st true *)
   187 	fun chk_prepat thy erls [] t = true
   188 	  | chk_prepat thy erls prepat t =
   189 	    let fun chk (pres, pat) =
   190 		    (let val subst: Type.tyenv * Envir.tenv = 
   191 			     Pattern.match thy (pat, t)
   192 					    (Vartab.empty, Vartab.empty)
   193 		     in snd (eval__true thy (i+1) 
   194 					(map (Envir.subst_term subst) pres)
   195 					[] erls)
   196 		     end)
   197 		    handle _ => false
   198 		fun scan_ f [] = false (*scan_ NEVER called by []*)
   199 		  | scan_ f (pp::pps) = if f pp then true
   200 					else scan_ f pps;
   201 	    in scan_ chk prepat end;
   202 
   203 	(* apply the normal_form of a rev-set *)
   204 	fun app_rev' thy (Rrls{erls,prepat,scr=Rfuns{normal_form,...},...}) t =
   205 	    if chk_prepat thy erls prepat t
   206 	    then ((*tracing("### app_rev': t = "^(term2str t));*)
   207                   normal_form t)
   208 	    else NONE;
   209 
   210 	val opt = app_rev' thy rrls t
   211     in case opt of
   212 	   SOME (t', asm) => (t', asm, true)
   213 	 | NONE => app_sub thy i rrls t
   214     end
   215 and app_sub thy i rrls t =
   216      ((*tracing("### app_sub: subterm = "^(term2str t));*)
   217       case t of
   218 	Const (s, T) => (Const(s, T), [], false)
   219       | Free (s, T) => (Free(s, T), [], false)
   220       | Var (n, T) => (Var(n, T), [], false)
   221       | Bound i => (Bound i, [], false)
   222       | Abs (s, T, body) => 
   223 	  let val (t', asm, rew) = app_rev thy i rrls body
   224 	  in (Abs(s, T, t'), asm, rew) end
   225       | t1 $ t2 => 
   226 	let val (t2', asm2, rew2) = app_rev thy i rrls t2
   227 	in if rew2 then (t1 $ t2', asm2, true)
   228 	   else let val (t1', asm1, rew1) = app_rev thy i rrls t1
   229 		in if rew1 then (t1' $ t2, asm1, true)
   230 		   else (t1 $ t2, [], false) end
   231 	end);
   232 
   233 
   234 
   235 (*.rewriting without argument [] for rew_ord.*)
   236 (*WN.11.6.03: shouldnt asm<>[] lead to false ????*)
   237 fun eval_true thy terms rls = (snd o (eval__true thy 1 terms [])) rls;
   238 
   239 
   240 (*.rewriting without internal argument [] for rew_ord.*)
   241 (* val (thy, rew_ord, erls, bool, thm, term) =
   242        (thy, (assoc_rew_ord ro), rls', false, (assoc_thm' thy thm'), f);
   243    val (thy, rew_ord, erls, bool, thm, term) =
   244        (thy, rew_ord, erls, false, thm, t'');
   245    *)
   246 fun rewrite_ thy rew_ord erls bool thm term = 
   247     rewrite__ thy 1 [] rew_ord erls bool thm term;
   248 fun rewrite_set_ thy bool rls term =
   249 (* val (thy, bool, rls, term) = (thy, false, srls, t);
   250    *)
   251     rewrite__set_ thy 1 bool [] rls term;
   252 
   253 
   254 fun subs'2subst thy (s:subs') = 
   255     (((map (apfst (term_of o the o (parse thy)))) 
   256      o (map (apsnd (term_of o the o (parse thy))))) s):subst;
   257 
   258 (*.variants of rewrite.*)
   259 (*FIXME 12.8.02: put_asm = true <==> rewrite_inst,
   260   thus the argument put_asm  IS NOT NECESSARY -- FIXME*)
   261 (* val (rew_ord,rls,put_asm,thm,ct)=
   262        (e_rew_ord,poly_erls,false,num_str d1_isolate_add2,t);
   263    *)
   264 fun rewrite_inst_ (thy:theory) rew_ord (rls:rls) (put_asm:bool) 
   265 		  (subst:(term * term) list) (thm:thm) (ct:term) =
   266     rewrite__ thy 1 subst rew_ord rls put_asm thm ct;
   267 
   268 fun rewrite_set_inst_ (thy:theory) 
   269   (put_asm:bool) (subst:(term * term) list) (rls:rls) (ct:term) =
   270   (*let 
   271     val subst = subs'2subst thy subs';
   272     val subrls = instantiate_rls subs' rls
   273   in*) rewrite__set_ thy 1 put_asm subst (*sub*)rls ct
   274   (*end*);
   275 
   276 (* given a list of equalities (lhs = rhs) and a term, 
   277    replace all occurrences of lhs in the term with rhs;
   278    thus the order or equalities matters: put variables in lhs first. *)
   279 fun rewrite_terms_ thy ord erls equs t =
   280     let (*val _=tracing("### rewrite_terms_ equs= '"^terms2str equs^"' ..."^
   281 		      term_detail2str (hd equs)^
   282 		      "### rewrite_terms_ t= '"^term2str t^"' ..."^
   283 		      term_detail2str t);*)
   284 	fun rew_ (t', asm') [] _ = (t', asm')
   285 	  (* 1st val (t', asm', rules as r::rs, t) = (e_term, [], equs, t);
   286 	     2nd val (t', asm', rules as r::rs, t) = (t'', [], rules, t'');
   287 	     rew_ (t', asm') (r::rs) t;
   288 	     *)
   289 	  | rew_ (t', asm') (rules as r::rs) t =
   290 	    let val _ = tracing("rew_ "^term2str t);
   291 		val (t'', asm'', lrd, rew) = 
   292 		    rew_sub thy 1 [] ord erls false [] (Trueprop $ r) t
   293 	    in if rew 
   294 	       then (tracing ("true  rew_ " ^ term2str t'');
   295 		   rew_ (t'', asm' @ asm'') rules t'')
   296 	       else (tracing ("false rew_ " ^ term2str t'');
   297 		   rew_ (t', asm') rs t')
   298 	    end
   299 	val (t'', asm'') = rew_ (e_term, []) equs t
   300     in if t'' = e_term 
   301        then NONE else SOME (t'', asm'')
   302     end;
   303 
   304 
   305 (*. search ct for adjacent numerals and calculate them by operator isa_fn .*)
   306 fun calculate_ thy isa_fn ct =
   307   let val ct = uminus_to_string ct
   308     in case get_calculation_ thy isa_fn ct of
   309 	   NONE => NONE
   310 	 | SOME (thmID, thm) => 
   311 	   (let val SOME (rew,_) = rewrite_ thy dummy_ord e_rls false thm ct
   312     in SOME (rew,(thmID, thm)) end)
   313 	   handle _ => error ("calculate_: "^thmID^" does not rewrite")
   314   end;
   315 (*
   316 > val thy = InsSort.thy;
   317 > val op_ = "le";      (* < *)
   318 > val ct = (the o (parse thy)) 
   319    "foldr ins [#2] (if #1 < #3 then #1 # ins [] #3 else [#3, #1])";
   320 > calculate_ thy op_ ct;
   321   SOME
   322     ("foldr ins [#2] (if True then #1 # ins [] #3 else [#3, #1])",
   323      "(#1 < #3) = True") : (cterm * thm) option  *)
   324 
   325 fun get_rls_scr rs' = ((#scr o rep_rls o #2 o the o assoc') (!ruleset',rs'))
   326   handle _ => error ("get_rls_scr: no script for "^rs');
   327 
   328 
   329 (*make_thm added to Pure/thm.ML*)
   330 fun mk_thm thy str = 
   331     let val t = (term_of o the o (parse thy)) str
   332 	val t' = case t of
   333 		     Const ("==>",_) $ _ $ _ => t
   334 		   | _ => Trueprop $ t
   335     in make_thm (cterm_of thy t') end;
   336 (*
   337   val str = "?r ^^^ 2 = ?r * ?r";
   338   val thm = realpow_twoI;
   339 
   340   val t1 = (#prop o rep_thm) (num_str thm);
   341   val t2 = Trueprop $ ((term_of o the o (parse thy)) str);
   342   t1 = t2;
   343 val it = true : bool      ... !!!
   344   val th1 = (num_str thm);
   345   val th2 = ((*num_str*) (mk_thm thy str)) handle e => print_exn e;
   346   th1 = th2;
   347 ML> val it = false : bool ... HIDDEN DIFFERENCES IRRELEVANT FOR ISAC ?!
   348 
   349 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   350   val str = "k ~= 0 ==> m * k / (n * k) = m / n";
   351   val thm = real_mult_div_cancel2;
   352 
   353   val t1 = (#prop o rep_thm) (num_str thm);
   354   val t2 = ((term_of o the o (parse thy)) str);
   355   t1 = t2;
   356 val it = false : bool     ... Var .. Free
   357   val th1 = (num_str thm);
   358   val th2 = ((*num_str*) (mk_thm thy str)) handle e => print_exn e;
   359   th1 = th2;
   360 ML> val it = false : bool ... PLUS HIDDEN DIFFERENCES IRRELEVANT FOR ISAC ?!
   361 *)
   362 
   363 
   364 (*prints subgoal etc. 
   365 ((goal thy);(topthm()) o ) str;                      *)
   366 (*assume rejects scheme variables 
   367   assume ((cterm_of thy) (Trueprop $ 
   368 		(term_of o the o (parse thy)) str)); *)
   369 
   370 
   371 (* outcommented 18.11.xx, xx < 02 -------
   372 fun rul2rul' (Thm (thmid, thm)) = Thm'(thmid, string_of_thmI thm)
   373   | rul2rul' (Calc op_)         = Calc' op_;
   374 fun rul'2rul thy (Thm'(thmid, ct')) = 
   375        Thm (thmid, mk_thm thy ct')
   376   | rul'2rul thy' (Calc' op_)        = Calc op_;
   377 
   378 
   379 fun rls2rls' (Rls{preconds=preconds,rew_ord=rew_ord,rules=rules}:rls) =
   380   Rls'{preconds'= map string_of_cterm preconds,
   381        rew_ord' = fst rew_ord,
   382        rules'   = map rul2rul' rules}:rlsdat';
   383 
   384 fun rls'2rls thy' (Rls'{preconds'=preconds,rew_ord'=rew_ord,
   385 		   rules'=rules}:rlsdat') =
   386   let val thy = assoc_thy thy';
   387   in Rls{preconds = map (the o (parse thy)) preconds,
   388 	 rew_ord  = (rew_ord, the (assoc'(rew_ord',rew_ord))),
   389 	 rules    = map (rul'2rul thy) rules}:rls end;
   390 ------- *)
   391 
   392 (*.get the theorem associated with the xstring-identifier;
   393    if the identifier starts with "sym_" then swap lhs = rhs around =
   394    (ATTENTION: "RS sym" attaches a [.] -- remove it with string_of_thmI);
   395    identifiers starting with "#" come from Calc and
   396    get a hand-made theorem (containing numerals only).*)
   397 fun assoc_thm' (thy:theory) ((thmid, ct'):thm') =
   398     (case Symbol.explode thmid of
   399 	"s"::"y"::"m"::"_"::id => 
   400 	if hd id = "#" 
   401 	then mk_thm thy ct'
   402 	else ((num_str o (PureThy.get_thm thy)) (implode id)) RS sym
   403       | id => 
   404 	if hd id = "#" 
   405 	then mk_thm thy ct'
   406 	else (num_str o (PureThy.get_thm thy)) thmid
   407 	     ) handle _ => 
   408 		      error ("assoc_thm': '"^thmid^"' not in '"^
   409 				   (theory2domID thy)^"' (and parents)");
   410 (*> assoc_thm' (Thy_Info.get_theory "Isac") ("sym_#mult_2_3","6 = 2 * 3");
   411 val it = "6 = 2 * 3" : thm          
   412 
   413 > assoc_thm' (Thy_Info.get_theory "Isac") ("add_0_left","");
   414 val it = "0 + ?z = ?z" : thm
   415 
   416 > assoc_thm' (Thy_Info.get_theory "Isac") ("sym_real_add_zero_left","");
   417 val it = "?t = 0 + ?t"  [.] : thm
   418 
   419 > assoc_thm' HOL.thy ("sym_real_add_zero_left","");
   420 *** Unknown theorem(s) "add_0_left"
   421 *** assoc_thm': 'sym_real_add_zero_left' not in 'HOL.thy' (and parents)
   422  uncaught exception ERROR*)
   423 
   424 
   425 fun parse' (thy:theory') (ct:cterm') =
   426     case parse (assoc_thy thy) ct of
   427 	NONE => NONE
   428       | SOME ct => SOME ((term2str (term_of ct)):cterm');
   429 
   430 
   431 (*FIXME 12.8.02: put_asm = true <==> rewrite_inst, rewrite_set_inst
   432   thus the argument put_asm  IS NOT NECESSARY -- FIXME        ~~~~~*)
   433 fun rewrite (thy':theory') (rew_ord:rew_ord') (rls:rls') 
   434     (put_asm:bool) (thm:thm') (ct:cterm') =
   435 (* val (rew_ord, rls, thm, ct) = (rew_ord', id_rls rls', thm', f);
   436    *)
   437     let val thy = assoc_thy thy';
   438     in
   439     case rewrite_ thy
   440 	((the o assoc')(!rew_ord',rew_ord))((#2 o the o assoc')(!ruleset',rls))
   441 	put_asm ((assoc_thm' thy) thm)
   442 	((term_of o the o (parse thy)) ct) of
   443 	NONE => NONE
   444       | SOME (t, ts) => SOME (term2str t, terms2str ts)
   445     end;
   446 
   447 (*
   448 val thy     = "RatArith";
   449 val rew_ord = "dummy_ord"; 
   450 > val rls     = "eval_rls";
   451 val put_asm = true; 
   452 val thm     = ("square_equation_left","");
   453 val ct      = "sqrt(#9+#4*x)=sqrt x + sqrt(#-3+x)";
   454 
   455 val Zthy     = assoc_thy thy;
   456 val Zrew_ord = ((the o assoc')(!rew_ord',rew_ord)); 
   457 val Zrls     = ((the o assoc')(!ruleset',rls));
   458 val Zput_asm = put_asm; 
   459 val Zthm     = ((the o (assoc'_thm' thy)) thm);
   460 val Zct      = ((the o (parse (assoc_thy thy))) ct);
   461 
   462 rewrite_ Zthy Zrew_ord Zrls Zput_asm Zthm Zct;
   463 
   464  use"Isa99/interface_ME_ISA.sml";
   465 *)
   466 
   467 (*FIXME 12.8.02: put_asm = true <==> rewrite_inst, rewrite_set_inst
   468   thus the argument put_asm  IS NOT NECESSARY -- FIXME        ~~~~~*)
   469 fun rewrite_set (thy':theory') (put_asm:bool)
   470     (rls:rls') (ct:cterm') =
   471     let val thy = (assoc_thy thy');
   472     in
   473     case rewrite_set_ thy put_asm ((#2 o the o assoc')(!ruleset',rls))
   474     ((term_of o the o (parse thy)) ct) of
   475 	NONE => NONE
   476       | SOME (t, ts) => SOME (term2str t, terms2str ts)
   477     end;
   478 
   479 (*evaluate list-expressions
   480   should work on term, and stand in Isa99/rewrite-parse.sml, 
   481   but there list_rls <- eval_binop is not yet defined*)
   482 (*fun eval_listexpr' ct = 
   483     let val rew = rewrite_set "ListC" false "list_rls" ct;
   484     in case rew of 
   485 	   SOME (res,_) => res
   486 	 | NONE => ct end;-----------------30.9.02---*)
   487 fun eval_listexpr_ thy srls t =
   488 (* val (thy,            srls, t) = 
   489        ((assoc_thy th), sr,  (subst_atomic (upd_env_opt E (a,v)) t));
   490    *) 
   491     let val rew = rewrite_set_ thy false srls t;
   492     in case rew of 
   493 	   SOME (res,_) => res
   494 	 | NONE => t end;
   495 
   496 
   497 fun get_calculation' (thy:theory') op_ (ct:cterm') =
   498    case get_calculation_ (assoc_thy thy) op_
   499     ((uminus_to_string o term_of o the o 
   500       (parse (assoc_thy thy))) ct) of
   501 	NONE => NONE
   502       | SOME (thmid, thm) => 
   503 	    SOME ((thmid, string_of_thmI thm):thm');
   504 
   505 fun calculate (thy':theory') op_ (ct:cterm') =
   506     let val thy = (assoc_thy thy');
   507     in
   508 	case calculate_ thy op_
   509 			((term_of o the o (parse thy)) ct) of
   510 	    NONE => NONE
   511 	  | SOME (ct,(thmID,thm)) => 
   512 	    SOME (term2str ct, 
   513 		  (thmID, string_of_thmI thm):thm')
   514     end;
   515 (*
   516 fun instantiate'' thy' subs ((thmid,ct'):thm') = 
   517   let val thmid_ = implode ("#"::(Symbol.explode thmid))  (*see type thm'*)
   518   in (thmid_, (string_of_thmI o (read_instantiate subs)) 
   519       ((the o (assoc_thm' thy')) (thmid_,ct'))):thm' end;
   520 
   521 fun instantiate_rls' thy' subs (rls:rls') = 
   522     rls2rls' (instantiate_rls subs ((the o (assoc_rls thy')) rls)):rlsdat';
   523 
   524 ... problem with these functions: 
   525 > val thm = mk_thm thy "(bdv + a = b) = (bdv = b - a)";
   526 val thm = "(bdv + a = b) = (bdv = b - a)" : thm
   527 > show_types:=true; thm;    
   528 val it = "((bdv::'a) + (a::'a) = (b::'a)) = (bdv = b - a)" : thm
   529 ... and this doesn't match because of too general typing (?!)
   530     and read_insitantiate doesn't instantiate the types (?!)
   531 === solutions:
   532 (1) hard-coded type-instantiation ("'a", "RatArith.rat")
   533 (2) instantiate', instantiate ... no help by isabelle-users@ !!!
   534 === conclusion:
   535     rewrite_inst, rewrite_set_inst circumvent the problem,
   536     according functions out-commented with 'instantiate''
   537 *)
   538 
   539 (* instantiate''
   540 fun instantiate'' thy' subs ((thmid,ct'):thm') = 
   541   let 
   542     val thmid_ = implode ("#"::(Symbol.explode thmid));  (*see type thm'*)
   543     val thy = assoc_thy thy';
   544     val typs = map (#T o rep_cterm o the o (parse thy)) 
   545       ((snd o split_list) subs);
   546     val ctyps = map 
   547       ((ctyp_of (sign_of thy)) o #T o rep_cterm o the o (parse thy)) 
   548       ((snd o split_list) subs);
   549 
   550 > val thy' = "RatArith";
   551 > val subs = [("bdv","x::rat"),("zzz","z::nat")];
   552 > (the o (parse (assoc_thy thy'))) "x::rat";
   553 > (#T o rep_cterm o the o (parse (assoc_thy thy')));
   554 
   555 > val ctyp = ((ctyp_of (sign_of thy)) o #T o rep_cterm o the o 
   556 	      (parse (assoc_thy thy'))) "x::rat";
   557 > val bdv = (the o (parse thy)) "bdv";
   558 > val x   = (the o (parse thy)) "x";
   559 > (instantiate ([(("'a",0),ctyp)],[(bdv,x)]) isolate_bdv_add)
   560       handle e => print_exn e;
   561 uncaught exception THM
   562   raised at: thm.ML:1085.18-1085.69
   563              thm.ML:1092.34
   564              goals.ML:536.61
   565 
   566 > val bdv = (the o (parse thy)) "bdv::nat";
   567 > val x   = (the o (parse thy)) "x::nat";
   568 > (instantiate ([(("'a",0),ctyp)],[(bdv,x)]) isolate_bdv_add)
   569       handle e => print_exn e;
   570 uncaught exception THM
   571   raised at: thm.ML:1085.18-1085.69
   572              thm.ML:1092.34
   573              goals.ML:536.61
   574 
   575 > (instantiate' [SOME ctyp] [] isolate_bdv_add)
   576       handle e => print_exn e;      
   577 uncaught exception TYPE
   578   raised at: drule.ML:613.13-615.44
   579              goals.ML:536.61
   580 
   581 > val repct = (rep_cterm o the o (parse ((the o assoc')(!theory',thy')))) "x::rat";
   582 *)
   583 
   584 (*FIXME 12.8.02: put_asm = true <==> rewrite_inst, rewrite_set_inst
   585   thus the argument put_asm  IS NOT NECESSARY -- FIXME        ~~~~~*)
   586 fun rewrite_inst (thy':theory') (rew_ord:rew_ord') (rls:rls') 
   587   (put_asm:bool) subs (thm:thm') (ct:cterm') =
   588   let
   589     val thy = assoc_thy thy';
   590     val thm = assoc_thm' thy thm; (*28.10.02*)
   591     (*val subthm = read_instantiate subs ((assoc_thm' thy) thm)*)
   592   in
   593     case rewrite_ thy
   594       ((the o assoc')(!rew_ord',rew_ord)) ((#2 o the o assoc')(!ruleset',rls))
   595       put_asm (*sub*)thm ((term_of o the o (parse thy)) ct) of
   596       NONE => NONE
   597     | SOME (ctm, ctms) => 
   598       SOME ((term2str ctm):cterm', (map term2str ctms):cterm' list)
   599   end;
   600 
   601 (*FIXME 12.8.02: put_asm = true <==> rewrite_inst, rewrite_set_inst
   602   thus the argument put_asm  IS NOT NECESSARY -- FIXME        ~~~~~*)
   603 fun rewrite_set_inst (thy':theory') (put_asm:bool)
   604   subs' (rls:rls') (ct:cterm') =
   605   let
   606     val thy = assoc_thy thy';
   607     val rls = assoc_rls rls
   608     val subst = subs'2subst thy subs'
   609     (*val subrls = instantiate_rls subs ((the o assoc')(!ruleset',rls))*)
   610   in case rewrite_set_inst_ thy put_asm subst (*sub*)rls
   611 			    ((term_of o the o (parse thy)) ct) of
   612 	 NONE => NONE
   613        | SOME (t, ts) => SOME (term2str t, terms2str ts)
   614   end;
   615 
   616 
   617 (*vor check_elementwise: SqRoot_eval_rls .. wie *_simplify ?! TODO *)
   618 fun eval_true' (thy':theory') (rls':rls') (Const ("True",_)) = true
   619 
   620   | eval_true' (thy':theory') (rls':rls') (t:term) =
   621 (* val thy'="Isac"; val rls'="eval_rls"; val t=hd pres';
   622    *)
   623     let val ct' = term2str t;
   624     in case rewrite_set thy' false rls' ct' of
   625 	   SOME ("True",_) => true
   626 	 | _ => false 
   627     end;
   628 fun eval_true_ _ _ (Const ("True",_)) = true
   629   | eval_true_ (thy':theory') rls t =
   630     case rewrite_set_ (assoc_thy thy') false rls t of
   631 	   SOME (Const ("True",_),_) => true
   632 	 | _ => false;
   633 
   634 (*
   635 val test_rls = 
   636   Rls{preconds = [], rew_ord = ("sqrt_right",sqrt_right), 
   637       rules = [Calc ("matches",eval_matches "")
   638 	       ],
   639       scr = Script ((term_of o the o (parse thy)) 
   640       "empty_script")
   641       }:rls;      
   642 
   643 
   644 
   645   rewrite_set_ (Thy_Info.get_theory "Isac") eval_rls false test_rls 
   646         ((the o (parse thy)) "matches (?a = ?b) (x = #0)");
   647   val xxx = (term_of o the o (parse thy)) 
   648 	       "matches (?a = ?b) (x = #0)";
   649   eval_matches """" xxx thy;
   650 SOME ("matches (?a = ?b) (x + #1 + #-1 * #2 = #0) = True",
   651      Const ("Trueprop","bool => prop") $ (Const # $ (# $ #) $ Const (#,#)))
   652 
   653 
   654 
   655   rewrite_set_ (Thy_Info.get_theory "Isac") eval_rls false eval_rls 
   656         ((the o (parse thy)) "contains_root (sqrt #0)");
   657 val it = SOME ("True",[]) : (cterm * cterm list) option
   658     
   659 *)
   660 
   661 
   662 (*----------WN:16.5.03 stuff below considered illdesigned, thus coded from scratch in appl.sml fun check_elementwise
   663 datatype det = TRUE  | FALSE | INDET;(*FIXXME.WN:16.5.03
   664 				     introduced with quick-and-dirty code*)
   665 fun determine dts =
   666     let val false_indet = 
   667 	    filter_out ((curry op= TRUE) o (#1:det * term -> det)) dts
   668         val ts = map (#2: det * term -> term) dts
   669     in if nil = false_indet then (TRUE, ts)
   670        else if nil = filter ((curry op= FALSE) o (#1:det * term -> det))
   671 			    false_indet
   672        then (INDET, ts)
   673        else (FALSE, ts) end;
   674 (* val dts = [(INDET,e_term), (FALSE,HOLogic.false_const), 
   675 	      (INDET,e_term), (TRUE,HOLogic.true_const)];
   676   determine dts;
   677 val it =
   678   (FALSE,
   679    [Const ("empty","'a"),Const ("False","bool"),Const ("empty","'a"),
   680     Const ("True","bool")]) : det * term list*)
   681 
   682 fun eval__indet_ thy cs rls = (*FIXXME.WN:16.5.03 pull into eval__true_, update check (check_elementwise), and regard eval_true_ + eval_true*)
   683 if cs = [HOLogic.true_const] orelse cs = [] then (TRUE, [])
   684     else if cs = [HOLogic.false_const] then (FALSE, cs)
   685     else
   686 	let fun eval t = 
   687 		let val taopt = rewrite__set_ thy 1 false [] rls t
   688 		in case taopt of
   689 		       SOME (t,_) =>
   690 		       if t = HOLogic.true_const then (TRUE, t)
   691 		       else if t = HOLogic.false_const then (FALSE, t)
   692 		       else (INDET, t)
   693 		     | NONE => (INDET, t) end
   694 	in (determine o (map eval)) cs end;
   695 WN.16.5.0-------------------------------------------------------------*)