src/Tools/isac/Interpret/rewtools.sml
author Walther Neuper <neuper@ist.tugraz.at>
Tue, 10 Apr 2012 09:31:21 +0200
changeset 42407 81afb8eb9b03
parent 42400 dcacb8077a98
child 42433 ed0ff27b6165
permissions -rw-r--r--
xml-files created from Knowledge (Isabelle2002 --> 2011)

# creation was 'quick and dirty', see scr/Tools/isac/TODO.txt
# further checks will be done in the html-format created by Java code as next step.
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@37936
    34
        val (deriv, {thy_ref = thy_ref, 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@37936
    44
    in assbl_thm deriv thy_ref 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@38053
    82
 (Print_Mode.setmp [] (Syntax.string_of_term @{context}))) (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@37906
   226
  | sym_rls (Rls {id, scr, calc, erls, srls, rules, rew_ord, preconds}) =
neuper@37906
   227
    Rls {id="sym_"^id, scr=scr, calc=calc, erls=erls, srls=srls, 
neuper@37906
   228
	 rules=rules, rew_ord=rew_ord, preconds=preconds}
neuper@37906
   229
  | sym_rls (Seq {id, scr, calc, erls, srls, rules, rew_ord, preconds}) =
neuper@37906
   230
    Seq {id="sym_"^id, scr=scr, calc=calc, erls=erls, srls=srls, 
neuper@37906
   231
	 rules=rules, rew_ord=rew_ord, preconds=preconds}
neuper@37906
   232
  | sym_rls (Rrls {id, scr, calc, erls, prepat, rew_ord}) = 
neuper@37906
   233
    Rrls {id="sym_"^id, scr=scr, calc=calc, 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 part2guh ([str]:theID) =
neuper@37906
   276
    (case str of
neuper@37906
   277
	"Isabelle" => "thy_isab_" ^ str ^ "-part" : guh
neuper@37906
   278
      | "IsacScripts" => "thy_scri_" ^ str ^ "-part"
neuper@37906
   279
      | "IsacKnowledge" => "thy_isac_" ^ str ^ "-part"
neuper@38031
   280
      | str => error ("thy2guh: called with '"^str^"'"))
neuper@38031
   281
  | part2guh theID = error ("part2guh called with theID = "
neuper@37906
   282
				  ^ theID2str theID);
neuper@37906
   283
fun part2filename str = part2guh str ^ ".xml" : filename;
neuper@37906
   284
neuper@37906
   285
neuper@37906
   286
fun thy2guh ([part, thyID]:theID) =
neuper@37906
   287
    (case part of
neuper@37906
   288
	"Isabelle" => "thy_isab_" ^ thyID : guh
neuper@37906
   289
      | "IsacScripts" => "thy_scri_" ^ thyID
neuper@37906
   290
      | "IsacKnowledge" => "thy_isac_" ^ thyID
neuper@38031
   291
      | str => error ("thy2guh: called with '"^str^"'"))
neuper@38031
   292
  | thy2guh theID = error ("thy2guh called with '"^strs2str' theID^"'");
neuper@37906
   293
fun thy2filename thy' = thy2guh thy' ^ ".xml" : filename;
neuper@37906
   294
fun thypart2guh ([part, thyID, thypart]:theID) = 
neuper@37906
   295
    case part of
neuper@37906
   296
	"Isabelle" => "thy_isab_" ^ thyID ^ "-" ^ thypart : guh
neuper@37906
   297
      | "IsacScripts" => "thy_scri_" ^ thyID ^ "-" ^ thypart
neuper@37906
   298
      | "IsacKnowledge" => "thy_isac_" ^ thyID ^ "-" ^ thypart
neuper@38031
   299
      | str => error ("thypart2guh: called with '"^str^"'");
neuper@37906
   300
fun thypart2filename thy' = thypart2guh thy' ^ ".xml" : filename;
neuper@37906
   301
neuper@37906
   302
(*.convert the data got via contextToThy to a globally unique handle
neuper@37906
   303
   there is another way to get the guh out of the 'theID' in the hierarchy.*)
neuper@37906
   304
fun thm2guh (isa, thyID:thyID) (thmID:thmID) =
neuper@37906
   305
    case isa of
neuper@37906
   306
	"Isabelle" => 
neuper@37906
   307
	"thy_isab_" ^ theory'2thyID thyID ^ "-thm-" ^ strip_thy thmID : guh
neuper@37906
   308
    | "IsacKnowledge" =>
neuper@37906
   309
	"thy_isac_" ^ theory'2thyID thyID ^ "-thm-" ^ strip_thy thmID
neuper@37906
   310
    | "IsacScripts" =>
neuper@37906
   311
	"thy_scri_" ^ theory'2thyID thyID ^ "-thm-" ^ strip_thy thmID
neuper@38031
   312
    | str => error ("thm2guh called with isa = '"^isa^
neuper@37906
   313
			  "' for thm = "^thmID^"'");
neuper@37906
   314
fun thm2filename (isa_thyID: string * thyID) thmID =
neuper@37906
   315
    (thm2guh isa_thyID thmID) ^ ".xml" : filename;
neuper@37906
   316
neuper@37906
   317
fun rls2guh (isa, thyID:thyID) (rls':rls') =
neuper@37906
   318
    case isa of
neuper@37906
   319
	"Isabelle" => 
neuper@37906
   320
	    "thy_isab_" ^ theory'2thyID thyID ^ "-rls-" ^ rls' : guh
neuper@37906
   321
    | "IsacKnowledge" =>
neuper@37906
   322
	    "thy_isac_" ^ theory'2thyID thyID ^ "-rls-" ^ rls'
neuper@37906
   323
    | "IsacScripts" =>
neuper@37906
   324
	    "thy_scri_" ^ theory'2thyID thyID ^ "-rls-" ^ rls'
neuper@38031
   325
    | str => error ("rls2guh called with isa = '"^isa^
neuper@37906
   326
			  "' for rls = '"^rls'^"'");
neuper@37906
   327
	fun rls2filename (isa, thyID) rls' =
neuper@37906
   328
    rls2guh (isa, thyID) rls' ^ ".xml" : filename;
neuper@37906
   329
neuper@37906
   330
fun cal2guh (isa, thyID:thyID) calID =
neuper@37906
   331
    case isa of
neuper@37906
   332
	"Isabelle" => 
neuper@37906
   333
	"thy_isab_" ^ theory'2thyID thyID ^ "-cal-" ^ calID : guh
neuper@37906
   334
      | "IsacKnowledge" =>
neuper@37906
   335
	"thy_isac_" ^ theory'2thyID thyID ^ "-cal-" ^ calID
neuper@37906
   336
      | "IsacScripts" =>
neuper@37906
   337
	"thy_scri_" ^ theory'2thyID thyID ^ "-cal-" ^ calID
neuper@38031
   338
      | str => error ("cal2guh called with isa = '"^isa^
neuper@37906
   339
			  "' for cal = '"^calID^"'");
neuper@37906
   340
fun cal2filename (isa, thyID:thyID) calID = 
neuper@37906
   341
    cal2guh (isa, thyID:thyID) calID ^ ".xml" : filename;
neuper@37906
   342
neuper@37906
   343
fun ord2guh (isa, thyID:thyID) (rew_ord':rew_ord') =
neuper@37906
   344
    case isa of
neuper@37906
   345
	"Isabelle" => 
neuper@37906
   346
	"thy_isab_" ^ theory'2thyID thyID ^ "-ord-" ^ rew_ord' : guh
neuper@37906
   347
      | "IsacKnowledge" =>
neuper@37906
   348
	"thy_isac_" ^ theory'2thyID thyID ^ "-ord-" ^ rew_ord'
neuper@37906
   349
      | "IsacScripts" =>
neuper@37906
   350
	"thy_scri_" ^ theory'2thyID thyID ^ "-ord-" ^ rew_ord'
neuper@38031
   351
      | str => error ("ord2guh called with isa = '"^isa^
neuper@37906
   352
			  "' for ord = '"^rew_ord'^"'");
neuper@37906
   353
fun ord2filename (isa, thyID:thyID) (rew_ord':rew_ord') =
neuper@37906
   354
    ord2guh (isa, thyID:thyID) (rew_ord':rew_ord') ^ ".xml" : filename;
neuper@37906
   355
neuper@37906
   356
neuper@37906
   357
(**.set up isab_thm_thy in Isac.ML.**)
neuper@37906
   358
neuper@38061
   359
fun rearrange (thyID, (thmID, thm)) = (thmID, (thyID, prop_of thm));
neuper@42399
   360
(* WN120320: reconsider design since thmID and thyID can be retrieved from thm: *)
neuper@42399
   361
fun rearrange' (thmID, thm) =
neuper@42399
   362
  (thmID_of_derivation_name thmID,
neuper@42399
   363
    (thyID_of_derivation_name thmID, prop_of thm)): (thmID * (thyID * term));
neuper@38061
   364
fun rearrange_inv (thmID, (thyID, term)) = (thyID, (thmID, term));
neuper@37906
   365
neuper@42399
   366
(*================= version before Isbelle2002 --> 2011 ===========================
neuper@37906
   367
(*.lookup the missing theorems in some thy (of Isabelle).*)
neuper@37906
   368
fun make_isa missthms thy =
neuper@37906
   369
    map (pair (theory2thyID thy)) 
neuper@42399
   370
	((inter eq_thmI) missthms [] (*Global_Theory.all_thms_of thy WN11xxxx: THIS IS TOO EXPENSIVE*)) 
neuper@37906
   371
	: (thyID * (thmID * Thm.thm)) list;
neuper@37906
   372
neuper@37906
   373
(*.separate handling of sym_thms.*)
neuper@37906
   374
fun make_isab rlsthmsNOTisac isab_thys = 
neuper@37906
   375
    let fun les ((s1,_), (s2,_)) = (s1 : string) < s2
neuper@37906
   376
	val notsym = filter_out (is_sym o #1) rlsthmsNOTisac
neuper@37906
   377
	val notsym_isab = (flat o (map (make_isa notsym))) isab_thys
neuper@37906
   378
			  
neuper@37906
   379
	val sym = filter (is_sym o #1) rlsthmsNOTisac
neuper@37906
   380
		  
neuper@37906
   381
	val symsym = map ((apfst sym_drop) o (apsnd sym_thm)) sym
neuper@37906
   382
	val symsym_isab = (flat o (map (make_isa symsym))) isab_thys
neuper@37906
   383
			  
neuper@37906
   384
	val sym_isab = map (((apsnd o apfst) sym_drop) o 
neuper@37906
   385
			    ((apsnd o apsnd) sym_thm)) symsym_isab
neuper@37906
   386
		       
neuper@37906
   387
	val isab = notsym_isab @ symsym_isab @ sym_isab
neuper@38061
   388
    in ((map rearrange) o (gen_sort les)) isab : (thmID * (thyID * term)) list
neuper@37906
   389
    end;
neuper@42399
   390
================= version before Isbelle2002 --> 2011 ===========================*)
neuper@42399
   391
neuper@42399
   392
(*================= trials while Isbelle2002 --> 2011 ===========================
neuper@42399
   393
  WN120320 update Isabelle2002 --> 2011 gave up with expensiveness of Global_Theory.all_thms_of;
neuper@42399
   394
  the code below is outcommented too due to problems with sym_*:
neuper@42399
   395
neuper@42399
   396
  version with term instead of thm, for Theory.axioms_of in isa02
neuper@38002
   397
fun make_isa missthms thy =
neuper@38002
   398
    map (pair (theory2thyID thy)) 
neuper@42376
   399
	((inter eq_thmI') missthms (Theory.axioms_of thy))
neuper@38002
   400
	: (thyID * (thmID * term)) list;
neuper@38061
   401
(* separate handling of sym_thms *)
neuper@38002
   402
fun make_isab rlsthmsNOTisac isab_thys = 
neuper@38002
   403
    let fun les ((s1,_), (s2,_)) = (s1 : string) < s2
neuper@38002
   404
	val notsym = filter_out (is_sym o #1) rlsthmsNOTisac
neuper@38002
   405
	val notsym_isab = (flat o (map (make_isa notsym))) isab_thys
neuper@38002
   406
			  
neuper@38002
   407
	val sym = filter (is_sym o #1) rlsthmsNOTisac
neuper@38002
   408
		  
neuper@38002
   409
	val symsym = map ((apfst sym_drop) o (apsnd sym_trm)) sym
neuper@38002
   410
	val symsym_isab = (flat o (map (make_isa symsym))) isab_thys
neuper@38002
   411
			  
neuper@38002
   412
	val sym_isab = map (((apsnd o apfst) sym_drop) o 
neuper@38002
   413
			    ((apsnd o apsnd) sym_trm)) symsym_isab
neuper@38002
   414
		       
neuper@38002
   415
	val isab = notsym_isab @ symsym_isab @ sym_isab
neuper@38002
   416
    in ((map rearrange) o (gen_sort les)) isab 
neuper@38002
   417
       : (thmID * (thyID * term)) list
neuper@38002
   418
    end;
neuper@42399
   419
================= trials while Isbelle2002 --> 2011 ===========================*)
neuper@37906
   420
neuper@42399
   421
(*================= cheap version without sym_* thms ===========================*)
neuper@42400
   422
fun make_isab rlsthmsNOTisac = map rearrange' rlsthmsNOTisac;
neuper@42399
   423
neuper@42399
   424
neuper@42400
   425
(*================= replaced by Isbelle2002 --> 2011 ===========================
neuper@42399
   426
(* which theory below (in ancestors of) thy' contains a theorem;
neuper@42399
   427
  this can be in isabelle !
neuper@42399
   428
  get the occurence _after_ in the _list_ (is up to asking TUM) theory' *)
neuper@38058
   429
(* val (str, (_, thy)) = ("real_diff_minus", ("Root", Root.thy));
neuper@38058
   430
   val (str, (_, thy)) = ("real_diff_minus", ("Poly", Poly.thy));
neuper@37906
   431
   *)
neuper@37906
   432
fun thy_contains_thm (str:xstring) (_, thy) = 
neuper@41899
   433
    member op = (map (strip_thy o fst) (Global_Theory.all_thms_of thy)) str;
neuper@38050
   434
(* val (thy', str) = ("Isac", "real_mult_minus1");
neuper@38058
   435
   val (thy', str) = ("PolyMinus", "klammer_minus_plus");
neuper@37906
   436
   *)
neuper@37906
   437
fun thy_containing_thm (thy':theory') (str:xstring) =
neuper@42399
   438
  let
neuper@42399
   439
    val thy' = thyID2theory' thy'
neuper@42399
   440
    val str = sym_drop str
neuper@42399
   441
    val startsearch =
neuper@42399
   442
      dropuntil ((curry op= thy') o (#1:theory' * theory -> theory')) (rev (! theory'))
neuper@42399
   443
  in case find_first (thy_contains_thm str) startsearch of
neuper@42399
   444
	     SOME (thy',_) => ("IsacKnowledge", thy')
neuper@42399
   445
	   | NONE => 
neuper@42399
   446
	       (case assoc (!isab_thm_thy (*see Isac.thy*), str) of
neuper@42399
   447
		        SOME (thyID,_) => ("Isabelle", thyID)
neuper@42400
   448
		      | NONE => error ("thy_containing_thm: theorem '" ^ str ^ 
neuper@42399
   449
				      "' not in !theory' above thy '" ^ thy' ^ "'"))
neuper@42399
   450
  end;
neuper@42400
   451
================= replaced by Isbelle2002 --> 2011 ===========================*)
neuper@42400
   452
fun thy_containing_thm thmDeriv =
neuper@42400
   453
  let
neuper@42400
   454
    val isabthys' = map Context.theory_name (! isabthys);
neuper@42400
   455
  in
neuper@42400
   456
    if member op= isabthys' (thyID_of_derivation_name thmDeriv)
neuper@42400
   457
    then ("Isabelle", thmID_of_derivation_name thmDeriv)
neuper@42400
   458
    else ("IsacKnowledge", thyID_of_derivation_name thmDeriv)
neuper@42400
   459
  end;
neuper@37906
   460
neuper@37906
   461
neuper@42407
   462
(* which theory in ancestors of thy' contains a ruleset;
neuper@42407
   463
  i.e. get the occurence _after_ in the _list_ (is up to asking TUM) theory' *)
neuper@37936
   464
local infix mem; (*from Isabelle2002*)
neuper@37936
   465
fun x mem [] = false
neuper@37936
   466
  | x mem (y :: ys) = x = y orelse x mem ys;
neuper@37936
   467
in
neuper@37906
   468
fun thy_containing_rls (thy':theory') (rls':rls') =
neuper@42407
   469
  let
neuper@42407
   470
    val rls' = strip_thy rls'
neuper@42407
   471
    val thy' = thyID2theory' thy'
neuper@42407
   472
    (*take thys between "Isac" and thy' excluding search for #1#--see calcelems.sml*)
neuper@42407
   473
    val dropthys =
neuper@42407
   474
      takewhile [] (not o (curry op= thy') o (#1:theory' * theory -> theory'))
neuper@42407
   475
        (rev (!theory'))
neuper@42407
   476
		val dropthys' = map (get_thy o (#1 : (theory' * theory) -> theory')) dropthys
neuper@42407
   477
		(*drop those rulesets which are generated in a theory found in #1#*)
neuper@42407
   478
		val ancestors_rls = 
neuper@42407
   479
		  filter_out ((curry ((op mem) o swap) dropthys') o ((#1 o #2): rls'*(theory' * rls)->theory'))
neuper@42407
   480
		    (rev (!ruleset'))
neuper@42407
   481
  in case assoc (ancestors_rls, rls') of
neuper@42407
   482
	     SOME (thy', _) => ("IsacKnowledge", thyID2theory' thy')
neuper@42407
   483
	   | _ => error ("thy_containing_rls : rls '" ^ rls' ^
neuper@42407
   484
	     "' not in !rulset' of ancestors of thy '" ^ thy' ^ "'")
neuper@42407
   485
  end;
neuper@42407
   486
neuper@37906
   487
fun thy_containing_cal (thy':theory') termop =
neuper@42407
   488
  let
neuper@42407
   489
    val thy' = thyID2theory' thy'
neuper@42407
   490
    val dropthys =
neuper@42407
   491
      takewhile [] (not o (curry op= thy') o (#1:theory' * theory -> theory'))
neuper@42407
   492
        (rev (!theory'))
neuper@42407
   493
    val dropthys' = map (get_thy o (#1 : (theory' * theory) -> theory')) dropthys
neuper@42407
   494
    val ancestors_cal =
neuper@42407
   495
      filter_out ((curry ((op mem) o swap) dropthys') o (#1 : calc -> string))
neuper@42407
   496
        (rev (!calclist'))
neuper@42407
   497
  in case assoc (ancestors_cal, strip_thy termop) of
neuper@42407
   498
	     SOME (th_termop, _) => ("IsacKnowledge", strip_thy th_termop)
neuper@42407
   499
	   | _ => error ("thy_containing_cal : rls '" ^ termop ^
neuper@42407
   500
			 "' not in ! calclist' of ancestors of thy '" ^ thy' ^ "'")
neuper@42407
   501
  end
neuper@42407
   502
end; (*local*)
neuper@37906
   503
neuper@37906
   504
(*.packing return-values to matchTheory, contextToThy for xml-generation.*)
neuper@37906
   505
datatype contthy =  (*also an item from KEStore on Browser ......#*)
neuper@37906
   506
	 EContThy   (*not from KEStore ...........................*)
neuper@37906
   507
       | ContThm of (*a theorem in contex =============*)
neuper@37906
   508
	 {thyID   : thyID,         (*for *2guh in sub-elems here .*)
neuper@37906
   509
	  thm     : guh,           (*theorem in the context      .*)
neuper@37906
   510
	  applto  : term,	   (*applied to formula ...      .*)
neuper@37906
   511
	  applat  : term,	   (*...  with lhs inserted      .*)
neuper@37906
   512
	  reword  : rew_ord',      (*order used for rewrite      .*)
neuper@37906
   513
	  asms    : (term          (*asumption instantiated      .*)
neuper@37906
   514
		     * term) list, (*asumption evaluated         .*)
neuper@37906
   515
	  lhs     : term           (*lhs of the theorem ...      #*)
neuper@37906
   516
		    * term,        (*... instantiated            .*)
neuper@37906
   517
	  rhs     : term           (*rhs of the theorem ...      #*)
neuper@37906
   518
		    * term,        (*... instantiated            .*)
neuper@37906
   519
	  result  : term,	   (*resulting from the rewrite  .*)
neuper@37906
   520
	  resasms : term list,     (*... with asms stored        .*)
neuper@37906
   521
	  asmrls  : rls'           (*ruleset for evaluating asms .*)
neuper@37906
   522
		    }						 
neuper@37906
   523
	| ContThmInst of (*a theorem with bdvs in contex ======== *)
neuper@37906
   524
	 {thyID   : thyID,         (*for *2guh in sub-elems here .*)
neuper@37906
   525
	  thm     : guh,           (*theorem in the context      .*)
neuper@37906
   526
	  bdvs    : subst,         (*bound variables to modify....*)
neuper@37906
   527
	  thminst : term,          (*... theorem instantiated    .*)
neuper@37906
   528
	  applto  : term,	   (*applied to formula ...      .*)
neuper@37906
   529
	  applat  : term,	   (*...  with lhs inserted      .*)
neuper@37906
   530
	  reword  : rew_ord',      (*order used for rewrite      .*)
neuper@37906
   531
	  asms    : (term          (*asumption instantiated      .*)
neuper@37906
   532
		     * term) list, (*asumption evaluated         .*)
neuper@37906
   533
	  lhs     : term           (*lhs of the theorem ...      #*)
neuper@37906
   534
		    * term,        (*... instantiated            .*)
neuper@37906
   535
	  rhs     : term           (*rhs of the theorem ...      #*)
neuper@37906
   536
		    * term,        (*... instantiated            .*)
neuper@37906
   537
	  result  : term,	   (*resulting from the rewrite  .*)
neuper@37906
   538
	  resasms : term list,     (*... with asms stored        .*)
neuper@37906
   539
	  asmrls  : rls'           (*ruleset for evaluating asms .*)
neuper@37906
   540
		      }						 
neuper@37906
   541
	| ContRls of (*a rule set in contex ===================== *)
neuper@37906
   542
	 {thyID   : thyID,         (*for *2guh in sub-elems here .*)
neuper@37906
   543
	  rls     : guh,           (*rule set in the context     .*)
neuper@37906
   544
	  applto  : term,	   (*rewrite this formula        .*)
neuper@37906
   545
	  result  : term,	   (*resulting from the rewrite  .*)
neuper@37906
   546
	  asms    : term list      (*... with asms stored        .*)
neuper@37906
   547
		    }						 
neuper@37906
   548
	| ContRlsInst of (*a rule set with bdvs in contex ======= *)
neuper@37906
   549
	 {thyID   : thyID,         (*for *2guh in sub-elems here .*)
neuper@37906
   550
	  rls     : guh,           (*rule set in the context     .*)
neuper@37906
   551
	  bdvs    : subst,         (*for bound variables in thms .*)
neuper@37906
   552
	  applto  : term,	   (*rewrite this formula        .*)
neuper@37906
   553
	  result  : term,	   (*resulting from the rewrite  .*)
neuper@37906
   554
	  asms    : term list      (*... with asms stored        .*)
neuper@37906
   555
		    }
neuper@37906
   556
	| ContNOrew of (*no rewrite for thm or rls ============== *)
neuper@37906
   557
	 {thyID   : thyID,         (*for *2guh in sub-elems here .*)
neuper@37906
   558
	  thm_rls : guh,           (*thm or rls in the context   .*)
neuper@37906
   559
	  applto  : term	   (*rewrite this formula        .*)
neuper@37906
   560
		    }						 
neuper@37906
   561
	| ContNOrewInst of (*no rewrite for some instantiation == *)
neuper@37906
   562
	 {thyID   : thyID,         (*for *2guh in sub-elems here .*)
neuper@37906
   563
	  thm_rls : guh,           (*thm or rls in the context   .*)
neuper@37906
   564
	  bdvs    : subst,         (*for bound variables in thms .*)
neuper@37906
   565
	  thminst : term,          (*... theorem instantiated    .*)
neuper@37906
   566
	  applto  : term	   (*rewrite this formula        .*)
neuper@37906
   567
		    };
neuper@37906
   568
neuper@37906
   569
(*.check a rewrite-tac for bdv (RL always used *_Inst !) TODO.WN060718
neuper@37906
   570
   pass other tacs unchanged.*)
neuper@37906
   571
fun get_tac_checked pt ((p,p_) : pos') = get_obj g_tac pt p;
neuper@37906
   572
neuper@37906
   573
(*..*)
neuper@37906
   574
neuper@37906
   575
neuper@37906
   576
neuper@37906
   577
(*.get the formula f at ptp rewritten by the Rewrite_* already applied to f.*)
neuper@37906
   578
(* val (Rewrite' (thy', ord', erls, _, (thmID,_), f, (res,asm))) = tac';
neuper@37906
   579
   *)
neuper@42400
   580
fun context_thy (pt, pos as (p,p_)) (tac as Rewrite (thmID,_)) =
neuper@42400
   581
      (case applicable_in pos pt tac of
neuper@42400
   582
        Appl (Rewrite' (thy', ord', erls, _, (thmID,_), f, (res,asm))) =>
neuper@42400
   583
          let 
neuper@42400
   584
            val thmDeriv = "WN120320: get this from a reference variable ?!?"
neuper@42400
   585
            val thy = assoc_thy thy'
neuper@42400
   586
            val thm = (norm o #prop o rep_thm o (Global_Theory.get_thm thy)) thmID
neuper@42400
   587
          in
neuper@42400
   588
            ContThm
neuper@42400
   589
             {thyID   = theory'2thyID thy',
neuper@42400
   590
              thm     =
neuper@42400
   591
                thm2guh (thy_containing_thm thmDeriv) (thmID_of_derivation_name thmDeriv),
neuper@42400
   592
              applto  = f,
neuper@42400
   593
              applat  = e_term,
neuper@42400
   594
              reword  = ord',
neuper@42400
   595
              asms    = [](*asms ~~ asms'*),
neuper@42400
   596
              lhs     = (e_term, e_term)(*(lhs, lhs')*),
neuper@42400
   597
              rhs     = (e_term, e_term)(*(rhs, rhs')*),
neuper@42400
   598
              result  = res,
neuper@42400
   599
              resasms = asm,
neuper@42400
   600
              asmrls  = id_rls erls}
neuper@42400
   601
		       end
neuper@42400
   602
       | Notappl _ =>
neuper@42400
   603
           let
neuper@42400
   604
             val thmDeriv = "WN120320: get this from a reference variable ?!?"
neuper@42400
   605
             val pp = par_pblobj pt p
neuper@42400
   606
             val thy' = get_obj g_domID pt pp
neuper@42400
   607
             val f = case p_ of
neuper@42400
   608
                   Frm => get_obj g_form pt p
neuper@42400
   609
                 | Res => (fst o (get_obj g_result pt)) p
neuper@42400
   610
           in
neuper@42400
   611
             ContNOrew
neuper@42400
   612
              {thyID   = theory'2thyID thy',
neuper@42400
   613
               thm_rls = thm2guh (thy_containing_thm thmDeriv) (thmID_of_derivation_name thmDeriv),
neuper@42400
   614
               applto  = f}
neuper@42400
   615
		       end)
neuper@37906
   616
    
neuper@42400
   617
  | context_thy (pt, pos as (p,p_)) (tac as Rewrite_Inst (subs, (thmID,_))) =
neuper@42400
   618
	    (case applicable_in pos pt tac of
neuper@42400
   619
	       Appl (Rewrite_Inst' (thy', ord', erls, _, subst, (thmID,_), f, (res, asm))) =>
neuper@42400
   620
	         let
neuper@42400
   621
             val thmDeriv = "WN120320: get this from a reference variable ?!?"
neuper@42400
   622
	           val thm = (norm o #prop o rep_thm o (Global_Theory.get_thm (assoc_thy thy'))) thmID
neuper@42400
   623
	           val thminst = inst_bdv subst thm
neuper@42400
   624
	         in
neuper@42400
   625
	           ContThmInst
neuper@42400
   626
	            {thyID   = theory'2thyID thy',
neuper@42400
   627
	             thm     = thm2guh (thy_containing_thm thmDeriv) (thmID_of_derivation_name thmDeriv),
neuper@42400
   628
	             bdvs    = subst,
neuper@42400
   629
	             thminst = thminst,
neuper@42400
   630
	             applto  = f,
neuper@42400
   631
	             applat  = e_term,
neuper@42400
   632
	             reword  = ord',
neuper@42400
   633
	             asms    = [](*asms ~~ asms'*),
neuper@42400
   634
	             lhs     = (e_term, e_term)(*(lhs, lhs')*),
neuper@42400
   635
	             rhs     = (e_term, e_term)(*(rhs, rhs')*),
neuper@42400
   636
	             result  = res,
neuper@42400
   637
	             resasms = asm,
neuper@42400
   638
	             asmrls  = id_rls erls}
neuper@42400
   639
	         end
neuper@42400
   640
       | Notappl _ =>
neuper@42400
   641
           let
neuper@42400
   642
             val thmDeriv = "WN120320: get this from a reference variable ?!?"
neuper@42400
   643
             val pp = par_pblobj pt p
neuper@42400
   644
             val thy' = get_obj g_domID pt pp
neuper@42400
   645
             val subst = subs2subst (assoc_thy thy') subs
neuper@42400
   646
             val thm = (norm o #prop o rep_thm o (Global_Theory.get_thm (assoc_thy thy'))) thmID
neuper@42400
   647
             val thminst = inst_bdv subst thm
neuper@42400
   648
             val f = case p_ of
neuper@42400
   649
                 Frm => get_obj g_form pt p
neuper@42400
   650
               | Res => (fst o (get_obj g_result pt)) p
neuper@42400
   651
           in
neuper@42400
   652
             ContNOrewInst
neuper@42400
   653
              {thyID   = theory'2thyID thy',
neuper@42400
   654
               thm_rls =
neuper@42400
   655
                 "WN120320: thm2guh (thy_containing_thm thmDeriv) (thmID_of_derivation_name thmDeriv)",
neuper@42400
   656
               bdvs    = subst,
neuper@42400
   657
               thminst = thminst,
neuper@42400
   658
               applto  = f}
neuper@42400
   659
           end)
neuper@37906
   660
  | context_thy (pt,p) (tac as Rewrite_Set rls') =
neuper@42400
   661
      (case applicable_in p pt tac of
neuper@42400
   662
         Appl (Rewrite_Set' (thy', _, rls, f, (res,asm))) =>
neuper@42400
   663
           ContRls
neuper@42400
   664
            {thyID  = theory'2thyID thy',
neuper@42400
   665
             rls    = "WN120320:  rls2guh (thy_containing_rls thy' rls') rls'",
neuper@42400
   666
             applto = f,	  
neuper@42400
   667
             result = res,	  
neuper@42400
   668
             asms   = asm})
neuper@37906
   669
  | context_thy (pt,p) (tac as Rewrite_Set_Inst (subs, rls')) = 
neuper@42400
   670
      (case applicable_in p pt tac of
neuper@42400
   671
         Appl (Rewrite_Set_Inst' (thy', _, subst, rls, f, (res,asm))) =>
neuper@42400
   672
           ContRlsInst
neuper@42400
   673
            {thyID  = theory'2thyID thy',
neuper@42400
   674
             rls    = "WN120320: rls2guh (thy_containing_rls thy' rls') rls'",
neuper@42400
   675
             bdvs   = subst,
neuper@42400
   676
             applto = f,	  
neuper@42400
   677
             result = res,	  
neuper@42400
   678
             asms   = asm});
neuper@37906
   679
neuper@42372
   680
(* get all theorems in a rule set (recursivley containing rule sets) *)
neuper@37906
   681
fun thm_of_rule Erule = []
neuper@37906
   682
  | thm_of_rule (thm as Thm _) = [thm]
neuper@37906
   683
  | thm_of_rule (Calc _) = []
neuper@37906
   684
  | thm_of_rule (Cal1 _) = []
neuper@37906
   685
  | thm_of_rule (Rls_ rls) = thms_of_rls rls
neuper@37906
   686
and thms_of_rls Erls = []
neuper@37906
   687
  | thms_of_rls (Rls {rules,...}) = (flat o (map  thm_of_rule)) rules
neuper@37906
   688
  | thms_of_rls (Seq {rules,...}) = (flat o (map  thm_of_rule)) rules
neuper@37906
   689
  | thms_of_rls (Rrls _) = [];
neuper@37906
   690
(* val Hrls {thy_rls = (_, rls),...} =
neuper@37906
   691
       get_the ["IsacKnowledge", "Test", "Rulesets", "expand_binomtest"];
neuper@37906
   692
> thms_of_rls rls;
neuper@37906
   693
   *)
neuper@37906
   694
neuper@37906
   695
(*. check if a rule is contained in a rule-set (recursivley down in Rls_);
neuper@37906
   696
    this rule can even be a rule-set itself.*)
neuper@37906
   697
fun contains_rule r rls = 
neuper@37906
   698
    let fun find (r, Rls_ rls) = finds (get_rules rls)
neuper@37906
   699
	  | find r12 = eq_rule r12
neuper@37906
   700
	and finds [] = false
neuper@37906
   701
	  | finds (r1 :: rs) = if eq_rule (r, r1) then true else finds rs;
neuper@37906
   702
    in 
neuper@38015
   703
    (*tracing ("### contains_rule: r = "^rule2str r^", rls = "^rls2str rls);*)
neuper@37906
   704
    finds (get_rules rls) 
neuper@37906
   705
    end;
neuper@37906
   706
neuper@37906
   707
(*. try if a rewrite-rule is applicable to a given formula; 
neuper@37906
   708
    in case of rule-sets (recursivley) collect all _atomic_ rewrites .*) 
neuper@37906
   709
fun try_rew thy ((_, ro):rew_ord) erls (subst:subst) f (thm' as Thm(id, thm)) =
neuper@37906
   710
    if contains_bdv thm
neuper@37906
   711
    then case rewrite_inst_ thy ro erls false subst thm f of
neuper@37926
   712
	      SOME (f',_) =>[rule2tac subst thm']
neuper@37926
   713
	    | NONE => []
neuper@37906
   714
    else (case rewrite_ thy ro erls false thm f of
neuper@37926
   715
	SOME (f',_) => [rule2tac [] thm']
neuper@37926
   716
	    | NONE => [])
neuper@37906
   717
  | try_rew thy _ _ _ f (cal as Calc c) = 
neuper@37906
   718
    (case get_calculation_ thy c f of
neuper@37926
   719
	SOME (str, _) => [rule2tac [] cal]
neuper@37926
   720
      | NONE => [])
neuper@37906
   721
  | try_rew thy _ _ _ f (cal as Cal1 c) = 
neuper@37906
   722
    (case get_calculation_ thy c f of
neuper@37926
   723
	SOME (str, _) => [rule2tac [] cal]
neuper@37926
   724
      | NONE => [])
neuper@37906
   725
  | try_rew thy _ _ subst f (Rls_ rls) = filter_appl_rews thy subst f rls
neuper@37906
   726
and filter_appl_rews thy subst f (Rls {rew_ord = ro, erls, rules,...}) = 
neuper@37906
   727
    distinct (flat (map (try_rew thy ro erls subst f) rules))
neuper@37906
   728
  | filter_appl_rews thy subst f (Seq {rew_ord = ro, erls, rules,...}) = 
neuper@37906
   729
    distinct (flat (map (try_rew thy ro erls subst f) rules))
neuper@37906
   730
  | filter_appl_rews thy subst f (Rrls _) = [];
neuper@37906
   731
neuper@37906
   732
(*. decide if a tactic is applicable to a given formula; 
neuper@37906
   733
    in case of Rewrite_Set* go down to _atomic_ rewrite-tactics .*)
neuper@37906
   734
(* val 
neuper@37906
   735
   *)
neuper@37906
   736
fun atomic_appl_tacs thy _ _ f (Calculate scrID) =
neuper@37906
   737
    try_rew thy e_rew_ordX e_rls [] f (Calc (snd(assoc1 (!calclist', scrID))))
neuper@37906
   738
  | atomic_appl_tacs thy ro erls f (Rewrite (thm' as (thmID, _))) =
neuper@37906
   739
    try_rew thy (ro, assoc_rew_ord ro) erls [] f 
neuper@37906
   740
	    (Thm (thmID, assoc_thm' thy thm'))
neuper@37906
   741
  | atomic_appl_tacs thy ro erls f (Rewrite_Inst (subs, thm' as (thmID, _))) =
neuper@37906
   742
    try_rew thy (ro, assoc_rew_ord ro) erls (subs2subst thy subs) f 
neuper@37906
   743
	    (Thm (thmID, assoc_thm' thy thm'))
neuper@37906
   744
neuper@37906
   745
  | atomic_appl_tacs thy _ _ f (Rewrite_Set rls') =
neuper@37906
   746
    filter_appl_rews thy [] f (assoc_rls rls')
neuper@37906
   747
  | atomic_appl_tacs thy _ _ f (Rewrite_Set_Inst (subs, rls')) =
neuper@37906
   748
    filter_appl_rews thy (subs2subst thy subs) f (assoc_rls rls')
neuper@37906
   749
  | atomic_appl_tacs _ _ _ _ tac = 
neuper@38015
   750
    (tracing ("### atomic_appl_tacs: not impl. for tac = '"^ tac2str tac ^"'");
neuper@37906
   751
     []);
neuper@37906
   752
neuper@37906
   753
neuper@37906
   754
neuper@37906
   755
neuper@37906
   756
neuper@37906
   757
(*.not only for thydata, but also for thy's etc.*)
neuper@37906
   758
fun theID2guh (theID:theID) =
neuper@37906
   759
    case length theID of
neuper@38031
   760
	0 => error ("theID2guh: called with theID = "^strs2str' theID)
neuper@37906
   761
      | 1 => part2guh theID
neuper@37906
   762
      | 2 => thy2guh theID
neuper@37906
   763
      | 3 => thypart2guh theID
neuper@37906
   764
      | 4 => let val [isa, thyID, typ, elemID] = theID
neuper@37906
   765
	     in case typ of
neuper@37906
   766
		    "Theorems" => thm2guh (isa, thyID) elemID
neuper@37906
   767
		  | "Rulesets" => rls2guh (isa, thyID) elemID
neuper@37906
   768
		  | "Calculations" => cal2guh (isa, thyID) elemID
neuper@37906
   769
		  | "Orders" => ord2guh (isa, thyID) elemID
neuper@37906
   770
		  | "Theorems" => thy2guh [isa, thyID]
neuper@38031
   771
		  | str => error ("theID2guh: called with theID = "^
neuper@37906
   772
					strs2str' theID)
neuper@37906
   773
	     end
neuper@38031
   774
      | n => error ("theID2guh called with theID = "^strs2str' theID);
neuper@37906
   775
(*.filenames not only for thydata, but also for thy's etc.*)
neuper@37906
   776
fun theID2filename (theID:theID) = theID2guh theID ^ ".xml" : filename;
neuper@37906
   777
neuper@37906
   778
fun guh2theID (guh:guh) =
neuper@40836
   779
    let val guh' = Symbol.explode guh
neuper@37906
   780
	val part = implode (take_fromto 1 4 guh')
neuper@37906
   781
	val isa = implode (take_fromto 5 9 guh')
neuper@37935
   782
    in if not (member op = ["exp_", "thy_", "pbl_", "met_"] part)
neuper@38031
   783
       then error ("guh '"^guh^"' does not begin with \
neuper@37906
   784
				     \exp_ | thy_ | pbl_ | met_")
neuper@37906
   785
       else let val chap = case isa of
neuper@37906
   786
				"isab_" => "Isabelle"
neuper@37906
   787
			      | "scri_" => "IsacScripts"
neuper@37906
   788
			      | "isac_" => "IsacKnowledge"
neuper@37906
   789
			      | _ => 
neuper@38031
   790
				error ("guh2theID: '"^guh^
neuper@37906
   791
					     "' does not have isab_ | scri_ | \
neuper@37906
   792
					     \isac_ at position 5..9")
neuper@37906
   793
		val rest = takerest (9, guh') 
neuper@37906
   794
		val thyID = takewhile [] (not o (curry op= "-")) rest
neuper@37906
   795
		val rest' = dropuntil (curry op= "-") rest
neuper@37906
   796
	    in case implode rest' of
neuper@37906
   797
		   "-part" => [chap] : theID
neuper@37906
   798
		 | "" => [chap, implode thyID]
neuper@37906
   799
		 | "-Theorems" => [chap, implode thyID, "Theorems"]
neuper@37906
   800
		 | "-Rulesets" => [chap, implode thyID, "Rulesets"]
neuper@37906
   801
		 | "-Operations" => [chap, implode thyID, "Operations"]
neuper@37906
   802
		 | "-Orders" => [chap, implode thyID, "Orders"]
neuper@37906
   803
		 | _ => 
neuper@37906
   804
		   let val sect = implode (take_fromto 1 5 rest')
neuper@37906
   805
		       val sect' = 
neuper@37906
   806
			   case sect of
neuper@37906
   807
			       "-thm-" => "Theorems"
neuper@37906
   808
			     | "-rls-" => "Rulesets"
neuper@37906
   809
			     | "-cal-" => "Operations"
neuper@37906
   810
			     | "-ord-" => "Orders"
neuper@37906
   811
			     | str => 
neuper@38031
   812
			       error ("guh2theID: '"^guh^"' has '"^sect^
neuper@37906
   813
					    "' instead -thm- | -rls- | \
neuper@37906
   814
					    \-cal- | -ord-")
neuper@37906
   815
		   in [chap, implode thyID, sect', implode 
neuper@37906
   816
						       (takerest (5, rest'))]
neuper@37906
   817
		   end
neuper@37906
   818
	    end	
neuper@37906
   819
    end;
neuper@37906
   820
(*> guh2theID "thy_isac_Biegelinie-Theorems";
neuper@37906
   821
val it = ["IsacKnowledge", "Biegelinie", "Theorems"] : theID
neuper@37947
   822
> guh2theID "thy_scri_ListC-thm-zip_Nil";
neuper@37947
   823
val it = ["IsacScripts", "ListC", "Theorems", "zip_Nil"] : theID*)
neuper@37906
   824
neuper@37906
   825
fun guh2filename (guh : guh) = guh ^ ".xml" : filename;
neuper@37906
   826
neuper@37906
   827
neuper@37906
   828
(*..*)
neuper@37906
   829
fun guh2rewtac (guh:guh) ([] : subs) =
neuper@37906
   830
    let val [isa, thy, sect, xstr] = guh2theID guh
neuper@37906
   831
    in case sect of
neuper@37906
   832
	   "Theorems" => Rewrite (xstr, "")
neuper@37906
   833
	 | "Rulesets" => Rewrite_Set xstr
neuper@38031
   834
	 | str => error ("guh2rewtac: not impl. for '"^xstr^"'") 
neuper@37906
   835
    end
neuper@37906
   836
  | guh2rewtac (guh:guh) subs =
neuper@37906
   837
    let val [isa, thy, sect, xstr] = guh2theID guh
neuper@37906
   838
    in case sect of
neuper@37906
   839
	   "Theorems" => Rewrite_Inst (subs, (xstr, ""))
neuper@37906
   840
	 | "Rulesets" => Rewrite_Set_Inst (subs,  xstr)
neuper@38031
   841
	 | str => error ("guh2rewtac: not impl. for '"^xstr^"'") 
neuper@37906
   842
    end;
neuper@37906
   843
(*> guh2rewtac "thy_isac_Test-thm-constant_mult_square" [];
neuper@37906
   844
val it = Rewrite ("constant_mult_square", "") : tac
neuper@37906
   845
> guh2rewtac "thy_isac_Test-thm-risolate_bdv_add" ["(bdv, x)"];
neuper@37906
   846
val it = Rewrite_Inst (["(bdv, x)"], ("risolate_bdv_add", "")) : tac
neuper@37906
   847
> guh2rewtac "thy_isac_Test-rls-Test_simplify" [];
neuper@37906
   848
val it = Rewrite_Set "Test_simplify" : tac
neuper@37906
   849
> guh2rewtac "thy_isac_Test-rls-isolate_bdv" ["(bdv, x)"];
neuper@37906
   850
val it = Rewrite_Set_Inst (["(bdv, x)"], "isolate_bdv") : tac*)
neuper@37906
   851
neuper@37906
   852
neuper@37906
   853
(*.the front-end may request a context for any element of the hierarchy.*)
neuper@37906
   854
(* val guh = "thy_isac_Test-rls-Test_simplify";
neuper@37906
   855
   *)
neuper@37906
   856
fun no_thycontext (guh : guh) = (guh2theID guh; false)
neuper@37906
   857
    handle _ => true;
neuper@37906
   858
neuper@37906
   859
(*> has_thycontext  "thy_isac_Test";
neuper@37906
   860
if has_thycontext  "thy_isac_Test" then "OK" else "NOTOK";
neuper@37906
   861
 *)
neuper@37906
   862
neuper@37906
   863
neuper@37906
   864
neuper@37906
   865
(*.get the substitution of bound variables for matchTheory:
neuper@37906
   866
   # lookup the thm|rls' in the script
neuper@37906
   867
   # take the [(bdv, v_),..] from the respective Rewrite_(Set_)Inst
neuper@37906
   868
   # instantiate this subs with the istates env to [(bdv, x),..]
neuper@37906
   869
   # otherwise [].*)
neuper@37906
   870
(*WN060617 hack assuming that all scripts use only one bound variable
neuper@37906
   871
and use 'v_' as the formal argument for this bound variable*)
neuper@37906
   872
(* val (ScrState (env,_,_,_,_,_), _, guh) = (is, "dummy", guh);
neuper@37906
   873
   *)
neuper@37906
   874
fun subs_from (ScrState (env,_,_,_,_,_)) _(*:Script sc*) (guh:guh) =
neuper@37906
   875
    let val theID as [isa, thyID, sect, xstr] = guh2theID guh
neuper@37906
   876
    in case sect of
neuper@37906
   877
	   "Theorems" => 
neuper@41899
   878
	   let val thm = Global_Theory.get_thm (assoc_thy (thyID2theory' thyID)) xstr
neuper@37906
   879
	   in if contains_bdv thm
neuper@37906
   880
	      then let val formal_arg = str2term "v_"
neuper@37906
   881
		       val value = subst_atomic env formal_arg
neuper@37906
   882
		   in ["(bdv," ^ term2str value ^ ")"]:subs end
neuper@37906
   883
	      else []
neuper@37906
   884
	   end
neuper@37906
   885
	 | "Rulesets" => 
neuper@37906
   886
	   let val rules = (get_rules o assoc_rls) xstr
neuper@37906
   887
	   in if contain_bdv rules
neuper@37906
   888
	      then let val formal_arg = str2term"v_"
neuper@37906
   889
		       val value = subst_atomic env formal_arg
neuper@37906
   890
		   in ["(bdv,"^term2str value^")"]:subs end
neuper@37906
   891
	      else []
neuper@37906
   892
	   end
neuper@37906
   893
    end;
neuper@37906
   894
neuper@37906
   895
(* use"ME/rewtools.sml";
neuper@37906
   896
   *)
neuper@37906
   897