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