src/Tools/isac/Interpret/rewtools.sml
author Walther Neuper <neuper@ist.tugraz.at>
Thu, 19 Jun 2014 07:51:40 +0200
changeset 55451 1395832f4f53
parent 55437 9c19751b2ad1
child 55456 467ccd9ef7d6
permissions -rw-r--r--
ad 967c8a1eb6b1 (7) thehier: remove code superfluous by last changeset
neuper@37906
     1
(* tools for rewriting, reverse rewriting, context to thy concerning rewriting
neuper@37906
     2
   authors: Walther Neuper 2002, 2006
neuper@37906
     3
  (c) due to copyright terms
neuper@37906
     4
neuper@37906
     5
use"ME/rewtools.sml";
neuper@37906
     6
use"rewtools.sml";
neuper@37906
     7
*)
neuper@37906
     8
neuper@37906
     9
neuper@37906
    10
neuper@37906
    11
(***.reverse rewriting.***)
neuper@37906
    12
neuper@37906
    13
(*.derivation for insertin one level of nodes into the calctree.*)
neuper@37906
    14
type deriv  = (term * rule * (term *term list)) list;
neuper@37906
    15
neuper@37906
    16
fun trta2str (t,r,(t',a)) = "\n("^(term2str t)^", "^(rule2str' r)^", ("^
neuper@37906
    17
			    (term2str t')^", "^(terms2str a)^"))";
neuper@37906
    18
fun trtas2str trtas = (strs2str o (map trta2str)) trtas;
neuper@37906
    19
val deriv2str = trtas2str;
neuper@37906
    20
fun rta2str (r,(t,a)) = "\n("^(rule2str' r)^", ("^
neuper@37906
    21
			    (term2str t)^", "^(terms2str a)^"))";
neuper@37906
    22
fun rtas2str rtas = (strs2str o (map rta2str)) rtas;
neuper@37906
    23
val deri2str = rtas2str;
neuper@37906
    24
neuper@37906
    25
neuper@42399
    26
(* A1==>...==>An==>(Lhs = Rhs) goes to A1==>...==>An==>(Rhs = Lhs) 
neuper@42399
    27
  WN120320: reconsider the desing including the java front-end with html representation;
neuper@42399
    28
  see tests 
neuper@42399
    29
  --- OLD compute rlsthmsNOTisac by eq_thmID ---
neuper@42399
    30
  --- compute val rlsthmsNOTisac ---
neuper@42399
    31
*)
neuper@37906
    32
fun sym_thm thm =
neuper@37936
    33
    let 
neuper@55275
    34
        val (deriv, {thy = thy, tags = tags, maxidx = maxidx, 
neuper@37936
    35
                     shyps = shyps, hyps = hyps, tpairs = tpairs, 
neuper@37936
    36
                     prop = prop}) = 
neuper@37936
    37
	    rep_thm_G thm;
neuper@37936
    38
        val (lhs,rhs) = (dest_equals' o strip_trueprop 
neuper@37936
    39
		         o Logic.strip_imp_concl) prop;
neuper@37936
    40
        val prop' = case strip_imp_prems' prop of
neuper@37936
    41
		        NONE => Trueprop $ (mk_equality (rhs, lhs))
neuper@37936
    42
		      | SOME cs => 
neuper@37936
    43
		        ins_concl cs (Trueprop $ (mk_equality (rhs, lhs)));
neuper@55275
    44
    in assbl_thm deriv thy tags maxidx shyps hyps tpairs prop' end;
neuper@37906
    45
(*
neuper@37906
    46
  (sym RS real_mult_div_cancel1) handle e => print_exn e;
neuper@37906
    47
Exception THM 1 raised:
neuper@37906
    48
RSN: no unifiers
neuper@37906
    49
"?s = ?t ==> ?t = ?s"
neuper@37906
    50
"?k ~= 0 ==> ?k * ?m / (?k * ?n) = ?m / ?n"
neuper@37906
    51
neuper@37906
    52
  val thm = real_mult_div_cancel1;
neuper@37906
    53
  val prop = (#prop o rep_thm) thm;
neuper@37906
    54
  atomt prop;
neuper@37906
    55
  val ppp = Logic.strip_imp_concl prop;
neuper@37906
    56
  atomt ppp;
neuper@37906
    57
  ((#prop o rep_thm o sym_thm o sym_thm) thm) = (#prop o rep_thm) thm;
neuper@37906
    58
val it = true : bool
neuper@37906
    59
  ((sym_thm o sym_thm) thm) = thm;
neuper@37906
    60
val it = true : bool
neuper@37906
    61
neuper@37906
    62
  val thm = real_le_anti_sym;
neuper@37906
    63
  ((sym_thm o sym_thm) thm) = thm;
neuper@37906
    64
val it = true : bool
neuper@37906
    65
neuper@37906
    66
  val thm = real_minus_zero;
neuper@37906
    67
  ((sym_thm o sym_thm) thm) = thm;
neuper@37906
    68
val it = true : bool
neuper@37906
    69
*)
neuper@38053
    70
(*WN100910 weaker than fun sym_thm for Theory.axioms_of in isa02*)
neuper@38002
    71
fun sym_trm trm =
neuper@38002
    72
    let val (lhs,rhs) = (dest_equals' o strip_trueprop
neuper@38002
    73
		         o Logic.strip_imp_concl) trm;
neuper@38002
    74
        val trm' = case strip_imp_prems' trm of
neuper@38002
    75
		        NONE => mk_equality (rhs, lhs)
neuper@38002
    76
		      | SOME cs => 
neuper@38002
    77
		        ins_concl cs (mk_equality (rhs, lhs));
neuper@38002
    78
    in trm' end;
neuper@38002
    79
(*ML {*
neuper@38002
    80
val trm = @{term "k ~= 0 ==> k * m / (k * n) = m / n"};
neuper@38053
    81
(tracing o 
neuper@52070
    82
 (term_to_string')) (sym_trm trm);
neuper@38002
    83
"~ k = (0::'a) ==> m / n = k * m / (k * n)"
neuper@38002
    84
*}*)
neuper@37906
    85
neuper@37906
    86
neuper@37906
    87
neuper@37926
    88
(*.derive normalform of a rls, or derive until SOME goal,
neuper@37906
    89
   and record rules applied and rewrites.
neuper@37906
    90
val it = fn
neuper@37906
    91
  : theory
neuper@37906
    92
    -> rls
neuper@37906
    93
    -> rule list
neuper@37906
    94
    -> rew_ord       : the order of this rls, which 1 theorem of is used 
neuper@37906
    95
                       for rewriting 1 single step (?14.4.03)
neuper@37906
    96
    -> term option   : 040214 ??? nonsense ??? 
neuper@37906
    97
    -> term 
neuper@37906
    98
    -> (term *       : to this term ...
neuper@37906
    99
        rule * 	     : ... this rule is applied yielding ...
neuper@37906
   100
        (term *      : ... this term ...
neuper@37906
   101
         term list)) : ... under these assumptions.
neuper@37906
   102
       list          :
neuper@37906
   103
returns empty list for a normal form
neuper@37906
   104
FIXME.WN040214: treats rules as in Rls, _not_ as in Seq
neuper@37906
   105
neuper@37906
   106
WN060825 too complicated for the intended use by cancel_, common_nominator_
neuper@37906
   107
and unreflectedly adapted to extion of rules by Rls_: returns Rls_("sym_simpl..
neuper@37906
   108
 -- replaced below*)
neuper@37906
   109
(* val (thy, erls, rs, ro, goal, tt) = (thy, erls, rs, ro, goal, t);
neuper@37926
   110
   val (thy, erls, rs, ro, goal, tt) = (thy, Atools_erls, rules, ro, NONE, tt);
neuper@37906
   111
   *)
neuper@37906
   112
fun make_deriv thy erls (rs:rule list) ro(*rew_ord*) goal tt = 
neuper@37906
   113
    let datatype switch = Appl | Noap
neuper@37906
   114
	fun rew_once lim rts t Noap [] = 
neuper@37906
   115
	    (case goal of 
neuper@37926
   116
		 NONE => rts
neuper@37926
   117
	       | SOME g => 
neuper@38031
   118
		 error ("make_deriv: no derivation for "^(term2str t)))
neuper@37906
   119
	  | rew_once lim rts t Appl [] = 
neuper@37906
   120
	    (*(case rs of Rls _ =>*) rew_once lim rts t Noap rs
neuper@37906
   121
	  (*| Seq _ => rts) FIXXXXXME 14.3.03*)
neuper@37906
   122
	  | rew_once lim rts t apno rs' =
neuper@37906
   123
	    (case goal of 
neuper@37926
   124
		 NONE => rew_or_calc lim rts t apno rs'
neuper@37926
   125
	       | SOME g =>
neuper@37906
   126
		 if g = t then rts
neuper@37906
   127
		 else rew_or_calc lim rts t apno rs')
neuper@37906
   128
	and rew_or_calc lim rts t apno (rrs' as (r::rs')) =
neuper@37906
   129
	    if lim < 0 
neuper@38015
   130
	    then (tracing ("make_deriv exceeds " ^ int2str (!lim_deriv) ^
neuper@38015
   131
			   "with deriv =\n"); tracing (deriv2str rts); rts)
neuper@37906
   132
	    else
neuper@37906
   133
	    case r of
neuper@37906
   134
		Thm (thmid, tm) =>
neuper@37906
   135
		(if not (!trace_rewrite) then () else
neuper@38015
   136
		 tracing ("### trying thm '" ^ thmid ^ "'");
neuper@37906
   137
		 case rewrite_ thy ro erls true tm t of
neuper@37926
   138
		     NONE => rew_once lim rts t apno rs'
neuper@37926
   139
		   | SOME (t',a') =>
neuper@37906
   140
		     (if ! trace_rewrite 
neuper@38015
   141
		      then tracing ("### rewrites to: "^(term2str t')) else();
neuper@37906
   142
		      rew_once (lim-1) (rts@[(t,r,(t',a'))]) t' Appl rrs'))
neuper@37906
   143
	      | Calc (c as (op_,_)) => 
neuper@37906
   144
		let val _ = if not (!trace_rewrite) then () else
neuper@38015
   145
			    tracing ("### trying calc. '" ^ op_ ^ "'")
neuper@37906
   146
		    val t = uminus_to_string t
neuper@37906
   147
		in case get_calculation_ thy c t of
neuper@37926
   148
		       NONE => rew_once lim rts t apno rs'
neuper@37926
   149
		     | SOME (thmid, tm) => 
neuper@37926
   150
		       (let val SOME (t',a') = rewrite_ thy ro erls true tm t
neuper@37906
   151
			    val _ = if not (!trace_rewrite) then () else
neuper@38015
   152
				    tracing("### calc. to: " ^ (term2str t'))
neuper@37906
   153
			    val r' = Thm (thmid, tm)
neuper@37906
   154
			in rew_once (lim-1) (rts@[(t,r',(t',a'))]) t' Appl rrs'
neuper@37906
   155
			end) 
neuper@38031
   156
		       handle _ => error "derive_norm, Calc: no rewrite"
neuper@37906
   157
		end
neuper@37906
   158
(* TODO.WN080222: see rewrite__set_
neuper@37906
   159
   @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
neuper@37906
   160
      | Cal1 (cc as (op_,_)) => 
neuper@37906
   161
	  (let val _= if !trace_rewrite andalso i < ! depth then
neuper@38015
   162
		      tracing((idt"#"(i+1))^" try cal1: "^op_^"'") else ();
neuper@37906
   163
	     val ct = uminus_to_string ct
neuper@37906
   164
	   in case get_calculation_ thy cc ct of
neuper@37926
   165
	     NONE => (ct, asm)
neuper@37926
   166
	   | SOME (thmid, thm') =>
neuper@37906
   167
	       let 
neuper@37906
   168
		 val pairopt = 
neuper@37906
   169
		   rewrite__ thy (i+1) bdv ((snd o #rew_ord o rep_rls) rls)
neuper@37906
   170
		   ((#erls o rep_rls) rls) put_asm thm' ct;
neuper@37926
   171
		 val _ = if pairopt <> NONE then () 
neuper@38031
   172
			 else error("rewrite_set_, rewrite_ \""^
neuper@37926
   173
			 (string_of_thmI thm')^"\" "^(term2str ct)^" = NONE")
neuper@37906
   174
		 val _ = if ! trace_rewrite andalso i < ! depth 
neuper@38015
   175
			   then tracing((idt"="(i+1))^" cal1. to: "^
neuper@37906
   176
					(term2str ((fst o the) pairopt)))
neuper@37906
   177
			 else()
neuper@37906
   178
	       in the pairopt end
neuper@37906
   179
	   end)
neuper@37906
   180
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@*)
neuper@37906
   181
	      | Rls_ rls => 
neuper@37906
   182
		(case rewrite_set_ thy true rls t of
neuper@37926
   183
		     NONE => rew_once lim rts t apno rs'
neuper@37926
   184
		   | SOME (t',a') =>
neuper@37906
   185
		     rew_once (lim-1) (rts @ [(t,r,(t',a'))]) t' Appl rrs');
neuper@37906
   186
(*WN060829    | Rls_ rls => 
neuper@37906
   187
		(case rewrite_set_ thy true rls t of
neuper@37926
   188
		     NONE => rew_once lim rts t apno rs'
neuper@37926
   189
		   | SOME (t',a') =>
neuper@37906
   190
		     if ro [] (t, t') then rew_once lim rts t apno rs'
neuper@37906
   191
		     else rew_once (lim-1) (rts@[(t,r,(t',a'))]) t' Appl rrs');
neuper@37906
   192
...lead to deriv = [] with make_polynomial.
neuper@37906
   193
THERE IS SOMETHING DIFFERENT beetween rewriting with the code above
neuper@37906
   194
and between rewriting with rewrite_set: with rules from make_polynomial and 
neuper@37906
   195
t = "(a^^^2 + -1*b^^^2) / (a^^^2 + -2*a*b + b^^^2)" the actual code
neuper@37979
   196
leads to cycling  Rls_ order_mult_rls_..Rls_ discard_parentheses1..Rls_ order..
neuper@37906
   197
*)
neuper@37906
   198
    in rew_once (!lim_deriv) [] tt Noap rs end;
neuper@37906
   199
neuper@37906
   200
neuper@37906
   201
(*.toggles the marker for 'fun sym_thm'.*)
neuper@37906
   202
fun sym_thmID (thmID : thmID) =
neuper@40836
   203
    case Symbol.explode thmID of
neuper@37906
   204
	"s"::"y"::"m"::"_"::id => implode id : thmID
neuper@37906
   205
      | id => "sym_"^thmID;
neuper@37906
   206
(* 
neuper@37906
   207
> val thmID = "sym_real_mult_2";
neuper@37906
   208
> sym_thmID thmID;
neuper@37906
   209
val it = "real_mult_2" : string
neuper@37906
   210
> val thmID = "real_num_collect";
neuper@37906
   211
> sym_thmID thmID;
neuper@37906
   212
val it = "sym_real_num_collect" : string*)
neuper@37906
   213
fun sym_drop (thmID : thmID) =
neuper@40836
   214
    case Symbol.explode thmID of
neuper@37906
   215
	"s"::"y"::"m"::"_"::id => implode id : thmID
neuper@37906
   216
      | id => thmID;
neuper@37906
   217
fun is_sym (thmID : thmID) =
neuper@40836
   218
    case Symbol.explode thmID of
neuper@37906
   219
	"s"::"y"::"m"::"_"::id => true
neuper@37906
   220
      | id => false;
neuper@37906
   221
neuper@37906
   222
neuper@37906
   223
(*FIXXXXME.040219: detail has to handle Rls id="sym_..." 
neuper@37906
   224
  by applying make_deriv, rev_deriv'; see concat_deriv*)
neuper@37906
   225
fun sym_rls Erls = Erls
neuper@42451
   226
  | sym_rls (Rls {id, scr, calc, errpatts, erls, srls, rules, rew_ord, preconds}) =
neuper@42451
   227
    Rls {id="sym_"^id, scr=scr, calc=calc, errpatts=errpatts, erls=erls, srls=srls, 
neuper@37906
   228
	 rules=rules, rew_ord=rew_ord, preconds=preconds}
neuper@42451
   229
  | sym_rls (Seq {id, scr, calc, errpatts, erls, srls, rules, rew_ord, preconds}) =
neuper@42451
   230
    Seq {id="sym_"^id, scr=scr, calc=calc, errpatts=errpatts,  erls=erls, srls=srls, 
neuper@37906
   231
	 rules=rules, rew_ord=rew_ord, preconds=preconds}
neuper@42451
   232
  | sym_rls (Rrls {id, scr, calc, errpatts, erls, prepat, rew_ord}) = 
neuper@42451
   233
    Rrls {id="sym_"^id, scr=scr, calc=calc,  errpatts=errpatts, erls=erls, prepat=prepat, 
neuper@37906
   234
	  rew_ord=rew_ord};
neuper@37906
   235
neuper@37906
   236
fun sym_Thm (Thm (thmID, thm)) = Thm (sym_thmID thmID, sym_thm thm)
neuper@37906
   237
  | sym_Thm (Rls_ rls) = Rls_ (*WN060825?!?*) (sym_rls rls)
neuper@38031
   238
  | sym_Thm r = error ("sym_Thm: not for "^(rule2str r));
neuper@37906
   239
(*
neuper@37969
   240
  val th =  Thm ("real_one_collect",num_str @{thm real_one_collect});
neuper@37906
   241
  sym_Thm th;
neuper@37906
   242
val th =
neuper@37906
   243
  Thm ("real_one_collect","?m is_const ==> ?n + ?m * ?n = (1 + ?m) * ?n")
neuper@37906
   244
  : rule
neuper@37906
   245
ML> val it =
neuper@37906
   246
  Thm ("sym_real_one_collect","?m is_const ==> (1 + ?m) * ?n = ?n + ?m * ?n")*)
neuper@37906
   247
neuper@37906
   248
neuper@37906
   249
(*version for reverse rewrite used before 040214*)
neuper@37906
   250
fun rev_deriv (t, r, (t', a)) = (sym_Thm r, (t, a));
neuper@37926
   251
(* val (thy, erls, rs, ro, goal, t) = (thy, eval_rls, rules, ro, NONE, t');
neuper@37906
   252
   *)
neuper@37906
   253
fun reverse_deriv thy erls (rs:rule list) ro(*rew_ord*) goal t =
neuper@37906
   254
    (rev o (map rev_deriv)) (make_deriv thy erls (rs:rule list) ro goal t);
neuper@37906
   255
(*
neuper@37906
   256
  val rev_rew = reverse_deriv thy e_rls ; 
neuper@38015
   257
  tracing(rtas2str rev_rew);
neuper@37906
   258
*)
neuper@37906
   259
neuper@37906
   260
fun eq_Thm (Thm (id1,_), Thm (id2,_)) = id1 = id2
neuper@37906
   261
  | eq_Thm (Thm (id1,_), _) = false
neuper@37906
   262
  | eq_Thm (Rls_ r1, Rls_ r2) = id_rls r1 = id_rls r2
neuper@37906
   263
  | eq_Thm (Rls_ r1, _) = false
neuper@38031
   264
  | eq_Thm (r1, r2) = error ("eq_Thm: called with '"^
neuper@37906
   265
				(rule2str r1)^"' '"^(rule2str r2)^"'");
neuper@37906
   266
fun distinct_Thm r = gen_distinct eq_Thm r;
neuper@37906
   267
neuper@37935
   268
fun eq_Thms thmIDs thm = (member op = thmIDs (id_of_thm thm))
neuper@37906
   269
    handle _ => false;
neuper@37906
   270
neuper@37906
   271
neuper@37906
   272
(***. context to thy concerning rewriting .***)
neuper@37906
   273
neuper@37906
   274
(*.create the unique handles and filenames for the theory-data.*)
neuper@37906
   275
fun part2filename str = part2guh str ^ ".xml" : filename;
neuper@37906
   276
fun thy2filename thy' = thy2guh thy' ^ ".xml" : filename;
neuper@37906
   277
fun thypart2filename thy' = thypart2guh thy' ^ ".xml" : filename;
neuper@37906
   278
neuper@37906
   279
fun thm2filename (isa_thyID: string * thyID) thmID =
neuper@37906
   280
    (thm2guh isa_thyID thmID) ^ ".xml" : filename;
neuper@55437
   281
fun rls2filename (isa, thyID) rls' =
neuper@37906
   282
    rls2guh (isa, thyID) rls' ^ ".xml" : filename;
neuper@37906
   283
fun cal2filename (isa, thyID:thyID) calID = 
neuper@37906
   284
    cal2guh (isa, thyID:thyID) calID ^ ".xml" : filename;
neuper@37906
   285
fun ord2filename (isa, thyID:thyID) (rew_ord':rew_ord') =
neuper@37906
   286
    ord2guh (isa, thyID:thyID) (rew_ord':rew_ord') ^ ".xml" : filename;
neuper@37906
   287
neuper@37906
   288
(**.set up isab_thm_thy in Isac.ML.**)
neuper@37906
   289
neuper@38061
   290
fun rearrange (thyID, (thmID, thm)) = (thmID, (thyID, prop_of thm));
neuper@42399
   291
(* WN120320: reconsider design since thmID and thyID can be retrieved from thm: *)
neuper@42399
   292
fun rearrange' (thmID, thm) =
neuper@42399
   293
  (thmID_of_derivation_name thmID,
neuper@42399
   294
    (thyID_of_derivation_name thmID, prop_of thm)): (thmID * (thyID * term));
neuper@38061
   295
fun rearrange_inv (thmID, (thyID, term)) = (thyID, (thmID, term));
neuper@37906
   296
neuper@42399
   297
(*================= version before Isbelle2002 --> 2011 ===========================
neuper@37906
   298
(*.lookup the missing theorems in some thy (of Isabelle).*)
neuper@37906
   299
fun make_isa missthms thy =
neuper@37906
   300
    map (pair (theory2thyID thy)) 
neuper@42399
   301
	((inter eq_thmI) missthms [] (*Global_Theory.all_thms_of thy WN11xxxx: THIS IS TOO EXPENSIVE*)) 
neuper@37906
   302
	: (thyID * (thmID * Thm.thm)) list;
neuper@37906
   303
neuper@37906
   304
(*.separate handling of sym_thms.*)
neuper@37906
   305
fun make_isab rlsthmsNOTisac isab_thys = 
neuper@37906
   306
    let fun les ((s1,_), (s2,_)) = (s1 : string) < s2
neuper@37906
   307
	val notsym = filter_out (is_sym o #1) rlsthmsNOTisac
neuper@37906
   308
	val notsym_isab = (flat o (map (make_isa notsym))) isab_thys
neuper@37906
   309
			  
neuper@37906
   310
	val sym = filter (is_sym o #1) rlsthmsNOTisac
neuper@37906
   311
		  
neuper@37906
   312
	val symsym = map ((apfst sym_drop) o (apsnd sym_thm)) sym
neuper@37906
   313
	val symsym_isab = (flat o (map (make_isa symsym))) isab_thys
neuper@37906
   314
			  
neuper@37906
   315
	val sym_isab = map (((apsnd o apfst) sym_drop) o 
neuper@37906
   316
			    ((apsnd o apsnd) sym_thm)) symsym_isab
neuper@37906
   317
		       
neuper@37906
   318
	val isab = notsym_isab @ symsym_isab @ sym_isab
neuper@38061
   319
    in ((map rearrange) o (gen_sort les)) isab : (thmID * (thyID * term)) list
neuper@37906
   320
    end;
neuper@42399
   321
================= version before Isbelle2002 --> 2011 ===========================*)
neuper@42399
   322
neuper@42399
   323
(*================= trials while Isbelle2002 --> 2011 ===========================
neuper@42399
   324
  WN120320 update Isabelle2002 --> 2011 gave up with expensiveness of Global_Theory.all_thms_of;
neuper@42399
   325
  the code below is outcommented too due to problems with sym_*:
neuper@42399
   326
neuper@42399
   327
  version with term instead of thm, for Theory.axioms_of in isa02
neuper@38002
   328
fun make_isa missthms thy =
neuper@38002
   329
    map (pair (theory2thyID thy)) 
neuper@42376
   330
	((inter eq_thmI') missthms (Theory.axioms_of thy))
neuper@38002
   331
	: (thyID * (thmID * term)) list;
neuper@38061
   332
(* separate handling of sym_thms *)
neuper@38002
   333
fun make_isab rlsthmsNOTisac isab_thys = 
neuper@38002
   334
    let fun les ((s1,_), (s2,_)) = (s1 : string) < s2
neuper@38002
   335
	val notsym = filter_out (is_sym o #1) rlsthmsNOTisac
neuper@38002
   336
	val notsym_isab = (flat o (map (make_isa notsym))) isab_thys
neuper@38002
   337
			  
neuper@38002
   338
	val sym = filter (is_sym o #1) rlsthmsNOTisac
neuper@38002
   339
		  
neuper@38002
   340
	val symsym = map ((apfst sym_drop) o (apsnd sym_trm)) sym
neuper@38002
   341
	val symsym_isab = (flat o (map (make_isa symsym))) isab_thys
neuper@38002
   342
			  
neuper@38002
   343
	val sym_isab = map (((apsnd o apfst) sym_drop) o 
neuper@38002
   344
			    ((apsnd o apsnd) sym_trm)) symsym_isab
neuper@38002
   345
		       
neuper@38002
   346
	val isab = notsym_isab @ symsym_isab @ sym_isab
neuper@38002
   347
    in ((map rearrange) o (gen_sort les)) isab 
neuper@38002
   348
       : (thmID * (thyID * term)) list
neuper@38002
   349
    end;
neuper@42399
   350
================= trials while Isbelle2002 --> 2011 ===========================*)
neuper@37906
   351
neuper@42399
   352
(*================= cheap version without sym_* thms ===========================*)
neuper@42400
   353
fun make_isab rlsthmsNOTisac = map rearrange' rlsthmsNOTisac;
neuper@42399
   354
neuper@42400
   355
fun thy_containing_thm thmDeriv =
neuper@42400
   356
  let
neuper@42400
   357
    val isabthys' = map Context.theory_name (! isabthys);
neuper@42400
   358
  in
neuper@42400
   359
    if member op= isabthys' (thyID_of_derivation_name thmDeriv)
neuper@42400
   360
    then ("Isabelle", thmID_of_derivation_name thmDeriv)
neuper@42400
   361
    else ("IsacKnowledge", thyID_of_derivation_name thmDeriv)
neuper@42400
   362
  end;
neuper@37906
   363
neuper@37906
   364
neuper@42407
   365
(* which theory in ancestors of thy' contains a ruleset;
neuper@42407
   366
  i.e. get the occurence _after_ in the _list_ (is up to asking TUM) theory' *)
neuper@37936
   367
local infix mem; (*from Isabelle2002*)
neuper@37936
   368
fun x mem [] = false
neuper@37936
   369
  | x mem (y :: ys) = x = y orelse x mem ys;
neuper@37936
   370
in
neuper@37906
   371
fun thy_containing_rls (thy':theory') (rls':rls') =
neuper@42407
   372
  let
neuper@42407
   373
    val rls' = strip_thy rls'
neuper@42407
   374
    val thy' = thyID2theory' thy'
neuper@42407
   375
    (*take thys between "Isac" and thy' excluding search for #1#--see calcelems.sml*)
neuper@42407
   376
    val dropthys =
neuper@42407
   377
      takewhile [] (not o (curry op= thy') o (#1:theory' * theory -> theory'))
neuper@42407
   378
        (rev (!theory'))
neuper@42407
   379
		val dropthys' = map (get_thy o (#1 : (theory' * theory) -> theory')) dropthys
neuper@42407
   380
		(*drop those rulesets which are generated in a theory found in #1#*)
neuper@42407
   381
		val ancestors_rls = 
neuper@42407
   382
		  filter_out ((curry ((op mem) o swap) dropthys') o ((#1 o #2): rls'*(theory' * rls)->theory'))
neuper@52155
   383
		    (rev (KEStore_Elems.get_rlss (Thy_Info.get_theory thy')))
neuper@42407
   384
  in case assoc (ancestors_rls, rls') of
neuper@42407
   385
	     SOME (thy', _) => ("IsacKnowledge", thyID2theory' thy')
neuper@42407
   386
	   | _ => error ("thy_containing_rls : rls '" ^ rls' ^
neuper@42407
   387
	     "' not in !rulset' of ancestors of thy '" ^ thy' ^ "'")
neuper@42407
   388
  end;
neuper@42407
   389
neuper@37906
   390
fun thy_containing_cal (thy':theory') termop =
neuper@42407
   391
  let
neuper@42407
   392
    val thy' = thyID2theory' thy'
neuper@42407
   393
    val dropthys =
neuper@42407
   394
      takewhile [] (not o (curry op= thy') o (#1:theory' * theory -> theory'))
neuper@42407
   395
        (rev (!theory'))
neuper@42407
   396
    val dropthys' = map (get_thy o (#1 : (theory' * theory) -> theory')) dropthys
neuper@42407
   397
    val ancestors_cal =
neuper@42407
   398
      filter_out ((curry ((op mem) o swap) dropthys') o (#1 : calc -> string))
s1210629013@52153
   399
        (Thy_Info.get_theory thy' |> KEStore_Elems.get_calcs |> rev)
neuper@42407
   400
  in case assoc (ancestors_cal, strip_thy termop) of
neuper@42407
   401
	     SOME (th_termop, _) => ("IsacKnowledge", strip_thy th_termop)
neuper@42407
   402
	   | _ => error ("thy_containing_cal : rls '" ^ termop ^
s1210629013@52153
   403
			 "' not in KEStore_Elems of ancestors of thy '" ^ thy' ^ "'")
neuper@42407
   404
  end
neuper@42407
   405
end; (*local*)
neuper@37906
   406
neuper@37906
   407
(*.packing return-values to matchTheory, contextToThy for xml-generation.*)
neuper@37906
   408
datatype contthy =  (*also an item from KEStore on Browser ......#*)
neuper@37906
   409
	 EContThy   (*not from KEStore ...........................*)
neuper@37906
   410
       | ContThm of (*a theorem in contex =============*)
neuper@37906
   411
	 {thyID   : thyID,         (*for *2guh in sub-elems here .*)
neuper@37906
   412
	  thm     : guh,           (*theorem in the context      .*)
neuper@37906
   413
	  applto  : term,	   (*applied to formula ...      .*)
neuper@37906
   414
	  applat  : term,	   (*...  with lhs inserted      .*)
neuper@37906
   415
	  reword  : rew_ord',      (*order used for rewrite      .*)
neuper@37906
   416
	  asms    : (term          (*asumption instantiated      .*)
neuper@37906
   417
		     * term) list, (*asumption evaluated         .*)
neuper@37906
   418
	  lhs     : term           (*lhs of the theorem ...      #*)
neuper@37906
   419
		    * term,        (*... instantiated            .*)
neuper@37906
   420
	  rhs     : term           (*rhs of the theorem ...      #*)
neuper@37906
   421
		    * term,        (*... instantiated            .*)
neuper@37906
   422
	  result  : term,	   (*resulting from the rewrite  .*)
neuper@37906
   423
	  resasms : term list,     (*... with asms stored        .*)
neuper@37906
   424
	  asmrls  : rls'           (*ruleset for evaluating asms .*)
neuper@37906
   425
		    }						 
neuper@37906
   426
	| ContThmInst of (*a theorem with bdvs in contex ======== *)
neuper@37906
   427
	 {thyID   : thyID,         (*for *2guh in sub-elems here .*)
neuper@37906
   428
	  thm     : guh,           (*theorem in the context      .*)
neuper@37906
   429
	  bdvs    : subst,         (*bound variables to modify....*)
neuper@37906
   430
	  thminst : term,          (*... theorem instantiated    .*)
neuper@37906
   431
	  applto  : term,	   (*applied to formula ...      .*)
neuper@37906
   432
	  applat  : term,	   (*...  with lhs inserted      .*)
neuper@37906
   433
	  reword  : rew_ord',      (*order used for rewrite      .*)
neuper@37906
   434
	  asms    : (term          (*asumption instantiated      .*)
neuper@37906
   435
		     * term) list, (*asumption evaluated         .*)
neuper@37906
   436
	  lhs     : term           (*lhs of the theorem ...      #*)
neuper@37906
   437
		    * term,        (*... instantiated            .*)
neuper@37906
   438
	  rhs     : term           (*rhs of the theorem ...      #*)
neuper@37906
   439
		    * term,        (*... instantiated            .*)
neuper@37906
   440
	  result  : term,	   (*resulting from the rewrite  .*)
neuper@37906
   441
	  resasms : term list,     (*... with asms stored        .*)
neuper@37906
   442
	  asmrls  : rls'           (*ruleset for evaluating asms .*)
neuper@37906
   443
		      }						 
neuper@37906
   444
	| ContRls of (*a rule set in contex ===================== *)
neuper@37906
   445
	 {thyID   : thyID,         (*for *2guh in sub-elems here .*)
neuper@37906
   446
	  rls     : guh,           (*rule set in the context     .*)
neuper@37906
   447
	  applto  : term,	   (*rewrite this formula        .*)
neuper@37906
   448
	  result  : term,	   (*resulting from the rewrite  .*)
neuper@37906
   449
	  asms    : term list      (*... with asms stored        .*)
neuper@37906
   450
		    }						 
neuper@37906
   451
	| ContRlsInst of (*a rule set with bdvs in contex ======= *)
neuper@37906
   452
	 {thyID   : thyID,         (*for *2guh in sub-elems here .*)
neuper@37906
   453
	  rls     : guh,           (*rule set in the context     .*)
neuper@37906
   454
	  bdvs    : subst,         (*for bound variables in thms .*)
neuper@37906
   455
	  applto  : term,	   (*rewrite this formula        .*)
neuper@37906
   456
	  result  : term,	   (*resulting from the rewrite  .*)
neuper@37906
   457
	  asms    : term list      (*... with asms stored        .*)
neuper@37906
   458
		    }
neuper@37906
   459
	| ContNOrew of (*no rewrite for thm or rls ============== *)
neuper@37906
   460
	 {thyID   : thyID,         (*for *2guh in sub-elems here .*)
neuper@37906
   461
	  thm_rls : guh,           (*thm or rls in the context   .*)
neuper@37906
   462
	  applto  : term	   (*rewrite this formula        .*)
neuper@37906
   463
		    }						 
neuper@37906
   464
	| ContNOrewInst of (*no rewrite for some instantiation == *)
neuper@37906
   465
	 {thyID   : thyID,         (*for *2guh in sub-elems here .*)
neuper@37906
   466
	  thm_rls : guh,           (*thm or rls in the context   .*)
neuper@37906
   467
	  bdvs    : subst,         (*for bound variables in thms .*)
neuper@37906
   468
	  thminst : term,          (*... theorem instantiated    .*)
neuper@37906
   469
	  applto  : term	   (*rewrite this formula        .*)
neuper@37906
   470
		    };
neuper@37906
   471
neuper@37906
   472
(*.check a rewrite-tac for bdv (RL always used *_Inst !) TODO.WN060718
neuper@37906
   473
   pass other tacs unchanged.*)
neuper@37906
   474
fun get_tac_checked pt ((p,p_) : pos') = get_obj g_tac pt p;
neuper@37906
   475
neuper@37906
   476
(*..*)
neuper@37906
   477
neuper@37906
   478
neuper@37906
   479
neuper@37906
   480
(*.get the formula f at ptp rewritten by the Rewrite_* already applied to f.*)
neuper@37906
   481
(* val (Rewrite' (thy', ord', erls, _, (thmID,_), f, (res,asm))) = tac';
neuper@37906
   482
   *)
neuper@42400
   483
fun context_thy (pt, pos as (p,p_)) (tac as Rewrite (thmID,_)) =
neuper@42400
   484
      (case applicable_in pos pt tac of
neuper@42400
   485
        Appl (Rewrite' (thy', ord', erls, _, (thmID,_), f, (res,asm))) =>
neuper@42400
   486
          let 
neuper@48892
   487
            val thmDeriv = "WN120320: AT Isa2009-2-->11 BROKEN"
neuper@42400
   488
            val thy = assoc_thy thy'
neuper@42400
   489
            val thm = (norm o #prop o rep_thm o (Global_Theory.get_thm thy)) thmID
neuper@42400
   490
          in
neuper@42400
   491
            ContThm
neuper@42400
   492
             {thyID   = theory'2thyID thy',
neuper@42400
   493
              thm     =
neuper@42400
   494
                thm2guh (thy_containing_thm thmDeriv) (thmID_of_derivation_name thmDeriv),
neuper@42400
   495
              applto  = f,
neuper@42400
   496
              applat  = e_term,
neuper@42400
   497
              reword  = ord',
neuper@42400
   498
              asms    = [](*asms ~~ asms'*),
neuper@42400
   499
              lhs     = (e_term, e_term)(*(lhs, lhs')*),
neuper@42400
   500
              rhs     = (e_term, e_term)(*(rhs, rhs')*),
neuper@42400
   501
              result  = res,
neuper@42400
   502
              resasms = asm,
neuper@42400
   503
              asmrls  = id_rls erls}
neuper@42400
   504
		       end
neuper@42400
   505
       | Notappl _ =>
neuper@42400
   506
           let
neuper@48892
   507
             val thmDeriv = "WN120320: AT Isa2009-2-->11 BROKEN"
neuper@42400
   508
             val pp = par_pblobj pt p
neuper@42400
   509
             val thy' = get_obj g_domID pt pp
neuper@42400
   510
             val f = case p_ of
neuper@42400
   511
                   Frm => get_obj g_form pt p
neuper@42400
   512
                 | Res => (fst o (get_obj g_result pt)) p
neuper@42400
   513
           in
neuper@42400
   514
             ContNOrew
neuper@42400
   515
              {thyID   = theory'2thyID thy',
neuper@42400
   516
               thm_rls = thm2guh (thy_containing_thm thmDeriv) (thmID_of_derivation_name thmDeriv),
neuper@42400
   517
               applto  = f}
neuper@42400
   518
		       end)
neuper@37906
   519
    
neuper@42400
   520
  | context_thy (pt, pos as (p,p_)) (tac as Rewrite_Inst (subs, (thmID,_))) =
neuper@42400
   521
	    (case applicable_in pos pt tac of
neuper@42400
   522
	       Appl (Rewrite_Inst' (thy', ord', erls, _, subst, (thmID,_), f, (res, asm))) =>
neuper@42400
   523
	         let
neuper@42400
   524
             val thmDeriv = "WN120320: get this from a reference variable ?!?"
neuper@42400
   525
	           val thm = (norm o #prop o rep_thm o (Global_Theory.get_thm (assoc_thy thy'))) thmID
neuper@42400
   526
	           val thminst = inst_bdv subst thm
neuper@42400
   527
	         in
neuper@42400
   528
	           ContThmInst
neuper@42400
   529
	            {thyID   = theory'2thyID thy',
neuper@42400
   530
	             thm     = thm2guh (thy_containing_thm thmDeriv) (thmID_of_derivation_name thmDeriv),
neuper@42400
   531
	             bdvs    = subst,
neuper@42400
   532
	             thminst = thminst,
neuper@42400
   533
	             applto  = f,
neuper@42400
   534
	             applat  = e_term,
neuper@42400
   535
	             reword  = ord',
neuper@42400
   536
	             asms    = [](*asms ~~ asms'*),
neuper@42400
   537
	             lhs     = (e_term, e_term)(*(lhs, lhs')*),
neuper@42400
   538
	             rhs     = (e_term, e_term)(*(rhs, rhs')*),
neuper@42400
   539
	             result  = res,
neuper@42400
   540
	             resasms = asm,
neuper@42400
   541
	             asmrls  = id_rls erls}
neuper@42400
   542
	         end
neuper@42400
   543
       | Notappl _ =>
neuper@42400
   544
           let
neuper@42400
   545
             val thmDeriv = "WN120320: get this from a reference variable ?!?"
neuper@42400
   546
             val pp = par_pblobj pt p
neuper@42400
   547
             val thy' = get_obj g_domID pt pp
neuper@42400
   548
             val subst = subs2subst (assoc_thy thy') subs
neuper@42400
   549
             val thm = (norm o #prop o rep_thm o (Global_Theory.get_thm (assoc_thy thy'))) thmID
neuper@42400
   550
             val thminst = inst_bdv subst thm
neuper@42400
   551
             val f = case p_ of
neuper@42400
   552
                 Frm => get_obj g_form pt p
neuper@42400
   553
               | Res => (fst o (get_obj g_result pt)) p
neuper@42400
   554
           in
neuper@42400
   555
             ContNOrewInst
neuper@42400
   556
              {thyID   = theory'2thyID thy',
neuper@42400
   557
               thm_rls =
neuper@42400
   558
                 "WN120320: thm2guh (thy_containing_thm thmDeriv) (thmID_of_derivation_name thmDeriv)",
neuper@42400
   559
               bdvs    = subst,
neuper@42400
   560
               thminst = thminst,
neuper@42400
   561
               applto  = f}
neuper@42400
   562
           end)
neuper@37906
   563
  | context_thy (pt,p) (tac as Rewrite_Set rls') =
neuper@42400
   564
      (case applicable_in p pt tac of
neuper@42400
   565
         Appl (Rewrite_Set' (thy', _, rls, f, (res,asm))) =>
neuper@42400
   566
           ContRls
neuper@42400
   567
            {thyID  = theory'2thyID thy',
neuper@42400
   568
             rls    = "WN120320:  rls2guh (thy_containing_rls thy' rls') rls'",
neuper@42400
   569
             applto = f,	  
neuper@42400
   570
             result = res,	  
neuper@42400
   571
             asms   = asm})
neuper@37906
   572
  | context_thy (pt,p) (tac as Rewrite_Set_Inst (subs, rls')) = 
neuper@42400
   573
      (case applicable_in p pt tac of
neuper@42400
   574
         Appl (Rewrite_Set_Inst' (thy', _, subst, rls, f, (res,asm))) =>
neuper@42400
   575
           ContRlsInst
neuper@42400
   576
            {thyID  = theory'2thyID thy',
neuper@42400
   577
             rls    = "WN120320: rls2guh (thy_containing_rls thy' rls') rls'",
neuper@42400
   578
             bdvs   = subst,
neuper@42400
   579
             applto = f,	  
neuper@42400
   580
             result = res,	  
neuper@42400
   581
             asms   = asm});
neuper@37906
   582
neuper@42372
   583
(* get all theorems in a rule set (recursivley containing rule sets) *)
neuper@37906
   584
fun thm_of_rule Erule = []
neuper@37906
   585
  | thm_of_rule (thm as Thm _) = [thm]
neuper@37906
   586
  | thm_of_rule (Calc _) = []
neuper@37906
   587
  | thm_of_rule (Cal1 _) = []
neuper@37906
   588
  | thm_of_rule (Rls_ rls) = thms_of_rls rls
neuper@37906
   589
and thms_of_rls Erls = []
neuper@37906
   590
  | thms_of_rls (Rls {rules,...}) = (flat o (map  thm_of_rule)) rules
neuper@37906
   591
  | thms_of_rls (Seq {rules,...}) = (flat o (map  thm_of_rule)) rules
neuper@37906
   592
  | thms_of_rls (Rrls _) = [];
neuper@37906
   593
(* val Hrls {thy_rls = (_, rls),...} =
neuper@37906
   594
       get_the ["IsacKnowledge", "Test", "Rulesets", "expand_binomtest"];
neuper@37906
   595
> thms_of_rls rls;
neuper@37906
   596
   *)
neuper@37906
   597
neuper@37906
   598
(*. check if a rule is contained in a rule-set (recursivley down in Rls_);
neuper@37906
   599
    this rule can even be a rule-set itself.*)
neuper@37906
   600
fun contains_rule r rls = 
neuper@37906
   601
    let fun find (r, Rls_ rls) = finds (get_rules rls)
neuper@37906
   602
	  | find r12 = eq_rule r12
neuper@37906
   603
	and finds [] = false
neuper@37906
   604
	  | finds (r1 :: rs) = if eq_rule (r, r1) then true else finds rs;
neuper@37906
   605
    in 
neuper@38015
   606
    (*tracing ("### contains_rule: r = "^rule2str r^", rls = "^rls2str rls);*)
neuper@37906
   607
    finds (get_rules rls) 
neuper@37906
   608
    end;
neuper@37906
   609
neuper@37906
   610
(*. try if a rewrite-rule is applicable to a given formula; 
neuper@37906
   611
    in case of rule-sets (recursivley) collect all _atomic_ rewrites .*) 
neuper@37906
   612
fun try_rew thy ((_, ro):rew_ord) erls (subst:subst) f (thm' as Thm(id, thm)) =
neuper@37906
   613
    if contains_bdv thm
neuper@37906
   614
    then case rewrite_inst_ thy ro erls false subst thm f of
s1210629013@52153
   615
	      SOME (f',_) =>[rule2tac thy subst thm']
neuper@37926
   616
	    | NONE => []
neuper@37906
   617
    else (case rewrite_ thy ro erls false thm f of
s1210629013@52153
   618
	SOME (f',_) => [rule2tac thy [] thm']
neuper@37926
   619
	    | NONE => [])
neuper@37906
   620
  | try_rew thy _ _ _ f (cal as Calc c) = 
neuper@37906
   621
    (case get_calculation_ thy c f of
s1210629013@52153
   622
	SOME (str, _) => [rule2tac thy [] cal]
neuper@37926
   623
      | NONE => [])
neuper@37906
   624
  | try_rew thy _ _ _ f (cal as Cal1 c) = 
neuper@37906
   625
    (case get_calculation_ thy c f of
s1210629013@52153
   626
	SOME (str, _) => [rule2tac thy [] cal]
neuper@37926
   627
      | NONE => [])
neuper@37906
   628
  | try_rew thy _ _ subst f (Rls_ rls) = filter_appl_rews thy subst f rls
neuper@37906
   629
and filter_appl_rews thy subst f (Rls {rew_ord = ro, erls, rules,...}) = 
neuper@37906
   630
    distinct (flat (map (try_rew thy ro erls subst f) rules))
neuper@37906
   631
  | filter_appl_rews thy subst f (Seq {rew_ord = ro, erls, rules,...}) = 
neuper@37906
   632
    distinct (flat (map (try_rew thy ro erls subst f) rules))
neuper@37906
   633
  | filter_appl_rews thy subst f (Rrls _) = [];
neuper@37906
   634
neuper@37906
   635
(*. decide if a tactic is applicable to a given formula; 
neuper@37906
   636
    in case of Rewrite_Set* go down to _atomic_ rewrite-tactics .*)
neuper@37906
   637
(* val 
neuper@37906
   638
   *)
neuper@37906
   639
fun atomic_appl_tacs thy _ _ f (Calculate scrID) =
s1210629013@52153
   640
    try_rew thy e_rew_ordX e_rls [] f (Calc (assoc_calc' thy scrID |> snd))
neuper@37906
   641
  | atomic_appl_tacs thy ro erls f (Rewrite (thm' as (thmID, _))) =
neuper@37906
   642
    try_rew thy (ro, assoc_rew_ord ro) erls [] f 
neuper@37906
   643
	    (Thm (thmID, assoc_thm' thy thm'))
neuper@37906
   644
  | atomic_appl_tacs thy ro erls f (Rewrite_Inst (subs, thm' as (thmID, _))) =
neuper@37906
   645
    try_rew thy (ro, assoc_rew_ord ro) erls (subs2subst thy subs) f 
neuper@37906
   646
	    (Thm (thmID, assoc_thm' thy thm'))
neuper@37906
   647
neuper@37906
   648
  | atomic_appl_tacs thy _ _ f (Rewrite_Set rls') =
neuper@37906
   649
    filter_appl_rews thy [] f (assoc_rls rls')
neuper@37906
   650
  | atomic_appl_tacs thy _ _ f (Rewrite_Set_Inst (subs, rls')) =
neuper@37906
   651
    filter_appl_rews thy (subs2subst thy subs) f (assoc_rls rls')
neuper@37906
   652
  | atomic_appl_tacs _ _ _ _ tac = 
neuper@38015
   653
    (tracing ("### atomic_appl_tacs: not impl. for tac = '"^ tac2str tac ^"'");
neuper@37906
   654
     []);
neuper@37906
   655
neuper@37906
   656
(*.filenames not only for thydata, but also for thy's etc.*)
neuper@37906
   657
fun theID2filename (theID:theID) = theID2guh theID ^ ".xml" : filename;
neuper@37906
   658
neuper@37906
   659
fun guh2theID (guh:guh) =
neuper@40836
   660
    let val guh' = Symbol.explode guh
neuper@37906
   661
	val part = implode (take_fromto 1 4 guh')
neuper@37906
   662
	val isa = implode (take_fromto 5 9 guh')
neuper@37935
   663
    in if not (member op = ["exp_", "thy_", "pbl_", "met_"] part)
neuper@38031
   664
       then error ("guh '"^guh^"' does not begin with \
neuper@37906
   665
				     \exp_ | thy_ | pbl_ | met_")
neuper@37906
   666
       else let val chap = case isa of
neuper@37906
   667
				"isab_" => "Isabelle"
neuper@37906
   668
			      | "scri_" => "IsacScripts"
neuper@37906
   669
			      | "isac_" => "IsacKnowledge"
neuper@37906
   670
			      | _ => 
neuper@38031
   671
				error ("guh2theID: '"^guh^
neuper@37906
   672
					     "' does not have isab_ | scri_ | \
neuper@37906
   673
					     \isac_ at position 5..9")
neuper@37906
   674
		val rest = takerest (9, guh') 
neuper@37906
   675
		val thyID = takewhile [] (not o (curry op= "-")) rest
neuper@37906
   676
		val rest' = dropuntil (curry op= "-") rest
neuper@37906
   677
	    in case implode rest' of
neuper@37906
   678
		   "-part" => [chap] : theID
neuper@37906
   679
		 | "" => [chap, implode thyID]
neuper@37906
   680
		 | "-Theorems" => [chap, implode thyID, "Theorems"]
neuper@37906
   681
		 | "-Rulesets" => [chap, implode thyID, "Rulesets"]
neuper@37906
   682
		 | "-Operations" => [chap, implode thyID, "Operations"]
neuper@37906
   683
		 | "-Orders" => [chap, implode thyID, "Orders"]
neuper@37906
   684
		 | _ => 
neuper@37906
   685
		   let val sect = implode (take_fromto 1 5 rest')
neuper@37906
   686
		       val sect' = 
neuper@37906
   687
			   case sect of
neuper@37906
   688
			       "-thm-" => "Theorems"
neuper@37906
   689
			     | "-rls-" => "Rulesets"
neuper@37906
   690
			     | "-cal-" => "Operations"
neuper@37906
   691
			     | "-ord-" => "Orders"
neuper@37906
   692
			     | str => 
neuper@38031
   693
			       error ("guh2theID: '"^guh^"' has '"^sect^
neuper@37906
   694
					    "' instead -thm- | -rls- | \
neuper@37906
   695
					    \-cal- | -ord-")
neuper@37906
   696
		   in [chap, implode thyID, sect', implode 
neuper@37906
   697
						       (takerest (5, rest'))]
neuper@37906
   698
		   end
neuper@37906
   699
	    end	
neuper@37906
   700
    end;
neuper@37906
   701
(*> guh2theID "thy_isac_Biegelinie-Theorems";
neuper@37906
   702
val it = ["IsacKnowledge", "Biegelinie", "Theorems"] : theID
neuper@37947
   703
> guh2theID "thy_scri_ListC-thm-zip_Nil";
neuper@37947
   704
val it = ["IsacScripts", "ListC", "Theorems", "zip_Nil"] : theID*)
neuper@37906
   705
neuper@37906
   706
fun guh2filename (guh : guh) = guh ^ ".xml" : filename;
neuper@37906
   707
neuper@37906
   708
neuper@37906
   709
(*..*)
neuper@37906
   710
fun guh2rewtac (guh:guh) ([] : subs) =
neuper@37906
   711
    let val [isa, thy, sect, xstr] = guh2theID guh
neuper@37906
   712
    in case sect of
neuper@37906
   713
	   "Theorems" => Rewrite (xstr, "")
neuper@37906
   714
	 | "Rulesets" => Rewrite_Set xstr
neuper@38031
   715
	 | str => error ("guh2rewtac: not impl. for '"^xstr^"'") 
neuper@37906
   716
    end
neuper@37906
   717
  | guh2rewtac (guh:guh) subs =
neuper@37906
   718
    let val [isa, thy, sect, xstr] = guh2theID guh
neuper@37906
   719
    in case sect of
neuper@37906
   720
	   "Theorems" => Rewrite_Inst (subs, (xstr, ""))
neuper@37906
   721
	 | "Rulesets" => Rewrite_Set_Inst (subs,  xstr)
neuper@38031
   722
	 | str => error ("guh2rewtac: not impl. for '"^xstr^"'") 
neuper@37906
   723
    end;
neuper@37906
   724
(*> guh2rewtac "thy_isac_Test-thm-constant_mult_square" [];
neuper@37906
   725
val it = Rewrite ("constant_mult_square", "") : tac
neuper@37906
   726
> guh2rewtac "thy_isac_Test-thm-risolate_bdv_add" ["(bdv, x)"];
neuper@37906
   727
val it = Rewrite_Inst (["(bdv, x)"], ("risolate_bdv_add", "")) : tac
neuper@37906
   728
> guh2rewtac "thy_isac_Test-rls-Test_simplify" [];
neuper@37906
   729
val it = Rewrite_Set "Test_simplify" : tac
neuper@37906
   730
> guh2rewtac "thy_isac_Test-rls-isolate_bdv" ["(bdv, x)"];
neuper@37906
   731
val it = Rewrite_Set_Inst (["(bdv, x)"], "isolate_bdv") : tac*)
neuper@37906
   732
neuper@37906
   733
neuper@37906
   734
(*.the front-end may request a context for any element of the hierarchy.*)
neuper@37906
   735
(* val guh = "thy_isac_Test-rls-Test_simplify";
neuper@37906
   736
   *)
neuper@37906
   737
fun no_thycontext (guh : guh) = (guh2theID guh; false)
neuper@37906
   738
    handle _ => true;
neuper@37906
   739
neuper@37906
   740
(*> has_thycontext  "thy_isac_Test";
neuper@37906
   741
if has_thycontext  "thy_isac_Test" then "OK" else "NOTOK";
neuper@37906
   742
 *)
neuper@37906
   743
neuper@37906
   744
neuper@37906
   745
neuper@37906
   746
(*.get the substitution of bound variables for matchTheory:
neuper@37906
   747
   # lookup the thm|rls' in the script
neuper@37906
   748
   # take the [(bdv, v_),..] from the respective Rewrite_(Set_)Inst
neuper@37906
   749
   # instantiate this subs with the istates env to [(bdv, x),..]
neuper@37906
   750
   # otherwise [].*)
neuper@37906
   751
(*WN060617 hack assuming that all scripts use only one bound variable
neuper@37906
   752
and use 'v_' as the formal argument for this bound variable*)
neuper@37906
   753
(* val (ScrState (env,_,_,_,_,_), _, guh) = (is, "dummy", guh);
neuper@37906
   754
   *)
neuper@48763
   755
fun subs_from (ScrState (env,_,_,_,_,_)) _ (guh:guh) =
neuper@37906
   756
    let val theID as [isa, thyID, sect, xstr] = guh2theID guh
neuper@37906
   757
    in case sect of
neuper@37906
   758
	   "Theorems" => 
neuper@41899
   759
	   let val thm = Global_Theory.get_thm (assoc_thy (thyID2theory' thyID)) xstr
neuper@37906
   760
	   in if contains_bdv thm
neuper@37906
   761
	      then let val formal_arg = str2term "v_"
neuper@37906
   762
		       val value = subst_atomic env formal_arg
neuper@37906
   763
		   in ["(bdv," ^ term2str value ^ ")"]:subs end
neuper@37906
   764
	      else []
neuper@37906
   765
	   end
neuper@37906
   766
	 | "Rulesets" => 
neuper@37906
   767
	   let val rules = (get_rules o assoc_rls) xstr
neuper@37906
   768
	   in if contain_bdv rules
neuper@37906
   769
	      then let val formal_arg = str2term"v_"
neuper@37906
   770
		       val value = subst_atomic env formal_arg
neuper@37906
   771
		   in ["(bdv,"^term2str value^")"]:subs end
neuper@37906
   772
	      else []
neuper@37906
   773
	   end
neuper@37906
   774
    end;
neuper@37906
   775
neuper@42433
   776
(* get a substitution for "bdv*" from the current program and environment.
neuper@42433
   777
    returns a substitution: subst for rewriting and another: sube for Rewrite:*)
neuper@42433
   778
fun get_bdv_subst prog env =
neuper@42433
   779
  let
neuper@42433
   780
    fun scan (Const _) = NONE
neuper@42433
   781
      | scan (Free _) = NONE
neuper@42433
   782
      | scan (Var _) = NONE
neuper@42433
   783
      | scan (Bound _) = NONE
neuper@42433
   784
      | scan (t as Const ("List.list.Cons", _) $
neuper@42433
   785
         (Const ("Product_Type.Pair", _) $ Free (str, _) $ _) $ _) =
neuper@42433
   786
           if is_bdv_subst t then SOME t else NONE
neuper@42433
   787
      | scan (Abs (_, _, body)) = scan body
neuper@42433
   788
      | scan (t1 $ t2) =
neuper@42433
   789
          case scan t1 of
neuper@42433
   790
            NONE => scan t2
neuper@42433
   791
          | SOME subst => SOME subst
neuper@42433
   792
  in
neuper@42433
   793
    case scan prog of
neuper@42433
   794
      NONE => (NONE: subs option, []: subst)
neuper@42433
   795
    | SOME tm =>
neuper@42433
   796
        let val subst = tm |> subst_atomic env |> isalist2list |> map isapair2pair
neuper@42433
   797
          (*"[(bdv,v_v)]": term
neuper@42433
   798
                          |> "[(bdv,x)]": term |> ["(bdv,x)"]: term list
neuper@42433
   799
                                                         |> [("bdv","x")]: (term * term) list*)
neuper@42433
   800
        in (SOME (subst2subs subst), subst) end
neuper@42433
   801
          (*"SOME [(bdv,v_v)]": term --> ["(bdv,v_v)"]: string list*)
neuper@42433
   802
  end;
neuper@42433
   803
neuper@42433
   804
neuper@37906
   805
(* use"ME/rewtools.sml";
neuper@37906
   806
   *)
neuper@37906
   807