src/Tools/isac/Interpret/rewtools.sml
author Walther Neuper <walther.neuper@jku.at>
Wed, 01 Apr 2020 12:42:39 +0200
changeset 59846 7184a26ac7d5
parent 59844 373d13915f8c
child 59848 06a5cfe04223
permissions -rw-r--r--
renaming, cleanup
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
wneuper@59263
     6
signature REWRITE_TOOLS =
wneuper@59263
     7
sig
wneuper@59263
     8
  type deriv
wneuper@59416
     9
  val contains_rule : Rule.rule -> Rule.rls -> bool
wneuper@59571
    10
  val atomic_appl_tacs : theory -> string -> Rule.rls -> term -> Tactic.input -> Tactic.input list
wneuper@59416
    11
  val thy_containing_rls : Rule.theory' -> Rule.rls' -> string * Rule.theory'
wneuper@59416
    12
  val thy_containing_cal : Rule.theory' -> Rule.prog_calcID -> string * string
wneuper@59263
    13
  datatype contthy
wneuper@59416
    14
  = ContNOrew of {applto: term, thm_rls: Celem.guh, thyID: Rule.thyID}
wneuper@59416
    15
     | ContNOrewInst of {applto: term, bdvs: Rule.subst, thm_rls: Celem.guh, thminst: term, thyID: Rule.thyID}
wneuper@59416
    16
     | ContRls of {applto: term, asms: term list, result: term, rls: Celem.guh, thyID: Rule.thyID}
wneuper@59416
    17
     | ContRlsInst of {applto: term, asms: term list, bdvs: Rule.subst, result: term, rls: Celem.guh, thyID: Rule.thyID}
wneuper@59416
    18
     | ContThm of {applat: term, applto: term, asmrls: Rule.rls', asms: (term * term) list,
wneuper@59416
    19
       lhs: term * term, resasms: term list, result: term, reword: Rule.rew_ord', rhs: term * term,
wneuper@59416
    20
       thm: Celem.guh, thyID: Rule.thyID}
wneuper@59416
    21
     | ContThmInst of {applat: term, applto: term, asmrls: Rule.rls', asms: (term * term) list,
wneuper@59416
    22
       bdvs: Rule.subst, lhs: term * term, resasms: term list, result: term, reword: Rule.rew_ord',
wneuper@59416
    23
       rhs: term * term, thm: Celem.guh, thminst: term, thyID: Rule.thyID}
wneuper@59263
    24
     | EContThy
wneuper@59405
    25
  val guh2filename : Celem.guh -> Celem.filename
wneuper@59405
    26
  val is_sym : Celem.thmID -> bool
wneuper@59405
    27
  val sym_drop : Celem.thmID -> Celem.thmID
wneuper@59416
    28
  val sym_rls : Rule.rls -> Rule.rls
wneuper@59416
    29
  val sym_rule : Rule.rule -> Rule.rule
wneuper@59416
    30
  val thms_of_rls : Rule.rls -> Rule.rule list
wneuper@59405
    31
  val theID2filename : Celem.theID -> Celem.filename
wneuper@59405
    32
  val no_thycontext : Celem.guh -> bool
wneuper@59572
    33
  val subs_from : Istate.T -> 'a -> Celem.guh -> Selem.subs
wneuper@59571
    34
  val guh2rewtac : Celem.guh -> Selem.subs -> Tactic.input
walther@59846
    35
  val get_tac_checked : Ctree.ctree -> Pos.pos' -> Tactic.input
walther@59775
    36
  val context_thy : Calc.T -> Tactic.input -> contthy
wneuper@59416
    37
  val distinct_Thm : Rule.rule list -> Rule.rule list
wneuper@59416
    38
  val eq_Thms : string list -> Rule.rule -> bool
wneuper@59416
    39
  val make_deriv : theory -> Rule.rls -> Rule.rule list -> ((term * term) list -> term * term -> bool) ->
wneuper@59263
    40
    term option -> term -> deriv
wneuper@59416
    41
  val reverse_deriv : theory -> Rule.rls -> Rule.rule list -> ((term * term) list -> term * term -> bool) ->
wneuper@59416
    42
    term option -> term -> (Rule.rule * (term * term list)) list
wneuper@59416
    43
  val get_bdv_subst : term -> (term * term) list -> Selem.subs option * Rule.subst
wneuper@59313
    44
  val thy_containing_thm : string -> string * string
wneuper@59405
    45
  val guh2theID : Celem.guh -> Celem.theID
wneuper@59310
    46
(* ---- for tests only: shifted from below to remove the Warning "unused" at fun.def. --------- *)
wneuper@59310
    47
  (*  NONE *)
walther@59785
    48
(*/-------------------------------------------------------- ! aktivate for Test_Isac BEGIN ---\* )
wneuper@59416
    49
  val trtas2str : (term * Rule.rule * (term * term list)) list -> string
wneuper@59416
    50
  val eq_Thm : Rule.rule * Rule.rule -> bool
wneuper@59416
    51
  val deriv2str : (term * Rule.rule * (term * term list)) list -> string
wneuper@59409
    52
  val deriv_of_thm'' : Celem.thm'' -> string
walther@59785
    53
