src/Tools/isac/Interpret/rewtools.sml
author Walther Neuper <wneuper@ist.tugraz.at>
Thu, 20 Oct 2016 10:26:29 +0200
changeset 59253 f0bb15a046ae
parent 59252 7d3dbc1171ff
child 59255 383722bfcff5
permissions -rw-r--r--
simplify handling of theorems

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