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