( *\--- ! aktivate for Test_Isac END ----------------------------------------------------------/*)
wneuper@59310
    54
wneuper@59310
    55
(*----- unused code, kept as hints to design ideas ---------------------------------------------*)
wneuper@59416
    56
  val deri2str : (Rule.rule * (term * term list)) list -> string
wneuper@59313
    57
  val sym_trm : term -> term
wneuper@59263
    58
end 
neuper@37906
    59
wneuper@59263
    60
structure Rtools(**): REWRITE_TOOLS(**) =
wneuper@59263
    61
struct
wneuper@59313
    62
wneuper@59263
    63
(*** reverse rewriting ***)
wneuper@59266
    64
(*. a '_deriv'ation is constructed during 'reverse rewring' by an Rrls       *
wneuper@59266
    65
 *  of for connecting a user-input formula with the current calc-state.	     *
wneuper@59266
    66
 *# It is somewhat incompatible with the rest of the math-engine:	     *
wneuper@59266
    67
 *  (1) it is not created by a script					     *
wneuper@59266
    68
 *  (2) thus there cannot be another user-input within a derivation	     *
wneuper@59266
    69
 *# It suffers particularily from the not-well-foundedness of the math-engine*
wneuper@59266
    70
 *  (1) FIXME other branchtyptes than Transitive will change 'embed_deriv'   *
wneuper@59266
    71
 *  (2) FIXME and eventually 'compare_step' (ie. the script interpreter)     *
wneuper@59266
    72
 *  (3) FIXME and eventually 'lev_back'                                      *
wneuper@59266
    73
 *# SOME improvements are evident FIXME.040215 '_deriv'ation:	             *
wneuper@59266
    74
 *  (1) FIXME nest Rls_ in 'make_deriv'					     *
wneuper@59266
    75
 *  (2) FIXME do the not-reversed part in 'make_deriv' by scripts -- thus    *
wneuper@59266
    76
 *	user-input will become possible in this part of a derivation	     *
wneuper@59266
    77
 *  (3) FIXME do (2) only if a derivation has been found -- for efficiency,  *
wneuper@59266
    78
 *	while a non-derivable inform requires to step until End_Proof'	     *
wneuper@59266
    79
 *  (4) FIXME find criteria on when _not_ to step until End_Proof'           *
wneuper@59266
    80
 *  (5) FIXME 
wneuper@59266
    81
.*)
wneuper@59263
    82
type deriv =      (* derivation for insertin one level of nodes into the calctree *) 
wneuper@59263
    83
  ( term *        (* where the rule is applied to                                 *)
wneuper@59416
    84
    Rule.rule *  (* rule to be applied                                           *)
wneuper@59263
    85
    ( term *      (* resulting from rule application                              *)
wneuper@59263
    86
      term list)) (* assumptions resulting from rule application                  *)
wneuper@59263
    87
  list            (*                                                              *) 
neuper@37906
    88
wneuper@59263
    89
fun trta2str (t, r, (t', a)) =
wneuper@59416
    90
  "\n(" ^ Rule.term2str t ^ ", " ^ Rule.rule2str' r ^ ", (" ^ Rule.term2str t' ^ ", " ^ Rule.terms2str a ^ "))"
wneuper@59263
    91
fun trtas2str trtas = (strs2str o (map trta2str)) trtas
wneuper@59263
    92
val deriv2str = trtas2str
wneuper@59263
    93
fun rta2str (r, (t, a)) =
wneuper@59416
    94
  "\n(" ^ Rule.rule2str' r ^ ", (" ^ Rule.term2str t ^ ", " ^ Rule.terms2str a ^ "))"
wneuper@59263
    95
fun rtas2str rtas = (strs2str o (map rta2str)) rtas
wneuper@59263
    96
val deri2str = rtas2str
neuper@37906
    97
wneuper@59263
    98
(* A1==>...==> An ==> (Lhs = Rhs) goes to A1 ==>...==> An ==> (Rhs = Lhs) 
neuper@42399
    99
  WN120320: reconsider the desing including the java front-end with html representation;
neuper@42399
   100
  see tests 
walther@59745
   101
  --- old compute rlsthmsNOTisac by eq_thmID ---
neuper@42399
   102
  --- compute val rlsthmsNOTisac ---
neuper@42399
   103
*)
neuper@37906
   104
fun sym_thm thm =
wneuper@59263
   105
  let 
wneuper@59263
   106
    val (deriv,
wneuper@59338
   107
      {cert = cert, tags = tags, maxidx = maxidx, shyps = shyps, hyps = hyps, tpairs = tpairs, 
wneuper@59263
   108
      prop = prop}) = Thm.rep_thm_G thm
wneuper@59390
   109
    val (lhs, rhs) = (TermC.dest_equals o TermC.strip_trueprop o Logic.strip_imp_concl) prop
wneuper@59389
   110
    val prop' = case TermC.strip_imp_prems' prop of
wneuper@59390
   111
        NONE => HOLogic.Trueprop $ (TermC.mk_equality (rhs, lhs))
wneuper@59390
   112
      | SOME cs => TermC.ins_concl cs (HOLogic.Trueprop $ (TermC.mk_equality (rhs, lhs)))
wneuper@59338
   113
  in Thm.assbl_thm deriv cert tags maxidx shyps hyps tpairs prop' end
neuper@37906
   114
wneuper@59263
   115
(* WN100910 weaker than fun sym_thm for Theory.axioms_of in isa02 *)
neuper@38002
   116
fun sym_trm trm =
wneuper@59263
   117
  let
wneuper@59390
   118
    val (lhs, rhs) = (TermC.dest_equals o TermC.strip_trueprop o Logic.strip_imp_concl) trm
wneuper@59389
   119
    val trm' = case TermC.strip_imp_prems' trm of
wneuper@59389
   120
	      NONE => TermC.mk_equality (rhs, lhs)
wneuper@59389
   121
	    | SOME cs => TermC.ins_concl cs (TermC.mk_equality (rhs, lhs))
wneuper@59263
   122
  in trm' end
neuper@37906
   123
neuper@55487
   124
(* derive normalform of a rls, or derive until SOME goal,
neuper@37906
   125
   and record rules applied and rewrites.
neuper@37906
   126
val it = fn
neuper@37906
   127
  : theory
neuper@37906
   128
    -> rls
neuper@37906
   129
    -> rule list
neuper@37906
   130
    -> rew_ord       : the order of this rls, which 1 theorem of is used 
neuper@37906
   131
                       for rewriting 1 single step (?14.4.03)
neuper@55487
   132
    -> term option   : 040214 ??? use for "derive until SOME goal" ??? 
neuper@37906
   133
    -> term 
neuper@37906
   134
    -> (term *       : to this term ...
neuper@37906
   135
        rule * 	     : ... this rule is applied yielding ...
neuper@37906
   136
        (term *      : ... this term ...
neuper@37906
   137
         term list)) : ... under these assumptions.
neuper@37906
   138
       list          :
neuper@37906
   139
returns empty list for a normal form
neuper@37906
   140
FIXME.WN040214: treats rules as in Rls, _not_ as in Seq
neuper@37906
   141
neuper@37906
   142
WN060825 too complicated for the intended use by cancel_, common_nominator_
neuper@55487
   143
and unreflectedly adapted to extension of rules by Rls_: returns Rls_("sym_simpl..
neuper@55487
   144
 -- replaced below *)
wneuper@59405
   145
fun make_deriv thy erls rs ro goal tt = 
neuper@55487
   146
  let 
wneuper@59263
   147
    datatype switch = Appl | Noap (* unify with version in rewrite.sml *)
wneuper@59263
   148
    fun rew_once _ rts t Noap [] = 
wneuper@59416
   149
        (case goal of NONE => rts | SOME _ => error ("make_deriv: no derivation for " ^ Rule.term2str t))
neuper@55487
   150
      | rew_once lim rts t Appl [] = rew_once lim rts t Noap rs
neuper@55487
   151
        (*| Seq _ => rts) FIXXXXXME 14.3.03*)
neuper@55487
   152
      | rew_once lim rts t apno rs' =
neuper@55487
   153
        (case goal of 
neuper@55487
   154
          NONE => rew_or_calc lim rts t apno rs'
neuper@55487
   155
        | SOME g => if g = t then rts else rew_or_calc lim rts t apno rs')
neuper@55487
   156
    and rew_or_calc lim rts t apno (rrs' as (r :: rs')) =
neuper@55487
   157
      if lim < 0 
wneuper@59405
   158
      then (tracing ("make_deriv exceeds " ^ int2str (! Celem.lim_deriv) ^ "with deriv =\n");
wneuper@59263
   159
        tracing (deriv2str rts); rts)
neuper@55487
   160
      else
wneuper@59263
   161
        (case r of
wneuper@59416
   162
          Rule.Thm (thmid, tm) => 
wneuper@59405
   163
            (if not (! Celem.trace_rewrite) then () else tracing ("### trying thm \"" ^ thmid ^ "\"");
wneuper@59380
   164
            case Rewrite.rewrite_ thy ro erls true tm t of
neuper@55487
   165
              NONE => rew_once lim rts t apno rs'
neuper@55487
   166
            | SOME (t', a') =>
walther@59844
   167
              (if ! Celem.trace_rewrite then tracing ("=== rewrites to: " ^ Rule.term2str t') else ();
neuper@55487
   168
              rew_once (lim - 1) (rts @ [(t, r, (t', a'))]) t' Appl rrs'))
walther@59773
   169
        | Rule.Num_Calc (c as (op_, _)) => 
neuper@55487
   170
          let 
wneuper@59405
   171
            val _ = if not (! Celem.trace_rewrite) then () else tracing ("### trying calc. \"" ^ op_^"\"")
wneuper@59389
   172
            val t = TermC.uminus_to_string t
neuper@55487
   173
          in 
walther@59773
   174
            case Num_Calc.adhoc_thm thy c t of
neuper@55487
   175
              NONE => rew_once lim rts t apno rs'
neuper@55487
   176
            | SOME (thmid, tm) => 
neuper@55487
   177
              (let
wneuper@59380
   178
                val (t', a') = case Rewrite.rewrite_ thy ro erls true tm t of
wneuper@59263
   179
                  SOME ta => ta
wneuper@59263
   180
                | NONE => error "adhoc_thm: NONE"
walther@59844
   181
                val _ = if not (! Celem.trace_rewrite) then () else tracing("=== calc. to: " ^ Rule.term2str t')
wneuper@59416
   182
                val r' = Rule.Thm (thmid, tm)
neuper@55487
   183
              in rew_once (lim - 1) (rts @ [(t, r', (t', a'))]) t' Appl rrs' end) 
walther@59773
   184
                handle _ => error "derive_norm, Num_Calc: no rewrite"
neuper@55487
   185
          end
neuper@55487
   186
      (*| Cal1 (cc as (op_,_)) => ... WN080222 see rewrite__set_: see 7df94616c1bd and earlier*)
wneuper@59416
   187
        | Rule.Rls_ rls =>           (* WN060829: CREATES "sym_rlsID", see 7df94616c1bd and earlier*)
wneuper@59380
   188
          (case Rewrite.rewrite_set_ thy true rls t of
wneuper@59263
   189
            NONE => rew_once lim rts t apno rs'
wneuper@59263
   190
          | SOME (t', a') => rew_once (lim - 1) (rts @ [(t, r, (t', a'))]) t' Appl rrs')
wneuper@59416
   191
        | rule => error ("rew_once: uncovered case " ^ Rule.rule2str rule))
wneuper@59263
   192
    | rew_or_calc _ _ _ _ [] = error "rew_or_calc: called with []"
wneuper@59405
   193
  in rew_once (! Celem.lim_deriv) [] tt Noap rs end
neuper@37906
   194
wneuper@59405
   195
fun sym_drop thmID =
wneuper@59263
   196
  case Symbol.explode thmID of
wneuper@59405
   197
	  "s" :: "y" :: "m" :: "_" :: id => implode id
wneuper@59263
   198
  | _ => thmID
wneuper@59405
   199
fun is_sym thmID =
wneuper@59263
   200
  case Symbol.explode thmID of
wneuper@59263
   201
	  "s" :: "y" :: "m" :: "_" :: _ => true
wneuper@59263
   202
  | _ => false;
neuper@37906
   203
neuper@37906
   204
(*FIXXXXME.040219: detail has to handle Rls id="sym_..." 
neuper@37906
   205
  by applying make_deriv, rev_deriv'; see concat_deriv*)
wneuper@59416
   206
fun sym_rls Rule.Erls = Rule.Erls
wneuper@59416
   207
  | sym_rls (Rule.Rls {id, scr, calc, errpatts, erls, srls, rules, rew_ord, preconds}) =
wneuper@59416
   208
    Rule.Rls {id = "sym_" ^ id, scr = scr, calc = calc, errpatts = errpatts, erls = erls, srls = srls, 
wneuper@59263
   209
      rules = rules, rew_ord = rew_ord, preconds = preconds}
wneuper@59416
   210
  | sym_rls (Rule.Seq {id, scr, calc, errpatts, erls, srls, rules, rew_ord, preconds}) =
wneuper@59416
   211
    Rule.Seq {id = "sym_" ^ id, scr = scr, calc = calc, errpatts = errpatts, erls = erls, srls = srls, 
wneuper@59263
   212
      rules = rules, rew_ord = rew_ord, preconds = preconds}
wneuper@59416
   213
  | sym_rls (Rule.Rrls {id, scr, calc, errpatts, erls, prepat, rew_ord}) = 
wneuper@59416
   214
    Rule.Rrls {id = "sym_" ^ id, scr = scr, calc = calc,  errpatts = errpatts, erls = erls,
wneuper@59263
   215
      prepat = prepat, rew_ord = rew_ord}
neuper@37906
   216
neuper@55485
   217
(* toggles sym_* and keeps "#:" for ad-hoc calculations *)
wneuper@59416
   218
fun sym_rule (Rule.Thm (thmID, thm)) =
wneuper@59263
   219
  let
wneuper@59263
   220
    val thm' = sym_thm thm
wneuper@59263
   221
    val thmID' = case Symbol.explode thmID of
wneuper@59263
   222
      "s" :: "y" :: "m" :: "_" :: id => implode id
wneuper@59416
   223
    | "#" :: ":" :: _ => "#: " ^ Rule.string_of_thmI thm'
wneuper@59263
   224
    | _ => "sym_" ^ thmID
wneuper@59416
   225
  in Rule.Thm (thmID', thm') end
wneuper@59416
   226
| sym_rule (Rule.Rls_ rls) = Rule.Rls_ (sym_rls rls) (* TODO? handle with interSteps ? *)
wneuper@59416
   227
| sym_rule r = error ("sym_rule: not for " ^  Rule.rule2str r)
neuper@37906
   228
neuper@37906
   229
(*version for reverse rewrite used before 040214*)
wneuper@59263
   230
fun rev_deriv (t, r, (_, a)) = (sym_rule r, (t, a));
wneuper@59405
   231
fun reverse_deriv thy erls rs ro goal t =
wneuper@59405
   232
    (rev o (map rev_deriv)) (make_deriv thy erls rs ro goal t)
neuper@37906
   233
wneuper@59416
   234
fun eq_Thm (Rule.Thm (id1, _), Rule.Thm (id2,_)) = id1 = id2
wneuper@59416
   235
  | eq_Thm (Rule.Thm (_, _), _) = false
wneuper@59416
   236
  | eq_Thm (Rule.Rls_ r1, Rule.Rls_ r2) = Rule.id_rls r1 = Rule.id_rls r2
wneuper@59416
   237
  | eq_Thm (Rule.Rls_ _, _) = false
wneuper@59416
   238
  | eq_Thm (r1, r2) = error ("eq_Thm: called with '" ^ Rule.rule2str r1 ^ "' '" ^ Rule.rule2str r2 ^ "'")
wneuper@59263
   239
fun distinct_Thm r = gen_distinct eq_Thm r
neuper@37906
   240
wneuper@59405
   241
fun eq_Thms thmIDs thm = (member op = thmIDs (Celem.id_of_thm thm))
wneuper@59263
   242
  handle ERROR _ => false
neuper@37906
   243
neuper@42400
   244
fun thy_containing_thm thmDeriv =
neuper@42400
   245
  let
wneuper@59405
   246
    val isabthys' = map Context.theory_name (Celem.isabthys ());
neuper@42400
   247
  in
wneuper@59405
   248
    if member op= isabthys' (Celem.thyID_of_derivation_name thmDeriv)
wneuper@59405
   249
    then ("Isabelle", Celem.thyID_of_derivation_name thmDeriv)
wneuper@59405
   250
    else ("IsacKnowledge", Celem.thyID_of_derivation_name thmDeriv)
wneuper@59263
   251
  end
neuper@37906
   252
neuper@55457
   253
(* which theory in ancestors of thy' contains a ruleset *)
wneuper@59405
   254
fun thy_containing_rls thy' rls' =
neuper@55457
   255
  let
wneuper@59416
   256
    val thy = Rule.Thy_Info_get_theory thy'
neuper@55457
   257
  in
neuper@55457
   258
    case AList.lookup op= (KEStore_Elems.get_rlss thy) rls' of
wneuper@59405
   259
      SOME (thy'', _) => (Celem.partID' thy'', thy'')
neuper@55457
   260
    | _ => error ("thy_containing_rls : rls '" ^ rls' ^ "' not in ancestors of thy '" ^ thy' ^ "'")
neuper@55457
   261
  end
neuper@37906
   262
neuper@55457
   263
(* this function cannot work as long as the datastructure does not contain thy' *)
wneuper@59405
   264
fun thy_containing_cal thy' sop =
neuper@42407
   265
  let
wneuper@59416
   266
    val thy = Rule.Thy_Info_get_theory thy'
neuper@55457
   267
  in
neuper@55457
   268
    case AList.lookup op= (KEStore_Elems.get_calcs thy) sop of
walther@59603
   269
      SOME (_(*"Groups.plus_class.plus"*), _) => ("IsacKnowledge", "Base_Tools")
neuper@55457
   270
    | _ => error ("thy_containing_cal : rls '" ^ sop ^ "' not in ancestors of thy '" ^ thy' ^ "'")
neuper@42407
   271
  end
neuper@37906
   272
wneuper@59263
   273
(* packing return-values to matchTheory, contextToThy for xml-generation *)
wneuper@59263
   274
datatype contthy =  (*also an item from KEStore on Browser .....#*)
wneuper@59263
   275
	EContThy   (* not from KEStore ..............................*)
wneuper@59263
   276
  | ContThm of (* a theorem in contex ===========================*)
wneuper@59416
   277
    {thyID   : Rule.thyID,     (* for *2guh in sub-elems here        .*)
wneuper@59405
   278
     thm     : Celem.guh,       (* theorem in the context             .*)
wneuper@59405
   279
     applto  : term,	          (* applied to formula ...             .*)
wneuper@59405
   280
     applat  : term,	          (* ...  with lhs inserted             .*)
wneuper@59416
   281
     reword  : Rule.rew_ord',  (* order used for rewrite             .*)
wneuper@59405
   282
     asms    : (term            (* asumption instantiated             .*)
wneuper@59405
   283
   	   * term) list,            (* asumption evaluated                .*)
wneuper@59405
   284
     lhs     : term             (* lhs of the theorem ...             #*)
wneuper@59405
   285
   	   * term,                  (* ... instantiated                   .*)
wneuper@59405
   286
     rhs     : term             (* rhs of the theorem ...             #*)
wneuper@59405
   287
   	   * term,                  (* ... instantiated                   .*)
wneuper@59405
   288
     result  : term,	          (* resulting from the rewrite         .*)
wneuper@59405
   289
     resasms : term list,       (* ... with asms stored               .*)
wneuper@59416
   290
     asmrls  : Rule.rls'       (* ruleset for evaluating asms        .*)
wneuper@59263
   291
   	}						 
wneuper@59263
   292
  | ContThmInst of (* a theorem with bdvs in contex ============ *)
wneuper@59416
   293
    {thyID   : Rule.thyID,     (*for *2guh in sub-elems here         .*)
wneuper@59405
   294
     thm     : Celem.guh,       (*theorem in the context              .*)
wneuper@59416
   295
     bdvs    : Rule.subst,     (*bound variables to modify...        .*)
wneuper@59405
   296
     thminst : term,            (*... theorem instantiated            .*)
wneuper@59405
   297
     applto  : term,	          (*applied to formula ...              .*)
wneuper@59405
   298
     applat  : term,	          (*...  with lhs inserted              .*)
wneuper@59416
   299
     reword  : Rule.rew_ord',  (*order used for rewrite              .*)
wneuper@59405
   300
     asms    : (term            (*asumption instantiated              .*)
wneuper@59405
   301
   	   * term) list,            (*asumption evaluated                 .*)
wneuper@59405
   302
     lhs     : term             (*lhs of the theorem ...              #*)
wneuper@59405
   303
   	   * term,                  (*... instantiated                    .*)
wneuper@59405
   304
     rhs     : term             (*rhs of the theorem ...              #*)
wneuper@59405
   305
   	   * term,                  (*... instantiated                    .*)
wneuper@59405
   306
     result  : term,	          (*resulting from the rewrite          .*)
wneuper@59405
   307
     resasms : term list,       (*... with asms stored                .*)
wneuper@59416
   308
     asmrls  : Rule.rls'       (*ruleset for evaluating asms         .*)
wneuper@59263
   309
    }						 
wneuper@59263
   310
  | ContRls of (* a rule set in contex ========================= *)
wneuper@59416
   311
    {thyID   : Rule.thyID,     (*for *2guh in sub-elems here         .*)
wneuper@59405
   312
     rls     : Celem.guh,       (*rule set in the context             .*)
wneuper@59405
   313
     applto  : term,	          (*rewrite this formula                .*)
wneuper@59405
   314
     result  : term,	          (*resulting from the rewrite          .*)
wneuper@59405
   315
     asms    : term list        (*... with asms stored                .*)
wneuper@59263
   316
   	}						 
wneuper@59263
   317
  | ContRlsInst of (* a rule set with bdvs in contex =========== *)
wneuper@59416
   318
	{thyID   : Rule.thyID,       (*for *2guh in sub-elems here         .*)
wneuper@59405
   319
	 rls     : Celem.guh,         (*rule set in the context             .*)
wneuper@59416
   320
	 bdvs    : Rule.subst,       (*for bound variables in thms         .*)
wneuper@59405
   321
	 applto  : term,	            (*rewrite this formula                .*)
wneuper@59405
   322
	 result  : term,	            (*resulting from the rewrite          .*)
wneuper@59405
   323
	 asms    : term list          (*... with asms stored                .*)
wneuper@59263
   324
   	}						 
wneuper@59263
   325
  | ContNOrew of (* no rewrite for thm or rls ================== *)
wneuper@59416
   326
    {thyID   : Rule.thyID,     (*for *2guh in sub-elems here         .*)
wneuper@59405
   327
     thm_rls : Celem.guh,       (*thm or rls in the context           .*)
wneuper@59405
   328
     applto  : term	            (*rewrite this formula                .*)
wneuper@59263
   329
   	}						 
wneuper@59263
   330
  | ContNOrewInst of (* no rewrite for some instantiation ====== *)
wneuper@59416
   331
    {thyID   : Rule.thyID,     (*for *2guh in sub-elems here         .*)
wneuper@59405
   332
     thm_rls : Celem.guh,       (*thm or rls in the context           .*)
wneuper@59416
   333
     bdvs    : Rule.subst,     (*for bound variables in thms         .*)
wneuper@59405
   334
     thminst : term,            (*... theorem instantiated            .*)
wneuper@59405
   335
     applto  : term	            (*rewrite this formula               .*)
wneuper@59263
   336
   	}						 
neuper@37906
   337
neuper@37906
   338
(*.check a rewrite-tac for bdv (RL always used *_Inst !) TODO.WN060718
neuper@37906
   339
   pass other tacs unchanged.*)
walther@59846
   340
fun get_tac_checked pt ((p, _) : Pos.pos') = Ctree.get_obj Ctree.g_tac pt p;
neuper@37906
   341
wneuper@59252
   342
(* create a derivation-name, eg. ("refl", _) --> "HOL.refl"*)
wneuper@59405
   343
fun deriv_of_thm'' (thmID, _) =
wneuper@59416
   344
  thmID |> Global_Theory.get_thm (Rule.Isac ()) |> Thm.get_name_hint
neuper@37906
   345
wneuper@59252
   346
(* get the formula f at ptp rewritten by the Rewrite_* already applied to f *)
wneuper@59571
   347
fun context_thy (pt, pos as (p,p_)) (tac as Tactic.Rewrite thm'') =
wneuper@59263
   348
    let val thm_deriv = deriv_of_thm'' thm''
wneuper@59263
   349
    in
wneuper@59272
   350
    (case Applicable.applicable_in pos pt tac of
walther@59735
   351
      Applicable.Appl (Tactic.Rewrite' (thy', ord', erls, _, _, f, (res,asm))) =>
wneuper@59263
   352
        ContThm
wneuper@59416
   353
         {thyID = Rule.theory'2thyID thy',
wneuper@59405
   354
          thm = Celem.thm2guh (thy_containing_thm thm_deriv) (Celem.thmID_of_derivation_name thm_deriv),
wneuper@59416
   355
          applto = f, applat  = Rule.e_term, reword  = ord',
wneuper@59416
   356
          asms = [](*asms ~~ asms'*), lhs = (Rule.e_term, Rule.e_term)(*(lhs, lhs')*), rhs = (Rule.e_term, Rule.e_term)(*(rhs, rhs')*),
wneuper@59416
   357
          result = res, resasms = asm, asmrls  = Rule.id_rls erls}
walther@59735
   358
     | Applicable.Notappl _ =>
wneuper@59263
   359
         let
wneuper@59276
   360
           val pp = Ctree.par_pblobj pt p
wneuper@59276
   361
           val thy' = Ctree.get_obj Ctree.g_domID pt pp
wneuper@59263
   362
           val f = case p_ of
walther@59694
   363
             Pos.Frm => Ctree.get_obj Ctree.g_form pt p
walther@59694
   364
           | Pos.Res => (fst o (Ctree.get_obj Ctree.g_result pt)) p 
wneuper@59313
   365
           | _ => error "context_thy: uncovered position"
wneuper@59263
   366
         in
wneuper@59263
   367
           ContNOrew
wneuper@59416
   368
            {thyID   = Rule.theory'2thyID thy',
wneuper@59263
   369
             thm_rls = 
wneuper@59405
   370
               Celem.thm2guh (thy_containing_thm thm_deriv) (Celem.thmID_of_derivation_name thm_deriv),
wneuper@59263
   371
             applto  = f}
wneuper@59263
   372
		     end
wneuper@59263
   373
     | _ => error "context_thy..Rewrite: uncovered case 2")
wneuper@59263
   374
    end
wneuper@59571
   375
  | context_thy (pt, pos as (p, p_)) (tac as Tactic.Rewrite_Inst (subs, (thmID, _))) =
wneuper@59416
   376
    let val thm = Global_Theory.get_thm (Rule.Isac ()) thmID
wneuper@59263
   377
    in
wneuper@59272
   378
	  (case Applicable.applicable_in pos pt tac of
walther@59735
   379
	     Applicable.Appl (Tactic.Rewrite_Inst' (thy', ord', erls, _, subst, _, f, (res, asm))) =>
wneuper@59263
   380
	       let
wneuper@59263
   381
           val thm_deriv = Thm.get_name_hint thm
walther@59773
   382
           val thminst = TermC.inst_bdv subst ((Num_Calc.norm o #prop o Thm.rep_thm) thm)
wneuper@59263
   383
	       in
wneuper@59263
   384
	         ContThmInst
wneuper@59416
   385
	          {thyID = Rule.theory'2thyID thy',
wneuper@59263
   386
	           thm = 
wneuper@59405
   387
	             Celem.thm2guh (thy_containing_thm thm_deriv) (Celem.thmID_of_derivation_name thm_deriv),
wneuper@59416
   388
	           bdvs = subst, thminst = thminst, applto  = f, applat  = Rule.e_term, reword  = ord',
wneuper@59416
   389
	           asms = [](*asms ~~ asms'*), lhs = (Rule.e_term, Rule.e_term)(*(lhs, lhs')*), rhs = (Rule.e_term, Rule.e_term)(*(rhs, rhs')*),
wneuper@59416
   390
	           result = res, resasms = asm, asmrls  = Rule.id_rls erls}
wneuper@59263
   391
	       end
walther@59735
   392
     | Applicable.Notappl _ =>
wneuper@59263
   393
         let
wneuper@59416
   394
           val thm = Global_Theory.get_thm (Rule.Isac ()(*WN141021 assoc_thy thy' ERROR*)) thmID
wneuper@59263
   395
           val thm_deriv = Thm.get_name_hint thm
wneuper@59276
   396
           val pp = Ctree.par_pblobj pt p
wneuper@59276
   397
           val thy' = Ctree.get_obj Ctree.g_domID pt pp
wneuper@59405
   398
           val subst = Selem.subs2subst (Celem.assoc_thy thy') subs
walther@59773
   399
           val thminst = TermC.inst_bdv subst ((Num_Calc.norm o #prop o Thm.rep_thm) thm)
wneuper@59263
   400
           val f = case p_ of
walther@59694
   401
               Pos.Frm => Ctree.get_obj Ctree.g_form pt p
walther@59694
   402
             | Pos.Res => (fst o (Ctree.get_obj Ctree.g_result pt)) p 
wneuper@59313
   403
             | _ => error "context_thy: uncovered case 3"
wneuper@59263
   404
         in
wneuper@59263
   405
           ContNOrewInst
wneuper@59416
   406
            {thyID = Rule.theory'2thyID thy',
wneuper@59405
   407
             thm_rls = Celem.thm2guh (thy_containing_thm thm_deriv) (Celem.thmID_of_derivation_name thm_deriv),
wneuper@59263
   408
             bdvs = subst, thminst = thminst, applto = f}
wneuper@59263
   409
         end
wneuper@59263
   410
      | _ => error "context_thy..Rewrite_Inst: uncovered case 4")
wneuper@59263
   411
    end
wneuper@59571
   412
  | context_thy (pt, p) (tac as Tactic.Rewrite_Set rls') =
wneuper@59272
   413
    (case Applicable.applicable_in p pt tac of
walther@59735
   414
       Applicable.Appl (Tactic.Rewrite_Set' (thy', _, _(*rls*), f, (res,asm))) =>
wneuper@59263
   415
         ContRls
wneuper@59416
   416
          {thyID = Rule.theory'2thyID thy',
wneuper@59405
   417
           rls = Celem.rls2guh (thy_containing_rls thy' rls') rls',
wneuper@59263
   418
           applto = f, result = res, asms = asm}
walther@59735
   419
     | _ => error ("context_thy Rewrite_Set: not for Applicable.Notappl"))
wneuper@59571
   420
  | context_thy (pt,p) (tac as Tactic.Rewrite_Set_Inst (_(*subs*), rls')) = 
wneuper@59272
   421
    (case Applicable.applicable_in p pt tac of
walther@59735
   422
       Applicable.Appl (Tactic.Rewrite_Set_Inst' (thy', _, subst, _, f, (res,asm))) =>
wneuper@59263
   423
         ContRlsInst
wneuper@59416
   424
          {thyID = Rule.theory'2thyID thy',
wneuper@59405
   425
           rls = Celem.rls2guh (thy_containing_rls thy' rls') rls',
wneuper@59263
   426
           bdvs = subst, applto = f, result = res, asms = asm}
walther@59735
   427
     | _ => error ("context_thy Rewrite_Set_Inst: not for Applicable.Notappl"))
wneuper@59263
   428
  | context_thy (_, p) tac =
walther@59846
   429
      error ("context_thy: not for tac " ^ Tactic.input_to_string tac ^ " at " ^ Pos.pos'2str p)
neuper@37906
   430
neuper@42372
   431
(* get all theorems in a rule set (recursivley containing rule sets) *)
wneuper@59416
   432
fun thm_of_rule Rule.Erule = []
wneuper@59416
   433
  | thm_of_rule (thm as Rule.Thm _) = [thm]
walther@59773
   434
  | thm_of_rule (Rule.Num_Calc _) = []
wneuper@59416
   435
  | thm_of_rule (Rule.Cal1 _) = []
wneuper@59416
   436
  | thm_of_rule (Rule.Rls_ rls) = thms_of_rls rls
wneuper@59416
   437
and thms_of_rls Rule.Erls = []
wneuper@59416
   438
  | thms_of_rls (Rule.Rls {rules,...}) = (flat o (map  thm_of_rule)) rules
wneuper@59416
   439
  | thms_of_rls (Rule.Seq {rules,...}) = (flat o (map  thm_of_rule)) rules
wneuper@59416
   440
  | thms_of_rls (Rule.Rrls _) = []
neuper@37906
   441
wneuper@59263
   442
(* check if a rule is contained in a rule-set (recursivley down in Rls_);
wneuper@59263
   443
   this rule can even be a rule-set itself                             *)
neuper@37906
   444
fun contains_rule r rls = 
wneuper@59263
   445
  let 
wneuper@59263
   446
    fun (*find (_, Rls_ rls) = finds (get_rules rls)
wneuper@59263
   447
      | find r12 = eq_rule r12
wneuper@59263
   448
    and*) finds [] = false
wneuper@59416
   449
    | finds (r1 :: rs) = if Rule.eq_rule (r, r1) then true else finds rs
wneuper@59263
   450
  in 
wneuper@59416
   451
    finds (Rule.get_rules rls) 
wneuper@59263
   452
  end
neuper@37906
   453
wneuper@59263
   454
(* try if a rewrite-rule is applicable to a given formula; 
wneuper@59263
   455
   in case of rule-sets (recursivley) collect all _atomic_ rewrites *) 
wneuper@59416
   456
fun try_rew thy ((_, ro) : Rule.rew_ord) erls (subst : Rule.subst) f (thm' as Rule.Thm (_, thm)) =
walther@59618
   457
    if Auto_Prog.contains_bdv thm
wneuper@59380
   458
    then case Rewrite.rewrite_inst_ thy ro erls false subst thm f of
wneuper@59571
   459
	    SOME _ => [Tactic.rule2tac thy subst thm']
wneuper@59263
   460
	  | NONE => []
wneuper@59380
   461
    else (case Rewrite.rewrite_ thy ro erls false thm f of
wneuper@59571
   462
	    SOME _ => [Tactic.rule2tac thy [] thm']
wneuper@59263
   463
	  | NONE => [])
walther@59773
   464
  | try_rew thy _ _ _ f (cal as Rule.Num_Calc c) = 
walther@59773
   465
    (case Num_Calc.adhoc_thm thy c f of
wneuper@59571
   466
	    SOME _ => [Tactic.rule2tac thy [] cal]
wneuper@59263
   467
    | NONE => [])
wneuper@59416
   468
  | try_rew thy _ _ _ f (cal as Rule.Cal1 c) = 
walther@59773
   469
    (case Num_Calc.adhoc_thm thy c f of
wneuper@59571
   470
	      SOME _ => [Tactic.rule2tac thy [] cal]
neuper@37926
   471
      | NONE => [])
wneuper@59416
   472
  | try_rew thy _ _ subst f (Rule.Rls_ rls) = filter_appl_rews thy subst f rls
wneuper@59263
   473
  | try_rew _ _ _ _ _ _ = error "try_rew: uncovered case"
wneuper@59416
   474
and filter_appl_rews thy subst f (Rule.Rls {rew_ord = ro, erls, rules, ...}) = 
wneuper@59571
   475
    gen_distinct Tactic.eq_tac (flat (map (try_rew thy ro erls subst f) rules))
wneuper@59416
   476
  | filter_appl_rews thy subst f (Rule.Seq {rew_ord = ro, erls, rules,...}) = 
wneuper@59571
   477
    gen_distinct Tactic.eq_tac (flat (map (try_rew thy ro erls subst f) rules))
wneuper@59416
   478
  | filter_appl_rews _ _ _ (Rule.Rrls _) = []
wneuper@59263
   479
  | filter_appl_rews _ _ _ _ = error "filter_appl_rews: uncovered case"
neuper@37906
   480
wneuper@59263
   481
(* decide if a tactic is applicable to a given formula; 
wneuper@59263
   482
   in case of Rewrite_Set* go down to _atomic_ rewrite-tactics *)
wneuper@59571
   483
fun atomic_appl_tacs thy _ _ f (Tactic.Calculate scrID) =
walther@59773
   484
    try_rew thy Rule.e_rew_ordX Rule.e_rls [] f (Rule.Num_Calc (assoc_calc' thy scrID |> snd))
wneuper@59571
   485
  | atomic_appl_tacs thy ro erls f (Tactic.Rewrite thm'') =
wneuper@59416
   486
    try_rew thy (ro, Rule.assoc_rew_ord ro) erls [] f (Rule.Thm thm'')
wneuper@59571
   487
  | atomic_appl_tacs thy ro erls f (Tactic.Rewrite_Inst (subs, thm'')) =
wneuper@59416
   488
    try_rew thy (ro, Rule.assoc_rew_ord ro) erls (Selem.subs2subst thy subs) f (Rule.Thm thm'')
neuper@37906
   489
wneuper@59571
   490
  | atomic_appl_tacs thy _ _ f (Tactic.Rewrite_Set rls') =
neuper@37906
   491
    filter_appl_rews thy [] f (assoc_rls rls')
wneuper@59571
   492
  | atomic_appl_tacs thy _ _ f (Tactic.Rewrite_Set_Inst (subs, rls')) =
wneuper@59301
   493
    filter_appl_rews thy (Selem.subs2subst thy subs) f (assoc_rls rls')
neuper@37906
   494
  | atomic_appl_tacs _ _ _ _ tac = 
walther@59846
   495
    (tracing ("### atomic_appl_tacs: not impl. for tac = '" ^ Tactic.input_to_string tac ^ "'"); []);
neuper@37906
   496
wneuper@59263
   497
(* filenames not only for thydata, but also for thy's etc *)
wneuper@59405
   498
fun theID2filename theID = Celem.theID2guh theID ^ ".xml"
neuper@37906
   499
wneuper@59405
   500
fun guh2theID guh =
wneuper@59263
   501
  let
wneuper@59263
   502
    val guh' = Symbol.explode guh
wneuper@59263
   503
    val part = implode (take_fromto 1 4 guh')
wneuper@59263
   504
    val isa = implode (take_fromto 5 9 guh')
wneuper@59263
   505
  in
wneuper@59263
   506
    if not (member op = ["exp_", "thy_", "pbl_", "met_"] part)
wneuper@59263
   507
    then raise ERROR ("guh '" ^ guh ^ "' does not begin with exp_ | thy_ | pbl_ | met_")
wneuper@59263
   508
    else
wneuper@59263
   509
      let
wneuper@59263
   510
  	    val chap = case isa of
wneuper@59263
   511
  		  "isab_" => "Isabelle"
wneuper@59263
   512
  		| "scri_" => "IsacScripts"
wneuper@59263
   513
  		| "isac_" => "IsacKnowledge"
wneuper@59263
   514
  		| _ =>
wneuper@59263
   515
  		  raise ERROR ("guh2theID: '" ^ guh ^ "' does not have isab_ | scri_ | isac_ at position 5..9")
wneuper@59263
   516
        val rest = takerest (9, guh') 
wneuper@59263
   517
        val thyID = takewhile [] (not o (curry op= "-")) rest
wneuper@59263
   518
        val rest' = dropuntil (curry op = "-") rest
neuper@37906
   519
	    in case implode rest' of
wneuper@59405
   520
		    "-part" => [chap] : Celem.theID
wneuper@59263
   521
      | "" => [chap, implode thyID]
wneuper@59263
   522
      | "-Theorems" => [chap, implode thyID, "Theorems"]
wneuper@59263
   523
      | "-Rulesets" => [chap, implode thyID, "Rulesets"]
wneuper@59263
   524
      | "-Operations" => [chap, implode thyID, "Operations"]
wneuper@59263
   525
      | "-Orders" => [chap, implode thyID, "Orders"]
wneuper@59263
   526
      | _ => 
wneuper@59263
   527
		    let val sect = implode (take_fromto 1 5 rest')
wneuper@59263
   528
		       val sect' = case sect of
neuper@37906
   529
			       "-thm-" => "Theorems"
neuper@37906
   530
			     | "-rls-" => "Rulesets"
neuper@37906
   531
			     | "-cal-" => "Operations"
neuper@37906
   532
			     | "-ord-" => "Orders"
wneuper@59263
   533
			     | _ =>
wneuper@59263
   534
			     raise ERROR ("guh2theID: '" ^ guh ^ "' has '" ^ sect ^ "' instead -thm- | -rls- | -cal- | -ord-")
wneuper@59263
   535
		    in
wneuper@59263
   536
		      [chap, implode thyID, sect', implode (takerest (5, rest'))]
wneuper@59263
   537
		    end
neuper@37906
   538
	    end	
wneuper@59263
   539
    end
neuper@37906
   540
wneuper@59405
   541
fun guh2filename guh = guh ^ ".xml";
neuper@37906
   542
wneuper@59301
   543
fun guh2rewtac guh [] =
wneuper@59263
   544
    let
wneuper@59263
   545
      val (_, thy, sect, xstr) = case guh2theID guh of
wneuper@59263
   546
        [isa, thy, sect, xstr] => (isa, thy, sect, xstr)
wneuper@59263
   547
      | _ => error "guh2rewtac: uncovered case"
wneuper@59263
   548
    in case sect of
wneuper@59571
   549
      "Theorems" => Tactic.Rewrite (xstr, Rewrite.assoc_thm'' (Celem.assoc_thy thy) xstr)
wneuper@59571
   550
    | "Rulesets" => Tactic.Rewrite_Set xstr
wneuper@59263
   551
    | _ => error ("guh2rewtac: not impl. for '"^xstr^"'") 
wneuper@59263
   552
    end
wneuper@59263
   553
  | guh2rewtac guh subs =
wneuper@59263
   554
    let
wneuper@59263
   555
      val (_, thy, sect, xstr) = case guh2theID guh of
wneuper@59263
   556
        [isa, thy, sect, xstr] => (isa, thy, sect, xstr)
wneuper@59263
   557
      | _ => error "guh2rewtac: uncovered case"
wneuper@59263
   558
    in case sect of
wneuper@59263
   559
      "Theorems" => 
wneuper@59571
   560
        Tactic.Rewrite_Inst (subs, (xstr, Rewrite.assoc_thm'' (Celem.assoc_thy thy) xstr))
wneuper@59571
   561
    | "Rulesets" => Tactic.Rewrite_Set_Inst (subs,  xstr)
wneuper@59263
   562
    | str => error ("guh2rewtac: not impl. for '" ^ str ^ "'") 
wneuper@59263
   563
    end
neuper@37906
   564
wneuper@59263
   565
(* the front-end may request a context for any element of the hierarchy *)
wneuper@59405
   566
fun no_thycontext guh = (guh2theID guh; false)
wneuper@59263
   567
  handle ERROR _ => true;
neuper@37906
   568
wneuper@59263
   569
(* get the substitution of bound variables for matchTheory:
neuper@37906
   570
   # lookup the thm|rls' in the script
neuper@37906
   571
   # take the [(bdv, v_),..] from the respective Rewrite_(Set_)Inst
neuper@37906
   572
   # instantiate this subs with the istates env to [(bdv, x),..]
wneuper@59263
   573
   # otherwise []
wneuper@59263
   574
   WN060617 hack assuming that all scripts use only one bound variable
wneuper@59263
   575
   and use 'v_' as the formal argument for this bound variable*)
walther@59685
   576
fun subs_from (Istate.Pstate (pst as {env, ...})) _ guh =
wneuper@59263
   577
    let
wneuper@59263
   578
      val (_, _, thyID, sect, xstr) = case guh2theID guh of
wneuper@59263
   579
        theID as [isa, thyID, sect, xstr] => (theID, isa, thyID, sect, xstr)
wneuper@59263
   580
      | _ => error "subs_from: uncovered case"
wneuper@59263
   581
    in
wneuper@59263
   582
      case sect of
wneuper@59263
   583
        "Theorems" => 
wneuper@59416
   584
          let val thm = Global_Theory.get_thm (Celem.assoc_thy (Rule.thyID2theory' thyID)) xstr
wneuper@59263
   585
          in
walther@59618
   586
            if Auto_Prog.contains_bdv thm
wneuper@59263
   587
            then
wneuper@59263
   588
              let
walther@59676
   589
                val formal_arg = TermC.str2term "v_"      
walther@59685
   590
                val value = subst_atomic env formal_arg
wneuper@59497
   591
              in ["(''bdv''," ^ Rule.term2str value ^ ")"] : Selem.subs end
wneuper@59263
   592
            else []
wneuper@59263
   593
	        end
wneuper@59263
   594
  	  | "Rulesets" => 
wneuper@59263
   595
        let
wneuper@59416
   596
          val rules = (Rule.get_rules o assoc_rls) xstr
wneuper@59263
   597
        in
walther@59618
   598
          if Auto_Prog.contain_bdv rules
wneuper@59263
   599
          then
wneuper@59263
   600
            let
wneuper@59389
   601
              val formal_arg = TermC.str2term "v_"
walther@59685
   602
              val value = subst_atomic env formal_arg
wneuper@59497
   603
            in ["(''bdv''," ^ Rule.term2str value ^ ")"] : Selem.subs end
wneuper@59263
   604
          else []
wneuper@59263
   605
        end
wneuper@59263
   606
      | str => error ("subs_from: uncovered case with " ^ str)
wneuper@59263
   607
    end
wneuper@59405
   608
  | subs_from _ _  guh = error ("subs_from: uncovered case with " ^ guh)
neuper@37906
   609
neuper@42433
   610
(* get a substitution for "bdv*" from the current program and environment.
wneuper@59494
   611
    returns a substitution: sube for tac, subst for tac_, i.e. for rewriting *)
neuper@42433
   612
fun get_bdv_subst prog env =
neuper@42433
   613
  let
neuper@42433
   614
    fun scan (Const _) = NONE
neuper@42433
   615
      | scan (Free _) = NONE
neuper@42433
   616
      | scan (Var _) = NONE
neuper@42433
   617
      | scan (Bound _) = NONE
wneuper@59494
   618
      | scan (t as Const ("List.list.Cons", _) $ (Const ("Product_Type.Pair", _) $ _ $ _) $ _) =
wneuper@59389
   619
        if TermC.is_bdv_subst t then SOME t else NONE
neuper@42433
   620
      | scan (Abs (_, _, body)) = scan body
wneuper@59263
   621
      | scan (t1 $ t2) = case scan t1 of NONE => scan t2 | SOME subst => SOME subst
neuper@42433
   622
  in
neuper@42433
   623
    case scan prog of
wneuper@59416
   624
      NONE => (NONE: Selem.subs option, []: Rule.subst)
neuper@42433
   625
    | SOME tm =>
wneuper@59494
   626
        let val subst = subst_atomic env tm
wneuper@59494
   627
        in (SOME (Selem.subst'_to_sube subst), Selem.subst'_to_subst subst) end
wneuper@59263
   628
  end
neuper@42433
   629
wneuper@59263
   630
end